Debian Codegraph @

js/widgets/widget-sortTbodies.js

raw symbols SCIP
1 /*! tablesorter tbody sorting widget (BETA) - 11/26/2016 (v2.28.0)
2 * Requires tablesorter v2.22.2+ and jQuery 1.4+
3 * by Rob Garrison
4 * Contributors: Chris Rogers
5 */
6 /*jshint browser:true, jquery:true, unused:false */
7 /*global jQuery: false */
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 // save serverSideSorting value; use to toggle internal row sorting
21 wo.sortTbody_original_serverSideSorting = c.serverSideSorting;
22
23 // include info-only tbodies - we need parsed data from *all* tbodies
24 wo.sortTbody_original_cssInfoBlock = c.cssInfoBlock;
25 c.cssInfoBlock = wo.sortTbody_noSort;
26 ts.sortTbodies.setTbodies( c, wo );
27
28 // add original order index for stable sort
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 // find parsers for each column
40 ts.sortTbodies.setTbodies( c, wo );
41 ts.updateCache( c, null, c.$tbodies );
42 })
43 .bind('sortEnd', function() {
44 // Moves the head row back to the top of the tbody
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 // detect parsers - in case the table contains only info-only tbodies
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 // find colMax; this only matter for numeric columns
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 // update column max value (ignore sign)
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 // make sure c.$tbodies is up-to-date (init & after updates)
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 // prevent multiple calls while processing
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 // toggle internal row sorting
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 // sort direction, true = asc, false = desc
111 dir = order === 0;
112 // column txt - tbody A
113 txt = ts.getElementText( c, $a.eq( col ), col );
114 colA = parsers[ col ].format( txt, table, $a[ col ], col );
115 // column txt - tbody B
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 // fallback to natural sort since it is more robust
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; // close to Number.MAX_VALUE
128 // sort strings in numerical columns
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 // fall back to built-in numeric sort
135 // var sort = $.tablesorter['sort' + s](a, b, dir, colMax, table);
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 // set a & b depending on sort direction
140 x = dir ? colA : colB;
141 y = dir ? colB : colA;
142 // text sort function
143 if ( typeof ( cts ) === 'function' ) {
144 // custom OVERALL text sorter
145 sort = cts( x, y, dir, col, table );
146 } else if ( typeof ( cts ) === 'object' && cts.hasOwnProperty( col ) ) {
147 // custom text sorter for a SPECIFIC COLUMN
148 sort = cts[ col ]( x, y, dir, col, table );
149 } else {
150 // fall back to natural sort
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 // hide entire table to improve sort performance
172 $table.hide();
173 $sortedTbodies.appendTo( $table );
174
175 // reposition no-sort tbodies
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 // don't allow the while loop to cycle more times than the set number of no-sort tbodies
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 // if target index > number of tbodies, make it last
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 // Are we trying to be the last tbody?
196 $thisTbody.appendTo( $table );
197 } else if ( targetIndex === 0 ) {
198 // Are we trying to be the first tbody?
199 $thisTbody.prependTo( $table );
200 } else {
201 // No, we want to be somewhere in the middle!
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 // priority < 50 (filter widget), so c.$tbodies has the correct elements
219 priority: 40,
220 options: {
221 // lock primary row as a header when sorting
222 sortTbody_lockHead : false,
223 // point to a row within the tbody that matches the number of header columns
224 sortTbody_primaryRow : null,
225 // sort tbody internal rows
226 sortTbody_sortRows : false,
227 // static tbodies (like static rows)
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 );