commit 78a20879e1d07ea890d46bead2683edfd1140c1a Author: Kiphaas7 Date: Tue Aug 28 21:03:31 2012 +0800 Cleanup function coding, fix item not getting removed from cache. diff --git a/core/misc/tableselect.js b/core/misc/tableselect.js index 3dab15f..9ea4e52 100644 --- a/core/misc/tableselect.js +++ b/core/misc/tableselect.js @@ -4,39 +4,18 @@ Drupal.behaviors.TableSelect = { attach: function (context) { - var $selectAllHeaders = $(context).find('th.select-all'); - tableSelectInitHandler($selectAllHeaders); + tableSelectInitHandler(context); }, detach: function (context) { - var tables = TableSelect.tables; - var $context = $(context); - - // Go through all the TabeSelect instances. - for (var i = 0, il = tables.length; i < il; i++) { - var $selectAll = $context.find(tables[i].$selectAll); - - // Only remove if 'select all' was found in the given context. - if ($selectAll.length) { - var $table = tables[i].$table; - // Unbind all events which are not directly bound to the 'select all' - // checkbox. - $table.off('.TableSelect'); - $table.parent().off('.TableSelect'); - - // Remove the 'select all' checkbox. This will also remove any events - // which are directly bound to the 'select all' checkbox. - $selectAll.remove(); - - // Trigger an event letting other scripts know TableSelect was removed. - $table.trigger('TableSelectRemoved'); - } - } + tableSelectRemoveHander(context); } }; -function tableSelectInitHandler($selectAllHeaders) { - $selectAllHeaders.each(function() { +function tableSelectInitHandler (context) { + var $selectAllHeaders = $(context).find('th.select-all'); + + $selectAllHeaders.each(function () { var $header = $(this); var $table = $header.closest('table'); @@ -49,6 +28,35 @@ function tableSelectInitHandler($selectAllHeaders) { }); } +function tableSelectRemoveHander (context) { + var tables = TableSelect.tables; + var $context = $(context); + + // Go through all the TabeSelect instances. + for (var i = 0, il = tables.length; i < il; i++) { + var $selectAll = $context.find(tables[i].$selectAll); + + // Only remove if 'select all' was found in the given context. + if ($selectAll.length) { + var $table = tables[i].$table; + // Unbind all events which are not directly bound to the 'select all' + // checkbox. + $table.off('.TableSelect'); + $table.parent().off('.TableSelect'); + + // Remove the 'select all' checkbox. This will also remove any events + // which are directly bound to the 'select all' checkbox. + $selectAll.remove(); + + // Remove this entry from the cache. + tables.splice(i, 1); + + // Trigger an event letting other scripts know TableSelect was removed. + $table.trigger('TableSelectRemoved'); + } + } +} + /** * Constructor for the TableSelect object. * @@ -61,23 +69,23 @@ function tableSelectInitHandler($selectAllHeaders) { * * @constructor */ -function TableSelect($table, $header) { +function TableSelect ($table, $header) { // Cached jQuery selectors. this.$table = $table; this.$header = $header; this.$selectAll = null; // DOM collections. - this.checkboxes = null; - this.rows = null; + this.checkboxes = []; + this.rows = []; // Keep track of 'select all' state. this.selectAllState = null; // Keep track of checkbox properties. - this.checkboxChecked = null; - this.checkboxMaxChecked = null; - this.lastState = null; + this.checkboxChecked = 0; + this.checkboxMaxChecked = 0; + this.lastState = []; this.lastIndex = null; // Create 'select all' checkbox and behaviour. @@ -106,12 +114,6 @@ $.extend(TableSelect.prototype, { }, createSelectAll: function () { - this.checkboxes = []; - this.rows = []; - this.lastState = []; - this.checkboxChecked = 0; - this.checkboxMaxChecked = 0; - // Store the checkboxes, initial state and their parent rows. var $checkboxes = this.$table.find('td input:checkbox'); for (var i = 0, il = $checkboxes.length; i < il; i++) {