Newer
Older
Dries Buytaert
committed
/**
* @file
* Provides JavaScript additions to the managed file field type.
*
* This file provides progress bar support (if available), popup windows for
* file previews, and disabling of other file fields during Ajax uploads (which
Dries Buytaert
committed
* prevents separate file fields from accidentally uploading files).
*/
(function ($) {
Dries Buytaert
committed
/**
* Attach behaviors to managed file element upload fields.
*/
Drupal.behaviors.fileValidateAutoAttach = {
Angie Byron
committed
attach: function (context, settings) {
if (settings.file && settings.file.elements) {
$.each(settings.file.elements, function(selector) {
var extensions = settings.file.elements[selector];
$(selector, context).bind('change', {extensions: extensions}, Drupal.file.validateExtension);
});
}
Dries Buytaert
committed
},
Angie Byron
committed
detach: function (context, settings) {
if (settings.file && settings.file.elements) {
$.each(settings.file.elements, function(selector) {
$(selector, context).unbind('change', Drupal.file.validateExtension);
});
}
Dries Buytaert
committed
}
};
/**
* Attach behaviors to the file upload and remove buttons.
*/
Drupal.behaviors.fileButtons = {
attach: function (context) {
Dries Buytaert
committed
$('input.form-submit', context).bind('mousedown', Drupal.file.disableFields);
$('div.form-managed-file input.form-submit', context).bind('mousedown', Drupal.file.progressBar);
},
Dries Buytaert
committed
detach: function (context) {
Dries Buytaert
committed
$('input.form-submit', context).unbind('mousedown', Drupal.file.disableFields);
$('div.form-managed-file input.form-submit', context).unbind('mousedown', Drupal.file.progressBar);
}
};
/**
* Attach behaviors to links within managed file elements.
*/
Drupal.behaviors.filePreviewLinks = {
attach: function (context) {
Dries Buytaert
committed
$('div.form-managed-file .file a, .file-widget .file a', context).bind('click',Drupal.file.openInNewWindow);
},
detach: function (context){
Dries Buytaert
committed
$('div.form-managed-file .file a, .file-widget .file a', context).unbind('click', Drupal.file.openInNewWindow);
}
};
/**
* File upload utility functions.
*/
Drupal.file = Drupal.file || {
/**
Angie Byron
committed
* Client-side file input validation of file extensions.
Dries Buytaert
committed
*/
validateExtension: function (event) {
Dries Buytaert
committed
// Remove any previous errors.
$('.file-upload-js-error').remove();
Angie Byron
committed
// Add client side validation for the input[type=file].
var extensionPattern = event.data.extensions.replace(/,\s*/g, '|');
if (extensionPattern.length > 1 && this.value.length > 0) {
var acceptableMatch = new RegExp('\\.(' + extensionPattern + ')$', 'gi');
Dries Buytaert
committed
if (!acceptableMatch.test(this.value)) {
var error = Drupal.t("The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.", {
Dries Buytaert
committed
'%filename': this.value,
Angie Byron
committed
'%extensions': extensionPattern.replace(/\|/g, ', ')
Dries Buytaert
committed
});
Angie Byron
committed
$(this).closest('div.form-managed-file').prepend('<div class="messages error file-upload-js-error">' + error + '</div>');
Dries Buytaert
committed
this.value = '';
return false;
}
}
},
/**
* Prevent file uploads when using buttons not intended to upload.
*/
disableFields: function (event){
Dries Buytaert
committed
var clickedButton = this;
// Only disable upload fields for Ajax buttons.
Dries Buytaert
committed
if (!$(clickedButton).hasClass('ajax-processed')) {
return;
}
// Check if we're working with an "Upload" button.
var $enabledFields = [];
Angie Byron
committed
if ($(this).closest('div.form-managed-file').length > 0) {
$enabledFields = $(this).closest('div.form-managed-file').find('input.form-file');
Dries Buytaert
committed
}
Angie Byron
committed
// Temporarily disable upload fields other than the one we're currently
// working with. Filter out fields that are already disabled so that they
// do not get enabled when we re-enable these fields at the end of behavior
// processing. Re-enable in a setTimeout set to a relatively short amount
// of time (1 second). All the other mousedown handlers (like Drupal's Ajax
Angie Byron
committed
// behaviors) are excuted before any timeout functions are called, so we
// don't have to worry about the fields being re-enabled too soon.
// @todo If the previous sentence is true, why not set the timeout to 0?
var $fieldsToTemporarilyDisable = $('div.form-managed-file input.form-file').not($enabledFields).not(':disabled');
$fieldsToTemporarilyDisable.attr('disabled', 'disabled');
setTimeout(function (){
David Rothstein
committed
$fieldsToTemporarilyDisable.attr('disabled', false);
Dries Buytaert
committed
}, 1000);
},
/**
* Add progress bar support if possible.
*/
progressBar: function (event) {
Dries Buytaert
committed
var clickedButton = this;
Angie Byron
committed
var $progressId = $(clickedButton).closest('div.form-managed-file').find('input.file-progress');
if ($progressId.length) {
Dries Buytaert
committed
var originalName = $progressId.attr('name');
// Replace the name with the required identifier.
$progressId.attr('name', originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]);
// Restore the original name after the upload begins.
setTimeout(function () {
Dries Buytaert
committed
$progressId.attr('name', originalName);
}, 1000);
}
// Show the progress bar if the upload takes longer than half a second.
setTimeout(function () {
Angie Byron
committed
$(clickedButton).closest('div.form-managed-file').find('div.ajax-progress-bar').slideDown();
Dries Buytaert
committed
}, 500);
},
/**
* Open links to files within forms in a new window.
*/
openInNewWindow: function (event) {
Dries Buytaert
committed
$(this).attr('target', '_blank');
window.open(this.href, 'filePreview', 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=500,height=550');
return false;
}
};
})(jQuery);