ajax.js 19 KB

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