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