diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index 3c810653..ae621583 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -21,7 +21,7 @@ */ /*jslint evil: true, undef: true, browser: true */ -/*globals $,require,jQuery,define,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageCompat,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnServerParams,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnApplyColumnDefs,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnNodeToColumnIndex,_fnInfoMacros,_fnBrowserDetect,_fnGetColumns,_fnHungarianMap,_fnCamelToHungarian*/ +/*globals $,require,jQuery,define,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageCompat,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnApplyColumnDefs,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnNodeToColumnIndex,_fnInfoMacros,_fnBrowserDetect,_fnGetColumns,_fnHungarianMap,_fnCamelToHungarian,_fnBuildAjax,_fnAjaxDataSrc*/ (/** @lends */function( window, document, undefined ) { @@ -1862,6 +1862,103 @@ } + + /** + * Create an Ajax call based on the table's settings, taking into account that + * parameters can have multiple forms, and backwards compatibility. + * + * @param {object} oSettings dataTables settings object + * @param {array} data Data to send to the server, required by + * DataTables - may be augmented by developer callbacks + * @param {function} fn Callback function to run when data is obtained + */ + function _fnBuildAjax( oSettings, data, fn ) + { + // Compatibility with 1.9-, allow fnServerData and event to manipulate + _fnCallbackFire( oSettings, 'aoServerParams', 'serverParams', [data] ); + + var ajaxData; + + if ( $.isPlainObject( oSettings.ajax ) && oSettings.ajax.data ) + { + ajaxData = oSettings.ajax.data; + var newData = $.isFunction( ajaxData ) ? + ajaxData( data ) : // fn can manipulate data or return an object or array + ajaxData; // object or array to merge + + if ( $.isArray( newData ) ) + { + // name value pair objects in an array + data = data.concat( newData ); + } + else if ( $.isPlainObject( newData ) ) + { + // aData is an array of name value pairs at this point - convert to + // an object to easily merge data - jQuery will cope with the switch + var oData = {}; + $.each( data, function (key, val) { + oData[val.name] = val.value; + } ); + + data = $.extend( true, oData, newData ); + } + + // Remove the data property as we've resolved it already + delete oSettings.ajax.data; + } + + var baseAjax = { + "data": data, + "success": function (json) { + if ( json.sError ) { + oSettings.oApi._fnLog( oSettings, 0, json.sError ); + } + + $(oSettings.oInstance).trigger('xhr', [oSettings, json]); + fn( json ); + }, + "dataType": "json", + "cache": false, + "type": oSettings.sServerMethod, + "error": function (xhr, error, thrown) { + if ( error == "parsererror" ) { + oSettings.oApi._fnLog( oSettings, 0, "DataTables: invalid JSON response" ); + } + } + }; + + if ( oSettings.fnServerData ) + { + // DataTables 1.9- compatibility + oSettings.fnServerData.call( oSettings.oInstance, + oSettings.sAjaxSource, data, fn, oSettings + ); + } + else if ( oSettings.sAjaxSource || typeof oSettings.ajax === 'string' ) + { + // DataTables 1.9- compatibility + oSettings.jqXHR = $.ajax( $.extend( baseAjax, { + url: oSettings.ajax || oSettings.sAjaxSource + } ) ); + } + else if ( $.isFunction( oSettings.ajax ) ) + { + // Is a function - let the caller define what needs to be done + oSettings.jqXHR = oSettings.ajax.call( oSettings.oInstance, + data, fn, oSettings + ); + } + else + { + // Object to extend the base settings + oSettings.jqXHR = $.ajax( $.extend( baseAjax, oSettings.ajax ) ); + + // Restore for next time around + oSettings.ajax.data = ajaxData; + } + } + + /** * Update the table using an Ajax call * @param {object} oSettings dataTables settings object @@ -1876,12 +1973,11 @@ _fnProcessingDisplay( oSettings, true ); var iColumns = oSettings.aoColumns.length; var aoData = _fnAjaxParameters( oSettings ); - _fnServerParams( oSettings, aoData ); - - oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aoData, - function(json) { - _fnAjaxUpdateDraw( oSettings, json ); - }, oSettings ); + + _fnBuildAjax( oSettings, aoData, function(json) { + _fnAjaxUpdateDraw( oSettings, json ); + }, oSettings ); + return false; } return true; @@ -1958,18 +2054,6 @@ } - /** - * Add Ajax parameters from plug-ins - * @param {object} oSettings dataTables settings object - * @param array {objects} aoData name/value pairs to send to the server - * @memberof DataTable#oApi - */ - function _fnServerParams( oSettings, aoData ) - { - _fnCallbackFire( oSettings, 'aoServerParams', 'serverParams', [aoData] ); - } - - /** * Data the data from the server (nuking the old) and redraw the table * @param {object} oSettings dataTables settings object @@ -2002,7 +2086,7 @@ oSettings._iRecordsTotal = parseInt(json.iTotalRecords, 10); oSettings._iRecordsDisplay = parseInt(json.iTotalDisplayRecords, 10); - var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ); + var aData = _fnAjaxDataSrc( oSettings, json ); for ( var i=0, iLen=aData.length ; itr>td.'+oSettings.oClasses.sRowEmpty, oSettings.nTable).parent().remove(); @@ -6050,7 +6156,6 @@ "_fnAjaxUpdate": _fnAjaxUpdate, "_fnAjaxParameters": _fnAjaxParameters, "_fnAjaxUpdateDraw": _fnAjaxUpdateDraw, - "_fnServerParams": _fnServerParams, "_fnAddOptionsHtml": _fnAddOptionsHtml, "_fnFeatureHtmlTable": _fnFeatureHtmlTable, "_fnScrollDraw": _fnScrollDraw, @@ -6117,7 +6222,9 @@ "_fnBrowserDetect": _fnBrowserDetect, "_fnGetColumns": _fnGetColumns, "_fnHungarianMap": _fnHungarianMap, - "_fnCamelToHungarian": _fnCamelToHungarian + "_fnCamelToHungarian": _fnCamelToHungarian, + "_fnBuildAjax": _fnBuildAjax, + "_fnAjaxDataSrc": _fnAjaxDataSrc }; $.extend( DataTable.ext.oApi, this.oApi ); @@ -6255,6 +6362,7 @@ _fnMap( oSettings.oScroll, oInit, "iScrollLoadGap", "iLoadGap" ); _fnMap( oSettings.oScroll, oInit, "bScrollAutoCss", "bAutoCss" ); _fnMap( oSettings, oInit, "asStripeClasses" ); + _fnMap( oSettings, oInit, "ajax" ); _fnMap( oSettings, oInit, "fnServerData" ); _fnMap( oSettings, oInit, "fnFormatNumber" ); _fnMap( oSettings, oInit, "sServerMethod" ); @@ -6543,7 +6651,7 @@ _fnAddData( oSettings, oInit.aaData[ i ] ); } } - else if ( oSettings.bDeferLoading || oSettings.sAjaxSource === null ) + else if ( oSettings.bDeferLoading || (oSettings.sAjaxSource === null && oSettings.ajax === null) ) { /* Grab the data from the page - only do this when deferred loading or no Ajax * source since there is no point in reading the DOM data if we are then going @@ -7683,6 +7791,161 @@ "aaSortingFixed": null, + /** + * DataTables can be instructed to load data to display in the table from a + * Ajax source. This option defines how that Ajax call is made and where to. + * + * The `ajax` property has three different modes of operation, depending on + * how it is defined. These are: + * + * * `string` - Set the URL from where the data should be loaded from. + * * `object` - Define properties for `jQuery.ajax`. + * * `function` - Custom data get function + * + * `string` + * -------- + * + * As a string, the `ajax` property simply defines the URL from which + * DataTables will load data. + * + * `object` + * -------- + * + * As an object, the parameters in the object are passed to + * [jQuery.ajax](http://api.jquery.com/jQuery.ajax/) allowing fine control + * of the Ajax request. DataTables has a number of default parameters which + * you can override using this option. Please refer to the jQuery + * documentation for a full description of the options available, although + * the following parameters provide additional options in DataTables or + * require special consideration: + * + * * `data` - As with jQuery, `data` can be provided as an object or array + * of name/value pairs, but it can also be used as a function to + * manipulate the data DataTables sends to the server. The function takes + * a single parameter, an array of name/value pairs that DataTables has + * readied for sending. An object or array can be returned which will be + * merged into the DataTables defaults, or you can add the items to the + * array passed in. This supersedes `fnServerParams` from DataTables 1.9-. + * + * * `dataSrc` - By default DataTables will look for the property 'aaData' + * when obtaining data from an Ajax source or for server-side processing - + * this parameter allows that property to be changed. You can use + * Javascript dotted object notation to get a data source for multiple + * levels of nesting, or it my be used as a function. As a function it + * takes a single parameter, the JSON returned from the server, which can + * be manipulated as required, with the returned value being that used by + * DataTables as the data source for the table. This supersedes + * `sAjaxDataProp` from DataTables 1.9-. + * + * * `success` - Should not be overridden it is used internally in + * DataTables. To manipulate the data returned by the server use + * `ajax.dataSrc`, or use `ajax` as a function (see below). + * + * `function` + * ---------- + * + * As a function, making the Ajax call is left up to yourself allowing + * complete control of the Ajax request. Indeed, if desired, a method other + * than Ajax could be used to obtain the required data, such as Web storage + * or an AIR database. + * + * The function is given four parameters and no return is required. The + * parameters are: + * + * 1. _object_ - Data to send to the server + * 2. _function_ - Callback function that must be executed when the required + * data has been obtained. That data should be passed into the callback + * as the only parameter + * 3. _object_ - DataTables settings object for the table + * + * Note that this supersedes `fnServerData` from DataTables 1.9-. + * + * @type string|object|function + * @default null + * + * @dtopt Option + * @name DataTable.defaults.ajax + * @since 1.10.0 + * + * @example + * // Get JSON data from a file via Ajax. + * // Note DataTables expects data in the form `{ aaData: [ ...data... ] }` by default). + * $('#example').dataTable( { + * "ajax": "data.json" + * } ); + * + * @example + * // Get JSON data from a file via Ajax, using `dataSrc` to change + * // `aaData` to `tableData` (i.e. `{ aaData: [ ...data... ] }`) + * $('#example').dataTable( { + * "ajax": { + * "url": "data.json", + * "dataSrc": "tableData" + * } + * } ); + * + * @example + * // Get JSON data from a file via Ajax, using `dataSrc` to read data + * // from a plain array rather than an array in an object + * $('#example').dataTable( { + * "ajax": { + * "url": "data.json", + * "dataSrc": "" + * } + * } ); + * + * @example + * // Manipulate the data returned from the server - add a link to data + * // (note this can, should, be done using `render` for the column - this + * // is just a simple example of how the data can be manipulated). + * $('#example').dataTable( { + * "ajax": { + * "url": "data.json", + * "dataSrc": function ( json ) { + * for ( var i=0, ien=json.length ; iView message'; + * } + * return json; + * } + * } + * } ); + * + * @example + * // Add data to the request + * $('#example').dataTable( { + * "ajax": { + * "url": "data.json", + * "data": function ( d ) { + * return { + * "extra_search": $('#extra').val() + * }; + * } + * } + * } ); + * + * @example + * // Send request as POST + * $('#example').dataTable( { + * "ajax": { + * "url": "data.json", + * "type": "POST" + * } + * } ); + * + * @example + * // Get the data from localStorage (could interface with a form for + * // adding, editing and removing rows). + * $('#example').dataTable( { + * "ajax": function (data, callback, settings) { + * callback( + * JSON.parse( localStorage.getItem('dataTablesData') ) + * ); + * } + * } ); + */ + "ajax": null, + + /** * This parameter allows you to readily specify the entries in the length drop * down menu that DataTables shows when pagination is enabled. It can be @@ -8109,7 +8372,7 @@ * $(document).ready( function () { * $('#example').dataTable( { * "serverSide": true, - * "ajaxSource": "xhr.php" + * "ajax": "xhr.php" * } ); * } ); */ @@ -8478,13 +8741,16 @@ /** + * __Deprecated__ The functionality provided by this parameter has now been + * superseded by that provided through `ajax`, which should be used instead. + * * This parameter allows you to override the default function which obtains - * the data from the server so something more suitable for your application. For - * example you could use POST data, or pull information from a Gears or AIR - * database. + * the data from the server so something more suitable for your application. + * For example you could use POST data, or pull information from a Gears or + * AIR database. * @type function * @member - * @param {string} source HTTP source to obtain the data from (`ajaxSource`) + * @param {string} source HTTP source to obtain the data from (`ajax`) * @param {array} data A key/value pair object containing the data to send * to the server * @param {function} callback to be called on completion of the data get @@ -8494,54 +8760,17 @@ * @dtopt Callbacks * @dtopt Server-side * @name DataTable.defaults.serverData - * - * @example - * // POST data to server (note you can use `serverMethod` to set the - * // HTTP method is that is all you want to use `serverData` for. - * $(document).ready( function() { - * $('#example').dataTable( { - * "processing": true, - * "serverSide": true, - * "ajaxSource": "xhr.php", - * "serverData": function ( source, data, callback, settings ) { - * settings.jqXHR = $.ajax( { - * "dataType": 'json', - * "type": "POST", - * "url": source, - * "data": data, - * "success": callback - * } ); - * } - * } ); - * } ); + * + * @deprecated 1.10. Please use `ajax` for this functionality now. */ - "fnServerData": function ( url, data, callback, settings ) { - settings.jqXHR = $.ajax( { - "url": url, - "data": data, - "success": function (json) { - if ( json.sError ) { - settings.oApi._fnLog( settings, 0, json.sError ); - } - - $(settings.oInstance).trigger('xhr', [settings, json]); - callback( json ); - }, - "dataType": "json", - "cache": false, - "type": settings.sServerMethod, - "error": function (xhr, error, thrown) { - if ( error == "parsererror" ) { - settings.oApi._fnLog( settings, 0, "DataTables warning: JSON data from "+ - "server could not be parsed. This is caused by a JSON formatting error." ); - } - } - } ); - }, + "fnServerData": null, /** - * It is often useful to send extra data to the server when making an Ajax + * __Deprecated__ The functionality provided by this parameter has now been + * superseded by that provided through `ajax`, which should be used instead. + * + * It is often useful to send extra data to the server when making an Ajax * request - for example custom filtering information, and this callback * function makes it trivial to send extra information to the server. The * passed in parameter is the data set that has been constructed by @@ -8558,18 +8787,8 @@ * @dtopt Callbacks * @dtopt Server-side * @name DataTable.defaults.serverParams - * - * @example - * $(document).ready( function() { - * $('#example').dataTable( { - * "processing": true, - * "serverSide": true, - * "ajaxSource": "scripts/server_processing.php", - * "serverParams": function ( data ) { - * data.push( { "name": "more_data", "value": "my_value" } ); - * } - * } ); - * } ); + * + * @deprecated 1.10. Please use `ajax` for this functionality now. */ "fnServerParams": null, @@ -8789,7 +9008,7 @@ * $(document).ready( function() { * $('#example').dataTable( { * "serverSide": true, - * "ajaxSource": "scripts/server_processing.php", + * "ajax": "scripts/server_processing.php", * "deferLoading": 57 * } ); * } ); @@ -8799,7 +9018,7 @@ * $(document).ready( function() { * $('#example').dataTable( { * "serverSide": true, - * "ajaxSource": "scripts/server_processing.php", + * "ajax": "scripts/server_processing.php", * "deferLoading": [ 57, 100 ], * "search": { * "search": "my_filter" @@ -9390,6 +9609,9 @@ /** + * __Deprecated__ The functionality provided by this parameter has now been + * superseded by that provided through `ajax`, which should be used instead. + * * By default DataTables will look for the property 'aaData' when obtaining * data from an Ajax source or for server-side processing - this parameter * allows that property to be changed. You can use Javascript dotted object @@ -9400,46 +9622,29 @@ * @dtopt Options * @dtopt Server-side * @name DataTable.defaults.ajaxDataProp - * - * @example - * // Get data from { "data": [...] } - * $(document).ready( function() { - * var oTable = $('#example').dataTable( { - * "ajaxSource": "sources/data.txt", - * "ajaxDataProp": "data" - * } ); - * } ); - * - * @example - * // Get data from { "data": { "inner": [...] } } - * $(document).ready( function() { - * var oTable = $('#example').dataTable( { - * "ajaxSource": "sources/data.txt", - * "ajaxDataProp": "data.inner" - * } ); - * } ); + * + * @deprecated 1.10. Please use `ajax` for this functionality now. */ "sAjaxDataProp": "aaData", /** - * You can instruct DataTables to load data from an external source using this - * parameter (use aData if you want to pass data in you already have). Simply - * provide a url a JSON object can be obtained from. This object must include - * the parameter `aaData` which is the data source for the table. + * __Deprecated__ The functionality provided by this parameter has now been + * superseded by that provided through `ajax`, which should be used instead. + * + * You can instruct DataTables to load data from an external + * source using this parameter (use aData if you want to pass data in you + * already have). Simply provide a url a JSON object can be obtained from. + * This object must include the parameter `aaData` which is the data source + * for the table. * @type string * @default null * * @dtopt Options * @dtopt Server-side * @name DataTable.defaults.ajaxSource - * - * @example - * $(document).ready( function() { - * $('#example').dataTable( { - * "ajaxSource": "/dataTables/json.php" - * } ); - * } ) + * + * @deprecated 1.10. Please use `ajax` for this functionality now. */ "sAjaxSource": null, @@ -9590,6 +9795,9 @@ /** + * __Deprecated__ The functionality provided by this parameter has now been + * superseded by that provided through `ajax`, which should be used instead. + * * Set the HTTP method that is used to make the Ajax call for server-side * processing or Ajax sourced data. * @type string @@ -9598,15 +9806,8 @@ * @dtopt Options * @dtopt Server-side * @name DataTable.defaults.serverMethod - * - * @example - * $(document).ready( function() { - * $('#example').dataTable( { - * "serverSide": true, - * "ajaxSource": "scripts/post.php", - * "serverMethod": "POST" - * } ); - * } ); + * + * @deprecated 1.10. Please use `ajax` for this functionality now. */ "sServerMethod": "GET" }; @@ -10737,6 +10938,10 @@ "bScrollbarLeft": false }, + + "ajax": null, + + /** * Array referencing the nodes which are used for the features. The * parameters of this object match what is allowed by sDom - i.e. diff --git a/media/src/DataTables.js b/media/src/DataTables.js index 9e289393..8f950c32 100644 --- a/media/src/DataTables.js +++ b/media/src/DataTables.js @@ -21,7 +21,7 @@ */ /*jslint evil: true, undef: true, browser: true */ -/*globals $,require,jQuery,define,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageCompat,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnServerParams,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnApplyColumnDefs,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnNodeToColumnIndex,_fnInfoMacros,_fnBrowserDetect,_fnGetColumns,_fnHungarianMap,_fnCamelToHungarian*/ +/*globals $,require,jQuery,define,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageCompat,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnApplyColumnDefs,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnNodeToColumnIndex,_fnInfoMacros,_fnBrowserDetect,_fnGetColumns,_fnHungarianMap,_fnCamelToHungarian,_fnBuildAjax,_fnAjaxDataSrc*/ (/** @lends */function( window, document, undefined ) { diff --git a/media/src/api/api.internal.js b/media/src/api/api.internal.js index 99a9f421..5dede528 100644 --- a/media/src/api/api.internal.js +++ b/media/src/api/api.internal.js @@ -45,7 +45,6 @@ this.oApi = { "_fnAjaxUpdate": _fnAjaxUpdate, "_fnAjaxParameters": _fnAjaxParameters, "_fnAjaxUpdateDraw": _fnAjaxUpdateDraw, - "_fnServerParams": _fnServerParams, "_fnAddOptionsHtml": _fnAddOptionsHtml, "_fnFeatureHtmlTable": _fnFeatureHtmlTable, "_fnScrollDraw": _fnScrollDraw, @@ -112,7 +111,9 @@ this.oApi = { "_fnBrowserDetect": _fnBrowserDetect, "_fnGetColumns": _fnGetColumns, "_fnHungarianMap": _fnHungarianMap, - "_fnCamelToHungarian": _fnCamelToHungarian + "_fnCamelToHungarian": _fnCamelToHungarian, + "_fnBuildAjax": _fnBuildAjax, + "_fnAjaxDataSrc": _fnAjaxDataSrc }; $.extend( DataTable.ext.oApi, this.oApi ); diff --git a/media/src/core/core.ajax.js b/media/src/core/core.ajax.js index db00d6ef..1074837d 100644 --- a/media/src/core/core.ajax.js +++ b/media/src/core/core.ajax.js @@ -1,4 +1,101 @@ + +/** + * Create an Ajax call based on the table's settings, taking into account that + * parameters can have multiple forms, and backwards compatibility. + * + * @param {object} oSettings dataTables settings object + * @param {array} data Data to send to the server, required by + * DataTables - may be augmented by developer callbacks + * @param {function} fn Callback function to run when data is obtained + */ +function _fnBuildAjax( oSettings, data, fn ) +{ + // Compatibility with 1.9-, allow fnServerData and event to manipulate + _fnCallbackFire( oSettings, 'aoServerParams', 'serverParams', [data] ); + + var ajaxData; + + if ( $.isPlainObject( oSettings.ajax ) && oSettings.ajax.data ) + { + ajaxData = oSettings.ajax.data; + var newData = $.isFunction( ajaxData ) ? + ajaxData( data ) : // fn can manipulate data or return an object or array + ajaxData; // object or array to merge + + if ( $.isArray( newData ) ) + { + // name value pair objects in an array + data = data.concat( newData ); + } + else if ( $.isPlainObject( newData ) ) + { + // aData is an array of name value pairs at this point - convert to + // an object to easily merge data - jQuery will cope with the switch + var oData = {}; + $.each( data, function (key, val) { + oData[val.name] = val.value; + } ); + + data = $.extend( true, oData, newData ); + } + + // Remove the data property as we've resolved it already + delete oSettings.ajax.data; + } + + var baseAjax = { + "data": data, + "success": function (json) { + if ( json.sError ) { + oSettings.oApi._fnLog( oSettings, 0, json.sError ); + } + + $(oSettings.oInstance).trigger('xhr', [oSettings, json]); + fn( json ); + }, + "dataType": "json", + "cache": false, + "type": oSettings.sServerMethod, + "error": function (xhr, error, thrown) { + if ( error == "parsererror" ) { + oSettings.oApi._fnLog( oSettings, 0, "DataTables: invalid JSON response" ); + } + } + }; + + if ( oSettings.fnServerData ) + { + // DataTables 1.9- compatibility + oSettings.fnServerData.call( oSettings.oInstance, + oSettings.sAjaxSource, data, fn, oSettings + ); + } + else if ( oSettings.sAjaxSource || typeof oSettings.ajax === 'string' ) + { + // DataTables 1.9- compatibility + oSettings.jqXHR = $.ajax( $.extend( baseAjax, { + url: oSettings.ajax || oSettings.sAjaxSource + } ) ); + } + else if ( $.isFunction( oSettings.ajax ) ) + { + // Is a function - let the caller define what needs to be done + oSettings.jqXHR = oSettings.ajax.call( oSettings.oInstance, + data, fn, oSettings + ); + } + else + { + // Object to extend the base settings + oSettings.jqXHR = $.ajax( $.extend( baseAjax, oSettings.ajax ) ); + + // Restore for next time around + oSettings.ajax.data = ajaxData; + } +} + + /** * Update the table using an Ajax call * @param {object} oSettings dataTables settings object @@ -13,12 +110,11 @@ function _fnAjaxUpdate( oSettings ) _fnProcessingDisplay( oSettings, true ); var iColumns = oSettings.aoColumns.length; var aoData = _fnAjaxParameters( oSettings ); - _fnServerParams( oSettings, aoData ); - - oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aoData, - function(json) { - _fnAjaxUpdateDraw( oSettings, json ); - }, oSettings ); + + _fnBuildAjax( oSettings, aoData, function(json) { + _fnAjaxUpdateDraw( oSettings, json ); + }, oSettings ); + return false; } return true; @@ -95,18 +191,6 @@ function _fnAjaxParameters( oSettings ) } -/** - * Add Ajax parameters from plug-ins - * @param {object} oSettings dataTables settings object - * @param array {objects} aoData name/value pairs to send to the server - * @memberof DataTable#oApi - */ -function _fnServerParams( oSettings, aoData ) -{ - _fnCallbackFire( oSettings, 'aoServerParams', 'serverParams', [aoData] ); -} - - /** * Data the data from the server (nuking the old) and redraw the table * @param {object} oSettings dataTables settings object @@ -139,7 +223,7 @@ function _fnAjaxUpdateDraw ( oSettings, json ) oSettings._iRecordsTotal = parseInt(json.iTotalRecords, 10); oSettings._iRecordsDisplay = parseInt(json.iTotalDisplayRecords, 10); - var aData = _fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ); + var aData = _fnAjaxDataSrc( oSettings, json ); for ( var i=0, iLen=aData.length ; iView message'; + * } + * return json; + * } + * } + * } ); + * + * @example + * // Add data to the request + * $('#example').dataTable( { + * "ajax": { + * "url": "data.json", + * "data": function ( d ) { + * return { + * "extra_search": $('#extra').val() + * }; + * } + * } + * } ); + * + * @example + * // Send request as POST + * $('#example').dataTable( { + * "ajax": { + * "url": "data.json", + * "type": "POST" + * } + * } ); + * + * @example + * // Get the data from localStorage (could interface with a form for + * // adding, editing and removing rows). + * $('#example').dataTable( { + * "ajax": function (data, callback, settings) { + * callback( + * JSON.parse( localStorage.getItem('dataTablesData') ) + * ); + * } + * } ); + */ + "ajax": null, + + /** * This parameter allows you to readily specify the entries in the length drop * down menu that DataTables shows when pagination is enabled. It can be @@ -562,7 +717,7 @@ DataTable.defaults = { * $(document).ready( function () { * $('#example').dataTable( { * "serverSide": true, - * "ajaxSource": "xhr.php" + * "ajax": "xhr.php" * } ); * } ); */ @@ -931,13 +1086,16 @@ DataTable.defaults = { /** + * __Deprecated__ The functionality provided by this parameter has now been + * superseded by that provided through `ajax`, which should be used instead. + * * This parameter allows you to override the default function which obtains - * the data from the server so something more suitable for your application. For - * example you could use POST data, or pull information from a Gears or AIR - * database. + * the data from the server so something more suitable for your application. + * For example you could use POST data, or pull information from a Gears or + * AIR database. * @type function * @member - * @param {string} source HTTP source to obtain the data from (`ajaxSource`) + * @param {string} source HTTP source to obtain the data from (`ajax`) * @param {array} data A key/value pair object containing the data to send * to the server * @param {function} callback to be called on completion of the data get @@ -947,54 +1105,17 @@ DataTable.defaults = { * @dtopt Callbacks * @dtopt Server-side * @name DataTable.defaults.serverData - * - * @example - * // POST data to server (note you can use `serverMethod` to set the - * // HTTP method is that is all you want to use `serverData` for. - * $(document).ready( function() { - * $('#example').dataTable( { - * "processing": true, - * "serverSide": true, - * "ajaxSource": "xhr.php", - * "serverData": function ( source, data, callback, settings ) { - * settings.jqXHR = $.ajax( { - * "dataType": 'json', - * "type": "POST", - * "url": source, - * "data": data, - * "success": callback - * } ); - * } - * } ); - * } ); + * + * @deprecated 1.10. Please use `ajax` for this functionality now. */ - "fnServerData": function ( url, data, callback, settings ) { - settings.jqXHR = $.ajax( { - "url": url, - "data": data, - "success": function (json) { - if ( json.sError ) { - settings.oApi._fnLog( settings, 0, json.sError ); - } - - $(settings.oInstance).trigger('xhr', [settings, json]); - callback( json ); - }, - "dataType": "json", - "cache": false, - "type": settings.sServerMethod, - "error": function (xhr, error, thrown) { - if ( error == "parsererror" ) { - settings.oApi._fnLog( settings, 0, "DataTables warning: JSON data from "+ - "server could not be parsed. This is caused by a JSON formatting error." ); - } - } - } ); - }, + "fnServerData": null, /** - * It is often useful to send extra data to the server when making an Ajax + * __Deprecated__ The functionality provided by this parameter has now been + * superseded by that provided through `ajax`, which should be used instead. + * + * It is often useful to send extra data to the server when making an Ajax * request - for example custom filtering information, and this callback * function makes it trivial to send extra information to the server. The * passed in parameter is the data set that has been constructed by @@ -1011,18 +1132,8 @@ DataTable.defaults = { * @dtopt Callbacks * @dtopt Server-side * @name DataTable.defaults.serverParams - * - * @example - * $(document).ready( function() { - * $('#example').dataTable( { - * "processing": true, - * "serverSide": true, - * "ajaxSource": "scripts/server_processing.php", - * "serverParams": function ( data ) { - * data.push( { "name": "more_data", "value": "my_value" } ); - * } - * } ); - * } ); + * + * @deprecated 1.10. Please use `ajax` for this functionality now. */ "fnServerParams": null, @@ -1242,7 +1353,7 @@ DataTable.defaults = { * $(document).ready( function() { * $('#example').dataTable( { * "serverSide": true, - * "ajaxSource": "scripts/server_processing.php", + * "ajax": "scripts/server_processing.php", * "deferLoading": 57 * } ); * } ); @@ -1252,7 +1363,7 @@ DataTable.defaults = { * $(document).ready( function() { * $('#example').dataTable( { * "serverSide": true, - * "ajaxSource": "scripts/server_processing.php", + * "ajax": "scripts/server_processing.php", * "deferLoading": [ 57, 100 ], * "search": { * "search": "my_filter" @@ -1843,6 +1954,9 @@ DataTable.defaults = { /** + * __Deprecated__ The functionality provided by this parameter has now been + * superseded by that provided through `ajax`, which should be used instead. + * * By default DataTables will look for the property 'aaData' when obtaining * data from an Ajax source or for server-side processing - this parameter * allows that property to be changed. You can use Javascript dotted object @@ -1853,46 +1967,29 @@ DataTable.defaults = { * @dtopt Options * @dtopt Server-side * @name DataTable.defaults.ajaxDataProp - * - * @example - * // Get data from { "data": [...] } - * $(document).ready( function() { - * var oTable = $('#example').dataTable( { - * "ajaxSource": "sources/data.txt", - * "ajaxDataProp": "data" - * } ); - * } ); - * - * @example - * // Get data from { "data": { "inner": [...] } } - * $(document).ready( function() { - * var oTable = $('#example').dataTable( { - * "ajaxSource": "sources/data.txt", - * "ajaxDataProp": "data.inner" - * } ); - * } ); + * + * @deprecated 1.10. Please use `ajax` for this functionality now. */ "sAjaxDataProp": "aaData", /** - * You can instruct DataTables to load data from an external source using this - * parameter (use aData if you want to pass data in you already have). Simply - * provide a url a JSON object can be obtained from. This object must include - * the parameter `aaData` which is the data source for the table. + * __Deprecated__ The functionality provided by this parameter has now been + * superseded by that provided through `ajax`, which should be used instead. + * + * You can instruct DataTables to load data from an external + * source using this parameter (use aData if you want to pass data in you + * already have). Simply provide a url a JSON object can be obtained from. + * This object must include the parameter `aaData` which is the data source + * for the table. * @type string * @default null * * @dtopt Options * @dtopt Server-side * @name DataTable.defaults.ajaxSource - * - * @example - * $(document).ready( function() { - * $('#example').dataTable( { - * "ajaxSource": "/dataTables/json.php" - * } ); - * } ) + * + * @deprecated 1.10. Please use `ajax` for this functionality now. */ "sAjaxSource": null, @@ -2043,6 +2140,9 @@ DataTable.defaults = { /** + * __Deprecated__ The functionality provided by this parameter has now been + * superseded by that provided through `ajax`, which should be used instead. + * * Set the HTTP method that is used to make the Ajax call for server-side * processing or Ajax sourced data. * @type string @@ -2051,15 +2151,8 @@ DataTable.defaults = { * @dtopt Options * @dtopt Server-side * @name DataTable.defaults.serverMethod - * - * @example - * $(document).ready( function() { - * $('#example').dataTable( { - * "serverSide": true, - * "ajaxSource": "scripts/post.php", - * "serverMethod": "POST" - * } ); - * } ); + * + * @deprecated 1.10. Please use `ajax` for this functionality now. */ "sServerMethod": "GET" }; diff --git a/media/src/model/model.settings.js b/media/src/model/model.settings.js index 6378c444..b4a99835 100644 --- a/media/src/model/model.settings.js +++ b/media/src/model/model.settings.js @@ -251,6 +251,10 @@ DataTable.models.oSettings = { "bScrollbarLeft": false }, + + "ajax": null, + + /** * Array referencing the nodes which are used for the features. The * parameters of this object match what is allowed by sDom - i.e. diff --git a/media/unit_testing/data_sources/arrays.php b/media/unit_testing/data_sources/arrays.php new file mode 100644 index 00000000..91ac0272 --- /dev/null +++ b/media/unit_testing/data_sources/arrays.php @@ -0,0 +1,50 @@ + array( + array(1, 2, 3, 4, 5) + ) + ); + } + else if ( $_REQUEST['dataSrc'] === 'data' ) { + return array( + 'data' => array( + array(1, 2, 3, 4, 5) + ) + ); + } + else if ( $_REQUEST['dataSrc'] === 'nested' ) { + return array( + 'data' => array( + 'inner' => array( + array(1, 2, 3, 4, 5) + ) + ) + ); + } + else if ( $_REQUEST['dataSrc'] === 'plain' ) { + return array( + array(1, 2, 3, 4, 5) + ); + } +} + + diff --git a/media/unit_testing/data_sources/method.php b/media/unit_testing/data_sources/method.php new file mode 100644 index 00000000..954e74a7 --- /dev/null +++ b/media/unit_testing/data_sources/method.php @@ -0,0 +1,53 @@ + intval( $_REQUEST['sEcho'] ), + 'iTotalRecords' => 1, + 'iTotalDisplayRecords' => 1, + 'aaData' => array( + array(1, 2, 3, 4, 5) + ) + ) ); + } + else { + // Client-side processing + echo json_encode( array( + 'aaData' => array( + array(1, 2, 3, 4, 5) + ) + ) ); + } +} + + +function fail() +{ + if ( isset($_REQUEST['sEcho']) ) { + // Server-side processing + echo json_encode( array( + 'sEcho' => intval( $_REQUEST['sEcho'] ), + 'iTotalRecords' => 0, + 'iTotalDisplayRecords' => 0, + 'aaData' => array() + ) ); + } + else { + // Client-side processing + echo json_encode( array( + 'aaData' => array() + ) ); + } +} + diff --git a/media/unit_testing/data_sources/objects.php b/media/unit_testing/data_sources/objects.php new file mode 100644 index 00000000..480dbf6d --- /dev/null +++ b/media/unit_testing/data_sources/objects.php @@ -0,0 +1,33 @@ + intval( $_REQUEST['sEcho'] ), + 'iTotalRecords' => 1, + 'iTotalDisplayRecords' => 1, + 'aaData' => array( + array( + 'engine' => 10, + 'browser' => 20, + 'platform' => 30, + 'version' => 40, + 'grade' => 50 + ) + ) + ) ); +} +else { + echo json_encode( array( + 'aaData' => array( + array( + 'engine' => 10, + 'browser' => 20, + 'platform' => 30, + 'version' => 40, + 'grade' => 50 + ) + ) + ) ); +} + diff --git a/media/unit_testing/data_sources/param.php b/media/unit_testing/data_sources/param.php new file mode 100644 index 00000000..021225ee --- /dev/null +++ b/media/unit_testing/data_sources/param.php @@ -0,0 +1,42 @@ + intval( $_REQUEST['sEcho'] ), + 'iTotalRecords' => 1, + 'iTotalDisplayRecords' => 1, + 'aaData' => array( + array(1, 2, 3, 4, 5) + ), + 'post' => xss( $_POST ), + 'get' => xss( $_GET ), + 'post_length' => count( array_keys( $_POST ) ), + 'get_length' => count( array_keys( $_GET ) ) + ) ); +} +else { + echo json_encode( array( + 'aaData' => array( + array(1, 2, 3, 4, 5) + ), + 'post' => xss( $_POST ), + 'get' => xss( $_GET ), + 'post_length' => count( array_keys( $_POST ) ), + 'get_length' => count( array_keys( $_GET ) ) + ) ); +} + + + +// This script shouldn't be hosted on a public server, but to prevent attacks: +function xss ( $a ) +{ + $out = array(); + + foreach ($a as $key => $value) { + $out[ $key ] = htmlentities( $value ); + } + + return $out; +} diff --git a/media/unit_testing/tests/6776-scrolling-table-grows.js b/media/unit_testing/tests/6776-scrolling-table-grows.js new file mode 100755 index 00000000..cfc70d5f --- /dev/null +++ b/media/unit_testing/tests/6776-scrolling-table-grows.js @@ -0,0 +1,64 @@ +// DATA_TEMPLATE: 6776 +oTest.fnStart( "Actions on a scrolling table keep width" ); + + +$(document).ready( function () { + var oTable = $('#example').dataTable( { + "bFilter": true, + "bSort": true, + "sScrollY": "100px", + "bPaginate": false + } ); + + var iWidth = $('div.dataTables_wrapper').width(); + + oTest.fnTest( + "First sort has no effect on width", + function () { $('th:eq(1)').click(); }, + function () { return $('div.dataTables_wrapper').width() == iWidth; } + ); + + oTest.fnTest( + "Second sort has no effect on width", + function () { $('th:eq(1)').click(); }, + function () { return $('div.dataTables_wrapper').width() == iWidth; } + ); + + oTest.fnTest( + "Third sort has no effect on width", + function () { $('th:eq(2)').click(); }, + function () { return $('div.dataTables_wrapper').width() == iWidth; } + ); + + oTest.fnTest( + "Filter has no effect on width", + function () { oTable.fnFilter('i'); }, + function () { return $('div.dataTables_wrapper').width() == iWidth; } + ); + + oTest.fnTest( + "Filter 2 has no effect on width", + function () { oTable.fnFilter('in'); }, + function () { return $('div.dataTables_wrapper').width() == iWidth; } + ); + + oTest.fnTest( + "No result filter has header and body at same width", + function () { oTable.fnFilter('xxx'); }, + function () { return $('#example').width() == $('div.dataTables_scrollHeadInner').width(); } + ); + + oTest.fnTest( + "Filter with no results has no effect on width", + function () { oTable.fnFilter('xxx'); }, + function () { return $('div.dataTables_wrapper').width() == iWidth; } + ); + + oTest.fnTest( + "Filter with no results has table equal to wrapper width", + function () { oTable.fnFilter('xxx'); }, + function () { return $('div.dataTables_wrapper').width() == $('#example').width(); } + ); + + oTest.fnComplete(); +} ); \ No newline at end of file diff --git a/media/unit_testing/tests_onhold/3_ajax/ajax.js b/media/unit_testing/tests_onhold/3_ajax/ajax.js new file mode 100644 index 00000000..f43e3aed --- /dev/null +++ b/media/unit_testing/tests_onhold/3_ajax/ajax.js @@ -0,0 +1,589 @@ +// DATA_TEMPLATE: empty_table +oTest.fnStart( "ajax" ); + + +$(document).ready( function () { + var json; + var result; + + // + // As a string + // + oTest.fnWaitTest( + "Basic request as a string - getting arrays", + function () { + $('#example').dataTable( { + "ajax": "../data_sources/arrays.php" + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "Basic request as a string - getting objects", + function () { + oSession.fnRestore(); + $('#example').dataTable( { + "ajax": "../data_sources/objects.php", + "columns": [ + { data: 'engine' }, + { data: 'browser' }, + { data: 'platform' }, + { data: 'version' }, + { data: 'grade' } + ] + } ); + }, + function () { + return $('tbody td').eq(1).html() === '20'; + } + ); + + oTest.fnWaitTest( + "Default request is GET - string based", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": "../data_sources/method.php?method=get" + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "jQuery anti-cache parameter is sent by default - string based", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": "../data_sources/param.php" + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get._; + } + ); + + oTest.fnWaitTest( + "No other parameters sent", + null, + function () { + return json.get_length === 1; + } + ); + + + + // + // As an object + // + oTest.fnWaitTest( + "Get Ajax using url parameter only", + function () { + oSession.fnRestore(); + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/arrays.php" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + + // props + oTest.fnWaitTest( + "Disable cache property", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/param.php", + "cache": true + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && typeof json.get._ === 'undefined'; + } + ); + + oTest.fnWaitTest( + "Set an error callback", + function () { + oSession.fnRestore(); + result = false; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/rubbish", + "error": function () { + result = true; + } + } + } ); + }, + function () { + return result; + } + ); + + // type + oTest.fnWaitTest( + "type - Default request is GET", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/method.php?method=get" + } + } ); + }, + function () { + return $('tbody td').eq(1).html() === '2'; + } + ); + + oTest.fnWaitTest( + "type - Can use `type` to make a POST request", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/method.php?method=post", + "type": "POST" + } + } ); + }, + function () { + return $('tbody td').eq(2).html() === '3'; + } + ); + + oTest.fnWaitTest( + "type - Can use `type` to make a PUT request", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/method.php?method=put", + "type": "PUT" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + + // data + oTest.fnWaitTest( + "data - Function based data - has empty array as input", + function () { + oSession.fnRestore(); + result = false; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + result = d.length === 0; + } + } + } ); + }, + function () { + return result; + } + ); + + oTest.fnWaitTest( + "data - Function based data - can return an object", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + return { 'tapestry': 'king' }; + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'king'; + } + ); + + oTest.fnWaitTest( + "data - Function based data - multiple properties", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + return { 'tapestry': 'king', 'move': 'earth' }; + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'king' && json.get.move === 'earth'; + } + ); + + oTest.fnWaitTest( + "data - Confirm no other parameters sent", + null, + function () { + return json.get_length === 3; + } + ); + + + oTest.fnWaitTest( + "data - Function based data - can return an array of key/value object pairs", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + return [ + { 'name': 'tapestry', 'value': 'carole' } + ]; + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'carole'; + } + ); + + oTest.fnWaitTest( + "data - Function based data - multiple properties", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + return [ + { 'name': 'tapestry', 'value': 'carole' }, + { 'name': 'feel', 'value': 'earth move' } + ]; + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'carole' && json.get.feel === 'earth move'; + } + ); + + oTest.fnWaitTest( + "data - Function based data - add parameters to passed in array", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + d.push( { 'name': 'tapestry', 'value': 'carole' } ); + d.push( { 'name': 'rich', 'value': 'hue' } ); + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'carole' && json.get.rich === 'hue'; + } + ); + + oTest.fnWaitTest( + "data - Function based data - send parameters by POST", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + d.push( { 'name': 'tapestry', 'value': 'king' } ); + d.push( { 'name': 'rich', 'value': 'hue' } ); + }, + "type": "POST" + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.post && json.post.tapestry === 'king' && json.post.rich === 'hue'; + } + ); + + oTest.fnWaitTest( + "data - Object based data - sends parameters defined", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/param.php", + "data": { + "too": "late", + "got": "friend" + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.too === 'late' && json.get.got === 'friend'; + } + ); + + oTest.fnWaitTest( + "data - Array based data - sends parameters defined", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/param.php", + "data": [ + { 'name': 'tapestry', 'value': 'king' }, + { 'name': 'far', 'value': 'away' } + ] + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'king' && json.get.far === 'away'; + } + ); + + + // dataSrc + oTest.fnWaitTest( + "dataSrc - Default data source is aaData", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/arrays.php" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "dataSrc - as a string - read from `data`", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/arrays.php?dataSrc=data", + "dataSrc": "data" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "dataSrc - as a string - read from nested property `data.inner`", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/arrays.php?dataSrc=nested", + "dataSrc": "data.inner" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "dataSrc - as a string - read from plain array", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/arrays.php?dataSrc=plain", + "dataSrc": "" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "dataSrc - as a function, return JSON property", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/arrays.php?dataSrc=nested", + "dataSrc": function ( json ) { + return json.data.inner; + } + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "dataSrc - as a function, can manipulate the data", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": { + "url": "../data_sources/arrays.php?dataSrc=data", + "dataSrc": function ( json ) { + json.data[0][0] = "Tapestry"; + return json.data; + } + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === 'Tapestry' && + $('tbody td').eq(1).html() === '2'; + } + ); + + + + // + // As a function + // + oTest.fnTest( + "ajax as a function - first parameter is array of data", + function () { + oSession.fnRestore(); + result = null; + + $('#example').dataTable( { + "ajax": function ( data, callback, settings ) { + result = arguments; + callback( { aaData: [] } ); + } + } ); + }, + function () { + return $.isArray( result[0] ) && result[0].length === 0; + } + ); + + oTest.fnTest( + "ajax as a function - second parameter is callback function", + null, + function () { + return $.isFunction( result[1] ); + } + ); + + oTest.fnTest( + "ajax as a function - third parameter is settings object", + null, + function () { + return result[2] === $('#example').dataTable().fnSettings(); + } + ); + + oTest.fnTest( + "ajax as a function - only three parameters", + null, + function () { + return result.length === 3; + } + ); + + oTest.fnTest( + "ajax as a function - callback will insert data into the table", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": function ( data, callback, settings ) { + callback( { aaData: [[1,2,3,4,5]] } ); + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + + + oTest.fnComplete(); +} ); diff --git a/media/unit_testing/tests_onhold/3_ajax/fnServerParams.js b/media/unit_testing/tests_onhold/3_ajax/fnServerParams.js new file mode 100644 index 00000000..586e8bdb --- /dev/null +++ b/media/unit_testing/tests_onhold/3_ajax/fnServerParams.js @@ -0,0 +1,82 @@ +// DATA_TEMPLATE: empty_table +oTest.fnStart( "fnServerParams" ); + + +$(document).ready( function () { + /* Check the default */ + var json = {}; + var oTable = $('#example').dataTable( { + "sAjaxSource": "../data_sources/param.php" + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + + oTest.fnWaitTest( + "jQuery anti-cache parameter was sent", + null, + function () { + return json.get && json.get._; + } + ); + + oTest.fnWaitTest( + "No other parameters sent", + null, + function () { + return 1 === $.map( json.get, function (val) { + return val; + } ).length; + } + ); + + oTest.fnWaitTest( + "Send additional parameters", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "sAjaxSource": "../data_sources/param.php", + "fnServerParams": function ( data ) { + data.push( { name: 'test', value: 'unit' } ); + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.test === 'unit'; + } + ); + + oTest.fnTest( + "jQuery anti-cache parameter was still sent", + null, + function () { + return json.get._; + } + ); + + oTest.fnWaitTest( + "Send multiple parameters", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "sAjaxSource": "../data_sources/param.php", + "fnServerParams": function ( data ) { + data.push( { name: 'test', value: 'unit' } ); + data.push( { name: 'tapestry', value: 'king' } ); + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.test === 'unit' && json.get.tapestry === 'king'; + } + ); + + oTest.fnComplete(); +} ); diff --git a/media/unit_testing/tests_onhold/3_ajax/sServerMethod.js b/media/unit_testing/tests_onhold/3_ajax/sServerMethod.js new file mode 100644 index 00000000..2428eaa5 --- /dev/null +++ b/media/unit_testing/tests_onhold/3_ajax/sServerMethod.js @@ -0,0 +1,49 @@ +// DATA_TEMPLATE: empty_table +oTest.fnStart( "sServerMethod" ); + + +$(document).ready( function () { + /* Check the default */ + var oTable = $('#example').dataTable( { + "sAjaxSource": "../data_sources/method.php?method=get" + } ); + + oTest.fnWaitTest( + "Default method was GET", + null, + function () { + // A valid request will place a single row in the table + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "Can make a POST request", + function () { + oSession.fnRestore(); + $('#example').dataTable( { + "sAjaxSource": "../data_sources/method.php?method=post", + "sServerMethod": "POST" + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "Can make a PUT request", + function () { + oSession.fnRestore(); + $('#example').dataTable( { + "sAjaxSource": "../data_sources/method.php?method=put", + "sServerMethod": "PUT" + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnComplete(); +} ); diff --git a/media/unit_testing/tests_onhold/4_server-side/ajax.js b/media/unit_testing/tests_onhold/4_server-side/ajax.js new file mode 100644 index 00000000..6e53bb1c --- /dev/null +++ b/media/unit_testing/tests_onhold/4_server-side/ajax.js @@ -0,0 +1,587 @@ +// DATA_TEMPLATE: empty_table +oTest.fnStart( "ajax" ); + + +$(document).ready( function () { + var json; + var result; + + // + // As a string + // + oTest.fnWaitTest( + "Basic request as a string - getting arrays", + function () { + $('#example').dataTable( { + "serverSide": true, + "ajax": "../data_sources/arrays.php" + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "Basic request as a string - getting objects", + function () { + oSession.fnRestore(); + $('#example').dataTable( { + "serverSide": true, + "ajax": "../data_sources/objects.php", + "columns": [ + { data: 'engine' }, + { data: 'browser' }, + { data: 'platform' }, + { data: 'version' }, + { data: 'grade' } + ] + } ); + }, + function () { + return $('tbody td').eq(1).html() === '20'; + } + ); + + oTest.fnWaitTest( + "Default request is GET - string based", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "serverSide": true, + "ajax": "../data_sources/method.php?method=get" + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "jQuery anti-cache parameter is sent by default - string based", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "serverSide": true, + "ajax": "../data_sources/param.php" + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get._; + } + ); + + oTest.fnWaitTest( + "Only the SSP parameters were also sent", + null, + function () { + return json.get_length === 36; + } + ); + + + + // + // As an object + // + oTest.fnWaitTest( + "Get Ajax using url parameter only", + function () { + oSession.fnRestore(); + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/arrays.php" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + + // props + oTest.fnWaitTest( + "Set an error callback", + function () { + oSession.fnRestore(); + result = false; + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/rubbish", + "error": function () { + result = true; + } + } + } ); + }, + function () { + return result; + } + ); + + // type + oTest.fnWaitTest( + "type - Default request is GET", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/method.php?method=get" + } + } ); + }, + function () { + return $('tbody td').eq(1).html() === '2'; + } + ); + + oTest.fnWaitTest( + "type - Can use `type` to make a POST request", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/method.php?method=post", + "type": "POST" + } + } ); + }, + function () { + return $('tbody td').eq(2).html() === '3'; + } + ); + + oTest.fnWaitTest( + "type - Can use `type` to make a PUT request", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/method.php?method=put", + "type": "PUT" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + + // data + oTest.fnWaitTest( + "data - Function based data - has standard SSP parameters only", + function () { + oSession.fnRestore(); + result = false; + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + result = d.length === 35; + } + } + } ); + }, + function () { + return result; + } + ); + + oTest.fnWaitTest( + "data - Function based data - can return an object", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + return { 'tapestry': 'king' }; + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'king'; + } + ); + + oTest.fnWaitTest( + "data - Function based data - multiple properties", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + return { 'tapestry': 'king', 'move': 'earth' }; + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'king' && json.get.move === 'earth'; + } + ); + + oTest.fnWaitTest( + "data - Confirm only SSP parameters were also sent", + null, + function () { + return json.get_length === 38; + } + ); + + + oTest.fnWaitTest( + "data - Function based data - can return an array of key/value object pairs", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + return [ + { 'name': 'tapestry', 'value': 'carole' } + ]; + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'carole'; + } + ); + + oTest.fnWaitTest( + "data - Function based data - multiple properties", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + return [ + { 'name': 'tapestry', 'value': 'carole' }, + { 'name': 'feel', 'value': 'earth move' } + ]; + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'carole' && json.get.feel === 'earth move'; + } + ); + + oTest.fnWaitTest( + "data - Function based data - add parameters to passed in array", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + d.push( { 'name': 'tapestry', 'value': 'carole' } ); + d.push( { 'name': 'rich', 'value': 'hue' } ); + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'carole' && json.get.rich === 'hue'; + } + ); + + oTest.fnWaitTest( + "data - Function based data - send parameters by POST", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/param.php", + "data": function ( d ) { + d.push( { 'name': 'tapestry', 'value': 'king' } ); + d.push( { 'name': 'rich', 'value': 'hue' } ); + }, + "type": "POST" + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.post && json.post.tapestry === 'king' && json.post.rich === 'hue'; + } + ); + + oTest.fnWaitTest( + "data - Object based data - sends parameters defined", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/param.php", + "data": { + "too": "late", + "got": "friend" + } + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.too === 'late' && json.get.got === 'friend'; + } + ); + + oTest.fnWaitTest( + "data - Array based data - sends parameters defined", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/param.php", + "data": [ + { 'name': 'tapestry', 'value': 'king' }, + { 'name': 'far', 'value': 'away' } + ] + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.tapestry === 'king' && json.get.far === 'away'; + } + ); + + + // dataSrc + oTest.fnWaitTest( + "dataSrc - Default data source is aaData", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/arrays.php" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "dataSrc - as a string - read from `data`", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/arrays.php?dataSrc=data", + "dataSrc": "data" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "dataSrc - as a string - read from nested property `data.inner`", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/arrays.php?dataSrc=nested", + "dataSrc": "data.inner" + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "dataSrc - as a function, return JSON property", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/arrays.php?dataSrc=nested", + "dataSrc": function ( json ) { + return json.data.inner; + } + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + oTest.fnWaitTest( + "dataSrc - as a function, can manipulate the data", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "serverSide": true, + "ajax": { + "url": "../data_sources/arrays.php?dataSrc=data", + "dataSrc": function ( json ) { + json.data[0][0] = "Tapestry"; + return json.data; + } + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === 'Tapestry' && + $('tbody td').eq(1).html() === '2'; + } + ); + + + + // + // As a function + // + oTest.fnTest( + "ajax as a function - first parameter is array of data", + function () { + oSession.fnRestore(); + result = null; + + $('#example').dataTable( { + "serverSide": true, + "ajax": function ( data, callback, settings ) { + result = arguments; + callback( { + sEcho: 1, + iTotalRecords: 1, + iTotalDisplayRecords: 1, + aaData: [] + } ); + } + } ); + }, + function () { + console.log( result ); + return $.isArray( result[0] ) && result[0].length === 35; + } + ); + + oTest.fnTest( + "ajax as a function - second parameter is callback function", + null, + function () { + return $.isFunction( result[1] ); + } + ); + + oTest.fnTest( + "ajax as a function - third parameter is settings object", + null, + function () { + return result[2] === $('#example').dataTable().fnSettings(); + } + ); + + oTest.fnTest( + "ajax as a function - only three parameters", + null, + function () { + return result.length === 3; + } + ); + + oTest.fnTest( + "ajax as a function - callback will insert data into the table", + function () { + oSession.fnRestore(); + + $('#example').dataTable( { + "ajax": function ( data, callback, settings ) { + callback( { + sEcho: 1, + iTotalRecords: 1, + iTotalDisplayRecords: 1, + aaData: [[1,2,3,4,5]] + } ); + } + } ); + }, + function () { + return $('tbody td').eq(0).html() === '1'; + } + ); + + + + oTest.fnComplete(); +} ); diff --git a/media/unit_testing/tests_onhold/4_server-side/fnServerData.js b/media/unit_testing/tests_onhold/4_server-side/fnServerData.js new file mode 100644 index 00000000..cbed47e3 --- /dev/null +++ b/media/unit_testing/tests_onhold/4_server-side/fnServerData.js @@ -0,0 +1,68 @@ +// DATA_TEMPLATE: empty_table +oTest.fnStart( "fnServerData for SSP sourced data" ); + +$(document).ready( function () { + var mPass; + + oTest.fnTest( + "Argument length", + function () { + $('#example').dataTable( { + "bServerSide": true, + "sAjaxSource": "../data_sources/param.php", + "fnServerData": function () { + mPass = arguments.length; + } + } ); + }, + function () { return mPass == 4; } + ); + + oTest.fnTest( + "Url", + function () { + $('#example').dataTable( { + "bDestroy": true, + "bServerSide": true, + "sAjaxSource": "../data_sources/param.php", + "fnServerData": function (sUrl, aoData, fnCallback, oSettings) { + mPass = sUrl == "../data_sources/param.php"; + } + } ); + }, + function () { return mPass; } + ); + + oTest.fnTest( + "Data array", + function () { + $('#example').dataTable( { + "bDestroy": true, + "bServerSide": true, + "sAjaxSource": "../data_sources/param.php", + "fnServerData": function (sUrl, aoData, fnCallback, oSettings) { + mPass = aoData.length==35; + } + } ); + }, + function () { return mPass; } + ); + + oTest.fnTest( + "Callback function", + function () { + $('#example').dataTable( { + "bDestroy": true, + "bServerSide": true, + "sAjaxSource": "../data_sources/param.php", + "fnServerData": function (sUrl, aoData, fnCallback, oSettings) { + mPass = typeof fnCallback == 'function'; + } + } ); + }, + function () { return mPass; } + ); + + + oTest.fnComplete(); +} ); \ No newline at end of file diff --git a/media/unit_testing/tests_onhold/4_server-side/fnServerParams.js b/media/unit_testing/tests_onhold/4_server-side/fnServerParams.js new file mode 100644 index 00000000..00bdd1cb --- /dev/null +++ b/media/unit_testing/tests_onhold/4_server-side/fnServerParams.js @@ -0,0 +1,122 @@ +// DATA_TEMPLATE: empty_table +oTest.fnStart( "fnServerParams" ); + + +$(document).ready( function () { + /* Check the default */ + var json = {}; + var oTable = $('#example').dataTable( { + "bServerSide": true, + "sAjaxSource": "../data_sources/param.php" + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + + oTest.fnWaitTest( + "jQuery anti-cache parameter was sent", + null, + function () { + return json.get && json.get._; + } + ); + + oTest.fnWaitTest( + "Default SSP parameters were sent", + null, + function () { + return 36 === $.map( json.get, function (val) { + return val; + } ).length; + } + ); + + oTest.fnWaitTest( + "Send additional parameters", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "bServerSide": true, + "sAjaxSource": "../data_sources/param.php", + "fnServerParams": function ( data ) { + data.push( { name: 'test', value: 'unit' } ); + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.test === 'unit'; + } + ); + + oTest.fnTest( + "Default parameters were still sent", + null, + function () { + return 37 === $.map( json.get, function (val) { + return val; + } ).length; + } + ); + + oTest.fnWaitTest( + "Send multiple parameters", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "bServerSide": true, + "sAjaxSource": "../data_sources/param.php", + "fnServerParams": function ( data ) { + data.push( { name: 'test', value: 'unit' } ); + data.push( { name: 'tapestry', value: 'king' } ); + } + } ).on('xhr', function (e, settings, o) { + json = o; + } ); + }, + function () { + return json.get && json.get.test === 'unit' && json.get.tapestry === 'king'; + } + ); + + oTest.fnWaitTest( + "Delete parameters", + function () { + oSession.fnRestore(); + json = {}; + + $('#example').dataTable( { + "bServerSide": true, + "sAjaxSource": "../data_sources/param.php", + "fnServerParams": function ( data ) { + var remove = function ( a, param ) { + for ( var i=0 ; i