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