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