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