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