/[drupal]/drupal/misc/tableselect.js
ViewVC logotype

Contents of /drupal/misc/tableselect.js

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.13 - (show annotations) (download) (as text)
Mon Aug 31 05:51:08 2009 UTC (2 months, 3 weeks ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, DRUPAL-7-0-UNSTABLE-9, HEAD
Changes since 1.12: +2 -3 lines
File MIME type: text/javascript
- Patch #444344 by kkaefer, sun, Rob Loach: this change introduces a jQuery .once() method which streamlines the way behavior functions work. Previously, we had to manually ensure that an element is only initialized once. Usually, this happens by adding classes and selecting only those elements which do not have that class. However, this process can be separated out into a jQuery ‘filtering’ function which does all the grunt work.
1 // $Id: tableselect.js,v 1.12 2009/04/27 20:19:35 webchick Exp $
2 (function ($) {
3
4 Drupal.behaviors.tableSelect = {
5 attach: function (context, settings) {
6 $('form table:has(th.select-all)', context).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).size() == 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 $('th.select-all input:checkbox', table).each(function () {
21 $(this).attr('title', state ? strings.selectNone : strings.selectAll);
22 this.checked = state;
23 });
24 };
25
26 // Find all <th> with class select-all, and insert the check all checkbox.
27 $('th.select-all', table).prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings.selectAll)).click(function (event) {
28 if ($(event.target).is('input:checkbox')) {
29 // Loop through all checkboxes and set their state to the select all checkbox' state.
30 checkboxes.each(function () {
31 this.checked = event.target.checked;
32 // Either add or remove the selected class based on the state of the check all checkbox.
33 $(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected');
34 });
35 // Update the title and the state of the check all box.
36 updateSelectAll(event.target.checked);
37 }
38 });
39
40 // For each of the checkboxes within the table.
41 checkboxes = $('td input:checkbox', table).click(function (e) {
42 // Either add or remove the selected class based on the state of the check all checkbox.
43 $(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected');
44
45 // If this is a shift click, we need to highlight everything in the range.
46 // Also make sure that we are actually checking checkboxes over a range and
47 // that a checkbox has been checked or unchecked before.
48 if (e.shiftKey && lastChecked && lastChecked != e.target) {
49 // We use the checkbox's parent TR to do our range searching.
50 Drupal.tableSelectRange($(e.target).parents('tr')[0], $(lastChecked).parents('tr')[0], e.target.checked);
51 }
52
53 // If all checkboxes are checked, make sure the select-all one is checked too, otherwise keep unchecked.
54 updateSelectAll((checkboxes.length == $(checkboxes).filter(':checked').length));
55
56 // Keep track of the last checked checkbox.
57 lastChecked = e.target;
58 });
59 };
60
61 Drupal.tableSelectRange = function (from, to, state) {
62 // We determine the looping mode based on the the order of from and to.
63 var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
64
65 // Traverse through the sibling nodes.
66 for (var i = from[mode]; i; i = i[mode]) {
67 // Make sure that we're only dealing with elements.
68 if (i.nodeType != 1) {
69 continue;
70 }
71
72 // Either add or remove the selected class based on the state of the target checkbox.
73 $(i)[ state ? 'addClass' : 'removeClass' ]('selected');
74 $('input:checkbox', i).each(function () {
75 this.checked = state;
76 });
77
78 if (to.nodeType) {
79 // If we are at the end of the range, stop.
80 if (i == to) {
81 break;
82 }
83 }
84 // A faster alternative to doing $(i).filter(to).length.
85 else if ($.filter(to, [i]).r.length) {
86 break;
87 }
88 }
89 };
90
91 })(jQuery);

  ViewVC Help
Powered by ViewVC 1.1.2