blob: 6c5c5f89b51d36ff6a65dfb0f6d67579d1fc9ef9 [file] [log] [blame]
(function(c){c.fn.extend({tableExport:function(q){function v(b,t,d,e,r){if(-1==c.inArray(d,a.ignoreRow)&&-1==c.inArray(d-e,a.ignoreRow)){var L=c(b).filter(function(){return"none"!=c(this).data("tableexport-display")&&(c(this).is(":visible")||"always"==c(this).data("tableexport-display")||"always"==c(this).closest("table").data("tableexport-display"))}).find(t);L.each(function(b){if(("always"==c(this).data("tableexport-display")||"none"!=c(this).css("display")&&"hidden"!=c(this).css("visibility")&&
"none"!=c(this).data("tableexport-display"))&&-1==c.inArray(b,a.ignoreColumn)&&-1==c.inArray(b-L.length,a.ignoreColumn)&&"function"===typeof r){var e,t=0;if("undefined"!=typeof A[d]&&0<A[d].length)for(e=0;e<=b;e++)"undefined"!=typeof A[d][e]&&(r(null,d,e),delete A[d][e],b++);r(this,d,b);if(c(this).is("[colspan]"))for(t=parseInt(c(this).attr("colspan")),e=0;e<t-1;e++)r(null,d,b+e);if(c(this).is("[rowspan]")){var f,g=parseInt(c(this).attr("rowspan"));for(f=1;f<g;f++)for("undefined"==typeof A[d+f]&&
(A[d+f]=[]),A[d+f][b]="",e=1;e<t;e++)A[d+f][b+e]=""}}})}}function M(b){!0===a.consoleLog&&console.log(b.output());if("string"==a.outputMode)return b.output();if("base64"==a.outputMode)return B(b.output());try{var t=b.output("blob");saveAs(t,a.fileName+".pdf")}catch(d){C(a.fileName+".pdf","data:application/pdf;base64,"+B(b.output()))}}function N(b,a,d){var e=0;"undefined"!=typeof d&&(e=d.colspan);if(0<=e){d=b.width;for(var c=b.textPos.x,f=a.table.columns.indexOf(a.column),g=1;g<e;g++)d+=a.table.columns[f+
g].width;1<e&&("right"===b.styles.halign?c=b.textPos.x+d-b.width:"center"===b.styles.halign&&(c=b.textPos.x+(d-b.width)/2));b.width=d;b.textPos.x=c;if("middle"===b.styles.valign||"bottom"===b.styles.valign)e=("string"===typeof b.text?b.text.split(/\r\n|\r|\n/g):b.text).length||1,2<e&&(b.textPos.y-=(2-1.15)/2*a.row.styles.fontSize*(e-2)/3);return!0}return!1}function I(b,a,d){return b.replace(new RegExp(a.replace(/([.*+?^=!:${}()|\[\]\/\\])/g,"\\$1"),"g"),d)}function O(b,t,d){var e="";if(null!=b)if(b=
w(b,t,d),t=null===b||""==b?"":b.toString(),b instanceof Date)e=a.csvEnclosure+b.toLocaleString()+a.csvEnclosure;else if(e=I(t,a.csvEnclosure,a.csvEnclosure+a.csvEnclosure),0<=e.indexOf(a.csvSeparator)||/[\r\n ]/g.test(e))e=a.csvEnclosure+e+a.csvEnclosure;return e}function W(b){b=I(b||"0",a.numbers.html.decimalMark,".");b=I(b,a.numbers.html.thousandsSeparator,"");return"number"===typeof b||!1!==jQuery.isNumeric(b)?b:!1}function w(b,t,d){var e="";if(null!=b){b=c(b);var r=b.html();"function"===typeof a.onCellHtmlData&&
(r=a.onCellHtmlData(b,t,d,r));if(!0===a.htmlContent)e=c.trim(r);else{var f=r.replace(/\n/g,"\u2028").replace(/<br\s*[\/]?>/gi,"\u2060"),r=c("<div/>").html(f).contents(),f="";c.each(r.text().split("\u2028"),function(b,a){0<b&&(f+=" ");f+=c.trim(a)});c.each(f.split("\u2060"),function(b,a){0<b&&(e+="\n");e+=c.trim(a).replace(/\u00AD/g,"")});if(a.numbers.html.decimalMark!=a.numbers.output.decimalMark||a.numbers.html.thousandsSeparator!=a.numbers.output.thousandsSeparator)if(r=W(e),!1!==r){var g=(""+r).split(".");
1==g.length&&(g[1]="");var h=3<g[0].length?g[0].length%3:0,e=(0>r?"-":"")+(a.numbers.output.thousandsSeparator?(h?g[0].substr(0,h)+a.numbers.output.thousandsSeparator:"")+g[0].substr(h).replace(/(\d{3})(?=\d)/g,"$1"+a.numbers.output.thousandsSeparator):g[0])+(g[1].length?a.numbers.output.decimalMark+g[1]:"")}}!0===a.escape&&(e=escape(e));"function"===typeof a.onCellData&&(e=a.onCellData(b,t,d,e))}return e}function X(b,a,d){return a+"-"+d.toLowerCase()}function P(b,a){var d=/^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/.exec(b),
e=a;d&&(e=[parseInt(d[1]),parseInt(d[2]),parseInt(d[3])]);return e}function Q(b){var a=D(b,"text-align"),d=D(b,"font-weight"),e=D(b,"font-style"),f="";"start"==a&&(a="rtl"==D(b,"direction")?"right":"left");700<=d&&(f="bold");"italic"==e&&(f+=e);""==f&&(f="normal");return{style:{align:a,bcolor:P(D(b,"background-color"),[255,255,255]),color:P(D(b,"color"),[0,0,0]),fstyle:f},colspan:parseInt(c(b).attr("colspan"))||0}}function D(b,a){try{return window.getComputedStyle?(a=a.replace(/([a-z])([A-Z])/,X),
window.getComputedStyle(b,null).getPropertyValue(a)):b.currentStyle?b.currentStyle[a]:b.style[a]}catch(d){}return""}function J(b,a,d){a=D(b,a).match(/\d+/);if(null!==a){a=a[0];var e=document.createElement("div");e.style.overflow="hidden";e.style.visibility="hidden";b.parentElement.appendChild(e);e.style.width=100+d;d=100/e.offsetWidth;b.parentElement.removeChild(e);return a*d}return 0}function C(a,f){var d=document.createElement("a");if(d){d.style.display="none";d.download=a;d.href=f;document.body.appendChild(d);
if(document.createEvent)null==G&&(G=document.createEvent("MouseEvents")),G.initEvent("click",!0,!1),d.dispatchEvent(G);else if(document.createEventObject)d.fireEvent("onclick");else if("function"==typeof d.onclick)d.onclick();document.body.removeChild(d)}}function B(a){var f="",d,e,c,g,h,k,l=0;a=a.replace(/\x0d\x0a/g,"\n");e="";for(c=0;c<a.length;c++)g=a.charCodeAt(c),128>g?e+=String.fromCharCode(g):(127<g&&2048>g?e+=String.fromCharCode(g>>6|192):(e+=String.fromCharCode(g>>12|224),e+=String.fromCharCode(g>>
6&63|128)),e+=String.fromCharCode(g&63|128));for(a=e;l<a.length;)d=a.charCodeAt(l++),e=a.charCodeAt(l++),c=a.charCodeAt(l++),g=d>>2,d=(d&3)<<4|e>>4,h=(e&15)<<2|c>>6,k=c&63,isNaN(e)?h=k=64:isNaN(c)&&(k=64),f=f+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(g)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(d)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(h)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(k);
return f}var a={consoleLog:!1,csvEnclosure:'"',csvSeparator:",",csvUseBOM:!0,displayTableName:!1,escape:!1,excelstyles:["border-bottom","border-top","border-left","border-right"],fileName:"tableExport",htmlContent:!1,ignoreColumn:[],ignoreRow:[],jspdf:{orientation:"p",unit:"pt",format:"a4",margins:{left:20,right:10,top:10,bottom:10},autotable:{styles:{cellPadding:2,rowHeight:12,fontSize:8,fillColor:255,textColor:50,fontStyle:"normal",overflow:"ellipsize",halign:"left",valign:"middle"},headerStyles:{fillColor:[52,
73,94],textColor:255,fontStyle:"bold",halign:"center"},alternateRowStyles:{fillColor:245},tableExport:{onAfterAutotable:null,onBeforeAutotable:null,onTable:null}}},numbers:{html:{decimalMark:".",thousandsSeparator:","},output:{decimalMark:".",thousandsSeparator:","}},onCellData:null,onCellHtmlData:null,outputMode:"file",tbodySelector:"tr",theadSelector:"tr",tableName:"myTableName",type:"csv",worksheetName:"xlsWorksheetName"},G=null,l=[],n=[],m=0,A=[],f="";c.extend(!0,a,q);if("csv"==a.type||"txt"==
a.type){var p="",m=0,l=c(this).find("thead").first().find(a.theadSelector);l.each(function(){f="";v(this,"th,td",m,l.length,function(b,c,d){f+=O(b,c,d)+a.csvSeparator});f=c.trim(f).substring(0,f.length-1);0<f.length&&(0<p.length&&(p+="\n"),p+=f);m++});n=c(this).find("tbody").first().find(a.tbodySelector);n.each(function(){f="";v(this,"td",m,l.length+n.length,function(b,c,d){f+=O(b,c,d)+a.csvSeparator});f=c.trim(f).substring(0,f.length-1);0<f.length&&(0<p.length&&(p+="\n"),p+=f);m++});p+="\n";!0===
a.consoleLog&&console.log(p);if("string"===a.outputMode)return p;if("base64"===a.outputMode)return B(p);try{var x=new Blob([p],{type:"text/"+("csv"==a.type?"csv":"plain")+";charset=utf-8"});saveAs(x,a.fileName+"."+a.type,"csv"!=a.type||!1===a.csvUseBOM)}catch(b){C(a.fileName+"."+a.type,"data:text/"+("csv"==a.type?"csv":"plain")+";charset=utf-8,"+encodeURIComponent(p))}}else if("sql"==a.type){var m=0,k="INSERT INTO `"+a.tableName+"` (",l=c(this).find("thead").first().find(a.theadSelector);l.each(function(){v(this,
"th,td",m,l.length,function(a,c,d){k+="'"+w(a,c,d)+"',"});m++;k=c.trim(k);k=c.trim(k).substring(0,k.length-1)});k+=") VALUES ";n=c(this).find("tbody").first().find(a.tbodySelector);n.each(function(){f="";v(this,"td",m,l.length+n.length,function(a,c,d){f+="'"+w(a,c,d)+"',"});3<f.length&&(k+="("+f,k=c.trim(k).substring(0,k.length-1),k+="),");m++});k=c.trim(k).substring(0,k.length-1);k+=";";!0===a.consoleLog&&console.log(k);if("string"==a.outputMode)return k;if("base64"==a.outputMode)return B(k);try{x=
new Blob([k],{type:"text/plain;charset=utf-8"}),saveAs(x,a.fileName+".sql")}catch(b){C(a.fileName+".sql","data:application/sql;charset=utf-8,"+encodeURIComponent(k))}}else if("json"==a.type){var R=[],l=c(this).find("thead").first().find(a.theadSelector);l.each(function(){var a=[];v(this,"th,td",m,l.length,function(c,d,e){a.push(w(c,d,e))});R.push(a)});var S=[],n=c(this).find("tbody").first().find(a.tbodySelector);n.each(function(){var a=[];v(this,"td",m,l.length+n.length,function(c,d,e){a.push(w(c,
d,e))});0<a.length&&(1!=a.length||""!=a[0])&&S.push(a);m++});q=[];q.push({header:R,data:S});var z=JSON.stringify(q);!0===a.consoleLog&&console.log(z);if("string"==a.outputMode)return z;q=B(z);if("base64"==a.outputMode)return q;try{x=new Blob([z],{type:"application/json;charset=utf-8"}),saveAs(x,a.fileName+".json")}catch(b){C(a.fileName+".json","data:application/json;charset=utf-8;base64,"+q)}}else if("xml"===a.type){var m=0,u='<?xml version="1.0" encoding="utf-8"?>',u=u+"<tabledata><fields>",l=c(this).find("thead").first().find(a.theadSelector);
l.each(function(){v(this,"th,td",m,n.length,function(a,c,d){u+="<field>"+w(a,c,d)+"</field>"});m++});var u=u+"</fields><data>",T=1,n=c(this).find("tbody").first().find(a.tbodySelector);n.each(function(){var a=1;f="";v(this,"td",m,l.length+n.length,function(c,d,e){f+="<column-"+a+">"+w(c,d,e)+"</column-"+a+">";a++});0<f.length&&"<column-1></column-1>"!=f&&(u+='<row id="'+T+'">'+f+"</row>",T++);m++});u+="</data></tabledata>";!0===a.consoleLog&&console.log(u);if("string"==a.outputMode)return u;q=B(u);
if("base64"==a.outputMode)return q;try{x=new Blob([u],{type:"application/xml;charset=utf-8"}),saveAs(x,a.fileName+".xml")}catch(b){C(a.fileName+".xml","data:application/xml;charset=utf-8;base64,"+q)}}else if("excel"==a.type||"xls"==a.type||"word"==a.type||"doc"==a.type){var z="excel"==a.type||"xls"==a.type?"excel":"word",K="excel"==z?"xls":"doc";q="xls"==K?'xmlns:x="urn:schemas-microsoft-com:office:excel"':'xmlns:w="urn:schemas-microsoft-com:office:word"';var m=0,y="<table>",l=c(this).find("thead").first().find(a.theadSelector);
l.each(function(){f="";v(this,"th,td",m,l.length,function(b,g,d){if(null!=b){f+='<td style="';for(var e in a.excelstyles)a.excelstyles.hasOwnProperty(e)&&(f+=a.excelstyles[e]+": "+c(b).css(a.excelstyles[e])+";");f+='">'+w(b,g,d)+"</td>"}});0<f.length&&(y+="<tr>"+f+"</tr>");m++});n=c(this).find("tbody").first().find(a.tbodySelector);n.each(function(){f="";v(this,"td",m,l.length+n.length,function(b,g,d){if(null!=b){f+='<td style="';for(var e in a.excelstyles)a.excelstyles.hasOwnProperty(e)&&(f+=a.excelstyles[e]+
": "+c(b).css(a.excelstyles[e])+";");c(b).is("[colspan]")&&(f+='" colspan="'+c(b).attr("colspan"));c(b).is("[rowspan]")&&(f+='" rowspan="'+c(b).attr("rowspan"));f+='">'+w(b,g,d)+"</td>"}});0<f.length&&(y+="<tr>"+f+"</tr>");m++});a.displayTableName&&(y+="<tr><td></td></tr><tr><td></td></tr><tr><td>"+w(c("<p>"+a.tableName+"</p>"))+"</td></tr>");y+="</table>";!0===a.consoleLog&&console.log(y);var g;g='<html xmlns:o="urn:schemas-microsoft-com:office:office" '+q+' xmlns="http://www.w3.org/TR/REC-html40">'+
('<meta http-equiv="content-type" content="application/vnd.ms-'+z+'; charset=UTF-8">');g+="<head>";"excel"===z&&(g+="\x3c!--[if gte mso 9]>",g+="<xml>",g+="<x:ExcelWorkbook>",g+="<x:ExcelWorksheets>",g+="<x:ExcelWorksheet>",g+="<x:Name>",g+=a.worksheetName,g+="</x:Name>",g+="<x:WorksheetOptions>",g+="<x:DisplayGridlines/>",g+="</x:WorksheetOptions>",g+="</x:ExcelWorksheet>",g+="</x:ExcelWorksheets>",g+="</x:ExcelWorkbook>",g+="</xml>",g+="<![endif]--\x3e");g+="</head>";g+="<body>";g+=y;g+="</body>";
g+="</html>";if("string"==a.outputMode)return g;q=B(g);if("base64"===a.outputMode)return q;try{x=new Blob([g],{type:"application/vnd.ms-"+a.type}),saveAs(x,a.fileName+"."+K)}catch(b){C(a.fileName+"."+K,"data:application/vnd.ms-"+z+";base64,"+q)}}else if("png"==a.type)html2canvas(c(this)[0],{allowTaint:!0,background:"#fff",onrendered:function(b){b=b.toDataURL();b=b.substring(22);for(var c=atob(b),d=new ArrayBuffer(c.length),e=new Uint8Array(d),f=0;f<c.length;f++)e[f]=c.charCodeAt(f);try{var g=new Blob([d],
{type:"image/png"});saveAs(g,a.fileName+".png")}catch(h){C(a.fileName+".png","data:image/png;base64,"+b)}}});else if("pdf"==a.type)if(!1===a.jspdf.autotable){var x={dim:{w:J(c(this).first().get(0),"width","mm"),h:J(c(this).first().get(0),"height","mm")},pagesplit:!1},U=new jsPDF(a.jspdf.orientation,a.jspdf.unit,a.jspdf.format);U.addHTML(c(this).first(),a.jspdf.margins.left,a.jspdf.margins.top,x,function(){M(U)})}else{var h=a.jspdf.autotable.tableExport;if("string"===typeof a.jspdf.format&&"bestfit"===
a.jspdf.format.toLowerCase()){var E={a0:[2383.94,3370.39],a1:[1683.78,2383.94],a2:[1190.55,1683.78],a3:[841.89,1190.55],a4:[595.28,841.89]},H="",F="",V=0;c(this).filter(":visible").each(function(){if("none"!=c(this).css("display")){var a=J(c(this).get(0),"width","pt");if(a>V){a>E.a0[0]&&(H="a0",F="l");for(var f in E)E.hasOwnProperty(f)&&E[f][1]>a&&(H=f,F="l",E[f][0]>a&&(F="p"));V=a}}});a.jspdf.format=""==H?"a4":H;a.jspdf.orientation=""==F?"w":F}h.doc=new jsPDF(a.jspdf.orientation,a.jspdf.unit,a.jspdf.format);
c(this).filter(function(){return"none"!=c(this).data("tableexport-display")&&(c(this).is(":visible")||"always"==c(this).data("tableexport-display"))}).each(function(){var b,f=0;h.columns=[];h.rows=[];h.rowoptions={};if("function"===typeof h.onTable&&!1===h.onTable(c(this),a))return!0;a.jspdf.autotable.tableExport=null;var d=c.extend(!0,{},a.jspdf.autotable);a.jspdf.autotable.tableExport=h;d.margin={};c.extend(!0,d.margin,a.jspdf.margins);"function"!==typeof d.beforePageContent&&(d.beforePageContent=
function(a){1==a.pageCount&&a.table.rows.concat(a.table.headerRow).forEach(function(b){0<b.height&&(b.height+=(2-1.15)/2*b.styles.fontSize,a.table.height+=(2-1.15)/2*b.styles.fontSize)})});"function"!==typeof d.createdHeaderCell&&(d.createdHeaderCell=function(a,b){if("undefined"!=typeof h.columns[b.column.dataKey]){var c=h.columns[b.column.dataKey];a.styles.halign=c.style.align;"inherit"===d.styles.fillColor&&(a.styles.fillColor=c.style.bcolor);"inherit"===d.styles.textColor&&(a.styles.textColor=
c.style.color);"inherit"===d.styles.fontStyle&&(a.styles.fontStyle=c.style.fstyle)}});"function"!==typeof d.createdCell&&(d.createdCell=function(a,b){var c=h.rowoptions[b.row.index+":"+b.column.dataKey];"undefined"!=typeof c&&"undefined"!=typeof c.style&&(a.styles.halign=c.style.align,"inherit"===d.styles.fillColor&&(a.styles.fillColor=c.style.bcolor),"inherit"===d.styles.textColor&&(a.styles.textColor=c.style.color),"inherit"===d.styles.fontStyle&&(a.styles.fontStyle=c.style.fstyle))});"function"!==
typeof d.drawHeaderCell&&(d.drawHeaderCell=function(a,b){var c=h.columns[b.column.dataKey];return 1!=c.style.hasOwnProperty("hidden")||!0!==c.style.hidden?N(a,b,c):!1});"function"!==typeof d.drawCell&&(d.drawCell=function(a,b){return N(a,b,h.rowoptions[b.row.index+":"+b.column.dataKey])});l=c(this).find("thead").find(a.theadSelector);l.each(function(){b=0;v(this,"th,td",f,l.length,function(a,c,d){var e=Q(a);e.title=w(a,c,d);e.key=b++;h.columns.push(e)});f++});var e=0;n=c(this).find("tbody").find(a.tbodySelector);
n.each(function(){var a=[];b=0;v(this,"td",f,l.length+n.length,function(d,f,g){if("undefined"===typeof h.columns[b]){var k={title:"",key:b,style:{hidden:!0}};h.columns.push(k)}null!==d?h.rowoptions[e+":"+b++]=Q(d):(k=c.extend(!0,{},h.rowoptions[e+":"+(b-1)]),k.colspan=-1,h.rowoptions[e+":"+b++]=k);a.push(w(d,f,g))});a.length&&(h.rows.push(a),e++);f++});if("function"===typeof h.onBeforeAutotable)h.onBeforeAutotable(c(this),h.columns,h.rows,d);h.doc.autoTable(h.columns,h.rows,d);if("function"===typeof h.onAfterAutotable)h.onAfterAutotable(c(this),
d);a.jspdf.autotable.startY=h.doc.autoTableEndPosY()+d.margin.top});M(h.doc);h.columns.length=0;h.rows.length=0;delete h.doc;h.doc=null}return this}})})(jQuery);