mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-12-05 05:04:20 +00:00
2 lines
6.2 KiB
JavaScript
2 lines
6.2 KiB
JavaScript
/*! Widget: output - updated 9/27/2017 (v2.29.0) */
|
|
!function(t){"use strict";var e=t.tablesorter,o=e.output={event:"outputTable",noDblClick:600,lastEvent:0,busy:!1,regexQuote:/([\n\t\x09\x0d\x0a]|<[^<]+>)/,regexBR:/(<br([\s\/])?>|\n)/g,regexIMG:/<img[^>]+alt\s*=\s*['"]([^'"]+)['"][^>]*>/i,regexHTML:/<[^<]+>/g,replaceCR:"\r\n",replaceTab:"\t",popupTitle:"Output",popupStyle:"width:100%;height:100%;margin:0;resize:none;",message:"Your device does not support downloading. Please try again in desktop browser.",init:function(t){t.$table.off(o.event).on(o.event,function(e){e.stopPropagation(),!o.busy&&e.timeStamp-o.lastEvent>o.noDblClick&&(o.lastEvent=e.timeStamp,o.busy=!0,o.process(t,t.widgetOptions))})},processRow:function(u,n,r,a){var p,i,l,s,c,d,f,_,w,g,h=u.widgetOptions,m=[],v=h.output_duplicateSpans,b=r&&a&&h.output_headerRows&&t.isFunction(h.output_callbackJSON),y=0,S=n.length;for(s=0;s<S;s++)for(m[s]||(m[s]=[]),y=0,l=(i=n.eq(s).children()).length,f=0;f<l;f++){if((p=i.eq(f)).filter("[rowspan]").length)for(_=parseInt(p.attr("rowspan"),10)-1,g=o.formatData(u,h,p,r),c=1;c<=_;c++)m[s+c]||(m[s+c]=[]),m[s+c][y]=r?g:v?g:"";if(p.filter("[colspan]").length)for(w=parseInt(p.attr("colspan"),10)-1,g=o.formatData(u,h,p,r),d=0;d<w;d++)if(p.filter("[rowspan]").length)for(_=parseInt(p.attr("rowspan"),10),c=0;c<_;c++)m[s+c]||(m[s+c]=[]),m[s+c][y+d]=b?h.output_callbackJSON(p,g,y+d)||g+"("+(y+d)+")":r?g:v?g:"";else m[s][y+d]=b?h.output_callbackJSON(p,g,y+d)||g+"("+(y+d)+")":r?g:v?g:"";for(;void 0!==m[s][y];)y++;m[s][y]=m[s][y]||o.formatData(u,h,p,r),y++}return e.output.removeColumns(u,h,m)},removeColumns:function(t,e,o){var u,n,r,a=[],p=o.length;for(u=0;u<p;u++)for(n=o[u],a[u]=[],r=0;r<t.columns;r++)e.output_hiddenColumnArray[r]||a[u].push(n[r]);return a},process:function(u,n,r,a){var p,i,l,s,c,d,f,_=window.JSON&&JSON.hasOwnProperty("stringify"),w=0,g=(n.output_separator||",").toLowerCase(),h="json"===g,m="array"===g,v=h||m?",":n.output_separator,b=n.output_saveRows,y=u.$table;for(n.output_regex=new RegExp("("+(/\\/.test(v)?"\\":"")+v+")"),n.output_hiddenColumnArray=[],w=0;w<u.columns;w++)n.output_hiddenColumnArray[w]=t.inArray(w,n.output_ignoreColumns)>-1||!n.output_hiddenColumns&&"none"===u.$headerIndexed[w].css("display")&&!u.$headerIndexed[w].hasClass("tablesorter-scroller-hidden-column");if(i=y.children("thead").children("tr").not("."+(e.css.filterRow||"tablesorter-filter-row")).filter(function(){return n.output_hiddenColumns||"none"!==t(this).css("display")}),l=o.processRow(u,i,!0,h),r||(r=y.children("tbody").children("tr").not(u.selectorRemove)),r="function"==typeof b?r.filter(b):/^f/.test(b)?r.not("."+(n.filter_filteredRow||"filtered")):/^v/.test(b)?r.filter(":visible"):/^[.#:\[]/.test(b)?r.filter(b):r,s=o.processRow(u,r),n.output_includeFooter&&(s=s.concat(o.processRow(u,y.children("tfoot").children("tr:visible")))),c=l.length,h){for(g=[],d=s.length,w=0;w<d;w++)f=l[c>1&&n.output_headerRows?w%c:c-1],g.push(o.row2Hash(f,s[w]));p=_?JSON.stringify(g):g}else n.output_includeHeader?(f=[l[c>1&&n.output_headerRows?w%c:c-1]],g=o.row2CSV(n,n.output_headerRows?l:f,m).concat(o.row2CSV(n,s,m))):g=o.row2CSV(n,s,m),p=m&&_?JSON.stringify(g):g.join("\n");if(a)return p;if(t.isFunction(n.output_callback)){if(!1===(f=n.output_callback(u,p,u.pager&&u.pager.ajaxObject.url||null)))return void(o.busy=!1);"string"==typeof f&&(p=f)}/p/i.test(n.output_delivery||"")?o.popup(p,n.output_popupStyle,h||m):o.download(u,n,p),o.busy=!1},row2CSV:function(t,e,o){var u,n,r=[],a=e.length;for(n=0;n<a;n++)u=(e[n]||[]).join("").replace(/\"/g,""),(e[n]||[]).length>0&&""!==u&&(r[r.length]=o?e[n]:e[n].join(t.output_separator));return r},row2Hash:function(t,e){var o,u={},n=e.length;for(o=0;o<n;o++)o<t.length&&(u[t[o]]=e[o]);return u},formatData:function(e,u,n,r){var a=n.attr(u.output_dataAttrib),p=void 0!==a?a:n.html(),i=(u.output_separator||",").toLowerCase(),l="json"===i||"array"===i,s=p.replace(/\"/g,u.output_replaceQuote||"“");return s=u.output_trimSpaces?s.replace(o.regexBR,""):s.replace(o.regexBR,o.replaceCR).replace(/\t/g,o.replaceTab),p=s.match(o.regexIMG),u.output_includeHTML||null===p||(s=p[1]),s=u.output_includeHTML&&!r?s:s.replace(o.regexHTML,""),s=u.output_trimSpaces||r?t.trim(s):s,i=!l&&(u.output_wrapQuotes||u.output_regex.test(s)||o.regexQuote.test(s)),s=i?'"'+s+'"':s,"function"==typeof u.output_formatContent?u.output_formatContent(e,u,{isHeader:r,$cell:n,content:s}):s},popup:function(t,e,u){var n=window.open("",o.popupTitle,e);try{n.document.write("<html><head><title>"+o.popupTitle+'</title></head><body><textarea wrap="'+(u?"on":"off")+'" style="'+o.popupStyle+'">'+t+"\n</textarea></body></html>"),n.document.close(),n.focus()}catch(r){return n.close(),o.popup(t,e,u)}return!0},download:function(t,e,u){if("function"==typeof e.output_savePlugin)return e.output_savePlugin(t,e,u);var n,r,a,p,i=window.navigator,l=document.createElement("a");if(/(iP)/g.test(i.userAgent))return alert(o.message),!1;try{a=!!new Blob}catch(t){a=!1}return a?(window.URL=window.URL||window.webkitURL,p=/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.output_encoding)?["\ufeff",u]:[u],r=new Blob(p,{type:e.output_encoding}),i.msSaveBlob?i.msSaveBlob(r,e.output_saveFileName):(l.href=window.URL.createObjectURL(r),l.download=e.output_saveFileName,document.createEvent&&((n=document.createEvent("MouseEvents")).initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null),l.dispatchEvent(n))),!1):(window.open(e.output_encoding+encodeURIComponent(u)+"?download","_self"),!0)},remove:function(t){t.$table.off(o.event)}};e.addWidget({id:"output",options:{output_separator:",",output_ignoreColumns:[],output_hiddenColumns:!1,output_includeFooter:!1,output_includeHeader:!0,output_headerRows:!1,output_dataAttrib:"data-name",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_formatContent:null,output_callback:function(t,e){return!0},output_callbackJSON:function(t,e,o){return e+"("+o+")"},output_encoding:"data:application/octet-stream;charset=utf8,",output_savePlugin:null},init:function(t,e,u){o.init(u)},remove:function(t,e){o.remove(e)}})}(jQuery); |