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