6 (function ($, Drupal, drupalSettings) {
10 // Cache IDs in an array for ease of use.
14 * Attaches field copy behavior from input fields to other input fields.
16 * When a field is filled out, apply its value to other fields that will
17 * likely use the same value. In the installer this is used to populate the
18 * administrator email address with the same value as the site email address.
20 * @type {Drupal~behavior}
22 * @prop {Drupal~behaviorAttach} attach
23 * Attaches the field copy behavior to an input field.
25 Drupal.behaviors.copyFieldValue = {
26 attach: function (context) {
27 // List of fields IDs on which to bind the event listener.
28 // Create an array of IDs to use with jQuery.
29 for (var sourceId in drupalSettings.copyFieldValue) {
30 if (drupalSettings.copyFieldValue.hasOwnProperty(sourceId)) {
35 // Listen to value:copy events on all dependent fields.
36 // We have to use body and not document because of the way jQuery events
37 // bubble up the DOM tree.
38 $('body').once('copy-field-values').on('value:copy', this.valueTargetCopyHandler);
39 // Listen on all source elements.
40 $('#' + ids.join(', #')).once('copy-field-values').on('blur', this.valueSourceBlurHandler);
43 detach: function (context, settings, trigger) {
44 if (trigger === 'unload' && ids.length) {
45 $('body').removeOnce('copy-field-values').off('value:copy');
46 $('#' + ids.join(', #')).removeOnce('copy-field-values').off('blur');
51 * Event handler that fill the target element with the specified value.
53 * @param {jQuery.Event} e
55 * @param {string} value
56 * Custom value from jQuery trigger.
58 valueTargetCopyHandler: function (e, value) {
59 var $target = $(e.target);
60 if ($target.val() === '') {
66 * Handler for a Blur event on a source field.
68 * This event handler will trigger a 'value:copy' event on all dependent
71 * @param {jQuery.Event} e
72 * The event triggered.
74 valueSourceBlurHandler: function (e) {
75 var value = $(e.target).val();
76 var targetIds = drupalSettings.copyFieldValue[e.target.id];
77 $('#' + targetIds.join(', #')).trigger('value:copy', value);
81 })(jQuery, Drupal, drupalSettings);