tablesorter/dist/js/widgets/widget-build-table.min.js

2 lines
5.8 KiB
JavaScript
Raw Normal View History

2018-03-18 13:49:07 +00:00
/*! Widget: Build Table - updated 2018-03-18 (v2.30.0) */
!function(e){"use strict";var t=e.tablesorter=e.tablesorter||{},r=t.buildTable=function(l,o){e.extend(!0,t.defaults.widgetOptions,r.defaults);var n="TABLE"===l.nodeName?e(l):e("<table>").appendTo(l),i=n[0],d=o.widgetOptions=e.extend(!0,{},r.defaults,o.widgetOptions),s=d.build_processing,a=d.build_type,u=d.build_source||o.data,c=t.debug(o,"build"),b=function(t){var l=e.type(t),n=t instanceof jQuery;if("function"==typeof s&&(t=s(t,d)),o.data=t,n||"string"===l){if(n||/<\s*\/tr\s*>/.test(t))return r.html(i,t,d);try{if(t=e.parseJSON(t||"null"))return r.object(i,t,d)}catch(e){}}return"array"===l||"string"===l||"array"===a||"csv"===a?r.csv(i,t,d):r.object(i,t,d)};if(i.config=o,!t.buildTable.hasOwnProperty(a)&&""!==a)return c&&console.error("Build >> ERROR: Aborting build table widget, incorrect build type"),!1;u instanceof jQuery?b(e.trim(u.html())):u&&(u.hasOwnProperty("url")||"json"===a)?e.ajax(d.build_source).done(function(e){b(e)}).fail(function(e,t){c&&console.error("Build >> ERROR: Aborting build table widget, failed ajax load"),n.html('<tr><td class="error">'+e.status+" "+t+"</td></tr>")}):b(u)};t.defaults.data="",r.defaults={build_type:"",build_source:"",build_processing:null,build_complete:"tablesorter-build-complete",build_headers:{rows:1,classes:[],text:[],widths:[]},build_footers:{rows:1,classes:[],text:[]},build_numbers:{addColumn:!1,sortable:!1},build_csvStartLine:0,build_csvSeparator:",",build_objectRowKey:"rows",build_objectCellKey:"cells",build_objectHeaderKey:"headers",build_objectFooterKey:"footers"},r.build={colgroup:function(t){var r="";return t&&t.length&&(r+="<colgroup>",e.each(t,function(e,t){r+="<col"+(t?' style="width:'+t+'"':"")+">"}),r+="</colgroup>"),r},cell:function(t,r,l,o,n){var i,d,s=n?e("<col>"):"",a=r.build_headers.classes,u=r.build_headers.widths;if(/string|number/.test(typeof t))d=e("<"+l+(a&&a[o]?' class="'+a[o]+'"':"")+">"+t+"</"+l+">"),n&&u&&u[o]&&s.width(u[o]||"");else{d=e("<"+l+">");for(i in t)t.hasOwnProperty(i)&&("text"===i||"html"===i?d[i](t[i]):n&&"width"===i?s.width(t[i]||""):d.attr(i,t[i]))}return[d,s]},header:function(t,r){var l=r.build_headers.text,o=r.build_headers.classes,n="<tr>"+(r.build_numbers.addColumn?"<th"+(r.build_numbers.sortable?"":' class="sorter-false"')+">"+r.build_numbers.addColumn+"</th>":"");return e.each(t,function(e,t){/<\s*\/t(d|h)\s*>/.test(t)?n+=t:n+="<th"+(o&&o[e]?' class="'+o[e]+'"':"")+">"+(l&&l[e]?l[e]:t)+"</th>"}),n+"</tr>"},rows:function(t,r,l,o,n,i){var d=i?"th":"td",s="<tr>"+(o.build_numbers.addColumn?"<"+d+">"+(i?"":n)+"</"+d+">":"");return e.each(t,function(e,t){/<\s*\/t(d|h)\s*>/.test(t)?s+=t:s+="<"+(i?d+(l&&l[e]?' class="'+l[e]+'"':""):d)+">"+(i&&r&&r.length&&r[e]?r[e]:t)+"</"+d+">"}),s+"</tr>"}},r.buildComplete=function(r,l){e(r).triggerHandler(l.build_complete),r.config&&t.debug(r.config,"build")&&console.log("Build >> Table build complete"),t.setup(r,r.config)},r.array=function(e,t,l){return r.csv(e,t,l)},r.csv=function(t,l,o){var n,i,d,s="csv"===o.build_type||"string"==typeof l,a=e(t),u=s?l.replace("\r","").split("\n"):l,c=u.length,b=0,h=!1,p=o.build_headers.rows+(s?o.build_csvStartLine:0),f=o.build_footers.rows,g=0,y="",_=r.build.colgroup(o.build_headers.widths)+"<thead>";e.each(u,function(e,t){e>=c-f&&(h=!0),(!s||e>=o.build_csvStartLine)&&e<p?(i=s?r.splitCSV(t,o.build_csvSeparator):t,g=i.length,_+=r.build.header(i,o)):e>=p&&(e===p&&(_+="</thead><tbody>"),d=s?r.splitCSV(t,o.build_csvSeparator):t,h&&f>0&&(_+=(e===c-f?"</tbody><tfoot>":"")+(e===c?"</tfoot>":"")),d.length>1&&(b++,d.length!==g&&(y+="error on line "+e+": Item count ("+d.length+") does not match header count ("+g+") \n"),n=h?o.build_footers.classes:"",_+=r.build.rows(d,o.build_footers.text,n,o,b,h)))}),_+=f>0?"":"</tbody>",y?a.html(y):(a.html(_),r.buildComplete(t,o))},r.splitCSV=function(t,r){var l,o,n=e.trim(t).split(r=r||",");for(l=n.length-1;l>=0;l--)'"'===n[l].replace(/\"\s+$/,'"').charAt(n[l].length-1)?(o=n[l].replace(/^\s+\"/,'"')).length>1&&'"'===o.charAt(0)?n[l]=n[l].replace(/^\s*"|"\s*$/g,"").replace(/""/g,'"'):l?n.splice(l-1,2,[n[l-1],n[l]].join(r)):n=n.shift().split(