Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / simpletest / simpletest.es6.js
1 /**
2  * @file
3  * Simpletest behaviors.
4  */
5
6 (function ($, Drupal, drupalSettings) {
7   /**
8    * Collapses table rows followed by group rows on the test listing page.
9    *
10    * @type {Drupal~behavior}
11    *
12    * @prop {Drupal~behaviorAttach} attach
13    *   Attach collapse behavior on the test listing page.
14    */
15   Drupal.behaviors.simpleTestGroupCollapse = {
16     attach(context) {
17       $(context).find('.simpletest-group').once('simpletest-group-collapse').each(function () {
18         const $group = $(this);
19         const $image = $group.find('.simpletest-image');
20         $image
21           .html(drupalSettings.simpleTest.images[0])
22           .on('click', () => {
23             const $tests = $group.nextUntil('.simpletest-group');
24             const expand = !$group.hasClass('expanded');
25             $group.toggleClass('expanded', expand);
26             $tests.toggleClass('js-hide', !expand);
27             $image.html(drupalSettings.simpleTest.images[+expand]);
28           });
29       });
30     },
31   };
32
33   /**
34    * Toggles test checkboxes to match the group checkbox.
35    *
36    * @type {Drupal~behavior}
37    *
38    * @prop {Drupal~behaviorAttach} attach
39    *   Attaches behavior for selecting all tests in a group.
40    */
41   Drupal.behaviors.simpleTestSelectAll = {
42     attach(context) {
43       $(context).find('.simpletest-group').once('simpletest-group-select-all').each(function () {
44         const $group = $(this);
45         const $cell = $group.find('.simpletest-group-select-all');
46         const $groupCheckbox = $(`<input type="checkbox" id="${$cell.attr('id')}-group-select-all" class="form-checkbox" />`);
47         const $testCheckboxes = $group.nextUntil('.simpletest-group').find('input[type=checkbox]');
48         $cell.append($groupCheckbox);
49
50         // Toggle the test checkboxes when the group checkbox is toggled.
51         $groupCheckbox.on('change', function () {
52           const checked = $(this).prop('checked');
53           $testCheckboxes.prop('checked', checked);
54         });
55
56         // Update the group checkbox when a test checkbox is toggled.
57         function updateGroupCheckbox() {
58           let allChecked = true;
59           $testCheckboxes.each(function () {
60             if (!$(this).prop('checked')) {
61               allChecked = false;
62               return false;
63             }
64           });
65           $groupCheckbox.prop('checked', allChecked);
66         }
67
68         $testCheckboxes.on('change', updateGroupCheckbox);
69       });
70     },
71   };
72
73   /**
74    * Filters the test list table by a text input search string.
75    *
76    * Text search input: input.table-filter-text
77    * Target table:      input.table-filter-text[data-table]
78    * Source text:       .table-filter-text-source
79    *
80    * @type {Drupal~behavior}
81    *
82    * @prop {Drupal~behaviorAttach} attach
83    *   Attaches the filter behavior to the text input element.
84    */
85   Drupal.behaviors.simpletestTableFilterByText = {
86     attach(context) {
87       const $input = $('input.table-filter-text').once('table-filter-text');
88       const $table = $($input.attr('data-table'));
89       let $rows;
90       let searched = false;
91
92       function filterTestList(e) {
93         const query = $(e.target).val().toLowerCase();
94
95         function showTestRow(index, row) {
96           const $row = $(row);
97           const $sources = $row.find('.table-filter-text-source');
98           const textMatch = $sources.text().toLowerCase().indexOf(query) !== -1;
99           $row.closest('tr').toggle(textMatch);
100         }
101
102         // Filter if the length of the query is at least 3 characters.
103         if (query.length >= 3) {
104           // Indicate that a search has been performed, and hide the
105           // "select all" checkbox.
106           searched = true;
107           $('#simpletest-form-table thead th.select-all input').hide();
108
109           $rows.each(showTestRow);
110         }
111         // Restore to the original state if any searching has occurred.
112         else if (searched) {
113           searched = false;
114           $('#simpletest-form-table thead th.select-all input').show();
115           // Restore all rows to their original display state.
116           $rows.css('display', '');
117         }
118       }
119
120       if ($table.length) {
121         $rows = $table.find('tbody tr');
122         $input.trigger('focus').on('keyup', Drupal.debounce(filterTestList, 200));
123       }
124     },
125   };
126 }(jQuery, Drupal, drupalSettings));