// $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').each(function() { var table = this, tableDrag = Drupal.tableDrag[$(table).attr('id')]; // Make sure the element is not processed more than once. $(table).addClass('content-multiple-remove-buttons-processed'); // Create remove buttons. $('input.content-multiple-remove-checkbox', table).each(function() { Drupal.contentRemoveButtons.createButton(tableDrag, this); }); }); }; /** * Global contentRemoveButtons object. */ Drupal.contentRemoveButtons = { _isBusy: false }; /** * Create a 'remove' button. */ Drupal.contentRemoveButtons.createButton = function(tableDrag, checkbox) { // Create a new remove button. 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) { $(this).blur(); // Do not allow users click on a 'remove' button while the process of // another one has not been finished yet. if (!Drupal.contentRemoveButtons._isBusy) { Drupal.contentRemoveButtons._isBusy = true; Drupal.contentRemoveButtons.onClick(button, checkbox, tableDrag); } return false; }); // Attach the new button to the DOM tree. $(checkbox).parent().append(button); // If the row is removed, then hide the contents of the cells. if (isRemoved) { $(button).parents('tr:first').children('td:not(.content-multiple-drag):not(.delta-order):not(.content-multiple-remove-cell)').children().hide(); } }; /** * onClick handler for 'remove' buttons. */ Drupal.contentRemoveButtons.onClick = 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 contents of all cells, except // the drag'n'drop, weight and remove button cells. $(row).children('td:not(.content-multiple-drag):not(.delta-order):not(.content-multiple-remove-cell)').each(function() { var cell = this; if (!isRemoved && $('.content-multiple-removed-warning', row).size()) { $('.content-multiple-removed-warning', row).remove(); } $(this).children().animate({opacity: (isRemoved ? 'hide' : 'show')}, 'slow', function() { if (isRemoved && $(cell).next(':first').hasClass('delta-order')) { $('.content-multiple-drag', row).next(':first').append(Drupal.theme('contentRemovedWarning', tableDrag.getRemovedWarning())); } }); }); // Display the table changed warning. tableDrag.displayChangedWarning(); // Disable the busy flag after a while. setTimeout(function() { Drupal.contentRemoveButtons._isBusy = false; }, 500); }); // Switch the 'remove' button image and 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 +'
'; };