Built files from Bizgaze WebServer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ajax.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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; };
  2. /* Tabulator v4.6.3 (c) Oliver Folkerd */
  3. var Ajax = function Ajax(table) {
  4. this.table = table; //hold Tabulator object
  5. this.config = false; //hold config object for ajax request
  6. this.url = ""; //request URL
  7. this.urlGenerator = false;
  8. this.params = false; //request parameters
  9. this.loaderElement = this.createLoaderElement(); //loader message div
  10. this.msgElement = this.createMsgElement(); //message element
  11. this.loadingElement = false;
  12. this.errorElement = false;
  13. this.loaderPromise = false;
  14. this.progressiveLoad = false;
  15. this.loading = false;
  16. this.requestOrder = 0; //prevent requests comming out of sequence if overridden by another load request
  17. };
  18. //initialize setup options
  19. Ajax.prototype.initialize = function () {
  20. var template;
  21. this.loaderElement.appendChild(this.msgElement);
  22. if (this.table.options.ajaxLoaderLoading) {
  23. if (typeof this.table.options.ajaxLoaderLoading == "string") {
  24. template = document.createElement('template');
  25. template.innerHTML = this.table.options.ajaxLoaderLoading.trim();
  26. this.loadingElement = template.content.firstChild;
  27. } else {
  28. this.loadingElement = this.table.options.ajaxLoaderLoading;
  29. }
  30. }
  31. this.loaderPromise = this.table.options.ajaxRequestFunc || this.defaultLoaderPromise;
  32. this.urlGenerator = this.table.options.ajaxURLGenerator || this.defaultURLGenerator;
  33. if (this.table.options.ajaxLoaderError) {
  34. if (typeof this.table.options.ajaxLoaderError == "string") {
  35. template = document.createElement('template');
  36. template.innerHTML = this.table.options.ajaxLoaderError.trim();
  37. this.errorElement = template.content.firstChild;
  38. } else {
  39. this.errorElement = this.table.options.ajaxLoaderError;
  40. }
  41. }
  42. if (this.table.options.ajaxParams) {
  43. this.setParams(this.table.options.ajaxParams);
  44. }
  45. if (this.table.options.ajaxConfig) {
  46. this.setConfig(this.table.options.ajaxConfig);
  47. }
  48. if (this.table.options.ajaxURL) {
  49. this.setUrl(this.table.options.ajaxURL);
  50. }
  51. if (this.table.options.ajaxProgressiveLoad) {
  52. if (this.table.options.pagination) {
  53. this.progressiveLoad = false;
  54. console.error("Progressive Load Error - Pagination and progressive load cannot be used at the same time");
  55. } else {
  56. if (this.table.modExists("page")) {
  57. this.progressiveLoad = this.table.options.ajaxProgressiveLoad;
  58. this.table.modules.page.initializeProgressive(this.progressiveLoad);
  59. } else {
  60. console.error("Pagination plugin is required for progressive ajax loading");
  61. }
  62. }
  63. }
  64. };
  65. Ajax.prototype.createLoaderElement = function () {
  66. var el = document.createElement("div");
  67. el.classList.add("tabulator-loader");
  68. return el;
  69. };
  70. Ajax.prototype.createMsgElement = function () {
  71. var el = document.createElement("div");
  72. el.classList.add("tabulator-loader-msg");
  73. el.setAttribute("role", "alert");
  74. return el;
  75. };
  76. //set ajax params
  77. Ajax.prototype.setParams = function (params, update) {
  78. if (update) {
  79. this.params = this.params || {};
  80. for (var key in params) {
  81. this.params[key] = params[key];
  82. }
  83. } else {
  84. this.params = params;
  85. }
  86. };
  87. Ajax.prototype.getParams = function () {
  88. return this.params || {};
  89. };
  90. //load config object
  91. Ajax.prototype.setConfig = function (config) {
  92. this._loadDefaultConfig();
  93. if (typeof config == "string") {
  94. this.config.method = config;
  95. } else {
  96. for (var key in config) {
  97. this.config[key] = config[key];
  98. }
  99. }
  100. };
  101. //create config object from default
  102. Ajax.prototype._loadDefaultConfig = function (force) {
  103. var self = this;
  104. if (!self.config || force) {
  105. self.config = {};
  106. //load base config from defaults
  107. for (var key in self.defaultConfig) {
  108. self.config[key] = self.defaultConfig[key];
  109. }
  110. }
  111. };
  112. //set request url
  113. Ajax.prototype.setUrl = function (url) {
  114. this.url = url;
  115. };
  116. //get request url
  117. Ajax.prototype.getUrl = function () {
  118. return this.url;
  119. };
  120. //lstandard loading function
  121. Ajax.prototype.loadData = function (inPosition, columnsChanged) {
  122. var self = this;
  123. if (this.progressiveLoad) {
  124. return this._loadDataProgressive();
  125. } else {
  126. return this._loadDataStandard(inPosition, columnsChanged);
  127. }
  128. };
  129. Ajax.prototype.nextPage = function (diff) {
  130. var margin;
  131. if (!this.loading) {
  132. margin = this.table.options.ajaxProgressiveLoadScrollMargin || this.table.rowManager.getElement().clientHeight * 2;
  133. if (diff < margin) {
  134. this.table.modules.page.nextPage().then(function () {}).catch(function () {});
  135. }
  136. }
  137. };
  138. Ajax.prototype.blockActiveRequest = function () {
  139. this.requestOrder++;
  140. };
  141. Ajax.prototype._loadDataProgressive = function () {
  142. this.table.rowManager.setData([]);
  143. return this.table.modules.page.setPage(1);
  144. };
  145. Ajax.prototype._loadDataStandard = function (inPosition, columnsChanged) {
  146. var _this = this;
  147. return new Promise(function (resolve, reject) {
  148. _this.sendRequest(inPosition).then(function (data) {
  149. _this.table.rowManager.setData(data, inPosition, columnsChanged).then(function () {
  150. resolve();
  151. }).catch(function (e) {
  152. reject(e);
  153. });
  154. }).catch(function (e) {
  155. reject(e);
  156. });
  157. });
  158. };
  159. Ajax.prototype.generateParamsList = function (data, prefix) {
  160. var self = this,
  161. output = [];
  162. prefix = prefix || "";
  163. if (Array.isArray(data)) {
  164. data.forEach(function (item, i) {
  165. output = output.concat(self.generateParamsList(item, prefix ? prefix + "[" + i + "]" : i));
  166. });
  167. } else if ((typeof data === "undefined" ? "undefined" : _typeof(data)) === "object") {
  168. for (var key in data) {
  169. output = output.concat(self.generateParamsList(data[key], prefix ? prefix + "[" + key + "]" : key));
  170. }
  171. } else {
  172. output.push({ key: prefix, value: data });
  173. }
  174. return output;
  175. };
  176. Ajax.prototype.serializeParams = function (params) {
  177. var output = this.generateParamsList(params),
  178. encoded = [];
  179. output.forEach(function (item) {
  180. encoded.push(encodeURIComponent(item.key) + "=" + encodeURIComponent(item.value));
  181. });
  182. return encoded.join("&");
  183. };
  184. //send ajax request
  185. Ajax.prototype.sendRequest = function (silent) {
  186. var _this2 = this;
  187. var self = this,
  188. url = self.url,
  189. requestNo,
  190. esc,
  191. query;
  192. self.requestOrder++;
  193. requestNo = self.requestOrder;
  194. self._loadDefaultConfig();
  195. return new Promise(function (resolve, reject) {
  196. if (self.table.options.ajaxRequesting.call(_this2.table, self.url, self.params) !== false) {
  197. self.loading = true;
  198. if (!silent) {
  199. self.showLoader();
  200. }
  201. _this2.loaderPromise(url, self.config, self.params).then(function (data) {
  202. if (requestNo === self.requestOrder) {
  203. if (self.table.options.ajaxResponse) {
  204. data = self.table.options.ajaxResponse.call(self.table, self.url, self.params, data);
  205. }
  206. resolve(data);
  207. self.hideLoader();
  208. self.loading = false;
  209. } else {
  210. console.warn("Ajax Response Blocked - An active ajax request was blocked by an attempt to change table data while the request was being made");
  211. }
  212. }).catch(function (error) {
  213. console.error("Ajax Load Error: ", error);
  214. self.table.options.ajaxError.call(self.table, error);
  215. self.showError();
  216. setTimeout(function () {
  217. self.hideLoader();
  218. }, 3000);
  219. self.loading = false;
  220. reject();
  221. });
  222. } else {
  223. reject();
  224. }
  225. });
  226. };
  227. Ajax.prototype.showLoader = function () {
  228. var shouldLoad = typeof this.table.options.ajaxLoader === "function" ? this.table.options.ajaxLoader() : this.table.options.ajaxLoader;
  229. if (shouldLoad) {
  230. this.hideLoader();
  231. while (this.msgElement.firstChild) {
  232. this.msgElement.removeChild(this.msgElement.firstChild);
  233. }this.msgElement.classList.remove("tabulator-error");
  234. this.msgElement.classList.add("tabulator-loading");
  235. if (this.loadingElement) {
  236. this.msgElement.appendChild(this.loadingElement);
  237. } else {
  238. this.msgElement.innerHTML = this.table.modules.localize.getText("ajax|loading");
  239. }
  240. this.table.element.appendChild(this.loaderElement);
  241. }
  242. };
  243. Ajax.prototype.showError = function () {
  244. this.hideLoader();
  245. while (this.msgElement.firstChild) {
  246. this.msgElement.removeChild(this.msgElement.firstChild);
  247. }this.msgElement.classList.remove("tabulator-loading");
  248. this.msgElement.classList.add("tabulator-error");
  249. if (this.errorElement) {
  250. this.msgElement.appendChild(this.errorElement);
  251. } else {
  252. this.msgElement.innerHTML = this.table.modules.localize.getText("ajax|error");
  253. }
  254. this.table.element.appendChild(this.loaderElement);
  255. };
  256. Ajax.prototype.hideLoader = function () {
  257. if (this.loaderElement.parentNode) {
  258. this.loaderElement.parentNode.removeChild(this.loaderElement);
  259. }
  260. };
  261. //default ajax config object
  262. Ajax.prototype.defaultConfig = {
  263. method: "GET"
  264. };
  265. Ajax.prototype.defaultURLGenerator = function (url, config, params) {
  266. if (url) {
  267. if (params && Object.keys(params).length) {
  268. if (!config.method || config.method.toLowerCase() == "get") {
  269. config.method = "get";
  270. url += (url.includes("?") ? "&" : "?") + this.serializeParams(params);
  271. }
  272. }
  273. }
  274. return url;
  275. };
  276. Ajax.prototype.defaultLoaderPromise = function (url, config, params) {
  277. var self = this,
  278. contentType;
  279. return new Promise(function (resolve, reject) {
  280. //set url
  281. url = self.urlGenerator(url, config, params);
  282. //set body content if not GET request
  283. if (config.method.toUpperCase() != "GET") {
  284. contentType = _typeof(self.table.options.ajaxContentType) === "object" ? self.table.options.ajaxContentType : self.contentTypeFormatters[self.table.options.ajaxContentType];
  285. if (contentType) {
  286. for (var key in contentType.headers) {
  287. if (!config.headers) {
  288. config.headers = {};
  289. }
  290. if (typeof config.headers[key] === "undefined") {
  291. config.headers[key] = contentType.headers[key];
  292. }
  293. }
  294. config.body = contentType.body.call(self, url, config, params);
  295. } else {
  296. console.warn("Ajax Error - Invalid ajaxContentType value:", self.table.options.ajaxContentType);
  297. }
  298. }
  299. if (url) {
  300. //configure headers
  301. if (typeof config.headers === "undefined") {
  302. config.headers = {};
  303. }
  304. if (typeof config.headers.Accept === "undefined") {
  305. config.headers.Accept = "application/json";
  306. }
  307. if (typeof config.headers["X-Requested-With"] === "undefined") {
  308. config.headers["X-Requested-With"] = "XMLHttpRequest";
  309. }
  310. if (typeof config.mode === "undefined") {
  311. config.mode = "cors";
  312. }
  313. if (config.mode == "cors") {
  314. if (typeof config.headers["Access-Control-Allow-Origin"] === "undefined") {
  315. config.headers["Access-Control-Allow-Origin"] = window.location.origin;
  316. }
  317. if (typeof config.credentials === "undefined") {
  318. config.credentials = 'same-origin';
  319. }
  320. } else {
  321. if (typeof config.credentials === "undefined") {
  322. config.credentials = 'include';
  323. }
  324. }
  325. //send request
  326. fetch(url, config).then(function (response) {
  327. if (response.ok) {
  328. response.json().then(function (data) {
  329. resolve(data);
  330. }).catch(function (error) {
  331. reject(error);
  332. console.warn("Ajax Load Error - Invalid JSON returned", error);
  333. });
  334. } else {
  335. console.error("Ajax Load Error - Connection Error: " + response.status, response.statusText);
  336. reject(response);
  337. }
  338. }).catch(function (error) {
  339. console.error("Ajax Load Error - Connection Error: ", error);
  340. reject(error);
  341. });
  342. } else {
  343. console.warn("Ajax Load Error - No URL Set");
  344. resolve([]);
  345. }
  346. });
  347. };
  348. Ajax.prototype.contentTypeFormatters = {
  349. "json": {
  350. headers: {
  351. 'Content-Type': 'application/json'
  352. },
  353. body: function body(url, config, params) {
  354. return JSON.stringify(params);
  355. }
  356. },
  357. "form": {
  358. headers: {},
  359. body: function body(url, config, params) {
  360. var output = this.generateParamsList(params),
  361. form = new FormData();
  362. output.forEach(function (item) {
  363. form.append(item.key, item.value);
  364. });
  365. return form;
  366. }
  367. }
  368. };
  369. Tabulator.prototype.registerModule("ajax", Ajax);