Projet

Général

Profil

Paste
Télécharger (3,64 ko) Statistiques
| Branche: | Révision:

root / drupal7 / misc / tableselect.js @ 76597ebf

1
(function ($) {
2

    
3
Drupal.behaviors.tableSelect = {
4
  attach: function (context, settings) {
5
    // Select the inner-most table in case of nested tables.
6
    $('th.select-all', context).closest('table').once('table-select', Drupal.tableSelect);
7
  }
8
};
9

    
10
Drupal.tableSelect = function () {
11
  // Do not add a "Select all" checkbox if there are no rows with checkboxes in the table
12
  if ($('td input:checkbox', this).length == 0) {
13
    return;
14
  }
15

    
16
  // Keep track of the table, which checkbox is checked and alias the settings.
17
  var table = this, checkboxes, lastChecked;
18
  var strings = { 'selectAll': Drupal.t('Select all rows in this table'), 'selectNone': Drupal.t('Deselect all rows in this table') };
19
  var updateSelectAll = function (state) {
20
    // Update table's select-all checkbox (and sticky header's if available).
21
    $(table).prev('table.sticky-header').andSelf().find('th.select-all input:checkbox').each(function() {
22
      $(this).attr('title', state ? strings.selectNone : strings.selectAll);
23
      this.checked = state;
24
    });
25
  };
26

    
27
  // Find all <th> with class select-all, and insert the check all checkbox.
28
  $('th.select-all', table).prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings.selectAll)).click(function (event) {
29
    if ($(event.target).is('input:checkbox')) {
30
      // Loop through all checkboxes and set their state to the select all checkbox' state.
31
      checkboxes.each(function () {
32
        this.checked = event.target.checked;
33
        // Either add or remove the selected class based on the state of the check all checkbox.
34
        $(this).closest('tr').toggleClass('selected', this.checked);
35
      });
36
      // Update the title and the state of the check all box.
37
      updateSelectAll(event.target.checked);
38
    }
39
  });
40

    
41
  // For each of the checkboxes within the table that are not disabled.
42
  checkboxes = $('td input:checkbox:enabled', table).click(function (e) {
43
    // Either add or remove the selected class based on the state of the check all checkbox.
44
    $(this).closest('tr').toggleClass('selected', this.checked);
45

    
46
    // If this is a shift click, we need to highlight everything in the range.
47
    // Also make sure that we are actually checking checkboxes over a range and
48
    // that a checkbox has been checked or unchecked before.
49
    if (e.shiftKey && lastChecked && lastChecked != e.target) {
50
      // We use the checkbox's parent TR to do our range searching.
51
      Drupal.tableSelectRange($(e.target).closest('tr')[0], $(lastChecked).closest('tr')[0], e.target.checked);
52
    }
53

    
54
    // If all checkboxes are checked, make sure the select-all one is checked too, otherwise keep unchecked.
55
    updateSelectAll((checkboxes.length == $(checkboxes).filter(':checked').length));
56

    
57
    // Keep track of the last checked checkbox.
58
    lastChecked = e.target;
59
  });
60
};
61

    
62
Drupal.tableSelectRange = function (from, to, state) {
63
  // We determine the looping mode based on the the order of from and to.
64
  var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
65

    
66
  // Traverse through the sibling nodes.
67
  for (var i = from[mode]; i; i = i[mode]) {
68
    // Make sure that we're only dealing with elements.
69
    if (i.nodeType != 1) {
70
      continue;
71
    }
72

    
73
    // Either add or remove the selected class based on the state of the target checkbox.
74
    $(i).toggleClass('selected', state);
75
    $('input:checkbox', i).each(function () {
76
      this.checked = state;
77
    });
78

    
79
    if (to.nodeType) {
80
      // If we are at the end of the range, stop.
81
      if (i == to) {
82
        break;
83
      }
84
    }
85
    // A faster alternative to doing $(i).filter(to).length.
86
    else if ($.filter(to, [i]).r.length) {
87
      break;
88
    }
89
  }
90
};
91

    
92
})(jQuery);