1
0
mirror of https://github.com/DataTables/DataTables.git synced 2025-01-18 11:52:11 +01:00

New: Cookie callback function (can be passed as an initialisation parameter) called fnCookieCallback, which allows the developer to modify the information stored in the cookie. Takes four arguments (sName, oData, sExpires, sPath) and expects a complete cookie string to be returned (with name, value, expires and path correctly formatted).

Updated: Now use jQuery's JSON parse function (in jQuery 1.4) rather than JSON.js

Fixed: State saving cookie used single quote strings in places (in correct JSON). This has now been addressed, and a work around in place (replace) until a sufficient period has passed
This commit is contained in:
Allan Jardine 2010-08-20 18:52:16 +01:00
parent 25148e4fec
commit 85b923cff3
2 changed files with 129 additions and 22 deletions

View File

@ -1215,6 +1215,13 @@
*/
this.sCookiePrefix = "SpryMedia_DataTables_";
/*
* Variable: fnCookieCallback
* Purpose: Callback function for cookie creation
* Scope: jQuery.dataTable.classSettings
*/
this.fnCookieCallback = null;
/*
* Variable: sAjaxSource
* Purpose: Source url for AJAX data for the table
@ -5628,7 +5635,7 @@
sValue += '"aaSorting": [ ';
for ( i=0 ; i<oSettings.aaSorting.length ; i++ )
{
sValue += "["+oSettings.aaSorting[i][0]+",'"+oSettings.aaSorting[i][1]+"'],";
sValue += '['+oSettings.aaSorting[i][0]+',"'+oSettings.aaSorting[i][1]+'"],';
}
sValue = sValue.substring(0, sValue.length-1);
sValue += "],";
@ -5636,8 +5643,8 @@
sValue += '"aaSearchCols": [ ';
for ( i=0 ; i<oSettings.aoPreSearchCols.length ; i++ )
{
sValue += "['"+oSettings.aoPreSearchCols[i].sSearch.replace("'","\'")+
"',"+!oSettings.aoPreSearchCols[i].bRegex+"],";
sValue += '["'+oSettings.aoPreSearchCols[i].sSearch.replace('"','\\"')+
'",'+!oSettings.aoPreSearchCols[i].bRegex+'],';
}
sValue = sValue.substring(0, sValue.length-1);
sValue += "],";
@ -5652,7 +5659,7 @@
sValue += "}";
_fnCreateCookie( oSettings.sCookiePrefix+oSettings.sInstance, sValue,
oSettings.iCookieDuration, oSettings.sCookiePrefix );
oSettings.iCookieDuration, oSettings.sCookiePrefix, oSettings.fnCookieCallback );
}
/*
@ -5673,22 +5680,13 @@
var sData = _fnReadCookie( oSettings.sCookiePrefix+oSettings.sInstance );
if ( sData !== null && sData !== '' )
{
/* Try/catch the JSON eval - if it is bad then we ignore it */
/* Try/catch the JSON eval - if it is bad then we ignore it - note that 1.7.0 and before
* incorrectly used single quotes for some strings - hence the replace below
*/
try
{
/* Use the JSON library for safety - if it is available */
if ( typeof JSON == 'object' && typeof JSON.parse == 'function' )
{
/* DT 1.4.0 used single quotes for a string - JSON.parse doesn't allow this and throws
* an error. So for now we can do this. This can be removed in future it is just to
* allow the tranfrer to 1.4.1+ to occur
*/
oData = JSON.parse( sData.replace(/'/g, '"') );
}
else
{
oData = eval( '('+sData+')' );
}
oData = (typeof $.parseJSON == 'function') ?
$.parseJSON( oData.replace(/'/g, '"') ) : eval( '('+sData+')' );
}
catch( e )
{
@ -5750,8 +5748,9 @@
* string:sValue - the value the cookie should take
* int:iSecs - duration of the cookie
* string:sBaseName - sName is made up of the base + file name - this is the base
* function:fnCallback - User definable function to modify the cookie
*/
function _fnCreateCookie ( sName, sValue, iSecs, sBaseName )
function _fnCreateCookie ( sName, sValue, iSecs, sBaseName, fnCallback )
{
var date = new Date();
date.setTime( date.getTime()+(iSecs*1000) );
@ -5764,9 +5763,20 @@
*/
var aParts = window.location.pathname.split('/');
var sNameFile = sName + '_' + aParts.pop().replace(/[\/:]/g,"").toLowerCase();
var sFullCookie = sNameFile + "=" + encodeURIComponent(sValue) +
"; expires=" + date.toGMTString() +
"; path=" + aParts.join('/') + "/";
var sFullCookie;
if ( fnCallback != null )
{
var oData = (typeof $.parseJSON == 'function') ?
$.parseJSON( sValue ) : eval( '('+sData+')' );
sFullCookie = fnCallback( sNameFile, oData, date.toGMTString(),
aParts.join('/')+"/" );
}
else
{
sFullCookie = sNameFile + "=" + encodeURIComponent(sValue) +
"; expires=" + date.toGMTString() +"; path=" + aParts.join('/')+"/";
}
/* Are we going to go over the cookie limit of 4KiB? If so, try to delete a cookies
* belonging to DataTables. This is FAR from bullet proof
@ -6195,6 +6205,7 @@
_fnMap( oSettings, oInit, "fnRowCallback" );
_fnMap( oSettings, oInit, "fnHeaderCallback" );
_fnMap( oSettings, oInit, "fnFooterCallback" );
_fnMap( oSettings, oInit, "fnCookieCallback" );
_fnMap( oSettings, oInit, "fnInitComplete" );
_fnMap( oSettings, oInit, "fnServerData" );
_fnMap( oSettings, oInit, "fnFormatNumber" );

View File

@ -0,0 +1,96 @@
// DATA_TEMPLATE: dom_data
oTest.fnStart( "Cookie callback" );
$(document).ready( function () {
var mPass;
/* Note that in order to be fully effective here for saving state, there would need to be a
* stringify function to serialise the data array
*/
oTest.fnTest(
"null by default",
function () {
$('#example').dataTable();
},
function () { return $('#example').dataTable().fnSettings().fnCookieCallback == null; }
);
oTest.fnTest(
"Number of arguments",
function () {
$('#example').dataTable( {
"bDestroy": true,
"bStateSave": true,
"fnCookieCallback": function (sName, oData, sExpires, sPath) {
mPass = arguments.length;
return sName + "=; expires=" + sExpires +"; path=" + sPath;
}
} );
},
function () { return mPass == 4; }
);
oTest.fnTest(
"Name",
function () {
$('#example').dataTable( {
"bDestroy": true,
"bStateSave": true,
"fnCookieCallback": function (sName, oData, sExpires, sPath) {
mPass = sName=="SpryMedia_DataTables_example_dom_data.php";
return sName + "=; expires=" + sExpires +"; path=" + sPath;
}
} );
},
function () { return mPass; }
);
oTest.fnTest(
"Data",
function () {
$('#example').dataTable( {
"bDestroy": true,
"bStateSave": true,
"fnCookieCallback": function (sName, oData, sExpires, sPath) {
mPass = typeof oData.iStart != 'undefined';
return sName + "=; expires=" + sExpires +"; path=" + sPath;
}
} );
},
function () { return mPass; }
);
oTest.fnTest(
"Expires",
function () {
$('#example').dataTable( {
"bDestroy": true,
"bStateSave": true,
"fnCookieCallback": function (sName, oData, sExpires, sPath) {
mPass = sExpires.match(/GMT/);
return sName + "=; expires=" + sExpires +"; path=" + sPath;
}
} );
},
function () { return mPass; }
);
oTest.fnTest(
"Path",
function () {
$('#example').dataTable( {
"bDestroy": true,
"bStateSave": true,
"fnCookieCallback": function (sName, oData, sExpires, sPath) {
mPass = sPath.match(/media\/unit_testing\/templates/);
return sName + "=; expires=" + sExpires +"; path=" + sPath;
}
} );
},
function () { return mPass; }
);
oTest.fnComplete();
} );