content_translation.admin.es6.js 4.6 KB

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