simpletest.es6.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file
  3. * Simpletest behaviors.
  4. */
  5. (function($, Drupal, drupalSettings) {
  6. /**
  7. * Collapses table rows followed by group rows on the test listing page.
  8. *
  9. * @type {Drupal~behavior}
  10. *
  11. * @prop {Drupal~behaviorAttach} attach
  12. * Attach collapse behavior on the test listing page.
  13. */
  14. Drupal.behaviors.simpleTestGroupCollapse = {
  15. attach(context) {
  16. $(context)
  17. .find('.simpletest-group')
  18. .once('simpletest-group-collapse')
  19. .each(function() {
  20. const $group = $(this);
  21. const $image = $group.find('.simpletest-image');
  22. $image.html(drupalSettings.simpleTest.images[0]).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. * Toggles test checkboxes to match the group checkbox.
  34. *
  35. * @type {Drupal~behavior}
  36. *
  37. * @prop {Drupal~behaviorAttach} attach
  38. * Attaches behavior for selecting all tests in a group.
  39. */
  40. Drupal.behaviors.simpleTestSelectAll = {
  41. attach(context) {
  42. $(context)
  43. .find('.simpletest-group')
  44. .once('simpletest-group-select-all')
  45. .each(function() {
  46. const $group = $(this);
  47. const $cell = $group.find('.simpletest-group-select-all');
  48. const $groupCheckbox = $(Drupal.theme('checkbox')).attr(
  49. 'id',
  50. `${$cell.attr('id')}-group-select-all`,
  51. );
  52. const $testCheckboxes = $group
  53. .nextUntil('.simpletest-group')
  54. .find('input[type=checkbox]');
  55. $cell.append($groupCheckbox);
  56. // Toggle the test checkboxes when the group checkbox is toggled.
  57. $groupCheckbox.on('change', function() {
  58. const checked = $(this).prop('checked');
  59. $testCheckboxes.prop('checked', checked);
  60. });
  61. // Update the group checkbox when a test checkbox is toggled.
  62. function updateGroupCheckbox() {
  63. let allChecked = true;
  64. $testCheckboxes.each(function() {
  65. if (!$(this).prop('checked')) {
  66. allChecked = false;
  67. return false;
  68. }
  69. });
  70. $groupCheckbox.prop('checked', allChecked);
  71. }
  72. $testCheckboxes.on('change', updateGroupCheckbox);
  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(context) {
  90. const $input = $('input.table-filter-text').once('table-filter-text');
  91. const $table = $($input.attr('data-table'));
  92. let $rows;
  93. let searched = false;
  94. function filterTestList(e) {
  95. const query = $(e.target)
  96. .val()
  97. .toLowerCase();
  98. function showTestRow(index, row) {
  99. const $row = $(row);
  100. const $sources = $row.find('.table-filter-text-source');
  101. const textMatch =
  102. $sources
  103. .text()
  104. .toLowerCase()
  105. .indexOf(query) !== -1;
  106. $row.closest('tr').toggle(textMatch);
  107. }
  108. // Filter if the length of the query is at least 3 characters.
  109. if (query.length >= 3) {
  110. // Indicate that a search has been performed, and hide the
  111. // "select all" checkbox.
  112. searched = true;
  113. $('#simpletest-form-table thead th.select-all input').hide();
  114. $rows.each(showTestRow);
  115. }
  116. // Restore to the original state if any searching has occurred.
  117. else if (searched) {
  118. searched = false;
  119. $('#simpletest-form-table thead th.select-all input').show();
  120. // Restore all rows to their original display state.
  121. $rows.css('display', '');
  122. }
  123. }
  124. if ($table.length) {
  125. $rows = $table.find('tbody tr');
  126. $input
  127. .trigger('focus')
  128. .on('keyup', Drupal.debounce(filterTestList, 200));
  129. }
  130. },
  131. };
  132. })(jQuery, Drupal, drupalSettings);