only in patch2: unchanged: --- a/core/misc/tabledrag.js +++ b/core/misc/tabledrag.js @@ -283,15 +283,13 @@ // Mark the column containing this field so it can be hidden. if (hidden && cell[0]) { - // Add 1 to our indexes. The nth-child selector is 1 based, not 0 - // based. Match immediate children of the parent element to allow - // nesting. - columnIndex = cell.parent().find('> td').index(cell.get(0)) + 1; + // Match immediate children of the parent element to allow nesting. + columnIndex = cell.parent().find('> td').index(cell.get(0)); $table.find('> thead > tr, > tbody > tr, > tr').each(this.addColspanClass(columnIndex)); } } } - this.displayColumns(showWeight); + this.displayColumns(showWeight, false); }; /** @@ -308,26 +306,16 @@ Drupal.tableDrag.prototype.addColspanClass = function (columnIndex) { return function () { // Get the columnIndex and adjust for any colspans in this row. - var $row = $(this); var index = columnIndex; - var cells = $row.children(); + var cells = $(this).children(); var cell; cells.each(function (n) { - if (n < index && this.colSpan && this.colSpan > 1) { + if (n < index && this.colSpan > 1) { index -= this.colSpan - 1; } }); - if (index > 0) { - cell = cells.filter(':nth-child(' + index + ')'); - if (cell[0].colSpan && cell[0].colSpan > 1) { - // If this cell has a colspan, mark it so we can reduce the colspan. - cell.addClass('tabledrag-has-colspan'); - } - else { - // Mark this cell so we can hide it. - cell.addClass('tabledrag-hide'); - } - } + cell = cells.eq(index); + cell.addClass('tabledrag-' + (cell.attr('colSpan') > 1 ? 'has-colspan' : 'hide')); }; }; @@ -338,14 +326,19 @@ * * @param {bool} displayWeight * 'true' will show weight columns. + * @param {Boolean} updateColSpans + * Whether "colspan" attributes should be recalculated. */ - Drupal.tableDrag.prototype.displayColumns = function (displayWeight) { + Drupal.tableDrag.prototype.displayColumns = function (displayWeight, updateColSpans) { + // Assumed that by default the attribute should be recalculated. + updateColSpans = undefined === updateColSpans ? true : updateColSpans; + if (displayWeight) { - this.showColumns(); + this.showColumns(updateColSpans); } // Default action is to hide columns. else { - this.hideColumns(); + this.hideColumns(updateColSpans); } // Trigger an event to allow other scripts to react to this display change. // Force the extra parameter as a bool. @@ -374,16 +367,26 @@ * Hide the columns containing weight/parent form elements. * * Undo showColumns(). + * + * @param {Boolean} updateColSpanAllTables + * Whether "colspan" attributes should be recalculated for all tables. */ - Drupal.tableDrag.prototype.hideColumns = function () { + Drupal.tableDrag.prototype.hideColumns = function (updateColSpanAllTables) { var $tables = $('table').findOnce('tabledrag'); // Hide weight/parent cells and headers. $tables.find('.tabledrag-hide').css('display', 'none'); // Show TableDrag handles. $tables.find('.tabledrag-handle').css('display', ''); - // Reduce the colspan of any effected multi-span columns. - $tables.find('.tabledrag-has-colspan').each(function () { - this.colSpan = this.colSpan - 1; + // Default action - is to hide the column, so the "colspan" must be + // subtracted by one. If we have multiple tabledrag-empowered tables + // on the page, then this code will be working as many times as we have + // tables on the page and "colspan" for last one will be calculated by + // formula: "(colspan - 1) * totalTablesCount". To avoid this behavior, + // update the "colspan" on initialization only for table that currently + // in processing. + (updateColSpanAllTables ? $tables : this.$table).find('.tabledrag-has-colspan').each(function () { + // Reduce the colspan of any effected multi-span columns. + this.colSpan -= 1; }); // Change link text. $('.tabledrag-toggle-weight').text(Drupal.t('Show row weights')); @@ -393,17 +396,24 @@ * Show the columns containing weight/parent form elements. * * Undo hideColumns(). + * + * @param {Boolean} updateColSpans + * Whether "colspan" attributes should be recalculated. */ - Drupal.tableDrag.prototype.showColumns = function () { + Drupal.tableDrag.prototype.showColumns = function (updateColSpans) { var $tables = $('table').findOnce('tabledrag'); // Show weight/parent cells and headers. $tables.find('.tabledrag-hide').css('display', ''); // Hide TableDrag handles. $tables.find('.tabledrag-handle').css('display', 'none'); - // Increase the colspan for any columns where it was previously reduced. - $tables.find('.tabledrag-has-colspan').each(function () { - this.colSpan = this.colSpan + 1; - }); + // On initialization we do not need to increase "colspan" since it should + // be correctly set on backend. + if (updateColSpans) { + // Increase the colspan for any columns where it was previously reduced. + $tables.find('.tabledrag-has-colspan').each(function () { + this.colSpan += 1; + }); + } // Change link text. $('.tabledrag-toggle-weight').text(Drupal.t('Hide row weights')); };