| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
;( function( $ ) { |
| 9 |
'use strict'; |
| 10 |
var ts = $.tablesorter; |
| 11 |
|
| 12 |
ts.sortTbodies = { |
| 13 |
init: function( c, wo ) { |
| 14 |
|
| 15 |
var index, rows, txt, max, $rows, |
| 16 |
namespace = c.namespace + 'sortTbody', |
| 17 |
$tbodies = c.$table.children( 'tbody' ), |
| 18 |
len = $tbodies.length; |
| 19 |
|
| 20 |
|
| 21 |
wo.sortTbody_original_serverSideSorting = c.serverSideSorting; |
| 22 |
|
| 23 |
|
| 24 |
wo.sortTbody_original_cssInfoBlock = c.cssInfoBlock; |
| 25 |
c.cssInfoBlock = wo.sortTbody_noSort; |
| 26 |
ts.sortTbodies.setTbodies( c, wo ); |
| 27 |
|
| 28 |
|
| 29 |
for ( index = 0; index < len; index++ ) { |
| 30 |
$tbodies.eq( index ).attr( 'data-ts-original-order', index ); |
| 31 |
} |
| 32 |
|
| 33 |
c.$table |
| 34 |
.unbind( 'sortBegin updateComplete '.split( ' ' ).join( namespace + ' ' ) ) |
| 35 |
.bind( 'sortBegin' + namespace, function() { |
| 36 |
ts.sortTbodies.sorter( c ); |
| 37 |
}) |
| 38 |
.bind( 'updateComplete' + namespace, function() { |
| 39 |
|
| 40 |
ts.sortTbodies.setTbodies( c, wo ); |
| 41 |
ts.updateCache( c, null, c.$tbodies ); |
| 42 |
}) |
| 43 |
.bind('sortEnd', function() { |
| 44 |
|
| 45 |
var primaryRow = wo.sortTbody_primaryRow; |
| 46 |
if ( wo.sortTbody_lockHead && primaryRow ) { |
| 47 |
c.$table.find( primaryRow ).each( function() { |
| 48 |
$( this ).parents( 'tbody' ).prepend( this ); |
| 49 |
}); |
| 50 |
} |
| 51 |
}); |
| 52 |
|
| 53 |
|
| 54 |
if ( $.isEmptyObject( c.parsers ) || c.$tbodies.length !== $tbodies.length ) { |
| 55 |
ts.sortTbodies.setTbodies( c, wo ); |
| 56 |
ts.updateCache( c, null, c.$tbodies ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
$rows = $tbodies.children( 'tr' ); |
| 61 |
len = $rows.length; |
| 62 |
for ( index = 0; index < c.columns; index++ ) { |
| 63 |
max = 0; |
| 64 |
if ( c.parsers[ index ].type === 'numeric' ) { |
| 65 |
for ( rows = 0; rows < len; rows++ ) { |
| 66 |
|
| 67 |
txt = ts.getParsedText( c, $rows.eq( rows ).children()[ index ], index ); |
| 68 |
max = Math.max( Math.abs( txt ) || 0, max ); |
| 69 |
} |
| 70 |
} |
| 71 |
c.$headerIndexed[ index ].attr( 'data-ts-col-max-value', max ); |
| 72 |
} |
| 73 |
|
| 74 |
}, |
| 75 |
|
| 76 |
|
| 77 |
setTbodies: function( c, wo ) { |
| 78 |
c.$tbodies = c.$table.children( 'tbody' ).not( '.' + wo.sortTbody_noSort ); |
| 79 |
}, |
| 80 |
|
| 81 |
sorter: function( c ) { |
| 82 |
var $table = c.$table, |
| 83 |
wo = c.widgetOptions; |
| 84 |
|
| 85 |
|
| 86 |
if ( wo.sortTbody_busy !== true ) { |
| 87 |
wo.sortTbody_busy = true; |
| 88 |
var $tbodies = $table.children( 'tbody' ).not( '.' + wo.sortTbody_noSort ), |
| 89 |
primary = wo.sortTbody_primaryRow || 'tr:eq(0)', |
| 90 |
sortList = c.sortList || [], |
| 91 |
len = sortList.length; |
| 92 |
|
| 93 |
if ( len ) { |
| 94 |
|
| 95 |
|
| 96 |
c.serverSideSorting = !wo.sortTbody_sortRows; |
| 97 |
|
| 98 |
$tbodies.sort( function( a, b ) { |
| 99 |
var sortListIndex, txt, dir, num, colMax, sort, col, order, colA, colB, x, y, |
| 100 |
table = c.table, |
| 101 |
parsers = c.parsers, |
| 102 |
cts = c.textSorter || '', |
| 103 |
$tbodyA = $( a ), |
| 104 |
$tbodyB = $( b ), |
| 105 |
$a = $tbodyA.find( primary ).children( 'td, th' ), |
| 106 |
$b = $tbodyB.find( primary ).children( 'td, th' ); |
| 107 |
for ( sortListIndex = 0; sortListIndex < len; sortListIndex++ ) { |
| 108 |
col = sortList[ sortListIndex ][0]; |
| 109 |
order = sortList[ sortListIndex ][1]; |
| 110 |
|
| 111 |
dir = order === 0; |
| 112 |
|
| 113 |
txt = ts.getElementText( c, $a.eq( col ), col ); |
| 114 |
colA = parsers[ col ].format( txt, table, $a[ col ], col ); |
| 115 |
|
| 116 |
txt = ts.getElementText( c, $b.eq( col ), col ); |
| 117 |
colB = parsers[ col ].format( txt, table, $b[ col ], col ); |
| 118 |
|
| 119 |
if (c.sortStable && colA === colB && len === 1) { |
| 120 |
return $tbodyA.attr( 'data-ts-original-order' ) - $tbodyB.attr( 'data-ts-original-order' ); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
num = /n/i.test( parsers && parsers[ col ] ? parsers[ col ].type || '' : '' ); |
| 125 |
if ( num && c.strings[ col ] ) { |
| 126 |
colMax = c.$headerIndexed[ col ].attr( 'data-ts-col-max-value' ) || |
| 127 |
1.79E+308; |
| 128 |
|
| 129 |
if ( typeof ( ts.string[ c.strings[ col ] ] ) === 'boolean' ) { |
| 130 |
num = ( dir ? 1 : -1 ) * ( ts.string[ c.strings[ col ] ] ? -1 : 1 ); |
| 131 |
} else { |
| 132 |
num = ( c.strings[ col ] ) ? ts.string[ c.strings[ col ] ] || 0 : 0; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
sort = c.numberSorter ? c.numberSorter( colA, colB, dir, colMax, table ) : |
| 137 |
ts[ 'sortNumeric' + ( dir ? 'Asc' : 'Desc' ) ]( colA, colB, num, colMax, col, c ); |
| 138 |
} else { |
| 139 |
|
| 140 |
x = dir ? colA : colB; |
| 141 |
y = dir ? colB : colA; |
| 142 |
|
| 143 |
if ( typeof ( cts ) === 'function' ) { |
| 144 |
|
| 145 |
sort = cts( x, y, dir, col, table ); |
| 146 |
} else if ( typeof ( cts ) === 'object' && cts.hasOwnProperty( col ) ) { |
| 147 |
|
| 148 |
sort = cts[ col ]( x, y, dir, col, table ); |
| 149 |
} else { |
| 150 |
|
| 151 |
sort = ts[ 'sortNatural' + ( dir ? 'Asc' : 'Desc' ) ]( colA, colB, col, c ); |
| 152 |
} |
| 153 |
} |
| 154 |
if ( sort ) { return sort; } |
| 155 |
} |
| 156 |
return $tbodyA.attr( 'data-ts-original-order' ) - $tbodyB.attr( 'data-ts-original-order' ); |
| 157 |
}); |
| 158 |
|
| 159 |
ts.sortTbodies.restoreTbodies( c, wo, $tbodies ); |
| 160 |
wo.sortTbody_busy = false; |
| 161 |
} |
| 162 |
} |
| 163 |
}, |
| 164 |
|
| 165 |
restoreTbodies : function ( c, wo, $sortedTbodies ) { |
| 166 |
var $nosort, $tbodies, $thisTbody, tbLen, nsLen, index, targetIndex, |
| 167 |
$table = c.$table, |
| 168 |
hasShuffled = true, |
| 169 |
indx = 0; |
| 170 |
|
| 171 |
|
| 172 |
$table.hide(); |
| 173 |
$sortedTbodies.appendTo( $table ); |
| 174 |
|
| 175 |
|
| 176 |
$tbodies = $table.children( 'tbody' ); |
| 177 |
tbLen = $tbodies.length; |
| 178 |
$nosort = $tbodies.filter( '.' + wo.sortTbody_noSort ).appendTo( $table ); |
| 179 |
nsLen = $nosort.length; |
| 180 |
|
| 181 |
if ( nsLen ) { |
| 182 |
|
| 183 |
while ( hasShuffled && indx < nsLen ) { |
| 184 |
hasShuffled = false; |
| 185 |
for ( index = 0; index < nsLen; index++ ) { |
| 186 |
targetIndex = parseInt( $nosort.eq( index ).attr( 'data-ts-original-order' ), 10 ); |
| 187 |
|
| 188 |
targetIndex = targetIndex >= tbLen ? tbLen : targetIndex < 0 ? 0 : targetIndex; |
| 189 |
|
| 190 |
if ( targetIndex !== $nosort.eq( index ).index() ) { |
| 191 |
hasShuffled = true; |
| 192 |
$thisTbody = $nosort.eq( index ).detach(); |
| 193 |
|
| 194 |
if ( targetIndex >= tbLen ) { |
| 195 |
|
| 196 |
$thisTbody.appendTo( $table ); |
| 197 |
} else if ( targetIndex === 0 ) { |
| 198 |
|
| 199 |
$thisTbody.prependTo( $table ); |
| 200 |
} else { |
| 201 |
|
| 202 |
$thisTbody.insertBefore( $table.children( 'tbody:eq(' + targetIndex + ')' ) ); |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
} |
| 207 |
indx++; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
$table.show(); |
| 212 |
} |
| 213 |
|
| 214 |
}; |
| 215 |
|
| 216 |
ts.addWidget({ |
| 217 |
id: 'sortTbody', |
| 218 |
|
| 219 |
priority: 40, |
| 220 |
options: { |
| 221 |
|
| 222 |
sortTbody_lockHead : false, |
| 223 |
|
| 224 |
sortTbody_primaryRow : null, |
| 225 |
|
| 226 |
sortTbody_sortRows : false, |
| 227 |
|
| 228 |
sortTbody_noSort : 'tablesorter-no-sort-tbody' |
| 229 |
}, |
| 230 |
init : function( table, thisWidget, c, wo ) { |
| 231 |
ts.sortTbodies.init( c, wo ); |
| 232 |
}, |
| 233 |
remove : function( table, c, wo ) { |
| 234 |
c.$table.unbind( 'sortBegin updateComplete '.split( ' ' ).join( c.namespace + 'sortTbody ' ) ); |
| 235 |
c.serverSideSorting = wo.sortTbody_original_serverSideSorting; |
| 236 |
c.cssInfoBlock = wo.sortTbody_original_cssInfoBlock; |
| 237 |
} |
| 238 |
}); |
| 239 |
|
| 240 |
})( jQuery ); |