Math: reduce update time to 20ms. See #898

This commit is contained in:
Mottie 2015-05-07 07:42:57 -05:00
parent 0824754f8c
commit 3fc40eb688
2 changed files with 12 additions and 10 deletions

View File

@ -6,4 +6,4 @@
* (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;(!f.math_isUpdating||g)&&(/filter/.test(d.type)||b.computeColumnIndex(e.$table.children("tbody").children()),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);
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;(!f.math_isUpdating||g)&&(/filter/.test(d.type)||b.computeColumnIndex(e.$table.children("tbody").children()),c.recalculate(a,e,f,g))}).on("updateComplete.tsmath",function(){setTimeout(function(){f.math_isUpdating=!1},20)}),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);

View File

@ -4,7 +4,7 @@
*/
/*jshint browser:true, jquery:true, unused:false */
/*global jQuery: false */
;(function($){
;(function($) {
"use strict";
var ts = $.tablesorter,
@ -23,7 +23,9 @@
$cells = $row.children();
if (!$row.hasClass(wo.filter_filteredRow || 'filtered')) {
if (wo.math_ignore.length) {
$cells = $cells.not('[' + dataAttrib + '=ignore]').not('[data-column=' + wo.math_ignore.join('],[data-column=') + ']');
$cells = $cells
.not('[' + dataAttrib + '=ignore]')
.not('[data-column=' + wo.math_ignore.join('],[data-column=') + ']');
}
arry = $cells.not($el).map(function(){
$t = $(this);
@ -39,7 +41,7 @@
},
// get all of the column numerical values in an arry
getColumn : function(table, wo, $el, type, dataAttrib){
getColumn : function(table, wo, $el, type, dataAttrib) {
var i, txt, $t, len, mathAbove,
arry = [],
c = table.config,
@ -89,7 +91,7 @@
},
// get all of the column numerical values in an arry
getAll : function(table, wo, dataAttrib){
getAll : function(table, wo, dataAttrib) {
var txt, $t, col,
arry = [],
c = table.config,
@ -114,7 +116,7 @@
return arry;
},
recalculate : function(table, c, wo, init){
recalculate : function(table, c, wo, init) {
if (c && (!wo.math_isUpdating || init)) {
// add data-column attributes to all table cells
@ -365,7 +367,7 @@
return Math.sqrt( vars );
},
// standard deviation (population)
stdevp : function(arry){
stdevp : function(arry) {
var varp = ts.equations.variance(arry, true);
return Math.sqrt( varp );
}
@ -392,7 +394,7 @@
math_suffix : '',
math_event : 'recalculate'
},
init : function(table, thisWidget, c, wo){
init : function(table, thisWidget, c, wo) {
c.$table
.off( (math.events + ' updateComplete.tsmath ' + wo.math_event).replace(/\s+/g, ' ') )
.on(math.events + ' ' + wo.math_event, function(e) {
@ -408,13 +410,13 @@
.on('updateComplete.tsmath', function(){
setTimeout(function(){
wo.math_isUpdating = false;
}, 500);
}, 20);
});
wo.math_isUpdating = false;
},
// this remove function is called when using the refreshWidgets method or when destroying the tablesorter plugin
// this function only applies to tablesorter v2.4+
remove: function(table, c, wo, refreshing){
remove: function(table, c, wo, refreshing) {
if (refreshing) { return; }
$(table)
.off( (math.events + ' updateComplete.tsmath ' + wo.math_event).replace(/\s+/g, ' ') )