1 var _typeof = typeof Symbol ===
"function" && typeof Symbol.iterator ===
"symbol" ?
function (obj) {
return typeof obj; } :
function (obj) {
return obj && typeof Symbol ===
"function" && obj.constructor === Symbol && obj !== Symbol.prototype ?
"symbol" : typeof obj; };
5 var Ajax =
function Ajax(table) {
10 this.urlGenerator =
false;
13 this.loaderElement = this.createLoaderElement();
14 this.msgElement = this.createMsgElement();
15 this.loadingElement =
false;
16 this.errorElement =
false;
17 this.loaderPromise =
false;
19 this.progressiveLoad =
false;
22 this.requestOrder = 0;
26 Ajax.prototype.initialize =
function () {
29 this.loaderElement.appendChild(this.msgElement);
31 if (this.table.options.ajaxLoaderLoading) {
32 if (typeof this.table.options.ajaxLoaderLoading ==
"string") {
33 template = document.createElement(
'template');
34 template.innerHTML = this.table.options.ajaxLoaderLoading.trim();
35 this.loadingElement =
template.content.firstChild;
37 this.loadingElement = this.table.options.ajaxLoaderLoading;
41 this.loaderPromise = this.table.options.ajaxRequestFunc || this.defaultLoaderPromise;
43 this.urlGenerator = this.table.options.ajaxURLGenerator || this.defaultURLGenerator;
45 if (this.table.options.ajaxLoaderError) {
46 if (typeof this.table.options.ajaxLoaderError ==
"string") {
47 template = document.createElement(
'template');
48 template.innerHTML = this.table.options.ajaxLoaderError.trim();
49 this.errorElement =
template.content.firstChild;
51 this.errorElement = this.table.options.ajaxLoaderError;
55 if (this.table.options.ajaxParams) {
56 this.setParams(this.table.options.ajaxParams);
59 if (this.table.options.ajaxConfig) {
60 this.setConfig(this.table.options.ajaxConfig);
63 if (this.table.options.ajaxURL) {
64 this.setUrl(this.table.options.ajaxURL);
67 if (this.table.options.ajaxProgressiveLoad) {
68 if (this.table.options.pagination) {
69 this.progressiveLoad =
false;
70 console.error(
"Progressive Load Error - Pagination and progressive load cannot be used at the same time");
72 if (this.table.modExists(
"page")) {
73 this.progressiveLoad = this.table.options.ajaxProgressiveLoad;
74 this.table.modules.page.initializeProgressive(this.progressiveLoad);
76 console.error(
"Pagination plugin is required for progressive ajax loading");
82 Ajax.prototype.createLoaderElement =
function () {
83 var el = document.createElement(
"div");
84 el.classList.add(
"tabulator-loader");
88 Ajax.prototype.createMsgElement =
function () {
89 var el = document.createElement(
"div");
91 el.classList.add(
"tabulator-loader-msg");
92 el.setAttribute(
"role",
"alert");
98 Ajax.prototype.setParams =
function (params, update) {
100 this.params = this.params || {};
102 for (var key in params) {
103 this.params[key] = params[key];
106 this.params = params;
110 Ajax.prototype.getParams =
function () {
111 return this.params || {};
115 Ajax.prototype.setConfig =
function (config) {
116 this._loadDefaultConfig();
118 if (typeof config ==
"string") {
119 this.config.method = config;
121 for (var key in config) {
122 this.config[key] = config[key];
128 Ajax.prototype._loadDefaultConfig =
function (force) {
130 if (!
self.config || force) {
135 for (var key in
self.defaultConfig) {
136 self.config[key] =
self.defaultConfig[key];
142 Ajax.prototype.setUrl =
function (url) {
147 Ajax.prototype.getUrl =
function () {
152 Ajax.prototype.loadData =
function (inPosition) {
155 if (this.progressiveLoad) {
156 return this._loadDataProgressive();
158 return this._loadDataStandard(inPosition);
162 Ajax.prototype.nextPage =
function (diff) {
167 margin = this.table.options.ajaxProgressiveLoadScrollMargin || this.table.rowManager.getElement().clientHeight * 2;
170 this.table.modules.page.nextPage().then(
function () {}).
catch(
function () {});
175 Ajax.prototype.blockActiveRequest =
function () {
179 Ajax.prototype._loadDataProgressive =
function () {
180 this.table.rowManager.setData([]);
181 return this.table.modules.page.setPage(1);
184 Ajax.prototype._loadDataStandard =
function (inPosition) {
187 return new Promise(
function (resolve, reject) {
188 _this.sendRequest(inPosition).then(
function (data) {
189 _this.table.rowManager.setData(data, inPosition).then(
function () {
191 }).
catch(
function (e) {
194 }).
catch(
function (e) {
200 Ajax.prototype.generateParamsList =
function (data, prefix) {
204 prefix = prefix ||
"";
206 if (Array.isArray(data)) {
207 data.forEach(
function (item, i) {
208 output = output.concat(
self.generateParamsList(item, prefix ? prefix +
"[" + i +
"]" : i));
210 }
else if ((typeof data ===
"undefined" ?
"undefined" : _typeof(data)) ===
"object") {
211 for (var key in data) {
212 output = output.concat(
self.generateParamsList(data[key], prefix ? prefix +
"[" + key +
"]" : key));
215 output.push({ key: prefix, value: data });
221 Ajax.prototype.serializeParams =
function (params) {
222 var output = this.generateParamsList(params),
225 output.forEach(
function (item) {
226 encoded.push(encodeURIComponent(item.key) +
"=" + encodeURIComponent(item.value));
229 return encoded.join(
"&");
233 Ajax.prototype.sendRequest =
function (silent) {
243 requestNo =
self.requestOrder;
245 self._loadDefaultConfig();
247 return new Promise(
function (resolve, reject) {
248 if (
self.table.options.ajaxRequesting.call(_this2.table,
self.url,
self.params) !==
false) {
256 _this2.loaderPromise(url,
self.config,
self.params).then(
function (data) {
257 if (requestNo ===
self.requestOrder) {
258 if (
self.table.options.ajaxResponse) {
259 data =
self.table.options.ajaxResponse.call(
self.table,
self.url,
self.params, data);
263 console.warn(
"Ajax Response Blocked - An active ajax request was blocked by an attempt to change table data while the request was being made");
268 self.loading =
false;
269 }).
catch(
function (error) {
270 console.error(
"Ajax Load Error: ", error);
271 self.table.options.ajaxError.call(
self.table, error);
275 setTimeout(
function () {
279 self.loading =
false;
289 Ajax.prototype.showLoader =
function () {
290 var shouldLoad = typeof this.table.options.ajaxLoader ===
"function" ? this.table.options.ajaxLoader() : this.table.options.ajaxLoader;
296 while (this.msgElement.firstChild) {
297 this.msgElement.removeChild(this.msgElement.firstChild);
298 }this.msgElement.classList.remove(
"tabulator-error");
299 this.msgElement.classList.add(
"tabulator-loading");
301 if (this.loadingElement) {
302 this.msgElement.appendChild(this.loadingElement);
304 this.msgElement.innerHTML = this.table.modules.localize.getText(
"ajax|loading");
307 this.table.element.appendChild(this.loaderElement);
311 Ajax.prototype.showError =
function () {
314 while (this.msgElement.firstChild) {
315 this.msgElement.removeChild(this.msgElement.firstChild);
316 }this.msgElement.classList.remove(
"tabulator-loading");
317 this.msgElement.classList.add(
"tabulator-error");
319 if (this.errorElement) {
320 this.msgElement.appendChild(this.errorElement);
322 this.msgElement.innerHTML = this.table.modules.localize.getText(
"ajax|error");
325 this.table.element.appendChild(this.loaderElement);
328 Ajax.prototype.hideLoader =
function () {
329 if (this.loaderElement.parentNode) {
330 this.loaderElement.parentNode.removeChild(this.loaderElement);
335 Ajax.prototype.defaultConfig = {
339 Ajax.prototype.defaultURLGenerator =
function (url, config, params) {
342 if (params && Object.keys(params).length) {
343 if (!config.method || config.method.toLowerCase() ==
"get") {
344 config.method =
"get";
346 url += (url.includes(
"?") ?
"&" :
"?") + this.serializeParams(params);
354 Ajax.prototype.defaultLoaderPromise =
function (url, config, params) {
358 return new Promise(
function (resolve, reject) {
361 url =
self.urlGenerator(url, config, params);
364 if (config.method.toUpperCase() !=
"GET") {
365 contentType = _typeof(
self.table.options.ajaxContentType) ===
"object" ?
self.table.options.ajaxContentType :
self.contentTypeFormatters[
self.table.options.ajaxContentType];
368 for (var key in contentType.headers) {
369 if (!config.headers) {
373 if (typeof config.headers[key] ===
"undefined") {
374 config.headers[key] = contentType.headers[key];
378 config.body = contentType.body.call(
self, url, config, params);
380 console.warn(
"Ajax Error - Invalid ajaxContentType value:",
self.table.options.ajaxContentType);
387 if (typeof config.headers ===
"undefined") {
391 if (typeof config.headers.Accept ===
"undefined") {
392 config.headers.Accept =
"application/json";
395 if (typeof config.headers[
"X-Requested-With"] ===
"undefined") {
396 config.headers[
"X-Requested-With"] =
"XMLHttpRequest";
399 if (typeof config.mode ===
"undefined") {
400 config.mode =
"cors";
403 if (config.mode ==
"cors") {
405 if (typeof config.headers[
"Access-Control-Allow-Origin"] ===
"undefined") {
406 config.headers[
"Access-Control-Allow-Origin"] = window.location.origin;
409 if (typeof config.credentials ===
"undefined") {
410 config.credentials =
'same-origin';
413 if (typeof config.credentials ===
"undefined") {
414 config.credentials =
'include';
419 fetch(url, config).then(
function (response) {
421 response.json().then(
function (data) {
423 }).
catch(
function (error) {
425 console.warn(
"Ajax Load Error - Invalid JSON returned", error);
428 console.error(
"Ajax Load Error - Connection Error: " + response.status, response.statusText);
431 }).
catch(
function (error) {
432 console.error(
"Ajax Load Error - Connection Error: ", error);
436 console.warn(
"Ajax Load Error - No URL Set");
442 Ajax.prototype.contentTypeFormatters = {
445 'Content-Type':
'application/json'
447 body:
function body(url, config, params) {
448 return JSON.stringify(params);
453 body:
function body(url, config, params) {
454 var output = this.generateParamsList(params),
455 form =
new FormData();
457 output.forEach(
function (item) {
458 form.append(item.key, item.value);
466 Tabulator.prototype.registerModule(
"ajax", Ajax);