1/** 2 * @file 3 * Provides Ajax page updating via jQuery $.ajax. 4 * 5 * Ajax is a method of making a request via JavaScript while viewing an HTML 6 * page. The request returns an array of commands encoded in JSON, which is 7 * then executed to make any changes that are necessary to the page. 8 * 9 * Drupal uses this file to enhance form elements with `#ajax['url']` and 10 * `#ajax['wrapper']` properties. If set, this file will automatically be 11 * included to provide Ajax capabilities. 12 */ 13 14(function ( 15 $, 16 window, 17 Drupal, 18 drupalSettings, 19 loadjs, 20 { isFocusable, tabbable }, 21) { 22 /** 23 * Attaches the Ajax behavior to each Ajax form element. 24 * 25 * @type {Drupal~behavior} 26 * 27 * @prop {Drupal~behaviorAttach} attach 28 * Initialize all {@link Drupal.Ajax} objects declared in 29 * `drupalSettings.ajax` or initialize {@link Drupal.Ajax} objects from 30 * DOM elements having the `use-ajax-submit` or `use-ajax` css class. 31 * @prop {Drupal~behaviorDetach} detach 32 * During `unload` remove all {@link Drupal.Ajax} objects related to 33 * the removed content. 34 */ 35 Drupal.behaviors.AJAX = { 36 attach(context, settings) { 37 function loadAjaxBehavior(base) { 38 const elementSettings = settings.ajax[base]; 39 if (typeof elementSettings.selector === 'undefined') { 40 elementSettings.selector = `#${base}`; 41 } 42 // Use jQuery selector instead of a native selector for 43 // backwards compatibility. 44 once('drupal-ajax', $(elementSettings.selector)).forEach((el) => { 45 elementSettings.element = el; 46 elementSettings.base = base; 47 Drupal.ajax(elementSettings); 48 }); 49 } 50 51 // Load all Ajax behaviors specified in the settings. 52 Object.keys(settings.ajax || {}).forEach(loadAjaxBehavior); 53 54 Drupal.ajax.bindAjaxLinks(document.body); 55 56 // This class means to submit the form to the action using Ajax. 57 once('ajax', '.use-ajax-submit').forEach((el) => { 58 const elementSettings = {}; 59 60 // Ajax submits specified in this manner automatically submit to the 61 // normal form action. 62 elementSettings.url = $(el.form).attr('action'); 63 // Form submit button clicks need to tell the form what was clicked so 64 // it gets passed in the POST request. 65 elementSettings.setClick = true; 66 // Form buttons use the 'click' event rather than mousedown. 67 elementSettings.event = 'click'; 68 // Clicked form buttons look better with the throbber than the progress 69 // bar. 70 elementSettings.progress = { type: 'throbber' }; 71 elementSettings.base = el.id; 72 elementSettings.element = el; 73 74 Drupal.ajax(elementSettings); 75 }); 76 }, 77 78 detach(context, settings, trigger) { 79 if (trigger === 'unload') { 80 Drupal.ajax.expired().forEach((instance) => { 81 // Set this to null and allow garbage collection to reclaim 82 // the memory. 83 Drupal.ajax.instances[instance.instanceIndex] = null; 84 }); 85 } 86 }, 87 }; 88 89 /** 90 * Extends Error to provide handling for Errors in Ajax. 91 * 92 * @constructor 93 * 94 * @augments Error 95 * 96 * @param {XMLHttpRequest} xmlhttp 97 * XMLHttpRequest object used for the failed request. 98 * @param {string} uri 99 * The URI where the error occurred. 100 * @param {string} customMessage 101 * The custom message. 102 */ 103 Drupal.AjaxError = function (xmlhttp, uri, customMessage) { 104 let statusCode; 105 let statusText; 106 let responseText; 107 if (xmlhttp.status) { 108 statusCode = `\n${Drupal.t('An AJAX HTTP error occurred.')}\n${Drupal.t( 109 'HTTP Result Code: !status', 110 { 111 '!status': xmlhttp.status, 112 }, 113 )}`; 114 } else { 115 statusCode = `\n${Drupal.t( 116 'An AJAX HTTP request terminated abnormally.', 117 )}`; 118 } 119 statusCode += `\n${Drupal.t('Debugging information follows.')}`; 120 const pathText = `\n${Drupal.t('Path: !uri', { '!uri': uri })}`; 121 statusText = ''; 122 // In some cases, when statusCode === 0, xmlhttp.statusText may not be 123 // defined. Unfortunately, testing for it with typeof, etc, doesn't seem to 124 // catch that and the test causes an exception. So we need to catch the 125 // exception here. 126 try { 127 statusText = `\n${Drupal.t('StatusText: !statusText', { 128 '!statusText': xmlhttp.statusText.trim(), 129 })}`; 130 } catch (e) { 131 // Empty. 132 } 133 134 responseText = ''; 135 // Again, we don't have a way to know for sure whether accessing 136 // xmlhttp.responseText is going to throw an exception. So we'll catch it. 137 try { 138 responseText = `\n${Drupal.t('ResponseText: !responseText', { 139 '!responseText': xmlhttp.responseText.trim(), 140 })}`; 141 } catch (e) { 142 // Empty. 143 } 144 145 // Make the responseText more readable by stripping HTML tags and newlines. 146 responseText = responseText.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, ''); 147 responseText = responseText.replace(/[\n]+\s+/g, '\n'); 148 149 // We don't need readyState except for status == 0. 150 const readyStateText = 151 xmlhttp.status === 0 152 ? `\n${Drupal.t('ReadyState: !readyState', { 153 '!readyState': xmlhttp.readyState, 154 })}` 155 : ''; 156 157 customMessage = customMessage 158 ? `\n${Drupal.t('CustomMessage: !customMessage', { 159 '!customMessage': customMessage, 160 })}` 161 : ''; 162 163 /** 164 * Formatted and translated error message. 165 * 166 * @type {string} 167 */ 168 this.message = 169 statusCode + 170 pathText + 171 statusText + 172 customMessage + 173 responseText + 174 readyStateText; 175 176 /** 177 * Used by some browsers to display a more accurate stack trace. 178 * 179 * @type {string} 180 */ 181 this.name = 'AjaxError'; 182 183 if (!Drupal.AjaxError.messages) { 184 Drupal.AjaxError.messages = new Drupal.Message(); 185 } 186 Drupal.AjaxError.messages.add( 187 Drupal.t( 188 "Oops, something went wrong. Check your browser's developer console for more details.", 189 ), 190 { 191 type: 'error', 192 }, 193 ); 194 }; 195 196 Drupal.AjaxError.prototype = new Error(); 197 Drupal.AjaxError.prototype.constructor = Drupal.AjaxError; 198 199 /** 200 * Provides Ajax page updating via jQuery $.ajax. 201 * 202 * This function is designed to improve developer experience by wrapping the 203 * initialization of {@link Drupal.Ajax} objects and storing all created 204 * objects in the {@link Drupal.ajax.instances} array. 205 * 206 * @example 207 * Drupal.behaviors.myCustomAJAXStuff = { 208 * attach: function (context, settings) { 209 * 210 * var ajaxSettings = { 211 * url: 'my/url/path', 212 * // If the old version of Drupal.ajax() needs to be used those 213 * // properties can be added 214 * base: 'myBase', 215 * element: $(context).find('.someElement') 216 * }; 217 * 218 * var myAjaxObject = Drupal.ajax(ajaxSettings); 219 * 220 * // Declare a new Ajax command specifically for this Ajax object. 221 * myAjaxObject.commands.insert = function (ajax, response, status) { 222 * $('#my-wrapper').append(response.data); 223 * alert('New content was appended to #my-wrapper'); 224 * }; 225 * 226 * // This command will remove this Ajax object from the page. 227 * myAjaxObject.commands.destroyObject = function (ajax, response, status) { 228 * Drupal.ajax.instances[this.instanceIndex] = null; 229 * }; 230 * 231 * // Programmatically trigger the Ajax request. 232 * myAjaxObject.execute(); 233 * } 234 * }; 235 * 236 * @param {object} settings 237 * The settings object passed to {@link Drupal.Ajax} constructor. 238 * @param {string} [settings.base] 239 * Base is passed to {@link Drupal.Ajax} constructor as the 'base' 240 * parameter. 241 * @param {HTMLElement} [settings.element] 242 * Element parameter of {@link Drupal.Ajax} constructor, element on which 243 * event listeners will be bound. 244 * 245 * @return {Drupal.Ajax} 246 * The created Ajax object. 247 * 248 * @see Drupal.AjaxCommands 249 */ 250 Drupal.ajax = function (settings) { 251 if (arguments.length !== 1) { 252 throw new Error( 253 'Drupal.ajax() function must be called with one configuration object only', 254 ); 255 } 256 // Map those config keys to variables for the old Drupal.ajax function. 257 const base = settings.base || false; 258 const element = settings.element || false; 259 delete settings.base; 260 delete settings.element; 261 262 // By default do not display progress for ajax calls without an element. 263 if (!settings.progress && !element) { 264 settings.progress = false;
265 } 266 267 const ajax = new Drupal.Ajax(base, element, settings); 268 ajax.instanceIndex = Drupal.ajax.instances.length; 269 Drupal.ajax.instances.push(ajax); 270 271 return ajax; 272 }; 273 274 /** 275 * Contains all created Ajax objects. 276 * 277 * @type {Array.<Drupal.Ajax|null>} 278 */ 279 Drupal.ajax.instances = []; 280 281 /** 282 * List all objects where the associated element is not in the DOM 283 * 284 * This method ignores {@link Drupal.Ajax} objects not bound to DOM elements 285 * when created with {@link Drupal.ajax}. 286 * 287 * @return {Array.<Drupal.Ajax>} 288 * The list of expired {@link Drupal.Ajax} objects. 289 */ 290 Drupal.ajax.expired = function () { 291 return Drupal.ajax.instances.filter( 292 (instance) => 293 instance && 294 instance.element !== false && 295 !document.body.contains(instance.element), 296 ); 297 }; 298 299 /** 300 * Bind Ajax functionality to links that use the 'use-ajax' class. 301 * 302 * @param {HTMLElement} element 303 * Element to enable Ajax functionality for. 304 */ 305 Drupal.ajax.bindAjaxLinks = (element) => { 306 // Bind Ajax behaviors to all items showing the class. 307 once('ajax', '.use-ajax', element).forEach((ajaxLink) => { 308 const $linkElement = $(ajaxLink); 309 310 const elementSettings = { 311 // Clicked links look better with the throbber than the progress bar. 312 progress: { type: 'throbber' }, 313 dialogType: $linkElement.data('dialog-type'), 314 dialog: $linkElement.data('dialog-options'), 315 dialogRenderer: $linkElement.data('dialog-renderer'), 316 base: $linkElement.attr('id'), 317 element: ajaxLink, 318 }; 319 const href = $linkElement.attr('href'); 320 /** 321 * For anchor tags, these will go to the target of the anchor rather than 322 * the usual location. 323 */ 324 if (href) { 325 elementSettings.url = href; 326 elementSettings.event = 'click'; 327 } 328 const httpMethod = $linkElement.data('ajax-http-method'); 329 /** 330 * In case of setting custom ajax http method for link we rewrite ajax.httpMethod. 331 */ 332 if (httpMethod) { 333 elementSettings.httpMethod = httpMethod; 334 } 335 Drupal.ajax(elementSettings); 336 }); 337 }; 338 339 /** 340 * Settings for an Ajax object. 341 * 342 * @typedef {object} Drupal.Ajax~elementSettings 343 * 344 * @prop {string} url 345 * Target of the Ajax request. 346 * @prop {?string} [event] 347 * Event bound to settings.element which will trigger the Ajax request. 348 * @prop {boolean} [keypress=true] 349 * Triggers a request on keypress events. 350 * @prop {?string} selector 351 * jQuery selector targeting the element to bind events to or used with 352 * {@link Drupal.AjaxCommands}. 353 * @prop {string} [effect='none'] 354 * Name of the jQuery method to use for displaying new Ajax content. 355 * @prop {string|number} [speed='none'] 356 * Speed with which to apply the effect. 357 * @prop {string} [method] 358 * Name of the jQuery method used to insert new content in the targeted 359 * element. 360 * @prop {object} [progress] 361 * Settings for the display of a user-friendly loader. 362 * @prop {string} [progress.type='throbber'] 363 * Type of progress element, core provides `'bar'`, `'throbber'` and 364 * `'fullscreen'`. 365 * @prop {string} [progress.message=Drupal.t('Processing...')] 366 * Custom message to be used with the bar indicator. 367 * @prop {object} [submit] 368 * Extra data to be sent with the Ajax request. 369 * @prop {boolean} [submit.js=true] 370 * Allows the PHP side to know this comes from an Ajax request. 371 * @prop {object} [dialog] 372 * Options for {@link Drupal.dialog}. 373 * @prop {string} [dialogType] 374 * One of `'modal'` or `'dialog'`. 375 * @prop {string} [prevent] 376 * List of events on which to stop default action and stop propagation. 377 */ 378 379 /** 380 * Ajax constructor. 381 * 382 * The Ajax request returns an array of commands encoded in JSON, which is 383 * then executed to make any changes that are necessary to the page. 384 * 385 * Drupal uses this file to enhance form elements with `#ajax['url']` and 386 * `#ajax['wrapper']` properties. If set, this file will automatically be 387 * included to provide Ajax capabilities. 388 * 389 * @constructor 390 * 391 * @param {string} [base] 392 * Base parameter of {@link Drupal.Ajax} constructor 393 * @param {HTMLElement} [element] 394 * Element parameter of {@link Drupal.Ajax} constructor, element on which 395 * event listeners will be bound. 396 * @param {Drupal.Ajax~elementSettings} elementSettings 397 * Settings for this Ajax object. 398 */ 399 Drupal.Ajax = function (base, element, elementSettings) { 400 const defaults = { 401 httpMethod: 'POST', 402 event: element ? 'mousedown' : null, 403 keypress: true, 404 selector: base ? `#${base}` : null, 405 effect: 'none', 406 speed: 'none', 407 method: 'replaceWith', 408 progress: { 409 type: 'throbber', 410 message: Drupal.t('Processing...'), 411 }, 412 submit: { 413 js: true, 414 }, 415 }; 416 417 $.extend(this, defaults, elementSettings); 418 419 /** 420 * @type {Drupal.AjaxCommands} 421 */ 422 this.commands = new Drupal.AjaxCommands(); 423 424 /** 425 * @type {boolean|number} 426 */ 427 this.instanceIndex = false;
428 429 // @todo Remove this after refactoring the PHP code to: 430 // - Call this 'selector'. 431 // - Include the '#' for ID-based selectors. 432 // - Support non-ID-based selectors. 433 if (this.wrapper) { 434 /** 435 * @type {string} 436 */ 437 this.wrapper = `#${this.wrapper}`; 438 } 439 440 /** 441 * @type {HTMLElement} 442 */ 443 this.element = element; 444 445 /** 446 * The last focused element right before processing ajax response. 447 * 448 * @type {string|null} 449 */ 450 this.preCommandsFocusedElementSelector = null; 451 452 /** 453 * @type {Drupal.Ajax~elementSettings} 454 */ 455 this.elementSettings = elementSettings; 456 457 // If there isn't a form, jQuery.ajax() will be used instead, allowing us to 458 // bind Ajax to links as well. 459 if (this.element && this.element.form) { 460 /** 461 * @type {jQuery} 462 */ 463 this.$form = $(this.element.form); 464 } 465 466 // If no Ajax callback URL was given, use the link href or form action. 467 if (!this.url) { 468 const $element = $(this.element); 469 if (this.element.tagName === 'A') { 470 this.url = $element.attr('href'); 471 } else if (this.element && element.form) { 472 this.url = this.$form.attr('action'); 473 } 474 } 475 476 // Replacing 'nojs' with 'ajax' in the URL allows for an easy method to let 477 // the server detect when it needs to degrade gracefully. 478 // There are four scenarios to check for: 479 // 1. /nojs/ 480 // 2. /nojs$ - The end of a URL string. 481 // 3. /nojs? - Followed by a query (e.g. path/nojs?destination=foobar). 482 // 4. /nojs# - Followed by a fragment (e.g.: path/nojs#my-fragment). 483 const originalUrl = this.url; 484 485 /** 486 * Processed Ajax URL. 487 * 488 * @type {string} 489 */ 490 this.url = this.url.replace(/\/nojs(\/|$|\?|#)/, '/ajax$1'); 491 // If the 'nojs' version of the URL is trusted, also trust the 'ajax' 492 // version. 493 if (drupalSettings.ajaxTrustedUrl[originalUrl]) { 494 drupalSettings.ajaxTrustedUrl[this.url] = true; 495 } 496 497 // Set the options for the ajaxSubmit function. 498 // The 'this' variable will not persist inside of the options object. 499 const ajax = this; 500 501 /** 502 * Options for the jQuery.ajax function. 503 * 504 * @name Drupal.Ajax#options 505 * 506 * @type {object} 507 * 508 * @prop {string} url 509 * Ajax URL to be called. 510 * @prop {object} data 511 * Ajax payload. 512 * @prop {function} beforeSerialize 513 * Implement jQuery beforeSerialize function to call 514 * {@link Drupal.Ajax#beforeSerialize}. 515 * @prop {function} beforeSubmit 516 * Implement jQuery beforeSubmit function to call 517 * {@link Drupal.Ajax#beforeSubmit}. 518 * @prop {function} beforeSend 519 * Implement jQuery beforeSend function to call 520 * {@link Drupal.Ajax#beforeSend}. 521 * @prop {function} success 522 * Implement jQuery success function to call 523 * {@link Drupal.Ajax#success}. 524 * @prop {function} complete 525 * Implement jQuery success function to clean up ajax state and trigger an 526 * error if needed. 527 * @prop {string} dataType='json' 528 * Type of the response expected. 529 * @prop {string} type='POST' 530 * HTTP method to use for the Ajax request. 531 */ 532 ajax.options = { 533 url: ajax.url, 534 data: ajax.submit, 535 isInProgress() { 536 return ajax.ajaxing; 537 }, 538 beforeSerialize(elementSettings, options) { 539 return ajax.beforeSerialize(elementSettings, options); 540 }, 541 beforeSubmit(formValues, elementSettings, options) { 542 ajax.ajaxing = true; 543 ajax.preCommandsFocusedElementSelector = null; 544 return ajax.beforeSubmit(formValues, elementSettings, options); 545 }, 546 beforeSend(xmlhttprequest, options) { 547 ajax.ajaxing = true; 548 return ajax.beforeSend(xmlhttprequest, options); 549 }, 550 success(response, status, xmlhttprequest) { 551 ajax.preCommandsFocusedElementSelector = 552 document.activeElement.getAttribute('data-drupal-selector'); 553 554 // Sanity check for browser support (object expected). 555 // When using iFrame uploads, responses must be returned as a string. 556 if (typeof response === 'string') { 557 response = $.parseJSON(response); 558 } 559 560 // Prior to invoking the response's commands, verify that they can be 561 // trusted by checking for a response header. See 562 // \Drupal\Core\EventSubscriber\AjaxResponseSubscriber for details. 563 // - Empty responses are harmless so can bypass verification. This 564 // avoids an alert message for server-generated no-op responses that 565 // skip Ajax rendering. 566 // - Ajax objects with trusted URLs (e.g., ones defined server-side via 567 // #ajax) can bypass header verification. This is especially useful 568 // for Ajax with multipart forms. Because IFRAME transport is used, 569 // the response headers cannot be accessed for verification. 570 if (response !== null && !drupalSettings.ajaxTrustedUrl[ajax.url]) { 571 if (xmlhttprequest.getResponseHeader('X-Drupal-Ajax-Token') !== '1') { 572 const customMessage = Drupal.t( 573 'The response failed verification so will not be processed.', 574 ); 575 return ajax.error(xmlhttprequest, ajax.url, customMessage); 576 } 577 } 578 579 return ( 580 // Ensure that the return of the success callback is a Promise. 581 // When the return is a Promise, using resolve will unwrap it, and 582 // when the return is not a Promise we make sure it can be used as 583 // one. This is useful for code that overrides the success method. 584 Promise.resolve(ajax.success(response, status))
585 // Ajaxing status is back to false when all the AJAX commands have 586 // finished executing. 587 .then(() => { 588 ajax.ajaxing = false; 589 // jQuery normally triggers the ajaxSuccess, ajaxComplete, and 590 // ajaxStop events after the "success" function passed to $.ajax() 591 // returns, but we prevented that via 592 // $.event.special[EVENT_NAME].trigger in order to wait for the 593 // commands to finish executing. Now that they have, re-trigger 594 // those events. 595 $(document).trigger('ajaxSuccess', [xmlhttprequest, this]); 596 $(document).trigger('ajaxComplete', [xmlhttprequest, this]); 597 if (--$.active === 0) { 598 $(document).trigger('ajaxStop'); 599 } 600 }) 601 ); 602 }, 603 error(xmlhttprequest, status, error) { 604 ajax.ajaxing = false; 605 }, 606 complete(xmlhttprequest, status) { 607 if (status === 'error' || status === 'parsererror') { 608 return ajax.error(xmlhttprequest, ajax.url); 609 } 610 }, 611 dataType: 'json', 612 jsonp: false, 613 method: ajax.httpMethod, 614 }; 615 616 if (elementSettings.dialog) { 617 ajax.options.data.dialogOptions = elementSettings.dialog; 618 } 619 620 // Ensure that we have a valid URL by adding ? when no query parameter is 621 // yet available, otherwise append using &. 622 if (!ajax.options.url.includes('?')) { 623 ajax.options.url += '?'; 624 } else { 625 ajax.options.url += '&'; 626 } 627 // If this element has a dialog type use if for the wrapper if not use 'ajax'. 628 let wrapper = `drupal_${elementSettings.dialogType || 'ajax'}`; 629 if (elementSettings.dialogRenderer) { 630 wrapper += `.${elementSettings.dialogRenderer}`; 631 } 632 ajax.options.url += `${Drupal.ajax.WRAPPER_FORMAT}=${wrapper}`; 633 634 // Bind the ajaxSubmit function to the element event. 635 $(ajax.element).on(elementSettings.event, function (event) { 636 if ( 637 !drupalSettings.ajaxTrustedUrl[ajax.url] && 638 !Drupal.url.isLocal(ajax.url) 639 ) { 640 throw new Error( 641 Drupal.t('The callback URL is not local and not trusted: !url', { 642 '!url': ajax.url, 643 }), 644 ); 645 } 646 return ajax.eventResponse(this, event); 647 }); 648 649 // If necessary, enable keyboard submission so that Ajax behaviors 650 // can be triggered through keyboard input as well as e.g. a mousedown 651 // action. 652 if (elementSettings.keypress) { 653 $(ajax.element).on('keypress', function (event) { 654 return ajax.keypressResponse(this, event); 655 }); 656 } 657 658 // If necessary, prevent the browser default action of an additional event. 659 // For example, prevent the browser default action of a click, even if the 660 // Ajax behavior binds to mousedown. 661 if (elementSettings.prevent) { 662 $(ajax.element).on(elementSettings.prevent, false); 663 } 664 }; 665 666 /** 667 * URL query attribute to indicate the wrapper used to render a request. 668 * 669 * The wrapper format determines how the HTML is wrapped, for example in a 670 * modal dialog. 671 * 672 * @const {string} 673 * 674 * @default 675 */ 676 Drupal.ajax.WRAPPER_FORMAT = '_wrapper_format'; 677 678 /** 679 * Request parameter to indicate that a request is a Drupal Ajax request. 680 * 681 * @const {string} 682 * 683 * @default 684 */ 685 Drupal.Ajax.AJAX_REQUEST_PARAMETER = '_drupal_ajax'; 686 687 /** 688 * Execute the ajax request. 689 * 690 * Allows developers to execute an Ajax request manually without specifying 691 * an event to respond to. 692 * 693 * @return {object} 694 * Returns the jQuery.Deferred object underlying the Ajax request. If 695 * pre-serialization fails, the Deferred will be returned in the rejected 696 * state. 697 */ 698 Drupal.Ajax.prototype.execute = function () { 699 // Do not perform another ajax command if one is already in progress. 700 if (this.ajaxing) { 701 return; 702 } 703 704 try { 705 this.beforeSerialize(this.element, this.options); 706 // Return the jqXHR so that external code can hook into the Deferred API. 707 return $.ajax(this.options); 708 } catch (e) { 709 // Unset the ajax.ajaxing flag here because it won't be unset during 710 // the complete response. 711 this.ajaxing = false;
712 window.alert( 713 `An error occurred while attempting to process ${this.options.url}: ${e.message}`, 714 ); 715 // For consistency, return a rejected Deferred (i.e., jqXHR's superclass) 716 // so that calling code can take appropriate action. 717 return $.Deferred().reject(); 718 } 719 }; 720 721 /** 722 * Handle a key press. 723 * 724 * The Ajax object will, if instructed, bind to a key press response. This 725 * will test to see if the key press is valid to trigger this event and 726 * if it is, trigger it for us and prevent other keypresses from triggering. 727 * In this case we're handling RETURN and SPACE BAR keypresses (event codes 13 728 * and 32. RETURN is often used to submit a form when in a textfield, and 729 * SPACE is often used to activate an element without submitting. 730 * 731 * @param {HTMLElement} element 732 * Element the event was triggered on. 733 * @param {jQuery.Event} event 734 * Triggered event. 735 */ 736 Drupal.Ajax.prototype.keypressResponse = function (element, event) { 737 // Create a synonym for this to reduce code confusion. 738 const ajax = this; 739 740 // Detect enter key and space bar and allow the standard response for them, 741 // except for form elements of type 'text', 'tel', 'number' and 'textarea', 742 // where the space bar activation causes inappropriate activation if 743 // #ajax['keypress'] is TRUE. On a text-type widget a space should always 744 // be a space. 745 if ( 746 event.which === 13 || 747 (event.which === 32 && 748 element.type !== 'text' && 749 element.type !== 'textarea' && 750 element.type !== 'tel' && 751 element.type !== 'number') 752 ) { 753 event.preventDefault(); 754 event.stopPropagation(); 755 $(element).trigger(ajax.elementSettings.event); 756 } 757 }; 758 759 /** 760 * Handle an event that triggers an Ajax response. 761 * 762 * When an event that triggers an Ajax response happens, this method will 763 * perform the actual Ajax call. It is bound to the event using 764 * bind() in the constructor, and it uses the options specified on the 765 * Ajax object. 766 * 767 * @param {HTMLElement} element 768 * Element the event was triggered on. 769 * @param {jQuery.Event} event 770 * Triggered event. 771 */ 772 Drupal.Ajax.prototype.eventResponse = function (element, event) { 773 event.preventDefault(); 774 event.stopPropagation(); 775 776 // Create a synonym for this to reduce code confusion. 777 const ajax = this; 778 779 // Do not perform another Ajax command if one is already in progress. 780 if (ajax.ajaxing) { 781 return; 782 } 783 784 try { 785 if (ajax.$form) { 786 // If setClick is set, we must set this to ensure that the button's 787 // value is passed. 788 if (ajax.setClick) { 789 // Mark the clicked button. 'form.clk' is a special variable for 790 // ajaxSubmit that tells the system which element got clicked to 791 // trigger the submit. Without it there would be no 'op' or 792 // equivalent. 793 element.form.clk = element; 794 } 795 796 ajax.$form.ajaxSubmit(ajax.options); 797 } else { 798 ajax.beforeSerialize(ajax.element, ajax.options); 799 $.ajax(ajax.options); 800 } 801 } catch (e) { 802 // Unset the ajax.ajaxing flag here because it won't be unset during 803 // the complete response. 804 ajax.ajaxing = false; 805 window.alert( 806 `An error occurred while attempting to process ${ajax.options.url}: ${e.message}`, 807 ); 808 } 809 }; 810 811 /** 812 * Handler for the form serialization. 813 * 814 * Runs before the beforeSend() handler (see below), and unlike that one, runs 815 * before field data is collected. 816 * 817 * @param {object} [element] 818 * Ajax object's `elementSettings`. 819 * @param {object} options 820 * jQuery.ajax options. 821 */ 822 Drupal.Ajax.prototype.beforeSerialize = function (element, options) { 823 // Allow detaching behaviors to update field values before collecting them. 824 // This is only needed when field values are added to the POST data, so only 825 // when there is a form such that this.$form.ajaxSubmit() is used instead of 826 // $.ajax(). When there is no form and $.ajax() is used, beforeSerialize() 827 // isn't called, but don't rely on that: explicitly check this.$form. 828 if (this.$form && document.body.contains(this.$form.get(0))) { 829 const settings = this.settings || drupalSettings; 830 Drupal.detachBehaviors(this.$form.get(0), settings, 'serialize'); 831 } 832 833 // Inform Drupal that this is an AJAX request. 834 options.data[Drupal.Ajax.AJAX_REQUEST_PARAMETER] = 1; 835 836 // Allow Drupal to return new JavaScript and CSS files to load without 837 // returning the ones already loaded. 838 // @see \Drupal\Core\StackMiddleWare\AjaxPageState 839 // @see \Drupal\Core\Theme\AjaxBasePageNegotiator 840 // @see \Drupal\Core\Asset\LibraryDependencyResolverInterface::getMinimalRepresentativeSubset() 841 // @see system_js_settings_alter() 842 const pageState = drupalSettings.ajaxPageState; 843 options.data['ajax_page_state[theme]'] = pageState.theme; 844 options.data['ajax_page_state[theme_token]'] = pageState.theme_token; 845 options.data['ajax_page_state[libraries]'] = pageState.libraries; 846 }; 847 848 /** 849 * Modify form values prior to form submission. 850 * 851 * @param {Array.<object>} formValues 852 * Processed form values. 853 * @param {jQuery} element 854 * The form node as a jQuery object. 855 * @param {object} options 856 * jQuery.ajax options. 857 */ 858 Drupal.Ajax.prototype.beforeSubmit = function (formValues, element, options) { 859 // This function is left empty to make it simple to override for modules 860 // that wish to add functionality here. 861 }; 862 863 /** 864 * Prepare the Ajax request before it is sent. 865 * 866 * @param {XMLHttpRequest} xmlhttprequest 867 * Native Ajax object. 868 * @param {object} options 869 * jQuery.ajax options. 870 */ 871 Drupal.Ajax.prototype.beforeSend = function (xmlhttprequest, options) { 872 // For forms without file inputs, the jQuery Form plugin serializes the 873 // form values, and then calls jQuery's $.ajax() function, which invokes 874 // this handler. In this circumstance, options.extraData is never used. For 875 // forms with file inputs, the jQuery Form plugin uses the browser's normal 876 // form submission mechanism, but captures the response in a hidden IFRAME. 877 // In this circumstance, it calls this handler first, and then appends 878 // hidden fields to the form to submit the values in options.extraData. 879 // There is no simple way to know which submission mechanism will be used, 880 // so we add to extraData regardless, and allow it to be ignored in the 881 // former case. 882 if (this.$form) { 883 options.extraData = options.extraData || {}; 884 885 // Let the server know when the IFRAME submission mechanism is used. The 886 // server can use this information to wrap the JSON response in a 887 // TEXTAREA, as per http://jquery.malsup.com/form/#file-upload. 888 options.extraData.ajax_iframe_upload = '1'; 889 890 // The triggering element is about to be disabled (see below), but if it 891 // contains a value (e.g., a checkbox, textfield, select, etc.), ensure 892 // that value is included in the submission. As per above, submissions 893 // that use $.ajax() are already serialized prior to the element being 894 // disabled, so this is only needed for IFRAME submissions. 895 const v = $.fieldValue(this.element); 896 if (v !== null) { 897 options.extraData[this.element.name] = v; 898 } 899 } 900 901 // Disable the element that received the change to prevent user interface 902 // interaction while the Ajax request is in progress. ajax.ajaxing prevents 903 // the element from triggering a new request, but does not prevent the user 904 // from changing its value. 905 $(this.element).prop('disabled', true); 906 907 if (!this.progress || !this.progress.type) { 908 return; 909 } 910 911 // Insert progress indicator. 912 const progressIndicatorMethod = `setProgressIndicator${this.progress.type 913 .slice(0, 1) 914 .toUpperCase()}${this.progress.type.slice(1).toLowerCase()}`; 915 if ( 916 progressIndicatorMethod in this && 917 typeof this[progressIndicatorMethod] === 'function' 918 ) { 919 this[progressIndicatorMethod].call(this); 920 } 921 }; 922 923 /** 924 * An animated progress throbber and container element for AJAX operations. 925 * 926 * @param {string} [message] 927 * (optional) The message shown on the UI. 928 * @return {string} 929 * The HTML markup for the throbber. 930 */ 931 Drupal.theme.ajaxProgressThrobber = (message) => { 932 // Build markup without adding extra white space since it affects rendering. 933 const messageMarkup = 934 typeof message === 'string' 935 ? Drupal.theme('ajaxProgressMessage', message) 936 : ''; 937 const throbber = '<div class="throbber"> </div>'; 938 939 return `<div class="ajax-progress ajax-progress-throbber">${throbber}${messageMarkup}</div>`; 940 }; 941 942 /** 943 * An animated progress throbber and container element for AJAX operations. 944 * 945 * @return {string} 946 * The HTML markup for the throbber. 947 */ 948 Drupal.theme.ajaxProgressIndicatorFullscreen = () => 949 '<div class="ajax-progress ajax-progress-fullscreen"> </div>'; 950 951 /** 952 * Formats text accompanying the AJAX progress throbber. 953 * 954 * @param {string} message 955 * The message shown on the UI. 956 * @return {string} 957 * The HTML markup for the throbber. 958 */ 959 Drupal.theme.ajaxProgressMessage = (message) => 960 `<div class="message">${message}</div>`; 961 962 /** 963 * Provide a wrapper for the AJAX progress bar element. 964 * 965 * @param {jQuery} $element 966 * Progress bar element. 967 * @return {string} 968 * The HTML markup for the progress bar. 969 */ 970 Drupal.theme.ajaxProgressBar = ($element) => 971 $('<div class="ajax-progress ajax-progress-bar"></div>').append($element); 972 973 /** 974 * Sets the progress bar progress indicator. 975 */ 976 Drupal.Ajax.prototype.setProgressIndicatorBar = function () { 977 const progressBar = new Drupal.ProgressBar(
978 `ajax-progress-${this.element.id}`, 979 $.noop, 980 this.progress.method, 981 $.noop, 982 ); 983 if (this.progress.message) { 984 progressBar.setProgress(-1, this.progress.message); 985 } 986 if (this.progress.url) { 987 progressBar.startMonitoring( 988 this.progress.url, 989 this.progress.interval || 1500, 990 ); 991 } 992 this.progress.element = $( 993 Drupal.theme('ajaxProgressBar', progressBar.element), 994 ); 995 this.progress.object = progressBar; 996 $(this.element).after(this.progress.element); 997 }; 998 999 /** 1000 * Sets the throbber progress indicator. 1001 */ 1002 Drupal.Ajax.prototype.setProgressIndicatorThrobber = function () { 1003 this.progress.element = $( 1004 Drupal.theme('ajaxProgressThrobber', this.progress.message), 1005 ); 1006 if ($(this.element).closest('[data-drupal-ajax-container]').length) { 1007 $(this.element) 1008 .closest('[data-drupal-ajax-container]') 1009 .after(this.progress.element); 1010 } else { 1011 $(this.element).after(this.progress.element); 1012 } 1013 }; 1014 1015 /** 1016 * Sets the fullscreen progress indicator. 1017 */ 1018 Drupal.Ajax.prototype.setProgressIndicatorFullscreen = function () { 1019 this.progress.element = $(Drupal.theme('ajaxProgressIndicatorFullscreen')); 1020 $('body').append(this.progress.element); 1021 }; 1022 1023 /** 1024 * Helper method to make sure commands are executed in sequence. 1025 * 1026 * @param {Array.<Drupal.AjaxCommands~commandDefinition>} response 1027 * Drupal Ajax response. 1028 * @param {number} status 1029 * XMLHttpRequest status. 1030 * 1031 * @return {Promise} 1032 * The promise that will resolve once all commands have finished executing. 1033 */ 1034 Drupal.Ajax.prototype.commandExecutionQueue = function (response, status) { 1035 const ajaxCommands = this.commands; 1036 return Object.keys(response || {}).reduce( 1037 // Add all commands to a single execution queue. 1038 (executionQueue, key) => 1039 executionQueue.then(() => { 1040 const { command } = response[key]; 1041 if (command && ajaxCommands[command]) { 1042 // When a command returns a promise, the remaining commands will not 1043 // execute until that promise has been fulfilled. This is typically 1044 // used to ensure JavaScript files added via the 'add_js' command 1045 // have loaded before subsequent commands execute. 1046 return ajaxCommands[command](this, response[key], status); 1047 } 1048 }), 1049 Promise.resolve(), 1050 ); 1051 }; 1052 1053 /** 1054 * Handler for the form redirection completion. 1055 * 1056 * @param {Array.<Drupal.AjaxCommands~commandDefinition>} response 1057 * Drupal Ajax response. 1058 * @param {number} status 1059 * XMLHttpRequest status. 1060 * 1061 * @return {Promise} 1062 * The promise that will resolve once all commands have finished executing. 1063 */ 1064 Drupal.Ajax.prototype.success = function (response, status) { 1065 // Remove the progress element. 1066 if (this.progress.element) { 1067 $(this.progress.element).remove(); 1068 } 1069 if (this.progress.object) { 1070 this.progress.object.stopMonitoring(); 1071 } 1072 $(this.element).prop('disabled', false); 1073 1074 // Save element's ancestors tree so if the element is removed from the dom 1075 // we can try to refocus one of its parents. Using addBack reverse the 1076 // result array, meaning that index 0 is the highest parent in the hierarchy 1077 // in this situation it is usually a <form> element. 1078 const elementParents = $(this.element) 1079 .parents('[data-drupal-selector]') 1080 .addBack() 1081 .toArray(); 1082 1083 // Track if any command is altering the focus so we can avoid changing the 1084 // focus set by the Ajax command. 1085 const focusChanged = Object.keys(response || {}).some((key) => { 1086 const { command, method } = response[key]; 1087 return ( 1088 command === 'focusFirst' || 1089 command === 'openDialog' || 1090 (command === 'invoke' && method === 'focus') 1091 ); 1092 }); 1093 1094 return ( 1095 this.commandExecutionQueue(response, status) 1096 // If the focus hasn't been changed by the AJAX commands, try to refocus 1097 // the triggering element or one of its parents if that element does not 1098 // exist anymore. 1099 .then(() => { 1100 if (!focusChanged) { 1101 let target = false;
1102 if (this.element) { 1103 if ( 1104 $(this.element).data('refocus-blur') && 1105 this.preCommandsFocusedElementSelector 1106 ) { 1107 target = document.querySelector( 1108 `[data-drupal-selector="${this.preCommandsFocusedElementSelector}"]`, 1109 ); 1110 } 1111 if (!target && !$(this.element).data('disable-refocus')) { 1112 for ( 1113 let n = elementParents.length - 1; 1114 !target && n >= 0; 1115 n-- 1116 ) { 1117 target = document.querySelector( 1118 `[data-drupal-selector="${elementParents[n].getAttribute( 1119 'data-drupal-selector', 1120 )}"]`, 1121 ); 1122 } 1123 } 1124 } 1125 if (target) { 1126 $(target).trigger('focus'); 1127 } 1128 } 1129 // Reattach behaviors, if they were detached in beforeSerialize(). The 1130 // attachBehaviors() called on the new content from processing the 1131 // response commands is not sufficient, because behaviors from the 1132 // entire form need to be reattached. 1133 if (this.$form && document.body.contains(this.$form.get(0))) { 1134 const settings = this.settings || drupalSettings; 1135 Drupal.attachBehaviors(this.$form.get(0), settings); 1136 } 1137 // Remove any response-specific settings so they don't get used on the 1138 // next call by mistake. 1139 this.settings = null; 1140 }) 1141 .catch((error) => 1142 // eslint-disable-next-line no-console 1143 console.error( 1144 Drupal.t( 1145 'An error occurred during the execution of the Ajax response: !error', 1146 { 1147 '!error': error, 1148 }, 1149 ), 1150 ), 1151 ) 1152 ); 1153 }; 1154 1155 /** 1156 * Build an effect object to apply an effect when adding new HTML. 1157 * 1158 * @param {object} response 1159 * Drupal Ajax response. 1160 * @param {string} [response.effect] 1161 * Override the default value of {@link Drupal.Ajax#elementSettings}. 1162 * @param {string|number} [response.speed] 1163 * Override the default value of {@link Drupal.Ajax#elementSettings}. 1164 * 1165 * @return {object} 1166 * Returns an object with `showEffect`, `hideEffect` and `showSpeed` 1167 * properties. 1168 */ 1169 Drupal.Ajax.prototype.getEffect = function (response) { 1170 const type = response.effect || this.effect; 1171 const speed = response.speed || this.speed; 1172 1173 const effect = {}; 1174 if (type === 'none') { 1175 effect.showEffect = 'show'; 1176 effect.hideEffect = 'hide'; 1177 effect.showSpeed = ''; 1178 } else if (type === 'fade') { 1179 effect.showEffect = 'fadeIn'; 1180 effect.hideEffect = 'fadeOut'; 1181 effect.showSpeed = speed; 1182 } else { 1183 effect.showEffect = `${type}Toggle`; 1184 effect.hideEffect = `${type}Toggle`; 1185 effect.showSpeed = speed; 1186 } 1187 1188 return effect; 1189 }; 1190 1191 /** 1192 * Handler for the form redirection error. 1193 * 1194 * @param {object} xmlhttprequest 1195 * Native XMLHttpRequest object. 1196 * @param {string} uri 1197 * Ajax Request URI. 1198 * @param {string} [customMessage] 1199 * Extra message to print with the Ajax error. 1200 */ 1201 Drupal.Ajax.prototype.error = function (xmlhttprequest, uri, customMessage) { 1202 // Remove the progress element. 1203 if (this.progress.element) { 1204 $(this.progress.element).remove(); 1205 } 1206 if (this.progress.object) { 1207 this.progress.object.stopMonitoring(); 1208 } 1209 // Undo hide. 1210 $(this.wrapper).show(); 1211 // Re-enable the element. 1212 $(this.element).prop('disabled', false); 1213 // Reattach behaviors, if they were detached in beforeSerialize(), and the 1214 // form is still part of the document. 1215 if (this.$form && document.body.contains(this.$form.get(0))) { 1216 const settings = this.settings || drupalSettings; 1217 Drupal.attachBehaviors(this.$form.get(0), settings); 1218 } 1219 throw new Drupal.AjaxError(xmlhttprequest, uri, customMessage); 1220 }; 1221 1222 /** 1223 * Provide a wrapper for new content via Ajax. 1224 * 1225 * Wrap the inserted markup when inserting multiple root elements with an 1226 * ajax effect. 1227 * 1228 * @param {jQuery} $newContent 1229 * Response elements after parsing. 1230 * @param {Drupal.Ajax} ajax 1231 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1232 * @param {object} response 1233 * The response from the Ajax request. 1234 *
1235 * @deprecated in drupal:8.6.0 and is removed from drupal:12.0.0. 1236 * Use data with desired wrapper. 1237 * 1238 * @see https://www.drupal.org/node/2940704 1239 * 1240 * @todo Add deprecation warning after it is possible. For more information 1241 * see: https://www.drupal.org/project/drupal/issues/2973400 1242 */ 1243 Drupal.theme.ajaxWrapperNewContent = ($newContent, ajax, response) => 1244 (response.effect || ajax.effect) !== 'none' && 1245 $newContent.filter( 1246 (i) => 1247 !( 1248 // We can not consider HTML comments or whitespace text as separate 1249 // roots, since they do not cause visual regression with effect. 1250 ( 1251 $newContent[i].nodeName === '#comment' || 1252 ($newContent[i].nodeName === '#text' && 1253 /^(\s|\n|\r)*$/.test($newContent[i].textContent)) 1254 ) 1255 ), 1256 ).length > 1 1257 ? Drupal.theme('ajaxWrapperMultipleRootElements', $newContent) 1258 : $newContent; 1259 1260 /** 1261 * Provide a wrapper for multiple root elements via Ajax. 1262 * 1263 * @param {jQuery} $elements 1264 * Response elements after parsing. 1265 * 1266 * @deprecated in drupal:8.6.0 and is removed from drupal:12.0.0. 1267 * Use data with desired wrapper. 1268 * 1269 * @see https://www.drupal.org/node/2940704 1270 * 1271 * @todo Add deprecation warning after it is possible. For more information 1272 * see: https://www.drupal.org/project/drupal/issues/2973400 1273 */ 1274 Drupal.theme.ajaxWrapperMultipleRootElements = ($elements) => 1275 $('<div></div>').append($elements); 1276 1277 /** 1278 * @typedef {object} Drupal.AjaxCommands~commandDefinition 1279 * 1280 * @prop {string} command 1281 * @prop {string} [method] 1282 * @prop {string} [selector] 1283 * @prop {string} [data] 1284 * @prop {object} [settings] 1285 * @prop {boolean} [asterisk] 1286 * @prop {string} [text] 1287 * @prop {string} [title] 1288 * @prop {string} [url] 1289 * @prop {object} [argument] 1290 * @prop {string} [name] 1291 * @prop {string} [value] 1292 * @prop {string} [old] 1293 * @prop {string} [new] 1294 * @prop {boolean} [merge] 1295 * @prop {Array} [args] 1296 * 1297 * @see Drupal.AjaxCommands 1298 */ 1299 1300 /** 1301 * Provide a series of commands that the client will perform. 1302 * 1303 * @constructor 1304 */ 1305 Drupal.AjaxCommands = function () {}; 1306 Drupal.AjaxCommands.prototype = { 1307 /** 1308 * Command to insert new content into the DOM. 1309 * 1310 * @param {Drupal.Ajax} ajax 1311 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1312 * @param {object} response 1313 * The response from the Ajax request. 1314 * @param {string} response.data 1315 * The data to use with the jQuery method. 1316 * @param {string} [response.method] 1317 * The jQuery DOM manipulation method to be used. 1318 * @param {string} [response.selector] 1319 * An optional jQuery selector string. 1320 * @param {object} [response.settings] 1321 * An optional array of settings that will be used. 1322 */ 1323 insert(ajax, response) { 1324 // Get information from the response. If it is not there, default to 1325 // our presets. 1326 const $wrapper = response.selector 1327 ? $(response.selector) 1328 : $(ajax.wrapper); 1329 const method = response.method || ajax.method; 1330 const effect = ajax.getEffect(response); 1331 1332 // Apply any settings from the returned JSON if available. 1333 const settings = response.settings || ajax.settings || drupalSettings; 1334 1335 // Parse response.data into an element collection. 1336 const parseHTML = (htmlString) => { 1337 const fragment = document.createDocumentFragment(); 1338 // Create a temporary template element. 1339 const template = fragment.appendChild( 1340 document.createElement('template'), 1341 ); 1342 1343 // Set the innerHTML of the template to the provided HTML string. 1344 template.innerHTML = htmlString; 1345 1346 // Return the contents of the temporary template. 1347 return template.content.childNodes; 1348 }; 1349 1350 let $newContent = $(parseHTML(response.data)); 1351 1352 // For backward compatibility, in some cases a wrapper will be added. This 1353 // behavior will be removed before Drupal 9.0.0. If different behavior is 1354 // needed, the theme functions can be overridden. 1355 // @see https://www.drupal.org/node/2940704 1356 $newContent = Drupal.theme( 1357 'ajaxWrapperNewContent', 1358 $newContent, 1359 ajax, 1360 response, 1361 ); 1362 1363 // If removing content from the wrapper, detach behaviors first. 1364 switch (method) { 1365 case 'html': 1366 case 'replaceWith': 1367 case 'replaceAll': 1368 case 'empty': 1369 case 'remove': 1370 Drupal.detachBehaviors($wrapper.get(0), settings); 1371 break; 1372 default: 1373 break; 1374 } 1375 1376 // Add the new content to the page. 1377 $wrapper[method]($newContent); 1378 1379 // Immediately hide the new content if we're using any effects. 1380 if (effect.showEffect !== 'show') { 1381 $newContent.hide(); 1382 } 1383 1384 // Determine which effect to use and what content will receive the 1385 // effect, then show the new content. 1386 const $ajaxNewContent = $newContent.find('.ajax-new-content'); 1387 if ($ajaxNewContent.length) { 1388 $ajaxNewContent.hide(); 1389 $newContent.show(); 1390 $ajaxNewContent[effect.showEffect](effect.showSpeed); 1391 } else if (effect.showEffect !== 'show') { 1392 $newContent[effect.showEffect](effect.showSpeed); 1393 } 1394 1395 // Attach behaviors to all element nodes. 1396 $newContent.each((index, element) => { 1397 if ( 1398 element.nodeType === Node.ELEMENT_NODE && 1399 // Attach all JavaScript behaviors to the new content, if it was 1400 // successfully added to the page, this condition allows 1401 // `#ajax['wrapper']` to be optional. 1402 document.documentElement.contains(element) 1403 ) { 1404 Drupal.attachBehaviors(element, settings); 1405 } 1406 }); 1407 }, 1408 1409 /** 1410 * Command to remove a chunk from the page. 1411 * 1412 * @param {Drupal.Ajax} [ajax] 1413 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1414 * @param {object} response 1415 * The response from the Ajax request. 1416 * @param {string} response.selector 1417 * A jQuery selector string. 1418 * @param {object} [response.settings] 1419 * An optional array of settings that will be used. 1420 * @param {number} [status] 1421 * The XMLHttpRequest status. 1422 */ 1423 remove(ajax, response, status) { 1424 const settings = response.settings || ajax.settings || drupalSettings; 1425 $(response.selector) 1426 .each(function () { 1427 Drupal.detachBehaviors(this, settings); 1428 }) 1429 .remove(); 1430 }, 1431 1432 /** 1433 * Command to mark a chunk changed. 1434 * 1435 * @param {Drupal.Ajax} [ajax] 1436 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1437 * @param {object} response 1438 * The JSON response object from the Ajax request. 1439 * @param {string} response.selector 1440 * A jQuery selector string. 1441 * @param {boolean} [response.asterisk] 1442 * An optional CSS selector. If specified, an asterisk will be 1443 * appended to the HTML inside the provided selector. 1444 * @param {number} [status] 1445 * The request status. 1446 */ 1447 changed(ajax, response, status) { 1448 const $element = $(response.selector); 1449 if (!$element.hasClass('ajax-changed')) { 1450 $element.addClass('ajax-changed'); 1451 if (response.asterisk) { 1452 $element 1453 .find(response.asterisk) 1454 .append( 1455 ` <abbr class="ajax-changed" title="${Drupal.t( 1456 'Changed', 1457 )}">*</abbr> `, 1458 ); 1459 } 1460 } 1461 }, 1462 1463 /** 1464 * Command to provide an alert. 1465 * 1466 * @param {Drupal.Ajax} [ajax] 1467 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1468 * @param {object} response 1469 * The JSON response from the Ajax request. 1470 * @param {string} response.text 1471 * The text that will be displayed in an alert dialog. 1472 * @param {number} [status] 1473 * The XMLHttpRequest status. 1474 */ 1475 alert(ajax, response, status) { 1476 window.alert(response.text); 1477 }, 1478 1479 /** 1480 * Command to provide triggers audio UAs to read the supplied text. 1481 * 1482 * @param {Drupal.Ajax} [ajax] 1483 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1484 * @param {object} response 1485 * The JSON response from the Ajax request. 1486 * @param {string} [response.text] 1487 * The text that will be read. 1488 * @param {string} [response.priority] 1489 * An optional priority that will be used for the announcement. 1490 */ 1491 announce(ajax, response) { 1492 if (response.priority) { 1493 Drupal.announce(response.text, response.priority); 1494 } else { 1495 Drupal.announce(response.text); 1496 } 1497 }, 1498 1499 /** 1500 * Command to set the window.location, redirecting the browser. 1501 * 1502 * @param {Drupal.Ajax} [ajax] 1503 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1504 * @param {object} response 1505 * The response from the Ajax request. 1506 * @param {string} response.url 1507 * The URL to redirect to. 1508 * @param {number} [status] 1509 * The XMLHttpRequest status. 1510 */ 1511 redirect(ajax, response, status) { 1512 window.location = response.url; 1513 }, 1514 1515 /** 1516 * Command to provide the jQuery css() function. 1517 * 1518 * @param {Drupal.Ajax} [ajax] 1519 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1520 * @param {object} response 1521 * The response from the Ajax request. 1522 * @param {string} response.selector 1523 * A jQuery selector string. 1524 * @param {object} response.argument 1525 * An array of key/value pairs to set in the CSS for the selector. 1526 * @param {number} [status] 1527 * The XMLHttpRequest status. 1528 */ 1529 css(ajax, response, status) { 1530 // eslint-disable-next-line no-jquery/no-css 1531 $(response.selector).css(response.argument); 1532 }, 1533 1534 /** 1535 * Command to set the settings used for other commands in this response. 1536 * 1537 * This method will also remove expired `drupalSettings.ajax` settings. 1538 * 1539 * @param {Drupal.Ajax} [ajax] 1540 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1541 * @param {object} response 1542 * The response from the Ajax request. 1543 * @param {boolean} response.merge 1544 * Determines whether the additional settings should be merged to the 1545 * global settings. 1546 * @param {object} response.settings 1547 * Contains additional settings to add to the global settings. 1548 * @param {number} [status] 1549 * The XMLHttpRequest status. 1550 */ 1551 settings(ajax, response, status) { 1552 const ajaxSettings = drupalSettings.ajax; 1553 1554 // Clean up drupalSettings.ajax. 1555 if (ajaxSettings) {
1556 Drupal.ajax.expired().forEach((instance) => { 1557 // If the Ajax object has been created through drupalSettings.ajax 1558 // it will have a selector. When there is no selector the object 1559 // has been initialized with a special class name picked up by the 1560 // Ajax behavior. 1561 1562 if (instance.selector) { 1563 const selector = instance.selector.replace('#', ''); 1564 if (selector in ajaxSettings) { 1565 delete ajaxSettings[selector]; 1566 } 1567 } 1568 }); 1569 } 1570 1571 if (response.merge) { 1572 $.extend(true, drupalSettings, response.settings); 1573 } else { 1574 ajax.settings = response.settings; 1575 } 1576 }, 1577 1578 /** 1579 * Command to attach data using jQuery's data API. 1580 * 1581 * @param {Drupal.Ajax} [ajax] 1582 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1583 * @param {object} response 1584 * The response from the Ajax request. 1585 * @param {string} response.name 1586 * The name or key (in the key value pair) of the data attached to this 1587 * selector. 1588 * @param {string} response.selector 1589 * A jQuery selector string. 1590 * @param {string|object} response.value 1591 * The value of to be attached. 1592 * @param {number} [status] 1593 * The XMLHttpRequest status. 1594 */ 1595 data(ajax, response, status) { 1596 $(response.selector).data(response.name, response.value); 1597 }, 1598 1599 /** 1600 * Command to focus the first tabbable element within a container. 1601 * 1602 * If no tabbable elements are found and the container is focusable, then 1603 * focus will move to that container. 1604 * 1605 * @param {Drupal.Ajax} [ajax] 1606 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1607 * @param {object} response 1608 * The response from the Ajax request. 1609 * @param {string} response.selector 1610 * A query selector string of the container to focus within. 1611 * @param {number} [status] 1612 * The XMLHttpRequest status. 1613 */ 1614 focusFirst(ajax, response, status) { 1615 let focusChanged = false; 1616 const container = document.querySelector(response.selector); 1617 if (container) { 1618 // Find all tabbable elements within the container. 1619 const tabbableElements = tabbable(container); 1620 1621 // Move focus to the first tabbable item found. 1622 if (tabbableElements.length) { 1623 tabbableElements[0].focus(); 1624 focusChanged = true; 1625 } else if (isFocusable(container)) { 1626 // If no tabbable elements are found, but the container is focusable, 1627 // move focus to the container. 1628 container.focus(); 1629 focusChanged = true; 1630 } 1631 } 1632 1633 // If no items were available to receive focus, return focus to the 1634 // triggering element. 1635 if (ajax.hasOwnProperty('element') && !focusChanged) { 1636 ajax.element.focus(); 1637 } 1638 }, 1639 1640 /** 1641 * Command to apply a jQuery method. 1642 * 1643 * @param {Drupal.Ajax} [ajax] 1644 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1645 * @param {object} response 1646 * The response from the Ajax request. 1647 * @param {Array} response.args 1648 * An array of arguments to the jQuery method, if any. 1649 * @param {string} response.method 1650 * The jQuery method to invoke. 1651 * @param {string} response.selector 1652 * A jQuery selector string. 1653 * @param {number} [status] 1654 * The XMLHttpRequest status. 1655 */ 1656 invoke(ajax, response, status) { 1657 const $element = $(response.selector); 1658 $element[response.method](...response.args); 1659 }, 1660 1661 /** 1662 * Command to restripe a table. 1663 * 1664 * @param {Drupal.Ajax} [ajax] 1665 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1666 * @param {object} response 1667 * The response from the Ajax request. 1668 * @param {string} response.selector 1669 * A jQuery selector string. 1670 * @param {number} [status] 1671 * The XMLHttpRequest status. 1672 */ 1673 restripe(ajax, response, status) { 1674 // :even and :odd are reversed because jQuery counts from 0 and 1675 // we count from 1, so we're out of sync. 1676 // Match immediate children of the parent element to allow nesting. 1677 $(response.selector) 1678 .find('> tbody > tr:visible, > tr:visible') 1679 .removeClass('odd even') 1680 .filter(':even') 1681 .addClass('odd') 1682 .end() 1683 .filter(':odd') 1684 .addClass('even'); 1685 }, 1686 1687 /** 1688 * Command to update a form's build ID. 1689 * 1690 * @param {Drupal.Ajax} [ajax] 1691 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1692 * @param {object} response 1693 * The response from the Ajax request. 1694 * @param {string} response.old 1695 * The old form build ID. 1696 * @param {string} response.new 1697 * The new form build ID. 1698 * @param {number} [status] 1699 * The XMLHttpRequest status. 1700 */ 1701 update_build_id(ajax, response, status) { 1702 document 1703 .querySelectorAll( 1704 `input[name="form_build_id"][value="${response.old}"]`, 1705 ) 1706 .forEach((item) => { 1707 item.value = response.new; 1708 }); 1709 }, 1710 1711 /** 1712 * Command to add css. 1713 * 1714 * @param {Drupal.Ajax} [ajax] 1715 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1716 * @param {object} response 1717 * The response from the Ajax request. 1718 * @param {object[]|string} response.data 1719 * An array of styles to be added. 1720 * @param {number} [status] 1721 * The XMLHttpRequest status. 1722 */ 1723 add_css(ajax, response, status) { 1724 if (typeof response.data === 'string') { 1725 Drupal.deprecationError({ 1726 message: 1727 'Passing a string to the Drupal.ajax.add_css() method is deprecated
1727in 10.1.0 and is removed from drupal:11.0.0. See https://www.drupal.org/node/3154948.', 1728 }); 1729 $('head').prepend(response.data); 1730 return; 1731 } 1732 1733 const allUniqueBundleIds = response.data.map(function (style) { 1734 const uniqueBundleId = style.href; 1735 // Force file to load as a CSS stylesheet using 'css!' flag. 1736 if (!loadjs.isDefined(uniqueBundleId)) { 1737 loadjs(`css!${style.href}`, uniqueBundleId, { 1738 before(path, styleEl) { 1739 // This allows all attributes to be added, like media. 1740 Object.keys(style).forEach((attributeKey) => { 1741 styleEl.setAttribute(attributeKey, style[attributeKey]); 1742 }); 1743 }, 1744 }); 1745 } 1746 return uniqueBundleId; 1747 }); 1748 // Returns the promise so that the next AJAX command waits on the 1749 // completion of this one to execute, ensuring the CSS is loaded before 1750 // executing. 1751 return new Promise((resolve, reject) => { 1752 loadjs.ready(allUniqueBundleIds, { 1753 success() { 1754 // All CSS files were loaded. Resolve the promise and let the 1755 // remaining commands execute. 1756 resolve(); 1757 }, 1758 error(depsNotFound) { 1759 const message = Drupal.t( 1760 `The following files could not be loaded: @dependencies`, 1761 { '@dependencies': depsNotFound.join(', ') }, 1762 ); 1763 reject(message); 1764 }, 1765 }); 1766 }); 1767 }, 1768 1769 /** 1770 * Command to add a message to the message area. 1771 * 1772 * @param {Drupal.Ajax} [ajax] 1773 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1774 * @param {object} response 1775 * The response from the Ajax request. 1776 * @param {string} response.messageWrapperQuerySelector 1777 * The zone where to add the message. If null, the default will be used. 1778 * @param {string} response.message 1779 * The message text. 1780 * @param {string} response.messageOptions 1781 * The options argument for Drupal.Message().add(). 1782 * @param {boolean} response.clearPrevious 1783 * If true, clear previous messages. 1784 */ 1785 message(ajax, response) { 1786 const messages = new Drupal.Message( 1787 document.querySelector(response.messageWrapperQuerySelector), 1788 ); 1789 if (response.clearPrevious) { 1790 messages.clear(); 1791 } 1792 messages.add(response.message, response.messageOptions); 1793 }, 1794 1795 /** 1796 * Command to add JS. 1797 * 1798 * @param {Drupal.Ajax} [ajax] 1799 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. 1800 * @param {object} response 1801 * The response from the Ajax request. 1802 * @param {Array} response.data 1803 * An array of objects of script attributes. 1804 * @param {number} [status] 1805 * The XMLHttpRequest status. 1806 */ 1807 add_js(ajax, response, status) { 1808 const parentEl = document.querySelector(response.selector || 'body'); 1809 const settings = ajax.settings || drupalSettings; 1810 const allUniqueBundleIds = response.data.map((script) => { 1811 const uniqueBundleId = script.src; 1812 if (!loadjs.isDefined(uniqueBundleId)) { 1813 loadjs(script.src, uniqueBundleId, { 1814 // The default loadjs behavior is to load script with async, in Drupal 1815 // we need to explicitly tell scripts to load async, this is set in 1816 // the before callback below if necessary. 1817 async: false, 1818 before(path, scriptEl) { 1819 // This allows all attributes to be added, like defer, async and 1820 // crossorigin. 1821 Object.keys(script).forEach((attributeKey) => { 1822 scriptEl.setAttribute(attributeKey, script[attributeKey]); 1823 }); 1824 1825 // By default, loadjs appends the script to the head. When scripts 1826 // are loaded via AJAX, their location has no impact on 1827 // functionality. But, since non-AJAX loaded scripts can choose 1828 // their parent element, we provide that option here for the sake of 1829 // consistency. 1830 parentEl.appendChild(scriptEl); 1831 // Return false to bypass loadjs' default DOM insertion mechanism. 1832 return false;
1833 }, 1834 }); 1835 } 1836 return uniqueBundleId; 1837 }); 1838 // Returns the promise so that the next AJAX command waits on the 1839 // completion of this one to execute, ensuring the JS is loaded before 1840 // executing. 1841 return new Promise((resolve, reject) => { 1842 loadjs.ready(allUniqueBundleIds, { 1843 success() { 1844 Drupal.attachBehaviors(parentEl, settings); 1845 // All JS files were loaded and new and old behaviors have 1846 // been attached. Resolve the promise and let the remaining 1847 // commands execute. 1848 resolve(); 1849 }, 1850 error(depsNotFound) { 1851 const message = Drupal.t( 1852 `The following files could not be loaded: @dependencies`, 1853 { '@dependencies': depsNotFound.join(', ') }, 1854 ); 1855 reject(message); 1856 }, 1857 }); 1858 }); 1859 }, 1860 1861 /** 1862 * Command to scroll the page to an html element. 1863 * 1864 * @param {Drupal.Ajax} [ajax] 1865 * A {@link Drupal.ajax} object. 1866 * @param {object} response 1867 * Ajax response. 1868 * @param {string} response.selector 1869 * Selector to use. 1870 */ 1871 scrollTop(ajax, response) { 1872 document.querySelector(response.selector)?.scrollIntoView(); 1873 }, 1874 }; 1875 1876 /** 1877 * Delay jQuery's global completion events until after commands have executed. 1878 * 1879 * jQuery triggers the ajaxSuccess, ajaxComplete, and ajaxStop events after 1880 * a successful response is returned and local success and complete events 1881 * are triggered. However, Drupal Ajax responses contain commands that run 1882 * asynchronously in a queue, so the following stops these events from getting 1883 * triggered until after the Promise that executes the command queue is 1884 * resolved. 1885 */ 1886 const stopEvent = (xhr, settings) => { 1887 return ( 1888 // Only interfere with Drupal's Ajax responses. 1889 xhr.getResponseHeader('X-Drupal-Ajax-Token') === '1' && 1890 // The isInProgress() function might not be defined if the Ajax request 1891 // was initiated without Drupal.ajax() or new Drupal.Ajax(). 1892 settings.isInProgress && 1893 // Until this is false, the Ajax request isn't completely done (the 1894 // response's commands might still be running). 1895 settings.isInProgress() 1896 ); 1897 }; 1898 $.extend(true, $.event.special, { 1899 ajaxSuccess: { 1900 trigger(event, xhr, settings) { 1901 if (stopEvent(xhr, settings)) { 1902 return false; 1903 } 1904 }, 1905 }, 1906 ajaxComplete: { 1907 trigger(event, xhr, settings) { 1908 if (stopEvent(xhr, settings)) { 1909 // jQuery decrements its internal active ajax counter even when we 1910 // stop the ajaxComplete event, but we don't want that counter 1911 // decremented, because for our purposes this request is still active 1912 // while commands are executing. By incrementing it here, the net 1913 // effect is that it remains unchanged. By remaining above 0, the 1914 // ajaxStop event is also prevented. 1915 $.active++; 1916 return false; 1917 } 1918 }, 1919 }, 1920 }); 1921})(jQuery, window, Drupal, drupalSettings, loadjs, window.tabbable);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.