comparison core/modules/content_translation/content_translation.admin.es6.js @ 0:4c8ae668cc8c

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