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