tablesorter/dist/js/widgets/widget-build-table.min.js
2016-07-31 20:48:27 -05:00

2 lines
5.6 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(a){}}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,b,c){e.debug&&console.error("aborting build table widget, failed ajax load"),f.html('<tr><td class="error">'+a.status+" "+b+"</td></tr>")}):l(k)):(e.debug&&console.error("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).triggerHandler(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)&&a<o?(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(b,d,e){var f,g,h,i,j,k,l,m=b.config,n=e.build_objectHeaderKey,o=e.build_objectRowKey,p=d.hasOwnProperty(n)&&!a.isEmptyObject(d.kh)?d.kh:!!d.hasOwnProperty("headers")&&d.headers,q=d.hasOwnProperty(o)&&!a.isEmptyObject(d.kr)?d.kr:!!d.hasOwnProperty("rows")&&d.rows;return p&&q&&0!==p.length&&0!==q.length?(i=a("<colgroup>"),j=a("<table><thead/></table>"),a.each(p,function(b,d){for(l=a("<tr>").appendTo(j.find("thead")),g=d.length,f=0;f<g;f++)h=c.build.cell(d[f],e,"th",f,0===b),h[0]&&h[0].length&&h[0].appendTo(l),0===b&&h[1]&&h[1].appendTo(i)}),i.find("col[style]").length&&j.prepend(i),k=a("<tbody>"),a.each(q,function(b,d){var f;if(h="object"===a.type(d),h&&d.newTbody){k=a("<tbody>").appendTo(j);for(f in d)d.hasOwnProperty(f)&&"newTbody"!==f&&k.attr(f,d[f])}else{if(0===b&&k.appendTo(j),l=a("<tr>").appendTo(k),h){for(f in d)d.hasOwnProperty(f)&&f!==e.build_objectCellKey&&l.attr(f,d[f]);d.hasOwnProperty(e.build_objectCellKey)&&(d=d.cells)}for(g=d.length,f=0;f<g;f++)i=c.build.cell(d[f],e,"td",f),i[0]&&i[0].length&&i[0].appendTo(l)}}),d.hasOwnProperty(e.build_objectFooterKey)&&(h=d[e.build_objectFooterKey],"clone"===h?(i=j.find("thead").html(),j.append("<tfoot>"+i+"</tfoot>")):(i=a("<tfoot>").appendTo(j),a.each(h,function(b,d){for(l=a("<tr>").appendTo(i),g=d.length,f=0;f<g;f++)k=c.build.cell(d[f],e,"th",f),k[0]&&k[0].length&&k[0].appendTo(l)}))),a(b).html(j.html()),void c.buildComplete(b,e)):(m.debug&&console.error("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);