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 let dependent_columns;
|
Chris@0
|
21
|
Chris@0
|
22 function fieldsChangeHandler($fields, dependent_columns) {
|
Chris@0
|
23 return function (e) {
|
Chris@0
|
24 Drupal.behaviors.contentTranslationDependentOptions.check($fields, dependent_columns, $(e.target));
|
Chris@0
|
25 };
|
Chris@0
|
26 }
|
Chris@0
|
27
|
Chris@0
|
28 // We're given a generic name to look for so we find all inputs containing
|
Chris@0
|
29 // that name and copy over the input values that require all columns to be
|
Chris@0
|
30 // translatable.
|
Chris@0
|
31 if (options && options.dependent_selectors) {
|
Chris@0
|
32 for (const field in options.dependent_selectors) {
|
Chris@0
|
33 if (options.dependent_selectors.hasOwnProperty(field)) {
|
Chris@0
|
34 $fields = $context.find(`input[name^="${field}"]`);
|
Chris@0
|
35 dependent_columns = options.dependent_selectors[field];
|
Chris@0
|
36
|
Chris@0
|
37 $fields.on('change', fieldsChangeHandler($fields, dependent_columns));
|
Chris@0
|
38 Drupal.behaviors.contentTranslationDependentOptions.check($fields, dependent_columns);
|
Chris@0
|
39 }
|
Chris@0
|
40 }
|
Chris@0
|
41 }
|
Chris@0
|
42 },
|
Chris@0
|
43 check($fields, dependent_columns, $changed) {
|
Chris@0
|
44 let $element = $changed;
|
Chris@0
|
45 let column;
|
Chris@0
|
46
|
Chris@0
|
47 function filterFieldsList(index, field) {
|
Chris@0
|
48 return $(field).val() === column;
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 // A field that has many different translatable parts can also define one
|
Chris@0
|
52 // or more columns that require all columns to be translatable.
|
Chris@0
|
53 for (const index in dependent_columns) {
|
Chris@0
|
54 if (dependent_columns.hasOwnProperty(index)) {
|
Chris@0
|
55 column = dependent_columns[index];
|
Chris@0
|
56
|
Chris@0
|
57 if (!$changed) {
|
Chris@0
|
58 $element = $fields.filter(filterFieldsList);
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 if ($element.is(`input[value="${column}"]:checked`)) {
|
Chris@0
|
62 $fields.prop('checked', true)
|
Chris@0
|
63 .not($element).prop('disabled', true);
|
Chris@0
|
64 }
|
Chris@0
|
65 else {
|
Chris@0
|
66 $fields.prop('disabled', false);
|
Chris@0
|
67 }
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@0
|
70 },
|
Chris@0
|
71 };
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * Makes field translatability inherit bundle translatability.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @type {Drupal~behavior}
|
Chris@0
|
77 *
|
Chris@0
|
78 * @prop {Drupal~behaviorAttach} attach
|
Chris@0
|
79 * Attaches content translation behavior.
|
Chris@0
|
80 */
|
Chris@0
|
81 Drupal.behaviors.contentTranslation = {
|
Chris@0
|
82 attach(context) {
|
Chris@0
|
83 // Initially hide all field rows for non translatable bundles and all
|
Chris@0
|
84 // column rows for non translatable fields.
|
Chris@0
|
85 $(context).find('table .bundle-settings .translatable :input').once('translation-entity-admin-hide').each(function () {
|
Chris@0
|
86 const $input = $(this);
|
Chris@0
|
87 const $bundleSettings = $input.closest('.bundle-settings');
|
Chris@0
|
88 if (!$input.is(':checked')) {
|
Chris@0
|
89 $bundleSettings.nextUntil('.bundle-settings').hide();
|
Chris@0
|
90 }
|
Chris@0
|
91 else {
|
Chris@0
|
92 $bundleSettings.nextUntil('.bundle-settings', '.field-settings').find('.translatable :input:not(:checked)').closest('.field-settings').nextUntil(':not(.column-settings)').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));
|