X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fsystem%2Fjs%2Fsystem.es6.js;fp=web%2Fcore%2Fmodules%2Fsystem%2Fjs%2Fsystem.es6.js;h=18153b09ec731a4e5fa4e8b07ce4ea74448f2ae1;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=0000000000000000000000000000000000000000;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/modules/system/js/system.es6.js b/web/core/modules/system/js/system.es6.js new file mode 100644 index 000000000..18153b09e --- /dev/null +++ b/web/core/modules/system/js/system.es6.js @@ -0,0 +1,77 @@ +/** + * @file + * System behaviors. + */ + +(function ($, Drupal, drupalSettings) { + // Cache IDs in an array for ease of use. + const ids = []; + + /** + * Attaches field copy behavior from input fields to other input fields. + * + * When a field is filled out, apply its value to other fields that will + * likely use the same value. In the installer this is used to populate the + * administrator email address with the same value as the site email address. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches the field copy behavior to an input field. + */ + Drupal.behaviors.copyFieldValue = { + attach(context) { + // List of fields IDs on which to bind the event listener. + // Create an array of IDs to use with jQuery. + for (const sourceId in drupalSettings.copyFieldValue) { + if (drupalSettings.copyFieldValue.hasOwnProperty(sourceId)) { + ids.push(sourceId); + } + } + if (ids.length) { + // Listen to value:copy events on all dependent fields. + // We have to use body and not document because of the way jQuery events + // bubble up the DOM tree. + $('body').once('copy-field-values').on('value:copy', this.valueTargetCopyHandler); + // Listen on all source elements. + $(`#${ids.join(', #')}`).once('copy-field-values').on('blur', this.valueSourceBlurHandler); + } + }, + detach(context, settings, trigger) { + if (trigger === 'unload' && ids.length) { + $('body').removeOnce('copy-field-values').off('value:copy'); + $(`#${ids.join(', #')}`).removeOnce('copy-field-values').off('blur'); + } + }, + + /** + * Event handler that fill the target element with the specified value. + * + * @param {jQuery.Event} e + * Event object. + * @param {string} value + * Custom value from jQuery trigger. + */ + valueTargetCopyHandler(e, value) { + const $target = $(e.target); + if ($target.val() === '') { + $target.val(value); + } + }, + + /** + * Handler for a Blur event on a source field. + * + * This event handler will trigger a 'value:copy' event on all dependent + * fields. + * + * @param {jQuery.Event} e + * The event triggered. + */ + valueSourceBlurHandler(e) { + const value = $(e.target).val(); + const targetIds = drupalSettings.copyFieldValue[e.target.id]; + $(`#${targetIds.join(', #')}`).trigger('value:copy', value); + }, + }; +}(jQuery, Drupal, drupalSettings));