// $Id$ /** * Manipulation of content remove buttons. * * TableDrag objects for multiple value fields (and multigroups) are scanned * to find 'remove' checkboxes. These checkboxes are hidden when javascript is * enabled (using the Global CSS Killswitch, html.js, defined in drupal.js). * A new 'remove' button is created here in place of these checkboxes aimed to * provide a more user-friendly method to remove items. */ Drupal.behaviors.contentRemoveButtons = function(context) { $('table.content-multiple-table', context).not('.content-multiple-remove-buttons-processed').addClass('content-multiple-remove-buttons-processed').each(function() { var table = this, tableDrag = Drupal.tableDrag[$(table).attr('id')]; // Replace 'remove' checkboxes with buttons. $('input.content-multiple-remove-checkbox', table).each(function() { var $checkbox = $(this), $row = $checkbox.parents('tr:first'); var isRemoved = $checkbox.attr('checked'); var $button = $(Drupal.theme('contentRemoveButton', tableDrag.getRemoveButtonTitle(isRemoved))); // Bind the onClick event to the 'remove' button. $button.bind('click', function(event) { Drupal.contentOnClickRemoveButton($button, $checkbox, tableDrag); return false; }); // Attach the new button to the DOM tree. $checkbox.parent().append($button); // Wrap the contents of all cells (except the drag'n'drop, weight and // remove button cells) with a dummy block element. This operation makes // animations faster because we just need to show/hide a single element // per cell, and it also prevents from creating more than one warning // element per row. $row.children('td:not(.content-multiple-drag):not(.delta-order):not(.content-multiple-remove-cell)').each(function() { $(this).wrapInner('
'); }); // If the row is removed, then hide the contents of the cells and show // the removed warning on the cell next to the drag'n'drop cell. if (isRemoved) { $('.content-multiple-removed-wrapper', $row).hide(); $('.content-multiple-drag', $row).next(':first').append(Drupal.theme('contentRemovedWarning', tableDrag.getRemovedWarning())); // If the form has been submitted and any error was found, FAPI will // send back the same exact form that was submitted to show the error // messages, but it will not invoke the rendering engine which is where // we actually assign the removed class to the row, so we need to check // this situation here and add the class if it is not present. if (!$row.hasClass('content-multiple-removed-row')) { $row.addClass('content-multiple-removed-row'); } } }); }); }; /** * onClick handler for 'remove' buttons. */ Drupal.contentOnClickRemoveButton = function($button, $checkbox, tableDrag) { // Switch the state of the checkbox related to the 'remove' button. var isRemoved = !$checkbox.attr('checked'); $checkbox.attr('checked', isRemoved); $button.parents('tr:first').each(function() { var row = this; // Switch the row class. if (isRemoved) { $(row).addClass('content-multiple-removed-row'); } else { $(row).removeClass('content-multiple-removed-row'); } // Switch the visibility state of the cell elements. $('.content-multiple-removed-wrapper', row).each(function() { var $cellWrapper = $(this); // Remove the warning during restore operation. if (!isRemoved && $('.content-multiple-removed-warning', row).size()) { $('.content-multiple-removed-warning', row).remove(); } // Perform an animation to hide or show the contents of the cells. $cellWrapper.animate({opacity: (isRemoved ? 'hide' : 'show')}, 'slow', function() { // Show the removed warning during remove operation (only on the first cell). if (isRemoved && $cellWrapper.parent().prev(':first').hasClass('content-multiple-drag')) { $('.content-multiple-drag', row).next(':first').append(Drupal.theme('contentRemovedWarning', tableDrag.getRemovedWarning())); } }); }); // Display the table changed warning. tableDrag.displayChangedWarning(); }); // Switch the 'remove' button title. $button.attr('title', tableDrag.getRemoveButtonTitle(isRemoved)); }; /** * tableDrag extension; Display table change warning when appropriate. */ Drupal.tableDrag.prototype.displayChangedWarning = function() { if (this.changed == false) { $(Drupal.theme('tableDragChangedWarning')).insertAfter(this.table).hide().fadeIn('slow'); this.changed = true; } }; /** * Get the title of the 'remove' button. * * This method is an extension of the tableDrag object. This means a separate * module can override this method for a particular tableDrag object. For example, * the multigroup module can change the text to read 'Remove this group of items', * another module could change it to 'Remove this image', and so on... * To override this function: * * var tableId = $(table).attr('id'); * Drupal.tableDrag[tableId].getRemoveButtonTitle = function(isRemoved) { * return (isRemoved ? Drupal.t('Restore this foo') : Drupal.t('Remove this foo')); * }; * * * @param isRemoved * A flag that indicates the state of the button. */ Drupal.tableDrag.prototype.getRemoveButtonTitle = function(isRemoved) { return (isRemoved ? Drupal.t('Restore this item') : Drupal.t('Remove this item')); }; /** * Get the item removed warning. * * This method is an extension of the tableDrag object. It can be overridden by * a separate module. See getRemoveButtonTitle() for further information. */ Drupal.tableDrag.prototype.getRemovedWarning = function() { return Drupal.t('Removed'); }; /** * Theme the 'remove' button. */ Drupal.theme.prototype.contentRemoveButton = function(title) { return ''; }; /** * Theme the item removed warning. */ Drupal.theme.prototype.contentRemovedWarning = function(warning) { return '
'+ warning +'
'; };