summaryrefslogtreecommitdiffstats
path: root/core/modules/content_translation/content_translation.admin.es6.js
blob: 84aaf7716f6f3fd3837e500112cb6e7a5d0c0fa2 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
 * @file
 * Content Translation admin behaviors.
 */

(function ($, Drupal, drupalSettings) {
  /**
   * Forces applicable options to be checked as translatable.
   *
   * @type {Drupal~behavior}
   *
   * @prop {Drupal~behaviorAttach} attach
   *   Attaches content translation dependent options to the UI.
   */
  Drupal.behaviors.contentTranslationDependentOptions = {
    attach(context) {
      const $context = $(context);
      const options = drupalSettings.contentTranslationDependentOptions;
      let $fields;

      function fieldsChangeHandler($fields, dependent_columns) {
        return function (e) {
          Drupal.behaviors.contentTranslationDependentOptions.check($fields, dependent_columns, $(e.target));
        };
      }

      // We're given a generic name to look for so we find all inputs containing
      // that name and copy over the input values that require all columns to be
      // translatable.
      if (options && options.dependent_selectors) {
        Object.keys(options.dependent_selectors).forEach((field) => {
          $fields = $context.find(`input[name^="${field}"]`);
          const dependent_columns = options.dependent_selectors[field];

          $fields.on('change', fieldsChangeHandler($fields, dependent_columns));
          Drupal.behaviors.contentTranslationDependentOptions.check($fields, dependent_columns);
        });
      }
    },
    check($fields, dependent_columns, $changed) {
      let $element = $changed;
      let column;

      function filterFieldsList(index, field) {
        return $(field).val() === column;
      }

      // A field that has many different translatable parts can also define one
      // or more columns that require all columns to be translatable.
      Object.keys(dependent_columns).forEach((index) => {
        column = dependent_columns[index];

        if (!$changed) {
          $element = $fields.filter(filterFieldsList);
        }

        if ($element.is(`input[value="${column}"]:checked`)) {
          $fields.prop('checked', true)
            .not($element).prop('disabled', true);
        }
        else {
          $fields.prop('disabled', false);
        }
      });
    },
  };

  /**
   * Makes field translatability inherit bundle translatability.
   *
   * @type {Drupal~behavior}
   *
   * @prop {Drupal~behaviorAttach} attach
   *   Attaches content translation behavior.
   */
  Drupal.behaviors.contentTranslation = {
    attach(context) {
      // Initially hide all field rows for non translatable bundles and all
      // column rows for non translatable fields.
      $(context).find('table .bundle-settings .translatable :input').once('translation-entity-admin-hide').each(function () {
        const $input = $(this);
        const $bundleSettings = $input.closest('.bundle-settings');
        if (!$input.is(':checked')) {
          $bundleSettings.nextUntil('.bundle-settings').hide();
        }
        else {
          $bundleSettings
            .nextUntil('.bundle-settings', '.field-settings')
            .find('.translatable :input:not(:checked)')
            .closest('.field-settings')
            .nextUntil(':not(.column-settings)')
            .hide();
        }
      });

      // When a bundle is made translatable all of its fields should inherit
      // this setting. Instead when it is made non translatable its fields are
      // hidden, since their translatability no longer matters.
      $('body').once('translation-entity-admin-bind').on('click', 'table .bundle-settings .translatable :input', (e) => {
        const $target = $(e.target);
        const $bundleSettings = $target.closest('.bundle-settings');
        const $settings = $bundleSettings.nextUntil('.bundle-settings');
        const $fieldSettings = $settings.filter('.field-settings');
        if ($target.is(':checked')) {
          $bundleSettings.find('.operations :input[name$="[language_alterable]"]').prop('checked', true);
          $fieldSettings.find('.translatable :input').prop('checked', true);
          $settings.show();
        }
        else {
          $settings.hide();
        }
      })
        .on('click', 'table .field-settings .translatable :input', (e) => {
          const $target = $(e.target);
          const $fieldSettings = $target.closest('.field-settings');
          const $columnSettings = $fieldSettings.nextUntil('.field-settings, .bundle-settings');
          if ($target.is(':checked')) {
            $columnSettings.show();
          }
          else {
            $columnSettings.hide();
          }
        });
    },
  };
}(jQuery, Drupal, drupalSettings));