From 491322645668cf07ba0d7f890a18839810907c18 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Thu, 16 May 2013 17:16:04 +0100 Subject: [PATCH] Internal: Refactoring the sort functions for invalidation - invalidation is going to play an important part in the new API, with the ability to invalid the cached data for sorting, filtering, display etc so we need to be able tos upport this in the core. - In fairness the sorting didn't actually need invalidation because it would always get data on every sort, which is bad for performance - proper invalidation and caching will resolve this. Which is what has been implemented here. - I had expected (hoped) to be able to save a bit of space in the refactor, but only aorund 100 bytes (compressed) saved, which will probably be lost when the invalidation is fully implemented in the API. Still, better performance, tidier code, and no extra space... Fix: Sorting classes now show multi column sorting when a column is defined to sort over multiple columns (sortData). Previously it would only show the first column. --- media/src/api/api.row.js | 6 + media/src/core/core.sort.js | 227 +++++++++++++++--------------- media/src/model/model.defaults.js | 2 +- media/src/model/model.row.js | 4 +- media/src/model/model.settings.js | 6 +- 5 files changed, 127 insertions(+), 118 deletions(-) diff --git a/media/src/api/api.row.js b/media/src/api/api.row.js index c8258352..ae9a6c51 100644 --- a/media/src/api/api.row.js +++ b/media/src/api/api.row.js @@ -45,8 +45,14 @@ _api.register( 'row().data()', function ( data ) { // return undefined; // @todo - Set operator + ctx[0].aoData[ this[0] ]._aData = data; + + // Invalidate the row } ); +// Should row object have an invalidated flag? Scan the array before doing +// anything with the data? or better to update at the invalidation point + _api.register( 'row().index()', function () { return this.length ? this[0] : undefined; diff --git a/media/src/core/core.sort.js b/media/src/core/core.sort.js index d913723b..6ea0ee0c 100644 --- a/media/src/core/core.sort.js +++ b/media/src/core/core.sort.js @@ -1,3 +1,37 @@ + + +function _fnSortFlatten ( settings ) +{ + var + i, iLen, k, kLen, + aSort = [], + aiOrig = [], + aoColumns = settings.aoColumns, + aDataSort, iCol, sType, + nestedSort = settings.aaSortingFixed.concat( settings.aaSorting ); + + for ( i=0 ; iy ? 1 : 0; - if ( test !== 0 ) - { + if ( test !== 0 ) { return sort.dir === 'asc' ? test : -test; } } - + x = aiOrig[a]; y = aiOrig[b]; return xy ? 1 : 0; @@ -162,34 +129,32 @@ function _fnSort ( oSettings, bApplyClasses ) // Depreciated - remove in 1.11 (providing a plug-in option) // Not all sort types have formatting methods, so we have to call their sorting // methods. - oSettings.aiDisplayMaster.sort( function ( a, b ) { + displayMaster.sort( function ( a, b ) { var x, y, k, l, test, sort, len=aSort.length, dataA = aoData[a]._aSortData, dataB = aoData[b]._aSortData; - for ( k=0 ; ky ? 1 : 0; } ); } } - + /* Alter the sorting classes to take account of the changes */ if ( (bApplyClasses === undefined || bApplyClasses) && !oSettings.oFeatures.bDeferRender ) { @@ -202,14 +167,14 @@ function _fnSort ( oSettings, bApplyClasses ) nTh = aoColumns[i].nTh; nTh.removeAttribute('aria-sort'); nTh.removeAttribute('aria-label'); - + /* In ARIA only the first sorting column can be marked as sorting - no multi-sort option */ if ( aoColumns[i].bSortable ) { if ( aSort.length > 0 && aSort[0].col == i ) { nTh.setAttribute('aria-sort', aSort[0].dir=="asc" ? "ascending" : "descending" ); - + var nextSort = (aoColumns[i].asSorting[ aSort[0].index+1 ]) ? aoColumns[i].asSorting[ aSort[0].index+1 ] : aoColumns[i].asSorting[0]; nTh.setAttribute('aria-label', sTitle+ @@ -226,7 +191,7 @@ function _fnSort ( oSettings, bApplyClasses ) nTh.setAttribute('aria-label', sTitle); } } - + /* Tell the draw function that we have sorted the data */ oSettings.bSorted = true; $(oSettings.oInstance).trigger('sort', oSettings); @@ -374,14 +339,7 @@ function _fnSortingClasses( oSettings ) } } - if ( oSettings.aaSortingFixed !== null ) - { - aaSort = oSettings.aaSortingFixed.concat( oSettings.aaSorting ); - } - else - { - aaSort = oSettings.aaSorting.slice(); - } + aaSort = _fnSortFlatten( oSettings ); /* Apply the required classes to the header */ for ( i=0 ; i