~ubuntu-branches/ubuntu/oneiric/drupal5/oneiric

« back to all changes in this revision

Viewing changes to misc/tableselect.js

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2007-03-10 20:04:24 UTC
  • Revision ID: james.westby@ubuntu.com-20070310200424-w6v3crmyowlx2zsq
Tags: upstream-5.1
ImportĀ upstreamĀ versionĀ 5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id: tableselect.js,v 1.1 2006/11/21 08:16:39 unconed Exp $
 
2
 
 
3
Drupal.tableSelect = function() {
 
4
  // Keep track of the table, which checkbox is checked and alias the settings.
 
5
  var table = this, selectAll, checkboxes, lastChecked, settings = Drupal.settings.tableSelect;
 
6
 
 
7
  // Store the select all checkbox in a variable as we need it quite often.
 
8
  selectAll = $('<input type="checkbox" class="form-checkbox" />').attr('title', settings.selectAll).click(function() {
 
9
    // Loop through all checkboxes and set their state to the select all checkbox' state.
 
10
    checkboxes.each(function() {
 
11
      this.checked = selectAll[0].checked;
 
12
      // Either add or remove the selected class based on the state of the check all checkbox.
 
13
      $(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected');
 
14
    });
 
15
    // Update the title and the state of the check all box.
 
16
    selectAll.attr('title', selectAll[0].checked ? settings.selectNone : settings.selectAll);
 
17
  });
 
18
 
 
19
  // Find all <th> with class select-all, and insert the check all checkbox.
 
20
  $('th.select-all', table).prepend(selectAll);
 
21
 
 
22
  // For each of the checkboxes within the table.
 
23
  checkboxes = $('td input:checkbox', table).click(function(e) {
 
24
    // Either add or remove the selected class based on the state of the check all checkbox.
 
25
    $(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected');
 
26
 
 
27
    // If this is a shift click, we need to highlight everything in the range.
 
28
    // Also make sure that we are actually checking checkboxes over a range and
 
29
    // that a checkbox has been checked or unchecked before.
 
30
    if (e.shiftKey && lastChecked && lastChecked != e.target) {
 
31
      // We use the checkbox's parent TR to do our range searching.
 
32
      Drupal.tableSelectRange($(e.target).parents('tr')[0], $(lastChecked).parents('tr')[0], e.target.checked);
 
33
    }
 
34
 
 
35
    // If all checkboxes are checked, make sure the select-all one is checked too, otherwise keep unchecked.
 
36
    selectAll[0].checked = (checkboxes.length == $(checkboxes).filter(':checked').length);
 
37
    // Set the title to the current action.
 
38
    selectAll.attr('title', selectAll[0].checked ? settings.selectNone : settings.selectAll);
 
39
 
 
40
    // Keep track of the last checked checkbox.
 
41
    lastChecked = e.target;
 
42
  });
 
43
}
 
44
 
 
45
Drupal.tableSelectRange = function(from, to, state) {
 
46
  // We determine the looping mode based on the the order of from and to.
 
47
  var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
 
48
 
 
49
  // Traverse through the sibling nodes.
 
50
  for (var i = from[mode]; i; i = i[mode]) {
 
51
    // Make sure that we're only dealing with elements.
 
52
    if (i.nodeType != 1) continue;
 
53
 
 
54
    // Either add or remove the selected class based on the state of the target checkbox.
 
55
    $(i)[ state ? 'addClass' : 'removeClass' ]('selected');
 
56
    $('input:checkbox', i).each(function() {
 
57
      this.checked = state;
 
58
    });
 
59
 
 
60
    if (to.nodeType) {
 
61
      // If we are at the end of the range, stop.
 
62
      if (i == to) break;
 
63
    }
 
64
    // A faster alternative to doing $(i).filter(to).length.
 
65
    else if (jQuery.filter(to, [i]).r.length) break;
 
66
 
 
67
  }
 
68
}
 
69
 
 
70
// Global Killswitch
 
71
if (Drupal.jsEnabled) {
 
72
  $(document).ready(function() {
 
73
    $('form table[th.select-all]').each(Drupal.tableSelect);
 
74
  });
 
75
}