fddf8444bec1a3f2a3f9192d58fcc0ddd39c8627
3 module( 'jquery.tablesorter' );
6 mw
.config
.set( 'wgMonthNames', ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']);
7 mw
.config
.set( 'wgMonthNamesShort', ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']);
8 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
10 test( '-- Initial check', function() {
12 ok( $.tablesorter
, '$.tablesorter defined' );
16 * Create an HTML table from an array of row arrays containing text strings.
17 * First row will be header row. No fancy rowspan/colspan stuff.
19 * @param {String[]} header
20 * @param {String[][]} data
23 var tableCreate = function( header
, data
) {
24 var $table
= $( '<table class="sortable"><thead></thead><tbody></tbody></table>' ),
25 $thead
= $table
.find( 'thead' ),
26 $tbody
= $table
.find( 'tbody' );
27 var $tr
= $( '<tr>' );
28 $.each( header
, function( i
, str
) {
29 var $th
= $( '<th>' );
30 $th
.text( str
).appendTo( $tr
);
32 $tr
.appendTo( $thead
);
34 for (var i
= 0; i
< data
.length
; i
++) {
36 $.each( data
[i
], function( j
, str
) {
37 var $td
= $( '<td>' );
38 $td
.text( str
).appendTo( $tr
);
40 $tr
.appendTo( $tbody
);
46 * Extract text from table.
48 * @param {jQuery} $table
51 var tableExtract = function( $table
) {
53 $table
.find( 'tbody' ).find( 'tr' ).each( function( i
, tr
) {
55 $( tr
).find( 'td,th' ).each( function( i
, td
) {
56 row
.push( $( td
).text() );
64 * Run a table test by building a table with the given data,
65 * running some callback on it, then checking the results.
67 * @param {String} msg text to pass on to qunit for the comparison
68 * @param {String[]} header cols to make the table
69 * @param {String[][]} data rows/cols to make the table
70 * @param {String[][]} expected rows/cols to compare against at end
71 * @param {function($table)} callback something to do with the table before we compare
73 var tableTest = function( msg
, header
, data
, expected
, callback
) {
74 test( msg
, function() {
77 var $table
= tableCreate( header
, data
);
78 //$( 'body' ).append($table);
80 // Give caller a chance to set up sorting and manipulate the table.
83 // Table sorting is done synchronously; if it ever needs to change back
84 // to asynchronous, we'll need a timeout or a callback here.
85 var extracted
= tableExtract( $table
);
86 deepEqual( extracted
, expected
, msg
);
90 var reversed = function(arr
) {
91 var arr2
= arr
.slice(0);
96 // Sample data set: some planets!
97 var header
= ['Planet', 'Radius (km)'],
98 mercury
= ['Mercury', '2439.7'],
99 venus
= ['Venus', '6051.8'],
100 earth
= ['Earth', '6371.0'],
101 mars
= ['Mars', '3390.0'],
102 jupiter
= ['Jupiter', '69911'],
103 saturn
= ['Saturn', '58232'];
106 var planets
= [mercury
, venus
, earth
, mars
, jupiter
, saturn
];
107 var ascendingName
= [earth
, jupiter
, mars
, mercury
, saturn
, venus
];
108 var ascendingRadius
= [mercury
, mars
, venus
, earth
, saturn
, jupiter
];
111 'Basic planet table: ascending by name',
116 $table
.tablesorter();
117 $table
.find( '.headerSort:eq(0)' ).click();
121 'Basic planet table: ascending by name a second time',
126 $table
.tablesorter();
127 $table
.find( '.headerSort:eq(0)' ).click();
131 'Basic planet table: descending by name',
134 reversed(ascendingName
),
136 $table
.tablesorter();
137 $table
.find( '.headerSort:eq(0)' ).click().click();
141 'Basic planet table: ascending radius',
146 $table
.tablesorter();
147 $table
.find( '.headerSort:eq(1)' ).click();
151 'Basic planet table: descending radius',
154 reversed(ascendingRadius
),
156 $table
.tablesorter();
157 $table
.find( '.headerSort:eq(1)' ).click().click();
164 'Bug 28775: German-style short numeric dates',
167 // German-style dates are day-month-year
175 // Sorted by ascending date
183 // @fixme reset it at end or change module to allow us to override it
184 mw
.config
.set( 'wgDefaultDateFormat', 'dmy' );
185 $table
.tablesorter();
186 $table
.find( '.headerSort:eq(0)' ).click();
190 'Bug 28775: American-style short numeric dates',
193 // American-style dates are month-day-year
201 // Sorted by ascending date
209 // @fixme reset it at end or change module to allow us to override it
210 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
211 $table
.tablesorter();
212 $table
.find( '.headerSort:eq(0)' ).click();
217 // Some randomly generated fake IPs
228 // Sort order should go octet by octet
239 'Bug 17141: IPv4 address sorting',
244 $table
.tablesorter();
245 $table
.find( '.headerSort:eq(0)' ).click();
249 'Bug 17141: IPv4 address sorting (reverse)',
252 reversed(ipv4Sorted
),
254 $table
.tablesorter();
255 $table
.find( '.headerSort:eq(0)' ).click().click();
260 // Some words with Umlauts
271 var umlautWordsSorted
= [
272 // Some words with Umlauts
284 'Accented Characters with custom collation',
289 mw
.config
.set( 'tableSorterCollation', {
295 $table
.tablesorter();
296 $table
.find( '.headerSort:eq(0)' ).click();
297 mw
.config
.set( 'tableSorterCollation', {} );
301 var planetsRowspan
= [["Earth","6051.8"], jupiter
, ["Mars","6051.8"], mercury
, saturn
, venus
];
302 var planetsRowspanII
= [jupiter
, mercury
, saturn
, ['Venus', '6371.0'], venus
, ['Venus', '3390.0']];
305 'Basic planet table: Same value for multiple rows via rowspan',
310 // Modify the table to have a multiuple-row-spanning cell:
311 // - Remove 2nd cell of 4th row, and, 2nd cell or 5th row.
312 $table
.find( 'tr:eq(3) td:eq(1), tr:eq(4) td:eq(1)' ).remove();
313 // - Set rowspan for 2nd cell of 3rd row to 3.
314 // This covers the removed cell in the 4th and 5th row.
315 $table
.find( 'tr:eq(2) td:eq(1)' ).prop( 'rowspan', '3' );
317 $table
.tablesorter();
318 $table
.find( '.headerSort:eq(0)' ).click();
322 'Basic planet table: Same value for multiple rows via rowspan II',
327 // Modify the table to have a multiuple-row-spanning cell:
328 // - Remove 1st cell of 4th row, and, 1st cell or 5th row.
329 $table
.find( 'tr:eq(3) td:eq(0), tr:eq(4) td:eq(0)' ).remove();
330 // - Set rowspan for 1st cell of 3rd row to 3.
331 // This covers the removed cell in the 4th and 5th row.
332 $table
.find( 'tr:eq(2) td:eq(0)' ).prop( 'rowspan', '3' );
334 $table
.tablesorter();
335 $table
.find( '.headerSort:eq(0)' ).click();
339 var complexMDYDates
= [
340 // Some words with Umlauts
341 ['January, 19 2010'],
348 var complexMDYSorted
= [
352 ["January, 19 2010"],
357 'Complex date parsing I',
362 mw
.config
.set( 'wgDefaultDateFormat', 'mdy' );
363 $table
.tablesorter();
364 $table
.find( '.headerSort:eq(0)' ).click();
368 var ascendingNameLegacy
= ascendingName
.slice(0);
369 ascendingNameLegacy
[4] = ascendingNameLegacy
[5];
370 ascendingNameLegacy
.pop();
373 'Legacy compat with .sortbottom',
378 $table
.find( 'tr:last' ).addClass( 'sortbottom' );
379 $table
.tablesorter();
380 $table
.find( '.headerSort:eq(0)' ).click();