2016-03-02 05:00:22 +00:00
|
|
|
/*! Widget: math - updated 3/1/2016 (v2.25.5) *//*
|
2014-04-14 23:37:17 +00:00
|
|
|
* Requires tablesorter v2.16+ and jQuery 1.7+
|
|
|
|
* by Rob Garrison
|
|
|
|
*/
|
|
|
|
/*jshint browser:true, jquery:true, unused:false */
|
|
|
|
/*global jQuery: false */
|
2015-07-24 23:23:00 +00:00
|
|
|
;( function( $ ) {
|
2015-07-23 04:29:51 +00:00
|
|
|
'use strict';
|
2014-04-14 23:37:17 +00:00
|
|
|
|
|
|
|
var ts = $.tablesorter,
|
2015-02-09 21:59:15 +00:00
|
|
|
|
2014-04-14 23:37:17 +00:00
|
|
|
math = {
|
|
|
|
|
2015-07-24 23:23:00 +00:00
|
|
|
error: {
|
|
|
|
0 : 'Infinity result: Divide by zero',
|
|
|
|
1 : 'Need more than one element to make this calculation',
|
|
|
|
'undef' : 'No elements found'
|
|
|
|
},
|
|
|
|
|
|
|
|
// value returned when calculation is not possible, e.g. no values, dividing by zero, etc.
|
2015-10-15 02:20:49 +00:00
|
|
|
invalid : function( c, name, errorIndex ) {
|
2015-07-24 23:23:00 +00:00
|
|
|
// name = function returning invalid results
|
|
|
|
// errorIndex = math.error index with an explanation of the error
|
2016-02-25 20:17:20 +00:00
|
|
|
console.warn( name, math.error[ errorIndex ] );
|
2015-12-13 12:56:38 +00:00
|
|
|
return c && c.widgetOptions.math_none || ''; // text for cell
|
2015-07-24 23:23:00 +00:00
|
|
|
},
|
|
|
|
|
2015-12-13 02:41:27 +00:00
|
|
|
events : ( 'tablesorter-initialized update updateAll updateRows addRows updateCell filterReset ' )
|
|
|
|
.split(' ').join('.tsmath '),
|
2015-02-09 21:59:15 +00:00
|
|
|
|
2015-07-24 23:23:00 +00:00
|
|
|
processText : function( c, $cell ) {
|
2015-12-09 19:37:52 +00:00
|
|
|
var txt = ts.getElementText( c, $cell, math.getCellIndex( $cell ) );
|
2015-07-24 23:23:00 +00:00
|
|
|
txt = ts.formatFloat( txt.replace( /[^\w,. \-()]/g, '' ), c.table ) || 0;
|
|
|
|
// isNaN('') => false
|
|
|
|
return isNaN( txt ) ? 0 : txt;
|
|
|
|
},
|
|
|
|
|
2014-04-14 23:37:17 +00:00
|
|
|
// get all of the row numerical values in an arry
|
2015-12-13 02:41:27 +00:00
|
|
|
getRow : function( c, $el, hasFilter ) {
|
2015-11-22 22:22:05 +00:00
|
|
|
var $cells,
|
|
|
|
wo = c.widgetOptions,
|
2014-04-14 23:37:17 +00:00
|
|
|
arry = [],
|
2015-07-24 23:23:00 +00:00
|
|
|
$row = $el.closest( 'tr' ),
|
2015-12-13 02:41:27 +00:00
|
|
|
isFiltered = $row.hasClass( wo.filter_filteredRow || 'filtered' );
|
2015-11-22 22:22:05 +00:00
|
|
|
if ( hasFilter ) {
|
|
|
|
$row = $row.filter( hasFilter );
|
|
|
|
}
|
2015-12-13 02:41:27 +00:00
|
|
|
if ( hasFilter || !isFiltered ) {
|
2015-07-24 23:23:00 +00:00
|
|
|
$cells = $row.children().not( '[' + wo.math_dataAttrib + '=ignore]' );
|
|
|
|
if ( wo.math_ignore.length ) {
|
2016-02-25 19:53:52 +00:00
|
|
|
$cells = $cells.filter( function() {
|
2015-12-04 06:48:41 +00:00
|
|
|
// using $.inArray is not optimal (needed for IE8)
|
|
|
|
return $.inArray( math.getCellIndex( $( this ) ), wo.math_ignore ) === -1;
|
|
|
|
});
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
arry = $cells.not( $el ).map( function() {
|
|
|
|
return math.processText( c, $( this ) );
|
2014-04-14 23:37:17 +00:00
|
|
|
}).get();
|
|
|
|
}
|
|
|
|
return arry;
|
|
|
|
},
|
|
|
|
|
|
|
|
// get all of the column numerical values in an arry
|
2015-12-13 02:41:27 +00:00
|
|
|
getColumn : function( c, $el, type, hasFilter ) {
|
2015-11-22 22:22:05 +00:00
|
|
|
var index, $t, $tr, len, $mathRows, mathAbove,
|
2015-07-24 23:23:00 +00:00
|
|
|
wo = c.widgetOptions,
|
2015-11-24 05:29:33 +00:00
|
|
|
arry = [],
|
|
|
|
$row = $el.closest( 'tr' ),
|
2015-10-07 01:51:40 +00:00
|
|
|
mathAttr = wo.math_dataAttrib,
|
2015-12-13 02:41:27 +00:00
|
|
|
mathIgnore = '[' + mathAttr + '=ignore]',
|
2014-04-14 23:37:17 +00:00
|
|
|
filtered = wo.filter_filteredRow || 'filtered',
|
2015-12-04 06:48:41 +00:00
|
|
|
cIndex = math.getCellIndex( $el ),
|
2015-12-13 02:41:27 +00:00
|
|
|
// get all rows to keep row indexing
|
|
|
|
$rows = c.$table.children( 'tbody' ).children(),
|
|
|
|
mathAttrs = [
|
|
|
|
'[' + mathAttr + '^=above]',
|
|
|
|
'[' + mathAttr + '^=below]',
|
|
|
|
'[' + mathAttr + '^=col]',
|
|
|
|
'[' + mathAttr + '^=all]'
|
|
|
|
];
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( type === 'above' ) {
|
|
|
|
len = $rows.index( $row );
|
|
|
|
index = len;
|
|
|
|
while ( index >= 0 ) {
|
2015-11-22 22:22:05 +00:00
|
|
|
$tr = $rows.eq( index );
|
2015-12-13 02:41:27 +00:00
|
|
|
mathAbove = $tr.children().filter( mathAttrs[0] ).length;
|
2015-11-22 22:22:05 +00:00
|
|
|
if ( hasFilter ) {
|
2015-11-24 05:29:33 +00:00
|
|
|
$tr = $tr.filter( hasFilter );
|
2015-11-22 22:22:05 +00:00
|
|
|
}
|
2016-02-25 19:53:52 +00:00
|
|
|
$t = $tr.children().filter( function() {
|
2015-12-04 06:48:41 +00:00
|
|
|
return math.getCellIndex( $( this ) ) === cIndex;
|
|
|
|
});
|
2014-04-14 23:37:17 +00:00
|
|
|
// ignore filtered rows & rows with data-math="ignore" (and starting row)
|
2015-12-13 02:41:27 +00:00
|
|
|
if ( ( ( hasFilter || !$tr.hasClass( filtered ) ) &&
|
|
|
|
$tr.not( mathIgnore ).length &&
|
2015-07-24 23:23:00 +00:00
|
|
|
index !== len ) ||
|
|
|
|
mathAbove && index !== len ) {
|
2015-07-23 04:29:51 +00:00
|
|
|
// stop calculating 'above', when encountering another 'above'
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( mathAbove ) {
|
|
|
|
index = 0;
|
|
|
|
} else if ( $t.length ) {
|
2016-02-25 19:53:52 +00:00
|
|
|
arry[ arry.length ] = math.processText( c, $t );
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
index--;
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-10-07 01:49:57 +00:00
|
|
|
} else if ( type === 'below' ) {
|
2015-10-04 15:06:07 +00:00
|
|
|
len = $rows.length;
|
|
|
|
// index + 1 to ignore starting node
|
2015-10-05 16:52:35 +00:00
|
|
|
for ( index = $rows.index( $row ) + 1; index < len; index++ ) {
|
2015-11-22 22:22:05 +00:00
|
|
|
$tr = $rows.eq( index );
|
2015-12-13 02:41:27 +00:00
|
|
|
if ( $tr.children().filter( mathAttrs[1] ).length ) {
|
|
|
|
break;
|
|
|
|
}
|
2015-11-22 22:22:05 +00:00
|
|
|
if ( hasFilter ) {
|
|
|
|
$tr = $tr.filter( hasFilter );
|
|
|
|
}
|
2016-02-25 19:53:52 +00:00
|
|
|
$t = $tr.children().filter( function() {
|
2015-12-04 06:48:41 +00:00
|
|
|
return math.getCellIndex( $( this ) ) === cIndex;
|
|
|
|
});
|
2015-12-13 02:41:27 +00:00
|
|
|
if ( ( hasFilter || !$tr.hasClass( filtered ) ) &&
|
|
|
|
$tr.not( mathIgnore ).length &&
|
|
|
|
$t.length ) {
|
2016-02-25 19:53:52 +00:00
|
|
|
arry[ arry.length ] = math.processText( c, $t );
|
2015-10-05 16:52:35 +00:00
|
|
|
}
|
2015-10-04 15:06:07 +00:00
|
|
|
}
|
2014-04-14 23:37:17 +00:00
|
|
|
} else {
|
2015-12-13 02:41:27 +00:00
|
|
|
$mathRows = $rows.not( mathIgnore );
|
2015-07-24 23:23:00 +00:00
|
|
|
len = $mathRows.length;
|
|
|
|
for ( index = 0; index < len; index++ ) {
|
2015-11-22 22:22:05 +00:00
|
|
|
$tr = $mathRows.eq( index );
|
|
|
|
if ( hasFilter ) {
|
|
|
|
$tr = $tr.filter( hasFilter );
|
|
|
|
}
|
2016-02-25 19:53:52 +00:00
|
|
|
$t = $tr.children().filter( function() {
|
2015-12-04 06:48:41 +00:00
|
|
|
return math.getCellIndex( $( this ) ) === cIndex;
|
|
|
|
});
|
2015-12-13 02:41:27 +00:00
|
|
|
if ( ( hasFilter || !$tr.hasClass( filtered ) ) &&
|
|
|
|
$t.not( mathAttrs.join( ',' ) ).length &&
|
2015-07-24 23:23:00 +00:00
|
|
|
!$t.is( $el ) ) {
|
2016-02-25 19:53:52 +00:00
|
|
|
arry[ arry.length ] = math.processText( c, $t );
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
}
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
return arry;
|
|
|
|
},
|
|
|
|
|
|
|
|
// get all of the column numerical values in an arry
|
2015-12-13 02:41:27 +00:00
|
|
|
getAll : function( c, hasFilter ) {
|
2015-07-28 20:57:01 +00:00
|
|
|
var $t, col, $row, rowIndex, rowLen, $cells, cellIndex, cellLen,
|
2014-04-14 23:37:17 +00:00
|
|
|
arry = [],
|
2015-07-24 23:23:00 +00:00
|
|
|
wo = c.widgetOptions,
|
2015-10-07 01:51:40 +00:00
|
|
|
mathAttr = wo.math_dataAttrib,
|
2015-12-13 02:41:27 +00:00
|
|
|
mathIgnore = '[' + mathAttr + '=ignore]',
|
2014-04-14 23:37:17 +00:00
|
|
|
filtered = wo.filter_filteredRow || 'filtered',
|
2015-12-13 02:41:27 +00:00
|
|
|
$rows = c.$table.children( 'tbody' ).children().not( mathIgnore );
|
2015-07-24 23:23:00 +00:00
|
|
|
rowLen = $rows.length;
|
|
|
|
for ( rowIndex = 0; rowIndex < rowLen; rowIndex++ ) {
|
|
|
|
$row = $rows.eq( rowIndex );
|
2015-11-22 22:22:05 +00:00
|
|
|
if ( hasFilter ) {
|
|
|
|
$row = $row.filter( hasFilter );
|
|
|
|
}
|
2015-12-13 02:41:27 +00:00
|
|
|
if ( hasFilter || !$row.hasClass( filtered ) ) {
|
|
|
|
$cells = $row.children().not( mathIgnore );
|
2015-07-24 23:23:00 +00:00
|
|
|
cellLen = $cells.length;
|
|
|
|
// $row.children().each(function(){
|
|
|
|
for ( cellIndex = 0; cellIndex < cellLen; cellIndex++ ) {
|
|
|
|
$t = $cells.eq( cellIndex );
|
2015-12-04 06:48:41 +00:00
|
|
|
col = math.getCellIndex( $t );
|
2015-10-07 01:51:40 +00:00
|
|
|
if ( !$t.filter( '[' + mathAttr + ']' ).length && $.inArray( col, wo.math_ignore ) < 0 ) {
|
2016-02-25 19:53:52 +00:00
|
|
|
arry[ arry.length ] = math.processText( c, $t );
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
}
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
}
|
2014-04-14 23:37:17 +00:00
|
|
|
return arry;
|
|
|
|
},
|
|
|
|
|
2015-10-15 01:46:42 +00:00
|
|
|
setColumnIndexes : function( c ) {
|
2016-02-25 20:17:20 +00:00
|
|
|
var $table = c.$table,
|
2015-12-04 06:48:41 +00:00
|
|
|
last = 1,
|
|
|
|
// only target rows with a colspan or rows included in a rowspan
|
|
|
|
$rows = $table.children( 'tbody' ).children().filter( function() {
|
2016-02-25 19:53:52 +00:00
|
|
|
var cells, indx,
|
2015-12-04 06:48:41 +00:00
|
|
|
$this = $( this ),
|
|
|
|
include = $this.children( '[colspan]' ).length > 0;
|
|
|
|
if ( last > 1 ) {
|
|
|
|
last--;
|
|
|
|
include = true;
|
|
|
|
} else if ( last < 1 ) {
|
|
|
|
last = 1;
|
|
|
|
}
|
|
|
|
if ( $this.children( '[rowspan]' ).length > 0 ) {
|
|
|
|
cells = this.cells;
|
|
|
|
// find max rowspan (in case more than one cell has a rowspan)
|
|
|
|
for ( indx = 0; indx < cells.length; indx++ ) {
|
|
|
|
last = Math.max( cells[ indx ].rowSpan, last );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return include;
|
|
|
|
});
|
|
|
|
// pass `c` (table.config) to computeColumnIndex so it won't add a data-column
|
|
|
|
// to every tbody cell, just the ones where the .cellIndex property doesn't match
|
|
|
|
// the calculated cell index - hopefully fixes the lag issue in #1048
|
|
|
|
ts.computeColumnIndex( $rows, c );
|
2015-10-15 01:46:42 +00:00
|
|
|
},
|
|
|
|
|
2015-12-04 06:48:41 +00:00
|
|
|
getCellIndex : function( $cell ) {
|
|
|
|
var indx = $cell.attr( 'data-column' );
|
|
|
|
return typeof indx === 'undefined' ? $cell[0].cellIndex : parseInt( indx, 10 );
|
|
|
|
},
|
|
|
|
|
2015-07-28 20:57:01 +00:00
|
|
|
recalculate : function(c, wo, init) {
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( c && ( !wo.math_isUpdating || init ) ) {
|
2014-04-14 23:37:17 +00:00
|
|
|
|
2015-12-13 02:41:27 +00:00
|
|
|
var undef, time, mathAttr, $mathCells, indx, len,
|
|
|
|
changed = false,
|
|
|
|
filters = {};
|
2015-10-07 02:06:25 +00:00
|
|
|
if ( c.debug ) {
|
|
|
|
time = new Date();
|
|
|
|
}
|
|
|
|
|
2014-04-14 23:37:17 +00:00
|
|
|
// add data-column attributes to all table cells
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( init ) {
|
2015-10-15 01:46:42 +00:00
|
|
|
math.setColumnIndexes( c ) ;
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// data-attribute name (defaults to data-math)
|
2015-07-24 23:23:00 +00:00
|
|
|
wo.math_dataAttrib = 'data-' + (wo.math_data || 'math');
|
2014-04-14 23:37:17 +00:00
|
|
|
|
|
|
|
// all non-info tbody cells
|
2015-10-07 02:06:25 +00:00
|
|
|
mathAttr = wo.math_dataAttrib;
|
2015-11-10 20:43:25 +00:00
|
|
|
$mathCells = c.$tbodies.children( 'tr' ).children( '[' + mathAttr + ']' );
|
2015-12-13 15:23:49 +00:00
|
|
|
changed = math.mathType( c, $mathCells, wo.math_priority ) || changed;
|
2014-04-14 23:37:17 +00:00
|
|
|
|
|
|
|
// only info tbody cells
|
2015-07-24 23:23:00 +00:00
|
|
|
$mathCells = c.$table
|
|
|
|
.children( '.' + c.cssInfoBlock + ', tfoot' )
|
2015-11-10 20:43:25 +00:00
|
|
|
.children( 'tr' )
|
|
|
|
.children( '[' + mathAttr + ']' );
|
2015-07-24 23:23:00 +00:00
|
|
|
math.mathType( c, $mathCells, wo.math_priority );
|
2014-04-14 23:37:17 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
// find the 'all' total
|
2015-11-10 20:43:25 +00:00
|
|
|
$mathCells = c.$table.children().children( 'tr' ).children( '[' + mathAttr + '^=all]' );
|
2015-12-13 02:41:27 +00:00
|
|
|
len = $mathCells.length;
|
|
|
|
// get math filter, if any
|
|
|
|
// hasFilter = $row.attr( mathAttr + '-filter' ) || wo.math_rowFilter;
|
2016-02-25 19:53:52 +00:00
|
|
|
for (indx = 0; indx < len; indx++) {
|
|
|
|
var $cell = $mathCells.eq( indx ),
|
|
|
|
filter = $cell.attr( mathAttr + '-filter' ) || wo.math_rowFilter;
|
2015-12-13 02:41:27 +00:00
|
|
|
filters[ filter ] = filters[ filter ] ? filters[ filter ].add( $cell ) : $cell;
|
2016-02-25 19:53:52 +00:00
|
|
|
}
|
2015-12-13 02:41:27 +00:00
|
|
|
$.each( filters, function( hasFilter, $cells ) {
|
2015-12-13 15:23:49 +00:00
|
|
|
changed = math.mathType( c, $cells, [ 'all' ], hasFilter ) || changed;
|
2015-12-13 02:41:27 +00:00
|
|
|
});
|
2014-04-14 23:37:17 +00:00
|
|
|
|
2015-12-13 02:41:27 +00:00
|
|
|
// trigger an update only if cells inside the tbody changed
|
|
|
|
if ( changed ) {
|
|
|
|
wo.math_isUpdating = true;
|
|
|
|
if ( c.debug ) {
|
|
|
|
console[ console.group ? 'group' : 'log' ]( 'Math widget triggering an update after recalculation' );
|
|
|
|
}
|
2015-10-07 01:49:57 +00:00
|
|
|
|
2015-12-13 02:41:27 +00:00
|
|
|
// update internal cache
|
|
|
|
ts.update( c, undef, function(){
|
|
|
|
math.updateComplete( c );
|
2016-05-16 19:44:55 +00:00
|
|
|
if ( !init && typeof wo.math_completed === 'function' ) {
|
|
|
|
wo.math_completed( c );
|
|
|
|
}
|
|
|
|
if ( c.debug ) {
|
|
|
|
console.log( 'Math widget update completed' + ts.benchmark( time ) );
|
|
|
|
}
|
2015-12-13 02:41:27 +00:00
|
|
|
});
|
2016-05-16 19:44:55 +00:00
|
|
|
} else {
|
|
|
|
if ( !init && typeof wo.math_completed === 'function' ) {
|
|
|
|
wo.math_completed( c );
|
|
|
|
}
|
2015-12-13 02:41:27 +00:00
|
|
|
if ( c.debug ) {
|
2016-05-16 19:44:55 +00:00
|
|
|
console.log( 'Math widget found no changes in data' + ts.benchmark( time ) );
|
2015-12-13 02:41:27 +00:00
|
|
|
}
|
2015-10-05 16:52:35 +00:00
|
|
|
}
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-23 03:44:10 +00:00
|
|
|
updateComplete : function( c ) {
|
|
|
|
var wo = c.widgetOptions;
|
|
|
|
if ( wo.math_isUpdating && c.debug && console.groupEnd ) { console.groupEnd(); }
|
|
|
|
wo.math_isUpdating = false;
|
|
|
|
},
|
|
|
|
|
2015-12-13 02:41:27 +00:00
|
|
|
mathType : function( c, $cells, priority, hasFilter ) {
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( $cells.length ) {
|
2015-12-13 02:41:27 +00:00
|
|
|
var getAll,
|
|
|
|
changed = false,
|
2015-07-24 23:23:00 +00:00
|
|
|
wo = c.widgetOptions,
|
2015-10-07 01:51:40 +00:00
|
|
|
mathAttr = wo.math_dataAttrib,
|
2015-07-24 23:23:00 +00:00
|
|
|
equations = ts.equations;
|
|
|
|
if ( priority[0] === 'all' ) {
|
2015-12-13 02:41:27 +00:00
|
|
|
// mathType is called multiple times if more than one "hasFilter" is used
|
|
|
|
getAll = math.getAll( c, hasFilter );
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
if (c.debug) {
|
|
|
|
console[ console.group ? 'group' : 'log' ]( 'Tablesorter Math widget recalculation' );
|
|
|
|
}
|
2015-10-07 01:49:57 +00:00
|
|
|
// $.each is okay here... only 4 priorities
|
2015-07-24 23:23:00 +00:00
|
|
|
$.each( priority, function( i, type ) {
|
2015-12-13 02:41:27 +00:00
|
|
|
var index, arry, formula, result, $el,
|
|
|
|
$targetCells = $cells.filter( '[' + mathAttr + '^=' + type + ']' ),
|
|
|
|
len = $targetCells.length;
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( len ) {
|
|
|
|
if (c.debug) {
|
|
|
|
console[ console.group ? 'group' : 'log' ]( type );
|
|
|
|
}
|
|
|
|
for ( index = 0; index < len; index++ ) {
|
|
|
|
$el = $targetCells.eq( index );
|
2015-10-07 01:49:57 +00:00
|
|
|
// Row is filtered, no need to do further checking
|
|
|
|
if ( $el.parent().hasClass( wo.filter_filteredRow || 'filtered' ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-13 11:37:38 +00:00
|
|
|
hasFilter = $el.attr( mathAttr + '-filter' ) || wo.math_rowFilter;
|
2015-10-07 01:51:40 +00:00
|
|
|
formula = ( $el.attr( mathAttr ) || '' ).replace( type + '-', '' );
|
2015-12-13 02:41:27 +00:00
|
|
|
arry = ( type === 'row' ) ? math.getRow( c, $el, hasFilter ) :
|
|
|
|
( type === 'all' ) ? getAll : math.getColumn( c, $el, type, hasFilter );
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( equations[ formula ] ) {
|
|
|
|
if ( arry.length ) {
|
2015-10-17 15:05:55 +00:00
|
|
|
result = equations[ formula ]( arry, c );
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( c.debug ) {
|
2015-12-13 02:41:27 +00:00
|
|
|
console.log( $el.attr( mathAttr ), hasFilter ? '("' + hasFilter + '")' : '', arry, '=', result );
|
2015-07-24 23:23:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// mean will return a divide by zero error, everything else shows an undefined error
|
2015-10-15 02:20:49 +00:00
|
|
|
result = math.invalid( c, formula, formula === 'mean' ? 0 : 'undef' );
|
2015-07-24 23:23:00 +00:00
|
|
|
}
|
2015-12-13 02:41:27 +00:00
|
|
|
changed = math.output( $el, c, result, arry ) || changed;
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( c.debug && console.groupEnd ) { console.groupEnd(); }
|
|
|
|
}
|
2014-04-14 23:37:17 +00:00
|
|
|
});
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( c.debug && console.groupEnd ) { console.groupEnd(); }
|
2015-12-13 02:41:27 +00:00
|
|
|
return changed;
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-12-13 02:41:27 +00:00
|
|
|
return false;
|
2014-04-14 23:37:17 +00:00
|
|
|
},
|
|
|
|
|
2015-12-13 02:41:27 +00:00
|
|
|
output : function( $cell, c, value, arry ) {
|
2014-04-25 03:20:11 +00:00
|
|
|
// get mask from cell data-attribute: data-math-mask="#,##0.00"
|
2015-12-13 02:41:27 +00:00
|
|
|
var $el,
|
|
|
|
wo = c.widgetOptions,
|
|
|
|
changed = false,
|
|
|
|
prev = $cell.html(),
|
|
|
|
mask = $cell.attr( 'data-' + wo.math_data + '-mask' ) || wo.math_mask,
|
2015-07-24 23:23:00 +00:00
|
|
|
result = ts.formatMask( mask, value, wo.math_wrapPrefix, wo.math_wrapSuffix );
|
|
|
|
if ( typeof wo.math_complete === 'function' ) {
|
|
|
|
result = wo.math_complete( $cell, wo, result, value, arry );
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( result !== false ) {
|
2015-12-13 02:41:27 +00:00
|
|
|
changed = prev !== result;
|
2015-07-24 23:23:00 +00:00
|
|
|
$cell.html( result );
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-12-13 02:41:27 +00:00
|
|
|
// check if in a regular tbody, otherwise don't pass a changed flag
|
|
|
|
// to prevent unnecessary updating of the table cache
|
|
|
|
if ( changed ) {
|
|
|
|
$el = $cell.closest( 'tbody' );
|
|
|
|
// content was changed in a tfoot, info-only tbody or the resulting tbody is in a nested table
|
|
|
|
// then don't signal a change
|
|
|
|
if ( !$el.length || $el.hasClass( c.cssInfoBlock ) || $el.parent()[0] !== c.table ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return changed;
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// Modified from https://code.google.com/p/javascript-number-formatter/
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2015-07-24 23:23:00 +00:00
|
|
|
ts.formatMask = function( mask, val, tmpPrefix, tmpSuffix ) {
|
|
|
|
if ( !mask || isNaN( +val ) ) {
|
|
|
|
return val; // return as it is.
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2014-05-03 18:17:27 +00:00
|
|
|
|
|
|
|
var isNegative, result, decimal, group, posLeadZero, posTrailZero, posSeparator, part, szSep,
|
2015-07-24 23:23:00 +00:00
|
|
|
integer, str, offset, index, end, inv,
|
|
|
|
suffix = '',
|
|
|
|
|
|
|
|
// find prefix/suffix
|
|
|
|
len = mask.length,
|
|
|
|
start = mask.search( /[0-9\-\+#]/ ),
|
|
|
|
tmp = start > 0 ? mask.substring( 0, start ) : '',
|
|
|
|
prefix = tmp;
|
|
|
|
|
2014-05-03 18:17:27 +00:00
|
|
|
if ( start > 0 && tmpPrefix ) {
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( /\{content\}/.test( tmpPrefix || '' ) ) {
|
|
|
|
prefix = ( tmpPrefix || '' ).replace( /\{content\}/g, tmp || '' );
|
2014-05-03 18:17:27 +00:00
|
|
|
} else {
|
2015-07-24 23:23:00 +00:00
|
|
|
prefix = ( tmpPrefix || '' ) + tmp;
|
2014-05-03 18:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// reverse string: not an ideal method if there are surrogate pairs
|
2015-07-24 23:23:00 +00:00
|
|
|
inv = mask.split( '' ).reverse().join( '' );
|
2014-05-03 18:17:27 +00:00
|
|
|
end = inv.search( /[0-9\-\+#]/ );
|
2015-07-24 23:23:00 +00:00
|
|
|
index = len - end;
|
|
|
|
index += ( mask.substring( index, index + 1 ) === '.' ) ? 1 : 0;
|
|
|
|
tmp = end > 0 ? mask.substring( index, len ) : '';
|
2014-05-03 18:17:27 +00:00
|
|
|
suffix = tmp;
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( tmp !== '' && tmpSuffix ) {
|
|
|
|
if ( /\{content\}/.test( tmpSuffix || '' ) ) {
|
|
|
|
suffix = ( tmpSuffix || '' ).replace( /\{content\}/g, tmp || '' );
|
2014-05-03 18:17:27 +00:00
|
|
|
} else {
|
2015-07-24 23:23:00 +00:00
|
|
|
suffix = tmp + ( tmpSuffix || '' );
|
2014-05-03 18:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
mask = mask.substring( start, index );
|
2014-05-03 18:17:27 +00:00
|
|
|
|
2014-04-14 23:37:17 +00:00
|
|
|
// convert any string to number according to formation sign.
|
2015-07-24 23:23:00 +00:00
|
|
|
val = mask.charAt( 0 ) == '-' ? -val : +val;
|
|
|
|
isNegative = val < 0 ? val = -val : 0; // process only abs(), and turn on flag.
|
2014-04-14 23:37:17 +00:00
|
|
|
|
|
|
|
// search for separator for grp & decimal, anything not digit, not +/- sign, not #.
|
2015-07-24 23:23:00 +00:00
|
|
|
result = mask.match( /[^\d\-\+#]/g );
|
|
|
|
decimal = ( result && result[ result.length - 1 ] ) || '.'; // treat the right most symbol as decimal
|
|
|
|
group = ( result && result[ 1 ] && result[ 0 ] ) || ','; // treat the left most symbol as group separator
|
2014-04-14 23:37:17 +00:00
|
|
|
|
|
|
|
// split the decimal for the format string if any.
|
2015-07-24 23:23:00 +00:00
|
|
|
mask = mask.split( decimal );
|
2014-04-14 23:37:17 +00:00
|
|
|
// Fix the decimal first, toFixed will auto fill trailing zero.
|
2015-07-24 23:23:00 +00:00
|
|
|
val = val.toFixed( mask[ 1 ] && mask[ 1 ].length );
|
|
|
|
val = +( val ) + ''; // convert number to string to trim off *all* trailing decimal zero(es)
|
2014-04-14 23:37:17 +00:00
|
|
|
|
|
|
|
// fill back any trailing zero according to format
|
2015-07-24 23:23:00 +00:00
|
|
|
posTrailZero = mask[ 1 ] && mask[ 1 ].lastIndexOf( '0' ); // look for last zero in format
|
|
|
|
part = val.split( '.' );
|
2014-04-14 23:37:17 +00:00
|
|
|
// integer will get !part[1]
|
2015-07-24 23:23:00 +00:00
|
|
|
if ( !part[ 1 ] || ( part[ 1 ] && part[ 1 ].length <= posTrailZero ) ) {
|
|
|
|
val = ( +val ).toFixed( posTrailZero + 1 );
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
szSep = mask[ 0 ].split( group ); // look for separator
|
|
|
|
mask[ 0 ] = szSep.join( '' ); // join back without separator for counting the pos of any leading 0.
|
2014-04-14 23:37:17 +00:00
|
|
|
|
2015-07-24 23:23:00 +00:00
|
|
|
posLeadZero = mask[ 0 ] && mask[ 0 ].indexOf( '0' );
|
2014-05-03 18:17:27 +00:00
|
|
|
if ( posLeadZero > -1 ) {
|
2015-07-24 23:23:00 +00:00
|
|
|
while ( part[ 0 ].length < ( mask[ 0 ].length - posLeadZero ) ) {
|
|
|
|
part[ 0 ] = '0' + part[ 0 ];
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
} else if ( +part[ 0 ] === 0 ) {
|
|
|
|
part[ 0 ] = '';
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 23:23:00 +00:00
|
|
|
val = val.split( '.' );
|
|
|
|
val[ 0 ] = part[ 0 ];
|
2014-04-14 23:37:17 +00:00
|
|
|
|
|
|
|
// process the first group separator from decimal (.) only, the rest ignore.
|
|
|
|
// get the length of the last slice of split result.
|
2015-07-24 23:23:00 +00:00
|
|
|
posSeparator = ( szSep[ 1 ] && szSep[ szSep.length - 1 ].length );
|
2014-05-03 18:17:27 +00:00
|
|
|
if ( posSeparator ) {
|
2015-07-24 23:23:00 +00:00
|
|
|
integer = val[ 0 ];
|
2014-04-14 23:37:17 +00:00
|
|
|
str = '';
|
2014-05-03 18:17:27 +00:00
|
|
|
offset = integer.length % posSeparator;
|
2015-07-24 23:23:00 +00:00
|
|
|
len = integer.length;
|
|
|
|
for ( index = 0; index < len; index++ ) {
|
|
|
|
str += integer.charAt( index ); // ie6 only support charAt for sz.
|
2014-05-03 18:17:27 +00:00
|
|
|
// -posSeparator so that won't trail separator on full length
|
|
|
|
/*jshint -W018 */
|
2015-07-28 20:57:01 +00:00
|
|
|
if ( !( ( index - offset + 1 ) % posSeparator ) && index < len - posSeparator ) {
|
2014-04-14 23:37:17 +00:00
|
|
|
str += group;
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
val[ 0 ] = str;
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 23:23:00 +00:00
|
|
|
val[ 1 ] = ( mask[ 1 ] && val[ 1 ] ) ? decimal + val[ 1 ] : '';
|
2014-05-03 18:17:27 +00:00
|
|
|
// put back any negation, combine integer and fraction, and add back prefix & suffix
|
2015-07-24 23:23:00 +00:00
|
|
|
return prefix + ( ( isNegative ? '-' : '' ) + val[ 0 ] + val[ 1 ] ) + suffix;
|
2014-04-14 23:37:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ts.equations = {
|
2015-07-24 23:23:00 +00:00
|
|
|
count : function( arry ) {
|
2014-04-14 23:37:17 +00:00
|
|
|
return arry.length;
|
|
|
|
},
|
2015-07-24 23:23:00 +00:00
|
|
|
sum : function( arry ) {
|
|
|
|
var index,
|
|
|
|
len = arry.length,
|
|
|
|
total = 0;
|
|
|
|
for ( index = 0; index < len; index++ ) {
|
|
|
|
total += arry[ index ];
|
|
|
|
}
|
2014-04-14 23:37:17 +00:00
|
|
|
return total;
|
|
|
|
},
|
2015-07-24 23:23:00 +00:00
|
|
|
mean : function( arry ) {
|
2014-04-14 23:37:17 +00:00
|
|
|
var total = ts.equations.sum( arry );
|
|
|
|
return total / arry.length;
|
|
|
|
},
|
2015-10-17 15:05:55 +00:00
|
|
|
median : function( arry, c ) {
|
2015-07-24 23:23:00 +00:00
|
|
|
var half,
|
|
|
|
len = arry.length;
|
|
|
|
if ( len > 1 ) {
|
|
|
|
// https://gist.github.com/caseyjustus/1166258
|
|
|
|
arry.sort( function( a, b ){ return a - b; } );
|
|
|
|
half = Math.floor( len / 2 );
|
|
|
|
return ( len % 2 ) ? arry[ half ] : ( arry[ half - 1 ] + arry[ half ] ) / 2;
|
|
|
|
}
|
2015-10-15 02:20:49 +00:00
|
|
|
return math.invalid( c, 'median', 1 );
|
2014-04-14 23:37:17 +00:00
|
|
|
},
|
2015-07-24 23:23:00 +00:00
|
|
|
mode : function( arry ) {
|
2014-04-14 23:37:17 +00:00
|
|
|
// http://stackoverflow.com/a/3451640/145346
|
2015-07-24 23:23:00 +00:00
|
|
|
var index, el, m,
|
2014-04-14 23:37:17 +00:00
|
|
|
modeMap = {},
|
|
|
|
maxCount = 1,
|
2015-07-24 23:23:00 +00:00
|
|
|
modes = [ arry[ 0 ] ];
|
|
|
|
for ( index = 0; index < arry.length; index++ ) {
|
|
|
|
el = arry[ index ];
|
|
|
|
modeMap[ el ] = modeMap[ el ] ? modeMap[ el ] + 1 : 1;
|
|
|
|
m = modeMap[ el ];
|
|
|
|
if ( m > maxCount ) {
|
2015-07-23 04:29:51 +00:00
|
|
|
modes = [ el ];
|
2015-07-24 23:23:00 +00:00
|
|
|
maxCount = m;
|
|
|
|
} else if ( m === maxCount ) {
|
2016-02-25 19:53:52 +00:00
|
|
|
modes[ modes.length ] = el;
|
2015-07-24 23:23:00 +00:00
|
|
|
maxCount = m;
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// returns arry of modes if there is a tie
|
2015-07-24 23:23:00 +00:00
|
|
|
return modes.sort( function( a, b ){ return a - b; } );
|
2014-04-14 23:37:17 +00:00
|
|
|
},
|
|
|
|
max : function(arry) {
|
|
|
|
return Math.max.apply( Math, arry );
|
|
|
|
},
|
|
|
|
min : function(arry) {
|
|
|
|
return Math.min.apply( Math, arry );
|
|
|
|
},
|
|
|
|
range: function(arry) {
|
2015-07-24 23:23:00 +00:00
|
|
|
var v = arry.sort( function( a, b ){ return a - b; } );
|
|
|
|
return v[ arry.length - 1 ] - v[ 0 ];
|
2014-04-14 23:37:17 +00:00
|
|
|
},
|
|
|
|
// common variance equation
|
|
|
|
// (not accessible via data-attribute setting)
|
2015-10-17 15:05:55 +00:00
|
|
|
variance: function( arry, population, c ) {
|
2015-07-24 23:23:00 +00:00
|
|
|
var divisor,
|
|
|
|
avg = ts.equations.mean( arry ),
|
2014-04-14 23:37:17 +00:00
|
|
|
v = 0,
|
|
|
|
i = arry.length;
|
2015-07-24 23:23:00 +00:00
|
|
|
while ( i-- ) {
|
|
|
|
v += Math.pow( ( arry[ i ] - avg ), 2 );
|
|
|
|
}
|
|
|
|
divisor = ( arry.length - ( population ? 0 : 1 ) );
|
|
|
|
if ( divisor === 0 ) {
|
2015-10-15 02:20:49 +00:00
|
|
|
return math.invalid( c, 'variance', 0 );
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
2015-07-24 23:23:00 +00:00
|
|
|
v /= divisor;
|
2014-04-14 23:37:17 +00:00
|
|
|
return v;
|
|
|
|
},
|
|
|
|
// variance (population)
|
2015-10-17 15:05:55 +00:00
|
|
|
varp : function( arry, c ) {
|
|
|
|
return ts.equations.variance( arry, true, c );
|
2014-04-14 23:37:17 +00:00
|
|
|
},
|
|
|
|
// variance (sample)
|
2015-10-17 15:05:55 +00:00
|
|
|
vars : function( arry, c ) {
|
|
|
|
return ts.equations.variance( arry, false, c );
|
2014-04-14 23:37:17 +00:00
|
|
|
},
|
|
|
|
// standard deviation (sample)
|
2015-10-17 15:05:55 +00:00
|
|
|
stdevs : function( arry, c ) {
|
|
|
|
var vars = ts.equations.variance( arry, false, c );
|
2014-04-14 23:37:17 +00:00
|
|
|
return Math.sqrt( vars );
|
|
|
|
},
|
|
|
|
// standard deviation (population)
|
2015-10-17 15:05:55 +00:00
|
|
|
stdevp : function( arry, c ) {
|
|
|
|
var varp = ts.equations.variance( arry, true, c );
|
2014-04-14 23:37:17 +00:00
|
|
|
return Math.sqrt( varp );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// add new widget called repeatHeaders
|
|
|
|
// ************************************
|
|
|
|
ts.addWidget({
|
2015-07-23 04:29:51 +00:00
|
|
|
id: 'math',
|
2014-04-14 23:37:17 +00:00
|
|
|
priority: 100,
|
|
|
|
options: {
|
|
|
|
math_data : 'math',
|
|
|
|
// column index to ignore
|
|
|
|
math_ignore : [],
|
|
|
|
// mask info: https://code.google.com/p/javascript-number-formatter/
|
|
|
|
math_mask : '#,##0.00',
|
|
|
|
// complete executed after each fucntion
|
|
|
|
math_complete : null, // function($cell, wo, result, value, arry){ return result; },
|
2016-05-16 19:44:55 +00:00
|
|
|
// math_completed called after all math calculations have completed
|
|
|
|
// math_completed : function( config ) {},
|
2015-07-23 04:29:51 +00:00
|
|
|
// order of calculation; 'all' is last
|
2015-10-04 15:06:07 +00:00
|
|
|
math_priority : [ 'row', 'above', 'below', 'col' ],
|
2014-05-03 18:17:27 +00:00
|
|
|
// template for or just prepend the mask prefix & suffix with this HTML
|
|
|
|
// e.g. '<span class="red">{content}</span>'
|
|
|
|
math_prefix : '',
|
2015-02-09 22:57:08 +00:00
|
|
|
math_suffix : '',
|
2015-10-15 02:20:49 +00:00
|
|
|
// no matching math elements found (text added to cell)
|
|
|
|
math_none : 'N/A',
|
2015-11-22 22:22:05 +00:00
|
|
|
math_event : 'recalculate',
|
|
|
|
// use this filter to target specific rows (e.g. ':visible', or ':not(.empty-row)')
|
|
|
|
math_rowFilter: ''
|
2014-04-14 23:37:17 +00:00
|
|
|
},
|
2015-07-24 23:23:00 +00:00
|
|
|
init : function( table, thisWidget, c, wo ) {
|
|
|
|
// filterEnd fires after updateComplete
|
2015-12-13 02:41:27 +00:00
|
|
|
var update = ( ts.hasWidget( table, 'filter' ) ? 'filterEnd' : 'updateComplete' ) + '.tsmath';
|
|
|
|
// filterEnd is when the pager hides rows... so bind to pagerComplete
|
|
|
|
math.events += ( ts.hasWidget( table, 'pager' ) ? 'pagerComplete' : 'filterEnd' ) + '.tsmath ';
|
2014-04-14 23:37:17 +00:00
|
|
|
c.$table
|
2015-12-13 02:41:27 +00:00
|
|
|
.off( ( math.events + 'updateComplete.tsmath ' + wo.math_event ).replace( /\s+/g, ' ' ) )
|
|
|
|
.on( math.events + wo.math_event, function( e ) {
|
|
|
|
if ( !this.hasInitialized ) { return; }
|
2015-02-09 20:32:37 +00:00
|
|
|
var init = e.type === 'tablesorter-initialized';
|
2015-04-30 13:04:24 +00:00
|
|
|
if ( !wo.math_isUpdating || init ) {
|
2015-12-04 06:48:41 +00:00
|
|
|
// don't setColumnIndexes on init here, or it gets done twice
|
|
|
|
if ( !/filter/.test( e.type ) && !init ) {
|
2015-04-30 13:04:24 +00:00
|
|
|
// redo data-column indexes on update
|
2015-12-04 06:48:41 +00:00
|
|
|
math.setColumnIndexes( c );
|
2015-04-30 13:04:24 +00:00
|
|
|
}
|
2015-07-28 20:57:01 +00:00
|
|
|
math.recalculate( c, wo, init );
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
})
|
2015-12-13 02:41:27 +00:00
|
|
|
.on( update, function() {
|
2015-07-24 23:23:00 +00:00
|
|
|
setTimeout( function(){
|
2015-11-23 03:44:10 +00:00
|
|
|
math.updateComplete( c );
|
2015-07-24 23:23:00 +00:00
|
|
|
}, 40 );
|
2014-04-14 23:37:17 +00:00
|
|
|
});
|
|
|
|
wo.math_isUpdating = false;
|
2015-09-05 14:28:02 +00:00
|
|
|
// math widget initialized after table - see #946
|
|
|
|
if ( table.hasInitialized ) {
|
|
|
|
math.recalculate( c, wo, true );
|
|
|
|
}
|
2014-04-14 23:37:17 +00:00
|
|
|
},
|
|
|
|
// this remove function is called when using the refreshWidgets method or when destroying the tablesorter plugin
|
|
|
|
// this function only applies to tablesorter v2.4+
|
2015-07-24 23:23:00 +00:00
|
|
|
remove: function( table, c, wo, refreshing ) {
|
|
|
|
if ( refreshing ) { return; }
|
|
|
|
c.$table
|
|
|
|
.off( ( math.events + ' updateComplete.tsmath ' + wo.math_event ).replace( /\s+/g, ' ' ) )
|
2015-11-10 20:43:25 +00:00
|
|
|
.children().children( 'tr' ).children( '[data-' + wo.math_data + ']' ).empty();
|
2014-04-14 23:37:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-02-09 18:20:11 +00:00
|
|
|
})(jQuery);
|