2 * TableSorter for MediaWiki
4 * Written 2011 Leo Koppelkamm
5 * Based on tablesorter.com plugin, written (c) 2007 Christian Bach.
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
11 * Depends on mw.config (wgDigitTransformTable, wgDefaultDateFormat, wgPageContentLanguage)
12 * and mw.language.months.
14 * Uses 'tableSorterCollation' in mw.config (if available)
16 * Create a sortable table with multi-column sorting capabilities
18 * // Create a simple tablesorter interface
19 * $( 'table' ).tablesorter();
21 * // Create a tablesorter interface, initially sorting on the first and second column
22 * $( 'table' ).tablesorter( { sortList: [ { 0: 'desc' }, { 1: 'asc' } ] } );
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.
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.
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.
33 * @param {string} [sortMultisortKey="shiftKey"] A string of the multi-column sort key.
35 * @param {boolean} [cancelSelection=true] Boolean flag indicating iftablesorter should cancel
36 * selection of the table headers text.
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'> }
42 * @event sortEnd.tablesorter: Triggered as soon as any sorting has been applied.
44 * @author Christian Bach/christian.bach@polyester.se
46 ( function ( $, mw ) {
50 /* Parser utility functions */
52 function getParserById( name ) {
54 for ( i = 0; i < parsers.length; i++ ) {
55 if ( parsers[ i ].id.toLowerCase() === name.toLowerCase() ) {
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' );
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 );
77 if ( node.tagName.toLowerCase() === 'img' ) {
78 return $node.attr( 'alt' ) || ''; // handle undefined alt
80 return $.map( $.makeArray( node.childNodes ), function ( elem ) {
81 if ( elem.nodeType === Node.ELEMENT_NODE ) {
82 return getElementSortKey( elem );
84 return $.text( elem );
88 function detectParserForColumn( table, rows, column ) {
89 var l = parsers.length,
90 config = $( table ).data( 'tablesorter' ).config,
94 // Start with 1 because 0 is the fallback parser
100 needed = ( rows.length > 4 ) ? 5 : rows.length;
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 = $.trim( getElementSortKey( rows[ rowIndex ].cells[ cellIndex ] ) );
114 if ( nodeValue !== '' ) {
115 if ( parsers[ i ].is( nodeValue, table ) ) {
118 if ( concurrent >= needed ) {
119 // Confirmed the parser for multiple cells, let's return it
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
127 // Check next parser, reset rows
143 if ( rowIndex >= rows.length ) {
144 if ( concurrent > 0 && concurrent >= rows.length - empty ) {
145 // Confirmed the parser for all filled cells
148 // Check next parser, reset rows
157 // 0 is always the generic parser (text)
161 function buildParserCache( table, $headers ) {
162 var sortType, len, j, parser,
163 rows = table.tBodies[ 0 ].rows,
164 config = $( table ).data( 'tablesorter' ).config,
168 len = config.columns;
169 for ( j = 0; j < len; j++ ) {
171 sortType = $headers.eq( config.columnToHeader[ j ] ).data( 'sortType' );
172 if ( sortType !== undefined ) {
173 parser = getParserById( sortType );
176 if ( parser === false ) {
177 parser = detectParserForColumn( table, rows, j );
180 parsers.push( parser );
186 /* Other utility functions */
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,
200 for ( i = 0; i < totalRows; i++ ) {
202 // Add the table data to main data array
203 $row = $( table.tBodies[ 0 ].rows[ i ] );
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
214 cache.row.push( $row );
216 for ( j = 0; j < len; j++ ) {
217 cellIndex = $row.data( 'columnToCell' )[ j ];
218 cols.push( parsers[ j ].format( getElementSortKey( $row[ 0 ].cells[ cellIndex ] ) ) );
221 cols.push( cache.normalized.length ); // add position for rowCache
222 cache.normalized.push( cols );
229 function appendToTable( table, cache ) {
232 normalized = cache.normalized,
233 totalRows = normalized.length,
234 checkCell = ( normalized[ 0 ].length - 1 ),
235 fragment = document.createDocumentFragment();
237 for ( i = 0; i < totalRows; i++ ) {
238 pos = normalized[ i ][ checkCell ];
240 l = row[ pos ].length;
241 for ( j = 0; j < l; j++ ) {
242 fragment.appendChild( row[ pos ][ j ] );
246 table.tBodies[ 0 ].appendChild( fragment );
248 $( table ).trigger( 'sortEnd.tablesorter' );
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.
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.
260 * @param {jQuery} $table object for a <table>
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
273 $thead.append( this );
275 $table.find( ' > tbody:first' ).before( $thead );
277 if ( !$table.get( 0 ).tFoot ) {
278 $tfoot = $( '<tfoot>' );
280 for ( i = len - 1; i >= 0; i-- ) {
281 if ( $( $rows[ i ] ).children( 'td' ).length ) {
284 $tfoot.prepend( $( $rows[ i ] ) );
286 $table.append( $tfoot );
290 function uniqueElements( array ) {
292 $.each( array, function ( i, elem ) {
293 if ( elem !== undefined && $.inArray( elem, uniques ) === -1 ) {
294 uniques.push( elem );
300 function buildHeaders( table, msg ) {
301 var config = $( table ).data( 'tablesorter' ).config,
313 $tableHeaders = $( [] ),
314 $tableRows = $( 'thead:eq(0) > tr', table );
316 if ( $tableRows.length <= 1 ) {
317 $tableHeaders = $tableRows.children( 'th' );
321 // Loop through all the dom cells of the thead
322 $tableRows.each( function ( rowIndex, row ) {
323 $.each( row.cells, function ( columnIndex, cell ) {
327 rowspan = Number( cell.rowSpan );
328 colspan = Number( cell.colSpan );
330 // Skip the spots in the exploded matrix that are already filled
331 while ( exploded[ rowIndex ] && exploded[ rowIndex ][ columnIndex ] !== undefined ) {
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 ] = [];
342 exploded[ matrixRowIndex ][ matrixColumnIndex ] = cell;
347 // We want to find the row that has the most columns (ignoring colspan)
348 $.each( exploded, function ( index, cellArray ) {
349 headerCount = $( uniqueElements( cellArray ) ).filter( 'th' ).length;
350 if ( headerCount >= maxSeen ) {
351 maxSeen = headerCount;
355 // We cannot use $.unique() here because it sorts into dom order, which is undesirable
356 $tableHeaders = $( uniqueElements( exploded[ longestTR ] ) ).filter( 'th' );
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 = [];
365 $tableHeaders.each( function () {
369 if ( !$cell.hasClass( config.unsortableClass ) ) {
371 .addClass( config.cssHeader )
372 .prop( 'tabIndex', 0 )
374 role: 'columnheader button',
378 for ( k = 0; k < this.colSpan; k++ ) {
379 config.columnToHeader[ colspanOffset + k ] = headerIndex;
380 columns.push( colspanOffset + k );
383 config.headerToColumns[ headerIndex ] = columns;
386 headerIndex: headerIndex,
391 // add only sortable cells to headerList
392 config.headerList[ headerIndex ] = this;
396 colspanOffset += this.colSpan;
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;
403 return $tableHeaders.not( '.' + config.unsortableClass );
406 function isValueInArray( v, a ) {
408 for ( i = 0; i < a.length; i++ ) {
409 if ( a[ i ][ 0 ] === v ) {
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.
420 * @param {jQuery} $headers
421 * @param {Array} sortList 2D number array
422 * @param {Array} headerToColumns 2D number array
424 function setHeadersOrder( $headers, sortList, headerToColumns ) {
425 // Loop through all headers to retrieve the indices of the columns the header spans across:
426 $.each( headerToColumns, function ( headerIndex, columns ) {
428 $.each( columns, function ( i, columnIndex ) {
429 var header = $headers[ headerIndex ],
430 $header = $( header );
432 if ( !isValueInArray( columnIndex, sortList ) ) {
433 // Column shall not be sorted: Reset header count and order.
439 // Column shall be sorted: Apply designated count and order.
440 $.each( sortList, function ( j, sortColumn ) {
441 if ( sortColumn[ 0 ] === i ) {
443 order: sortColumn[ 1 ],
444 count: sortColumn[ 1 ] + 1
455 function setHeadersCss( table, $headers, list, css, msg, columnToHeader ) {
457 // Remove all header information and reset titles to default message
458 $headers.removeClass( css[ 0 ] ).removeClass( css[ 1 ] ).attr( 'title', msg[ 1 ] );
460 for ( i = 0, len = list.length; i < len; i++ ) {
462 .eq( columnToHeader[ list[ i ][ 0 ] ] )
463 .addClass( css[ list[ i ][ 1 ] ] )
464 .attr( 'title', msg[ list[ i ][ 1 ] ] );
468 function sortText( a, b ) {
469 return ( ( a < b ) ? -1 : ( ( a > b ) ? 1 : 0 ) );
472 function sortTextDesc( a, b ) {
473 return ( ( b < a ) ? -1 : ( ( b > a ) ? 1 : 0 ) );
476 function multisort( table, sortList, cache ) {
480 for ( i = 0; i < sortList.length; i++ ) {
481 sortFn[ i ] = ( sortList[ i ][ 1 ] ) ? sortTextDesc : sortText;
483 cache.normalized.sort( function ( array1, array2 ) {
485 for ( i = 0; i < sortList.length; i++ ) {
486 col = sortList[ i ][ 0 ];
487 ret = sortFn[ i ].call( this, array1[ col ], array2[ col ] );
492 // Fall back to index number column to ensure stable sort
493 return sortText.call( this, array1[ array1.length - 1 ], array2[ array2.length - 1 ] );
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' );
504 if ( separatorTransformTable === null || ( separatorTransformTable[ 0 ] === '' && digitTransformTable[ 2 ] === '' ) ) {
505 ts.transformTable = false;
507 ts.transformTable = {};
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' ) );
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 ] ) );
519 digitClass = '[' + digits.join( '', digits ) + ']';
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(
525 '[-+\u2212]?[0-9][0-9,]*(\\.[0-9,]*)?(E[-+\u2212]?[0-9][0-9,]*)?' + // Fortran-style scientific
527 '[-+\u2212]?' + digitClass + '+[\\s\\xa0]*%?' + // Generic localised
533 function buildDateTable() {
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 ) );
551 // Build piped string
552 regex = regex.join( '|' );
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 );
558 // Written Month name, dmy
559 ts.dateRegex[ 1 ] = new RegExp(
560 '^\\s*(\\d{1,2})[\\,\\.\\-\\/\'\\s]+(' +
563 '[\\,\\.\\-\\/\'\\s]+(\\d{2,4})\\s*$',
567 // Written Month name, mdy
568 ts.dateRegex[ 2 ] = new RegExp(
569 '^\\s*(' + regex + ')' +
570 '[\\,\\.\\-\\/\'\\s]+(\\d{1,2})[\\,\\.\\-\\/\'\\s]+(\\d{2,4})\\s*$',
577 * Replace all rowspanned cells in the body with clones in each row, so sorting
578 * need not worry about them.
580 * @param {jQuery} $table jQuery object for a <table>
582 function explodeRowspans( $table ) {
583 var spanningRealCellIndex, rowSpan, colSpan,
584 cell, cellData, i, $tds, $clone, $nextRows,
585 rowspanCells = $table.find( '> tbody > tr > [rowspan]' ).get();
588 if ( !rowspanCells.length ) {
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 () {
598 len = this.cells.length;
599 for ( i = 0; i < len; i++ ) {
600 $( this.cells[ i ] ).data( 'tablesorter', {
602 realRowIndex: this.rowIndex
604 col += this.cells[ i ].colSpan;
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() {
616 rowspanCells = rowspanCells.sort( function ( a, b ) {
617 cellAData = $.data( a, 'tablesorter' );
618 cellBData = $.data( b, 'tablesorter' );
619 ret = cellAData.realCellIndex - cellBData.realCellIndex;
621 ret = cellAData.realRowIndex - cellBData.realRowIndex;
625 $.each( rowspanCells, function () {
626 $.data( this, 'tablesorter' ).needResort = false;
631 function filterfunc() {
632 return $.data( this, 'tablesorter' ).realCellIndex >= spanningRealCellIndex;
635 function fixTdCellIndex() {
636 $.data( this, 'tablesorter' ).realCellIndex += colSpan;
637 if ( this.rowSpan > 1 ) {
638 $.data( this, 'tablesorter' ).needResort = true;
642 while ( rowspanCells.length ) {
643 if ( $.data( rowspanCells[ 0 ], 'tablesorter' ).needResort ) {
647 cell = rowspanCells.shift();
648 cellData = $.data( cell, 'tablesorter' );
649 rowSpan = cell.rowSpan;
650 colSpan = cell.colSpan;
651 spanningRealCellIndex = cellData.realCellIndex;
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,
663 $tds.each( fixTdCellIndex );
664 $tds.first().before( $clone );
666 $nextRows.eq( i ).append( $clone );
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.
678 * @param {jQuery} $table object for a <table>
680 function manageColspans( $table ) {
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;
688 for ( i = 0; i < totalRows; i++ ) {
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
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>' );
706 for ( k = 0; k < $row[ 0 ].cells[ index ].colSpan; k++ ) {
707 columnToCell[ j++ ] = index;
711 $row.data( 'columnToCell', columnToCell );
715 function buildCollationTable() {
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 ) );
728 ts.collationRegex = new RegExp( keys.join( '|' ), 'ig' );
733 function cacheRegexs() {
739 new RegExp( /^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}$/ )
742 new RegExp( /(^[£$€¥]|[£$€¥]$)/ ),
743 new RegExp( /[£$€¥]/g )
746 new RegExp( /^(https?|ftp|file):\/\/$/ ),
747 new RegExp( /(https?|ftp|file):\/\// )
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)?)?)?/ )
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)))$/ )
757 new RegExp( /^(([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(am|pm)))$/ )
763 * Converts sort objects [ { Integer: String }, ... ] to the internally used nested array
764 * structure [ [ Integer , Integer ], ... ]
766 * @param {Array} sortObjects List of sort objects.
767 * @return {Array} List of internal sort definitions.
769 function convertSortList( sortObjects ) {
771 $.each( sortObjects, function ( i, sortObject ) {
772 $.each( sortObject, function ( columnIndex, order ) {
773 var orderIndex = ( order === 'desc' ) ? 1 : 0;
774 sortList.push( [ parseInt( columnIndex, 10 ), orderIndex ] );
784 cssHeader: 'headerSort',
785 cssAsc: 'headerSortUp',
786 cssDesc: 'headerSortDown',
787 cssChildRow: 'expand-child',
788 sortMultiSortKey: 'shiftKey',
789 unsortableClass: 'unsortable',
791 cancelSelection: true,
803 * @param {jQuery} $tables
804 * @param {Object} [settings]
807 construct: function ( $tables, settings ) {
808 return $tables.each( function ( i, table ) {
809 // Declare and cache.
810 var $headers, cache, config, sortCSS, sortMsg,
815 if ( !table.tBodies ) {
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 );
823 // Still no thead? Then quit
824 if ( !table.tHead ) {
828 $table.addClass( 'jquery-tablesorter' );
831 config = $.extend( {}, $.tablesorter.defaultOptions, settings );
833 // Save the settings where they read
834 $.data( table, 'tablesorter', { config: config } );
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' ) ];
843 $headers = buildHeaders( table, sortMsg );
845 // Grab and process locale settings.
846 buildTransformTable();
849 // Precaching regexps can bring 10 fold
850 // performance improvements in some browsers.
853 function setupForFirstSort() {
854 var $tfoot, $sortbottoms;
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();
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 );
873 $table.append( $( '<tfoot>' ).append( $sortbottoms ) );
877 explodeRowspans( $table );
878 manageColspans( $table );
880 // Try to auto detect column type, and store in tables config
881 config.parsers = buildParserCache( table, $headers );
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,
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.
897 if ( e.type === 'keypress' && e.which !== 13 ) {
898 // Only handle keypresses on the "Enter" key.
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 );
914 totalRows = ( $table[ 0 ].tBodies[ 0 ] && $table[ 0 ].tBodies[ 0 ].rows.length ) || 0;
915 if ( totalRows > 0 ) {
919 // Get current column sort order
921 order: $cell.data( 'count' ) % 2,
922 count: $cell.data( 'count' ) + 1
926 // Get current column index
927 columns = config.headerToColumns[ $cell.data( 'headerIndex' ) ];
928 newSortList = $.map( columns, function ( c ) {
929 // jQuery "helpfully" flattens the arrays...
930 return [ [ c, $cell.data( 'order' ) ] ];
932 // Index of first column belonging to this header
935 if ( !e[ config.sortMultiSortKey ] ) {
936 // User only wants to sort on one column set
937 // Flush the sort list and add new columns
938 config.sortList = newSortList;
940 // Multi column sorting
941 // It is not possible for one column to belong to multiple headers,
942 // so this is okay - we don't need to check for every value in the columns array
943 if ( isValueInArray( i, config.sortList ) ) {
944 // The user has clicked on an already sorted column.
945 // Reverse the sorting direction for all tables.
946 for ( j = 0; j < config.sortList.length; j++ ) {
947 s = config.sortList[ j ];
948 o = config.headerList[ config.columnToHeader[ s[ 0 ] ] ];
949 if ( isValueInArray( s[ 0 ], newSortList ) ) {
950 $( o ).data( 'count', s[ 1 ] + 1 );
951 s[ 1 ] = $( o ).data( 'count' ) % 2;
955 // Add columns to sort list array
956 config.sortList = config.sortList.concat( newSortList );
960 // Reset order/counts of cells not affected by sorting
961 setHeadersOrder( $headers, config.sortList, config.headerToColumns );
963 // Set CSS for headers
964 setHeadersCss( $table[ 0 ], $headers, config.sortList, sortCSS, sortMsg, config.columnToHeader );
966 $table[ 0 ], multisort( $table[ 0 ], config.sortList, cache )
969 // Stop normal event by returning false
974 } ).mousedown( function () {
975 if ( config.cancelSelection ) {
976 this.onselectstart = function () {
984 * Sorts the table. If no sorting is specified by passing a list of sort
985 * objects, the table is sorted according to the initial sorting order.
986 * Passing an empty array will reset sorting (basically just reset the headers
987 * making the table appear unsorted).
989 * @param {Array} [sortList] List of sort objects.
991 $table.data( 'tablesorter' ).sort = function ( sortList ) {
997 if ( sortList === undefined ) {
998 sortList = config.sortList;
999 } else if ( sortList.length > 0 ) {
1000 sortList = convertSortList( sortList );
1003 // Set each column's sort count to be able to determine the correct sort
1004 // order when clicking on a header cell the next time
1005 setHeadersOrder( $headers, sortList, config.headerToColumns );
1007 // re-build the cache for the tbody cells
1008 cache = buildCache( table );
1010 // set css for headers
1011 setHeadersCss( table, $headers, sortList, sortCSS, sortMsg, config.columnToHeader );
1013 // sort the table and append it to the dom
1014 appendToTable( table, multisort( table, sortList, cache ) );
1018 if ( config.sortList.length > 0 ) {
1019 config.sortList = convertSortList( config.sortList );
1020 $table.data( 'tablesorter' ).sort();
1026 addParser: function ( parser ) {
1027 if ( !getParserById( parser.id ) ) {
1028 parsers.push( parser );
1032 formatDigit: function ( s ) {
1034 if ( ts.transformTable !== false ) {
1036 for ( p = 0; p < s.length; p++ ) {
1038 if ( c in ts.transformTable ) {
1039 out += ts.transformTable[ c ];
1046 i = parseFloat( s.replace( /[, ]/g, '' ).replace( '\u2212', '-' ) );
1047 return isNaN( i ) ? -Infinity : i;
1050 formatFloat: function ( s ) {
1051 var i = parseFloat( s );
1052 return isNaN( i ) ? -Infinity : i;
1055 formatInt: function ( s ) {
1056 var i = parseInt( s, 10 );
1057 return isNaN( i ) ? -Infinity : i;
1060 clearTableBody: function ( table ) {
1061 $( table.tBodies[ 0 ] ).empty();
1064 getParser: function ( id ) {
1065 buildTransformTable();
1068 buildCollationTable();
1070 return getParserById( id );
1073 getParsers: function () { // for table diagnosis
1081 // Register as jQuery prototype method
1082 $.fn.tablesorter = function ( settings ) {
1083 return ts.construct( this, settings );
1086 // Add default parsers
1092 format: function ( s ) {
1094 s = $.trim( s.toLowerCase() );
1095 if ( ts.collationRegex ) {
1096 tsc = ts.collationTable;
1097 s = s.replace( ts.collationRegex, function ( match ) {
1098 var r = tsc[ match ] ? tsc[ match ] : tsc[ match.toUpperCase() ];
1099 return r.toLowerCase();
1109 is: function ( s ) {
1110 return ts.rgx.IPAddress[ 0 ].test( s );
1112 format: function ( s ) {
1116 for ( i = 0; i < a.length; i++ ) {
1118 if ( item.length === 1 ) {
1120 } else if ( item.length === 2 ) {
1126 return $.tablesorter.formatFloat( r );
1133 is: function ( s ) {
1134 return ts.rgx.currency[ 0 ].test( s );
1136 format: function ( s ) {
1137 return $.tablesorter.formatDigit( s.replace( ts.rgx.currency[ 1 ], '' ) );
1144 is: function ( s ) {
1145 return ts.rgx.url[ 0 ].test( s );
1147 format: function ( s ) {
1148 return $.trim( s.replace( ts.rgx.url[ 1 ], '' ) );
1155 is: function ( s ) {
1156 return ts.rgx.isoDate[ 0 ].test( s );
1158 format: function ( s ) {
1159 var match, i, isodate, ms, hOffset, mOffset;
1160 match = s.match( ts.rgx.isoDate[ 0 ] );
1161 if ( match === null ) {
1162 // Otherwise a signed number with 1-4 digit is parsed as isoDate
1163 match = s.match( ts.rgx.isoDate[ 1 ] );
1169 for ( i = 2; i <= 4; i += 2 ) {
1170 if ( !match[ i ] || match[ i ].length === 0 ) {
1175 for ( i = 6; i <= 15; i++ ) {
1176 if ( !match[ i ] || match[ i ].length === 0 ) {
1180 ms = parseFloat( match[ 11 ].replace( /,/, '.' ) ) * 1000;
1181 hOffset = $.tablesorter.formatInt( match[ 13 ] + match[ 14 ] );
1182 mOffset = $.tablesorter.formatInt( match[ 13 ] + match[ 15 ] );
1184 isodate = new Date( 0 );
1185 // Because Date constructor changes year 0-99 to 1900-1999, use setUTCFullYear()
1186 isodate.setUTCFullYear( match[ 1 ], match[ 2 ] - 1, match[ 4 ] );
1187 isodate.setUTCHours( match[ 6 ] - hOffset, match[ 8 ] - mOffset, match[ 10 ], ms );
1188 return isodate.getTime();
1195 is: function ( s ) {
1196 return ts.rgx.usLongDate[ 0 ].test( s );
1198 format: function ( s ) {
1199 return $.tablesorter.formatFloat( new Date( s ).getTime() );
1206 is: function ( s ) {
1207 return ( ts.dateRegex[ 0 ].test( s ) || ts.dateRegex[ 1 ].test( s ) || ts.dateRegex[ 2 ].test( s ) );
1209 format: function ( s ) {
1211 s = $.trim( s.toLowerCase() );
1213 if ( ( match = s.match( ts.dateRegex[ 0 ] ) ) !== null ) {
1214 if ( mw.config.get( 'wgDefaultDateFormat' ) === 'mdy' || mw.config.get( 'wgPageContentLanguage' ) === 'en' ) {
1215 s = [ match[ 3 ], match[ 1 ], match[ 2 ] ];
1216 } else if ( mw.config.get( 'wgDefaultDateFormat' ) === 'dmy' ) {
1217 s = [ match[ 3 ], match[ 2 ], match[ 1 ] ];
1219 // If we get here, we don't know which order the dd-dd-dddd
1220 // date is in. So return something not entirely invalid.
1223 } else if ( ( match = s.match( ts.dateRegex[ 1 ] ) ) !== null ) {
1224 s = [ match[ 3 ], String( ts.monthNames[ match[ 2 ] ] ), match[ 1 ] ];
1225 } else if ( ( match = s.match( ts.dateRegex[ 2 ] ) ) !== null ) {
1226 s = [ match[ 3 ], String( ts.monthNames[ match[ 1 ] ] ), match[ 2 ] ];
1228 // Should never get here
1232 // Pad Month and Day
1233 if ( s[ 1 ].length === 1 ) {
1234 s[ 1 ] = '0' + s[ 1 ];
1236 if ( s[ 2 ].length === 1 ) {
1237 s[ 2 ] = '0' + s[ 2 ];
1240 if ( ( y = parseInt( s[ 0 ], 10 ) ) < 100 ) {
1241 // Guestimate years without centuries
1248 while ( s[ 0 ].length < 4 ) {
1249 s[ 0 ] = '0' + s[ 0 ];
1251 return parseInt( s.join( '' ), 10 );
1258 is: function ( s ) {
1259 return ts.rgx.time[ 0 ].test( s );
1261 format: function ( s ) {
1262 return $.tablesorter.formatFloat( new Date( '2000/01/01 ' + s ).getTime() );
1269 is: function ( s ) {
1270 return $.tablesorter.numberRegex.test( $.trim( s ) );
1272 format: function ( s ) {
1273 return $.tablesorter.formatDigit( s );
1278 }( jQuery, mediaWiki ) );