// $Id$ Drupal.filefieldShowDeleteButton = function() { $('.file-delete-button-wrapper').each(function () { fileDeleteData = Drupal.getFileDeleteData(this.id); Drupal.initFileDelete(fileDeleteData); }); } /** * Attaches the upload behaviour to the upload form. * This implementation extends the original function in upload.js. */ Drupal.uploadAutoAttachOriginal = Drupal.uploadAutoAttach; Drupal.uploadAutoAttach = function() { Drupal.uploadAutoAttachOriginal(); Drupal.filefieldShowDeleteButton(); } /** * Handler for the form redirection completion. */ Drupal.jsUpload.prototype.jsUploadCompleteOriginal = Drupal.jsUpload.prototype.oncomplete; Drupal.jsUpload.prototype.oncomplete = function (data) { // Store status of the Delete checkboxes, so that they can be restored later var fileDeleteData = new Array(); $('.file-delete-button-wrapper').each(function () { fileDeleteData[this.id] = Drupal.getFileDeleteData(this.id); }); this.jsUploadCompleteOriginal(data); for (key in fileDeleteData) { Drupal.updateFileDelete(fileDeleteData[key], 'show'); } } /** * Retrieve some ids and classes that will be modified when Delete is pressed. * The id of each wrapper div is used as base name (e.g. files-0-remove), * other elements will have to adjust their id or class accordingly. */ Drupal.getFileDeleteData = function(wrapper) { var fileDeleteData = new Array(); fileDeleteData['wrapper'] = wrapper; // Transform stuff like update_0 to update-0, like Form API does fileDeleteData['checkbox'] = 'edit-' + wrapper.replace(/_/g, '-'); fileDeleteData['button'] = wrapper + '-button'; fileDeleteData['classHide'] = '.' + wrapper + '-hide'; fileDeleteData['classShow'] = '.' + wrapper + '-show'; fileDeleteData['aboutToDelete'] = $('#' + fileDeleteData['checkbox']).attr('checked'); fileDeleteData['textDelete'] = Drupal.settings.fileDeleteButton[0]; fileDeleteData['textCancel'] = Drupal.settings.fileDeleteButton[1]; return fileDeleteData; } /** * Initialize the JS file delete buttons and behaviour. */ Drupal.initFileDelete = function(fileDeleteData) { // Hide the usual wrapper contents, and add a delete/cancel button instead. $('#' + fileDeleteData['wrapper'] + '/*').hide(); $('#' + fileDeleteData['wrapper']).append( "" ); // When the button is clicked, toggle the checkbox and css class visibility $('#' + fileDeleteData['button']).click(function () { fileDeleteData['aboutToDelete'] = ! $('#' + fileDeleteData['checkbox']).attr('checked'); Drupal.updateFileDelete(fileDeleteData, 'fade'); }); } /** * Show the right button text (Delete or Cancel) and css class visibility: * when Delete has been pressed, classHide is hidden and classShow is shown. */ Drupal.updateFileDelete = function(fileDeleteData, appearanceMethod) { $('#' + fileDeleteData['checkbox']).attr('checked', fileDeleteData['aboutToDelete']); if (fileDeleteData['aboutToDelete']) { $('#' + fileDeleteData['button']).val(fileDeleteData['textCancel']); $(fileDeleteData['classHide']).hide(); toBeShown = $(fileDeleteData['classShow']); } else { $('#' + fileDeleteData['button']).val(fileDeleteData['textDelete']); $(fileDeleteData['classShow']).hide(); toBeShown = $(fileDeleteData['classHide']); } // When re-initialization is done after an attachment upload, // we don't want stuff to be faded in - just plain show it. if (appearanceMethod == 'show') { toBeShown.show(); } else { toBeShown.fadeIn(); } } // Global killswitch if (Drupal.jsEnabled) { $(document).ready(Drupal.filefieldShowDeleteButton); }