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

2 lines
5.6 KiB
JavaScript
Raw Normal View History

2017-04-02 11:33:20 +00:00
/*! Widget: Build Table - updated 4/2/2017 (v2.28.6) */
2017-05-16 20:33:15 +00:00
!function(e){"use strict";var t=e.tablesorter=e.tablesorter||{},r=t.buildTable=function(o,l){e.extend(!0,t.defaults.widgetOptions,r.defaults);var n="TABLE"===o.nodeName?e(o):e("<table>").appendTo(o),i=n[0],a=l.widgetOptions=e.extend(!0,{},r.defaults,l.widgetOptions),d=a.build_processing,s=a.build_type,u=a.build_source||l.data,c=function(t){var o=e.type(t),n=t instanceof jQuery;if("function"==typeof d&&(t=d(t,a)),l.data=t,n||"string"===o){if(n||/<\s*\/tr\s*>/.test(t))return r.html(i,t,a);try{if(t=e.parseJSON(t||"null"))return r.object(i,t,a)}catch(e){}}return"array"===o||"string"===o||"array"===s||"csv"===s?r.csv(i,t,a):r.object(i,t,a)};if(i.config=l,!t.buildTable.hasOwnProperty(s)&&""!==s)return l.debug&&console.error("aborting build table widget, incorrect build type"),!1;u instanceof jQuery?c(e.trim(u.html())):u&&(u.hasOwnProperty("url")||"json"===s)?e.ajax(a.build_source).done(function(e){c(e)}).fail(function(e,t,r){l.debug&&console.error("aborting build table widget, failed ajax load"),n.html('<tr><td class="error">'+e.status+" "+t+"</td></tr>")}):c(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,o,l,n){var i,a,d=n?e("<col>"):"",s=r.build_headers.classes,u=r.build_headers.widths;if(/string|number/.test(typeof t))a=e("<"+o+(s&&s[l]?' class="'+s[l]+'"':"")+">"+t+"</"+o+">"),n&&u&&u[l]&&d.width(u[l]||"");else{a=e("<"+o+">");for(i in t)t.hasOwnProperty(i)&&("text"===i||"html"===i?a[i](t[i]):n&&"width"===i?d.width(t[i]||""):a.attr(i,t[i]))}return[a,d]},header:function(t,r){var o=r.build_headers.text,l=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"+(l&&l[e]?' class="'+l[e]+'"':"")+">"+(o&&o[e]?o[e]:t)+"</th>"}),n+"</tr>"},rows:function(t,r,o,l,n,i){var a=i?"th":"td",d="<tr>"+(l.build_numbers.addColumn?"<"+a+">"+(i?"":n)+"</"+a+">":"");return e.each(t,function(e,t){/<\s*\/t(d|h)\s*>/.test(t)?d+=t:d+="<"+(i?a+(o&&o[e]?' class="'+o[e]+'"':""):a)+">"+(i&&r&&r.length&&r[e]?r[e]:t)+"</"+a+">"}),d+"</tr>"}},r.buildComplete=function(r,o){e(r).triggerHandler(o.build_complete),t.setup(r,r.config)},r.array=function(e,t,o){return r.csv(e,t,o)},r.csv=function(t,o,l){var n,i,a,d="csv"===l.build_type||"string"==typeof o,s=e(t),u=d?o.replace("\r","").split("\n"):o,c=u.length,b=0,h=!1,p=l.build_headers.rows+(d?l.build_csvStartLine:0),f=l.build_footers.rows,g=0,y="",_=r.build.colgroup(l.build_headers.widths)+"<thead>";e.each(u,function(e,t){e>=c-f&&(h=!0),(!d||e>=l.build_csvStartLine)&&e<p?(i=d?r.splitCSV(t,l.build_csvSeparator):t,g=i.length,_+=r.build.header(i,l)):e>=p&&(e===p&&(_+="</thead><tbody>"),a=d?r.splitCSV(t,l.build_csvSeparator):t,h&&f>0&&(_+=(e===c-f?"</tbody><tfoot>":"")+(e===c?"</tfoot>":"")),a.length>1&&(b++,a.length!==g&&(y+="error on line "+e+": Item count ("+a.length+") does not match header count ("+g+") \n"),n=h?l.build_footers.classes:"",_+=r.build.rows(a,l.build_footers.text,n,l,b,h)))}),_+=f>0?"":"</tbody>",y?s.html(y):(s.html(_),r.buildComplete(t,l))},r.splitCSV=function(t,r){var o,l,n=e.trim(t).split(r=r||",");for(o=n.length-1;o>=0;o--)'"'===n[o].replace(/\"\s+$/,'"').charAt(n[o].length-1)?(l=n[o].replace(/^\s+\"/,'"')).length>1&&'"'===l.charAt(0)?n[o]=n[o].replace(/^\s*"|"\s*$/g,"").replace(/""/g,'"'):o?n.splice(o-1,2,[n[o-1],n[o]].join(r)):n=n.shift().split(r).concat(n):n[o].replace(/""/g,'"');return n},r.html=function(t,o,l){var n=e(t);o instanceof jQuery?n.empty().append(o):