UNPKG

tabulator-tables

Version:

Interactive table generation JavaScript library

2 lines 5.5 kB
/* Tabulator v4.9.3 (c) Oliver Folkerd */ var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},Download=function(e){this.table=e};Download.prototype.download=function(e,o,t,n,a){function r(t,n){a?!0===a?i.triggerDownload(t,n,e,o,!0):a(t):i.triggerDownload(t,n,e,o)}var i=this,l=!1;if("function"==typeof e?l=e:i.downloaders[e]?l=i.downloaders[e]:console.warn("Download Error - No such download type found: ",e),l){var s=this.generateExportList(n);l.call(this.table,s,t||{},r)}},Download.prototype.generateExportList=function(e){var o=this.table.modules.export.generateExportList(this.table.options.downloadConfig,!1,e||this.table.options.downloadRowRange,"download"),t=this.table.options.groupHeaderDownload;return t&&!Array.isArray(t)&&(t=[t]),o.forEach(function(e){var o;"group"===e.type&&(o=e.columns[0],t&&t[e.indent]&&(o.value=t[e.indent](o.value,e.component._group.getRowCount(),e.component._group.getData(),e.component)))}),o},Download.prototype.triggerDownload=function(e,o,t,n,a){var r=document.createElement("a"),i=new Blob([e],{type:o}),n=n||"Tabulator."+("function"==typeof t?"txt":t);(i=this.table.options.downloadReady.call(this.table,e,i))&&(a?window.open(window.URL.createObjectURL(i)):navigator.msSaveOrOpenBlob?navigator.msSaveOrOpenBlob(i,n):(r.setAttribute("href",window.URL.createObjectURL(i)),r.setAttribute("download",n),r.style.display="none",document.body.appendChild(r),r.click(),document.body.removeChild(r)),this.table.options.downloadComplete&&this.table.options.downloadComplete())},Download.prototype.commsReceived=function(e,o,t){switch(o){case"intercept":this.download(t.type,"",t.options,t.active,t.intercept)}},Download.prototype.downloaders={csv:function(e,o,t){var n=o&&o.delimiter?o.delimiter:",",a=[],r=[];e.forEach(function(e){var o=[];switch(e.type){case"group":console.warn("Download Warning - CSV downloader cannot process row groups");break;case"calc":console.warn("Download Warning - CSV downloader cannot process column calculations");break;case"header":e.columns.forEach(function(e,o){e&&1===e.depth&&(r[o]=void 0===e.value||null===e.value?"":'"'+String(e.value).split('"').join('""')+'"')});break;case"row":e.columns.forEach(function(e){if(e){switch(_typeof(e.value)){case"object":e.value=JSON.stringify(e.value);break;case"undefined":case"null":e.value=""}o.push('"'+String(e.value).split('"').join('""')+'"')}}),a.push(o.join(n))}}),r.length&&a.unshift(r.join(n)),a=a.join("\n"),o.bom&&(a="\ufeff"+a),t(a,"text/csv")},json:function(e,o,t){var n=[];e.forEach(function(e){var o={};switch(e.type){case"header":break;case"group":console.warn("Download Warning - JSON downloader cannot process row groups");break;case"calc":console.warn("Download Warning - JSON downloader cannot process column calculations");break;case"row":e.columns.forEach(function(e){e&&(o[e.component.getField()]=e.value)}),n.push(o)}}),n=JSON.stringify(n,null,"\t"),t(n,"application/json")},pdf:function(e,o,t){function n(e,o){var t=[];return e.columns.forEach(function(e){var n;if(e){switch(_typeof(e.value)){case"object":e.value=JSON.stringify(e.value);break;case"undefined":case"null":e.value=""}n={content:e.value,colSpan:e.width,rowSpan:e.height},o&&(n.styles=o),t.push(n)}else t.push("")}),t}var a=[],r=[],i={},l=o.rowGroupStyles||{fontStyle:"bold",fontSize:12,cellPadding:6,fillColor:220},s=o.rowCalcStyles||{fontStyle:"bold",fontSize:10,cellPadding:4,fillColor:232},c=o.jsPDF||{},u=o&&o.title?o.title:"";c.orientation||(c.orientation=o.orientation||"landscape"),c.unit||(c.unit="pt"),e.forEach(function(e){switch(e.type){case"header":a.push(n(e));break;case"group":r.push(n(e,l));break;case"calc":r.push(n(e,s));break;case"row":r.push(n(e))}});var d=new jsPDF(c);o&&o.autoTable&&(i="function"==typeof o.autoTable?o.autoTable(d)||{}:o.autoTable),u&&(i.addPageContent=function(e){d.text(u,40,30)}),i.head=a,i.body=r,d.autoTable(i),o&&o.documentProcessing&&o.documentProcessing(d),t(d.output("arraybuffer"),"application/pdf")},xlsx:function(e,o,t){function n(){var o=[],t=[],n={},a={s:{c:0,r:0},e:{c:e[0]?e[0].columns.reduce(function(e,o){return e+(o&&o.width?o.width:1)},0):0,r:e.length}};return e.forEach(function(e,n){var a=[];e.columns.forEach(function(e,o){e?(a.push(e.value instanceof Date||"object"!==_typeof(e.value)?e.value:JSON.stringify(e.value)),(e.width>1||e.height>-1)&&t.push({s:{r:n,c:o},e:{r:n+e.height-1,c:o+e.width-1}})):a.push("")}),o.push(a)}),XLSX.utils.sheet_add_aoa(n,o),n["!ref"]=XLSX.utils.encode_range(a),t.length&&(n["!merges"]=t),n}var a,r=this,i=o.sheetName||"Sheet1",l=XLSX.utils.book_new();if(l.SheetNames=[],l.Sheets={},o.sheetOnly)return void t(n());if(o.sheets)for(var s in o.sheets)!0===o.sheets[s]?(l.SheetNames.push(s),l.Sheets[s]=n()):(l.SheetNames.push(s),this.modules.comms.send(o.sheets[s],"download","intercept",{type:"xlsx",options:{sheetOnly:!0},active:r.active,intercept:function(e){l.Sheets[s]=e}}));else l.SheetNames.push(i),l.Sheets[i]=n();o.documentProcessing&&(l=o.documentProcessing(l)),a=XLSX.write(l,{bookType:"xlsx",bookSST:!0,type:"binary"}),t(function(e){for(var o=new ArrayBuffer(e.length),t=new Uint8Array(o),n=0;n!=e.length;++n)t[n]=255&e.charCodeAt(n);return o}(a),"application/octet-stream")},html:function(e,o,t){this.modExists("export",!0)&&t(this.modules.export.genereateHTMLTable(e),"text/html")}},Tabulator.prototype.registerModule("download",Download);