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