diff --git a/core/misc/tableheader.js b/core/misc/tableheader.js index 364cad2..c5cb1ac 100644 --- a/core/misc/tableheader.js +++ b/core/misc/tableheader.js @@ -174,8 +174,9 @@ $.extend(TableHeader.prototype, { * Create the duplicate header. */ createSticky: function () { - // Clone the table header so it inherits original jQuery properties. - var $stickyHeader = this.$originalHeader.clone(true); + // Deep clone the table header so it and its children inherit their original + // jQuery properties. + var $stickyHeader = this.$originalHeader.clone(true, true); // Hide the table to avoid a flash of the header clone upon page load. this.$stickyTable = $('') .css({ @@ -190,6 +191,9 @@ $.extend(TableHeader.prototype, { // Initialize all computations. this.recalculateSticky(); + + // Let other scripts know a sticky table header was created. + this.$stickyTable.trigger('stickyTableCreated'); }, /** diff --git a/core/misc/tableselect.js b/core/misc/tableselect.js index 36af864..3dab15f 100644 --- a/core/misc/tableselect.js +++ b/core/misc/tableselect.js @@ -1,93 +1,258 @@ -(function ($) { +(function ($, Drupal) { "use strict"; -Drupal.behaviors.tableSelect = { - attach: function (context, settings) { - // Select the inner-most table in case of nested tables. - $(context).find('th.select-all').closest('table').once('table-select', Drupal.tableSelect); +Drupal.behaviors.TableSelect = { + attach: function (context) { + var $selectAllHeaders = $(context).find('th.select-all'); + tableSelectInitHandler($selectAllHeaders); + }, + + 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'); + } + } } }; -Drupal.tableSelect = function () { - // Do not add a "Select all" checkbox if there are no rows with checkboxes in the table - if ($(this).find('td input:checkbox').length === 0) { - return; - } +function tableSelectInitHandler($selectAllHeaders) { + $selectAllHeaders.each(function() { + var $header = $(this); + var $table = $header.closest('table'); - // Keep track of the table, which checkbox is checked and alias the settings. - var table = this, checkboxes, lastChecked; - var $table = $(table); - var strings = { 'selectAll': Drupal.t('Select all rows in this table'), 'selectNone': Drupal.t('Deselect all rows in this table') }; - var updateSelectAll = function (state) { - // Update table's select-all checkbox (and sticky header's if available). - $table.prev('table.sticky-header').andSelf().find('th.select-all input:checkbox').each(function() { - $(this).attr('title', state ? strings.selectNone : strings.selectAll); - this.checked = state; - }); - }; - - // Find all