Merge "jquery.tablesorter: Reset unaffected columns' sort counts when sorting"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 5 Jun 2013 11:48:07 +0000 (11:48 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 5 Jun 2013 11:48:07 +0000 (11:48 +0000)
1  2 
resources/jquery/jquery.tablesorter.js
tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js

  
        }
  
+       /**
+        * Sets the sort count of the columns that are not affected by the sorting to have them sorted
+        * in default (ascending) order when their header cell is clicked the next time.
+        *
+        * @param {jQuery} $headers
+        * @param {Number[][]} sortList
+        * @param {Number[][]} headerToColumns
+        */
+       function setHeadersOrder( $headers, sortList, headerToColumns ) {
+               // Loop through all headers to retrieve the indices of the columns the header spans across:
+               $.each( headerToColumns, function( headerIndex, columns ) {
+                       $.each( columns, function( i, columnIndex ) {
+                               var header = $headers[headerIndex];
+                               if ( !isValueInArray( columnIndex, sortList ) ) {
+                                       // Column shall not be sorted: Reset header count and order.
+                                       header.order = 0;
+                                       header.count = 0;
+                               } else {
+                                       // Column shall be sorted: Apply designated count and order.
+                                       $.each( sortList, function( j, sortColumn ) {
+                                               if ( sortColumn[0] === i ) {
+                                                       header.order = sortColumn[1];
+                                                       header.count = sortColumn[1] + 1;
+                                                       return false;
+                                               }
+                                       } );
+                               }
+                       } );
+               } );
+       }
        function isValueInArray( v, a ) {
                var l = a.length;
                for ( var i = 0; i < l; i++ ) {
                ts.dateRegex[0] = new RegExp( /^\s*(\d{1,2})[\,\.\-\/'\s]{1,2}(\d{1,2})[\,\.\-\/'\s]{1,2}(\d{2,4})\s*?/i);
  
                // Written Month name, dmy
 -              ts.dateRegex[1] = new RegExp( '^\\s*(\\d{1,2})[\\,\\.\\-\\/\'\\s]*(' + regex + ')' + '[\\,\\.\\-\\/\'\\s]*(\\d{2,4})\\s*$', 'i' );
 +              ts.dateRegex[1] = new RegExp( '^\\s*(\\d{1,2})[\\,\\.\\-\\/\'\\s]+(' + regex + ')' + '[\\,\\.\\-\\/\'\\s]+(\\d{2,4})\\s*$', 'i' );
  
                // Written Month name, mdy
 -              ts.dateRegex[2] = new RegExp( '^\\s*(' + regex + ')' + '[\\,\\.\\-\\/\'\\s]*(\\d{1,2})[\\,\\.\\-\\/\'\\s]*(\\d{2,4})\\s*$', 'i' );
 +              ts.dateRegex[2] = new RegExp( '^\\s*(' + regex + ')' + '[\\,\\.\\-\\/\'\\s]+(\\d{1,2})[\\,\\.\\-\\/\'\\s]+(\\d{2,4})\\s*$', 'i' );
  
        }
  
                $.each( sortObjects, function( i, sortObject ) {
                        $.each ( sortObject, function( columnIndex, order ) {
                                var orderIndex = ( order === 'desc' ) ? 1 : 0;
-                               sortList.push( [columnIndex, orderIndex] );
+                               sortList.push( [parseInt( columnIndex, 10 ), orderIndex] );
                        } );
                } );
                return sortList;
                                                                }
                                                        }
  
+                                                       // Reset order/counts of cells not affected by sorting
+                                                       setHeadersOrder( $headers, config.sortList, headerToColumns );
                                                        // Set CSS for headers
                                                        setHeadersCss( $table[0], $headers, config.sortList, sortCSS, sortMsg, columnToHeader );
                                                        appendToTable(
                                                        sortList = convertSortList( sortList );
                                                }
  
+                                               // Set each column's sort count to be able to determine the correct sort
+                                               // order when clicking on a header cell the next time
+                                               setHeadersOrder( $headers, sortList, headerToColumns );
                                                // re-build the cache for the tbody cells
                                                cache = buildCache( table );
  
@@@ -5,8 -5,6 +5,8 @@@
                wgMonthNames: ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
                wgMonthNamesShort: ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
                wgDefaultDateFormat: 'dmy',
 +              wgSeparatorTransformTable: ['', ''],
 +              wgDigitTransformTable: ['', ''],
                wgContentLanguage: 'en'
        };
  
                        $table.find( '.headerSort:eq(0)' ).click();
                }
        );
+       tableTest(
+               'Basic planet table: ascending by name (multiple clicks)',
+               header,
+               planets,
+               ascendingName,
+               function ( $table ) {
+                       $table.tablesorter();
+                       $table.find( '.headerSort:eq(0)' ).click();
+                       $table.find( '.headerSort:eq(1)' ).click();
+                       $table.find( '.headerSort:eq(0)' ).click();
+               }
+       );
        tableTest(
                'Basic planet table: descending by name',
                header,
                        $table.data( 'tablesorter' ).sort();
                }
        );
