mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
2 lines
8.3 KiB
JavaScript
2 lines
8.3 KiB
JavaScript
/*! Widget: math - updated 12/13/2015 (v2.25.0) */
|
|
!function(a){"use strict";var b=a.tablesorter,c={error:{0:"Infinity result: Divide by zero",1:"Need more than one element to make this calculation",undef:"No elements found"},invalid:function(a,b,d){return console.log(b,c.error[d]),a&&a.widgetOptions.math_none||""},events:"tablesorter-initialized update updateAll updateRows addRows updateCell filterReset ".split(" ").join(".tsmath "),processText:function(a,d){var e=b.getElementText(a,d,c.getCellIndex(d));return e=b.formatFloat(e.replace(/[^\w,. \-()]/g,""),a.table)||0,isNaN(e)?0:e},getRow:function(b,d,e){var f,g=b.widgetOptions,h=[],i=d.closest("tr"),j=i.hasClass(g.filter_filteredRow||"filtered");return e&&(i=i.filter(e)),(e||!j)&&(f=i.children().not("["+g.math_dataAttrib+"=ignore]"),g.math_ignore.length&&(f=f.filter(function(b){return-1===a.inArray(c.getCellIndex(a(this)),g.math_ignore)})),h=f.not(d).map(function(){return c.processText(b,a(this))}).get()),h},getColumn:function(b,d,e,f){var g,h,i,j,k,l,m=b.widgetOptions,n=[],o=d.closest("tr"),p=m.math_dataAttrib,q="["+p+"=ignore]",r=m.filter_filteredRow||"filtered",s=c.getCellIndex(d),t=b.$table.children("tbody").children(),u=["["+p+"^=above]","["+p+"^=below]","["+p+"^=col]","["+p+"^=all]"];if("above"===e)for(j=t.index(o),g=j;g>=0;)i=t.eq(g),l=i.children().filter(u[0]).length,f&&(i=i.filter(f)),h=i.children().filter(function(b){return c.getCellIndex(a(this))===s}),((f||!i.hasClass(r))&&i.not(q).length&&g!==j||l&&g!==j)&&(l?g=0:h.length&&n.push(c.processText(b,h))),g--;else if("below"===e)for(j=t.length,g=t.index(o)+1;j>g&&(i=t.eq(g),!i.children().filter(u[1]).length);g++)f&&(i=i.filter(f)),h=i.children().filter(function(b){return c.getCellIndex(a(this))===s}),(f||!i.hasClass(r))&&i.not(q).length&&h.length&&n.push(c.processText(b,h));else for(k=t.not(q),j=k.length,g=0;j>g;g++)i=k.eq(g),f&&(i=i.filter(f)),h=i.children().filter(function(b){return c.getCellIndex(a(this))===s}),!f&&i.hasClass(r)||!h.not(u.join(",")).length||h.is(d)||n.push(c.processText(b,h));return n},getAll:function(b,d){var e,f,g,h,i,j,k,l,m=[],n=b.widgetOptions,o=n.math_dataAttrib,p="["+o+"=ignore]",q=n.filter_filteredRow||"filtered",r=b.$table.children("tbody").children().not(p);for(i=r.length,h=0;i>h;h++)if(g=r.eq(h),d&&(g=g.filter(d)),d||!g.hasClass(q))for(j=g.children().not(p),l=j.length,k=0;l>k;k++)e=j.eq(k),f=c.getCellIndex(e),!e.filter("["+o+"]").length&&a.inArray(f,n.math_ignore)<0&&m.push(c.processText(b,e));return m},setColumnIndexes:function(c){c.$table.after('<div id="_tablesorter_table_placeholder"></div>');var d=c.$table.detach(),e=1,f=d.children("tbody").children().filter(function(){var b,c,d=a(this),f=d.children("[colspan]").length>0;if(e>1?(e--,f=!0):1>e&&(e=1),d.children("[rowspan]").length>0)for(b=this.cells,c=0;c<b.length;c++)e=Math.max(b[c].rowSpan,e);return f});b.computeColumnIndex(f,c),a("#_tablesorter_table_placeholder").after(d).remove()},getCellIndex:function(a){var b=a.attr("data-column");return"undefined"==typeof b?a[0].cellIndex:parseInt(b,10)},recalculate:function(d,e,f){if(d&&(!e.math_isUpdating||f)){var g,h,i,j,k,l=!1,m={};d.debug&&(h=new Date),f&&c.setColumnIndexes(d),e.math_dataAttrib="data-"+(e.math_data||"math"),i=e.math_dataAttrib,j=d.$tbodies.children("tr").children("["+i+"]"),l=c.mathType(d,j,e.math_priority)||l,j=d.$table.children("."+d.cssInfoBlock+", tfoot").children("tr").children("["+i+"]"),c.mathType(d,j,e.math_priority),j=d.$table.children().children("tr").children("["+i+"^=all]"),k=j.length,j.each(function(b,c){var d=a(c),f=j.eq(b).attr(i+"-filter")||e.math_rowFilter;m[f]=m[f]?m[f].add(d):d}),a.each(m,function(a,b){l=c.mathType(d,b,["all"],a)||l}),l&&(e.math_isUpdating=!0,d.debug&&console[console.group?"group":"log"]("Math widget triggering an update after recalculation"),b.update(d,g,function(){c.updateComplete(d)}),d.debug&&console.log("Math widget update completed"+b.benchmark(h)))}},updateComplete:function(a){var b=a.widgetOptions;b.math_isUpdating&&a.debug&&console.groupEnd&&console.groupEnd(),b.math_isUpdating=!1},mathType:function(d,e,f,g){if(e.length){var h,i=!1,j=d.widgetOptions,k=j.math_dataAttrib,l=b.equations;return"all"===f[0]&&(h=c.getAll(d,g)),d.debug&&console[console.group?"group":"log"]("Tablesorter Math widget recalculation"),a.each(f,function(a,b){var f,m,n,o,p,q=e.filter("["+k+"^="+b+"]"),r=q.length;if(r){for(d.debug&&console[console.group?"group":"log"](b),f=0;r>f;f++)p=q.eq(f),p.parent().hasClass(j.filter_filteredRow||"filtered")||(g=p.attr(k+"-filter")||j.math_rowFilter,n=(p.attr(k)||"").replace(b+"-",""),m="row"===b?c.getRow(d,p,g):"all"===b?h:c.getColumn(d,p,b,g),l[n]&&(m.length?(o=l[n](m,d),d.debug&&console.log(p.attr(k),g?'("'+g+'")':"",m,"=",o)):o=c.invalid(d,n,"mean"===n?0:"undef"),i=c.output(p,d,o,m)||i));d.debug&&console.groupEnd&&console.groupEnd()}}),d.debug&&console.groupEnd&&console.groupEnd(),i}return!1},output:function(a,c,d,e){var f,g=c.widgetOptions,h=!1,i=a.html(),j=a.attr("data-"+g.math_data+"-mask")||g.math_mask,k=b.formatMask(j,d,g.math_wrapPrefix,g.math_wrapSuffix);return"function"==typeof g.math_complete&&(k=g.math_complete(a,g,k,d,e)),k!==!1&&(h=i!==k,a.html(k)),h&&(f=a.closest("tbody"),!f.length||f.hasClass(c.cssInfoBlock)||f.parent()[0]!==c.table)?!1:h}};b.formatMask=function(a,b,c,d){if(!a||isNaN(+b))return b;var e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t="",u=a.length,v=a.search(/[0-9\-\+#]/),w=v>0?a.substring(0,v):"",x=w;if(v>0&&c&&(x=/\{content\}/.test(c||"")?(c||"").replace(/\{content\}/g,w||""):(c||"")+w),s=a.split("").reverse().join(""),r=s.search(/[0-9\-\+#]/),q=u-r,q+="."===a.substring(q,q+1)?1:0,w=r>0?a.substring(q,u):"",t=w,""!==w&&d&&(t=/\{content\}/.test(d||"")?(d||"").replace(/\{content\}/g,w||""):w+(d||"")),a=a.substring(v,q),b="-"==a.charAt(0)?-b:+b,e=0>b?b=-b:0,f=a.match(/[^\d\-\+#]/g),g=f&&f[f.length-1]||".",h=f&&f[1]&&f[0]||",",a=a.split(g),b=b.toFixed(a[1]&&a[1].length),b=+b+"",j=a[1]&&a[1].lastIndexOf("0"),l=b.split("."),(!l[1]||l[1]&&l[1].length<=j)&&(b=(+b).toFixed(j+1)),m=a[0].split(h),a[0]=m.join(""),i=a[0]&&a[0].indexOf("0"),i>-1)for(;l[0].length<a[0].length-i;)l[0]="0"+l[0];else 0===+l[0]&&(l[0]="");if(b=b.split("."),b[0]=l[0],k=m[1]&&m[m.length-1].length){for(n=b[0],o="",p=n.length%k,u=n.length,q=0;u>q;q++)o+=n.charAt(q),!((q-p+1)%k)&&u-k>q&&(o+=h);b[0]=o}return b[1]=a[1]&&b[1]?g+b[1]:"",x+((e?"-":"")+b[0]+b[1])+t},b.equations={count:function(a){return a.length},sum:function(a){var b,c=a.length,d=0;for(b=0;c>b;b++)d+=a[b];return d},mean:function(a){var c=b.equations.sum(a);return c/a.length},median:function(a,b){var d,e=a.length;return e>1?(a.sort(function(a,b){return a-b}),d=Math.floor(e/2),e%2?a[d]:(a[d-1]+a[d])/2):c.invalid(b,"median",1)},mode:function(a){var b,c,d,e={},f=1,g=[a[0]];for(b=0;b<a.length;b++)c=a[b],e[c]=e[c]?e[c]+1:1,d=e[c],d>f?(g=[c],f=d):d===f&&(g.push(c),f=d);return g.sort(function(a,b){return a-b})},max:function(a){return Math.max.apply(Math,a)},min:function(a){return Math.min.apply(Math,a)},range:function(a){var b=a.sort(function(a,b){return a-b});return b[a.length-1]-b[0]},variance:function(a,d,e){for(var f,g=b.equations.mean(a),h=0,i=a.length;i--;)h+=Math.pow(a[i]-g,2);return f=a.length-(d?0:1),0===f?c.invalid(e,"variance",0):h/=f},varp:function(a,c){return b.equations.variance(a,!0,c)},vars:function(a,c){return b.equations.variance(a,!1,c)},stdevs:function(a,c){var d=b.equations.variance(a,!1,c);return Math.sqrt(d)},stdevp:function(a,c){var d=b.equations.variance(a,!0,c);return Math.sqrt(d)}},b.addWidget({id:"math",priority:100,options:{math_data:"math",math_ignore:[],math_mask:"#,##0.00",math_complete:null,math_priority:["row","above","below","col"],math_prefix:"",math_suffix:"",math_none:"N/A",math_event:"recalculate",math_rowFilter:""},init:function(a,d,e,f){var g=(b.hasWidget(a,"filter")?"filterEnd":"updateComplete")+".tsmath";c.events+=(b.hasWidget(a,"pager")?"pagerComplete":"filterEnd")+".tsmath ",e.$table.off((c.events+"updateComplete.tsmath "+f.math_event).replace(/\s+/g," ")).on(c.events+f.math_event,function(a){if(this.hasInitialized){var b="tablesorter-initialized"===a.type;(!f.math_isUpdating||b)&&(/filter/.test(a.type)||b||c.setColumnIndexes(e),c.recalculate(e,f,b))}}).on(g,function(){setTimeout(function(){c.updateComplete(e)},40)}),f.math_isUpdating=!1,a.hasInitialized&&c.recalculate(e,f,!0)},remove:function(a,b,d,e){e||b.$table.off((c.events+" updateComplete.tsmath "+d.math_event).replace(/\s+/g," ")).children().children("tr").children("[data-"+d.math_data+"]").empty()}})}(jQuery); |