summaryrefslogtreecommitdiff
path: root/modules/simpletest/simpletest.js
blob: a7af10d1b4c2dfe649ed0772b4ee375c33ee2565 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// $Id$

/**
 * Add the cool table collapsing on the testing overview page.
 */
Drupal.behaviors.simpleTestMenuCollapse = {
  attach: function() {
    var timeout = null;
    // Adds expand-collapse functionality.
    $('div.simpletest-image').each(function() {
      direction = Drupal.settings.simpleTest[$(this).attr('id')].imageDirection;
      $(this).html(Drupal.settings.simpleTest.images[direction]);
    });

    // Adds group toggling functionality to arrow images.
    $('div.simpletest-image').click(function() {
      var trs = $(this).parents('tbody').children('.' + Drupal.settings.simpleTest[this.id].testClass);
      var direction = Drupal.settings.simpleTest[this.id].imageDirection;
      var row = direction ? trs.size() - 1 : 0;

      // If clicked in the middle of expanding a group, stop so we can switch directions.
      if (timeout) {
        clearTimeout(timeout);
      }

      // Function to toggle an individual row according to the current direction.
      // We set a timeout of 20 ms until the next row will be shown/hidden to
      // create a sliding effect.
      function rowToggle() {
        if (direction) {
          if (row >= 0) {
            $(trs[row]).hide();
            row--;
            timeout = setTimeout(rowToggle, 20);
          }
        }
        else {
          if (row < trs.size()) {
            $(trs[row]).removeClass('js-hide').show();
            row++;
            timeout = setTimeout(rowToggle, 20);
          }
        }
      }

      // Kick-off the toggling upon a new click.
      rowToggle();

      // Toggle the arrow image next to the test group title.
      $(this).html(Drupal.settings.simpleTest.images[(direction ? 0 : 1)]);
      Drupal.settings.simpleTest[this.id].imageDirection = !direction;

    });
  }
};

/**
 * Select/deselect all the inner checkboxes when the outer checkboxes are
 * selected/deselected.
 */
Drupal.behaviors.simpleTestSelectAll = {
  attach: function() {
    $('td.simpletest-select-all').each(function() {
      var checkboxes = Drupal.settings.simpleTest['simpletest-test-group-'+ $(this).attr('id')].testNames, totalCheckboxes = 0,
        checkbox = $('<input type="checkbox" class="form-checkbox" id="'+ $(this).attr('id') +'-select-all" />').change(function() {
        var checked = !!($(this).attr('checked'));
        for (var i = 0; i < checkboxes.length; i++) {
          $('#'+ checkboxes[i]).attr('checked', checked);
        }
        self.data('simpletest-checked-tests', (checked ? checkboxes.length : 0));
      }).data('simpletest-checked-tests', 0);
      var self = $(this);
      for (var i = 0; i < checkboxes.length; i++) {
        if ($('#' + checkboxes[i]).change(function() {
          if (checkbox.attr('checked') == 'checked') {
            checkbox.attr('checked', '');
          }
          var data = (!self.data('simpletest-checked-tests') ? 0 : self.data('simpletest-checked-tests')) + (!!($(this).attr('checked')) ? 1 : -1);
          self.data('simpletest-checked-tests', data);
          if (data == checkboxes.length) {
            checkbox.attr('checked', 'checked');
          }
          else {
            checkbox.removeAttr('checked');
          }
        }).attr('checked') == 'checked') {
          totalCheckboxes++;
        }
      }
      if (totalCheckboxes == checkboxes.length) {
        $(checkbox).attr('checked', 'checked');
      }
      $(this).append(checkbox);
    });
  }
};