Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / locale / locale.admin.es6.js
1 /**
2  * @file
3  * Locale admin behavior.
4  */
5
6 (function($, Drupal) {
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() {
19       const $form = $('#locale-translate-edit-form').once(
20         'localetranslatedirty',
21       );
22       if ($form.length) {
23         // Display a notice if any row changed.
24         $form.one('formUpdated.localeTranslateDirty', 'table', function() {
25           const $marker = $(
26             Drupal.theme('localeTranslateChangedWarning'),
27           ).hide();
28           $(this)
29             .addClass('changed')
30             .before($marker);
31           $marker.fadeIn('slow');
32         });
33         // Highlight changed row.
34         $form.on('formUpdated.localeTranslateDirty', 'tr', function() {
35           const $row = $(this);
36           const $rowToMark = $row.once('localemark');
37           const marker = Drupal.theme('localeTranslateChangedMarker');
38
39           $row.addClass('changed');
40           // Add an asterisk only once if row changed.
41           if ($rowToMark.length) {
42             $rowToMark.find('td:first-child .js-form-item').append(marker);
43           }
44         });
45       }
46     },
47     detach(context, settings, trigger) {
48       if (trigger === 'unload') {
49         const $form = $('#locale-translate-edit-form').removeOnce(
50           'localetranslatedirty',
51         );
52         if ($form.length) {
53           $form.off('formUpdated.localeTranslateDirty');
54         }
55       }
56     },
57   };
58
59   /**
60    * Show/hide the description details on Available translation updates page.
61    *
62    * @type {Drupal~behavior}
63    *
64    * @prop {Drupal~behaviorAttach} attach
65    *   Attaches behavior for toggling details on the translation update page.
66    */
67   Drupal.behaviors.hideUpdateInformation = {
68     attach(context, settings) {
69       const $table = $('#locale-translation-status-form').once(
70         'expand-updates',
71       );
72       if ($table.length) {
73         const $tbodies = $table.find('tbody');
74
75         // Open/close the description details by toggling a tr class.
76         $tbodies.on('click keydown', '.description', function(e) {
77           if (e.keyCode && (e.keyCode !== 13 && e.keyCode !== 32)) {
78             return;
79           }
80           e.preventDefault();
81           const $tr = $(this).closest('tr');
82
83           $tr.toggleClass('expanded');
84
85           // Change screen reader text.
86           $tr.find('.locale-translation-update__prefix').text(() => {
87             if ($tr.hasClass('expanded')) {
88               return Drupal.t('Hide description');
89             }
90
91             return Drupal.t('Show description');
92           });
93         });
94         $table.find('.requirements, .links').hide();
95       }
96     },
97   };
98
99   $.extend(
100     Drupal.theme,
101     /** @lends Drupal.theme */ {
102       /**
103        * Creates markup for a changed translation marker.
104        *
105        * @return {string}
106        *   Markup for the marker.
107        */
108       localeTranslateChangedMarker() {
109         return `<abbr class="warning ajax-changed" title="${Drupal.t(
110           'Changed',
111         )}">*</abbr>`;
112       },
113
114       /**
115        * Creates markup for the translation changed warning.
116        *
117        * @return {string}
118        *   Markup for the warning.
119        */
120       localeTranslateChangedWarning() {
121         return `<div class="clearfix messages messages--warning">${Drupal.theme(
122           'localeTranslateChangedMarker',
123         )} ${Drupal.t(
124           'Changes made in this table will not be saved until the form is submitted.',
125         )}</div>`;
126       },
127     },
128   );
129 })(jQuery, Drupal);