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