tablesorter/dist/js/widgets/widget-output.min.js
2015-10-31 10:20:22 -05:00

84 lines
8.4 KiB
JavaScript

/*! Widget: output - updated 7/28/2015 (v2.22.4) */
/*
* Requires tablesorter v2.8+ and jQuery 1.7+
* Modified from:
* HTML Table to CSV: http://www.kunalbabre.com/projects/table2CSV.php (License unknown?)
* Download-File-JS: https://github.com/PixelsCommander/Download-File-JS (http://www.apache.org/licenses/LICENSE-2.0)
*/
/*jshint browser:true, jquery:true, unused:false */
/*global jQuery:false, alert:false */
!function(a){"use strict";var b=a.tablesorter,c=b.output={event:"outputTable",
// wrap line breaks & tabs in quotes
regexQuote:/([\n\t\x09\x0d\x0a]|<[^<]+>)/,// test if cell needs wrapping quotes
regexBR:/(<br([\s\/])?>|\n)/g,// replace
regexIMG:/<img[^>]+alt\s*=\s*['"]([^'"]+)['"][^>]*>/i,// match
regexHTML:/<[^<]+>/g,// replace
replaceCR:"\r\n",replaceTab:" ",popupTitle:"Output",popupStyle:"width:100%;height:100%;",// for textarea
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(b){b.stopPropagation(),
// explicitly use table.config.widgetOptions because we want
// the most up-to-date values; not the 'wo' from initialization
c.process(a,a.widgetOptions)})},processRow:function(d,e,f,g){var h,i,j,k,l,m,n,o,p,q,r=d.widgetOptions,s=[],t=r.output_duplicateSpans,u=f&&g&&r.output_headerRows&&a.isFunction(r.output_callbackJSON),v=0,w=e.length;for(k=0;w>k;k++)for(s[k]||(s[k]=[]),v=0,i=e.eq(k).children(),j=i.length,n=0;j>n;n++){
// process rowspans
if(h=i.eq(n),h.filter("[rowspan]").length)for(o=parseInt(h.attr("rowspan"),10)-1,q=c.formatData(d,r,h,f),l=1;o>=l;l++)s[k+l]||(s[k+l]=[]),s[k+l][v]=f?q:t?q:"";
// process colspans
if(h.filter("[colspan]").length)for(p=parseInt(h.attr("colspan"),10)-1,q=c.formatData(d,r,h,f),m=1;p>=m;m++)
// if we're processing the header & making JSON, the header names need to be unique
if(h.filter("[rowspan]").length)for(o=parseInt(h.attr("rowspan"),10),l=0;o>l;l++)s[k+l]||(s[k+l]=[]),s[k+l][v+m]=u?r.output_callbackJSON(h,q,v+m)||q+"("+(v+m)+")":f?q:t?q:"";else s[k][v+m]=u?r.output_callbackJSON(h,q,v+m)||q+"("+(v+m)+")":f?q:t?q:"";
// skip column if already defined
for(;"undefined"!=typeof s[k][v];)v++;s[k][v]=s[k][v]||c.formatData(d,r,h,f),v++}return b.output.removeColumns(d,r,s)},
// remove hidden/ignored columns
removeColumns:function(a,b,c){var d,e,f,g=[],h=c.length;for(d=0;h>d;d++)for(e=c[d],g[d]=[],f=0;f<a.columns;f++)b.output_hiddenColumnArray[f]||g[d].push(e[f]);return g},process:function(d,e){var f,g,h,i,j,k,l,m,n=window.JSON&&JSON.hasOwnProperty("stringify"),o=0,p=(e.output_separator||",").toLowerCase(),q="json"===p,r="array"===p,s=q||r?",":e.output_separator,t=e.output_saveRows,u=d.$table;for(
// regex to look for the set separator or HTML
e.output_regex=new RegExp("("+(/\\/.test(s)?"\\":"")+s+")"),
// make a list of hidden columns
e.output_hiddenColumnArray=[],o=0;o<d.columns;o++)e.output_hiddenColumnArray[o]=a.inArray(o,e.output_ignoreColumns)>-1||!e.output_hiddenColumns&&"none"===d.$headerIndexed[o].css("display")&&!d.$headerIndexed[o].hasClass("tablesorter-scroller-hidden-column");if(g=u.children("thead").children("tr").not("."+(b.css.filterRow||"tablesorter-filter-row")).filter(function(){return e.output_hiddenColumns||"none"!==a(this).css("display")}),i=c.processRow(d,g,!0,q),h=u.children("tbody").children("tr"),h=/^f/.test(t)?h.not("."+(e.filter_filteredRow||"filtered")):/^v/.test(t)?h.filter(":visible"):/^[.#:\[]/.test(t)?h.filter(t):h,j=c.processRow(d,h),e.output_includeFooter&&(j=j.concat(c.processRow(d,u.children("tfoot").children("tr:visible")))),k=i.length,q){for(p=[],l=j.length,o=0;l>o;o++)m=i[k>1&&e.output_headerRows?o%k:k-1],p.push(c.row2Hash(m,j[o]));
// requires JSON stringify; if it doesn't exist, the output will show [object Object],... in the output window
f=n?JSON.stringify(p):p}else m=[i[k>1&&e.output_headerRows?o%k:k-1]],p=c.row2CSV(e,e.output_headerRows?i:m,r).concat(c.row2CSV(e,j,r)),f=r&&n?JSON.stringify(p):p.join("\n");
// callback; if true returned, continue processing
(!a.isFunction(e.output_callback)||e.output_callback(d,f))&&(/p/i.test(e.output_delivery||"")?c.popup(f,e.output_popupStyle,q||r):c.download(e,f))},// end process
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(a,b){var c,d={},e=b.length;for(c=0;e>c;c++)c<a.length&&(d[a[c]]=b[c]);return d},formatData:function(b,d,e,f){var g=e.attr(d.output_dataAttrib),h="undefined"!=typeof g?g:e.html(),i=(d.output_separator||",").toLowerCase(),j="json"===i||"array"===i,
// replace " with “ if undefined
k=h.replace(/\"/g,d.output_replaceQuote||"“");return
// replace line breaks with \\n & tabs with \\t
k=d.output_trimSpaces?k.replace(c.regexBR,""):k.replace(c.regexBR,c.replaceCR).replace(/\t/g,c.replaceTab),h=k.match(c.regexIMG),d.output_includeHTML||null===h||(k=h[1]),k=d.output_includeHTML&&!f?k:k.replace(c.regexHTML,""),k=d.output_trimSpaces||f?a.trim(k):k,i=j?!1:d.output_wrapQuotes||d.output_regex.test(k)||c.regexQuote.test(k),k=i?'"'+k+'"':k,"function"==typeof d.output_formatContent?d.output_formatContent(b,d,{isHeader:f,$cell:e,content:k}):k},popup:function(a,b,d){var e=window.open("",c.popupTitle,b);
// select all text and focus within the textarea in the popup
// $(generator.document).find('textarea').select().focus();
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},
// modified from https://github.com/PixelsCommander/Download-File-JS
// & http://html5-demos.appspot.com/static/a.download.html
download:function(a,b){var d,e,f,g=window.navigator,h=document.createElement("a");
// iOS devices do not support downloading. We have to inform user about this.
if(/(iP)/g.test(g.userAgent))return alert(c.message),!1;
// test for blob support
try{f=!!new Blob}catch(i){f=!1}
// Use HTML5 Blob if browser supports it
// Use HTML5 Blob if browser supports it
// prepend BOM for utf-8 encoding - see https://github.com/eligrey/FileSaver.js/blob/master/FileSaver.js#L140
// IE 10+
// all other browsers
// Dispatching click event; using $(link).trigger() won't work
// event.initMouseEvent(type, canBubble, cancelable, view, detail, screenX, screenY, clientX, clientY,
// ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget);
// fallback to force file download (whether supported by server).
// not sure if this actually works in IE9 and older...
return f?(window.URL=window.URL||window.webkitURL,e=new Blob(["\ufeff",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:",",// set to 'json', 'array' or any separator
output_ignoreColumns:[],// columns to ignore [0, 1,... ] (zero-based index)
output_hiddenColumns:!1,// include hidden columns in the output
output_includeFooter:!1,// include footer rows in the output
output_dataAttrib:"data-name",// header attrib containing modified header name
output_headerRows:!1,// if true, include multiple header rows (JSON only)
output_delivery:"popup",// popup, download
output_saveRows:"filtered",// (a)ll, (v)isible, (f)iltered or jQuery filter selector
output_duplicateSpans:!0,// duplicate output data in tbody colspan/rowspan
output_replaceQuote:"“;",// left double quote
output_includeHTML:!1,output_trimSpaces:!0,output_wrapQuotes:!1,output_popupStyle:"width=500,height=300",output_saveFileName:"mytable.csv",
// format $cell content callback
output_formatContent:null,// function(config, widgetOptions, data){ return data.content; }
// callback executed when processing completes
// return true to continue download/output
// return false to stop delivery & do something else with the data
output_callback:function(a,b){return!0},
// JSON callback executed when a colspan is encountered in the header
output_callbackJSON:function(a,b,c){return b+"("+c+")"},
// the need to modify this for Excel no longer exists
output_encoding:"data:application/octet-stream;charset=utf8,"},init:function(a,b,d){c.init(d)},remove:function(a,b){c.remove(b)}})}(jQuery);