simpletest.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @file
  3. * Simpletest behaviors.
  4. */
  5. (function ($, Drupal, drupalSettings) {
  6. 'use strict';
  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: function (context) {
  17. $(context).find('.simpletest-group').once('simpletest-group-collapse').each(function () {
  18. var $group = $(this);
  19. var $image = $group.find('.simpletest-image');
  20. $image
  21. .html(drupalSettings.simpleTest.images[0])
  22. .on('click', function () {
  23. var $tests = $group.nextUntil('.simpletest-group');
  24. var 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: function (context) {
  42. $(context).find('.simpletest-group').once('simpletest-group-select-all').each(function () {
  43. var $group = $(this);
  44. var $cell = $group.find('.simpletest-group-select-all');
  45. var $groupCheckbox = $('<input type="checkbox" id="' + $cell.attr('id') + '-group-select-all" class="form-checkbox" />');
  46. var $testCheckboxes = $group.nextUntil('.simpletest-group').find('input[type=checkbox]');
  47. $cell.append($groupCheckbox);
  48. // Toggle the test checkboxes when the group checkbox is toggled.
  49. $groupCheckbox.on('change', function () {
  50. var checked = $(this).prop('checked');
  51. $testCheckboxes.prop('checked', checked);
  52. });
  53. // Update the group checkbox when a test checkbox is toggled.
  54. function updateGroupCheckbox() {
  55. var allChecked = true;
  56. $testCheckboxes.each(function () {
  57. if (!$(this).prop('checked')) {
  58. allChecked = false;
  59. return false;
  60. }
  61. });
  62. $groupCheckbox.prop('checked', allChecked);
  63. }
  64. $testCheckboxes.on('change', updateGroupCheckbox);
  65. });
  66. }
  67. };
  68. /**
  69. * Filters the test list table by a text input search string.
  70. *
  71. * Text search input: input.table-filter-text
  72. * Target table: input.table-filter-text[data-table]
  73. * Source text: .table-filter-text-source
  74. *
  75. * @type {Drupal~behavior}
  76. *
  77. * @prop {Drupal~behaviorAttach} attach
  78. * Attaches the filter behavior to the text input element.
  79. */
  80. Drupal.behaviors.simpletestTableFilterByText = {
  81. attach: function (context) {
  82. var $input = $('input.table-filter-text').once('table-filter-text');
  83. var $table = $($input.attr('data-table'));
  84. var $rows;
  85. var searched = false;
  86. function filterTestList(e) {
  87. var query = $(e.target).val().toLowerCase();
  88. function showTestRow(index, row) {
  89. var $row = $(row);
  90. var $sources = $row.find('.table-filter-text-source');
  91. var textMatch = $sources.text().toLowerCase().indexOf(query) !== -1;
  92. $row.closest('tr').toggle(textMatch);
  93. }
  94. // Filter if the length of the query is at least 3 characters.
  95. if (query.length >= 3) {
  96. // Indicate that a search has been performed, and hide the
  97. // "select all" checkbox.
  98. searched = true;
  99. $('#simpletest-form-table thead th.select-all input').hide();
  100. $rows.each(showTestRow);
  101. }
  102. // Restore to the original state if any searching has occurred.
  103. else if (searched) {
  104. searched = false;
  105. $('#simpletest-form-table thead th.select-all input').show();
  106. // Restore all rows to their original display state.
  107. $rows.css('display', '');
  108. }
  109. }
  110. if ($table.length) {
  111. $rows = $table.find('tbody tr');
  112. $input.trigger('focus').on('keyup', Drupal.debounce(filterTestList, 200));
  113. }
  114. }
  115. };
  116. })(jQuery, Drupal, drupalSettings);