UNPKG

@harvest-profit/doc-flux-spreadsheets

Version:
118 lines (86 loc) 4.32 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _saveAs = require('save-as'); var _saveAs2 = _interopRequireDefault(_saveAs); var _xlsx = require('xlsx'); var XLSX = _interopRequireWildcard(_xlsx); var _docFlux = require('@harvest-profit/doc-flux'); var _Parser = require('./Parser'); var _Parser2 = _interopRequireDefault(_Parser); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * The generated document object. Just call download * @module GeneratedDocument */ var GeneratedDocument = function () { function GeneratedDocument(doc, settings) { _classCallCheck(this, GeneratedDocument); var bookType = settings.type || 'csv'; var filename = settings.name || 'Untitled'; var wopts = { bookType: bookType, type: 'array' }; this.filename = filename; this.extension = bookType; this.doc = XLSX.write(doc, wopts); } /** * Downloads the document * @function */ _createClass(GeneratedDocument, [{ key: 'download', value: function () { function download() { /* the saveAs call downloads a file on the local machine */ (0, _saveAs2['default'])(new Blob([this.doc], { type: 'application/octet-stream' }), String(this.filename) + '.' + String(this.extension)); } return download; }() }]); return GeneratedDocument; }(); /** * The DocFlux Document (exported as Document). * @module SpreadSheetDocument */ var SpreadSheetDocument = function (_Document) { _inherits(SpreadSheetDocument, _Document); function SpreadSheetDocument() { _classCallCheck(this, SpreadSheetDocument); return _possibleConstructorReturn(this, (SpreadSheetDocument.__proto__ || Object.getPrototypeOf(SpreadSheetDocument)).apply(this, arguments)); } _createClass(SpreadSheetDocument, null, [{ key: 'documentSettings', value: function () { function documentSettings() { return {}; } return documentSettings; }() }, { key: 'createDocument', value: function () { function createDocument(doc, props) { return new GeneratedDocument(doc, this.documentSettings(props)); } return createDocument; }() }, { key: 'createBuilder', value: function () { function createBuilder() { return XLSX.utils.book_new(); } return createBuilder; }() }]); return SpreadSheetDocument; }(_docFlux.Document); SpreadSheetDocument.parser = _Parser2['default']; exports['default'] = SpreadSheetDocument;