Merge "Fix sessionfailure i18n message during authentication"
[lhc/web/wiklou.git] / resources / src / jquery / jquery.tablesorter.js
1 /*!
2 * TableSorter for MediaWiki
3 *
4 * Written 2011 Leo Koppelkamm
5 * Based on tablesorter.com plugin, written (c) 2007 Christian Bach.
6 *
7 * Dual licensed under the MIT and GPL licenses:
8 * http://www.opensource.org/licenses/mit-license.php
9 * http://www.gnu.org/licenses/gpl.html
10 *
11 * Depends on mw.config (wgDigitTransformTable, wgDefaultDateFormat, wgPageContentLanguage)
12 * and mw.language.months.
13 *
14 * Uses 'tableSorterCollation' in mw.config (if available)
15 *
16 * Create a sortable table with multi-column sorting capabilities
17 *
18 * // Create a simple tablesorter interface
19 * $( 'table' ).tablesorter();
20 *
21 * // Create a tablesorter interface, initially sorting on the first and second column
22 * $( 'table' ).tablesorter( { sortList: [ { 0: 'desc' }, { 1: 'asc' } ] } );
23 *
24 * @param {string} [cssHeader="header"] A string of the class name to be appended to sortable
25 * tr elements in the thead of the table.
26 *
27 * @param {string} [cssAsc="headerSortUp"] A string of the class name to be appended to
28 * sortable tr elements in the thead on a ascending sort.
29 *
30 * @param {string} [cssDesc="headerSortDown"] A string of the class name to be appended to
31 * sortable tr elements in the thead on a descending sort.
32 *
33 * @param {string} [sortMultisortKey="shiftKey"] A string of the multi-column sort key.
34 *
35 * @param {boolean} [cancelSelection=true] Boolean flag indicating iftablesorter should cancel
36 * selection of the table headers text.
37 *
38 * @param {Array} [sortList] An array containing objects specifying sorting. By passing more
39 * than one object, multi-sorting will be applied. Object structure:
40 * { <Integer column index>: <String 'asc' or 'desc'> }
41 *
42 * @event sortEnd.tablesorter: Triggered as soon as any sorting has been applied.
43 *
44 * @author Christian Bach/christian.bach@polyester.se
45 */
46 ( function ( $, mw ) {
47 var ts,
48 parsers = [];
49
50 /* Parser utility functions */
51
52 function getParserById( name ) {
53 var i;
54 for ( i = 0; i < parsers.length; i++ ) {
55 if ( parsers[ i ].id.toLowerCase() === name.toLowerCase() ) {
56 return parsers[ i ];
57 }
58 }
59 return false;
60 }
61
62 function getElementSortKey( node ) {
63 var $node = $( node ),
64 // Use data-sort-value attribute.
65 // Use data() instead of attr() so that live value changes
66 // are processed as well (T40152).
67 data = $node.data( 'sortValue' );
68
69 if ( data !== null && data !== undefined ) {
70 // Cast any numbers or other stuff to a string, methods
71 // like charAt, toLowerCase and split are expected.
72 return String( data );
73 }
74 if ( !node ) {
75 return $node.text();
76 }
77 if ( node.tagName.toLowerCase() === 'img' ) {
78 return $node.attr( 'alt' ) || ''; // handle undefined alt
79 }
80 return $.makeArray( node.childNodes ).map( function ( elem ) {
81 if ( elem.nodeType === Node.ELEMENT_NODE ) {
82 return getElementSortKey( elem );
83 }
84 return $.text( elem );
85 } ).join( '' );
86 }
87
88 function detectParserForColumn( table, rows, column ) {
89 var l = parsers.length,
90 config = $( table ).data( 'tablesorter' ).config,
91 cellIndex,
92 nodeValue,
93 nextRow = false,
94 // Start with 1 because 0 is the fallback parser
95 i = 1,
96 lastRowIndex = -1,
97 rowIndex = 0,
98 concurrent = 0,
99 empty = 0,
100 needed = ( rows.length > 4 ) ? 5 : rows.length;
101
102 while ( i < l ) {
103 // if this is a child row, continue to the next row (as buildCache())
104 if ( rows[ rowIndex ] && !$( rows[ rowIndex ] ).hasClass( config.cssChildRow ) ) {
105 if ( rowIndex !== lastRowIndex ) {
106 lastRowIndex = rowIndex;
107 cellIndex = $( rows[ rowIndex ] ).data( 'columnToCell' )[ column ];
108 nodeValue = getElementSortKey( rows[ rowIndex ].cells[ cellIndex ] ).trim();
109 }
110 } else {
111 nodeValue = '';
112 }
113
114 if ( nodeValue !== '' ) {
115 if ( parsers[ i ].is( nodeValue, table ) ) {
116 concurrent++;
117 nextRow = true;
118 if ( concurrent >= needed ) {
119 // Confirmed the parser for multiple cells, let's return it
120 return parsers[ i ];
121 }
122 } else if ( parsers[ i ].id.match( /isoDate/ ) && /^\D*(\d{1,4}) ?(\[.+\])?$/.test( nodeValue ) ) {
123 // For 1-4 digits and maybe reference(s) parser "isoDate" or "number" is possible, check next row
124 empty++;
125 nextRow = true;
126 } else {
127 // Check next parser, reset rows
128 i++;
129 rowIndex = 0;
130 concurrent = 0;
131 empty = 0;
132 nextRow = false;
133 }
134 } else {
135 // Empty cell
136 empty++;
137 nextRow = true;
138 }
139
140 if ( nextRow ) {
141 nextRow = false;
142 rowIndex++;
143 if ( rowIndex >= rows.length ) {
144 if ( concurrent > 0 && concurrent >= rows.length - empty ) {
145 // Confirmed the parser for all filled cells
146 return parsers[ i ];
147 }
148 // Check next parser, reset rows
149 i++;
150 rowIndex = 0;
151 concurrent = 0;
152 empty = 0;
153 }
154 }
155 }
156
157 // 0 is always the generic parser (text)
158 return parsers[ 0 ];
159 }
160
161 function buildParserCache( table, $headers ) {
162 var sortType, len, j, parser,
163 rows = table.tBodies[ 0 ].rows,
164 config = $( table ).data( 'tablesorter' ).config,
165 parsers = [];
166
167 if ( rows[ 0 ] ) {
168 len = config.columns;
169 for ( j = 0; j < len; j++ ) {
170 parser = false;
171 sortType = $headers.eq( config.columnToHeader[ j ] ).data( 'sortType' );
172 if ( sortType !== undefined ) {
173 parser = getParserById( sortType );
174 }
175
176 if ( parser === false ) {
177 parser = detectParserForColumn( table, rows, j );
178 }
179
180 parsers.push( parser );
181 }
182 }
183 return parsers;
184 }
185
186 /* Other utility functions */
187
188 function buildCache( table ) {
189 var i, j, $row, cols,
190 totalRows = ( table.tBodies[ 0 ] && table.tBodies[ 0 ].rows.length ) || 0,
191 config = $( table ).data( 'tablesorter' ).config,
192 parsers = config.parsers,
193 len = parsers.length,
194 cellIndex,
195 cache = {
196 row: [],
197 normalized: []
198 };
199
200 for ( i = 0; i < totalRows; i++ ) {
201
202 // Add the table data to main data array
203 $row = $( table.tBodies[ 0 ].rows[ i ] );
204 cols = [];
205
206 // if this is a child row, add it to the last row's children and
207 // continue to the next row
208 if ( $row.hasClass( config.cssChildRow ) ) {
209 cache.row[ cache.row.length - 1 ] = cache.row[ cache.row.length - 1 ].add( $row );
210 // go to the next for loop
211 continue;
212 }
213
214 cache.row.push( $row );
215
216 for ( j = 0; j < len; j++ ) {
217 cellIndex = $row.data( 'columnToCell' )[ j ];
218 cols.push( parsers[ j ].format( getElementSortKey( $row[ 0 ].cells[ cellIndex ] ) ) );
219 }
220
221 cols.push( cache.normalized.length ); // add position for rowCache
222 cache.normalized.push( cols );
223 cols = null;
224 }
225
226 return cache;
227 }
228
229 function appendToTable( table, cache ) {
230 var i, pos, l, j,
231 row = cache.row,
232 normalized = cache.normalized,
233 totalRows = normalized.length,
234 checkCell = ( normalized[ 0 ].length - 1 ),
235 fragment = document.createDocumentFragment();
236
237 for ( i = 0; i < totalRows; i++ ) {
238 pos = normalized[ i ][ checkCell ];
239
240 l = row[ pos ].length;
241 for ( j = 0; j < l; j++ ) {
242 fragment.appendChild( row[ pos ][ j ] );
243 }
244
245 }
246 table.tBodies[ 0 ].appendChild( fragment );
247
248 $( table ).trigger( 'sortEnd.tablesorter' );
249 }
250
251 /**
252 * Find all header rows in a thead-less table and put them in a <thead> tag.
253 * This only treats a row as a header row if it contains only <th>s (no <td>s)
254 * and if it is preceded entirely by header rows. The algorithm stops when
255 * it encounters the first non-header row.
256 *
257 * After this, it will look at all rows at the bottom for footer rows
258 * And place these in a tfoot using similar rules.
259 *
260 * @param {jQuery} $table object for a <table>
261 */
262 function emulateTHeadAndFoot( $table ) {
263 var $thead, $tfoot, i, len,
264 $rows = $table.find( '> tbody > tr' );
265 if ( !$table.get( 0 ).tHead ) {
266 $thead = $( '<thead>' );
267 $rows.each( function () {
268 if ( $( this ).children( 'td' ).length ) {
269 // This row contains a <td>, so it's not a header row
270 // Stop here
271 return false;
272 }
273 $thead.append( this );
274 } );
275 $table.find( ' > tbody:first' ).before( $thead );
276 }
277 if ( !$table.get( 0 ).tFoot ) {
278 $tfoot = $( '<tfoot>' );
279 len = $rows.length;
280 for ( i = len - 1; i >= 0; i-- ) {
281 if ( $( $rows[ i ] ).children( 'td' ).length ) {
282 break;
283 }
284 $tfoot.prepend( $( $rows[ i ] ) );
285 }
286 $table.append( $tfoot );
287 }
288 }
289
290 function uniqueElements( array ) {
291 var uniques = [];
292 array.forEach( function ( elem ) {
293 if ( elem !== undefined && uniques.indexOf( elem ) === -1 ) {
294 uniques.push( elem );
295 }
296 } );
297 return uniques;
298 }
299
300 function buildHeaders( table, msg ) {
301 var config = $( table ).data( 'tablesorter' ).config,
302 maxSeen = 0,
303 colspanOffset = 0,
304 columns,
305 k,
306 $cell,
307 rowspan,
308 colspan,
309 headerCount,
310 longestTR,
311 headerIndex,
312 exploded,
313 $tableHeaders = $( [] ),
314 $tableRows = $( 'thead:eq(0) > tr', table );
315
316 if ( $tableRows.length <= 1 ) {
317 $tableHeaders = $tableRows.children( 'th' );
318 } else {
319 exploded = [];
320
321 // Loop through all the dom cells of the thead
322 $tableRows.each( function ( rowIndex, row ) {
323 $.each( row.cells, function ( columnIndex, cell ) {
324 var matrixRowIndex,
325 matrixColumnIndex;
326
327 rowspan = Number( cell.rowSpan );
328 colspan = Number( cell.colSpan );
329
330 // Skip the spots in the exploded matrix that are already filled
331 while ( exploded[ rowIndex ] && exploded[ rowIndex ][ columnIndex ] !== undefined ) {
332 ++columnIndex;
333 }
334
335 // Find the actual dimensions of the thead, by placing each cell
336 // in the exploded matrix rowspan times colspan times, with the proper offsets
337 for ( matrixColumnIndex = columnIndex; matrixColumnIndex < columnIndex + colspan; ++matrixColumnIndex ) {
338 for ( matrixRowIndex = rowIndex; matrixRowIndex < rowIndex + rowspan; ++matrixRowIndex ) {
339 if ( !exploded[ matrixRowIndex ] ) {
340 exploded[ matrixRowIndex ] = [];
341 }
342 exploded[ matrixRowIndex ][ matrixColumnIndex ] = cell;
343 }
344 }
345 } );
346 } );
347 // We want to find the row that has the most columns (ignoring colspan)
348 exploded.forEach( function ( cellArray, index ) {
349 headerCount = $( uniqueElements( cellArray ) ).filter( 'th' ).length;
350 if ( headerCount >= maxSeen ) {
351 maxSeen = headerCount;
352 longestTR = index;
353 }
354 } );
355 // We cannot use $.unique() here because it sorts into dom order, which is undesirable
356 $tableHeaders = $( uniqueElements( exploded[ longestTR ] ) ).filter( 'th' );
357 }
358
359 // as each header can span over multiple columns (using colspan=N),
360 // we have to bidirectionally map headers to their columns and columns to their headers
361 config.columnToHeader = [];
362 config.headerToColumns = [];
363 config.headerList = [];
364 headerIndex = 0;
365 $tableHeaders.each( function () {
366 $cell = $( this );
367 columns = [];
368
369 if ( !$cell.hasClass( config.unsortableClass ) ) {
370 $cell
371 .addClass( config.cssHeader )
372 .prop( 'tabIndex', 0 )
373 .attr( {
374 role: 'columnheader button',
375 title: msg[ 1 ]
376 } );
377
378 for ( k = 0; k < this.colSpan; k++ ) {
379 config.columnToHeader[ colspanOffset + k ] = headerIndex;
380 columns.push( colspanOffset + k );
381 }
382
383 config.headerToColumns[ headerIndex ] = columns;
384
385 $cell.data( {
386 headerIndex: headerIndex,
387 order: 0,
388 count: 0
389 } );
390
391 // add only sortable cells to headerList
392 config.headerList[ headerIndex ] = this;
393 headerIndex++;
394 }
395
396 colspanOffset += this.colSpan;
397 } );
398
399 // number of columns with extended colspan, inclusive unsortable
400 // parsers[j], cache[][j], columnToHeader[j], columnToCell[j] have so many elements
401 config.columns = colspanOffset;
402
403 return $tableHeaders.not( '.' + config.unsortableClass );
404 }
405
406 function isValueInArray( v, a ) {
407 var i;
408 for ( i = 0; i < a.length; i++ ) {
409 if ( a[ i ][ 0 ] === v ) {
410 return true;
411 }
412 }
413 return false;
414 }
415
416 /**
417 * Sets the sort count of the columns that are not affected by the sorting to have them sorted
418 * in default (ascending) order when their header cell is clicked the next time.
419 *
420 * @param {jQuery} $headers
421 * @param {Array} sortList 2D number array
422 * @param {Array} headerToColumns 2D number array
423 */
424 function setHeadersOrder( $headers, sortList, headerToColumns ) {
425 // Loop through all headers to retrieve the indices of the columns the header spans across:
426 headerToColumns.forEach( function ( columns, headerIndex ) {
427
428 columns.forEach( function ( columnIndex, i ) {
429 var header = $headers[ headerIndex ],
430 $header = $( header );
431
432 if ( !isValueInArray( columnIndex, sortList ) ) {
433 // Column shall not be sorted: Reset header count and order.
434 $header.data( {
435 order: 0,
436 count: 0
437 } );
438 } else {
439 // Column shall be sorted: Apply designated count and order.
440 sortList.forEach( function ( sortColumn ) {
441 if ( sortColumn[ 0 ] === i ) {
442 $header.data( {
443 order: sortColumn[ 1 ],
444 count: sortColumn[ 1 ] + 1
445 } );
446 return false;
447 }
448 } );
449 }
450 } );
451
452 } );
453 }
454
455 function setHeadersCss( table, $headers, list, css, msg, columnToHeader ) {
456 var i, len;
457 // Remove all header information and reset titles to default message
458 $headers.removeClass( css[ 0 ] ).removeClass( css[ 1 ] ).attr( 'title', msg[ 1 ] );
459
460 for ( i = 0, len = list.length; i < len; i++ ) {
461 $headers
462 .eq( columnToHeader[ list[ i ][ 0 ] ] )
463 .addClass( css[ list[ i ][ 1 ] ] )
464 .attr( 'title', msg[ list[ i ][ 1 ] ] );
465 }
466 }
467
468 function sortText( a, b ) {
469 return ( ( a < b ) ? -1 : ( ( a > b ) ? 1 : 0 ) );
470 }
471
472 function sortTextDesc( a, b ) {
473 return ( ( b < a ) ? -1 : ( ( b > a ) ? 1 : 0 ) );
474 }
475
476 function multisort( table, sortList, cache ) {
477 var i,
478 sortFn = [];
479
480 for ( i = 0; i < sortList.length; i++ ) {
481 sortFn[ i ] = ( sortList[ i ][ 1 ] ) ? sortTextDesc : sortText;
482 }
483 cache.normalized.sort( function ( array1, array2 ) {
484 var i, col, ret;
485 for ( i = 0; i < sortList.length; i++ ) {
486 col = sortList[ i ][ 0 ];
487 ret = sortFn[ i ].call( this, array1[ col ], array2[ col ] );
488 if ( ret !== 0 ) {
489 return ret;
490 }
491 }
492 // Fall back to index number column to ensure stable sort
493 return sortText.call( this, array1[ array1.length - 1 ], array2[ array2.length - 1 ] );
494 } );
495 return cache;
496 }
497
498 function buildTransformTable() {
499 var ascii, localised, i, digitClass,
500 digits = '0123456789,.'.split( '' ),
501 separatorTransformTable = mw.config.get( 'wgSeparatorTransformTable' ),
502 digitTransformTable = mw.config.get( 'wgDigitTransformTable' );
503
504 if ( separatorTransformTable === null || ( separatorTransformTable[ 0 ] === '' && digitTransformTable[ 2 ] === '' ) ) {
505 ts.transformTable = false;
506 } else {
507 ts.transformTable = {};
508
509 // Unpack the transform table
510 ascii = separatorTransformTable[ 0 ].split( '\t' ).concat( digitTransformTable[ 0 ].split( '\t' ) );
511 localised = separatorTransformTable[ 1 ].split( '\t' ).concat( digitTransformTable[ 1 ].split( '\t' ) );
512
513 // Construct regexes for number identification
514 for ( i = 0; i < ascii.length; i++ ) {
515 ts.transformTable[ localised[ i ] ] = ascii[ i ];
516 digits.push( mw.RegExp.escape( localised[ i ] ) );
517 }
518 }
519 digitClass = '[' + digits.join( '', digits ) + ']';
520
521 // We allow a trailing percent sign, which we just strip. This works fine
522 // if percents and regular numbers aren't being mixed.
523 ts.numberRegex = new RegExp(
524 '^(' +
525 '[-+\u2212]?[0-9][0-9,]*(\\.[0-9,]*)?(E[-+\u2212]?[0-9][0-9,]*)?' + // Fortran-style scientific
526 '|' +
527 '[-+\u2212]?' + digitClass + '+[\\s\\xa0]*%?' + // Generic localised
528 ')$',
529 'i'
530 );
531 }
532
533 function buildDateTable() {
534 var i, name,
535 regex = [];
536
537 ts.monthNames = {};
538
539 for ( i = 0; i < 12; i++ ) {
540 name = mw.language.months.names[ i ].toLowerCase();
541 ts.monthNames[ name ] = i + 1;
542 regex.push( mw.RegExp.escape( name ) );
543 name = mw.language.months.genitive[ i ].toLowerCase();
544 ts.monthNames[ name ] = i + 1;
545 regex.push( mw.RegExp.escape( name ) );
546 name = mw.language.months.abbrev[ i ].toLowerCase().replace( '.', '' );
547 ts.monthNames[ name ] = i + 1;
548 regex.push( mw.RegExp.escape( name ) );
549 }
550
551 // Build piped string
552 regex = regex.join( '|' );
553
554 // Build RegEx
555 // Any date formated with . , ' - or /
556 ts.dateRegex[ 0 ] = new RegExp( /^\s*(\d{1,2})[,.\-/'\s]{1,2}(\d{1,2})[,.\-/'\s]{1,2}(\d{2,4})\s*?/i );
557
558 // Written Month name, dmy
559 ts.dateRegex[ 1 ] = new RegExp(
560 '^\\s*(\\d{1,2})[\\,\\.\\-\\/\'\\s]+(' +
561 regex +
562 ')' +
563 '[\\,\\.\\-\\/\'\\s]+(\\d{2,4})\\s*$',
564 'i'
565 );
566
567 // Written Month name, mdy
568 ts.dateRegex[ 2 ] = new RegExp(
569 '^\\s*(' + regex + ')' +
570 '[\\,\\.\\-\\/\'\\s]+(\\d{1,2})[\\,\\.\\-\\/\'\\s]+(\\d{2,4})\\s*$',
571 'i'
572 );
573
574 }
575
576 /**
577 * Replace all rowspanned cells in the body with clones in each row, so sorting
578 * need not worry about them.
579 *
580 * @param {jQuery} $table jQuery object for a <table>
581 */
582 function explodeRowspans( $table ) {
583 var spanningRealCellIndex, rowSpan, colSpan,
584 cell, cellData, i, $tds, $clone, $nextRows,
585 rowspanCells = $table.find( '> tbody > tr > [rowspan]' ).get();
586
587 // Short circuit
588 if ( !rowspanCells.length ) {
589 return;
590 }
591
592 // First, we need to make a property like cellIndex but taking into
593 // account colspans. We also cache the rowIndex to avoid having to take
594 // cell.parentNode.rowIndex in the sorting function below.
595 $table.find( '> tbody > tr' ).each( function () {
596 var i,
597 col = 0,
598 len = this.cells.length;
599 for ( i = 0; i < len; i++ ) {
600 $( this.cells[ i ] ).data( 'tablesorter', {
601 realCellIndex: col,
602 realRowIndex: this.rowIndex
603 } );
604 col += this.cells[ i ].colSpan;
605 }
606 } );
607
608 // Split multi row cells into multiple cells with the same content.
609 // Sort by column then row index to avoid problems with odd table structures.
610 // Re-sort whenever a rowspanned cell's realCellIndex is changed, because it
611 // might change the sort order.
612 function resortCells() {
613 var cellAData,
614 cellBData,
615 ret;
616 rowspanCells = rowspanCells.sort( function ( a, b ) {
617 cellAData = $.data( a, 'tablesorter' );
618 cellBData = $.data( b, 'tablesorter' );
619 ret = cellAData.realCellIndex - cellBData.realCellIndex;
620 if ( !ret ) {
621 ret = cellAData.realRowIndex - cellBData.realRowIndex;
622 }
623 return ret;
624 } );
625 rowspanCells.forEach( function ( cell ) {
626 $.data( cell, 'tablesorter' ).needResort = false;
627 } );
628 }
629 resortCells();
630
631 function filterfunc() {
632 return $.data( this, 'tablesorter' ).realCellIndex >= spanningRealCellIndex;
633 }
634
635 function fixTdCellIndex() {
636 $.data( this, 'tablesorter' ).realCellIndex += colSpan;
637 if ( this.rowSpan > 1 ) {
638 $.data( this, 'tablesorter' ).needResort = true;
639 }
640 }
641
642 while ( rowspanCells.length ) {
643 if ( $.data( rowspanCells[ 0 ], 'tablesorter' ).needResort ) {
644 resortCells();
645 }
646
647 cell = rowspanCells.shift();
648 cellData = $.data( cell, 'tablesorter' );
649 rowSpan = cell.rowSpan;
650 colSpan = cell.colSpan;
651 spanningRealCellIndex = cellData.realCellIndex;
652 cell.rowSpan = 1;
653 $nextRows = $( cell ).parent().nextAll();
654 for ( i = 0; i < rowSpan - 1; i++ ) {
655 $tds = $( $nextRows[ i ].cells ).filter( filterfunc );
656 $clone = $( cell ).clone();
657 $clone.data( 'tablesorter', {
658 realCellIndex: spanningRealCellIndex,
659 realRowIndex: cellData.realRowIndex + i,
660 needResort: true
661 } );
662 if ( $tds.length ) {
663 $tds.each( fixTdCellIndex );
664 $tds.first().before( $clone );
665 } else {
666 $nextRows.eq( i ).append( $clone );
667 }
668 }
669 }
670 }
671
672 /**
673 * Build index to handle colspanned cells in the body.
674 * Set the cell index for each column in an array,
675 * so that colspaned cells set multiple in this array.
676 * columnToCell[collumnIndex] point at the real cell in this row.
677 *
678 * @param {jQuery} $table object for a <table>
679 */
680 function manageColspans( $table ) {
681 var i, j, k, $row,
682 $rows = $table.find( '> tbody > tr' ),
683 totalRows = $rows.length || 0,
684 config = $table.data( 'tablesorter' ).config,
685 columns = config.columns,
686 columnToCell, cellsInRow, index;
687
688 for ( i = 0; i < totalRows; i++ ) {
689
690 $row = $rows.eq( i );
691 // if this is a child row, continue to the next row (as buildCache())
692 if ( $row.hasClass( config.cssChildRow ) ) {
693 // go to the next for loop
694 continue;
695 }
696
697 columnToCell = [];
698 cellsInRow = ( $row[ 0 ].cells.length ) || 0; // all cells in this row
699 index = 0; // real cell index in this row
700 for ( j = 0; j < columns; index++ ) {
701 if ( index === cellsInRow ) {
702 // Row with cells less than columns: add empty cell
703 $row.append( '<td>' );
704 cellsInRow++;
705 }
706 for ( k = 0; k < $row[ 0 ].cells[ index ].colSpan; k++ ) {
707 columnToCell[ j++ ] = index;
708 }
709 }
710 // Store it in $row
711 $row.data( 'columnToCell', columnToCell );
712 }
713 }
714
715 function buildCollationTable() {
716 var key, keys = [];
717 ts.collationTable = mw.config.get( 'tableSorterCollation' );
718 ts.collationRegex = null;
719 if ( ts.collationTable ) {
720 // Build array of key names
721 for ( key in ts.collationTable ) {
722 // Check hasOwn to be safe
723 if ( ts.collationTable.hasOwnProperty( key ) ) {
724 keys.push( mw.RegExp.escape( key ) );
725 }
726 }
727 if ( keys.length ) {
728 ts.collationRegex = new RegExp( keys.join( '|' ), 'ig' );
729 }
730 }
731 }
732
733 function cacheRegexs() {
734 if ( ts.rgx ) {
735 return;
736 }
737 ts.rgx = {
738 IPAddress: [
739 new RegExp( /^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}$/ )
740 ],
741 currency: [
742 new RegExp( /(^[£$€¥]|[£$€¥]$)/ ),
743 new RegExp( /[£$€¥]/g )
744 ],
745 url: [
746 new RegExp( /^(https?|ftp|file):\/\/$/ ),
747 new RegExp( /(https?|ftp|file):\/\// )
748 ],
749 isoDate: [
750 new RegExp( /^[^-\d]*(-?\d{1,4})-(0\d|1[0-2])(-([0-3]\d))?([T\s]([01]\d|2[0-4]):?(([0-5]\d):?(([0-5]\d|60)([.,]\d{1,3})?)?)?([zZ]|([-+])([01]\d|2[0-3]):?([0-5]\d)?)?)?/ ),
751 new RegExp( /^[^-\d]*(-?\d{1,4})-?(\d\d)?(-?(\d\d))?([T\s](\d\d):?((\d\d)?:?((\d\d)?([.,]\d{1,3})?)?)?([zZ]|([-+])(\d\d):?(\d\d)?)?)?/ )
752 ],
753 usLongDate: [
754 new RegExp( /^[A-Za-z]{3,10}\.? [0-9]{1,2}, ([0-9]{4}|'?[0-9]{2}) (([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(AM|PM)))$/ )
755 ],
756 time: [
757 new RegExp( /^(([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(am|pm)))$/ )
758 ]
759 };
760 }
761
762 /**
763 * Converts sort objects [ { Integer: String }, ... ] to the internally used nested array
764 * structure [ [ Integer, Integer ], ... ]
765 *
766 * @param {Array} sortObjects List of sort objects.
767 * @return {Array} List of internal sort definitions.
768 */
769 function convertSortList( sortObjects ) {
770 var sortList = [];
771 sortObjects.forEach( function ( sortObject ) {
772 $.each( sortObject, function ( columnIndex, order ) {
773 var orderIndex = ( order === 'desc' ) ? 1 : 0;
774 sortList.push( [ parseInt( columnIndex, 10 ), orderIndex ] );
775 } );
776 } );
777 return sortList;
778 }
779
780 /* Public scope */
781
782 $.tablesorter = {
783 defaultOptions: {
784 cssHeader: 'headerSort',
785 cssAsc: 'headerSortUp',
786 cssDesc: 'headerSortDown',
787 cssChildRow: 'expand-child',
788 sortMultiSortKey: 'shiftKey',
789 unsortableClass: 'unsortable',
790 parsers: [],
791 cancelSelection: true,
792 sortList: [],
793 headerList: [],
794 headerToColumns: [],
795 columnToHeader: [],
796 columns: 0
797 },
798
799 dateRegex: [],
800 monthNames: {},
801
802 /**
803 * @param {jQuery} $tables
804 * @param {Object} [settings]
805 * @return {jQuery}
806 */
807 construct: function ( $tables, settings ) {
808 return $tables.each( function ( i, table ) {
809 // Declare and cache.
810 var $headers, cache, config, sortCSS, sortMsg,
811 $table = $( table ),
812 firstTime = true;
813
814 // Quit if no tbody
815 if ( !table.tBodies ) {
816 return;
817 }
818 if ( !table.tHead ) {
819 // No thead found. Look for rows with <th>s and
820 // move them into a <thead> tag or a <tfoot> tag
821 emulateTHeadAndFoot( $table );
822
823 // Still no thead? Then quit
824 if ( !table.tHead ) {
825 return;
826 }
827 }
828 $table.addClass( 'jquery-tablesorter' );
829
830 // Merge and extend
831 config = $.extend( {}, $.tablesorter.defaultOptions, settings );
832
833 // Save the settings where they read
834 $.data( table, 'tablesorter', { config: config } );
835
836 // Get the CSS class names, could be done elsewhere
837 sortCSS = [ config.cssAsc, config.cssDesc ];
838 // Messages tell the the user what the *next* state will be
839 // so are in reverse order to the CSS classes.
840 sortMsg = [ mw.msg( 'sort-descending' ), mw.msg( 'sort-ascending' ) ];
841
842 // Build headers
843 $headers = buildHeaders( table, sortMsg );
844
845 // Grab and process locale settings.
846 buildTransformTable();
847 buildDateTable();
848
849 // Precaching regexps can bring 10 fold
850 // performance improvements in some browsers.
851 cacheRegexs();
852
853 function setupForFirstSort() {
854 var $tfoot, $sortbottoms;
855
856 firstTime = false;
857
858 // Defer buildCollationTable to first sort. As user and site scripts
859 // may customize tableSorterCollation but load after $.ready(), other
860 // scripts may call .tablesorter() before they have done the
861 // tableSorterCollation customizations.
862 buildCollationTable();
863
864 // Legacy fix of .sortbottoms
865 // Wrap them inside a tfoot (because that's what they actually want to be)
866 // and put the <tfoot> at the end of the <table>
867 $sortbottoms = $table.find( '> tbody > tr.sortbottom' );
868 if ( $sortbottoms.length ) {
869 $tfoot = $table.children( 'tfoot' );
870 if ( $tfoot.length ) {
871 $tfoot.eq( 0 ).prepend( $sortbottoms );
872 } else {
873 $table.append( $( '<tfoot>' ).append( $sortbottoms ) );
874 }
875 }
876
877 explodeRowspans( $table );
878 manageColspans( $table );
879
880 // Try to auto detect column type, and store in tables config
881 config.parsers = buildParserCache( table, $headers );
882 }
883
884 // Apply event handling to headers
885 // this is too big, perhaps break it out?
886 $headers.on( 'keypress click', function ( e ) {
887 var cell, $cell, columns, newSortList, i,
888 totalRows,
889 j, s, o;
890
891 if ( e.type === 'click' && e.target.nodeName.toLowerCase() === 'a' ) {
892 // The user clicked on a link inside a table header.
893 // Do nothing and let the default link click action continue.
894 return true;
895 }
896
897 if ( e.type === 'keypress' && e.which !== 13 ) {
898 // Only handle keypresses on the "Enter" key.
899 return true;
900 }
901
902 if ( firstTime ) {
903 setupForFirstSort();
904 }
905
906 // Build the cache for the tbody cells
907 // to share between calculations for this sort action.
908 // Re-calculated each time a sort action is performed due to possiblity
909 // that sort values change. Shouldn't be too expensive, but if it becomes
910 // too slow an event based system should be implemented somehow where
911 // cells get event .change() and bubbles up to the <table> here
912 cache = buildCache( table );
913
914 totalRows = ( $table[ 0 ].tBodies[ 0 ] && $table[ 0 ].tBodies[ 0 ].rows.length ) || 0;
915 if ( totalRows > 0 ) {
916 cell = this;
917 $cell = $( cell );
918
919 // Get current column sort order
920 $cell.data( {
921 order: $cell.data( 'count' ) % 2,
922 count: $cell.data( 'count' ) + 1
923 } );
924
925 cell = this;
926 // Get current column index
927 columns = config.headerToColumns[ $cell.data( 'headerIndex' ) ];
928 newSortList = columns.map( function ( c ) {
929 return [ c, $cell.data( 'order' ) ];
930 } );
931 // Index of first column belonging to this header
932 i = columns[ 0 ];
933
934 if ( !e[ config.sortMultiSortKey ] ) {
935 // User only wants to sort on one column set
936 // Flush the sort list and add new columns
937 config.sortList = newSortList;
938 } else {
939 // Multi column sorting
940 // It is not possible for one column to belong to multiple headers,
941 // so this is okay - we don't need to check for every value in the columns array
942 if ( isValueInArray( i, config.sortList ) ) {
943 // The user has clicked on an already sorted column.
944 // Reverse the sorting direction for all tables.
945 for ( j = 0; j < config.sortList.length; j++ ) {
946 s = config.sortList[ j ];
947 o = config.headerList[ config.columnToHeader[ s[ 0 ] ] ];
948 if ( isValueInArray( s[ 0 ], newSortList ) ) {
949 $( o ).data( 'count', s[ 1 ] + 1 );
950 s[ 1 ] = $( o ).data( 'count' ) % 2;
951 }
952 }
953 } else {
954 // Add columns to sort list array
955 config.sortList = config.sortList.concat( newSortList );
956 }
957 }
958
959 // Reset order/counts of cells not affected by sorting
960 setHeadersOrder( $headers, config.sortList, config.headerToColumns );
961
962 // Set CSS for headers
963 setHeadersCss( $table[ 0 ], $headers, config.sortList, sortCSS, sortMsg, config.columnToHeader );
964 appendToTable(
965 $table[ 0 ], multisort( $table[ 0 ], config.sortList, cache )
966 );
967
968 // Stop normal event by returning false
969 return false;
970 }
971
972 // Cancel selection
973 } ).mousedown( function () {
974 if ( config.cancelSelection ) {
975 this.onselectstart = function () {
976 return false;
977 };
978 return false;
979 }
980 } );
981
982 /**
983 * Sorts the table. If no sorting is specified by passing a list of sort
984 * objects, the table is sorted according to the initial sorting order.
985 * Passing an empty array will reset sorting (basically just reset the headers
986 * making the table appear unsorted).
987 *
988 * @param {Array} [sortList] List of sort objects.
989 */
990 $table.data( 'tablesorter' ).sort = function ( sortList ) {
991
992 if ( firstTime ) {
993 setupForFirstSort();
994 }
995
996 if ( sortList === undefined ) {
997 sortList = config.sortList;
998 } else if ( sortList.length > 0 ) {
999 sortList = convertSortList( sortList );
1000 }
1001
1002 // Set each column's sort count to be able to determine the correct sort
1003 // order when clicking on a header cell the next time
1004 setHeadersOrder( $headers, sortList, config.headerToColumns );
1005
1006 // re-build the cache for the tbody cells
1007 cache = buildCache( table );
1008
1009 // set css for headers
1010 setHeadersCss( table, $headers, sortList, sortCSS, sortMsg, config.columnToHeader );
1011
1012 // sort the table and append it to the dom
1013 appendToTable( table, multisort( table, sortList, cache ) );
1014 };
1015
1016 // sort initially
1017 if ( config.sortList.length > 0 ) {
1018 config.sortList = convertSortList( config.sortList );
1019 $table.data( 'tablesorter' ).sort();
1020 }
1021
1022 } );
1023 },
1024
1025 addParser: function ( parser ) {
1026 if ( !getParserById( parser.id ) ) {
1027 parsers.push( parser );
1028 }
1029 },
1030
1031 formatDigit: function ( s ) {
1032 var out, c, p, i;
1033 if ( ts.transformTable !== false ) {
1034 out = '';
1035 for ( p = 0; p < s.length; p++ ) {
1036 c = s.charAt( p );
1037 if ( c in ts.transformTable ) {
1038 out += ts.transformTable[ c ];
1039 } else {
1040 out += c;
1041 }
1042 }
1043 s = out;
1044 }
1045 i = parseFloat( s.replace( /[, ]/g, '' ).replace( '\u2212', '-' ) );
1046 return isNaN( i ) ? -Infinity : i;
1047 },
1048
1049 formatFloat: function ( s ) {
1050 var i = parseFloat( s );
1051 return isNaN( i ) ? -Infinity : i;
1052 },
1053
1054 formatInt: function ( s ) {
1055 var i = parseInt( s, 10 );
1056 return isNaN( i ) ? -Infinity : i;
1057 },
1058
1059 clearTableBody: function ( table ) {
1060 $( table.tBodies[ 0 ] ).empty();
1061 },
1062
1063 getParser: function ( id ) {
1064 buildTransformTable();
1065 buildDateTable();
1066 cacheRegexs();
1067 buildCollationTable();
1068
1069 return getParserById( id );
1070 },
1071
1072 getParsers: function () { // for table diagnosis
1073 return parsers;
1074 }
1075 };
1076
1077 // Shortcut
1078 ts = $.tablesorter;
1079
1080 // Register as jQuery prototype method
1081 $.fn.tablesorter = function ( settings ) {
1082 return ts.construct( this, settings );
1083 };
1084
1085 // Add default parsers
1086 ts.addParser( {
1087 id: 'text',
1088 is: function () {
1089 return true;
1090 },
1091 format: function ( s ) {
1092 var tsc;
1093 s = s.toLowerCase().trim();
1094 if ( ts.collationRegex ) {
1095 tsc = ts.collationTable;
1096 s = s.replace( ts.collationRegex, function ( match ) {
1097 var r = tsc[ match ] ? tsc[ match ] : tsc[ match.toUpperCase() ];
1098 return r.toLowerCase();
1099 } );
1100 }
1101 return s;
1102 },
1103 type: 'text'
1104 } );
1105
1106 ts.addParser( {
1107 id: 'IPAddress',
1108 is: function ( s ) {
1109 return ts.rgx.IPAddress[ 0 ].test( s );
1110 },
1111 format: function ( s ) {
1112 var i, item,
1113 a = s.split( '.' ),
1114 r = '';
1115 for ( i = 0; i < a.length; i++ ) {
1116 item = a[ i ];
1117 if ( item.length === 1 ) {
1118 r += '00' + item;
1119 } else if ( item.length === 2 ) {
1120 r += '0' + item;
1121 } else {
1122 r += item;
1123 }
1124 }
1125 return $.tablesorter.formatFloat( r );
1126 },
1127 type: 'numeric'
1128 } );
1129
1130 ts.addParser( {
1131 id: 'currency',
1132 is: function ( s ) {
1133 return ts.rgx.currency[ 0 ].test( s );
1134 },
1135 format: function ( s ) {
1136 return $.tablesorter.formatDigit( s.replace( ts.rgx.currency[ 1 ], '' ) );
1137 },
1138 type: 'numeric'
1139 } );
1140
1141 ts.addParser( {
1142 id: 'url',
1143 is: function ( s ) {
1144 return ts.rgx.url[ 0 ].test( s );
1145 },
1146 format: function ( s ) {
1147 return s.replace( ts.rgx.url[ 1 ], '' ).trim();
1148 },
1149 type: 'text'
1150 } );
1151
1152 ts.addParser( {
1153 id: 'isoDate',
1154 is: function ( s ) {
1155 return ts.rgx.isoDate[ 0 ].test( s );
1156 },
1157 format: function ( s ) {
1158 var match, i, isodate, ms, hOffset, mOffset;
1159 match = s.match( ts.rgx.isoDate[ 0 ] );
1160 if ( match === null ) {
1161 // Otherwise a signed number with 1-4 digit is parsed as isoDate
1162 match = s.match( ts.rgx.isoDate[ 1 ] );
1163 }
1164 if ( !match ) {
1165 return -Infinity;
1166 }
1167 // Month and day
1168 for ( i = 2; i <= 4; i += 2 ) {
1169 if ( !match[ i ] || match[ i ].length === 0 ) {
1170 match[ i ] = 1;
1171 }
1172 }
1173 // Time
1174 for ( i = 6; i <= 15; i++ ) {
1175 if ( !match[ i ] || match[ i ].length === 0 ) {
1176 match[ i ] = '0';
1177 }
1178 }
1179 ms = parseFloat( match[ 11 ].replace( /,/, '.' ) ) * 1000;
1180 hOffset = $.tablesorter.formatInt( match[ 13 ] + match[ 14 ] );
1181 mOffset = $.tablesorter.formatInt( match[ 13 ] + match[ 15 ] );
1182
1183 isodate = new Date( 0 );
1184 // Because Date constructor changes year 0-99 to 1900-1999, use setUTCFullYear()
1185 isodate.setUTCFullYear( match[ 1 ], match[ 2 ] - 1, match[ 4 ] );
1186 isodate.setUTCHours( match[ 6 ] - hOffset, match[ 8 ] - mOffset, match[ 10 ], ms );
1187 return isodate.getTime();
1188 },
1189 type: 'numeric'
1190 } );
1191
1192 ts.addParser( {
1193 id: 'usLongDate',
1194 is: function ( s ) {
1195 return ts.rgx.usLongDate[ 0 ].test( s );
1196 },
1197 format: function ( s ) {
1198 return $.tablesorter.formatFloat( new Date( s ).getTime() );
1199 },
1200 type: 'numeric'
1201 } );
1202
1203 ts.addParser( {
1204 id: 'date',
1205 is: function ( s ) {
1206 return ( ts.dateRegex[ 0 ].test( s ) || ts.dateRegex[ 1 ].test( s ) || ts.dateRegex[ 2 ].test( s ) );
1207 },
1208 format: function ( s ) {
1209 var match, y;
1210 s = s.toLowerCase().trim();
1211
1212 if ( ( match = s.match( ts.dateRegex[ 0 ] ) ) !== null ) {
1213 if ( mw.config.get( 'wgDefaultDateFormat' ) === 'mdy' || mw.config.get( 'wgPageContentLanguage' ) === 'en' ) {
1214 s = [ match[ 3 ], match[ 1 ], match[ 2 ] ];
1215 } else if ( mw.config.get( 'wgDefaultDateFormat' ) === 'dmy' ) {
1216 s = [ match[ 3 ], match[ 2 ], match[ 1 ] ];
1217 } else {
1218 // If we get here, we don't know which order the dd-dd-dddd
1219 // date is in. So return something not entirely invalid.
1220 return '99999999';
1221 }
1222 } else if ( ( match = s.match( ts.dateRegex[ 1 ] ) ) !== null ) {
1223 s = [ match[ 3 ], String( ts.monthNames[ match[ 2 ] ] ), match[ 1 ] ];
1224 } else if ( ( match = s.match( ts.dateRegex[ 2 ] ) ) !== null ) {
1225 s = [ match[ 3 ], String( ts.monthNames[ match[ 1 ] ] ), match[ 2 ] ];
1226 } else {
1227 // Should never get here
1228 return '99999999';
1229 }
1230
1231 // Pad Month and Day
1232 if ( s[ 1 ].length === 1 ) {
1233 s[ 1 ] = '0' + s[ 1 ];
1234 }
1235 if ( s[ 2 ].length === 1 ) {
1236 s[ 2 ] = '0' + s[ 2 ];
1237 }
1238
1239 if ( ( y = parseInt( s[ 0 ], 10 ) ) < 100 ) {
1240 // Guestimate years without centuries
1241 if ( y < 30 ) {
1242 s[ 0 ] = 2000 + y;
1243 } else {
1244 s[ 0 ] = 1900 + y;
1245 }
1246 }
1247 while ( s[ 0 ].length < 4 ) {
1248 s[ 0 ] = '0' + s[ 0 ];
1249 }
1250 return parseInt( s.join( '' ), 10 );
1251 },
1252 type: 'numeric'
1253 } );
1254
1255 ts.addParser( {
1256 id: 'time',
1257 is: function ( s ) {
1258 return ts.rgx.time[ 0 ].test( s );
1259 },
1260 format: function ( s ) {
1261 return $.tablesorter.formatFloat( new Date( '2000/01/01 ' + s ).getTime() );
1262 },
1263 type: 'numeric'
1264 } );
1265
1266 ts.addParser( {
1267 id: 'number',
1268 is: function ( s ) {
1269 return $.tablesorter.numberRegex.test( s.trim() );
1270 },
1271 format: function ( s ) {
1272 return $.tablesorter.formatDigit( s );
1273 },
1274 type: 'numeric'
1275 } );
1276
1277 }( jQuery, mediaWiki ) );