Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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(
24             $fields,
25             dependentColumns,
26             $(e.target),
27           );
28         };
29       }
30
31       // We're given a generic name to look for so we find all inputs containing
32       // that name and copy over the input values that require all columns to be
33       // translatable.
34       if (options && options.dependent_selectors) {
35         Object.keys(options.dependent_selectors).forEach(field => {
36           $fields = $context.find(`input[name^="${field}"]`);
37           const dependentColumns = options.dependent_selectors[field];
38
39           $fields.on('change', fieldsChangeHandler($fields, dependentColumns));
40           Drupal.behaviors.contentTranslationDependentOptions.check(
41             $fields,
42             dependentColumns,
43           );
44         });
45       }
46     },
47     check($fields, dependentColumns, $changed) {
48       let $element = $changed;
49       let column;
50
51       function filterFieldsList(index, field) {
52         return $(field).val() === column;
53       }
54
55       // A field that has many different translatable parts can also define one
56       // or more columns that require all columns to be translatable.
57       Object.keys(dependentColumns || {}).forEach(index => {
58         column = dependentColumns[index];
59
60         if (!$changed) {
61           $element = $fields.filter(filterFieldsList);
62         }
63
64         if ($element.is(`input[value="${column}"]:checked`)) {
65           $fields
66             .prop('checked', true)
67             .not($element)
68             .prop('disabled', true);
69         } else {
70           $fields.prop('disabled', false);
71         }
72       });
73     },
74   };
75
76   /**
77    * Makes field translatability inherit bundle translatability.
78    *
79    * @type {Drupal~behavior}
80    *
81    * @prop {Drupal~behaviorAttach} attach
82    *   Attaches content translation behavior.
83    */
84   Drupal.behaviors.contentTranslation = {
85     attach(context) {
86       // Initially hide all field rows for non translatable bundles and all
87       // column rows for non translatable fields.
88       $(context)
89         .find('table .bundle-settings .translatable :input')
90         .once('translation-entity-admin-hide')
91         .each(function() {
92           const $input = $(this);
93           const $bundleSettings = $input.closest('.bundle-settings');
94           if (!$input.is(':checked')) {
95             $bundleSettings.nextUntil('.bundle-settings').hide();
96           } else {
97             $bundleSettings
98               .nextUntil('.bundle-settings', '.field-settings')
99               .find('.translatable :input:not(:checked)')
100               .closest('.field-settings')
101               .nextUntil(':not(.column-settings)')
102               .hide();
103           }
104         });
105
106       // When a bundle is made translatable all of its fields should inherit
107       // this setting. Instead when it is made non translatable its fields are
108       // hidden, since their translatability no longer matters.
109       $('body')
110         .once('translation-entity-admin-bind')
111         .on('click', 'table .bundle-settings .translatable :input', e => {
112           const $target = $(e.target);
113           const $bundleSettings = $target.closest('.bundle-settings');
114           const $settings = $bundleSettings.nextUntil('.bundle-settings');
115           const $fieldSettings = $settings.filter('.field-settings');
116           if ($target.is(':checked')) {
117             $bundleSettings
118               .find('.operations :input[name$="[language_alterable]"]')
119               .prop('checked', true);
120             $fieldSettings.find('.translatable :input').prop('checked', true);
121             $settings.show();
122           } else {
123             $settings.hide();
124           }
125         })
126         .on('click', 'table .field-settings .translatable :input', e => {
127           const $target = $(e.target);
128           const $fieldSettings = $target.closest('.field-settings');
129           const $columnSettings = $fieldSettings.nextUntil(
130             '.field-settings, .bundle-settings',
131           );
132           if ($target.is(':checked')) {
133             $columnSettings.show();
134           } else {
135             $columnSettings.hide();
136           }
137         });
138     },
139   };
140 })(jQuery, Drupal, drupalSettings);