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