summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/system/js/system.js
blob: 56c29f433d7967f315eb47a2f1b971c9b8b1df10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* DO NOT EDIT THIS FILE.
* All changes should be applied to ./modules/system/js/system.es6.js
* See the following change record for more information,
* https://www.drupal.org/node/2873849
* @preserve
**/

(function ($, Drupal, drupalSettings) {

  'use strict';

  var ids = [];

  Drupal.behaviors.copyFieldValue = {
    attach: function attach(context) {
      for (var sourceId in drupalSettings.copyFieldValue) {
        if (drupalSettings.copyFieldValue.hasOwnProperty(sourceId)) {
          ids.push(sourceId);
        }
      }
      if (ids.length) {
        $('body').once('copy-field-values').on('value:copy', this.valueTargetCopyHandler);

        $('#' + ids.join(', #')).once('copy-field-values').on('blur', this.valueSourceBlurHandler);
      }
    },
    detach: function 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');
      }
    },

    valueTargetCopyHandler: function valueTargetCopyHandler(e, value) {
      var $target = $(e.target);
      if ($target.val() === '') {
        $target.val(value);
      }
    },

    valueSourceBlurHandler: function valueSourceBlurHandler(e) {
      var value = $(e.target).val();
      var targetIds = drupalSettings.copyFieldValue[e.target.id];
      $('#' + targetIds.join(', #')).trigger('value:copy', value);
    }
  };
})(jQuery, Drupal, drupalSettings);