d47ca7031e8f9514408a2dae0754da6a5a529ac0
[yaffs-website] / web / core / modules / system / js / system.modules.js
1 /**
2  * @file
3  * Module page behaviors.
4  */
5
6 (function ($, Drupal, debounce) {
7
8   'use strict';
9
10   /**
11    * Filters the module list table by a text input search string.
12    *
13    * Additionally accounts for multiple tables being wrapped in "package" details
14    * elements.
15    *
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
19    *
20    * @type {Drupal~behavior}
21    */
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'));
26       var $rowsAndDetails;
27       var $rows;
28       var $details;
29       var searching = false;
30
31       function hidePackageDetails(index, element) {
32         var $packDetails = $(element);
33         var $visibleRows = $packDetails.find('tbody tr:visible');
34         $packDetails.toggle($visibleRows.length > 0);
35       }
36
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');
41
42         function showModuleRow(index, row) {
43           var $row = $(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);
47         }
48         // Search over all rows and packages.
49         $rowsAndDetails.show();
50
51         // Filter if the length of the query is at least 2 characters.
52         if (query.length >= 2) {
53           searching = true;
54           $rows.each(showModuleRow);
55
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');
60
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);
64
65           Drupal.announce(
66             Drupal.t(
67               '!modules modules are available in the modified list.',
68               {'!modules': $rowsAndDetails.find('tbody tr:visible').length}
69             )
70           );
71         }
72         else if (searching) {
73           searching = false;
74           $rowsAndDetails.show();
75           // Return <details> elements that had been closed before filtering
76           // to a closed state.
77           $details.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
103 }(jQuery, Drupal, Drupal.debounce));