Index: misc/tabledrag.js =================================================================== RCS file: /cvs/drupal/drupal/misc/tabledrag.js,v retrieving revision 1.38 diff -u -p -r1.38 tabledrag.js --- tabledrag.js 18 May 2010 06:46:45 -0000 1.38 +++ tabledrag.js 31 May 2010 07:05:48 -0000 @@ -81,10 +81,22 @@ // Make each applicable row draggable. // Match immediate children of the parent element to allow nesting. - $('> tr.draggable, > tbody > tr.draggable', table).each(function() { self.makeDraggable(this); }); + $('> tr.draggable, > tbody > tr.draggable', table).each(function () { self.makeDraggable(this); }); - // Hide columns containing affected form elements. - this.hideColumns(); + // Add a link before the table for users to show or hide weight columns. + $(table).before($('') + .click(function () { + self.toggleShowWeight(self); + return false; + }) + .wrap('
') + .parent() + ); + + // Hide or show weight and parent columns according to user preference. + // This aids screenreader accessibility so users can enter weight values. + // Initialize the weight columns for the show/hide toggle. + self.initColumns(); // Add mouse bindings to the document. The self variable is passed along // as event handlers do not have direct access to the tableDrag object. @@ -93,10 +105,13 @@ }; /** - * Hide the columns containing form elements according to the settings for - * this tableDrag instance. - */ -Drupal.tableDrag.prototype.hideColumns = function () { + * Initialize weight/parent columns to be hidden by default. + * + * Identify and mark each cell with a CSS class so we can easily toggle + * show/hide it. Finally, hide columns if user does not have a + * 'Drupal.tableDrag.showWeight' cookie. +*/ +Drupal.tableDrag.prototype.initColumns = function () { for (var group in this.tableSettings) { // Find the first field in this group. for (var d in this.tableSettings[group]) { @@ -108,13 +123,13 @@ } } - // Hide the column containing this field. + // Mark the column containing this field so it can be hidden. if (hidden && cell[0] && cell.css('display') != 'none') { // 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. var columnIndex = $('> td', cell.parent()).index(cell.get(0)) + 1; var headerIndex = $('> td:not(:hidden)', cell.parent()).index(cell.get(0)) + 1; - $('> thead > tr, > tbody > tr, > tr', this.table).each(function(){ + $('> thead > tr, > tbody > tr, > tr', this.table).each(function (){ var row = $(this); var parentTag = row.parent().get(0).tagName.toLowerCase(); var index = (parentTag == 'thead') ? headerIndex : columnIndex; @@ -128,21 +143,104 @@ if (index > 0) { cell = row.children(':nth-child(' + index + ')'); if (cell[0].colSpan > 1) { - // If this cell has a colspan, simply reduce it. - cell[0].colSpan = cell[0].colSpan - 1; + // If this cell has a colspan, mark it so we can reduce the colspan. + $(cell[0]).addClass('tabledrag-reduce-colspan'); } else { - // Hide table body cells, but remove table header cells entirely - // (Safari doesn't hide properly). - parentTag == 'thead' ? cell.remove() : cell.css('display', 'none'); + // Mark this cell so we can hide it. + $(cell[0]).addClass('tabledrag-hide'); } } }); } } + + // Now hide cells and reduce colspans unless cookie indicates previous choice. + // Set a cookie if it is not already present. + if ($.cookie('Drupal.tableDrag.showWeight') === null) { + $.cookie('Drupal.tableDrag.showWeight', 0, { + path: Drupal.settings.basePath, + // The cookie expires in one year. + expires: 365 + } + ); + this.hideColumns(); + } + // Check cookie value and show/hide weight columns accordingly. + // The following code is basically identical to toggleShowWeight(). + else { + if ($.cookie('Drupal.tableDrag.showWeight') == 1) { + this.showColumns(); + } + else { + this.hideColumns(); + } +// this.toggleShowWeight(self); + } }; /** + * Hide the columns containing weight/parent form elements. + * Undo showColumns(). + */ +Drupal.tableDrag.prototype.hideColumns = function () { + // Turn off display of weight/parent cells and headers. + $('.tabledrag-hide').css('display', 'none'); + // Show TableDrag handles. + $('.tabledrag-handle').css('display', ''); + // Reduce colspan of any effected multi-span columns. + $('.tabledrag-reduce-colspan').each(function () { + this.colSpan = this.colSpan - 1; + }); + // Change link text. + $('.tabledrag-toggle-weight').text(Drupal.t('Show row weights')); + // Change cookie. + $.cookie('Drupal.tableDrag.showWeight', 0, { + path: Drupal.settings.basePath, + // The cookie expires in one year. + expires: 365 + } + ); +} + +/** + * Show the columns containing weight/parent form elements + * Undo hideColumns(). + */ +Drupal.tableDrag.prototype.showColumns = function () { + // Turn on display of weight/parent cells and headers. + $('.tabledrag-hide').css('display', ''); + // Hide TableDrag handles. + $('.tabledrag-handle').css('display', 'none'); + // Increase colspan back to columns it was reduced. + $('.tabledrag-reduce-colspan').each(function () { + this.colSpan = this.colSpan + 1; + }); + // Change link text. + $('.tabledrag-toggle-weight').text(Drupal.t('Hide row weights')); + // Change cookie. + $.cookie('Drupal.tableDrag.showWeight', 1, { + path: Drupal.settings.basePath, + // The cookie expires in one year. + expires: 365 + } + ); +} + +/** + * Toggle visibility of weight/parent columns. Use a saved cookie to store the + * user preference. + */ +Drupal.tableDrag.prototype.toggleShowWeight = function (self) { + if ($.cookie('Drupal.tableDrag.showWeight') == 1) { + this.hideColumns(); + } + else { + this.showColumns(); + } +} + +/** * Find the target used within a particular row and group. */ Drupal.tableDrag.prototype.rowSettings = function (group, row) { @@ -442,7 +540,7 @@ // fields in the entire dragged group. for (var group in self.tableSettings) { var rowSettings = self.rowSettings(group, droppedRow); - if (rowSettings.relationship == 'group') { + if (rowSettings !== undefined && rowSettings.relationship == 'group') { for (var n in self.rowObject.children) { self.updateField(self.rowObject.children[n], group); } @@ -594,11 +692,11 @@ var rowSettings = this.rowSettings(group, changedRow); // Set the row as it's own target. - if (rowSettings.relationship == 'self' || rowSettings.relationship == 'group') { + if (rowSettings !== undefined && (rowSettings.relationship == 'self' || rowSettings.relationship == 'group')) { var sourceRow = changedRow; } // Siblings are easy, check previous and next rows. - else if (rowSettings.relationship == 'sibling') { + else if (rowSettings !== undefined && rowSettings.relationship == 'sibling') { var previousRow = $(changedRow).prev('tr').get(0); var nextRow = $(changedRow).next('tr').get(0); var sourceRow = changedRow; @@ -625,7 +723,7 @@ } // Parents, look up the tree until we find a field not in this group. // Go up as many parents as indentations in the changed row. - else if (rowSettings.relationship == 'parent') { + else if (rowSettings !== undefined && rowSettings.relationship == 'parent') { var previousRow = $(changedRow).prev('tr'); while (previousRow.length && $('.indentation', previousRow).length >= this.rowObject.indents) { previousRow = previousRow.prev('tr'); @@ -660,8 +758,10 @@ rowSettings.source = rowSettings.target; } - var targetClass = '.' + rowSettings.target; - var targetElement = $(targetClass, changedRow).get(0); + if(rowSettings !== undefined) { + var targetClass = '.' + rowSettings.target; + var targetElement = $(targetClass, changedRow).get(0); + } // Check if a target element exists in this row. if (targetElement) {