tablesorter/dist/js/widgets/widget-build-table.min.js
Rob Garrison 5e4ab3b843 2.30.2
2018-04-26 15:25:54 -05:00

2 lines
5.8 KiB
JavaScript

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