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