53ace084db0394cb8fc0619879dc50afcb704f83
[yaffs-website] / web / core / modules / content_translation / content_translation.admin.es6.js
1 /**
2  * @file
3  * Content Translation admin behaviors.
4  */
5
6 (function ($, Drupal, drupalSettings) {
7   /**
8    * Forces applicable options to be checked as translatable.
9    *
10    * @type {Drupal~behavior}
11    *
12    * @prop {Drupal~behaviorAttach} attach
13    *   Attaches content translation dependent options to the UI.
14    */
15   Drupal.behaviors.contentTranslationDependentOptions = {
16     attach(context) {
17       const $context = $(context);
18       const options = drupalSettings.contentTranslationDependentOptions;
19       let $fields;
20
21       function fieldsChangeHandler($fields, dependentColumns) {
22         return function (e) {
23           Drupal.behaviors.contentTranslationDependentOptions.check($fields, dependentColumns, $(e.target));
24         };
25       }
26
27       // We're given a generic name to look for so we find all inputs containing
28       // that name and copy over the input values that require all columns to be
29       // translatable.
30       if (options && options.dependent_selectors) {
31         Object.keys(options.dependent_selectors).forEach((field) => {
32           $fields = $context.find(`input[name^="${field}"]`);
33           const dependentColumns = options.dependent_selectors[field];
34
35           $fields.on('change', fieldsChangeHandler($fields, dependentColumns));
36           Drupal.behaviors.contentTranslationDependentOptions.check($fields, dependentColumns);
37         });
38       }
39     },
40     check($fields, dependentColumns, $changed) {
41       let $element = $changed;
42       let column;
43
44       function filterFieldsList(index, field) {
45         return $(field).val() === column;
46       }
47
48       // A field that has many different translatable parts can also define one
49       // or more columns that require all columns to be translatable.
50       Object.keys(dependentColumns || {}).forEach((index) => {
51         column = dependentColumns[index];
52
53         if (!$changed) {
54           $element = $fields.filter(filterFieldsList);
55         }
56
57         if ($element.is(`input[value="${column}"]:checked`)) {
58           $fields.prop('checked', true)
59             .not($element).prop('disabled', true);
60         }
61         else {
62           $fields.prop('disabled', false);
63         }
64       });
65     },
66   };
67
68   /**
69    * Makes field translatability inherit bundle translatability.
70    *
71    * @type {Drupal~behavior}
72    *
73    * @prop {Drupal~behaviorAttach} attach
74    *   Attaches content translation behavior.
75    */
76   Drupal.behaviors.contentTranslation = {
77     attach(context) {
78       // Initially hide all field rows for non translatable bundles and all
79       // column rows for non translatable fields.
80       $(context).find('table .bundle-settings .translatable :input').once('translation-entity-admin-hide').each(function () {
81         const $input = $(this);
82         const $bundleSettings = $input.closest('.bundle-settings');
83         if (!$input.is(':checked')) {
84           $bundleSettings.nextUntil('.bundle-settings').hide();
85         }
86         else {
87           $bundleSettings
88             .nextUntil('.bundle-settings', '.field-settings')
89             .find('.translatable :input:not(:checked)')
90             .closest('.field-settings')
91             .nextUntil(':not(.column-settings)')
92             .hide();
93         }
94       });
95
96       // When a bundle is made translatable all of its fields should inherit
97       // this setting. Instead when it is made non translatable its fields are
98       // hidden, since their translatability no longer matters.
99       $('body').once('translation-entity-admin-bind').on('click', 'table .bundle-settings .translatable :input', (e) => {
100         const $target = $(e.target);
101         const $bundleSettings = $target.closest('.bundle-settings');
102         const $settings = $bundleSettings.nextUntil('.bundle-settings');
103         const $fieldSettings = $settings.filter('.field-settings');
104         if ($target.is(':checked')) {
105           $bundleSettings.find('.operations :input[name$="[language_alterable]"]').prop('checked', true);
106           $fieldSettings.find('.translatable :input').prop('checked', true);
107           $settings.show();
108         }
109         else {
110           $settings.hide();
111         }
112       })
113         .on('click', 'table .field-settings .translatable :input', (e) => {
114           const $target = $(e.target);
115           const $fieldSettings = $target.closest('.field-settings');
116           const $columnSettings = $fieldSettings.nextUntil('.field-settings, .bundle-settings');
117           if ($target.is(':checked')) {
118             $columnSettings.show();
119           }
120           else {
121             $columnSettings.hide();
122           }
123         });
124     },
125   };
126 }(jQuery, Drupal, drupalSettings));