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