diff options
author | Steven Wittens <steven@10.no-reply.drupal.org> | 2006-11-21 08:16:39 +0000 |
---|---|---|
committer | Steven Wittens <steven@10.no-reply.drupal.org> | 2006-11-21 08:16:39 +0000 |
commit | 001d54f663136894ce0dce0a09693cc107cfb788 (patch) | |
tree | 745c0a9479855e27b331cf5b7a03ddebd2f8dd29 /misc | |
parent | 50641eafc0bfd321e09f71ab361dcf5c26469878 (diff) | |
download | brdo-001d54f663136894ce0dce0a09693cc107cfb788.tar.gz brdo-001d54f663136894ce0dce0a09693cc107cfb788.tar.bz2 |
#84961: Add 'select all' and range select feature to admin tables.
Diffstat (limited to 'misc')
-rw-r--r-- | misc/tableselect.js | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/misc/tableselect.js b/misc/tableselect.js new file mode 100644 index 000000000..052af02dd --- /dev/null +++ b/misc/tableselect.js @@ -0,0 +1,75 @@ +// $Id$ + +Drupal.tableSelect = function() { + // Keep track of the table, which checkbox is checked and alias the settings. + var table = this, selectAll, checkboxes, lastChecked, settings = Drupal.settings.tableSelect; + + // Store the select all checkbox in a variable as we need it quite often. + selectAll = $('<input type="checkbox" class="form-checkbox" />').attr('title', settings.selectAll).click(function() { + // Loop through all checkboxes and set their state to the select all checkbox' state. + checkboxes.each(function() { + this.checked = selectAll[0].checked; + // Either add or remove the selected class based on the state of the check all checkbox. + $(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected'); + }); + // Update the title and the state of the check all box. + selectAll.attr('title', selectAll[0].checked ? settings.selectNone : settings.selectAll); + }); + + // Find all <th> with class select-all, and insert the check all checkbox. + $('th.select-all', table).prepend(selectAll); + + // For each of the checkboxes within the table. + checkboxes = $('td input:checkbox', table).click(function(e) { + // Either add or remove the selected class based on the state of the check all checkbox. + $(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected'); + + // If this is a shift click, we need to highlight everything in the range. + // Also make sure that we are actually checking checkboxes over a range and + // that a checkbox has been checked or unchecked before. + if (e.shiftKey && lastChecked && lastChecked != e.target) { + // We use the checkbox's parent TR to do our range searching. + Drupal.tableSelectRange($(e.target).parents('tr')[0], $(lastChecked).parents('tr')[0], e.target.checked); + } + + // If all checkboxes are checked, make sure the select-all one is checked too, otherwise keep unchecked. + selectAll[0].checked = (checkboxes.length == $(checkboxes).filter(':checked').length); + // Set the title to the current action. + selectAll.attr('title', selectAll[0].checked ? settings.selectNone : settings.selectAll); + + // Keep track of the last checked checkbox. + lastChecked = e.target; + }); +} + +Drupal.tableSelectRange = function(from, to, state) { + // We determine the looping mode based on the the order of from and to. + var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling'; + + // Traverse through the sibling nodes. + for (var i = from[mode]; i; i = i[mode]) { + // Make sure that we're only dealing with elements. + if (i.nodeType != 1) continue; + + // Either add or remove the selected class based on the state of the target checkbox. + $(i)[ state ? 'addClass' : 'removeClass' ]('selected'); + $('input:checkbox', i).each(function() { + this.checked = state; + }); + + if (to.nodeType) { + // If we are at the end of the range, stop. + if (i == to) break; + } + // A faster alternative to doing $(i).filter(to).length. + else if (jQuery.filter(to, [i]).r.length) break; + + } +} + +// Global Killswitch +if (Drupal.jsEnabled) { + $(document).ready(function() { + $('form table[th.select-all]').each(Drupal.tableSelect); + }); +} |