ajax.js 20 KB

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