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