tablesorter/dist/js/widgets/widget-output.min.js

1 line
5.0 KiB
JavaScript

!function(a){"use strict";var b=a.tablesorter,c=b.output={event:"outputTable",regexQuote:/([\n\t]|<[^<]+>)/,regexBR:/(<br([\s\/])?>|\n)/g,regexIMG:/<img[^>]+alt\s*=\s*['"]([^'"]+)['"][^>]*>/i,regexHTML:/<[^<]+>/g,replaceCR:"\\n",replaceTab:"\\t",popupTitle:"Output",popupStyle:"width:100%;height:100%;",message:"Your device does not support downloading. Please try again in desktop browser.",init:function(a){a.$table.off(c.event).on(c.event,function(){c.process(a,a.widgetOptions)})},processRow:function(b,d,e,f){var g,h,i,j,k,l,m=b.widgetOptions,n=[],o=m.output_duplicateSpans,p=e&&f&&m.output_headerRows&&a.isFunction(m.output_callbackJSON),q=0;return d.each(function(b){n[b]||(n[b]=[]),q=0,a(this).children().each(function(){if(g=a(this),g.filter("[rowspan]").length)for(j=parseInt(g.attr("rowspan"),10)-1,l=c.formatData(m,g.attr(m.output_dataAttrib)||g.html(),e),h=1;j>=h;h++)n[b+h]||(n[b+h]=[]),n[b+h][q]=e?l:o?l:"";if(g.filter("[colspan]").length)for(k=parseInt(g.attr("colspan"),10)-1,l=c.formatData(m,g.attr(m.output_dataAttrib)||g.html(),e),i=1;k>=i;i++)if(g.filter("[rowspan]").length)for(j=parseInt(g.attr("rowspan"),10),h=0;j>h;h++)n[b+h]||(n[b+h]=[]),n[b+h][q+i]=p?m.output_callbackJSON(g,l,q+i)||l+"("+(q+i)+")":e?l:o?l:"";else n[b][q+i]=p?m.output_callbackJSON(g,l,q+i)||l+"("+(q+i)+")":e?l:o?l:"";if("none"!==g.css("display")){for(;"undefined"!=typeof n[b][q];)q++;n[b][q]=n[b][q]||c.formatData(m,g.attr(m.output_dataAttrib)||g.html(),e),q++}})}),n},ignoreColumns:function(b,c){return a.each(c,function(d,e){c[d]=a.grep(e,function(c,d){return a.inArray(d,b.output_ignoreColumns)<0})}),c},process:function(d,e){var f,g,h,i,j,k,l=window.JSON&&JSON.hasOwnProperty("stringify"),m=0,n=(e.output_separator||",").toLowerCase(),o="json"===n,p="array"===n,q=o||p?",":e.output_separator,r=d.$table;e.output_regex=new RegExp("("+(/\\/.test(q)?"\\":"")+q+")"),g=r.find("thead tr:visible").not("."+(b.css.filterRow||"tablesorter-filter-row")),i=c.processRow(d,g,!0,o),h=r.children("tbody").children("tr"),e.output_includeFooter&&(h=h.add(r.children("tfoot").children("tr").clone())),h=/f/.test(e.output_saveRows)?h.not("."+(e.filter_filteredRow||"filtered")):/v/.test(e.output_saveRows)?h.filter(":visible"):h,j=c.processRow(d,h),k=i.length,e.output_ignoreColumns.length&&(i=c.ignoreColumns(e,i),j=c.ignoreColumns(e,j)),o?(n=[],a.each(j,function(a,b){n.push(c.row2Hash(i[k>1&&e.output_headerRows?a%k:k-1],b))}),f=l?JSON.stringify(n):n):(n=c.row2CSV(e,e.output_headerRows?i:[i[k>1&&e.output_headerRows?m%k:k-1]],p).concat(c.row2CSV(e,j,p)),f=p&&l?JSON.stringify(n):n.join("\n")),(!a.isFunction(e.output_callback)||e.output_callback(d,f))&&(/p/.test((e.output_delivery||"").toLowerCase())?c.popup(f,e.output_popupStyle,o||p):c.download(e,f))},row2CSV:function(a,b,c){var d,e,f=[],g=b.length;for(e=0;g>e;e++)d=b[e].join("").replace(/\"/g,""),b[e].length>0&&""!==d&&(f[f.length]=c?b[e]:b[e].join(a.output_separator));return f},row2Hash:function(b,c){var d={};return a.each(c,function(a,c){a<b.length&&(d[b[a]]=c)}),d},formatData:function(b,d,e){var f,g=(b.output_separator||",").toLowerCase(),h="json"===g||"array"===g,i=d.replace(/\"/g,b.output_replaceQuote||"“");return i=b.output_trimSpaces?i.replace(c.regexBR,""):i.replace(c.regexBR,c.replaceCR).replace(/\t/g,c.replaceTab),f=i.match(c.regexIMG),b.output_includeHTML||null===f||(i=f[1]),i=b.output_includeHTML&&!e?i:i.replace(c.regexHTML,""),i=b.output_trimSpaces||e?a.trim(i):i,g=h?!1:b.output_wrapQuotes||b.output_regex.test(i)||c.regexQuote.test(i),g?'"'+i+'"':i},popup:function(a,b,d){var e=window.open("",c.popupTitle,b);return e.document.write("<html><head><title>"+c.popupTitle+'</title></head><body><textarea wrap="'+(d?"on":"off")+'" style="'+c.popupStyle+'">'+a+"\n</textarea></body></html>"),e.document.close(),e.focus(),!0},download:function(a,b){var d,e,f,g=window.navigator,h=document.createElement("a");if(/(iP)/g.test(g.userAgent))return alert(c.message),!1;try{f=!!new Blob}catch(i){f=!1}return f?(window.URL=window.webkitURL||window.URL,e=new Blob([b],{type:a.output_encoding}),g.msSaveBlob?g.msSaveBlob(e,a.output_saveFileName):(h.href=window.URL.createObjectURL(e),h.download=a.output_saveFileName,document.createEvent&&(d=document.createEvent("MouseEvents"),d.initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null),h.dispatchEvent(d))),!1):(window.open(a.output_encoding+encodeURIComponent(b)+"?download","_self"),!0)},remove:function(a){a.$table.off(c.event)}};b.addWidget({id:"output",options:{output_separator:",",output_ignoreColumns:[],output_includeFooter:!1,output_dataAttrib:"data-name",output_headerRows:!1,output_delivery:"popup",output_saveRows:"filtered",output_duplicateSpans:!0,output_replaceQuote:"“;",output_includeHTML:!1,output_trimSpaces:!0,output_wrapQuotes:!1,output_popupStyle:"width=500,height=300",output_saveFileName:"mytable.csv",output_callback:function(){return!0},output_callbackJSON:function(a,b,c){return b+"("+c+")"},output_encoding:"data:application/octet-stream;charset=utf8,"},init:function(a,b,d){c.init(d)},remove:function(a,b){c.remove(b)}})}(jQuery);