ajax.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. jsonp: false,
  200. type: 'POST'
  201. };
  202. if (elementSettings.dialog) {
  203. ajax.options.data.dialogOptions = elementSettings.dialog;
  204. }
  205. if (ajax.options.url.indexOf('?') === -1) {
  206. ajax.options.url += '?';
  207. } else {
  208. ajax.options.url += '&';
  209. }
  210. var wrapper = 'drupal_' + (elementSettings.dialogType || 'ajax');
  211. if (elementSettings.dialogRenderer) {
  212. wrapper += '.' + elementSettings.dialogRenderer;
  213. }
  214. ajax.options.url += Drupal.ajax.WRAPPER_FORMAT + '=' + wrapper;
  215. $(ajax.element).on(elementSettings.event, function (event) {
  216. if (!drupalSettings.ajaxTrustedUrl[ajax.url] && !Drupal.url.isLocal(ajax.url)) {
  217. throw new Error(Drupal.t('The callback URL is not local and not trusted: !url', {
  218. '!url': ajax.url
  219. }));
  220. }
  221. return ajax.eventResponse(this, event);
  222. });
  223. if (elementSettings.keypress) {
  224. $(ajax.element).on('keypress', function (event) {
  225. return ajax.keypressResponse(this, event);
  226. });
  227. }
  228. if (elementSettings.prevent) {
  229. $(ajax.element).on(elementSettings.prevent, false);
  230. }
  231. };
  232. Drupal.ajax.WRAPPER_FORMAT = '_wrapper_format';
  233. Drupal.Ajax.AJAX_REQUEST_PARAMETER = '_drupal_ajax';
  234. Drupal.Ajax.prototype.execute = function () {
  235. if (this.ajaxing) {
  236. return;
  237. }
  238. try {
  239. this.beforeSerialize(this.element, this.options);
  240. return $.ajax(this.options);
  241. } catch (e) {
  242. this.ajaxing = false;
  243. window.alert('An error occurred while attempting to process ' + this.options.url + ': ' + e.message);
  244. return $.Deferred().reject();
  245. }
  246. };
  247. Drupal.Ajax.prototype.keypressResponse = function (element, event) {
  248. var ajax = this;
  249. if (event.which === 13 || event.which === 32 && element.type !== 'text' && element.type !== 'textarea' && element.type !== 'tel' && element.type !== 'number') {
  250. event.preventDefault();
  251. event.stopPropagation();
  252. $(element).trigger(ajax.elementSettings.event);
  253. }
  254. };
  255. Drupal.Ajax.prototype.eventResponse = function (element, event) {
  256. event.preventDefault();
  257. event.stopPropagation();
  258. var ajax = this;
  259. if (ajax.ajaxing) {
  260. return;
  261. }
  262. try {
  263. if (ajax.$form) {
  264. if (ajax.setClick) {
  265. element.form.clk = element;
  266. }
  267. ajax.$form.ajaxSubmit(ajax.options);
  268. } else {
  269. ajax.beforeSerialize(ajax.element, ajax.options);
  270. $.ajax(ajax.options);
  271. }
  272. } catch (e) {
  273. ajax.ajaxing = false;
  274. window.alert('An error occurred while attempting to process ' + ajax.options.url + ': ' + e.message);
  275. }
  276. };
  277. Drupal.Ajax.prototype.beforeSerialize = function (element, options) {
  278. if (this.$form && document.body.contains(this.$form.get(0))) {
  279. var settings = this.settings || drupalSettings;
  280. Drupal.detachBehaviors(this.$form.get(0), settings, 'serialize');
  281. }
  282. options.data[Drupal.Ajax.AJAX_REQUEST_PARAMETER] = 1;
  283. var pageState = drupalSettings.ajaxPageState;
  284. options.data['ajax_page_state[theme]'] = pageState.theme;
  285. options.data['ajax_page_state[theme_token]'] = pageState.theme_token;
  286. options.data['ajax_page_state[libraries]'] = pageState.libraries;
  287. };
  288. Drupal.Ajax.prototype.beforeSubmit = function (formValues, element, options) {};
  289. Drupal.Ajax.prototype.beforeSend = function (xmlhttprequest, options) {
  290. if (this.$form) {
  291. options.extraData = options.extraData || {};
  292. options.extraData.ajax_iframe_upload = '1';
  293. var v = $.fieldValue(this.element);
  294. if (v !== null) {
  295. options.extraData[this.element.name] = v;
  296. }
  297. }
  298. $(this.element).prop('disabled', true);
  299. if (!this.progress || !this.progress.type) {
  300. return;
  301. }
  302. var progressIndicatorMethod = 'setProgressIndicator' + this.progress.type.slice(0, 1).toUpperCase() + this.progress.type.slice(1).toLowerCase();
  303. if (progressIndicatorMethod in this && typeof this[progressIndicatorMethod] === 'function') {
  304. this[progressIndicatorMethod].call(this);
  305. }
  306. };
  307. Drupal.theme.ajaxProgressThrobber = function (message) {
  308. var messageMarkup = typeof message === 'string' ? Drupal.theme('ajaxProgressMessage', message) : '';
  309. var throbber = '<div class="throbber">&nbsp;</div>';
  310. return '<div class="ajax-progress ajax-progress-throbber">' + throbber + messageMarkup + '</div>';
  311. };
  312. Drupal.theme.ajaxProgressIndicatorFullscreen = function () {
  313. return '<div class="ajax-progress ajax-progress-fullscreen">&nbsp;</div>';
  314. };
  315. Drupal.theme.ajaxProgressMessage = function (message) {
  316. return '<div class="message">' + message + '</div>';
  317. };
  318. Drupal.theme.ajaxProgressBar = function ($element) {
  319. return $('<div class="ajax-progress ajax-progress-bar"></div>').append($element);
  320. };
  321. Drupal.Ajax.prototype.setProgressIndicatorBar = function () {
  322. var progressBar = new Drupal.ProgressBar('ajax-progress-' + this.element.id, $.noop, this.progress.method, $.noop);
  323. if (this.progress.message) {
  324. progressBar.setProgress(-1, this.progress.message);
  325. }
  326. if (this.progress.url) {
  327. progressBar.startMonitoring(this.progress.url, this.progress.interval || 1500);
  328. }
  329. this.progress.element = $(Drupal.theme('ajaxProgressBar', progressBar.element));
  330. this.progress.object = progressBar;
  331. $(this.element).after(this.progress.element);
  332. };
  333. Drupal.Ajax.prototype.setProgressIndicatorThrobber = function () {
  334. this.progress.element = $(Drupal.theme('ajaxProgressThrobber', this.progress.message));
  335. $(this.element).after(this.progress.element);
  336. };
  337. Drupal.Ajax.prototype.setProgressIndicatorFullscreen = function () {
  338. this.progress.element = $(Drupal.theme('ajaxProgressIndicatorFullscreen'));
  339. $('body').append(this.progress.element);
  340. };
  341. Drupal.Ajax.prototype.success = function (response, status) {
  342. var _this = this;
  343. if (this.progress.element) {
  344. $(this.progress.element).remove();
  345. }
  346. if (this.progress.object) {
  347. this.progress.object.stopMonitoring();
  348. }
  349. $(this.element).prop('disabled', false);
  350. var elementParents = $(this.element).parents('[data-drupal-selector]').addBack().toArray();
  351. var focusChanged = false;
  352. Object.keys(response || {}).forEach(function (i) {
  353. if (response[i].command && _this.commands[response[i].command]) {
  354. _this.commands[response[i].command](_this, response[i], status);
  355. if (response[i].command === 'invoke' && response[i].method === 'focus') {
  356. focusChanged = true;
  357. }
  358. }
  359. });
  360. if (!focusChanged && this.element && !$(this.element).data('disable-refocus')) {
  361. var target = false;
  362. for (var n = elementParents.length - 1; !target && n >= 0; n--) {
  363. target = document.querySelector('[data-drupal-selector="' + elementParents[n].getAttribute('data-drupal-selector') + '"]');
  364. }
  365. if (target) {
  366. $(target).trigger('focus');
  367. }
  368. }
  369. if (this.$form && document.body.contains(this.$form.get(0))) {
  370. var settings = this.settings || drupalSettings;
  371. Drupal.attachBehaviors(this.$form.get(0), settings);
  372. }
  373. this.settings = null;
  374. };
  375. Drupal.Ajax.prototype.getEffect = function (response) {
  376. var type = response.effect || this.effect;
  377. var speed = response.speed || this.speed;
  378. var effect = {};
  379. if (type === 'none') {
  380. effect.showEffect = 'show';
  381. effect.hideEffect = 'hide';
  382. effect.showSpeed = '';
  383. } else if (type === 'fade') {
  384. effect.showEffect = 'fadeIn';
  385. effect.hideEffect = 'fadeOut';
  386. effect.showSpeed = speed;
  387. } else {
  388. effect.showEffect = type + 'Toggle';
  389. effect.hideEffect = type + 'Toggle';
  390. effect.showSpeed = speed;
  391. }
  392. return effect;
  393. };
  394. Drupal.Ajax.prototype.error = function (xmlhttprequest, uri, customMessage) {
  395. if (this.progress.element) {
  396. $(this.progress.element).remove();
  397. }
  398. if (this.progress.object) {
  399. this.progress.object.stopMonitoring();
  400. }
  401. $(this.wrapper).show();
  402. $(this.element).prop('disabled', false);
  403. if (this.$form && document.body.contains(this.$form.get(0))) {
  404. var settings = this.settings || drupalSettings;
  405. Drupal.attachBehaviors(this.$form.get(0), settings);
  406. }
  407. throw new Drupal.AjaxError(xmlhttprequest, uri, customMessage);
  408. };
  409. Drupal.theme.ajaxWrapperNewContent = function ($newContent, ajax, response) {
  410. return (response.effect || ajax.effect) !== 'none' && $newContent.filter(function (i) {
  411. return !($newContent[i].nodeName === '#comment' || $newContent[i].nodeName === '#text' && /^(\s|\n|\r)*$/.test($newContent[i].textContent));
  412. }).length > 1 ? Drupal.theme('ajaxWrapperMultipleRootElements', $newContent) : $newContent;
  413. };
  414. Drupal.theme.ajaxWrapperMultipleRootElements = function ($elements) {
  415. return $('<div></div>').append($elements);
  416. };
  417. Drupal.AjaxCommands = function () {};
  418. Drupal.AjaxCommands.prototype = {
  419. insert: function insert(ajax, response) {
  420. var $wrapper = response.selector ? $(response.selector) : $(ajax.wrapper);
  421. var method = response.method || ajax.method;
  422. var effect = ajax.getEffect(response);
  423. var settings = response.settings || ajax.settings || drupalSettings;
  424. var $newContent = $($.parseHTML(response.data, document, true));
  425. $newContent = Drupal.theme('ajaxWrapperNewContent', $newContent, ajax, response);
  426. switch (method) {
  427. case 'html':
  428. case 'replaceWith':
  429. case 'replaceAll':
  430. case 'empty':
  431. case 'remove':
  432. Drupal.detachBehaviors($wrapper.get(0), settings);
  433. break;
  434. default:
  435. break;
  436. }
  437. $wrapper[method]($newContent);
  438. if (effect.showEffect !== 'show') {
  439. $newContent.hide();
  440. }
  441. var $ajaxNewContent = $newContent.find('.ajax-new-content');
  442. if ($ajaxNewContent.length) {
  443. $ajaxNewContent.hide();
  444. $newContent.show();
  445. $ajaxNewContent[effect.showEffect](effect.showSpeed);
  446. } else if (effect.showEffect !== 'show') {
  447. $newContent[effect.showEffect](effect.showSpeed);
  448. }
  449. if ($newContent.parents('html').length) {
  450. $newContent.each(function (index, element) {
  451. if (element.nodeType === Node.ELEMENT_NODE) {
  452. Drupal.attachBehaviors(element, settings);
  453. }
  454. });
  455. }
  456. },
  457. remove: function remove(ajax, response, status) {
  458. var settings = response.settings || ajax.settings || drupalSettings;
  459. $(response.selector).each(function () {
  460. Drupal.detachBehaviors(this, settings);
  461. }).remove();
  462. },
  463. changed: function changed(ajax, response, status) {
  464. var $element = $(response.selector);
  465. if (!$element.hasClass('ajax-changed')) {
  466. $element.addClass('ajax-changed');
  467. if (response.asterisk) {
  468. $element.find(response.asterisk).append(' <abbr class="ajax-changed" title="' + Drupal.t('Changed') + '">*</abbr> ');
  469. }
  470. }
  471. },
  472. alert: function alert(ajax, response, status) {
  473. window.alert(response.text, response.title);
  474. },
  475. announce: function announce(ajax, response) {
  476. if (response.priority) {
  477. Drupal.announce(response.text, response.priority);
  478. } else {
  479. Drupal.announce(response.text);
  480. }
  481. },
  482. redirect: function redirect(ajax, response, status) {
  483. window.location = response.url;
  484. },
  485. css: function css(ajax, response, status) {
  486. $(response.selector).css(response.argument);
  487. },
  488. settings: function settings(ajax, response, status) {
  489. var ajaxSettings = drupalSettings.ajax;
  490. if (ajaxSettings) {
  491. Drupal.ajax.expired().forEach(function (instance) {
  492. if (instance.selector) {
  493. var selector = instance.selector.replace('#', '');
  494. if (selector in ajaxSettings) {
  495. delete ajaxSettings[selector];
  496. }
  497. }
  498. });
  499. }
  500. if (response.merge) {
  501. $.extend(true, drupalSettings, response.settings);
  502. } else {
  503. ajax.settings = response.settings;
  504. }
  505. },
  506. data: function data(ajax, response, status) {
  507. $(response.selector).data(response.name, response.value);
  508. },
  509. invoke: function invoke(ajax, response, status) {
  510. var $element = $(response.selector);
  511. $element[response.method].apply($element, _toConsumableArray(response.args));
  512. },
  513. restripe: function restripe(ajax, response, status) {
  514. $(response.selector).find('> tbody > tr:visible, > tr:visible').removeClass('odd even').filter(':even').addClass('odd').end().filter(':odd').addClass('even');
  515. },
  516. update_build_id: function update_build_id(ajax, response, status) {
  517. $('input[name="form_build_id"][value="' + response.old + '"]').val(response.new);
  518. },
  519. add_css: function add_css(ajax, response, status) {
  520. $('head').prepend(response.data);
  521. },
  522. message: function message(ajax, response) {
  523. var messages = new Drupal.Message(document.querySelector(response.messageWrapperQuerySelector));
  524. if (response.clearPrevious) {
  525. messages.clear();
  526. }
  527. messages.add(response.message, response.messageOptions);
  528. }
  529. };
  530. })(jQuery, window, Drupal, drupalSettings);