1
// $Id: tableselect.js,v 1.1 2006/11/21 08:16:39 unconed Exp $
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;
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');
15
// Update the title and the state of the check all box.
16
selectAll.attr('title', selectAll[0].checked ? settings.selectNone : settings.selectAll);
19
// Find all <th> with class select-all, and insert the check all checkbox.
20
$('th.select-all', table).prepend(selectAll);
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');
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);
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);
40
// Keep track of the last checked checkbox.
41
lastChecked = e.target;
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';
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;
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() {
61
// If we are at the end of the range, stop.
64
// A faster alternative to doing $(i).filter(to).length.
65
else if (jQuery.filter(to, [i]).r.length) break;
71
if (Drupal.jsEnabled) {
72
$(document).ready(function() {
73
$('form table[th.select-all]').each(Drupal.tableSelect);