1/** 2 * @file 3 * System behaviors. 4 */ 5 6(function ($, Drupal, drupalSettings) { 7 // Cache IDs in an array for ease of use. 8 const ids = []; 9 10 /** 11 * Attaches field copy behavior from input fields to other input fields. 12 * 13 * When a field is filled out, apply its value to other fields that will 14 * likely use the same value. In the installer this is used to populate the 15 * administrator email address with the same value as the site email address. 16 * 17 * @type {Drupal~behavior} 18 * 19 * @prop {Drupal~behaviorAttach} attach 20 * Attaches the field copy behavior to an input field. 21 */ 22 Drupal.behaviors.copyFieldValue = { 23 attach(context) { 24 // List of fields IDs on which to bind the event listener. 25 // Create an array of IDs to use with jQuery. 26 Object.keys(drupalSettings.copyFieldValue || {}).forEach((element) => { 27 ids.push(element); 28 }); 29 30 if (ids.length) { 31 // Listen to value:copy events on all dependent fields. 32 // We have to use body and not document because of the way jQuery events 33 // bubble up the DOM tree. 34 $(once('copy-field-values', 'body')).on( 35 'value:copy', 36 this.valueTargetCopyHandler, 37 ); 38 // Listen on all source elements. 39 $(once('copy-field-values', `#${ids.join(', #')}`)).on( 40 'blur', 41 this.valueSourceBlurHandler, 42 ); 43 } 44 }, 45 detach(context, settings, trigger) { 46 if (trigger === 'unload' && ids.length) { 47 $(once.remove('copy-field-values', 'body')).off('value:copy'); 48 $(once.remove('copy-field-values', `#${ids.join(', #')}`)).off('blur'); 49 } 50 }, 51 52 /** 53 * Event handler that fill the target element with the specified value. 54 * 55 * @param {jQuery.Event} e 56 * Event object. 57 * @param {string} value 58 * Custom value from jQuery trigger. 59 */ 60 valueTargetCopyHandler(e, value) { 61 const { target } = e; 62 if (target.value === '') { 63 target.value = value; 64 } 65 }, 66 67 /** 68 * Handler for a Blur event on a source field. 69 * 70 * This event handler will trigger a 'value:copy' event on all dependent 71 * fields. 72 * 73 * @param {jQuery.Event} e 74 * The event triggered. 75 */ 76 valueSourceBlurHandler(e) { 77 const { value } = e.target; 78 const targetIds = drupalSettings.copyFieldValue[e.target.id]; 79 $(`#${targetIds.join(', #')}`).trigger('value:copy', value); 80 }, 81 }; 82})(jQuery, Drupal, drupalSettings);
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.