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