webmaster@1: // $Id: tableselect.js,v 1.8 2007/11/19 12:15:16 goba Exp $ webmaster@1: webmaster@1: Drupal.behaviors.tableSelect = function (context) { webmaster@1: $('form table:has(th.select-all):not(.tableSelect-processed)', context).each(Drupal.tableSelect); webmaster@1: }; webmaster@1: webmaster@1: Drupal.tableSelect = function() { webmaster@1: // Do not add a "Select all" checkbox if there are no rows with checkboxes in the table webmaster@1: if ($('td input:checkbox', this).size() == 0) { webmaster@1: return; webmaster@1: } webmaster@1: webmaster@1: // Keep track of the table, which checkbox is checked and alias the settings. webmaster@1: var table = this, checkboxes, lastChecked; webmaster@1: var strings = { 'selectAll': Drupal.t('Select all rows in this table'), 'selectNone': Drupal.t('Deselect all rows in this table') }; webmaster@1: var updateSelectAll = function(state) { webmaster@1: $('th.select-all input:checkbox', table).each(function() { webmaster@1: $(this).attr('title', state ? strings.selectNone : strings.selectAll); webmaster@1: this.checked = state; webmaster@1: }); webmaster@1: }; webmaster@1: webmaster@1: // Find all with class select-all, and insert the check all checkbox. webmaster@1: $('th.select-all', table).prepend($('').attr('title', strings.selectAll)).click(function(event) { webmaster@1: if ($(event.target).is('input:checkbox')) { webmaster@1: // Loop through all checkboxes and set their state to the select all checkbox' state. webmaster@1: checkboxes.each(function() { webmaster@1: this.checked = event.target.checked; webmaster@1: // Either add or remove the selected class based on the state of the check all checkbox. webmaster@1: $(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected'); webmaster@1: }); webmaster@1: // Update the title and the state of the check all box. webmaster@1: updateSelectAll(event.target.checked); webmaster@1: } webmaster@1: }); webmaster@1: webmaster@1: // For each of the checkboxes within the table. webmaster@1: checkboxes = $('td input:checkbox', table).click(function(e) { webmaster@1: // Either add or remove the selected class based on the state of the check all checkbox. webmaster@1: $(this).parents('tr:first')[ this.checked ? 'addClass' : 'removeClass' ]('selected'); webmaster@1: webmaster@1: // If this is a shift click, we need to highlight everything in the range. webmaster@1: // Also make sure that we are actually checking checkboxes over a range and webmaster@1: // that a checkbox has been checked or unchecked before. webmaster@1: if (e.shiftKey && lastChecked && lastChecked != e.target) { webmaster@1: // We use the checkbox's parent TR to do our range searching. webmaster@1: Drupal.tableSelectRange($(e.target).parents('tr')[0], $(lastChecked).parents('tr')[0], e.target.checked); webmaster@1: } webmaster@1: webmaster@1: // If all checkboxes are checked, make sure the select-all one is checked too, otherwise keep unchecked. webmaster@1: updateSelectAll((checkboxes.length == $(checkboxes).filter(':checked').length)); webmaster@1: webmaster@1: // Keep track of the last checked checkbox. webmaster@1: lastChecked = e.target; webmaster@1: }); webmaster@1: $(this).addClass('tableSelect-processed'); webmaster@1: }; webmaster@1: webmaster@1: Drupal.tableSelectRange = function(from, to, state) { webmaster@1: // We determine the looping mode based on the the order of from and to. webmaster@1: var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling'; webmaster@1: webmaster@1: // Traverse through the sibling nodes. webmaster@1: for (var i = from[mode]; i; i = i[mode]) { webmaster@1: // Make sure that we're only dealing with elements. webmaster@1: if (i.nodeType != 1) { webmaster@1: continue; webmaster@1: } webmaster@1: webmaster@1: // Either add or remove the selected class based on the state of the target checkbox. webmaster@1: $(i)[ state ? 'addClass' : 'removeClass' ]('selected'); webmaster@1: $('input:checkbox', i).each(function() { webmaster@1: this.checked = state; webmaster@1: }); webmaster@1: webmaster@1: if (to.nodeType) { webmaster@1: // If we are at the end of the range, stop. webmaster@1: if (i == to) { webmaster@1: break; webmaster@1: } webmaster@1: } webmaster@1: // A faster alternative to doing $(i).filter(to).length. webmaster@1: else if (jQuery.filter(to, [i]).r.length) { webmaster@1: break; webmaster@1: } webmaster@1: } webmaster@1: };