tablesorter/dist/js/widgets/widget-build-table.min.js
2015-05-30 15:07:37 -05:00

2 lines
5.5 KiB
JavaScript

/*! Widget: Build Table - updated 3/26/2015 (v2.21.3) */
!function(a){"use strict";var b=a.tablesorter=a.tablesorter||{},c=b.buildTable=function(d,e){var f="TABLE"===d.nodeName?a(d):a("<table>").appendTo(d),g=f[0],h=e.widgetOptions=a.extend(!0,{},c.defaults,e.widgetOptions),i=h.build_processing,j=h.build_type,k=h.build_source||e.data,l=function(b){var d=a.type(b),f=b instanceof jQuery;if("function"==typeof i&&(b=i(b,h)),e.data=b,f||"string"===d){if(f||/<\s*\/tr\s*>/.test(b))return c.html(g,b,h);try{if(b=a.parseJSON(b||"null"))return c.object(g,b,h)}catch(k){}}return"array"===d||"string"===d||"array"===j||"csv"===j?c.csv(g,b,h):c.object(g,b,h)};return g.config=e,b.buildTable.hasOwnProperty(j)||""===j?void(k instanceof jQuery?l(a.trim(k.html())):k&&(k.hasOwnProperty("url")||"json"===j)?a.ajax(h.build_source).done(function(a){l(a)}).fail(function(a,c,d){e.debug&&b.log("aborting build table widget, failed ajax load"),f.html('<tr><td class="error">'+a.status+" "+c+"</td></tr>")}):l(k)):(e.debug&&b.log("aborting build table widget, incorrect build type"),!1)};c.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"},c.build={colgroup:function(b){var c="";return b&&b.length&&(c+="<colgroup>",a.each(b,function(a,b){c+="<col"+(b?' style="width:'+b+'"':"")+">"}),c+="</colgroup>"),c},cell:function(b,c,d,e,f){var g,h,i=f?a("<col>"):"",j=c.build_headers.classes,k=c.build_headers.widths;if(/string|number/.test(typeof b))h=a("<"+d+(j&&j[e]?' class="'+j[e]+'"':"")+">"+b+"</"+d+">"),f&&k&&k[e]&&i.width(k[e]||"");else{h=a("<"+d+">");for(g in b)b.hasOwnProperty(g)&&("text"===g||"html"===g?h[g](b[g]):f&&"width"===g?i.width(b[g]||""):h.attr(g,b[g]))}return[h,i]},header:function(b,c){var d=c.build_headers.text,e=c.build_headers.classes,f="<tr>"+(c.build_numbers.addColumn?"<th"+(c.build_numbers.sortable?"":' class="sorter-false"')+">"+c.build_numbers.addColumn+"</th>":"");return a.each(b,function(a,b){f+=/<\s*\/t(d|h)\s*>/.test(b)?b:"<th"+(e&&e[a]?' class="'+e[a]+'"':"")+">"+(d&&d[a]?d[a]:b)+"</th>"}),f+"</tr>"},rows:function(b,c,d,e,f,g){var h=g?"th":"td",i="<tr>"+(e.build_numbers.addColumn?"<"+h+">"+(g?"":f)+"</"+h+">":"");return a.each(b,function(a,b){i+=/<\s*\/t(d|h)\s*>/.test(b)?b:"<"+(g?h+(d&&d[a]?' class="'+d[a]+'"':""):h)+">"+(g&&c&&c.length&&c[a]?c[a]:b)+"</"+h+">"}),i+"</tr>"}},c.buildComplete=function(c,d){a(c).trigger(d.build_complete),b.setup(c,c.config)},c.array=function(a,b,d){return c.csv(a,b,d)},c.csv=function(b,d,e){var f,g,h,i="csv"===e.build_type||"string"==typeof d,j=a(b),k=i?d.replace("\r","").split("\n"):d,l=k.length,m=0,n=!1,o=e.build_headers.rows+(i?e.build_csvStartLine:0),p=e.build_footers.rows,q=0,r="",s=c.build.colgroup(e.build_headers.widths)+"<thead>";a.each(k,function(a,b){a>=l-p&&(n=!0),(i?a>=e.build_csvStartLine:!0)&&o>a?(g=i?c.splitCSV(b,e.build_csvSeparator):b,q=g.length,s+=c.build.header(g,e)):a>=o&&(a===o&&(s+="</thead><tbody>"),h=i?c.splitCSV(b,e.build_csvSeparator):b,n&&p>0&&(s+=(a===l-p?"</tbody><tfoot>":"")+(a===l?"</tfoot>":"")),h.length>1&&(m++,h.length!==q&&(r+="error on line "+a+": Item count ("+h.length+") does not match header count ("+q+") \n"),f=n?e.build_footers.classes:"",s+=c.build.rows(h,e.build_footers.text,f,e,m,n)))}),s+=p>0?"":"</tbody>",r?j.html(r):(j.html(s),c.buildComplete(b,e))},c.splitCSV=function(b,c){var d,e,f=a.trim(b).split(c=c||",");for(d=f.length-1;d>=0;d--)'"'===f[d].replace(/\"\s+$/,'"').charAt(f[d].length-1)?(e=f[d].replace(/^\s+\"/,'"')).length>1&&'"'===e.charAt(0)?f[d]=f[d].replace(/^\s*"|"\s*$/g,"").replace(/""/g,'"'):d?f.splice(d-1,2,[f[d-1],f[d]].join(c)):f=f.shift().split(c).concat(f):f[d].replace(/""/g,'"');return f},c.html=function(b,d,e){var f=a(b);d instanceof jQuery?f.empty().append(d):f.html(d),c.buildComplete(b,e)},c.object=function(d,e,f){var g,h,i,j,k,l,m,n=d.config,o=f.build_objectHeaderKey,p=f.build_objectRowKey,q=e.hasOwnProperty(o)&&!a.isEmptyObject(e.kh)?e.kh:e.hasOwnProperty("headers")?e.headers:!1,r=e.hasOwnProperty(p)&&!a.isEmptyObject(e.kr)?e.kr:e.hasOwnProperty("rows")?e.rows:!1;return q&&r&&0!==q.length&&0!==r.length?(j=a("<colgroup>"),k=a("<table><thead/></table>"),a.each(q,function(b,d){for(m=a("<tr>").appendTo(k.find("thead")),h=d.length,g=0;h>g;g++)i=c.build.cell(d[g],f,"th",g,0===b),i[0]&&i[0].length&&i[0].appendTo(m),0===b&&i[1]&&i[1].appendTo(j)}),j.find("col[style]").length&&k.prepend(j),l=a("<tbody>"),a.each(r,function(b,d){var e;if(i="object"===a.type(d),i&&d.newTbody){l=a("<tbody>").appendTo(k);for(e in d)d.hasOwnProperty(e)&&"newTbody"!==e&&l.attr(e,d[e])}else{if(0===b&&l.appendTo(k),m=a("<tr>").appendTo(l),i){for(e in d)d.hasOwnProperty(e)&&e!==f.build_objectCellKey&&m.attr(e,d[e]);d.hasOwnProperty(f.build_objectCellKey)&&(d=d.cells)}for(h=d.length,e=0;h>e;e++)j=c.build.cell(d[e],f,"td",e),j[0]&&j[0].length&&j[0].appendTo(m)}}),e.hasOwnProperty(f.build_objectFooterKey)&&(i=e[f.build_objectFooterKey],"clone"===i?(j=k.find("thead").html(),k.append("<tfoot>"+j+"</tfoot>")):(j=a("<tfoot>").appendTo(k),a.each(i,function(b,d){for(m=a("<tr>").appendTo(j),h=d.length,g=0;h>g;g++)l=c.build.cell(d[g],f,"th",g),l[0]&&l[0].length&&l[0].appendTo(m)}))),a(d).html(k.html()),void c.buildComplete(d,f)):(n.debug&&b.log("aborting build table widget, missing data for object build"),!1)},c.ajax=c.json=function(a,b,d){return c.object(a,b,d)}}(jQuery);