locale.admin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @file
  3. * Locale admin behavior.
  4. */
  5. (function ($, Drupal) {
  6. 'use strict';
  7. /**
  8. * Marks changes of translations.
  9. *
  10. * @type {Drupal~behavior}
  11. *
  12. * @prop {Drupal~behaviorAttach} attach
  13. * Attaches behavior to show the user if translations has changed.
  14. * @prop {Drupal~behaviorDetach} detach
  15. * Detach behavior to show the user if translations has changed.
  16. */
  17. Drupal.behaviors.localeTranslateDirty = {
  18. attach: function () {
  19. var $form = $('#locale-translate-edit-form').once('localetranslatedirty');
  20. if ($form.length) {
  21. // Display a notice if any row changed.
  22. $form.one('formUpdated.localeTranslateDirty', 'table', function () {
  23. var $marker = $(Drupal.theme('localeTranslateChangedWarning')).hide();
  24. $(this).addClass('changed').before($marker);
  25. $marker.fadeIn('slow');
  26. });
  27. // Highlight changed row.
  28. $form.on('formUpdated.localeTranslateDirty', 'tr', function () {
  29. var $row = $(this);
  30. var $rowToMark = $row.once('localemark');
  31. var marker = Drupal.theme('localeTranslateChangedMarker');
  32. $row.addClass('changed');
  33. // Add an asterisk only once if row changed.
  34. if ($rowToMark.length) {
  35. $rowToMark.find('td:first-child .js-form-item').append(marker);
  36. }
  37. });
  38. }
  39. },
  40. detach: function (context, settings, trigger) {
  41. if (trigger === 'unload') {
  42. var $form = $('#locale-translate-edit-form').removeOnce('localetranslatedirty');
  43. if ($form.length) {
  44. $form.off('formUpdated.localeTranslateDirty');
  45. }
  46. }
  47. }
  48. };
  49. /**
  50. * Show/hide the description details on Available translation updates page.
  51. *
  52. * @type {Drupal~behavior}
  53. *
  54. * @prop {Drupal~behaviorAttach} attach
  55. * Attaches behavior for toggling details on the translation update page.
  56. */
  57. Drupal.behaviors.hideUpdateInformation = {
  58. attach: function (context, settings) {
  59. var $table = $('#locale-translation-status-form').once('expand-updates');
  60. if ($table.length) {
  61. var $tbodies = $table.find('tbody');
  62. // Open/close the description details by toggling a tr class.
  63. $tbodies.on('click keydown', '.description', function (e) {
  64. if (e.keyCode && (e.keyCode !== 13 && e.keyCode !== 32)) {
  65. return;
  66. }
  67. e.preventDefault();
  68. var $tr = $(this).closest('tr');
  69. $tr.toggleClass('expanded');
  70. // Change screen reader text.
  71. $tr.find('.locale-translation-update__prefix').text(function () {
  72. if ($tr.hasClass('expanded')) {
  73. return Drupal.t('Hide description');
  74. }
  75. else {
  76. return Drupal.t('Show description');
  77. }
  78. });
  79. });
  80. $table.find('.requirements, .links').hide();
  81. }
  82. }
  83. };
  84. $.extend(Drupal.theme, /** @lends Drupal.theme */{
  85. /**
  86. * Creates markup for a changed translation marker.
  87. *
  88. * @return {string}
  89. * Markup for the marker.
  90. */
  91. localeTranslateChangedMarker: function () {
  92. return '<abbr class="warning ajax-changed" title="' + Drupal.t('Changed') + '">*</abbr>';
  93. },
  94. /**
  95. * Creates markup for the translation changed warning.
  96. *
  97. * @return {string}
  98. * Markup for the warning.
  99. */
  100. localeTranslateChangedWarning: function () {
  101. return '<div class="clearfix messages messages--warning">' + Drupal.theme('localeTranslateChangedMarker') + ' ' + Drupal.t('Changes made in this table will not be saved until the form is submitted.') + '</div>';
  102. }
  103. });
  104. })(jQuery, Drupal);