From 6a7a7151b8932d024e359a66ed2f7db41c7a9ec0 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Mon, 5 Dec 2011 09:21:20 +0000 Subject: [PATCH] Alter the extending of the initialisation parameter with the defaults a little such that objects are deep copied and arrays are not (code needs tidied a little - want to get it passing all unit tests first). The thing here is that we can't use jQuery's $.extend for a full deep copy since we don't want to deep copy arrays (for example aaSorting, where the default sort would always be applied), but we do want to deep copy objects (and not just take a reference to the default object). Thus we can't use $.extend :-(. --- media/js/jquery.dataTables.js | 87 ++++++++++++++++++++++-------- media/src/DataTables.js | 2 +- media/src/core/core.constructor.js | 49 ++++++++++++++--- media/src/core/core.init.js | 36 +++++++------ 4 files changed, 130 insertions(+), 44 deletions(-) diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index 7580c0a8..5c37d968 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -57,7 +57,7 @@ * } ); * } ); */ - var DataTable = function( oInit ) + window.DataTable = function( oInit ) { @@ -2507,6 +2507,27 @@ } + // xxx - needs to be called for Ajax as well + function _fnLanguageCompat( oLanguage ) + { + /* Backwards compatibility - if there is no sEmptyTable given, then use the same as + * sZeroRecords - assuming that is given. + */ + if ( typeof oLanguage.sEmptyTable == 'undefined' && + typeof oLanguage.sZeroRecords != 'undefined' ) + { + _fnMap( oLanguage, oLanguage, 'sZeroRecords', 'sEmptyTable' ); + } + + /* Likewise with loading records */ + if ( typeof oLanguage.sLoadingRecords == 'undefined' && + typeof oLanguage.sZeroRecords != 'undefined' ) + { + _fnMap( oLanguage, oLanguage, 'sZeroRecords', 'sLoadingRecords' ); + } + } + + /** * Copy language variables from remote object to a local one * @param {object} oSettings dataTables settings object @@ -2518,21 +2539,6 @@ { oSettings.oLanguage = $.extend( true, oSettings.oLanguage, oLanguage ); - /* Backwards compatibility - if there is no sEmptyTable given, then use the same as - * sZeroRecords - assuming that is given. - */ - if ( typeof oLanguage.sEmptyTable == 'undefined' && - typeof oLanguage.sZeroRecords != 'undefined' ) - { - _fnMap( oSettings.oLanguage, oLanguage, 'sZeroRecords', 'sEmptyTable' ); - } - - /* Likewise with loading records */ - if ( typeof oLanguage.sLoadingRecords == 'undefined' && - typeof oLanguage.sZeroRecords != 'undefined' ) - { - _fnMap( oSettings.oLanguage, oLanguage, 'sZeroRecords', 'sLoadingRecords' ); - } if ( bInit ) { @@ -5773,13 +5779,50 @@ oSettings.sDestroyWidth = $(this).width(); + if (typeof oInit === 'undefined' || oInit === null) + { + oInit = {}; + }; + + // Need a backwards compatibility function for mapping old parameters to the new - specifically + // the language records. Perhaps should have an event? Sounds sensible... + + if ( typeof oInit.oLanguage != 'undefined' ) { + _fnLanguageCompat( oInit.oLanguage ); + } + + var oFullInit = $.extend( true, {}, DataTable.models.oInit ); + + for ( var initProp in oFullInit ) { + if ( oFullInit.hasOwnProperty(initProp) ) { + if ( typeof oInit[initProp] != 'undefined' ) { + if ( typeof oInit[initProp] == 'object' && $.isArray(oInit[initProp]) === false ) { + $.extend( true, oFullInit[initProp], oInit[initProp] ); + } else { + oFullInit[initProp] = oInit[initProp]; + } + } + } + } + + oInit = oFullInit; + + // Can't use deep extend as we don't want to copy the array values + // Can't use shallow extend as that copies object references (i.e. the default object IS the object in use) + + + + /* Store the initialisation object that was passed in, useful for debugging */ - oSettings.oInit = oInit; - - oInit = (typeof oInit === 'undefined' || oInit === null) ? - $.extend( true, {}, DataTable.models.oInit ) : - $.extend( true, {}, DataTable.models.oInit, oInit ); - + //oSettings.oInit = oInit; + // + //console.log( DataTable.models.oInit.oSearch.sSearch ); + //oInit = (typeof oInit === 'undefined' || oInit === null) ? + // $.extend( {}, DataTable.models.oInit ) : + // $.extend( {}, DataTable.models.oInit, oInit ); + //console.log( oInit.oSearch === DataTable.models.oInit.oSearch ); + //console.log( oInit.oSearch === DataTable.models.oSearch ); + //console.log( DataTable.models.oSearch === DataTable.models.oInit.oSearch ); _fnMap( oSettings.oFeatures, oInit, "bPaginate" ); _fnMap( oSettings.oFeatures, oInit, "bLengthChange" ); diff --git a/media/src/DataTables.js b/media/src/DataTables.js index 654d767b..d7422696 100644 --- a/media/src/DataTables.js +++ b/media/src/DataTables.js @@ -57,7 +57,7 @@ * } ); * } ); */ - var DataTable = function( oInit ) + window.DataTable = function( oInit ) { require('core.columns.js'); require('core.data.js'); diff --git a/media/src/core/core.constructor.js b/media/src/core/core.constructor.js index 4ae8c0c2..323d24f1 100644 --- a/media/src/core/core.constructor.js +++ b/media/src/core/core.constructor.js @@ -84,13 +84,50 @@ oSettings.oApi = _that.oApi; oSettings.sDestroyWidth = $(this).width(); +if (typeof oInit === 'undefined' || oInit === null) +{ + oInit = {}; +}; + +// Need a backwards compatibility function for mapping old parameters to the new - specifically +// the language records. Perhaps should have an event? Sounds sensible... + +if ( typeof oInit.oLanguage != 'undefined' ) { + _fnLanguageCompat( oInit.oLanguage ); +} + +var oFullInit = $.extend( true, {}, DataTable.models.oInit ); + +for ( var initProp in oFullInit ) { + if ( oFullInit.hasOwnProperty(initProp) ) { + if ( typeof oInit[initProp] != 'undefined' ) { + if ( typeof oInit[initProp] == 'object' && $.isArray(oInit[initProp]) === false ) { + $.extend( true, oFullInit[initProp], oInit[initProp] ); + } else { + oFullInit[initProp] = oInit[initProp]; + } + } + } +} + +oInit = oFullInit; + +// Can't use deep extend as we don't want to copy the array values +// Can't use shallow extend as that copies object references (i.e. the default object IS the object in use) + + + + /* Store the initialisation object that was passed in, useful for debugging */ -oSettings.oInit = oInit; - -oInit = (typeof oInit === 'undefined' || oInit === null) ? - $.extend( true, {}, DataTable.models.oInit ) : - $.extend( true, {}, DataTable.models.oInit, oInit ); - +//oSettings.oInit = oInit; +// +//console.log( DataTable.models.oInit.oSearch.sSearch ); +//oInit = (typeof oInit === 'undefined' || oInit === null) ? +// $.extend( {}, DataTable.models.oInit ) : +// $.extend( {}, DataTable.models.oInit, oInit ); +//console.log( oInit.oSearch === DataTable.models.oInit.oSearch ); +//console.log( oInit.oSearch === DataTable.models.oSearch ); +//console.log( DataTable.models.oSearch === DataTable.models.oInit.oSearch ); _fnMap( oSettings.oFeatures, oInit, "bPaginate" ); _fnMap( oSettings.oFeatures, oInit, "bLengthChange" ); diff --git a/media/src/core/core.init.js b/media/src/core/core.init.js index b1288c02..0d2c51c8 100644 --- a/media/src/core/core.init.js +++ b/media/src/core/core.init.js @@ -131,6 +131,27 @@ function _fnInitComplete ( oSettings, json ) } +// xxx - needs to be called for Ajax as well +function _fnLanguageCompat( oLanguage ) +{ + /* Backwards compatibility - if there is no sEmptyTable given, then use the same as + * sZeroRecords - assuming that is given. + */ + if ( typeof oLanguage.sEmptyTable == 'undefined' && + typeof oLanguage.sZeroRecords != 'undefined' ) + { + _fnMap( oLanguage, oLanguage, 'sZeroRecords', 'sEmptyTable' ); + } + + /* Likewise with loading records */ + if ( typeof oLanguage.sLoadingRecords == 'undefined' && + typeof oLanguage.sZeroRecords != 'undefined' ) + { + _fnMap( oLanguage, oLanguage, 'sZeroRecords', 'sLoadingRecords' ); + } +} + + /** * Copy language variables from remote object to a local one * @param {object} oSettings dataTables settings object @@ -142,21 +163,6 @@ function _fnLanguageProcess( oSettings, oLanguage, bInit ) { oSettings.oLanguage = $.extend( true, oSettings.oLanguage, oLanguage ); - /* Backwards compatibility - if there is no sEmptyTable given, then use the same as - * sZeroRecords - assuming that is given. - */ - if ( typeof oLanguage.sEmptyTable == 'undefined' && - typeof oLanguage.sZeroRecords != 'undefined' ) - { - _fnMap( oSettings.oLanguage, oLanguage, 'sZeroRecords', 'sEmptyTable' ); - } - - /* Likewise with loading records */ - if ( typeof oLanguage.sLoadingRecords == 'undefined' && - typeof oLanguage.sZeroRecords != 'undefined' ) - { - _fnMap( oSettings.oLanguage, oLanguage, 'sZeroRecords', 'sLoadingRecords' ); - } if ( bInit ) {