92c14e233f2d13fb6b7dbf80d211079245762f67
[yaffs-website] / web / core / modules / views_ui / js / views_ui.listing.es6.js
1 /**
2  * @file
3  * Views listing behaviors.
4  */
5
6 (function($, Drupal) {
7   /**
8    * Filters the view listing tables by a text input search string.
9    *
10    * Text search input: input.views-filter-text
11    * Target table:      input.views-filter-text[data-table]
12    * Source text:       [data-drupal-selector="views-table-filter-text-source"]
13    *
14    * @type {Drupal~behavior}
15    *
16    * @prop {Drupal~behaviorAttach} attach
17    *   Attaches the filter functionality to the views admin text search field.
18    */
19   Drupal.behaviors.viewTableFilterByText = {
20     attach(context, settings) {
21       const $input = $('input.views-filter-text').once('views-filter-text');
22       const $table = $($input.attr('data-table'));
23       let $rows;
24
25       function filterViewList(e) {
26         const query = $(e.target)
27           .val()
28           .toLowerCase();
29
30         function showViewRow(index, row) {
31           const $row = $(row);
32           const $sources = $row.find(
33             '[data-drupal-selector="views-table-filter-text-source"]',
34           );
35           const textMatch =
36             $sources
37               .text()
38               .toLowerCase()
39               .indexOf(query) !== -1;
40           $row.closest('tr').toggle(textMatch);
41         }
42
43         // Filter if the length of the query is at least 2 characters.
44         if (query.length >= 2) {
45           $rows.each(showViewRow);
46         } else {
47           $rows.show();
48         }
49       }
50
51       if ($table.length) {
52         $rows = $table.find('tbody tr');
53         $input.on('keyup', filterViewList);
54       }
55     },
56   };
57 })(jQuery, Drupal);