mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
12 lines
6.2 KiB
JavaScript
12 lines
6.2 KiB
JavaScript
/*! tablesorter math widget - updated 2/9/2015 (v2.19.1)
|
|
* Requires tablesorter v2.16+ and jQuery 1.7+
|
|
* by Rob Garrison
|
|
*/
|
|
!function(a){"use strict";var b=a.tablesorter,c={events:"tablesorter-initialized update updateAll updateRows addRows updateCell filterReset filterEnd ".split(" ").join(".tsmath "),getRow:function(c,d,e,f){var g,h,i=c.config,j=[],k=e.closest("tr"),l=k.children();return k.hasClass(d.filter_filteredRow||"filtered")||(d.math_ignore.length&&(l=l.not("["+f+"=ignore]").not("[data-column="+d.math_ignore.join("],[data-column=")+"]")),j=l.not(e).map(function(){return g=a(this),h=g.attr(i.textAttribute),"undefined"==typeof h&&(h=this.textContent||g.text()),h=b.formatFloat(h.replace(/[^\w,. \-()]/g,""),c),isNaN(h)?0:h}).get()),j},getColumn:function(c,d,e,f,g){var h,i,j,k,l,m=[],n=c.config,o=d.filter_filteredRow||"filtered",p=parseInt(e.attr("data-column"),10),q=n.$table.children("tbody").children(),r=e.closest("tr");if("above"===f)for(k=q.index(r),h=k;h>=0;)j=q.eq(h).children().filter("[data-column="+p+"]"),l=j.filter("["+g+"^=above]").length,(!q.eq(h).hasClass(o)&&q.eq(h).not("["+g+"=ignore]").length&&h!==k||l&&h!==k)&&(l?h=0:j.length&&(i=j.attr(n.textAttribute),"undefined"==typeof i&&(i=j[0].textContent||j.text()),i=b.formatFloat(i.replace(/[^\w,. \-()]/g,""),c),m.push(isNaN(i)?0:i))),h--;else q.each(function(){j=a(this).children().filter("[data-column="+p+"]"),a(this).hasClass(o)||!j.not("["+g+"^=above],["+g+"^=col]").length||j.is(e)||(i=j.attr(n.textAttribute),"undefined"==typeof i&&(i=(j[0]?j[0].textContent:"")||j.text()),i=b.formatFloat(i.replace(/[^\w,. \-()]/g,""),c),m.push(isNaN(i)?0:i))});return m},getAll:function(c,d,e){var f,g,h,i=[],j=c.config,k=d.filter_filteredRow||"filtered",l=j.$table.children("tbody").children();return l.each(function(){a(this).hasClass(k)||a(this).children().each(function(){g=a(this),h=parseInt(g.attr("data-column"),10),!g.filter("["+e+"]").length&&a.inArray(h,d.math_ignore)<0&&(f=g.attr(j.textAttribute),"undefined"==typeof f&&(f=(g[0]?g[0].textContent:"")||g.text()),f=b.formatFloat(f.replace(/[^\w,. \-()]/g,""),c),i.push(isNaN(f)?0:f))})}),i},recalculate:function(a,d,e,f){if(d&&(!e.math_isUpdating||f)){f&&b.computeColumnIndex(d.$table.children("tbody").children());var g="data-"+(e.math_data||"math"),h=d.$tbodies.find("["+g+"]");c.mathType(a,e,h,e.math_priority,g),h=d.$table.find("."+d.cssInfoBlock+", tfoot").find("["+g+"]"),c.mathType(a,e,h,e.math_priority,g),c.mathType(a,e,d.$table.find("["+g+"^=all]"),["all"],g),e.math_isUpdating=!0,d.$table.trigger("update")}},mathType:function(d,e,f,g,h){if(f.length){var i,j,k,l,m,n=b.equations;"all"===g[0]&&(m=c.getAll(d,e,h)),a.each(g,function(b,g){f.filter("["+h+"^="+g+"]").each(function(){k=a(this),i=(k.attr(h)||"").replace(g+"-",""),l="row"===g?c.getRow(d,e,k,h):"all"===g?m:c.getColumn(d,e,k,g,h),n[i]&&(j=n[i](l),d.config.debug&&console&&console.log&&console.log(k.attr(h),l,"=",j),c.output(k,e,j,l))})})}},output:function(c,d,e,f){var g=b.formatMask(c.attr("data-"+d.math_data+"-mask")||d.math_mask,e,d.math_wrapPrefix,d.math_wrapSuffix);a.isFunction(d.math_complete)&&(g=d.math_complete(c,d,g,e,f)),g!==!1&&c.html(g)}};/**
|
|
* @preserve IntegraXor Web SCADA - JavaScript Number Formatter
|
|
* http:// www.integraxor.com/
|
|
* author: KPL, KHL
|
|
* (c)2011 ecava
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
*/
|
|
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,v,w,x="",y="";if(s=a.length,t=a.search(/[0-9\-\+#]/),u=t>0?a.substring(0,t):"",x=u,t>0&&c&&(x=/\{content\}/.test(c||"")?(c||"").replace(/\{content\}/g,u||""):(c||"")+u),w=a.split("").reverse().join(""),v=w.search(/[0-9\-\+#]/),q=s-v,q+="."===a.substring(q,q+1)?1:0,u=v>0?a.substring(q,s):"",y=u,""!==u&&d&&(y=/\{content\}/.test(d||"")?(d||"").replace(/\{content\}/g,u||""):u+(d||"")),a=a.substring(t,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,r=n.length,q=0;r>q;q++)o+=n.charAt(q),!((q-p+1)%k)&&r-k>q&&(o+=h);b[0]=o}return b[1]=a[1]&&b[1]?g+b[1]:"",x+((e?"-":"")+b[0]+b[1])+y},b.equations={count:function(a){return a.length},sum:function(b){var c=0;return a.each(b,function(a){c+=b[a]}),c},mean:function(a){var c=b.equations.sum(a);return c/a.length},median:function(a){a.sort(function(a,b){return a-b});var b=Math.floor(a.length/2);return a.length%2?a[b]:(a[b-1]+a[b])/2},mode:function(a){if(0===a.length)return"none";var b,c,d={},e=1,f=[a[0]];for(b=0;b<a.length;b++)c=a[b],d[c]=d[c]?d[c]+1:1,d[c]>e?(f=[c],e=d[c]):d[c]===e&&(f.push(c),e=d[c]);return f.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,c){for(var d=b.equations.mean(a),e=0,f=a.length;f--;)e+=Math.pow(a[f]-d,2);return e/=a.length-(c?0:1)},varp:function(a){return b.equations.variance(a,!0)},vars:function(a){return b.equations.variance(a)},stdevs:function(a){var c=b.equations.variance(a);return Math.sqrt(c)},stdevp:function(a){var c=b.equations.variance(a,!0);return Math.sqrt(c)}},b.addWidget({id:"math",priority:100,options:{math_data:"math",math_ignore:[],math_mask:"#,##0.00",math_complete:null,math_priority:["row","above","col"],math_prefix:"",math_suffix:"",math_event:"recalculate"},init:function(a,d,e,f){e.$table.off((c.events+" updateComplete.tsmath "+f.math_event).replace(/\s+/g," ")).on(c.events+" "+f.math_event,function(d){var g="tablesorter-initialized"===d.type;"updateAll"===d.type?b.computeColumnIndex(e.$table.children("tbody").children()):(!f.math_isUpdating||g)&&c.recalculate(a,e,f,g)}).on("updateComplete.tsmath",function(){setTimeout(function(){f.math_isUpdating=!1},500)}),f.math_isUpdating=!1},remove:function(b,d,e,f){f||a(b).off((c.events+" updateComplete.tsmath "+e.math_event).replace(/\s+/g," ")).find("[data-"+e.math_data+"]").empty()}})}(jQuery); |