mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-12-05 05:04:20 +00:00
2 lines
5.8 KiB
JavaScript
2 lines
5.8 KiB
JavaScript
/*! 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(r).concat(n):n[l].replace(/""/g,'"');return n},r.html=function(t,l,o){var n=e(t);l instanceof jQuery?n.empty().append(l):n.html(l),r.buildComplete(t,o)},r.object=function(l,o,n){var i,d,s,a,u,c,b,h=l.config,p=n.build_objectHeaderKey,f=n.build_objectRowKey,g=o.hasOwnProperty(p)&&!e.isEmptyObject(o.kh)?o.kh:!!o.hasOwnProperty("headers")&&o.headers,y=o.hasOwnProperty(f)&&!e.isEmptyObject(o.kr)?o.kr:!!o.hasOwnProperty("rows")&&o.rows;if(!g||!y||0===g.length||0===y.length)return t.debug(h,"build")&&console.error("Build >> ERROR: Aborting build table widget, missing data for object build"),!1;a=e("<colgroup>"),u=e("<table><thead/></table>"),e.each(g,function(t,l){for(b=e("<tr>").appendTo(u.find("thead")),d=l.length,i=0;i<d;i++)(s=r.build.cell(l[i],n,"th",i,0===t))[0]&&s[0].length&&s[0].appendTo(b),0===t&&s[1]&&s[1].appendTo(a)}),a.find("col[style]").length&&u.prepend(a),c=e("<tbody>"),e.each(y,function(t,l){var o;if((s="object"===e.type(l))&&l.newTbody){c=e("<tbody>").appendTo(u);for(o in l)l.hasOwnProperty(o)&&"newTbody"!==o&&c.attr(o,l[o])}else{if(0===t&&c.appendTo(u),b=e("<tr>").appendTo(c),s){for(o in l)l.hasOwnProperty(o)&&o!==n.build_objectCellKey&&b.attr(o,l[o]);l.hasOwnProperty(n.build_objectCellKey)&&(l=l.cells)}for(d=l.length,o=0;o<d;o++)(a=r.build.cell(l[o],n,"td",o))[0]&&a[0].length&&a[0].appendTo(b)}}),o.hasOwnProperty(n.build_objectFooterKey)&&("clone"===(s=o[n.build_objectFooterKey])?(a=u.find("thead").html(),u.append("<tfoot>"+a+"</tfoot>")):(a=e("<tfoot>").appendTo(u),e.each(s,function(t,l){for(b=e("<tr>").appendTo(a),d=l.length,i=0;i<d;i++)(c=r.build.cell(l[i],n,"th",i))[0]&&c[0].length&&c[0].appendTo(b)}))),e(l).html(u.html()),r.buildComplete(l,n)},r.ajax=r.json=function(e,t,l){return r.object(e,t,l)}}(jQuery); |