3 * Module page behaviors.
6 (function($, Drupal, debounce) {
8 * Filters the module list table by a text input search string.
10 * Additionally accounts for multiple tables being wrapped in "package" details
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
17 * @type {Drupal~behavior}
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'));
26 let searching = false;
28 function hidePackageDetails(index, element) {
29 const $packDetails = $(element);
30 const $visibleRows = $packDetails.find('tbody tr:visible');
31 $packDetails.toggle($visibleRows.length > 0);
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');
39 function showModuleRow(index, row) {
41 const $sources = $row.find(
42 '.table-filter-text-source, .module-name, .module-description',
44 const textMatch = $sources.text().search(re) !== -1;
45 $row.closest('tr').toggle(textMatch);
47 // Search over all rows and packages.
48 $rowsAndDetails.show();
50 // Filter if the length of the query is at least 2 characters.
51 if (query.length >= 2) {
53 $rows.each(showModuleRow);
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.
60 .attr('data-drupal-system-state', 'forced-open');
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);
67 Drupal.t('!modules modules are available in the modified list.', {
68 '!modules': $rowsAndDetails.find('tbody tr:visible').length,
71 } else if (searching) {
73 $rowsAndDetails.show();
74 // Return <details> elements that had been closed before filtering
77 .filter('[data-drupal-system-state="forced-open"]')
78 .removeAttr('data-drupal-system-state')
83 function preventEnterKey(event) {
84 if (event.which === 13) {
85 event.preventDefault();
86 event.stopPropagation();
91 $rowsAndDetails = $table.find('tr, details');
92 $rows = $table.find('tbody tr');
93 $details = $rowsAndDetails.filter('.package-listing');
96 keyup: debounce(filterModuleList, 200),
97 keydown: preventEnterKey,
102 })(jQuery, Drupal, Drupal.debounce);