-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
Copy pathcore.ajax.js
317 lines (276 loc) · 8.33 KB
/
core.ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* 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 )
{
var ajaxData;
var ajax = oSettings.ajax;
var instance = oSettings.oInstance;
var callback = function ( json ) {
var status = oSettings.jqXHR
? oSettings.jqXHR.status
: null;
if ( json === null || (typeof status === 'number' && status == 204 ) ) {
json = {};
_fnAjaxDataSrc( oSettings, json, [] );
}
var error = json.error || json.sError;
if ( error ) {
_fnLog( oSettings, 0, error );
}
oSettings.json = json;
_fnCallbackFire( oSettings, null, 'xhr', [oSettings, json, oSettings.jqXHR], true );
fn( json );
};
if ( $.isPlainObject( ajax ) && ajax.data )
{
ajaxData = ajax.data;
var newData = typeof ajaxData === 'function' ?
ajaxData( data, oSettings ) : // fn can manipulate data or return
ajaxData; // an object object or array to merge
// If the function returned something, use that alone
data = typeof ajaxData === 'function' && newData ?
newData :
$.extend( true, data, newData );
// Remove the data property as we've resolved it already and don't want
// jQuery to do it again (it is restored at the end of the function)
delete ajax.data;
}
var baseAjax = {
"url": typeof ajax === 'string' ?
ajax :
'',
"data": data,
"success": callback,
"dataType": "json",
"cache": false,
"type": oSettings.sServerMethod,
"error": function (xhr, error) {
var ret = _fnCallbackFire( oSettings, null, 'xhr', [oSettings, null, oSettings.jqXHR], true );
if ( ret.indexOf(true) === -1 ) {
if ( error == "parsererror" ) {
_fnLog( oSettings, 0, 'Invalid JSON response', 1 );
}
else if ( xhr.readyState === 4 ) {
_fnLog( oSettings, 0, 'Ajax error', 7 );
}
}
_fnProcessingDisplay( oSettings, false );
}
};
// If `ajax` option is an object, extend and override our default base
if ( $.isPlainObject( ajax ) ) {
$.extend( baseAjax, ajax )
}
// Store the data submitted for the API
oSettings.oAjaxData = data;
// Allow plug-ins and external processes to modify the data
_fnCallbackFire( oSettings, null, 'preXhr', [oSettings, data, baseAjax], true );
if ( typeof ajax === 'function' )
{
// Is a function - let the caller define what needs to be done
oSettings.jqXHR = ajax.call( instance, data, callback, oSettings );
}
else if (ajax.url === '') {
callback({});
}
else {
// Object to extend the base settings
oSettings.jqXHR = $.ajax( baseAjax );
// Restore for next time around
if ( ajaxData ) {
ajax.data = ajaxData;
}
}
}
/**
* Update the table using an Ajax call
* @param {object} settings dataTables settings object
* @returns {boolean} Block the table drawing or not
* @memberof DataTable#oApi
*/
function _fnAjaxUpdate( settings )
{
settings.iDraw++;
_fnProcessingDisplay( settings, true );
_fnBuildAjax(
settings,
_fnAjaxParameters( settings ),
function(json) {
_fnAjaxUpdateDraw( settings, json );
}
);
}
/**
* Build up the parameters in an object needed for a server-side processing
* request.
* @param {object} oSettings dataTables settings object
* @returns {bool} block the table drawing or not
* @memberof DataTable#oApi
*/
function _fnAjaxParameters( settings )
{
var
columns = settings.aoColumns,
features = settings.oFeatures,
preSearch = settings.oPreviousSearch,
preColSearch = settings.aoPreSearchCols,
colData = function ( idx, prop ) {
return typeof columns[idx][prop] === 'function' ?
'function' :
columns[idx][prop];
};
return {
draw: settings.iDraw,
columns: columns.map( function ( column, i ) {
return {
data: colData(i, 'mData'),
name: column.sName,
searchable: column.bSearchable,
orderable: column.bSortable,
search: {
value: preColSearch[i].search,
regex: preColSearch[i].regex,
fixed: Object.keys(column.searchFixed).map( function(name) {
return {
name: name,
term: column.searchFixed[name].toString()
}
})
}
};
} ),
order: _fnSortFlatten( settings ).map( function ( val ) {
return {
column: val.col,
dir: val.dir,
name: colData(val.col, 'sName')
};
} ),
start: settings._iDisplayStart,
length: features.bPaginate ?
settings._iDisplayLength :
-1,
search: {
value: preSearch.search,
regex: preSearch.regex,
fixed: Object.keys(settings.searchFixed).map( function(name) {
return {
name: name,
term: settings.searchFixed[name].toString()
}
})
}
};
}
/**
* Data the data from the server (nuking the old) and redraw the table
* @param {object} oSettings dataTables settings object
* @param {object} json json data return from the server.
* @param {string} json.sEcho Tracking flag for DataTables to match requests
* @param {int} json.iTotalRecords Number of records in the data set, not accounting for filtering
* @param {int} json.iTotalDisplayRecords Number of records in the data set, accounting for filtering
* @param {array} json.aaData The data to display on this page
* @param {string} [json.sColumns] Column ordering (sName, comma separated)
* @memberof DataTable#oApi
*/
function _fnAjaxUpdateDraw ( settings, json )
{
var data = _fnAjaxDataSrc(settings, json);
var draw = _fnAjaxDataSrcParam(settings, 'draw', json);
var recordsTotal = _fnAjaxDataSrcParam(settings, 'recordsTotal', json);
var recordsFiltered = _fnAjaxDataSrcParam(settings, 'recordsFiltered', json);
if ( draw !== undefined ) {
// Protect against out of sequence returns
if ( draw*1 < settings.iDraw ) {
return;
}
settings.iDraw = draw * 1;
}
// No data in returned object, so rather than an array, we show an empty table
if ( ! data ) {
data = [];
}
_fnClearTable( settings );
settings._iRecordsTotal = parseInt(recordsTotal, 10);
settings._iRecordsDisplay = parseInt(recordsFiltered, 10);
for ( var i=0, ien=data.length ; i<ien ; i++ ) {
_fnAddData( settings, data[i] );
}
settings.aiDisplay = settings.aiDisplayMaster.slice();
_fnDraw( settings, true );
_fnInitComplete( settings );
_fnProcessingDisplay( settings, false );
}
/**
* Get the data from the JSON data source to use for drawing a table. Using
* `_fnGetObjectDataFn` allows the data to be sourced from a property of the
* source object, or from a processing function.
* @param {object} settings dataTables settings object
* @param {object} json Data source object / array from the server
* @return {array} Array of data to use
*/
function _fnAjaxDataSrc ( settings, json, write )
{
var dataProp = 'data';
if ($.isPlainObject( settings.ajax ) && settings.ajax.dataSrc !== undefined) {
// Could in inside a `dataSrc` object, or not!
var dataSrc = settings.ajax.dataSrc;
// string, function and object are valid types
if (typeof dataSrc === 'string' || typeof dataSrc === 'function') {
dataProp = dataSrc;
}
else if (dataSrc.data !== undefined) {
dataProp = dataSrc.data;
}
}
if ( ! write ) {
if ( dataProp === 'data' ) {
// If the default, then we still want to support the old style, and safely ignore
// it if possible
return json.aaData || json[dataProp];
}
return dataProp !== "" ?
_fnGetObjectDataFn( dataProp )( json ) :
json;
}
// set
_fnSetObjectDataFn( dataProp )( json, write );
}
/**
* Very similar to _fnAjaxDataSrc, but for the other SSP properties
* @param {*} settings DataTables settings object
* @param {*} param Target parameter
* @param {*} json JSON data
* @returns Resolved value
*/
function _fnAjaxDataSrcParam (settings, param, json) {
var dataSrc = $.isPlainObject( settings.ajax )
? settings.ajax.dataSrc
: null;
if (dataSrc && dataSrc[param]) {
// Get from custom location
return _fnGetObjectDataFn( dataSrc[param] )( json );
}
// else - Default behaviour
var old = '';
// Legacy support
if (param === 'draw') {
old = 'sEcho';
}
else if (param === 'recordsTotal') {
old = 'iTotalRecords';
}
else if (param === 'recordsFiltered') {
old = 'iTotalDisplayRecords';
}
return json[old] !== undefined
? json[old]
: json[param];
}