ajax.js 20 KB

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