| 1 |
// $Id: subscriptions_tableselect.js,v 1.4 2009/04/13 15:26:19 salvis Exp $
|
| 2 |
|
| 3 |
Drupal.subscriptions_rowToggle = function(ckb) {
|
| 4 |
var thisRow = $(ckb).parents('tr:first');
|
| 5 |
var controls = $('input, select', thisRow);
|
| 6 |
for (var i = 1; i < controls.length; i++) {
|
| 7 |
controls[i].style['visibility'] = (ckb.checked ? 'visible' : 'hidden');
|
| 8 |
}
|
| 9 |
}
|
| 10 |
|
| 11 |
Drupal.behaviors.subscriptions_rowSelect = function (context) {
|
| 12 |
$('form table:has(th.subscriptions-table):not(.subscriptions_rowSelect-processed)', context)
|
| 13 |
.addClass('subscriptions_rowSelect-processed').each(Drupal.subscriptions_rowSelect);
|
| 14 |
};
|
| 15 |
|
| 16 |
Drupal.subscriptions_rowSelect = function() {
|
| 17 |
// Dynamically hide/show the other columns depending on the checkbox state in the first column.
|
| 18 |
var rows = $('tr', this);
|
| 19 |
for (var i = 1; i < rows.length; i++) {
|
| 20 |
var row = rows[i];
|
| 21 |
var input = $('td:first input:checkbox', row);
|
| 22 |
input.click(function(e) {
|
| 23 |
Drupal.subscriptions_rowToggle(this);
|
| 24 |
});
|
| 25 |
|
| 26 |
Drupal.subscriptions_rowToggle(input[0]);
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
Drupal.behaviors.subscriptions_tableSelect = function (context) {
|
| 31 |
$('form table:has(th.select-all):not(.tableSelect-processed)', context).each(Drupal.subscriptions_tableSelect);
|
| 32 |
};
|
| 33 |
|
| 34 |
Drupal.subscriptions_tableSelect = function() {
|
| 35 |
// Do not add a "Select all" checkbox if there are no rows with checkboxes in the table
|
| 36 |
if ($('td input:checkbox.select-row', this).size() == 0) {
|
| 37 |
return;
|
| 38 |
}
|
| 39 |
|
| 40 |
// Keep track of the table, which checkbox is checked and alias the settings.
|
| 41 |
var table = this, checkboxes, lastChecked;
|
| 42 |
var strings = { 'selectAll': Drupal.t('Turn all subscriptions on.'), 'selectNone': Drupal.t('Turn all subscriptions off.') };
|
| 43 |
var updateSelectAll = function(state) {
|
| 44 |
$('th.select-all input:checkbox', table).each(function() {
|
| 45 |
$(this).attr('title', state ? strings.selectNone : strings.selectAll);
|
| 46 |
this.checked = state;
|
| 47 |
});
|
| 48 |
};
|
| 49 |
|
| 50 |
// Find all <th> with class select-all, and insert the check all checkbox.
|
| 51 |
$('th.select-all', table).prepend($('<input type="checkbox" class="form-checkbox"/>').attr('title', strings.selectAll)).click(function(event) {
|
| 52 |
if ($(event.target).is('input:checkbox')) {
|
| 53 |
// Loop through all checkboxes and set their state to the select all checkbox' state.
|
| 54 |
checkboxes.each(function() {
|
| 55 |
this.checked = event.target.checked;
|
| 56 |
// Show/hide the dependent columns of this row.
|
| 57 |
Drupal.subscriptions_rowToggle(this);
|
| 58 |
});
|
| 59 |
// Update the title and the state of the check all box.
|
| 60 |
updateSelectAll(event.target.checked);
|
| 61 |
}
|
| 62 |
});
|
| 63 |
|
| 64 |
// For each of the checkboxes within the table.
|
| 65 |
checkboxes = $('td input:checkbox.select-row', table).click(function(e) {
|
| 66 |
// If this is a shift click, we need to highlight everything in the range.
|
| 67 |
// Also make sure that we are actually checking checkboxes over a range and
|
| 68 |
// that a checkbox has been checked or unchecked before.
|
| 69 |
if (e.shiftKey && lastChecked && lastChecked != e.target) {
|
| 70 |
// We use the checkbox's parent TR to do our range searching.
|
| 71 |
Drupal.subscriptions_tableSelectRange($(e.target).parents('tr')[0], $(lastChecked).parents('tr')[0], e.target.checked);
|
| 72 |
}
|
| 73 |
|
| 74 |
// If all checkboxes are checked, make sure the select-all one is checked too, otherwise keep unchecked.
|
| 75 |
updateSelectAll((checkboxes.length == $(checkboxes).filter(':checked').length));
|
| 76 |
|
| 77 |
// Keep track of the last checked checkbox.
|
| 78 |
lastChecked = e.target;
|
| 79 |
});
|
| 80 |
// If all checkboxes are checked, make sure the select-all one is checked too, otherwise keep unchecked.
|
| 81 |
updateSelectAll((checkboxes.length == $(checkboxes).filter(':checked').length));
|
| 82 |
$(this).addClass('tableSelect-processed');
|
| 83 |
};
|
| 84 |
|
| 85 |
Drupal.subscriptions_tableSelectRange = function(from, to, state) {
|
| 86 |
// We determine the looping mode based on the the order of from and to.
|
| 87 |
var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
|
| 88 |
|
| 89 |
// Traverse through the sibling nodes.
|
| 90 |
for (var i = from[mode]; i; i = i[mode]) {
|
| 91 |
// Make sure that we're only dealing with elements.
|
| 92 |
if (i.nodeType != 1) {
|
| 93 |
continue;
|
| 94 |
}
|
| 95 |
|
| 96 |
$('input:checkbox.select-row', i).each(function() {
|
| 97 |
this.checked = state;
|
| 98 |
// Show/hide the dependent columns of this row.
|
| 99 |
Drupal.subscriptions_rowToggle(this);
|
| 100 |
});
|
| 101 |
|
| 102 |
if (to.nodeType) {
|
| 103 |
// If we are at the end of the range, stop.
|
| 104 |
if (i == to) {
|
| 105 |
break;
|
| 106 |
}
|
| 107 |
}
|
| 108 |
// A faster alternative to doing $(i).filter(to).length.
|
| 109 |
else if (jQuery.filter(to, [i]).r.length) {
|
| 110 |
break;
|
| 111 |
}
|
| 112 |
}
|
| 113 |
};
|