ajax.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  8. (function ($, window, Drupal, drupalSettings) {
  9. Drupal.behaviors.AJAX = {
  10. attach: function attach(context, settings) {
  11. function loadAjaxBehavior(base) {
  12. var elementSettings = settings.ajax[base];
  13. if (typeof elementSettings.selector === 'undefined') {
  14. elementSettings.selector = '#' + base;
  15. }
  16. $(elementSettings.selector).once('drupal-ajax').each(function () {
  17. elementSettings.element = this;
  18. elementSettings.base = base;
  19. Drupal.ajax(elementSettings);
  20. });
  21. }
  22. Object.keys(settings.ajax || {}).forEach(function (base) {
  23. return loadAjaxBehavior(base);
  24. });
  25. Drupal.ajax.bindAjaxLinks(document.body);
  26. $('.use-ajax-submit').once('ajax').each(function () {
  27. var elementSettings = {};
  28. elementSettings.url = $(this.form).attr('action');
  29. elementSettings.setClick = true;
  30. elementSettings.event = 'click';
  31. elementSettings.progress = { type: 'throbber' };
  32. elementSettings.base = $(this).attr('id');
  33. elementSettings.element = this;
  34. Drupal.ajax(elementSettings);
  35. });
  36. },
  37. detach: function detach(context, settings, trigger) {
  38. if (trigger === 'unload') {
  39. Drupal.ajax.expired().forEach(function (instance) {
  40. Drupal.ajax.instances[instance.instanceIndex] = null;
  41. });
  42. }
  43. }
  44. };
  45. Drupal.AjaxError = function (xmlhttp, uri, customMessage) {
  46. var statusCode = void 0;
  47. var statusText = void 0;
  48. var responseText = void 0;
  49. if (xmlhttp.status) {
  50. statusCode = '\n' + Drupal.t('An AJAX HTTP error occurred.') + '\n' + Drupal.t('HTTP Result Code: !status', { '!status': xmlhttp.status });
  51. } else {
  52. statusCode = '\n' + Drupal.t('An AJAX HTTP request terminated abnormally.');
  53. }
  54. statusCode += '\n' + Drupal.t('Debugging information follows.');
  55. var pathText = '\n' + Drupal.t('Path: !uri', { '!uri': uri });
  56. statusText = '';
  57. try {
  58. statusText = '\n' + Drupal.t('StatusText: !statusText', { '!statusText': $.trim(xmlhttp.statusText) });
  59. } catch (e) {}
  60. responseText = '';
  61. try {
  62. responseText = '\n' + Drupal.t('ResponseText: !responseText', { '!responseText': $.trim(xmlhttp.responseText) });
  63. } catch (e) {}
  64. responseText = responseText.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, '');
  65. responseText = responseText.replace(/[\n]+\s+/g, '\n');
  66. var readyStateText = xmlhttp.status === 0 ? '\n' + Drupal.t('ReadyState: !readyState', { '!readyState': xmlhttp.readyState }) : '';
  67. customMessage = customMessage ? '\n' + Drupal.t('CustomMessage: !customMessage', { '!customMessage': customMessage }) : '';
  68. this.message = statusCode + pathText + statusText + customMessage + responseText + readyStateText;
  69. this.name = 'AjaxError';
  70. };
  71. Drupal.AjaxError.prototype = new Error();
  72. Drupal.AjaxError.prototype.constructor = Drupal.AjaxError;
  73. Drupal.ajax = function (settings) {
  74. if (arguments.length !== 1) {
  75. throw new Error('Drupal.ajax() function must be called with one configuration object only');
  76. }
  77. var base = settings.base || false;
  78. var element = settings.element || false;
  79. delete settings.base;
  80. delete settings.element;
  81. if (!settings.progress && !element) {
  82. settings.progress = false;
  83. }
  84. var ajax = new Drupal.Ajax(base, element, settings);
  85. ajax.instanceIndex = Drupal.ajax.instances.length;
  86. Drupal.ajax.instances.push(ajax);
  87. return ajax;
  88. };
  89. Drupal.ajax.instances = [];
  90. Drupal.ajax.expired = function () {
  91. return Drupal.ajax.instances.filter(function (instance) {
  92. return instance && instance.element !== false && !document.body.contains(instance.element);
  93. });
  94. };
  95. Drupal.ajax.bindAjaxLinks = function (element) {
  96. $(element).find('.use-ajax').once('ajax').each(function (i, ajaxLink) {
  97. var $linkElement = $(ajaxLink);
  98. var elementSettings = {
  99. progress: { type: 'throbber' },
  100. dialogType: $linkElement.data('dialog-type'),
  101. dialog: $linkElement.data('dialog-options'),
  102. dialogRenderer: $linkElement.data('dialog-renderer'),
  103. base: $linkElement.attr('id'),
  104. element: ajaxLink
  105. };
  106. var href = $linkElement.attr('href');
  107. if (href) {
  108. elementSettings.url = href;
  109. elementSettings.event = 'click';
  110. }
  111. Drupal.ajax(elementSettings);
  112. });
  113. };
  114. Drupal.Ajax = function (base, element, elementSettings) {
  115. var defaults = {
  116. event: element ? 'mousedown' : null,
  117. keypress: true,
  118. selector: base ? '#' + base : null,
  119. effect: 'none',
  120. speed: 'none',
  121. method: 'replaceWith',
  122. progress: {
  123. type: 'throbber',
  124. message: Drupal.t('Please wait...')
  125. },
  126. submit: {
  127. js: true
  128. }
  129. };
  130. $.extend(this, defaults, elementSettings);
  131. this.commands = new Drupal.AjaxCommands();
  132. this.instanceIndex = false;
  133. if (this.wrapper) {
  134. this.wrapper = '#' + this.wrapper;
  135. }
  136. this.element = element;
  137. this.element_settings = elementSettings;
  138. this.elementSettings = elementSettings;
  139. if (this.element && this.element.form) {
  140. this.$form = $(this.element.form);
  141. }
  142. if (!this.url) {
  143. var $element = $(this.element);
  144. if ($element.is('a')) {
  145. this.url = $element.attr('href');
  146. } else if (this.element && element.form) {
  147. this.url = this.$form.attr('action');
  148. }
  149. }
  150. var originalUrl = this.url;
  151. this.url = this.url.replace(/\/nojs(\/|$|\?|#)/g, '/ajax$1');
  152. if (drupalSettings.ajaxTrustedUrl[originalUrl]) {
  153. drupalSettings.ajaxTrustedUrl[this.url] = true;
  154. }
  155. var ajax = this;
  156. ajax.options = {
  157. url: ajax.url,
  158. data: ajax.submit,
  159. beforeSerialize: function beforeSerialize(elementSettings, options) {
  160. return ajax.beforeSerialize(elementSettings, options);
  161. },
  162. beforeSubmit: function beforeSubmit(formValues, elementSettings, options) {
  163. ajax.ajaxing = true;
  164. return ajax.beforeSubmit(formValues, elementSettings, options);
  165. },
  166. beforeSend: function beforeSend(xmlhttprequest, options) {
  167. ajax.ajaxing = true;
  168. return ajax.beforeSend(xmlhttprequest, options);
  169. },
  170. success: function success(response, status, xmlhttprequest) {
  171. if (typeof response === 'string') {
  172. response = $.parseJSON(response);
  173. }
  174. if (response !== null && !drupalSettings.ajaxTrustedUrl[ajax.url]) {
  175. if (xmlhttprequest.getResponseHeader('X-Drupal-Ajax-Token') !== '1') {
  176. var customMessage = Drupal.t('The response failed verification so will not be processed.');
  177. return ajax.error(xmlhttprequest, ajax.url, customMessage);
  178. }
  179. }
  180. return ajax.success(response, status);
  181. },
  182. complete: function complete(xmlhttprequest, status) {
  183. ajax.ajaxing = false;
  184. if (status === 'error' || status === 'parsererror') {
  185. return ajax.error(xmlhttprequest, ajax.url);
  186. }
  187. },
  188. dataType: 'json',
  189. type: 'POST'
  190. };
  191. if (elementSettings.dialog) {
  192. ajax.options.data.dialogOptions = elementSettings.dialog;
  193. }
  194. if (ajax.options.url.indexOf('?') === -1) {
  195. ajax.options.url += '?';
  196. } else {
  197. ajax.options.url += '&';
  198. }
  199. var wrapper = 'drupal_' + (elementSettings.dialogType || 'ajax');
  200. if (elementSettings.dialogRenderer) {
  201. wrapper += '.' + elementSettings.dialogRenderer;
  202. }
  203. ajax.options.url += Drupal.ajax.WRAPPER_FORMAT + '=' + wrapper;
  204. $(ajax.element).on(elementSettings.event, function (event) {
  205. if (!drupalSettings.ajaxTrustedUrl[ajax.url] && !Drupal.url.isLocal(ajax.url)) {
  206. throw new Error(Drupal.t('The callback URL is not local and not trusted: !url', { '!url': ajax.url }));
  207. }
  208. return ajax.eventResponse(this, event);
  209. });
  210. if (elementSettings.keypress) {
  211. $(ajax.element).on('keypress', function (event) {
  212. return ajax.keypressResponse(this, event);
  213. });
  214. }
  215. if (elementSettings.prevent) {
  216. $(ajax.element).on(elementSettings.prevent, false);
  217. }
  218. };
  219. Drupal.ajax.WRAPPER_FORMAT = '_wrapper_format';
  220. Drupal.Ajax.AJAX_REQUEST_PARAMETER = '_drupal_ajax';
  221. Drupal.Ajax.prototype.execute = function () {
  222. if (this.ajaxing) {
  223. return;
  224. }
  225. try {
  226. this.beforeSerialize(this.element, this.options);
  227. return $.ajax(this.options);
  228. } catch (e) {
  229. this.ajaxing = false;
  230. window.alert('An error occurred while attempting to process ' + this.options.url + ': ' + e.message);
  231. return $.Deferred().reject();
  232. }
  233. };
  234. Drupal.Ajax.prototype.keypressResponse = function (element, event) {
  235. var ajax = this;
  236. if (event.which === 13 || event.which === 32 && element.type !== 'text' && element.type !== 'textarea' && element.type !== 'tel' && element.type !== 'number') {
  237. event.preventDefault();
  238. event.stopPropagation();
  239. $(element).trigger(ajax.elementSettings.event);
  240. }
  241. };
  242. Drupal.Ajax.prototype.eventResponse = function (element, event) {
  243. event.preventDefault();
  244. event.stopPropagation();
  245. var ajax = this;
  246. if (ajax.ajaxing) {
  247. return;
  248. }
  249. try {
  250. if (ajax.$form) {
  251. if (ajax.setClick) {
  252. element.form.clk = element;
  253. }
  254. ajax.$form.ajaxSubmit(ajax.options);
  255. } else {
  256. ajax.beforeSerialize(ajax.element, ajax.options);
  257. $.ajax(ajax.options);
  258. }
  259. } catch (e) {
  260. ajax.ajaxing = false;
  261. window.alert('An error occurred while attempting to process ' + ajax.options.url + ': ' + e.message);
  262. }
  263. };
  264. Drupal.Ajax.prototype.beforeSerialize = function (element, options) {
  265. if (this.$form) {
  266. var settings = this.settings || drupalSettings;
  267. Drupal.detachBehaviors(this.$form.get(0), settings, 'serialize');
  268. }
  269. options.data[Drupal.Ajax.AJAX_REQUEST_PARAMETER] = 1;
  270. var pageState = drupalSettings.ajaxPageState;
  271. options.data['ajax_page_state[theme]'] = pageState.theme;
  272. options.data['ajax_page_state[theme_token]'] = pageState.theme_token;
  273. options.data['ajax_page_state[libraries]'] = pageState.libraries;
  274. };
  275. Drupal.Ajax.prototype.beforeSubmit = function (formValues, element, options) {};
  276. Drupal.Ajax.prototype.beforeSend = function (xmlhttprequest, options) {
  277. if (this.$form) {
  278. options.extraData = options.extraData || {};
  279. options.extraData.ajax_iframe_upload = '1';
  280. var v = $.fieldValue(this.element);
  281. if (v !== null) {
  282. options.extraData[this.element.name] = v;
  283. }
  284. }
  285. $(this.element).prop('disabled', true);
  286. if (!this.progress || !this.progress.type) {
  287. return;
  288. }
  289. var progressIndicatorMethod = 'setProgressIndicator' + this.progress.type.slice(0, 1).toUpperCase() + this.progress.type.slice(1).toLowerCase();
  290. if (progressIndicatorMethod in this && typeof this[progressIndicatorMethod] === 'function') {
  291. this[progressIndicatorMethod].call(this);
  292. }
  293. };
  294. Drupal.Ajax.prototype.setProgressIndicatorBar = function () {
  295. var progressBar = new Drupal.ProgressBar('ajax-progress-' + this.element.id, $.noop, this.progress.method, $.noop);
  296. if (this.progress.message) {
  297. progressBar.setProgress(-1, this.progress.message);
  298. }
  299. if (this.progress.url) {
  300. progressBar.startMonitoring(this.progress.url, this.progress.interval || 1500);
  301. }
  302. this.progress.element = $(progressBar.element).addClass('ajax-progress ajax-progress-bar');
  303. this.progress.object = progressBar;
  304. $(this.element).after(this.progress.element);
  305. };
  306. Drupal.Ajax.prototype.setProgressIndicatorThrobber = function () {
  307. this.progress.element = $('<div class="ajax-progress ajax-progress-throbber"><div class="throbber">&nbsp;</div></div>');
  308. if (this.progress.message) {
  309. this.progress.element.find('.throbber').after('<div class="message">' + this.progress.message + '</div>');
  310. }
  311. $(this.element).after(this.progress.element);
  312. };
  313. Drupal.Ajax.prototype.setProgressIndicatorFullscreen = function () {
  314. this.progress.element = $('<div class="ajax-progress ajax-progress-fullscreen">&nbsp;</div>');
  315. $('body').after(this.progress.element);
  316. };
  317. Drupal.Ajax.prototype.success = function (response, status) {
  318. var _this = this;
  319. if (this.progress.element) {
  320. $(this.progress.element).remove();
  321. }
  322. if (this.progress.object) {
  323. this.progress.object.stopMonitoring();
  324. }
  325. $(this.element).prop('disabled', false);
  326. var elementParents = $(this.element).parents('[data-drupal-selector]').addBack().toArray();
  327. var focusChanged = false;
  328. Object.keys(response || {}).forEach(function (i) {
  329. if (response[i].command && _this.commands[response[i].command]) {
  330. _this.commands[response[i].command](_this, response[i], status);
  331. if (response[i].command === 'invoke' && response[i].method === 'focus') {
  332. focusChanged = true;
  333. }
  334. }
  335. });
  336. if (!focusChanged && this.element && !$(this.element).data('disable-refocus')) {
  337. var target = false;
  338. for (var n = elementParents.length - 1; !target && n > 0; n--) {
  339. target = document.querySelector('[data-drupal-selector="' + elementParents[n].getAttribute('data-drupal-selector') + '"]');
  340. }
  341. if (target) {
  342. $(target).trigger('focus');
  343. }
  344. }
  345. if (this.$form) {
  346. var settings = this.settings || drupalSettings;
  347. Drupal.attachBehaviors(this.$form.get(0), settings);
  348. }
  349. this.settings = null;
  350. };
  351. Drupal.Ajax.prototype.getEffect = function (response) {
  352. var type = response.effect || this.effect;
  353. var speed = response.speed || this.speed;
  354. var effect = {};
  355. if (type === 'none') {
  356. effect.showEffect = 'show';
  357. effect.hideEffect = 'hide';
  358. effect.showSpeed = '';
  359. } else if (type === 'fade') {
  360. effect.showEffect = 'fadeIn';
  361. effect.hideEffect = 'fadeOut';
  362. effect.showSpeed = speed;
  363. } else {
  364. effect.showEffect = type + 'Toggle';
  365. effect.hideEffect = type + 'Toggle';
  366. effect.showSpeed = speed;
  367. }
  368. return effect;
  369. };
  370. Drupal.Ajax.prototype.error = function (xmlhttprequest, uri, customMessage) {
  371. if (this.progress.element) {
  372. $(this.progress.element).remove();
  373. }
  374. if (this.progress.object) {
  375. this.progress.object.stopMonitoring();
  376. }
  377. $(this.wrapper).show();
  378. $(this.element).prop('disabled', false);
  379. if (this.$form) {
  380. var settings = this.settings || drupalSettings;
  381. Drupal.attachBehaviors(this.$form.get(0), settings);
  382. }
  383. throw new Drupal.AjaxError(xmlhttprequest, uri, customMessage);
  384. };
  385. Drupal.AjaxCommands = function () {};
  386. Drupal.AjaxCommands.prototype = {
  387. insert: function insert(ajax, response, status) {
  388. var $wrapper = response.selector ? $(response.selector) : $(ajax.wrapper);
  389. var method = response.method || ajax.method;
  390. var effect = ajax.getEffect(response);
  391. var settings = void 0;
  392. var $newContentWrapped = $('<div></div>').html(response.data);
  393. var $newContent = $newContentWrapped.contents();
  394. if ($newContent.length !== 1 || $newContent.get(0).nodeType !== 1) {
  395. $newContent = $newContentWrapped;
  396. }
  397. switch (method) {
  398. case 'html':
  399. case 'replaceWith':
  400. case 'replaceAll':
  401. case 'empty':
  402. case 'remove':
  403. settings = response.settings || ajax.settings || drupalSettings;
  404. Drupal.detachBehaviors($wrapper.get(0), settings);
  405. }
  406. $wrapper[method]($newContent);
  407. if (effect.showEffect !== 'show') {
  408. $newContent.hide();
  409. }
  410. if ($newContent.find('.ajax-new-content').length > 0) {
  411. $newContent.find('.ajax-new-content').hide();
  412. $newContent.show();
  413. $newContent.find('.ajax-new-content')[effect.showEffect](effect.showSpeed);
  414. } else if (effect.showEffect !== 'show') {
  415. $newContent[effect.showEffect](effect.showSpeed);
  416. }
  417. if ($newContent.parents('html').length > 0) {
  418. settings = response.settings || ajax.settings || drupalSettings;
  419. Drupal.attachBehaviors($newContent.get(0), settings);
  420. }
  421. },
  422. remove: function remove(ajax, response, status) {
  423. var settings = response.settings || ajax.settings || drupalSettings;
  424. $(response.selector).each(function () {
  425. Drupal.detachBehaviors(this, settings);
  426. }).remove();
  427. },
  428. changed: function changed(ajax, response, status) {
  429. var $element = $(response.selector);
  430. if (!$element.hasClass('ajax-changed')) {
  431. $element.addClass('ajax-changed');
  432. if (response.asterisk) {
  433. $element.find(response.asterisk).append(' <abbr class="ajax-changed" title="' + Drupal.t('Changed') + '">*</abbr> ');
  434. }
  435. }
  436. },
  437. alert: function alert(ajax, response, status) {
  438. window.alert(response.text, response.title);
  439. },
  440. redirect: function redirect(ajax, response, status) {
  441. window.location = response.url;
  442. },
  443. css: function css(ajax, response, status) {
  444. $(response.selector).css(response.argument);
  445. },
  446. settings: function settings(ajax, response, status) {
  447. var ajaxSettings = drupalSettings.ajax;
  448. if (ajaxSettings) {
  449. Drupal.ajax.expired().forEach(function (instance) {
  450. if (instance.selector) {
  451. var selector = instance.selector.replace('#', '');
  452. if (selector in ajaxSettings) {
  453. delete ajaxSettings[selector];
  454. }
  455. }
  456. });
  457. }
  458. if (response.merge) {
  459. $.extend(true, drupalSettings, response.settings);
  460. } else {
  461. ajax.settings = response.settings;
  462. }
  463. },
  464. data: function data(ajax, response, status) {
  465. $(response.selector).data(response.name, response.value);
  466. },
  467. invoke: function invoke(ajax, response, status) {
  468. var $element = $(response.selector);
  469. $element[response.method].apply($element, _toConsumableArray(response.args));
  470. },
  471. restripe: function restripe(ajax, response, status) {
  472. $(response.selector).find('> tbody > tr:visible, > tr:visible').removeClass('odd even').filter(':even').addClass('odd').end().filter(':odd').addClass('even');
  473. },
  474. update_build_id: function update_build_id(ajax, response, status) {
  475. $('input[name="form_build_id"][value="' + response.old + '"]').val(response.new);
  476. },
  477. add_css: function add_css(ajax, response, status) {
  478. $('head').prepend(response.data);
  479. var match = void 0;
  480. var importMatch = /^@import url\("(.*)"\);$/igm;
  481. if (document.styleSheets[0].addImport && importMatch.test(response.data)) {
  482. importMatch.lastIndex = 0;
  483. do {
  484. match = importMatch.exec(response.data);
  485. document.styleSheets[0].addImport(match[1]);
  486. } while (match);
  487. }
  488. }
  489. };
  490. })(jQuery, window, Drupal, drupalSettings);