From f99d3d7b22f1f103bc00082a2d4eba2fd05d9a7b Mon Sep 17 00:00:00 2001 From: Travis Clarke Date: Mon, 1 May 2017 22:50:36 -0700 Subject: [PATCH] bump v4.0.0-alpha.5 --- .gitignore | 1 - README.md | 8 +- bower.json | 2 +- dist/css/tableexport.css | 2 +- dist/css/tableexport.min.css | 2 +- dist/js/tableexport.js | 228 ++++++++++++++++++++++------ dist/js/tableexport.min.js | 4 +- dist/typings/tableexport.d.ts | 42 ++++- package.json | 2 +- src/stable/css/tableexport.css | 2 +- src/stable/css/tableexport.min.css | 2 +- src/stable/js/tableexport.js | 99 ++++++++---- src/stable/js/tableexport.min.js | 4 +- src/stable/typings/tableexport.d.ts | 42 ++++- 14 files changed, 343 insertions(+), 97 deletions(-) diff --git a/.gitignore b/.gitignore index c8623b4..2f7f057 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ .idea/ *.iml /assets -/.gitignore /index.html /search /test diff --git a/README.md b/README.md index 45a2171..3d23b48 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ The simple, easy-to-implement plugin to export HTML tables to xlsx, xls, csv, an [TableExport](https://www.travismclarke.com/tableexport/) demo **--** [TableExport + RequireJS](https://github.com/clarketm/tableexport_requirejs_app) skeleton **--** [TableExport + Flask](https://github.com/clarketm/tableexport_flask_app) skeleton. -> **Notice:** In May 2017, [v3.0.0](https://github.com/clarketm/TableExport/releases/tag/v3.3.9) will be superceded by [v4.0.0](https://github.com/clarketm/TableExport/releases/tag/v4.0.0-alpha.4). Althought this is a major version bump, fear not, because all changes will be 100% backwards-compatible. +> **Notice:** In May 2017, [v3.0.0](https://github.com/clarketm/TableExport/releases/tag/v3.3.9) will be superceded by [v4.0.0](https://github.com/clarketm/TableExport/releases/tag/v4.0.0-alpha.5). Althought this is a major version bump, fear not, because all changes will be 100% backwards-compatible. -> **So why the major version bump you ask?** Well, the rationale for a major version bump is that due to a change in `TableExport`'s dependencies, in [v4.0.0](https://github.com/clarketm/TableExport/releases/tag/v4.0.0-alpha.4) forth, JQuery will no longer be a **required** dependency, instead it will be purely **optional**. So existing implementations *with* jQuery will continue to work unimpeded, now with the added benefit that new projets no longer need to rely on the overhead of such large library, unless of course you prefer jQuery or it is already part of your project. +> **So why the major version bump you ask?** Well, the rationale for a major version bump is that due to a change in `TableExport`'s dependencies, in [v4.0.0](https://github.com/clarketm/TableExport/releases/tag/v4.0.0-alpha.5) forth, JQuery will no longer be a **required** dependency, instead it will be purely **optional**. So existing implementations *with* jQuery will continue to work unimpeded, now with the added benefit that new projets no longer need to rely on the overhead of such large library, unless of course you prefer jQuery or it is already part of your project. ## Getting Started @@ -120,7 +120,7 @@ $("table").tableExport({ headings: true, // (Boolean), display table headings (th/td elements) in the footers: true, // (Boolean), display table footers (th/td elements) in the formats: ["xls", "csv", "txt"], // (String[]), filetype(s) for the export - fileName: "id", // (id, String), filename for the downloaded file + filename: "id", // (id, String), filename for the downloaded file bootstrap: true, // (Boolean), style buttons using bootstrap position: "bottom", // (top, bottom), position of the caption element relative to table ignoreRows: null, // (Number, Number[]), row indices to exclude from the exported file(s) @@ -142,7 +142,7 @@ var tables = $("table").tableExport(); ```js /* update */ tables.update({ - fileName: "newFile" // pass in a new set of properties + filename: "newFile" // pass in a new set of properties }); /* reset */ diff --git a/bower.json b/bower.json index 7dcb30a..b529338 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "tableexport.js", - "version": "4.0.0-alpha.4", + "version": "4.0.0-alpha.5", "authors": [ "clarketm " ], diff --git a/dist/css/tableexport.css b/dist/css/tableexport.css index 29b0114..233a601 100644 --- a/dist/css/tableexport.css +++ b/dist/css/tableexport.css @@ -1,5 +1,5 @@ /*! - * TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) + * TableExport.js 4.0.0-alpha.5 (https://www.travismclarke.com) * Copyright 2017 Travis Clarke * Licensed under the MIT license */ diff --git a/dist/css/tableexport.min.css b/dist/css/tableexport.min.css index 0fd03a4..96ae58a 100644 --- a/dist/css/tableexport.min.css +++ b/dist/css/tableexport.min.css @@ -1,5 +1,5 @@ /*! - * TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) + * TableExport.js 4.0.0-alpha.5 (https://www.travismclarke.com) * Copyright 2017 Travis Clarke * Licensed under the MIT license */.top{caption-side:top}.bottom{caption-side:bottom}.button-default,.button-default:active,.button-default:focus,.button-default:hover{text-decoration:none}.button-default{font:700 12px sans-serif;color:#222;cursor:pointer;padding:5px;margin:5px}.button-default.csv:before,.button-default.txt:before,.button-default.xls:before,.button-default.xlsx:before{content:none}.csv,.txt,.xls,.xlsx{margin:4px 0}.csv:before,.txt:before,.xls:before,.xlsx:before{margin-right:10px;padding:11px 15px 12px;box-shadow:1px 1px 2px rgba(0,0,0,.2)}.xlsx:before{content:"";background:#006400 url(../img/xlsx.svg) no-repeat center}.xls:before{content:"";background:green url(../img/xls.svg) no-repeat center}.csv:before{content:"";background:#00f url(../img/csv.svg) no-repeat center}.txt:before{content:"";background:purple url(../img/txt.svg) no-repeat center} \ No newline at end of file diff --git a/dist/js/tableexport.js b/dist/js/tableexport.js index 846f1b9..d6fd128 100644 --- a/dist/js/tableexport.js +++ b/dist/js/tableexport.js @@ -1,5 +1,5 @@ /*! - * TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) + * TableExport.js 4.0.0-alpha.5 (https://www.travismclarke.com) * Copyright 2017 Travis Clarke * Licensed under the MIT license */ @@ -15,7 +15,7 @@ // Browser globals factory(root, root.jQuery, root.Blob, root.saveAs, root.XLSX); } -}(this, function (exports, $, Blob, saveAs, XLSX) { +}(this || window, function (exports, $, Blob, saveAs, XLSX) { 'use strict'; /** * TableExport main plugin constructor @@ -41,6 +41,9 @@ ignoreCols = self.settings.ignoreCols instanceof Array ? self.settings.ignoreCols : [self.settings.ignoreCols], ignoreCSS = self.settings.ignoreCSS instanceof Array ? self.settings.ignoreCSS.join(", ") : self.settings.ignoreCSS, emptyCSS = self.settings.emptyCSS instanceof Array ? self.settings.emptyCSS.join(", ") : self.settings.emptyCSS, + formatValue = TableExport.prototype.formatValue.bind(this, self.settings.trimWhitespace), + getType = TableExport.prototype.getType, + store = new TableExport.prototype.LocalStorage.getInstance(), bootstrapClass, bootstrapTheme, bootstrapSpacing; if (self.settings.bootstrap) { @@ -97,10 +100,18 @@ break; } } - return new Array(total).concat($(val).text()); + return new Array(total).concat({ + v: formatValue(val.textContent), + t: getType(val.className) + }); } - return val.textContent; + return { + v: formatValue(val.textContent), + t: getType(val.className) + }; }); + }).map(function (val, ir) { + return [].concat.apply([], val); }), dataObject = TableExport.prototype.escapeHtml( JSON.stringify({ @@ -148,10 +159,18 @@ break; } } - return new Array(total).concat($(val).text()); + return new Array(total).concat({ + v: formatValue(val.textContent), + t: getType(val.className) + }); } - return val.textContent; + return { + v: formatValue(val.textContent), + t: getType(val.className) + }; }); + }).map(function (val, ir) { + return [].concat.apply([], val); }), dataObject = TableExport.prototype.escapeHtml( JSON.stringify({ @@ -178,9 +197,14 @@ if (_hasClass(val, emptyCSS)) { return " " } - return val.textContent; + return { + v: formatValue(val.textContent), + t: getType(val.className) + }; }).join(colD); - }).join(rdel), + }).join(rdel).map(function (val, ir) { + return [].concat.apply([], val); + }), dataObject = TableExport.prototype.escapeHtml( JSON.stringify({ data: dataURL, @@ -206,7 +230,7 @@ if (_hasClass(val, emptyCSS)) { return " " } - return '"' + val.textContent.replace(/"/g, '""') + '"'; + return '"' + formatValue(val.textContent.replace(/"/g, '""')) + '"'; }).join(colD); }).join(rdel), dataObject = TableExport.prototype.escapeHtml( @@ -234,7 +258,7 @@ if (_hasClass(val, emptyCSS)) { return " " } - return val.textContent; + return formatValue(val.textContent); }).join(colD); }).join(rdel), dataObject = TableExport.prototype.escapeHtml( @@ -258,6 +282,10 @@ } ); + /** + * Initializes table caption with export buttons + * @param exportButton {HTMLButtonElement} + */ function checkCaption(exportButton) { var caption = el.querySelectorAll('caption:not(.head)'); if (caption.length) { @@ -270,9 +298,17 @@ } } + /** + * Creates file export buttons + * @param dataObject {JSON} + * @param myContent {String} + * @param myClass {String} + */ function createObjButton(dataObject, myContent, myClass) { var exportButton = document.createElement('button'); - exportButton.setAttribute('data-fileblob', dataObject); + var uuid = _uuid(); + exportButton.setAttribute('data-fileblob', uuid); + store.setItem(uuid, dataObject, true); exportButton.className = bootstrapClass + bootstrapTheme + myClass; exportButton.textContent = myContent; checkCaption(exportButton); @@ -281,7 +317,7 @@ var exportButton = document.querySelectorAll("button[data-fileblob]"); _on(exportButton, "click", function () { - var object = JSON.parse(this.getAttribute("data-fileblob")), + var object = JSON.parse(store.getItem(this.getAttribute("data-fileblob"))), data = object.data, filename = object.filename, mimeType = object.mimeType, @@ -297,7 +333,7 @@ * Version. * @memberof TableExport.prototype */ - version: "4.0.0-alpha.4", + version: "4.0.0-alpha.5", /** * Default plugin options. * @memberof TableExport.prototype @@ -309,10 +345,11 @@ filename: "id", // (id, String), filename for the downloaded file, (default: "id") bootstrap: true, // (Boolean), style buttons using bootstrap, (default: true) position: "bottom", // (top, bottom), position of the caption element relative to table, (default: "bottom") - ignoreRows: null, // (Number, Number[]), row indices to exclude from the exported file (default: null) - ignoreCols: null, // (Number, Number[]), column indices to exclude from the exported file (default: null) - ignoreCSS: ".tableexport-ignore", // (selector, selector[]), selector(s) to exclude cells from the exported file (default: ".tableexport-ignore") - emptyCSS: ".tableexport-empty" // (selector, selector[]), selector(s) to replace cells with an empty string in the exported file (default: ".tableexport-empty") + ignoreRows: null, // (Number, Number[]), row indices to exclude from the exported file(s) (default: null) + ignoreCols: null, // (Number, Number[]), column indices to exclude from the exported file(s) (default: null) + ignoreCSS: ".tableexport-ignore", // (selector, selector[]), selector(s) to exclude cells from the exported file(s) (default: ".tableexport-ignore") + emptyCSS: ".tableexport-empty", // (selector, selector[]), selector(s) to replace cells with an empty string in the exported file(s) (default: ".tableexport-empty") + trimWhitespace: false // (Boolean), remove all leading/trailing newlines, spaces, and tabs from cell text in the exported file(s) (default: false) }, /** * Character set (character encoding) of the HTML. @@ -387,6 +424,33 @@ mimeType: "text/plain", fileExtension: ".txt" }, + /** + * Cell-types override and assertion configuration + * @memberof TableExport.prototype + */ + types: { + string: { + defaultClass: "tableexport-string" + }, + number: { + defaultClass: "tableexport-number", + assert: function (v) { + return !isNaN(v.replace(/,/g, '')); + } + }, + boolean: { + defaultClass: "tableexport-boolean", + assert: function (v) { + return v.toLowerCase() === 'true' || v.toLowerCase() === 'false'; + } + }, + date: { + defaultClass: "tableexport-date", + assert: function (v) { + return !isNaN(Date.parse(v)) + } + } + }, /** * Escapes special characters with HTML entities * @memberof TableExport.prototype @@ -398,6 +462,35 @@ return TableExport.prototype.entityMap[s]; }); }, + /** + * Removes leading/trailing whitespace from cell string + * @param isTrimWhitespace {Boolean} + * @param string {String} + * @returns {String} trimmed string + */ + formatValue: function (isTrimWhitespace, string) { + return isTrimWhitespace ? string.trim() : string; + }, + /** + * Get cell data-type + * @param string {String} + * @returns {String} data-type + */ + getType: function (string) { + if (!string) return ''; + var types = TableExport.prototype.types; + if (~string.indexOf(types.string.defaultClass)) { + return 's'; + } else if (~string.indexOf(types.number.defaultClass)) { + return 'n'; + } else if (~string.indexOf(types.boolean.defaultClass)) { + return 'b'; + } else if (~string.indexOf(types.date.defaultClass)) { + return 'd'; + } else { + return ''; + } + }, /** * Formats datetimes for compatibility with Excel * @memberof TableExport.prototype @@ -414,29 +507,33 @@ * Creates an Excel spreadsheet from a data string * @memberof TableExport.prototype * @param data {String} - * @returns {Number} epoch time */ createSheet: function (data) { var ws = {}; var range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}}; - for (var R = 0; R != data.length; ++R) { - for (var C = 0; C != data[R].length; ++C) { + var types = TableExport.prototype.types; + for (var R = 0; R !== data.length; ++R) { + for (var C = 0; C !== data[R].length; ++C) { if (range.s.r > R) range.s.r = R; if (range.s.c > C) range.s.c = C; if (range.e.r < R) range.e.r = R; if (range.e.c < C) range.e.c = C; - var cell = {v: data[R][C]}; - if (cell.v == null) continue; + var cell = data[R][C]; + if (!cell || !cell.v) continue; var cell_ref = XLSX.utils.encode_cell({c: C, r: R}); - if (typeof cell.v === 'number') cell.t = 'n'; - else if (typeof cell.v === 'boolean') cell.t = 'b'; - else if (cell.v instanceof Date) { + if (!cell.t) { + if (types.number.assert(cell.v)) cell.t = 'n'; + else if (types.boolean.assert(cell.v)) cell.t = 'b'; + else if (types.date.assert(cell.v)) cell.t = 'd'; + else cell.t = 's'; + } + + if (cell.t === 'd') { cell.t = 'n'; cell.z = XLSX.SSF._table[14]; cell.v = this.dateNum(cell.v); } - else cell.t = 's'; ws[cell_ref] = cell; } @@ -462,7 +559,7 @@ string2ArrayBuffer: function (s) { var buf = new ArrayBuffer(s.length); var view = new Uint8Array(buf); - for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; + for (var i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; return buf; }, /** @@ -474,7 +571,7 @@ * @param extension {String} file extension */ export2file: function (data, mime, name, extension) { - if (XLSX && extension.substr(0, 4) == ".xls") { + if (XLSX && extension.substr(0, 4) === (".xls")) { var wb = new this.Workbook(), ws = this.createSheet(data); @@ -494,31 +591,62 @@ name + extension, true); }, /** - * Updates the plugin instance with new/updated options - * @param options {Object} TableExport configuration options - * @returns {TableExport} updated TableExport instance - */ - update: function (options) { - return new TableExport(this.selectors, _extend({}, this.settings, options), true); - }, - /** - * Reset the plugin instance to its original state - * @returns {TableExport} original TableExport instance - */ - reset: function () { - return new TableExport(this.selectors, this.settings, true); - }, - /** - * Remove the instance (i.e. caption containing the export buttons) + * LocalStorage main interface constructor + * @memberof TableExport.prototype + * @constructor */ - remove: function () { - this.selectors.each(function () { - var caption = this.querySelectorAll('caption:not(.head)'); - caption.parentNode.removeChild(caption); - }); + LocalStorage: function () { + this.type = 'localStorage'; + this.store = exports[this.type]; + this.namespace = 'te'; + this.getKey = function (key) { + return this.namespace + key; + }; + this.setItem = function (_key, value, overwrite) { + var key = this.getKey(_key); + if (this.exists(key) && !overwrite) { + return; + } + return this.store.setItem(key, value); + }; + this.getItem = function (_key) { + var key = this.getKey(_key); + return this.store.getItem(key); + }; + this.exists = function (_key) { + var key = this.getKey(_key); + return this.store.getItem(key) !== null; + }; + this.removeItem = function (_key) { + var key = this.getKey(_key); + return this.store.removeItem(key); + }; + this.error = function (message) { + return new Error('unknown error occurred', message); + }; + } + }; + + var _store = TableExport.prototype.LocalStorage; + _store._instance = null; + _store.getInstance = function () { + if (!_store._instance) { + _store._instance = new _store(); } + return _store._instance; }; + function _uuid() { + function s4() { + return Math.floor((1 + Math.random()) * 0x10000) + .toString(16) + .substring(1); + } + + return s4() + s4() + '-' + s4() + '-' + s4() + '-' + + s4() + '-' + s4() + s4() + s4(); + } + function _extend() { var args = arguments; for (var i = 1; i < args.length; i++) @@ -559,7 +687,7 @@ } } - exports.default = exports.TableExport = TableExport; + return exports.default = exports.TableExport = TableExport; } )); diff --git a/dist/js/tableexport.min.js b/dist/js/tableexport.min.js index 7043dfc..75109ec 100644 --- a/dist/js/tableexport.min.js +++ b/dist/js/tableexport.min.js @@ -1,6 +1,6 @@ /*! - * TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) + * TableExport.js 4.0.0-alpha.5 (https://www.travismclarke.com) * Copyright 2017 Travis Clarke * Licensed under the MIT license */ -!function(t,e){"function"==typeof define&&define.amd?define(["exports","jquery","blobjs","file-saverjs","xlsx-js"],e):"object"==typeof exports&&"string"!=typeof exports.nodeName?e(exports,require("jquery"),require("blobjs"),require("file-saverjs"),require("xlsx-js")):e(t,t.jQuery,t.Blob,t.saveAs,t.XLSX)}(this,function(t,e,n,o,r){"use strict";function s(){for(var t=arguments,e=1;e tr")),b=f.settings.headings?i(t.querySelectorAll("thead > tr")).concat(b):b,b=f.settings.footers?i(t.querySelectorAll("tfoot > tr")).concat(b):b,v=f.settings.headings?t.querySelectorAll("thead > tr").length:0,S="id"===f.settings.filename?t.getAttribute("id")?t.getAttribute("id"):p.prototype.defaultFilename:f.settings.filename,C={xlsx:function(t,n){var o={},r=i(b).map(function(t,n){if(!~m.indexOf(n-v)&&!l(t,h)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!l(t,h)){if(l(t,g))return" ";if(t.hasAttribute("colspan")&&(o[n]=o[n]||{},o[n][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var s=1;s=r?a+o[n][s]:a:p++,p!==i);s++);return new Array(a).concat(e(t).text())}return t.textContent}})}}),a=p.prototype.escapeHtml(JSON.stringify({data:r,filename:n,mimeType:p.prototype.xlsx.mimeType,fileExtension:p.prototype.xlsx.fileExtension})),f=p.prototype.xlsx.buttonContent,u=p.prototype.xlsx.defaultClass;s(a,f,u)},xlsm:function(t,n){var o={},r=i(b).map(function(t,n){if(!~m.indexOf(n-v)&&!l(t,h)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!l(t,h)){if(l(t,g))return" ";if(t.hasAttribute("colspan")&&(o[n]=o[n]||{},o[n][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var s=1;s=r?a+o[n][s]:a:p++,p!==i);s++);return new Array(a).concat(e(t).text())}return t.textContent}})}}),a=p.prototype.escapeHtml(JSON.stringify({data:r,filename:n,mimeType:p.prototype.xls.mimeType,fileExtension:p.prototype.xls.fileExtension})),f=p.prototype.xls.buttonContent,u=p.prototype.xls.defaultClass;s(a,f,u)},xls:function(t,e){var n=p.prototype.xls.separator,o=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,h)){var o=t.querySelectorAll("th, td");return i(o).map(function(t,e){if(!~d.indexOf(e)&&!l(t,h))return l(t,g)?" ":t.textContent}).join(n)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:o,filename:e,mimeType:p.prototype.xls.mimeType,fileExtension:p.prototype.xls.fileExtension})),a=p.prototype.xls.buttonContent,f=p.prototype.xls.defaultClass;s(r,a,f)},csv:function(t,e){var n=p.prototype.csv.separator,o=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,h)){var o=t.querySelectorAll("th, td");return i(o).map(function(t,e){if(!~d.indexOf(e)&&!l(t,h))return l(t,g)?" ":'"'+t.textContent.replace(/"/g,'""')+'"'}).join(n)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:o,filename:e,mimeType:p.prototype.csv.mimeType,fileExtension:p.prototype.csv.fileExtension})),a=p.prototype.csv.buttonContent,f=p.prototype.csv.defaultClass;s(r,a,f)},txt:function(t,e){var n=p.prototype.txt.separator,o=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,h)){var o=t.querySelectorAll("th, td");return i(o).map(function(t,e){if(!~d.indexOf(e)&&!l(t,h))return l(t,g)?" ":t.textContent}).join(n)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:o,filename:e,mimeType:p.prototype.txt.mimeType,fileExtension:p.prototype.txt.fileExtension})),a=p.prototype.txt.buttonContent,f=p.prototype.txt.defaultClass;s(r,a,f)}};f.settings.formats.forEach(function(t){!(!r||"xls"!==t)&&(t="xlsm"),!r&&"xlsx"===t&&(t=null),t&&C[t](y,S)})});var b=document.querySelectorAll("button[data-fileblob]");return a(b,"click",function(){var t=JSON.parse(this.getAttribute("data-fileblob")),e=t.data,n=t.filename,o=t.mimeType,r=t.fileExtension;p.prototype.export2file(e,o,n,r)}),f};if(p.prototype={version:"4.0.0-alpha.4",defaults:{headings:!0,footers:!0,formats:["xls","csv","txt"],filename:"id",bootstrap:!0,position:"bottom",ignoreRows:null,ignoreCols:null,ignoreCSS:".tableexport-ignore",emptyCSS:".tableexport-empty"},charset:"charset=utf-8",defaultFilename:"myDownload",defaultButton:"button-default",bootstrap:["btn","btn-default","btn-toolbar"],rowDel:"\r\n",entityMap:{"&":"&","<":"<",">":">","'":"'","/":"/"},xlsx:{defaultClass:"xlsx",buttonContent:"Export to xlsx",mimeType:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",fileExtension:".xlsx"},xls:{defaultClass:"xls",buttonContent:"Export to xls",separator:"\t",mimeType:"application/vnd.ms-excel",fileExtension:".xls"},csv:{defaultClass:"csv",buttonContent:"Export to csv",separator:",",mimeType:"text/csv",fileExtension:".csv"},txt:{defaultClass:"txt",buttonContent:"Export to txt",separator:" ",mimeType:"text/plain",fileExtension:".txt"},escapeHtml:function(t){return String(t).replace(/[&<>'\/]/g,function(t){return p.prototype.entityMap[t]})},dateNum:function(t,e){e&&(t+=1462);var n=Date.parse(t);return(n-new Date(Date.UTC(1899,11,30)))/864e5},createSheet:function(t){for(var e={},n={s:{c:1e7,r:1e7},e:{c:0,r:0}},o=0;o!=t.length;++o)for(var s=0;s!=t[o].length;++s){n.s.r>o&&(n.s.r=o),n.s.c>s&&(n.s.c=s),n.e.r tr")),S=r.settings.headings?i(t.querySelectorAll("thead > tr")).concat(S):S,S=r.settings.footers?i(t.querySelectorAll("tfoot > tr")).concat(S):S,A=r.settings.headings?t.querySelectorAll("thead > tr").length:0,w="id"===r.settings.filename?t.getAttribute("id")?t.getAttribute("id"):u.prototype.defaultFilename:r.settings.filename,E={xlsx:function(t,e){var n={},r=i(S).map(function(t,e){if(!~x.indexOf(e-A)&&!p(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!p(t,g)){if(p(t,h))return" ";if(t.hasAttribute("colspan")&&(n[e]=n[e]||{},n[e][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var o=1;o=r?a+n[e][o]:a:i++,i!==s);o++);return new Array(a).concat({v:b(t.textContent),t:v(t.className)})}return{v:b(t.textContent),t:v(t.className)}}})}}).map(function(t,e){return[].concat.apply([],t)}),o=u.prototype.escapeHtml(JSON.stringify({data:r,filename:e,mimeType:u.prototype.xlsx.mimeType,fileExtension:u.prototype.xlsx.fileExtension})),s=u.prototype.xlsx.buttonContent,l=u.prototype.xlsx.defaultClass;a(o,s,l)},xlsm:function(t,e){var n={},r=i(S).map(function(t,e){if(!~x.indexOf(e-A)&&!p(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!p(t,g)){if(p(t,h))return" ";if(t.hasAttribute("colspan")&&(n[e]=n[e]||{},n[e][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var o=1;o=r?a+n[e][o]:a:i++,i!==s);o++);return new Array(a).concat({v:b(t.textContent),t:v(t.className)})}return{v:b(t.textContent),t:v(t.className)}}})}}).map(function(t,e){return[].concat.apply([],t)}),o=u.prototype.escapeHtml(JSON.stringify({data:r,filename:e,mimeType:u.prototype.xls.mimeType,fileExtension:u.prototype.xls.fileExtension})),s=u.prototype.xls.buttonContent,l=u.prototype.xls.defaultClass;a(o,s,l)},xls:function(t,e){var n=u.prototype.xls.separator,r=i(S).map(function(t,e){if(!~x.indexOf(e-A)&&!p(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,e){if(!~d.indexOf(e)&&!p(t,g))return p(t,h)?" ":{v:b(t.textContent),t:v(t.className)}}).join(n)}}).join(t).map(function(t,e){return[].concat.apply([],t)}),o=u.prototype.escapeHtml(JSON.stringify({data:r,filename:e,mimeType:u.prototype.xls.mimeType,fileExtension:u.prototype.xls.fileExtension})),s=u.prototype.xls.buttonContent,l=u.prototype.xls.defaultClass;a(o,s,l)},csv:function(t,e){var n=u.prototype.csv.separator,r=i(S).map(function(t,e){if(!~x.indexOf(e-A)&&!p(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,e){if(!~d.indexOf(e)&&!p(t,g))return p(t,h)?" ":'"'+b(t.textContent.replace(/"/g,'""'))+'"'}).join(n)}}).join(t),o=u.prototype.escapeHtml(JSON.stringify({data:r,filename:e,mimeType:u.prototype.csv.mimeType,fileExtension:u.prototype.csv.fileExtension})),s=u.prototype.csv.buttonContent,l=u.prototype.csv.defaultClass;a(o,s,l)},txt:function(t,e){var n=u.prototype.txt.separator,r=i(S).map(function(t,e){if(!~x.indexOf(e-A)&&!p(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,e){if(!~d.indexOf(e)&&!p(t,g))return p(t,h)?" ":b(t.textContent)}).join(n)}}).join(t),o=u.prototype.escapeHtml(JSON.stringify({data:r,filename:e,mimeType:u.prototype.txt.mimeType,fileExtension:u.prototype.txt.fileExtension})),s=u.prototype.txt.buttonContent,l=u.prototype.txt.defaultClass;a(o,s,l)}};r.settings.formats.forEach(function(t){!(!o||"xls"!==t)&&(t="xlsm"),!o&&"xlsx"===t&&(t=null),t&&E[t](m,w)})});var S=document.querySelectorAll("button[data-fileblob]");return l(S,"click",function(){var t=JSON.parse(C.getItem(this.getAttribute("data-fileblob"))),e=t.data,n=t.filename,r=t.mimeType,o=t.fileExtension;u.prototype.export2file(e,r,n,o)}),r};u.prototype={version:"4.0.0-alpha.5",defaults:{headings:!0,footers:!0,formats:["xls","csv","txt"],filename:"id",bootstrap:!0,position:"bottom",ignoreRows:null,ignoreCols:null,ignoreCSS:".tableexport-ignore",emptyCSS:".tableexport-empty",trimWhitespace:!1},charset:"charset=utf-8",defaultFilename:"myDownload",defaultButton:"button-default",bootstrap:["btn","btn-default","btn-toolbar"],rowDel:"\r\n",entityMap:{"&":"&","<":"<",">":">","'":"'","/":"/"},xlsx:{defaultClass:"xlsx",buttonContent:"Export to xlsx",mimeType:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",fileExtension:".xlsx"},xls:{defaultClass:"xls",buttonContent:"Export to xls",separator:"\t",mimeType:"application/vnd.ms-excel",fileExtension:".xls"},csv:{defaultClass:"csv",buttonContent:"Export to csv",separator:",",mimeType:"text/csv",fileExtension:".csv"},txt:{defaultClass:"txt",buttonContent:"Export to txt",separator:" ",mimeType:"text/plain",fileExtension:".txt"},types:{string:{defaultClass:"tableexport-string"},number:{defaultClass:"tableexport-number",assert:function(t){return!isNaN(t.replace(/,/g,""))}},"boolean":{defaultClass:"tableexport-boolean",assert:function(t){return"true"===t.toLowerCase()||"false"===t.toLowerCase()}},date:{defaultClass:"tableexport-date",assert:function(t){return!isNaN(Date.parse(t))}}},escapeHtml:function(t){return String(t).replace(/[&<>'\/]/g,function(t){return u.prototype.entityMap[t]})},formatValue:function(t,e){return t?e.trim():e},getType:function(t){if(!t)return"";var e=u.prototype.types;return~t.indexOf(e.string.defaultClass)?"s":~t.indexOf(e.number.defaultClass)?"n":~t.indexOf(e["boolean"].defaultClass)?"b":~t.indexOf(e.date.defaultClass)?"d":""},dateNum:function(t,e){e&&(t+=1462);var n=Date.parse(t);return(n-new Date(Date.UTC(1899,11,30)))/864e5},createSheet:function(t){for(var e={},n={s:{c:1e7,r:1e7},e:{c:0,r:0}},r=u.prototype.types,s=0;s!==t.length;++s)for(var a=0;a!==t[s].length;++a){n.s.r>s&&(n.s.r=s),n.s.c>a&&(n.s.c=a),n.e.r string; + /** + * Removes leading/trailing whitespace from cell string + * @memberof TableExport.prototype + * @param isTrimWhitespace {Boolean} + * @param string {String} + * @returns {String} trimmed string + */ + formatValue: (string: string) => string; + + /** + * Get cell data-type + * @memberof TableExport.prototype + * @param string {String} + * @returns {String} data-type + */ + getType: (string: string) => string; + /** * Formats datetimes for compatibility with Excel * @memberof TableExport.prototype @@ -111,7 +134,7 @@ export class TableExport { * @param data {String} * @returns {Number} epoch time */ - createSheet: (data: string) => number; + createSheet: (data: string) => void; /** * Converts a string to an arraybuffer @@ -174,6 +197,7 @@ export interface Defaults { ignoreCols: number[]; ignoreCSS: string; emptyCSS: string; + trimWhitespace: boolean; } /** @@ -224,6 +248,22 @@ export interface TXT { fileExtension: string; } +/** + * Cell-types override and assertion configuration + * @memberof TableExport.prototype + */ +export interface Types { + string: Type; + number: Type; + boolean: Type; + date: Type; +} + +export interface Type { + defaultClass: string; + assert: (v: any) => boolean; +} + interface JQuery { /** diff --git a/package.json b/package.json index c923e7b..8f64df6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tableexport", - "version": "4.0.0-alpha.4", + "version": "4.0.0-alpha.5", "authors": [ "clarketm " ], diff --git a/src/stable/css/tableexport.css b/src/stable/css/tableexport.css index 29b0114..233a601 100644 --- a/src/stable/css/tableexport.css +++ b/src/stable/css/tableexport.css @@ -1,5 +1,5 @@ /*! - * TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) + * TableExport.js 4.0.0-alpha.5 (https://www.travismclarke.com) * Copyright 2017 Travis Clarke * Licensed under the MIT license */ diff --git a/src/stable/css/tableexport.min.css b/src/stable/css/tableexport.min.css index 0fd03a4..96ae58a 100644 --- a/src/stable/css/tableexport.min.css +++ b/src/stable/css/tableexport.min.css @@ -1,5 +1,5 @@ /*! - * TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) + * TableExport.js 4.0.0-alpha.5 (https://www.travismclarke.com) * Copyright 2017 Travis Clarke * Licensed under the MIT license */.top{caption-side:top}.bottom{caption-side:bottom}.button-default,.button-default:active,.button-default:focus,.button-default:hover{text-decoration:none}.button-default{font:700 12px sans-serif;color:#222;cursor:pointer;padding:5px;margin:5px}.button-default.csv:before,.button-default.txt:before,.button-default.xls:before,.button-default.xlsx:before{content:none}.csv,.txt,.xls,.xlsx{margin:4px 0}.csv:before,.txt:before,.xls:before,.xlsx:before{margin-right:10px;padding:11px 15px 12px;box-shadow:1px 1px 2px rgba(0,0,0,.2)}.xlsx:before{content:"";background:#006400 url(../img/xlsx.svg) no-repeat center}.xls:before{content:"";background:green url(../img/xls.svg) no-repeat center}.csv:before{content:"";background:#00f url(../img/csv.svg) no-repeat center}.txt:before{content:"";background:purple url(../img/txt.svg) no-repeat center} \ No newline at end of file diff --git a/src/stable/js/tableexport.js b/src/stable/js/tableexport.js index 3214c9a..d6fd128 100644 --- a/src/stable/js/tableexport.js +++ b/src/stable/js/tableexport.js @@ -1,5 +1,5 @@ /*! - * TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) + * TableExport.js 4.0.0-alpha.5 (https://www.travismclarke.com) * Copyright 2017 Travis Clarke * Licensed under the MIT license */ @@ -15,7 +15,7 @@ // Browser globals factory(root, root.jQuery, root.Blob, root.saveAs, root.XLSX); } -}(this, function (exports, $, Blob, saveAs, XLSX) { +}(this || window, function (exports, $, Blob, saveAs, XLSX) { 'use strict'; /** * TableExport main plugin constructor @@ -43,6 +43,7 @@ emptyCSS = self.settings.emptyCSS instanceof Array ? self.settings.emptyCSS.join(", ") : self.settings.emptyCSS, formatValue = TableExport.prototype.formatValue.bind(this, self.settings.trimWhitespace), getType = TableExport.prototype.getType, + store = new TableExport.prototype.LocalStorage.getInstance(), bootstrapClass, bootstrapTheme, bootstrapSpacing; if (self.settings.bootstrap) { @@ -109,6 +110,8 @@ t: getType(val.className) }; }); + }).map(function (val, ir) { + return [].concat.apply([], val); }), dataObject = TableExport.prototype.escapeHtml( JSON.stringify({ @@ -166,6 +169,8 @@ t: getType(val.className) }; }); + }).map(function (val, ir) { + return [].concat.apply([], val); }), dataObject = TableExport.prototype.escapeHtml( JSON.stringify({ @@ -197,7 +202,9 @@ t: getType(val.className) }; }).join(colD); - }).join(rdel), + }).join(rdel).map(function (val, ir) { + return [].concat.apply([], val); + }), dataObject = TableExport.prototype.escapeHtml( JSON.stringify({ data: dataURL, @@ -299,7 +306,9 @@ */ function createObjButton(dataObject, myContent, myClass) { var exportButton = document.createElement('button'); - exportButton.setAttribute('data-fileblob', dataObject); + var uuid = _uuid(); + exportButton.setAttribute('data-fileblob', uuid); + store.setItem(uuid, dataObject, true); exportButton.className = bootstrapClass + bootstrapTheme + myClass; exportButton.textContent = myContent; checkCaption(exportButton); @@ -308,7 +317,7 @@ var exportButton = document.querySelectorAll("button[data-fileblob]"); _on(exportButton, "click", function () { - var object = JSON.parse(this.getAttribute("data-fileblob")), + var object = JSON.parse(store.getItem(this.getAttribute("data-fileblob"))), data = object.data, filename = object.filename, mimeType = object.mimeType, @@ -324,7 +333,7 @@ * Version. * @memberof TableExport.prototype */ - version: "4.0.0-alpha.4", + version: "4.0.0-alpha.5", /** * Default plugin options. * @memberof TableExport.prototype @@ -498,7 +507,6 @@ * Creates an Excel spreadsheet from a data string * @memberof TableExport.prototype * @param data {String} - * @returns {Number} epoch time */ createSheet: function (data) { var ws = {}; @@ -563,7 +571,7 @@ * @param extension {String} file extension */ export2file: function (data, mime, name, extension) { - if (XLSX && extension.substr(0, 4) === ".xls") { + if (XLSX && extension.substr(0, 4) === (".xls")) { var wb = new this.Workbook(), ws = this.createSheet(data); @@ -583,31 +591,62 @@ name + extension, true); }, /** - * Updates the plugin instance with new/updated options - * @param options {Object} TableExport configuration options - * @returns {TableExport} updated TableExport instance - */ - update: function (options) { - return new TableExport(this.selectors, _extend({}, this.settings, options), true); - }, - /** - * Reset the plugin instance to its original state - * @returns {TableExport} original TableExport instance - */ - reset: function () { - return new TableExport(this.selectors, this.settings, true); - }, - /** - * Remove the instance (i.e. caption containing the export buttons) + * LocalStorage main interface constructor + * @memberof TableExport.prototype + * @constructor */ - remove: function () { - this.selectors.each(function () { - var caption = this.querySelectorAll('caption:not(.head)'); - caption.parentNode.removeChild(caption); - }); + LocalStorage: function () { + this.type = 'localStorage'; + this.store = exports[this.type]; + this.namespace = 'te'; + this.getKey = function (key) { + return this.namespace + key; + }; + this.setItem = function (_key, value, overwrite) { + var key = this.getKey(_key); + if (this.exists(key) && !overwrite) { + return; + } + return this.store.setItem(key, value); + }; + this.getItem = function (_key) { + var key = this.getKey(_key); + return this.store.getItem(key); + }; + this.exists = function (_key) { + var key = this.getKey(_key); + return this.store.getItem(key) !== null; + }; + this.removeItem = function (_key) { + var key = this.getKey(_key); + return this.store.removeItem(key); + }; + this.error = function (message) { + return new Error('unknown error occurred', message); + }; + } + }; + + var _store = TableExport.prototype.LocalStorage; + _store._instance = null; + _store.getInstance = function () { + if (!_store._instance) { + _store._instance = new _store(); } + return _store._instance; }; + function _uuid() { + function s4() { + return Math.floor((1 + Math.random()) * 0x10000) + .toString(16) + .substring(1); + } + + return s4() + s4() + '-' + s4() + '-' + s4() + '-' + + s4() + '-' + s4() + s4() + s4(); + } + function _extend() { var args = arguments; for (var i = 1; i < args.length; i++) @@ -648,7 +687,7 @@ } } - exports.default = exports.TableExport = TableExport; + return exports.default = exports.TableExport = TableExport; } )); diff --git a/src/stable/js/tableexport.min.js b/src/stable/js/tableexport.min.js index 7043dfc..75109ec 100644 --- a/src/stable/js/tableexport.min.js +++ b/src/stable/js/tableexport.min.js @@ -1,6 +1,6 @@ /*! - * TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) + * TableExport.js 4.0.0-alpha.5 (https://www.travismclarke.com) * Copyright 2017 Travis Clarke * Licensed under the MIT license */ -!function(t,e){"function"==typeof define&&define.amd?define(["exports","jquery","blobjs","file-saverjs","xlsx-js"],e):"object"==typeof exports&&"string"!=typeof exports.nodeName?e(exports,require("jquery"),require("blobjs"),require("file-saverjs"),require("xlsx-js")):e(t,t.jQuery,t.Blob,t.saveAs,t.XLSX)}(this,function(t,e,n,o,r){"use strict";function s(){for(var t=arguments,e=1;e tr")),b=f.settings.headings?i(t.querySelectorAll("thead > tr")).concat(b):b,b=f.settings.footers?i(t.querySelectorAll("tfoot > tr")).concat(b):b,v=f.settings.headings?t.querySelectorAll("thead > tr").length:0,S="id"===f.settings.filename?t.getAttribute("id")?t.getAttribute("id"):p.prototype.defaultFilename:f.settings.filename,C={xlsx:function(t,n){var o={},r=i(b).map(function(t,n){if(!~m.indexOf(n-v)&&!l(t,h)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!l(t,h)){if(l(t,g))return" ";if(t.hasAttribute("colspan")&&(o[n]=o[n]||{},o[n][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var s=1;s=r?a+o[n][s]:a:p++,p!==i);s++);return new Array(a).concat(e(t).text())}return t.textContent}})}}),a=p.prototype.escapeHtml(JSON.stringify({data:r,filename:n,mimeType:p.prototype.xlsx.mimeType,fileExtension:p.prototype.xlsx.fileExtension})),f=p.prototype.xlsx.buttonContent,u=p.prototype.xlsx.defaultClass;s(a,f,u)},xlsm:function(t,n){var o={},r=i(b).map(function(t,n){if(!~m.indexOf(n-v)&&!l(t,h)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!l(t,h)){if(l(t,g))return" ";if(t.hasAttribute("colspan")&&(o[n]=o[n]||{},o[n][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var s=1;s=r?a+o[n][s]:a:p++,p!==i);s++);return new Array(a).concat(e(t).text())}return t.textContent}})}}),a=p.prototype.escapeHtml(JSON.stringify({data:r,filename:n,mimeType:p.prototype.xls.mimeType,fileExtension:p.prototype.xls.fileExtension})),f=p.prototype.xls.buttonContent,u=p.prototype.xls.defaultClass;s(a,f,u)},xls:function(t,e){var n=p.prototype.xls.separator,o=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,h)){var o=t.querySelectorAll("th, td");return i(o).map(function(t,e){if(!~d.indexOf(e)&&!l(t,h))return l(t,g)?" ":t.textContent}).join(n)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:o,filename:e,mimeType:p.prototype.xls.mimeType,fileExtension:p.prototype.xls.fileExtension})),a=p.prototype.xls.buttonContent,f=p.prototype.xls.defaultClass;s(r,a,f)},csv:function(t,e){var n=p.prototype.csv.separator,o=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,h)){var o=t.querySelectorAll("th, td");return i(o).map(function(t,e){if(!~d.indexOf(e)&&!l(t,h))return l(t,g)?" ":'"'+t.textContent.replace(/"/g,'""')+'"'}).join(n)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:o,filename:e,mimeType:p.prototype.csv.mimeType,fileExtension:p.prototype.csv.fileExtension})),a=p.prototype.csv.buttonContent,f=p.prototype.csv.defaultClass;s(r,a,f)},txt:function(t,e){var n=p.prototype.txt.separator,o=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,h)){var o=t.querySelectorAll("th, td");return i(o).map(function(t,e){if(!~d.indexOf(e)&&!l(t,h))return l(t,g)?" ":t.textContent}).join(n)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:o,filename:e,mimeType:p.prototype.txt.mimeType,fileExtension:p.prototype.txt.fileExtension})),a=p.prototype.txt.buttonContent,f=p.prototype.txt.defaultClass;s(r,a,f)}};f.settings.formats.forEach(function(t){!(!r||"xls"!==t)&&(t="xlsm"),!r&&"xlsx"===t&&(t=null),t&&C[t](y,S)})});var b=document.querySelectorAll("button[data-fileblob]");return a(b,"click",function(){var t=JSON.parse(this.getAttribute("data-fileblob")),e=t.data,n=t.filename,o=t.mimeType,r=t.fileExtension;p.prototype.export2file(e,o,n,r)}),f};if(p.prototype={version:"4.0.0-alpha.4",defaults:{headings:!0,footers:!0,formats:["xls","csv","txt"],filename:"id",bootstrap:!0,position:"bottom",ignoreRows:null,ignoreCols:null,ignoreCSS:".tableexport-ignore",emptyCSS:".tableexport-empty"},charset:"charset=utf-8",defaultFilename:"myDownload",defaultButton:"button-default",bootstrap:["btn","btn-default","btn-toolbar"],rowDel:"\r\n",entityMap:{"&":"&","<":"<",">":">","'":"'","/":"/"},xlsx:{defaultClass:"xlsx",buttonContent:"Export to xlsx",mimeType:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",fileExtension:".xlsx"},xls:{defaultClass:"xls",buttonContent:"Export to xls",separator:"\t",mimeType:"application/vnd.ms-excel",fileExtension:".xls"},csv:{defaultClass:"csv",buttonContent:"Export to csv",separator:",",mimeType:"text/csv",fileExtension:".csv"},txt:{defaultClass:"txt",buttonContent:"Export to txt",separator:" ",mimeType:"text/plain",fileExtension:".txt"},escapeHtml:function(t){return String(t).replace(/[&<>'\/]/g,function(t){return p.prototype.entityMap[t]})},dateNum:function(t,e){e&&(t+=1462);var n=Date.parse(t);return(n-new Date(Date.UTC(1899,11,30)))/864e5},createSheet:function(t){for(var e={},n={s:{c:1e7,r:1e7},e:{c:0,r:0}},o=0;o!=t.length;++o)for(var s=0;s!=t[o].length;++s){n.s.r>o&&(n.s.r=o),n.s.c>s&&(n.s.c=s),n.e.r tr")),S=r.settings.headings?i(t.querySelectorAll("thead > tr")).concat(S):S,S=r.settings.footers?i(t.querySelectorAll("tfoot > tr")).concat(S):S,A=r.settings.headings?t.querySelectorAll("thead > tr").length:0,w="id"===r.settings.filename?t.getAttribute("id")?t.getAttribute("id"):u.prototype.defaultFilename:r.settings.filename,E={xlsx:function(t,e){var n={},r=i(S).map(function(t,e){if(!~x.indexOf(e-A)&&!p(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!p(t,g)){if(p(t,h))return" ";if(t.hasAttribute("colspan")&&(n[e]=n[e]||{},n[e][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var o=1;o=r?a+n[e][o]:a:i++,i!==s);o++);return new Array(a).concat({v:b(t.textContent),t:v(t.className)})}return{v:b(t.textContent),t:v(t.className)}}})}}).map(function(t,e){return[].concat.apply([],t)}),o=u.prototype.escapeHtml(JSON.stringify({data:r,filename:e,mimeType:u.prototype.xlsx.mimeType,fileExtension:u.prototype.xlsx.fileExtension})),s=u.prototype.xlsx.buttonContent,l=u.prototype.xlsx.defaultClass;a(o,s,l)},xlsm:function(t,e){var n={},r=i(S).map(function(t,e){if(!~x.indexOf(e-A)&&!p(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!p(t,g)){if(p(t,h))return" ";if(t.hasAttribute("colspan")&&(n[e]=n[e]||{},n[e][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var o=1;o=r?a+n[e][o]:a:i++,i!==s);o++);return new Array(a).concat({v:b(t.textContent),t:v(t.className)})}return{v:b(t.textContent),t:v(t.className)}}})}}).map(function(t,e){return[].concat.apply([],t)}),o=u.prototype.escapeHtml(JSON.stringify({data:r,filename:e,mimeType:u.prototype.xls.mimeType,fileExtension:u.prototype.xls.fileExtension})),s=u.prototype.xls.buttonContent,l=u.prototype.xls.defaultClass;a(o,s,l)},xls:function(t,e){var n=u.prototype.xls.separator,r=i(S).map(function(t,e){if(!~x.indexOf(e-A)&&!p(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,e){if(!~d.indexOf(e)&&!p(t,g))return p(t,h)?" ":{v:b(t.textContent),t:v(t.className)}}).join(n)}}).join(t).map(function(t,e){return[].concat.apply([],t)}),o=u.prototype.escapeHtml(JSON.stringify({data:r,filename:e,mimeType:u.prototype.xls.mimeType,fileExtension:u.prototype.xls.fileExtension})),s=u.prototype.xls.buttonContent,l=u.prototype.xls.defaultClass;a(o,s,l)},csv:function(t,e){var n=u.prototype.csv.separator,r=i(S).map(function(t,e){if(!~x.indexOf(e-A)&&!p(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,e){if(!~d.indexOf(e)&&!p(t,g))return p(t,h)?" ":'"'+b(t.textContent.replace(/"/g,'""'))+'"'}).join(n)}}).join(t),o=u.prototype.escapeHtml(JSON.stringify({data:r,filename:e,mimeType:u.prototype.csv.mimeType,fileExtension:u.prototype.csv.fileExtension})),s=u.prototype.csv.buttonContent,l=u.prototype.csv.defaultClass;a(o,s,l)},txt:function(t,e){var n=u.prototype.txt.separator,r=i(S).map(function(t,e){if(!~x.indexOf(e-A)&&!p(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,e){if(!~d.indexOf(e)&&!p(t,g))return p(t,h)?" ":b(t.textContent)}).join(n)}}).join(t),o=u.prototype.escapeHtml(JSON.stringify({data:r,filename:e,mimeType:u.prototype.txt.mimeType,fileExtension:u.prototype.txt.fileExtension})),s=u.prototype.txt.buttonContent,l=u.prototype.txt.defaultClass;a(o,s,l)}};r.settings.formats.forEach(function(t){!(!o||"xls"!==t)&&(t="xlsm"),!o&&"xlsx"===t&&(t=null),t&&E[t](m,w)})});var S=document.querySelectorAll("button[data-fileblob]");return l(S,"click",function(){var t=JSON.parse(C.getItem(this.getAttribute("data-fileblob"))),e=t.data,n=t.filename,r=t.mimeType,o=t.fileExtension;u.prototype.export2file(e,r,n,o)}),r};u.prototype={version:"4.0.0-alpha.5",defaults:{headings:!0,footers:!0,formats:["xls","csv","txt"],filename:"id",bootstrap:!0,position:"bottom",ignoreRows:null,ignoreCols:null,ignoreCSS:".tableexport-ignore",emptyCSS:".tableexport-empty",trimWhitespace:!1},charset:"charset=utf-8",defaultFilename:"myDownload",defaultButton:"button-default",bootstrap:["btn","btn-default","btn-toolbar"],rowDel:"\r\n",entityMap:{"&":"&","<":"<",">":">","'":"'","/":"/"},xlsx:{defaultClass:"xlsx",buttonContent:"Export to xlsx",mimeType:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",fileExtension:".xlsx"},xls:{defaultClass:"xls",buttonContent:"Export to xls",separator:"\t",mimeType:"application/vnd.ms-excel",fileExtension:".xls"},csv:{defaultClass:"csv",buttonContent:"Export to csv",separator:",",mimeType:"text/csv",fileExtension:".csv"},txt:{defaultClass:"txt",buttonContent:"Export to txt",separator:" ",mimeType:"text/plain",fileExtension:".txt"},types:{string:{defaultClass:"tableexport-string"},number:{defaultClass:"tableexport-number",assert:function(t){return!isNaN(t.replace(/,/g,""))}},"boolean":{defaultClass:"tableexport-boolean",assert:function(t){return"true"===t.toLowerCase()||"false"===t.toLowerCase()}},date:{defaultClass:"tableexport-date",assert:function(t){return!isNaN(Date.parse(t))}}},escapeHtml:function(t){return String(t).replace(/[&<>'\/]/g,function(t){return u.prototype.entityMap[t]})},formatValue:function(t,e){return t?e.trim():e},getType:function(t){if(!t)return"";var e=u.prototype.types;return~t.indexOf(e.string.defaultClass)?"s":~t.indexOf(e.number.defaultClass)?"n":~t.indexOf(e["boolean"].defaultClass)?"b":~t.indexOf(e.date.defaultClass)?"d":""},dateNum:function(t,e){e&&(t+=1462);var n=Date.parse(t);return(n-new Date(Date.UTC(1899,11,30)))/864e5},createSheet:function(t){for(var e={},n={s:{c:1e7,r:1e7},e:{c:0,r:0}},r=u.prototype.types,s=0;s!==t.length;++s)for(var a=0;a!==t[s].length;++a){n.s.r>s&&(n.s.r=s),n.s.c>a&&(n.s.c=a),n.e.r string; + /** + * Removes leading/trailing whitespace from cell string + * @memberof TableExport.prototype + * @param isTrimWhitespace {Boolean} + * @param string {String} + * @returns {String} trimmed string + */ + formatValue: (string: string) => string; + + /** + * Get cell data-type + * @memberof TableExport.prototype + * @param string {String} + * @returns {String} data-type + */ + getType: (string: string) => string; + /** * Formats datetimes for compatibility with Excel * @memberof TableExport.prototype @@ -111,7 +134,7 @@ export class TableExport { * @param data {String} * @returns {Number} epoch time */ - createSheet: (data: string) => number; + createSheet: (data: string) => void; /** * Converts a string to an arraybuffer @@ -174,6 +197,7 @@ export interface Defaults { ignoreCols: number[]; ignoreCSS: string; emptyCSS: string; + trimWhitespace: boolean; } /** @@ -224,6 +248,22 @@ export interface TXT { fileExtension: string; } +/** + * Cell-types override and assertion configuration + * @memberof TableExport.prototype + */ +export interface Types { + string: Type; + number: Type; + boolean: Type; + date: Type; +} + +export interface Type { + defaultClass: string; + assert: (v: any) => boolean; +} + interface JQuery { /**