+       tableTest(
+               'Sort via click event after having initialized the tablesorter with initial sorting',
+               header,
+               initial,
+               descasc,
+               function ( $table ) {
+                       $table.tablesorter(
+                               { sortList: [ { 0: 'asc' }, { 1: 'asc' } ] }
+                       );
+                       $table.find( '.headerSort:eq(0)' ).click();
+               }
+       );
+       tableTest(
+               'Multi-sort via click event after having initialized the tablesorter with initial sorting',
+               header,
+               initial,
+               asc,
+               function ( $table ) {
+                       $table.tablesorter(
+                               { sortList: [ { 0: 'desc' }, { 1: 'desc' } ] }
+                       );
+                       $table.find( '.headerSort:eq(0)' ).click();
+                       // Pretend to click while pressing the multi-sort key
+                       var event = $.Event( 'click' );
+                       event[$table.data( 'tablesorter' ).config.sortMultiSortKey] = true;
+                       $table.find( '.headerSort:eq(1)' ).trigger( event );
+               }
+       );
        QUnit.test( 'Reset sorting making table appear unsorted', 3, function ( assert ) {
                var $table = tableCreate( header, initial );
                $table.tablesorter(
                        $table.find( '.headerSort:eq(0)' ).click();
                }
        );
+       tableTest( 'Sorting with colspanned headers: sort spanned column twice',
+               header,
+               initial,
+               [ caa4, bbc2, abc3, aab5, aaa1 ],
+               function ( $table ) {
+                       // Make colspanned header for test
+                       $table.find( 'tr:eq(0) th:eq(1), tr:eq(0) th:eq(2)' ).remove();
+                       $table.find( 'tr:eq(0) th:eq(0)' ).prop( 'colspan', '3' );
+                       $table.tablesorter();
+                       $table.find( '.headerSort:eq(0)' ).click();
+                       $table.find( '.headerSort:eq(0)' ).click();
+               }
+       );
        tableTest( 'Sorting with colspanned headers: subsequent column',
                header,
                initial,
                        $table.find( '.headerSort:eq(1)' ).click();
                }
        );
+       tableTest( 'Sorting with colspanned headers: sort subsequent column twice',
+               header,
+               initial,
+               [ aab5, caa4, abc3, bbc2, aaa1 ],
+               function ( $table ) {
+                       // Make colspanned header for test
+                       $table.find( 'tr:eq(0) th:eq(1), tr:eq(0) th:eq(2)' ).remove();
+                       $table.find( 'tr:eq(0) th:eq(0)' ).prop( 'colspan', '3' );
+                       $table.tablesorter();
+                       $table.find( '.headerSort:eq(1)' ).click();
+                       $table.find( '.headerSort:eq(1)' ).click();
+               }
+       );
  
        // Regression tests!
        tableTest(
                }
        );
  
 -      QUnit.test( 'Test detection routine', function ( assert ) {
 +      QUnit.test( 'Test detection routine', 1, function ( assert ) {
                var $table;
                $table = $(
                        '<table class="sortable">' +
        } );
  
        /** FIXME: the diff output is not very readeable. */
 -      QUnit.test( 'bug 32047 - caption must be before thead', function ( assert ) {
 +      QUnit.test( 'bug 32047 - caption must be before thead', 1, function ( assert ) {
                var $table;
                $table = $(
                        '<table class="sortable">' +
                );
        } );
  
 -      QUnit.test( 'data-sort-value attribute, when available, should override sorting position', function ( assert ) {
 +      QUnit.test( 'data-sort-value attribute, when available, should override sorting position', 3, function ( assert ) {
                var $table, data;
  
                // Example 1: All cells except one cell without data-sort-value,
                }
        );
  
 -      QUnit.test( 'Sorting images using alt text', function ( assert ) {
 +      QUnit.test( 'Sorting images using alt text', 1, function ( assert ) {
                var $table = $(
                        '<table class="sortable">' +
                                '<tr><th>THEAD</th></tr>' +
                );
        } );
  
 -      QUnit.test( 'Sorting images using alt text (complex)', function ( assert ) {
 +      QUnit.test( 'Sorting images using alt text (complex)', 1, function ( assert ) {
                var $table = $(
                        '<table class="sortable">' +
                                '<tr><th>THEAD</th></tr>' +
                );
        } );
  
 -      QUnit.test( 'Sorting images using alt text (with format autodetection)', function ( assert ) {
 +      QUnit.test( 'Sorting images using alt text (with format autodetection)', 1, function ( assert ) {
                var $table = $(
                        '<table class="sortable">' +
                                '<tr><th>THEAD</th></tr>' +