3 * Module page behaviors.
6 (function ($, Drupal, debounce) {
11 * Filters the module list table by a text input search string.
13 * Additionally accounts for multiple tables being wrapped in "package" details
16 * Text search input: input.table-filter-text
17 * Target table: input.table-filter-text[data-table]
18 * Source text: .table-filter-text-source, .module-name, .module-description
20 * @type {Drupal~behavior}
22 Drupal.behaviors.tableFilterByText = {
23 attach: function (context, settings) {
24 var $input = $('input.table-filter-text').once('table-filter-text');
25 var $table = $($input.attr('data-table'));
29 var searching = false;
31 function hidePackageDetails(index, element) {
32 var $packDetails = $(element);
33 var $visibleRows = $packDetails.find('tbody tr:visible');
34 $packDetails.toggle($visibleRows.length > 0);
37 function filterModuleList(e) {
38 var query = $(e.target).val();
39 // Case insensitive expression to find query at the beginning of a word.
40 var re = new RegExp('\\b' + query, 'i');
42 function showModuleRow(index, row) {
44 var $sources = $row.find('.table-filter-text-source, .module-name, .module-description');
45 var textMatch = $sources.text().search(re) !== -1;
46 $row.closest('tr').toggle(textMatch);
48 // Search over all rows and packages.
49 $rowsAndDetails.show();
51 // Filter if the length of the query is at least 2 characters.
52 if (query.length >= 2) {
54 $rows.each(showModuleRow);
56 // Note that we first open all <details> to be able to use ':visible'.
57 // Mark the <details> elements that were closed before filtering, so
58 // they can be reclosed when filtering is removed.
59 $details.not('[open]').attr('data-drupal-system-state', 'forced-open');
61 // Hide the package <details> if they don't have any visible rows.
62 // Note that we first show() all <details> to be able to use ':visible'.
63 $details.attr('open', true).each(hidePackageDetails);
67 '!modules modules are available in the modified list.',
68 {'!modules': $rowsAndDetails.find('tbody tr:visible').length}
74 $rowsAndDetails.show();
75 // Return <details> elements that had been closed before filtering
77 $details.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
103 }(jQuery, Drupal, Drupal.debounce));