Review and clean up of jquery.tablesorter.js + applying code conventions
[lhc/web/wiklou.git] / resources / 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, wgMonthNames, wgMonthNamesShort,
12 * wgDefaultDateFormat, wgContentLanguage)
13 */
14 /**
15 *
16 * @description Create a sortable table with multi-column sorting capabilitys
17 *
18 * @example $( 'table' ).tablesorter();
19 * @desc Create a simple tablesorter interface.
20 *
21 * @option String cssHeader ( optional ) A string of the class name to be appended
22 * to sortable tr elements in the thead of the table. Default value:
23 * "header"
24 *
25 * @option String cssAsc ( optional ) A string of the class name to be appended to
26 * sortable tr elements in the thead on a ascending sort. Default value:
27 * "headerSortUp"
28 *
29 * @option String cssDesc ( optional ) A string of the class name to be appended
30 * to sortable tr elements in the thead on a descending sort. Default
31 * value: "headerSortDown"
32 *
33 * @option String sortInitialOrder ( optional ) A string of the inital sorting
34 * order can be asc or desc. Default value: "asc"
35 *
36 * @option String sortMultisortKey ( optional ) A string of the multi-column sort
37 * key. Default value: "shiftKey"
38 *
39 * @option Boolean sortLocaleCompare ( optional ) Boolean flag indicating whatever
40 * to use String.localeCampare method or not. Set to false.
41 *
42 * @option Boolean cancelSelection ( optional ) Boolean flag indicating if
43 * tablesorter should cancel selection of the table headers text.
44 * Default value: true
45 *
46 * @option Boolean debug ( optional ) Boolean flag indicating if tablesorter
47 * should display debuging information usefull for development.
48 *
49 * @type jQuery
50 *
51 * @name tablesorter
52 *
53 * @cat Plugins/Tablesorter
54 *
55 * @author Christian Bach/christian.bach@polyester.se
56 */
57
58 ( function( $ ) {
59
60 /* Local scope */
61
62 var ts,
63 parsers = [];
64
65 /* Parser utility functions */
66
67 function getParserById( name ) {
68 var len = parsers.length;
69 for ( var i = 0; i < len; i++ ) {
70 if ( parsers[i].id.toLowerCase() === name.toLowerCase() ) {
71 return parsers[i];
72 }
73 }
74 return false;
75 }
76
77 function getElementText( node ) {
78 if ( node.hasAttribute && node.hasAttribute( 'data-sort-value' ) ) {
79 return node.getAttribute( 'data-sort-value' );
80 } else {
81 return $( node ).text();
82 }
83 }
84
85 function getTextFromRowAndCellIndex( rows, rowIndex, cellIndex ) {
86 if ( rows[rowIndex] && rows[rowIndex].cells[cellIndex] ) {
87 return $.trim( getElementText( rows[rowIndex].cells[cellIndex] ) );
88 } else {
89 return '';
90 }
91 }
92
93 function detectParserForColumn( table, rows, cellIndex ) {
94 var l = parsers.length,
95 nodeValue,
96 // Start with 1 because 0 is the fallback parser
97 i = 1,
98 rowIndex = 0,
99 concurrent = 0,
100 needed = ( rows.length > 4 ) ? 5 : rows.length;
101
102 while( i < l ) {
103 nodeValue = getTextFromRowAndCellIndex( rows, rowIndex, cellIndex );
104 if ( nodeValue !== '') {
105 if ( parsers[i].is( nodeValue, table ) ) {
106 concurrent++;
107 rowIndex++;
108 if ( concurrent >= needed ) {
109 // Confirmed the parser for multiple cells, let's return it
110 return parsers[i];
111 }
112 } else {
113 // Check next parser, reset rows
114 i++;
115 rowIndex = 0;
116 concurrent = 0;
117 }
118 } else {
119 // Empty cell
120 rowIndex++;
121 if ( rowIndex > rows.length ) {
122 rowIndex = 0;
123 i++;
124 }
125 }
126 }
127
128 // 0 is always the generic parser (text)
129 return parsers[0];
130 }
131
132 function buildParserCache( table, $headers ) {
133 var rows = table.tBodies[0].rows,
134 sortType,
135 parsers = [];
136
137 if ( rows[0] ) {
138
139 var cells = rows[0].cells,
140 len = cells.length,
141 i, parser;
142
143 for ( i = 0; i < len; i++ ) {
144 parser = false;
145 sortType = $headers.eq( i ).data( 'sort-type' );
146 if ( sortType !== undefined ) {
147 parser = getParserById( sortType );
148 }
149
150 if ( parser === false ) {
151 parser = detectParserForColumn( table, rows, i );
152 }
153
154 parsers.push( parser );
155 }
156 }
157 return parsers;
158 }
159
160 /* Other utility functions */
161
162 function buildCache( table ) {
163 var totalRows = ( table.tBodies[0] && table.tBodies[0].rows.length ) || 0,
164 totalCells = ( table.tBodies[0].rows[0] && table.tBodies[0].rows[0].cells.length ) || 0,
165 parsers = table.config.parsers,
166 cache = {
167 row: [],
168 normalized: []
169 };
170
171 for ( var i = 0; i < totalRows; ++i ) {
172
173 // Add the table data to main data array
174 var $row = $( table.tBodies[0].rows[i] ),
175 cols = [];
176
177 // if this is a child row, add it to the last row's children and
178 // continue to the next row
179 if ( $row.hasClass( table.config.cssChildRow ) ) {
180 cache.row[cache.row.length - 1] = cache.row[cache.row.length - 1].add( $row );
181 // go to the next for loop
182 continue;
183 }
184
185 cache.row.push( $row );
186
187 for ( var j = 0; j < totalCells; ++j ) {
188 cols.push( parsers[j].format( getElementText( $row[0].cells[j] ), table, $row[0].cells[j] ) );
189 }
190
191 cols.push( cache.normalized.length ); // add position for rowCache
192 cache.normalized.push( cols );
193 cols = null;
194 }
195
196 return cache;
197 }
198
199 function appendToTable( table, cache ) {
200 var row = cache.row,
201 normalized = cache.normalized,
202 totalRows = normalized.length,
203 checkCell = ( normalized[0].length - 1 ),
204 fragment = document.createDocumentFragment();
205
206 for ( var i = 0; i < totalRows; i++ ) {
207 var pos = normalized[i][checkCell];
208
209 var l = row[pos].length;
210
211 for ( var j = 0; j < l; j++ ) {
212 fragment.appendChild( row[pos][j] );
213 }
214
215 }
216 table.tBodies[0].appendChild( fragment );
217 }
218
219 function buildHeaders( table, msg ) {
220 var maxSeen = 0,
221 longest,
222 realCellIndex = 0,
223 $tableHeaders = $( 'thead:eq(0) tr', table );
224 if ( $tableHeaders.length > 1 ) {
225 $tableHeaders.each(function() {
226 if ( this.cells.length > maxSeen ) {
227 maxSeen = this.cells.length;
228 longest = this;
229 }
230 });
231 $tableHeaders = $( longest );
232 }
233 $tableHeaders = $tableHeaders.find( 'th' ).each( function( index ) {
234 this.column = realCellIndex;
235
236 var colspan = this.colspan;
237 colspan = colspan ? parseInt( colspan, 10 ) : 1;
238 realCellIndex += colspan;
239
240 this.order = 0;
241 this.count = 0;
242
243 if ( $( this ).is( '.unsortable, .mw-unsortable' ) ) {
244 this.sortDisabled = true;
245 }
246
247 if ( !this.sortDisabled ) {
248 var $th = $( this ).addClass( table.config.cssHeader ).attr( 'title', msg[1] );
249 }
250
251 // add cell to headerList
252 table.config.headerList[index] = this;
253 } );
254
255 return $tableHeaders;
256
257 }
258
259 function isValueInArray( v, a ) {
260 var l = a.length;
261 for ( var i = 0; i < l; i++ ) {
262 if ( a[i][0] == v ) {
263 return true;
264 }
265 }
266 return false;
267 }
268
269 function setHeadersCss( table, $headers, list, css, msg ) {
270 // Remove all header information
271 $headers.removeClass( css[0] ).removeClass( css[1] );
272
273 var h = [];
274 $headers.each( function( offset ) {
275 if ( !this.sortDisabled ) {
276 h[this.column] = $( this );
277 }
278 } );
279
280 var l = list.length;
281 for ( var i = 0; i < l; i++ ) {
282 h[ list[i][0] ].addClass( css[ list[i][1] ] ).attr( 'title', msg[ list[i][1] ] );
283 }
284 }
285
286 function sortText( a, b ) {
287 return ( (a < b) ? false : ((a > b) ? true : 0) );
288 }
289
290 function sortTextDesc( a, b ) {
291 return ( (b < a) ? false : ((b > a) ? true : 0) );
292 }
293
294 function checkSorting( array1, array2, sortList ) {
295 var col, fn, ret;
296 for ( var i = 0, len = sortList.length; i < len; i++ ) {
297 col = sortList[i][0];
298 fn = ( sortList[i][1] ) ? sortTextDesc : sortText;
299 ret = fn.call( this, array1[col], array2[col] );
300 if ( ret !== 0 ) {
301 return ret;
302 }
303 }
304 return ret;
305 }
306
307 // Merge sort algorithm
308 // Based on http://en.literateprograms.org/Merge_sort_(JavaScript)
309 function mergeSortHelper( array, begin, beginRight, end, sortList ) {
310 for ( ; begin < beginRight; ++begin ) {
311 if ( checkSorting( array[begin], array[beginRight], sortList ) ) {
312 var v = array[begin];
313 array[begin] = array[beginRight];
314 var begin2 = beginRight;
315 while ( begin2 + 1 < end && checkSorting( v, array[begin2 + 1], sortList ) ) {
316 var tmp = array[begin2];
317 array[begin2] = array[begin2 + 1];
318 array[begin2 + 1] = tmp;
319 ++begin2;
320 }
321 array[begin2] = v;
322 }
323 }
324 }
325
326 function mergeSort(array, begin, end, sortList) {
327 var size = end - begin;
328 if ( size < 2 ) {
329 return;
330 }
331
332 var beginRight = begin + Math.floor(size / 2);
333
334 mergeSort( array, begin, beginRight, sortList );
335 mergeSort( array, beginRight, end, sortList );
336 mergeSortHelper( array, begin, beginRight, end, sortList );
337 }
338
339 function multisort( table, sortList, cache ) {
340 var i = sortList.length;
341 mergeSort( cache.normalized, 0, cache.normalized.length, sortList );
342
343 return cache;
344 }
345
346 function buildTransformTable() {
347 var digits = '0123456789,.'.split( '' );
348 var separatorTransformTable = mw.config.get( 'wgSeparatorTransformTable' );
349 var digitTransformTable = mw.config.get( 'wgDigitTransformTable' );
350 if ( separatorTransformTable === null || ( separatorTransformTable[0] === '' && digitTransformTable[2] === '' ) ) {
351 ts.transformTable = false;
352 } else {
353 ts.transformTable = {};
354
355 // Unpack the transform table
356 var ascii = separatorTransformTable[0].split( "\t" ).concat( digitTransformTable[0].split( "\t" ) );
357 var localised = separatorTransformTable[1].split( "\t" ).concat( digitTransformTable[1].split( "\t" ) );
358
359 // Construct regex for number identification
360 for ( var i = 0; i < ascii.length; i++ ) {
361 ts.transformTable[localised[i]] = ascii[i];
362 digits.push( $.escapeRE( localised[i] ) );
363 }
364 }
365 var digitClass = '[' + digits.join( '', digits ) + ']';
366
367 // We allow a trailing percent sign, which we just strip. This works fine
368 // if percents and regular numbers aren't being mixed.
369 ts.numberRegex = new RegExp("^(" + "[-+\u2212]?[0-9][0-9,]*(\\.[0-9,]*)?(E[-+\u2212]?[0-9][0-9,]*)?" + // Fortran-style scientific
370 "|" + "[-+\u2212]?" + digitClass + "+[\\s\\xa0]*%?" + // Generic localised
371 ")$", "i");
372 }
373
374 function buildDateTable() {
375 var regex = [];
376 ts.monthNames = [
377 [],
378 []
379 ];
380
381 for ( var i = 1; i < 13; i++ ) {
382 ts.monthNames[0][i] = mw.config.get( 'wgMonthNames' )[i].toLowerCase();
383 ts.monthNames[1][i] = mw.config.get( 'wgMonthNamesShort' )[i].toLowerCase().replace( '.', '' );
384 regex.push( $.escapeRE( ts.monthNames[0][i] ) );
385 regex.push( $.escapeRE( ts.monthNames[1][i] ) );
386 }
387
388 // Build piped string
389 regex = regex.join( '|' );
390
391 // Build RegEx
392 // Any date formated with . , ' - or /
393 ts.dateRegex[0] = new RegExp( /^\s*\d{1,2}[\,\.\-\/'\s]{1,2}\d{1,2}[\,\.\-\/'\s]{1,2}\d{2,4}\s*?/i);
394
395 // Written Month name, dmy
396 ts.dateRegex[1] = new RegExp( '^\\s*\\d{1,2}[\\,\\.\\-\\/\'\\s]*(' + regex + ')' + '[\\,\\.\\-\\/\'\\s]*\\d{2,4}\\s*$', 'i' );
397
398 // Written Month name, mdy
399 ts.dateRegex[2] = new RegExp( '^\\s*(' + regex + ')' + '[\\,\\.\\-\\/\'\\s]*\\d{1,2}[\\,\\.\\-\\/\'\\s]*\\d{2,4}\\s*$', 'i' );
400
401 }
402
403 function explodeRowspans( $table ) {
404 // Split multi row cells into multiple cells with the same content
405 $table.find( '[rowspan]' ).each(function() {
406 var rowSpan = this.rowSpan;
407 this.rowSpan = 1;
408 var cell = $( this );
409 var next = cell.parent().nextAll();
410 for ( var i = 0; i < rowSpan - 1; i++ ) {
411 var td = next.eq( i ).find( 'td' );
412 if ( !td.length ) {
413 next.eq( i ).append( cell.clone() );
414 } else if ( this.cellIndex === 0 ) {
415 td.eq( this.cellIndex ).before( cell.clone() );
416 } else {
417 td.eq( this.cellIndex - 1 ).after( cell.clone() );
418 }
419 }
420 });
421 }
422
423 function buildCollationTable() {
424 ts.collationTable = mw.config.get( 'tableSorterCollation' );
425 ts.collationRegex = null;
426 if ( ts.collationTable ) {
427 var keys = [];
428
429 // Build array of key names
430 for ( var key in ts.collationTable ) {
431 if ( ts.collationTable.hasOwnProperty(key) ) { //to be safe
432 keys.push(key);
433 }
434 }
435 if (keys.length) {
436 ts.collationRegex = new RegExp( '[' + keys.join( '' ) + ']', 'ig' );
437 }
438 }
439 }
440
441 function cacheRegexs() {
442 if ( ts.rgx ) {
443 return;
444 }
445 ts.rgx = {
446 IPAddress: [
447 new RegExp( /^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$/)
448 ],
449 currency: [
450 new RegExp( /^[£$€?.]/),
451 new RegExp( /[£$€]/g)
452 ],
453 url: [
454 new RegExp( /^(https?|ftp|file):\/\/$/),
455 new RegExp( /(https?|ftp|file):\/\//)
456 ],
457 isoDate: [
458 new RegExp( /^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/)
459 ],
460 usLongDate: [
461 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)))$/)
462 ],
463 time: [
464 new RegExp( /^(([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(am|pm)))$/)
465 ]
466 };
467 }
468
469 /* Public scope */
470
471 $.tablesorter = {
472
473 defaultOptions: {
474 cssHeader: 'headerSort',
475 cssAsc: 'headerSortUp',
476 cssDesc: 'headerSortDown',
477 cssChildRow: 'expand-child',
478 sortInitialOrder: 'asc',
479 sortMultiSortKey: 'shiftKey',
480 sortLocaleCompare: false,
481 parsers: {},
482 widgets: [],
483 headers: {},
484 cancelSelection: true,
485 sortList: [],
486 headerList: [],
487 selectorHeaders: 'thead tr:eq(0) th',
488 debug: false
489 },
490
491 dateRegex: [],
492 monthNames: [],
493
494 /**
495 * @param $tables {jQuery}
496 * @param settings {Object} (optional)
497 */
498 construct: function( $tables, settings ) {
499 return $tables.each( function( i, table ) {
500
501 // Quit if no thead or tbody.
502 if ( !table.tHead || !table.tBodies ) {
503 return;
504 }
505 // Declare and cache.
506 var $document, $headers, cache, config, sortOrder,
507 $table = $( table ),
508 shiftDown = 0,
509 firstTime = true;
510
511 // New config object.
512 table.config = {};
513
514 // Merge and extend.
515 config = $.extend( table.config, $.tablesorter.defaultOptions, settings );
516
517 // Save the settings where they read
518 $.data( table, 'tablesorter', config );
519
520 // Get the CSS class names, could be done else where.
521 var sortCSS = [ config.cssDesc, config.cssAsc ];
522 var sortMsg = [ mw.msg( 'sort-descending' ), mw.msg( 'sort-ascending' ) ];
523
524 // Build headers
525 $headers = buildHeaders( table, sortMsg );
526
527 // Grab and process locale settings
528 buildTransformTable();
529 buildDateTable();
530 buildCollationTable();
531
532 // Precaching regexps can bring 10 fold
533 // performance improvements in some browsers.
534 cacheRegexs();
535
536 // Apply event handling to headers
537 // this is to big, perhaps break it out?
538 $headers.click( function( e ) {
539
540 if ( firstTime ) {
541 firstTime = false;
542 explodeRowspans( $table );
543 // try to auto detect column type, and store in tables config
544 table.config.parsers = buildParserCache( table, $headers );
545 // build the cache for the tbody cells
546 cache = buildCache( table );
547 }
548 var totalRows = ( $table[0].tBodies[0] && $table[0].tBodies[0].rows.length ) || 0;
549 if ( !table.sortDisabled && totalRows > 0 ) {
550
551 // Cache jQuery object
552 var $cell = $( this );
553
554 // Get current column index
555 var i = this.column;
556
557 // Get current column sort order
558 this.order = this.count % 2;
559 this.count++;
560
561 // User only wants to sort on one column
562 if ( !e[config.sortMultiSortKey] ) {
563 // Flush the sort list
564 config.sortList = [];
565 // Add column to sort list
566 config.sortList.push( [i, this.order] );
567
568 // Multi column sorting
569 } else {
570 // The user has clicked on an already sorted column.
571 if ( isValueInArray( i, config.sortList ) ) {
572 // Reverse the sorting direction for all tables.
573 for ( var j = 0; j < config.sortList.length; j++ ) {
574 var s = config.sortList[j],
575 o = config.headerList[s[0]];
576 if ( s[0] == i ) {
577 o.count = s[1];
578 o.count++;
579 s[1] = o.count % 2;
580 }
581 }
582 } else {
583 // Add column to sort list array
584 config.sortList.push( [i, this.order] );
585 }
586 }
587
588 // Set CSS for headers
589 setHeadersCss( $table[0], $headers, config.sortList, sortCSS, sortMsg );
590 appendToTable(
591 $table[0], multisort( $table[0], config.sortList, cache )
592 );
593
594 // Stop normal event by returning false
595 return false;
596 }
597
598 // Cancel selection
599 } ).mousedown( function() {
600 if ( config.cancelSelection ) {
601 this.onselectstart = function() {
602 return false;
603 };
604 return false;
605 }
606 } );
607
608 } );
609 },
610
611 addParser: function( parser ) {
612 var l = parsers.length,
613 a = true;
614 for ( var i = 0; i < l; i++ ) {
615 if ( parsers[i].id.toLowerCase() == parser.id.toLowerCase() ) {
616 a = false;
617 }
618 }
619 if ( a ) {
620 parsers.push( parser );
621 }
622 },
623
624 formatDigit: function( s ) {
625 if ( ts.transformTable !== false ) {
626 var out = '',
627 c;
628 for ( var p = 0; p < s.length; p++ ) {
629 c = s.charAt(p);
630 if ( c in ts.transformTable ) {
631 out += ts.transformTable[c];
632 } else {
633 out += c;
634 }
635 }
636 s = out;
637 }
638 var i = parseFloat( s.replace( /[, ]/g, '' ).replace( "\u2212", '-' ) );
639 return ( isNaN(i)) ? 0 : i;
640 },
641
642 formatFloat: function( s ) {
643 var i = parseFloat(s);
644 return ( isNaN(i)) ? 0 : i;
645 },
646
647 formatInt: function( s ) {
648 var i = parseInt( s, 10 );
649 return ( isNaN(i)) ? 0 : i;
650 },
651
652 clearTableBody: function( table ) {
653 if ( $.browser.msie ) {
654 var empty = function( el ) {
655 while ( el.firstChild ) {
656 el.removeChild( el.firstChild );
657 }
658 };
659 empty( table.tBodies[0] );
660 } else {
661 table.tBodies[0].innerHTML = '';
662 }
663 }
664 };
665
666 // Shortcut
667 ts = $.tablesorter;
668
669 // Register as jQuery prototype method
670 $.fn.tablesorter = function( settings ) {
671 return ts.construct( this, settings );
672 };
673
674 // Add default parsers
675 ts.addParser( {
676 id: 'text',
677 is: function( s ) {
678 return true;
679 },
680 format: function( s ) {
681 s = $.trim( s.toLowerCase() );
682 if ( ts.collationRegex ) {
683 var tsc = ts.collationTable;
684 s = s.replace( ts.collationRegex, function( match ) {
685 var r = tsc[match] ? tsc[match] : tsc[match.toUpperCase()];
686 return r.toLowerCase();
687 } );
688 }
689 return s;
690 },
691 type: 'text'
692 } );
693
694 ts.addParser( {
695 id: 'IPAddress',
696 is: function( s ) {
697 return ts.rgx.IPAddress[0].test(s);
698 },
699 format: function( s ) {
700 var a = s.split( '.' ),
701 r = '',
702 l = a.length;
703 for ( var i = 0; i < l; i++ ) {
704 var item = a[i];
705 if ( item.length == 1 ) {
706 r += '00' + item;
707 } else if ( item.length == 2 ) {
708 r += '0' + item;
709 } else {
710 r += item;
711 }
712 }
713 return $.tablesorter.formatFloat(r);
714 },
715 type: 'numeric'
716 } );
717
718 ts.addParser( {
719 id: 'currency',
720 is: function( s ) {
721 return ts.rgx.currency[0].test(s);
722 },
723 format: function( s ) {
724 return $.tablesorter.formatDigit( s.replace( ts.rgx.currency[1], '' ) );
725 },
726 type: 'numeric'
727 } );
728
729 ts.addParser( {
730 id: 'url',
731 is: function( s ) {
732 return ts.rgx.url[0].test(s);
733 },
734 format: function( s ) {
735 return $.trim( s.replace( ts.rgx.url[1], '' ) );
736 },
737 type: 'text'
738 } );
739
740 ts.addParser( {
741 id: 'isoDate',
742 is: function( s ) {
743 return ts.rgx.isoDate[0].test(s);
744 },
745 format: function( s ) {
746 return $.tablesorter.formatFloat((s !== '') ? new Date(s.replace(
747 new RegExp( /-/g), '/')).getTime() : '0' );
748 },
749 type: 'numeric'
750 } );
751
752 ts.addParser( {
753 id: 'usLongDate',
754 is: function( s ) {
755 return ts.rgx.usLongDate[0].test(s);
756 },
757 format: function( s ) {
758 return $.tablesorter.formatFloat( new Date(s).getTime() );
759 },
760 type: 'numeric'
761 } );
762
763 ts.addParser( {
764 id: 'date',
765 is: function( s ) {
766 return ( ts.dateRegex[0].test(s) || ts.dateRegex[1].test(s) || ts.dateRegex[2].test(s ));
767 },
768 format: function( s, table ) {
769 s = $.trim( s.toLowerCase() );
770
771 for ( var i = 1, j = 0; i < 13 && j < 2; i++ ) {
772 s = s.replace( ts.monthNames[j][i], i );
773 if ( i == 12 ) {
774 j++;
775 i = 0;
776 }
777 }
778
779 s = s.replace( /[\-\.\,' ]/g, '/' );
780
781 // Replace double slashes
782 s = s.replace( /\/\//g, '/' );
783 s = s.replace( /\/\//g, '/' );
784 s = s.split( '/' );
785
786 // Pad Month and Day
787 if ( s[0] && s[0].length == 1 ) {
788 s[0] = '0' + s[0];
789 }
790 if ( s[1] && s[1].length == 1 ) {
791 s[1] = '0' + s[1];
792 }
793 var y;
794
795 if ( !s[2] ) {
796 // Fix yearless dates
797 s[2] = 2000;
798 } else if ( ( y = parseInt( s[2], 10) ) < 100 ) {
799 // Guestimate years without centuries
800 if ( y < 30 ) {
801 s[2] = 2000 + y;
802 } else {
803 s[2] = 1900 + y;
804 }
805 }
806 // Resort array depending on preferences
807 if ( mw.config.get( 'wgDefaultDateFormat' ) == 'mdy' || mw.config.get( 'wgContentLanguage' ) == 'en' ) {
808 s.push( s.shift() );
809 s.push( s.shift() );
810 } else if ( mw.config.get( 'wgDefaultDateFormat' ) == 'dmy' ) {
811 var d = s.shift();
812 s.push( s.shift() );
813 s.push(d);
814 }
815 return parseInt( s.join( '' ), 10 );
816 },
817 type: 'numeric'
818 } );
819
820 ts.addParser( {
821 id: 'time',
822 is: function( s ) {
823 return ts.rgx.time[0].test(s);
824 },
825 format: function( s ) {
826 return $.tablesorter.formatFloat( new Date( '2000/01/01 ' + s ).getTime() );
827 },
828 type: 'numeric'
829 } );
830
831 ts.addParser( {
832 id: 'number',
833 is: function( s, table ) {
834 return $.tablesorter.numberRegex.test( $.trim( s ));
835 },
836 format: function( s ) {
837 return $.tablesorter.formatDigit(s);
838 },
839 type: 'numeric'
840 } );
841
842 } )( jQuery );