Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / js / system.modules.es6.js
1 /**
2  * @file
3  * Module page behaviors.
4  */
5
6 (function($, Drupal, debounce) {
7   /**
8    * Filters the module list table by a text input search string.
9    *
10    * Additionally accounts for multiple tables being wrapped in "package" details
11    * elements.
12    *
13    * Text search input: input.table-filter-text
14    * Target table:      input.table-filter-text[data-table]
15    * Source text:       .table-filter-text-source, .module-name, .module-description
16    *
17    * @type {Drupal~behavior}
18    */
19   Drupal.behaviors.tableFilterByText = {
20     attach(context, settings) {
21       const $input = $('input.table-filter-text').once('table-filter-text');
22       const $table = $($input.attr('data-table'));
23       let $rowsAndDetails;
24       let $rows;
25       let $details;
26       let searching = false;
27
28       function hidePackageDetails(index, element) {
29         const $packDetails = $(element);
30         const $visibleRows = $packDetails.find('tbody tr:visible');
31         $packDetails.toggle($visibleRows.length > 0);
32       }
33
34       function filterModuleList(e) {
35         const query = $(e.target).val();
36         // Case insensitive expression to find query at the beginning of a word.
37         const re = new RegExp(`\\b${query}`, 'i');
38
39         function showModuleRow(index, row) {
40           const $row = $(row);
41           const $sources = $row.find(
42             '.table-filter-text-source, .module-name, .module-description',
43           );
44           const textMatch = $sources.text().search(re) !== -1;
45           $row.closest('tr').toggle(textMatch);
46         }
47         // Search over all rows and packages.
48         $rowsAndDetails.show();
49
50         // Filter if the length of the query is at least 2 characters.
51         if (query.length >= 2) {
52           searching = true;
53           $rows.each(showModuleRow);
54
55           // Note that we first open all <details> to be able to use ':visible'.
56           // Mark the <details> elements that were closed before filtering, so
57           // they can be reclosed when filtering is removed.
58           $details
59             .not('[open]')
60             .attr('data-drupal-system-state', 'forced-open');
61
62           // Hide the package <details> if they don't have any visible rows.
63           // Note that we first show() all <details> to be able to use ':visible'.
64           $details.attr('open', true).each(hidePackageDetails);
65
66           Drupal.announce(
67             Drupal.t('!modules modules are available in the modified list.', {
68               '!modules': $rowsAndDetails.find('tbody tr:visible').length,
69             }),
70           );
71         } else if (searching) {
72           searching = false;
73           $rowsAndDetails.show();
74           // Return <details> elements that had been closed before filtering
75           // to a closed state.
76           $details
77             .filter('[data-drupal-system-state="forced-open"]')
78             .removeAttr('data-drupal-system-state')
79             .attr('open', false);
80         }
81       }
82
83       function preventEnterKey(event) {
84         if (event.which === 13) {
85           event.preventDefault();
86           event.stopPropagation();
87         }
88       }
89
90       if ($table.length) {
91         $rowsAndDetails = $table.find('tr, details');
92         $rows = $table.find('tbody tr');
93         $details = $rowsAndDetails.filter('.package-listing');
94
95         $input.on({
96           keyup: debounce(filterModuleList, 200),
97           keydown: preventEnterKey,
98         });
99       }
100     },
101   };
102 })(jQuery, Drupal, Drupal.debounce);