comparison misc/tableselect.js @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 // $Id: tableselect.js,v 1.8 2007/11/19 12:15:16 goba Exp $
2
3 Drupal.behaviors.tableSelect = function (context) {
4 $('form table:has(th.select-all):not(.tableSelect-processed)', context).each(Drupal.tableSelect);
5 };
6
7 Drupal.tableSelect = function() {
8 // Do not add a "Select all" checkbox if there are no rows with checkboxes in the table
9 if ($('td input:checkbox', this).size() == 0) {
10 return;
11 }
12
13 // Keep track of the table, which checkbox is checked and alias the settings.
14 var table = this, checkboxes, lastChecked;
15 var strings = { 'selectAll': Drupal.t('Select all rows in this table'), 'selectNone': Drupal.t('Deselect all rows in this table') };
16 var updateSelectAll = function(state) {
17 $('th.select-all input:checkbox', table).each(function() {
18 $(this).attr('title', state ? strings.selectNone : strings.selectAll);
19 this.checked = state;
20 });
21 };
22
23 // Find all <th> with class select-all, and insert the check all checkbox.
24 $('th.select-all', table).prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings.selectAll)).click(function(event) {
25 if ($(event.target).is('input:checkbox')) {
26 // Loop through all checkboxes and set their state to the select all checkbox' state.
27 checkboxes.each(function() {
28 this.checked = event.target.checked;
29 // Either add or remove the selected class based on the state of the check all checkbox.
30 $(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected');
31 });
32 // Update the title and the state of the check all box.
33 updateSelectAll(event.target.checked);
34 }
35 });
36
37 // For each of the checkboxes within the table.
38 checkboxes = $('td input:checkbox', table).click(function(e) {
39 // Either add or remove the selected class based on the state of the check all checkbox.
40 $(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected');
41
42 // If this is a shift click, we need to highlight everything in the range.
43 // Also make sure that we are actually checking checkboxes over a range and
44 // that a checkbox has been checked or unchecked before.
45 if (e.shiftKey && lastChecked && lastChecked != e.target) {
46 // We use the checkbox's parent TR to do our range searching.
47 Drupal.tableSelectRange($(e.target).parents('tr')[0], $(lastChecked).parents('tr')[0], e.target.checked);
48 }
49
50 // If all checkboxes are checked, make sure the select-all one is checked too, otherwise keep unchecked.
51 updateSelectAll((checkboxes.length == $(checkboxes).filter(':checked').length));
52
53 // Keep track of the last checked checkbox.
54 lastChecked = e.target;
55 });
56 $(this).addClass('tableSelect-processed');
57 };
58
59 Drupal.tableSelectRange = function(from, to, state) {
60 // We determine the looping mode based on the the order of from and to.
61 var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
62
63 // Traverse through the sibling nodes.
64 for (var i = from[mode]; i; i = i[mode]) {
65 // Make sure that we're only dealing with elements.
66 if (i.nodeType != 1) {
67 continue;
68 }
69
70 // Either add or remove the selected class based on the state of the target checkbox.
71 $(i)[ state ? 'addClass' : 'removeClass' ]('selected');
72 $('input:checkbox', i).each(function() {
73 this.checked = state;
74 });
75
76 if (to.nodeType) {
77 // If we are at the end of the range, stop.
78 if (i == to) {
79 break;
80 }
81 }
82 // A faster alternative to doing $(i).filter(to).length.
83 else if (jQuery.filter(to, [i]).r.length) {
84 break;
85 }
86 }
87 };