tableexport
Advanced tools
Comparing version 4.0.0-alpha.4 to 4.0.0-alpha.5
{ | ||
"name": "tableexport.js", | ||
"version": "4.0.0-alpha.4", | ||
"version": "4.0.0-alpha.5", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "clarketm <travis.m.clarke@gmail.com>" |
/*! | ||
* TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) | ||
* Copyright 2016 Travis Clarke | ||
* TableExport.js 4.0.0-alpha.5 (https://www.travismclarke.com) | ||
* Copyright 2017 Travis Clarke | ||
* Licensed under the MIT license | ||
@@ -18,3 +18,3 @@ */ | ||
} | ||
}(this, function (exports, $, Blob, saveAs, XLSX) { | ||
}(this || window, function (exports, $, Blob, saveAs, XLSX) { | ||
'use strict'; | ||
@@ -45,2 +45,5 @@ /** | ||
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; | ||
@@ -65,3 +68,3 @@ | ||
thAdj = self.settings.headings ? el.querySelectorAll('thead > tr').length : 0, | ||
fileName = self.settings.fileName === "id" ? (el.getAttribute('id') ? el.getAttribute('id') : TableExport.prototype.defaultFileName) : self.settings.fileName, | ||
filename = self.settings.filename === "id" ? (el.getAttribute('id') ? el.getAttribute('id') : TableExport.prototype.defaultFilename) : self.settings.filename, | ||
exporters = { | ||
@@ -103,6 +106,14 @@ xlsx: function (rDel, name) { | ||
} | ||
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); | ||
}), | ||
@@ -112,3 +123,3 @@ dataObject = TableExport.prototype.escapeHtml( | ||
data: dataURL, | ||
fileName: name, | ||
filename: name, | ||
mimeType: TableExport.prototype.xlsx.mimeType, | ||
@@ -156,6 +167,14 @@ fileExtension: TableExport.prototype.xlsx.fileExtension | ||
} | ||
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); | ||
}), | ||
@@ -165,3 +184,3 @@ dataObject = TableExport.prototype.escapeHtml( | ||
data: dataURL, | ||
fileName: name, | ||
filename: name, | ||
mimeType: TableExport.prototype.xls.mimeType, | ||
@@ -188,9 +207,14 @@ fileExtension: TableExport.prototype.xls.fileExtension | ||
} | ||
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, | ||
fileName: name, | ||
filename: name, | ||
mimeType: TableExport.prototype.xls.mimeType, | ||
@@ -217,3 +241,3 @@ fileExtension: TableExport.prototype.xls.fileExtension | ||
} | ||
return '"' + val.textContent.replace(/"/g, '""') + '"'; | ||
return '"' + formatValue(val.textContent.replace(/"/g, '""')) + '"'; | ||
}).join(colD); | ||
@@ -224,3 +248,3 @@ }).join(rdel), | ||
data: dataURL, | ||
fileName: name, | ||
filename: name, | ||
mimeType: TableExport.prototype.csv.mimeType, | ||
@@ -247,3 +271,3 @@ fileExtension: TableExport.prototype.csv.fileExtension | ||
} | ||
return val.textContent; | ||
return formatValue(val.textContent); | ||
}).join(colD); | ||
@@ -254,3 +278,3 @@ }).join(rdel), | ||
data: dataURL, | ||
fileName: name, | ||
filename: name, | ||
mimeType: TableExport.prototype.txt.mimeType, | ||
@@ -269,6 +293,10 @@ fileExtension: TableExport.prototype.txt.fileExtension | ||
!XLSX && key === 'xlsx' ? key = null : false; | ||
key && exporters[key](rowD, fileName); | ||
key && exporters[key](rowD, filename); | ||
} | ||
); | ||
/** | ||
* Initializes table caption with export buttons | ||
* @param exportButton {HTMLButtonElement} | ||
*/ | ||
function checkCaption(exportButton) { | ||
@@ -286,5 +314,13 @@ var caption = el.querySelectorAll('caption:not(.head)'); | ||
/** | ||
* 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; | ||
@@ -298,8 +334,8 @@ exportButton.textContent = myContent; | ||
_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, | ||
filename = object.filename, | ||
mimeType = object.mimeType, | ||
fileExtension = object.fileExtension; | ||
TableExport.prototype.export2file(data, mimeType, fileName, fileExtension); | ||
TableExport.prototype.export2file(data, mimeType, filename, fileExtension); | ||
}); | ||
@@ -315,3 +351,3 @@ | ||
*/ | ||
version: "4.0.0-alpha.4", | ||
version: "4.0.0-alpha.5", | ||
/** | ||
@@ -325,9 +361,10 @@ * Default plugin options. | ||
formats: ["xls", "csv", "txt"], // (String[]), filetype(s) for the export, (default: ["xls", "csv", "txt"]) | ||
fileName: "id", // (id, String), filename for the downloaded file, (default: "id") | ||
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) | ||
}, | ||
@@ -343,3 +380,3 @@ /** | ||
*/ | ||
defaultFileName: "myDownload", | ||
defaultFilename: "myDownload", | ||
/** | ||
@@ -409,2 +446,29 @@ * Class applied to each export button element. | ||
/** | ||
* 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 | ||
@@ -421,2 +485,31 @@ * @memberof TableExport.prototype | ||
/** | ||
* 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 | ||
@@ -437,3 +530,2 @@ * @memberof TableExport.prototype | ||
* @param data {String} | ||
* @returns {Number} epoch time | ||
*/ | ||
@@ -443,4 +535,5 @@ createSheet: function (data) { | ||
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; | ||
@@ -450,9 +543,14 @@ if (range.s.c > C) range.s.c = C; | ||
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'; | ||
@@ -462,3 +560,2 @@ cell.z = XLSX.SSF._table[14]; | ||
} | ||
else cell.t = 's'; | ||
@@ -489,3 +586,3 @@ ws[cell_ref] = cell; | ||
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; | ||
@@ -502,3 +599,3 @@ }, | ||
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(), | ||
@@ -523,27 +620,58 @@ ws = this.createSheet(data); | ||
/** | ||
* Updates the plugin instance with new/updated options | ||
* @param options {Object} TableExport configuration options | ||
* @returns {TableExport} updated TableExport instance | ||
* LocalStorage main interface constructor | ||
* @memberof TableExport.prototype | ||
* @constructor | ||
*/ | ||
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, settings, true); | ||
}, | ||
/** | ||
* Remove the instance (i.e. caption containing the export buttons) | ||
*/ | ||
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() { | ||
@@ -589,5 +717,5 @@ var args = arguments; | ||
exports.default = exports.TableExport = TableExport; | ||
return exports.default = exports.TableExport = TableExport; | ||
} | ||
)); |
/*! | ||
* TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) | ||
* Copyright 2016 Travis Clarke | ||
* 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,o,n,r){"use strict";function s(){for(var t=arguments,e=1;e<t.length;e++)for(var o in t[e])t[e].hasOwnProperty(o)&&(t[0][o]=t[e][o]);return t[0]}function i(t){return[].slice.call(t)}function a(t,e,o){for(var n=0;n<t.length;++n)t[n].addEventListener(e,o,!1)}function l(t,e){return t.classList?t.classList.contains(e):new RegExp("(^| )"+e+"( |$)","gi").test(t.cls)}var p=function(t,o,n){var f=this;f.settings=n?o:s({},p.prototype.defaults,o),f.selectors=i(t);var u,c,x,y=p.prototype.rowDel,m=f.settings.ignoreRows instanceof Array?f.settings.ignoreRows:[f.settings.ignoreRows],d=f.settings.ignoreCols instanceof Array?f.settings.ignoreCols:[f.settings.ignoreCols],g=f.settings.ignoreCSS instanceof Array?f.settings.ignoreCSS.join(", "):f.settings.ignoreCSS,h=f.settings.emptyCSS instanceof Array?f.settings.emptyCSS.join(", "):f.settings.emptyCSS;f.settings.bootstrap?(u=p.prototype.bootstrap[0]+" ",c=p.prototype.bootstrap[1]+" ",x=p.prototype.bootstrap[2]+" "):(u=p.prototype.defaultButton+" ",c=x=""),f.selectors.forEach(function(t){function o(e){var o=t.querySelectorAll("caption:not(.head)");o.length?o[0].appendChild(e):(o=document.createElement("caption"),o.className=x+f.settings.position,o.appendChild(e),t.insertBefore(o,t.firstChild))}function s(t,e,n){var r=document.createElement("button");r.setAttribute("data-fileblob",t),r.className=u+c+n,r.textContent=e,o(r)}var a=t.querySelectorAll("caption:not(.head)");n&&a.parentNode.removeChild(a);var b=i(t.querySelectorAll("tbody > 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,o){var n={},r=i(b).map(function(t,o){if(!~m.indexOf(o-v)&&!l(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!l(t,g)){if(l(t,h))return" ";if(t.hasAttribute("colspan")&&(n[o]=n[o]||{},n[o][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var s=1;s<t.getAttribute("rowspan");s++)n[o+s]=n[o+s]||{},n[o+s][r]=1;if(n[o]){for(var i=r+1,a=0,p=0,s=0;s<=Math.max.apply(Math,Object.keys(n[o]))&&(n[o][s]?a=p>=r?a+n[o][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:o,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,o){var n={},r=i(b).map(function(t,o){if(!~m.indexOf(o-v)&&!l(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!l(t,g)){if(l(t,h))return" ";if(t.hasAttribute("colspan")&&(n[o]=n[o]||{},n[o][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var s=1;s<t.getAttribute("rowspan");s++)n[o+s]=n[o+s]||{},n[o+s][r]=1;if(n[o]){for(var i=r+1,a=0,p=0,s=0;s<=Math.max.apply(Math,Object.keys(n[o]))&&(n[o][s]?a=p>=r?a+n[o][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:o,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 o=p.prototype.xls.separator,n=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,g)){var n=t.querySelectorAll("th, td");return i(n).map(function(t,e){if(!~d.indexOf(e)&&!l(t,g))return l(t,h)?" ":t.textContent}).join(o)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:n,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 o=p.prototype.csv.separator,n=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,g)){var n=t.querySelectorAll("th, td");return i(n).map(function(t,e){if(!~d.indexOf(e)&&!l(t,g))return l(t,h)?" ":'"'+t.textContent.replace(/"/g,'""')+'"'}).join(o)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:n,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 o=p.prototype.txt.separator,n=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,g)){var n=t.querySelectorAll("th, td");return i(n).map(function(t,e){if(!~d.indexOf(e)&&!l(t,g))return l(t,h)?" ":t.textContent}).join(o)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:n,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,o=t.fileName,n=t.mimeType,r=t.fileExtension;p.prototype.export2file(e,n,o,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 o=Date.parse(t);return(o-new Date(Date.UTC(1899,11,30)))/864e5},createSheet:function(t){for(var e={},o={s:{c:1e7,r:1e7},e:{c:0,r:0}},n=0;n!=t.length;++n)for(var s=0;s!=t[n].length;++s){o.s.r>n&&(o.s.r=n),o.s.c>s&&(o.s.c=s),o.e.r<n&&(o.e.r=n),o.e.c<s&&(o.e.c=s);var i={v:t[n][s]};if(null!=i.v){var a=r.utils.encode_cell({c:s,r:n});"number"==typeof i.v?i.t="n":"boolean"==typeof i.v?i.t="b":i.v instanceof Date?(i.t="n",i.z=r.SSF._table[14],i.v=this.dateNum(i.v)):i.t="s",e[a]=i}}return o.s.c<1e7&&(e["!ref"]=r.utils.encode_range(o)),e},Workbook:function(){this.SheetNames=[],this.Sheets={}},string2ArrayBuffer:function(t){for(var e=new ArrayBuffer(t.length),o=new Uint8Array(e),n=0;n!=t.length;++n)o[n]=255&t.charCodeAt(n);return e},export2file:function(t,e,s,i){if(r&&".xls"==i.substr(0,4)){var a=new this.Workbook,l=this.createSheet(t);a.SheetNames.push(s),a.Sheets[s]=l;var p={bookType:i.substr(1,3)+(i.substr(4)||"m"),bookSST:!1,type:"binary"},f=r.write(a,p);t=this.string2ArrayBuffer(f)}n(new o([t],{type:e+";"+this.charset}),s+i,!0)},update:function(t){return new p(this.selectors,s({},this.settings,t),(!0))},reset:function(){return new p(this.selectors,settings,(!0))},remove:function(){this.selectors.each(function(){var t=this.querySelectorAll("caption:not(.head)");t.parentNode.removeChild(t)})}},e){e.fn.tableExport=function(t,e){return new p(this,t,e)};for(var f in p.prototype)e.fn.tableExport[f]=p.prototype[f]}t["default"]=t.TableExport=p}); | ||
!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||window,function(t,e,n,r,o){"use strict";function s(){function t(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}return t()+t()+"-"+t()+"-"+t()+"-"+t()+"-"+t()+t()+t()}function a(){for(var t=arguments,e=1;e<t.length;e++)for(var n in t[e])t[e].hasOwnProperty(n)&&(t[0][n]=t[e][n]);return t[0]}function i(t){return[].slice.call(t)}function l(t,e,n){for(var r=0;r<t.length;++r)t[r].addEventListener(e,n,!1)}function p(t,e){return t.classList?t.classList.contains(e):new RegExp("(^| )"+e+"( |$)","gi").test(t.cls)}var u=function(t,e,n){var r=this;r.settings=n?e:a({},u.prototype.defaults,e),r.selectors=i(t);var f,c,y,m=u.prototype.rowDel,x=r.settings.ignoreRows instanceof Array?r.settings.ignoreRows:[r.settings.ignoreRows],d=r.settings.ignoreCols instanceof Array?r.settings.ignoreCols:[r.settings.ignoreCols],g=r.settings.ignoreCSS instanceof Array?r.settings.ignoreCSS.join(", "):r.settings.ignoreCSS,h=r.settings.emptyCSS instanceof Array?r.settings.emptyCSS.join(", "):r.settings.emptyCSS,b=u.prototype.formatValue.bind(this,r.settings.trimWhitespace),v=u.prototype.getType,C=new u.prototype.LocalStorage.getInstance;r.settings.bootstrap?(f=u.prototype.bootstrap[0]+" ",c=u.prototype.bootstrap[1]+" ",y=u.prototype.bootstrap[2]+" "):(f=u.prototype.defaultButton+" ",c=y=""),r.selectors.forEach(function(t){function e(e){var n=t.querySelectorAll("caption:not(.head)");n.length?n[0].appendChild(e):(n=document.createElement("caption"),n.className=y+r.settings.position,n.appendChild(e),t.insertBefore(n,t.firstChild))}function a(t,n,r){var o=document.createElement("button"),a=s();o.setAttribute("data-fileblob",a),C.setItem(a,t,!0),o.className=f+c+r,o.textContent=n,e(o)}var l=t.querySelectorAll("caption:not(.head)");n&&l.parentNode.removeChild(l);var S=i(t.querySelectorAll("tbody > 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<t.getAttribute("rowspan");o++)n[e+o]=n[e+o]||{},n[e+o][r]=1;if(n[e]){for(var s=r+1,a=0,i=0,o=0;o<=Math.max.apply(Math,Object.keys(n[e]))&&(n[e][o]?a=i>=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<t.getAttribute("rowspan");o++)n[e+o]=n[e+o]||{},n[e+o][r]=1;if(n[e]){for(var s=r+1,a=0,i=0,o=0;o<=Math.max.apply(Math,Object.keys(n[e]))&&(n[e][o]?a=i>=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<s&&(n.e.r=s),n.e.c<a&&(n.e.c=a);var i=t[s][a];if(i&&i.v){var l=o.utils.encode_cell({c:a,r:s});i.t||(r.number.assert(i.v)?i.t="n":r["boolean"].assert(i.v)?i.t="b":r.date.assert(i.v)?i.t="d":i.t="s"),"d"===i.t&&(i.t="n",i.z=o.SSF._table[14],i.v=this.dateNum(i.v)),e[l]=i}}return n.s.c<1e7&&(e["!ref"]=o.utils.encode_range(n)),e},Workbook:function(){this.SheetNames=[],this.Sheets={}},string2ArrayBuffer:function(t){for(var e=new ArrayBuffer(t.length),n=new Uint8Array(e),r=0;r!==t.length;++r)n[r]=255&t.charCodeAt(r);return e},export2file:function(t,e,s,a){if(o&&".xls"===a.substr(0,4)){var i=new this.Workbook,l=this.createSheet(t);i.SheetNames.push(s),i.Sheets[s]=l;var p={bookType:a.substr(1,3)+(a.substr(4)||"m"),bookSST:!1,type:"binary"},u=o.write(i,p);t=this.string2ArrayBuffer(u)}r(new n([t],{type:e+";"+this.charset}),s+a,!0)},LocalStorage:function(){this.type="localStorage",this.store=t[this.type],this.namespace="te",this.getKey=function(t){return this.namespace+t},this.setItem=function(t,e,n){var r=this.getKey(t);if(!this.exists(r)||n)return this.store.setItem(r,e)},this.getItem=function(t){var e=this.getKey(t);return this.store.getItem(e)},this.exists=function(t){var e=this.getKey(t);return null!==this.store.getItem(e)},this.removeItem=function(t){var e=this.getKey(t);return this.store.removeItem(e)},this.error=function(t){return new Error("unknown error occurred",t)}}};var f=u.prototype.LocalStorage;if(f._instance=null,f.getInstance=function(){return f._instance||(f._instance=new f),f._instance},e){e.fn.tableExport=function(t,e){return new u(this,t,e)};for(var c in u.prototype)e.fn.tableExport[c]=u.prototype[c]}return t["default"]=t.TableExport=u}); |
@@ -41,3 +41,3 @@ /** | ||
*/ | ||
defaultFileName: string; | ||
defaultFilename: string; | ||
@@ -93,2 +93,8 @@ /** | ||
/** | ||
* Cell-types override and assertion configuration | ||
* @memberof TableExport.prototype | ||
*/ | ||
types: Types; | ||
/** | ||
* Escapes special characters with HTML entities | ||
@@ -102,2 +108,19 @@ * @memberof TableExport.prototype | ||
/** | ||
* 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 | ||
@@ -117,3 +140,3 @@ * @memberof TableExport.prototype | ||
*/ | ||
createSheet: (data: string) => number; | ||
createSheet: (data: string) => void; | ||
@@ -174,3 +197,3 @@ /** | ||
formats: string[]; | ||
fileName: string; | ||
filename: string; | ||
bootstrap: boolean; | ||
@@ -182,2 +205,3 @@ position: string; | ||
emptyCSS: string; | ||
trimWhitespace: boolean; | ||
} | ||
@@ -233,2 +257,18 @@ | ||
/** | ||
* 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 { | ||
@@ -235,0 +275,0 @@ |
{ | ||
"name": "tableexport", | ||
"version": "4.0.0-alpha.4", | ||
"version": "4.0.0-alpha.5", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "clarketm <travis.m.clarke@gmail.com>" |
@@ -5,2 +5,8 @@ [![Build Status](https://travis-ci.org/clarketm/TableExport.svg?branch=master)](https://travis-ci.org/clarketm/TableExport) | ||
[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.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.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 | ||
@@ -33,6 +39,15 @@ | ||
| :------: | :----------: | :--------: | | ||
| __CSS__ | [🔗](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.5/css/tableexport.css) | [🔗](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.5/css/tableexport.min.css) | | ||
| __JS__ | [🔗](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.5/js/tableexport.js) | [🔗](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.5/js/tableexport.min.js) | | ||
| __Images__ | — | [🔗<sup>xlsx</sup>](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.5/img/xlsx.svg)[🔗<sup>xls</sup>](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.5/img/xls.svg)[🔗<sup>csv</sup>](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.5/img/csv.svg)[🔗<sup>txt</sup>](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.5/img/txt.svg) | | ||
| __CSS__ | [🔗](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.9/css/tableexport.css) | [🔗](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.9/css/tableexport.min.css) | | ||
| __JS__ | [🔗](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.9/js/tableexport.js) | [🔗](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.9/js/tableexport.min.js) | | ||
| __Images__ | — | [🔗<sup>xlsx</sup>](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.9/img/xlsx.svg)[🔗<sup>xls</sup>](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.9/img/xls.svg)[🔗<sup>csv</sup>](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.9/img/csv.svg)[🔗<sup>txt</sup>](https://cdnjs.cloudflare.com/ajax/libs/TableExport/3.3.9/img/txt.svg) | | ||
### [unpkg](https://unpkg.com/#/) | ||
| | uncompressed | compressed | | ||
| :------: | :----------: | :--------: | | ||
| __CSS__ | [🔗](https://unpkg.com/tableexport/dist/css/tableexport.css) | [🔗](https://unpkg.com/tableexport/dist/css/tableexport.min.css) | | ||
| __JS__ | [🔗](https://unpkg.com/tableexport/dist/js/tableexport.js) | [🔗](https://unpkg.com/tableexport/dist/js/tableexport.min.js) | | ||
| __Images__ | — | [🔗<sup>xlsx</sup>](https://unpkg.com/tableexport/dist/img/xlsx.svg)[🔗<sup>xls</sup>](https://unpkg.com/tableexport/dist/img/xls.svg)[🔗<sup>csv</sup>](https://unpkg.com/tableexport/dist/img/csv.svg)[🔗<sup>txt</sup>](https://unpkg.com/tableexport/dist/img/txt.svg) | | ||
### Dependencies | ||
@@ -45,3 +60,3 @@ | ||
> `*` jQuery dependency requirement is removed as of [4.0.0-alpha.4](https://github.com/clarketm/TableExport/tree/v4.0.0-alpha.4) | ||
> `*` jQuery dependency requirement is removed as of [4.0.0-alpha.2](https://github.com/clarketm/TableExport/tree/v4.0.0-alpha.2) | ||
@@ -111,9 +126,10 @@ #### Optional / Theming: | ||
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 | ||
ignoreCols: null, // (Number, Number[]), column indices to exclude from the exported file | ||
ignoreCSS: ".tableexport-ignore" // (selector, selector[]), selector(s) to exclude cells from the exported file | ||
emptyCSS: ".tableexport-empty" // (selector, selector[]), selector(s) to replace cells with an empty string in the exported file | ||
ignoreRows: null, // (Number, Number[]), row indices to exclude from the exported file(s) | ||
ignoreCols: null, // (Number, Number[]), column indices to exclude from the exported file(s) | ||
ignoreCSS: ".tableexport-ignore", // (selector, selector[]), selector(s) to exclude cells from the exported file(s) | ||
emptyCSS: ".tableexport-empty", // (selector, selector[]), selector(s) to replace cells with an empty string in the exported file(s) | ||
trimWhitespace: false // (Boolean), remove all leading/trailing newlines, spaces, and tabs from cell text in the exported file(s) | ||
}); | ||
@@ -228,3 +244,5 @@ ``` | ||
### Live Demo | ||
A live, interactive demo can be found **[here](https://www.travismclarke.com/tableexport/#live-demo)**. Also, check out the [TableExport + RequireJS](https://github.com/clarketm/tableexport_requirejs_app/blob/master/README.md) skeleton, | ||
A live, interactive demo can be found on the **[TableExport](https://www.travismclarke.com/tableexport/#live-demo)** webpage. | ||
* [TableExport + RequireJS](https://github.com/clarketm/tableexport_requirejs_app) skeleton. | ||
* [TableExport + Flask](https://github.com/clarketm/tableexport_flask_app) skeleton. | ||
@@ -234,6 +252,6 @@ ### License | ||
### Credits | ||
### :star: Credits | ||
Special thanks the the following contributors: | ||
* [John Resig](https://github.com/jeresig) - jQuery | ||
* [SheetJS](https://github.com/SheetJS) - js-xlsx | ||
* [Eli Grey](https://github.com/eligrey) - FileSaver.js & Blob.js |
/*! | ||
* TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) | ||
* Copyright 2016 Travis Clarke | ||
* TableExport.js 4.0.0-alpha.5 (https://www.travismclarke.com) | ||
* Copyright 2017 Travis Clarke | ||
* Licensed under the MIT license | ||
@@ -18,3 +18,3 @@ */ | ||
} | ||
}(this, function (exports, $, Blob, saveAs, XLSX) { | ||
}(this || window, function (exports, $, Blob, saveAs, XLSX) { | ||
'use strict'; | ||
@@ -45,2 +45,5 @@ /** | ||
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; | ||
@@ -65,3 +68,3 @@ | ||
thAdj = self.settings.headings ? el.querySelectorAll('thead > tr').length : 0, | ||
fileName = self.settings.fileName === "id" ? (el.getAttribute('id') ? el.getAttribute('id') : TableExport.prototype.defaultFileName) : self.settings.fileName, | ||
filename = self.settings.filename === "id" ? (el.getAttribute('id') ? el.getAttribute('id') : TableExport.prototype.defaultFilename) : self.settings.filename, | ||
exporters = { | ||
@@ -103,6 +106,14 @@ xlsx: function (rDel, name) { | ||
} | ||
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); | ||
}), | ||
@@ -112,3 +123,3 @@ dataObject = TableExport.prototype.escapeHtml( | ||
data: dataURL, | ||
fileName: name, | ||
filename: name, | ||
mimeType: TableExport.prototype.xlsx.mimeType, | ||
@@ -156,6 +167,14 @@ fileExtension: TableExport.prototype.xlsx.fileExtension | ||
} | ||
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); | ||
}), | ||
@@ -165,3 +184,3 @@ dataObject = TableExport.prototype.escapeHtml( | ||
data: dataURL, | ||
fileName: name, | ||
filename: name, | ||
mimeType: TableExport.prototype.xls.mimeType, | ||
@@ -188,9 +207,14 @@ fileExtension: TableExport.prototype.xls.fileExtension | ||
} | ||
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, | ||
fileName: name, | ||
filename: name, | ||
mimeType: TableExport.prototype.xls.mimeType, | ||
@@ -217,3 +241,3 @@ fileExtension: TableExport.prototype.xls.fileExtension | ||
} | ||
return '"' + val.textContent.replace(/"/g, '""') + '"'; | ||
return '"' + formatValue(val.textContent.replace(/"/g, '""')) + '"'; | ||
}).join(colD); | ||
@@ -224,3 +248,3 @@ }).join(rdel), | ||
data: dataURL, | ||
fileName: name, | ||
filename: name, | ||
mimeType: TableExport.prototype.csv.mimeType, | ||
@@ -247,3 +271,3 @@ fileExtension: TableExport.prototype.csv.fileExtension | ||
} | ||
return val.textContent; | ||
return formatValue(val.textContent); | ||
}).join(colD); | ||
@@ -254,3 +278,3 @@ }).join(rdel), | ||
data: dataURL, | ||
fileName: name, | ||
filename: name, | ||
mimeType: TableExport.prototype.txt.mimeType, | ||
@@ -269,6 +293,10 @@ fileExtension: TableExport.prototype.txt.fileExtension | ||
!XLSX && key === 'xlsx' ? key = null : false; | ||
key && exporters[key](rowD, fileName); | ||
key && exporters[key](rowD, filename); | ||
} | ||
); | ||
/** | ||
* Initializes table caption with export buttons | ||
* @param exportButton {HTMLButtonElement} | ||
*/ | ||
function checkCaption(exportButton) { | ||
@@ -286,5 +314,13 @@ var caption = el.querySelectorAll('caption:not(.head)'); | ||
/** | ||
* 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; | ||
@@ -298,8 +334,8 @@ exportButton.textContent = myContent; | ||
_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, | ||
filename = object.filename, | ||
mimeType = object.mimeType, | ||
fileExtension = object.fileExtension; | ||
TableExport.prototype.export2file(data, mimeType, fileName, fileExtension); | ||
TableExport.prototype.export2file(data, mimeType, filename, fileExtension); | ||
}); | ||
@@ -315,3 +351,3 @@ | ||
*/ | ||
version: "4.0.0-alpha.4", | ||
version: "4.0.0-alpha.5", | ||
/** | ||
@@ -325,9 +361,10 @@ * Default plugin options. | ||
formats: ["xls", "csv", "txt"], // (String[]), filetype(s) for the export, (default: ["xls", "csv", "txt"]) | ||
fileName: "id", // (id, String), filename for the downloaded file, (default: "id") | ||
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) | ||
}, | ||
@@ -343,3 +380,3 @@ /** | ||
*/ | ||
defaultFileName: "myDownload", | ||
defaultFilename: "myDownload", | ||
/** | ||
@@ -409,2 +446,29 @@ * Class applied to each export button element. | ||
/** | ||
* 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 | ||
@@ -421,2 +485,31 @@ * @memberof TableExport.prototype | ||
/** | ||
* 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 | ||
@@ -437,3 +530,2 @@ * @memberof TableExport.prototype | ||
* @param data {String} | ||
* @returns {Number} epoch time | ||
*/ | ||
@@ -443,4 +535,5 @@ createSheet: function (data) { | ||
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; | ||
@@ -450,9 +543,14 @@ if (range.s.c > C) range.s.c = C; | ||
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'; | ||
@@ -462,3 +560,2 @@ cell.z = XLSX.SSF._table[14]; | ||
} | ||
else cell.t = 's'; | ||
@@ -489,3 +586,3 @@ ws[cell_ref] = cell; | ||
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; | ||
@@ -502,3 +599,3 @@ }, | ||
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(), | ||
@@ -523,27 +620,58 @@ ws = this.createSheet(data); | ||
/** | ||
* Updates the plugin instance with new/updated options | ||
* @param options {Object} TableExport configuration options | ||
* @returns {TableExport} updated TableExport instance | ||
* LocalStorage main interface constructor | ||
* @memberof TableExport.prototype | ||
* @constructor | ||
*/ | ||
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, settings, true); | ||
}, | ||
/** | ||
* Remove the instance (i.e. caption containing the export buttons) | ||
*/ | ||
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() { | ||
@@ -589,5 +717,5 @@ var args = arguments; | ||
exports.default = exports.TableExport = TableExport; | ||
return exports.default = exports.TableExport = TableExport; | ||
} | ||
)); |
/*! | ||
* TableExport.js 4.0.0-alpha.4 (https://www.travismclarke.com) | ||
* Copyright 2016 Travis Clarke | ||
* 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,o,n,r){"use strict";function s(){for(var t=arguments,e=1;e<t.length;e++)for(var o in t[e])t[e].hasOwnProperty(o)&&(t[0][o]=t[e][o]);return t[0]}function i(t){return[].slice.call(t)}function a(t,e,o){for(var n=0;n<t.length;++n)t[n].addEventListener(e,o,!1)}function l(t,e){return t.classList?t.classList.contains(e):new RegExp("(^| )"+e+"( |$)","gi").test(t.cls)}var p=function(t,o,n){var f=this;f.settings=n?o:s({},p.prototype.defaults,o),f.selectors=i(t);var u,c,x,y=p.prototype.rowDel,m=f.settings.ignoreRows instanceof Array?f.settings.ignoreRows:[f.settings.ignoreRows],d=f.settings.ignoreCols instanceof Array?f.settings.ignoreCols:[f.settings.ignoreCols],g=f.settings.ignoreCSS instanceof Array?f.settings.ignoreCSS.join(", "):f.settings.ignoreCSS,h=f.settings.emptyCSS instanceof Array?f.settings.emptyCSS.join(", "):f.settings.emptyCSS;f.settings.bootstrap?(u=p.prototype.bootstrap[0]+" ",c=p.prototype.bootstrap[1]+" ",x=p.prototype.bootstrap[2]+" "):(u=p.prototype.defaultButton+" ",c=x=""),f.selectors.forEach(function(t){function o(e){var o=t.querySelectorAll("caption:not(.head)");o.length?o[0].appendChild(e):(o=document.createElement("caption"),o.className=x+f.settings.position,o.appendChild(e),t.insertBefore(o,t.firstChild))}function s(t,e,n){var r=document.createElement("button");r.setAttribute("data-fileblob",t),r.className=u+c+n,r.textContent=e,o(r)}var a=t.querySelectorAll("caption:not(.head)");n&&a.parentNode.removeChild(a);var b=i(t.querySelectorAll("tbody > 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,o){var n={},r=i(b).map(function(t,o){if(!~m.indexOf(o-v)&&!l(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!l(t,g)){if(l(t,h))return" ";if(t.hasAttribute("colspan")&&(n[o]=n[o]||{},n[o][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var s=1;s<t.getAttribute("rowspan");s++)n[o+s]=n[o+s]||{},n[o+s][r]=1;if(n[o]){for(var i=r+1,a=0,p=0,s=0;s<=Math.max.apply(Math,Object.keys(n[o]))&&(n[o][s]?a=p>=r?a+n[o][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:o,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,o){var n={},r=i(b).map(function(t,o){if(!~m.indexOf(o-v)&&!l(t,g)){var r=t.querySelectorAll("th, td");return i(r).map(function(t,r){if(!~d.indexOf(r)&&!l(t,g)){if(l(t,h))return" ";if(t.hasAttribute("colspan")&&(n[o]=n[o]||{},n[o][r+1]=t.getAttribute("colspan")-1),t.hasAttribute("rowspan"))for(var s=1;s<t.getAttribute("rowspan");s++)n[o+s]=n[o+s]||{},n[o+s][r]=1;if(n[o]){for(var i=r+1,a=0,p=0,s=0;s<=Math.max.apply(Math,Object.keys(n[o]))&&(n[o][s]?a=p>=r?a+n[o][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:o,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 o=p.prototype.xls.separator,n=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,g)){var n=t.querySelectorAll("th, td");return i(n).map(function(t,e){if(!~d.indexOf(e)&&!l(t,g))return l(t,h)?" ":t.textContent}).join(o)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:n,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 o=p.prototype.csv.separator,n=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,g)){var n=t.querySelectorAll("th, td");return i(n).map(function(t,e){if(!~d.indexOf(e)&&!l(t,g))return l(t,h)?" ":'"'+t.textContent.replace(/"/g,'""')+'"'}).join(o)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:n,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 o=p.prototype.txt.separator,n=i(b).map(function(t,e){if(!~m.indexOf(e-v)&&!l(t,g)){var n=t.querySelectorAll("th, td");return i(n).map(function(t,e){if(!~d.indexOf(e)&&!l(t,g))return l(t,h)?" ":t.textContent}).join(o)}}).join(t),r=p.prototype.escapeHtml(JSON.stringify({data:n,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,o=t.fileName,n=t.mimeType,r=t.fileExtension;p.prototype.export2file(e,n,o,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 o=Date.parse(t);return(o-new Date(Date.UTC(1899,11,30)))/864e5},createSheet:function(t){for(var e={},o={s:{c:1e7,r:1e7},e:{c:0,r:0}},n=0;n!=t.length;++n)for(var s=0;s!=t[n].length;++s){o.s.r>n&&(o.s.r=n),o.s.c>s&&(o.s.c=s),o.e.r<n&&(o.e.r=n),o.e.c<s&&(o.e.c=s);var i={v:t[n][s]};if(null!=i.v){var a=r.utils.encode_cell({c:s,r:n});"number"==typeof i.v?i.t="n":"boolean"==typeof i.v?i.t="b":i.v instanceof Date?(i.t="n",i.z=r.SSF._table[14],i.v=this.dateNum(i.v)):i.t="s",e[a]=i}}return o.s.c<1e7&&(e["!ref"]=r.utils.encode_range(o)),e},Workbook:function(){this.SheetNames=[],this.Sheets={}},string2ArrayBuffer:function(t){for(var e=new ArrayBuffer(t.length),o=new Uint8Array(e),n=0;n!=t.length;++n)o[n]=255&t.charCodeAt(n);return e},export2file:function(t,e,s,i){if(r&&".xls"==i.substr(0,4)){var a=new this.Workbook,l=this.createSheet(t);a.SheetNames.push(s),a.Sheets[s]=l;var p={bookType:i.substr(1,3)+(i.substr(4)||"m"),bookSST:!1,type:"binary"},f=r.write(a,p);t=this.string2ArrayBuffer(f)}n(new o([t],{type:e+";"+this.charset}),s+i,!0)},update:function(t){return new p(this.selectors,s({},this.settings,t),(!0))},reset:function(){return new p(this.selectors,settings,(!0))},remove:function(){this.selectors.each(function(){var t=this.querySelectorAll("caption:not(.head)");t.parentNode.removeChild(t)})}},e){e.fn.tableExport=function(t,e){return new p(this,t,e)};for(var f in p.prototype)e.fn.tableExport[f]=p.prototype[f]}t["default"]=t.TableExport=p}); | ||
!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||window,function(t,e,n,r,o){"use strict";function s(){function t(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}return t()+t()+"-"+t()+"-"+t()+"-"+t()+"-"+t()+t()+t()}function a(){for(var t=arguments,e=1;e<t.length;e++)for(var n in t[e])t[e].hasOwnProperty(n)&&(t[0][n]=t[e][n]);return t[0]}function i(t){return[].slice.call(t)}function l(t,e,n){for(var r=0;r<t.length;++r)t[r].addEventListener(e,n,!1)}function p(t,e){return t.classList?t.classList.contains(e):new RegExp("(^| )"+e+"( |$)","gi").test(t.cls)}var u=function(t,e,n){var r=this;r.settings=n?e:a({},u.prototype.defaults,e),r.selectors=i(t);var f,c,y,m=u.prototype.rowDel,x=r.settings.ignoreRows instanceof Array?r.settings.ignoreRows:[r.settings.ignoreRows],d=r.settings.ignoreCols instanceof Array?r.settings.ignoreCols:[r.settings.ignoreCols],g=r.settings.ignoreCSS instanceof Array?r.settings.ignoreCSS.join(", "):r.settings.ignoreCSS,h=r.settings.emptyCSS instanceof Array?r.settings.emptyCSS.join(", "):r.settings.emptyCSS,b=u.prototype.formatValue.bind(this,r.settings.trimWhitespace),v=u.prototype.getType,C=new u.prototype.LocalStorage.getInstance;r.settings.bootstrap?(f=u.prototype.bootstrap[0]+" ",c=u.prototype.bootstrap[1]+" ",y=u.prototype.bootstrap[2]+" "):(f=u.prototype.defaultButton+" ",c=y=""),r.selectors.forEach(function(t){function e(e){var n=t.querySelectorAll("caption:not(.head)");n.length?n[0].appendChild(e):(n=document.createElement("caption"),n.className=y+r.settings.position,n.appendChild(e),t.insertBefore(n,t.firstChild))}function a(t,n,r){var o=document.createElement("button"),a=s();o.setAttribute("data-fileblob",a),C.setItem(a,t,!0),o.className=f+c+r,o.textContent=n,e(o)}var l=t.querySelectorAll("caption:not(.head)");n&&l.parentNode.removeChild(l);var S=i(t.querySelectorAll("tbody > 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<t.getAttribute("rowspan");o++)n[e+o]=n[e+o]||{},n[e+o][r]=1;if(n[e]){for(var s=r+1,a=0,i=0,o=0;o<=Math.max.apply(Math,Object.keys(n[e]))&&(n[e][o]?a=i>=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<t.getAttribute("rowspan");o++)n[e+o]=n[e+o]||{},n[e+o][r]=1;if(n[e]){for(var s=r+1,a=0,i=0,o=0;o<=Math.max.apply(Math,Object.keys(n[e]))&&(n[e][o]?a=i>=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<s&&(n.e.r=s),n.e.c<a&&(n.e.c=a);var i=t[s][a];if(i&&i.v){var l=o.utils.encode_cell({c:a,r:s});i.t||(r.number.assert(i.v)?i.t="n":r["boolean"].assert(i.v)?i.t="b":r.date.assert(i.v)?i.t="d":i.t="s"),"d"===i.t&&(i.t="n",i.z=o.SSF._table[14],i.v=this.dateNum(i.v)),e[l]=i}}return n.s.c<1e7&&(e["!ref"]=o.utils.encode_range(n)),e},Workbook:function(){this.SheetNames=[],this.Sheets={}},string2ArrayBuffer:function(t){for(var e=new ArrayBuffer(t.length),n=new Uint8Array(e),r=0;r!==t.length;++r)n[r]=255&t.charCodeAt(r);return e},export2file:function(t,e,s,a){if(o&&".xls"===a.substr(0,4)){var i=new this.Workbook,l=this.createSheet(t);i.SheetNames.push(s),i.Sheets[s]=l;var p={bookType:a.substr(1,3)+(a.substr(4)||"m"),bookSST:!1,type:"binary"},u=o.write(i,p);t=this.string2ArrayBuffer(u)}r(new n([t],{type:e+";"+this.charset}),s+a,!0)},LocalStorage:function(){this.type="localStorage",this.store=t[this.type],this.namespace="te",this.getKey=function(t){return this.namespace+t},this.setItem=function(t,e,n){var r=this.getKey(t);if(!this.exists(r)||n)return this.store.setItem(r,e)},this.getItem=function(t){var e=this.getKey(t);return this.store.getItem(e)},this.exists=function(t){var e=this.getKey(t);return null!==this.store.getItem(e)},this.removeItem=function(t){var e=this.getKey(t);return this.store.removeItem(e)},this.error=function(t){return new Error("unknown error occurred",t)}}};var f=u.prototype.LocalStorage;if(f._instance=null,f.getInstance=function(){return f._instance||(f._instance=new f),f._instance},e){e.fn.tableExport=function(t,e){return new u(this,t,e)};for(var c in u.prototype)e.fn.tableExport[c]=u.prototype[c]}return t["default"]=t.TableExport=u}); |
@@ -41,3 +41,3 @@ /** | ||
*/ | ||
defaultFileName: string; | ||
defaultFilename: string; | ||
@@ -93,2 +93,8 @@ /** | ||
/** | ||
* Cell-types override and assertion configuration | ||
* @memberof TableExport.prototype | ||
*/ | ||
types: Types; | ||
/** | ||
* Escapes special characters with HTML entities | ||
@@ -102,2 +108,19 @@ * @memberof TableExport.prototype | ||
/** | ||
* 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 | ||
@@ -117,3 +140,3 @@ * @memberof TableExport.prototype | ||
*/ | ||
createSheet: (data: string) => number; | ||
createSheet: (data: string) => void; | ||
@@ -174,3 +197,3 @@ /** | ||
formats: string[]; | ||
fileName: string; | ||
filename: string; | ||
bootstrap: boolean; | ||
@@ -182,2 +205,3 @@ position: string; | ||
emptyCSS: string; | ||
trimWhitespace: boolean; | ||
} | ||
@@ -233,2 +257,18 @@ | ||
/** | ||
* 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 { | ||
@@ -235,0 +275,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7522280
167
52298
252