| 1 |
// $Id: filefield.js,v 1.20 2009/04/28 01:39:31 quicksketch Exp $
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Auto-attach standard client side file input validation.
|
| 5 |
*/
|
| 6 |
Drupal.behaviors.filefieldValidateAutoAttach = function(context) {
|
| 7 |
$("input[type='file'][accept]", context).change( function() {
|
| 8 |
// Remove any previous errors.
|
| 9 |
$('.file-upload-js-error').remove();
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Add client side validation for the input[type=file] accept attribute.
|
| 13 |
*/
|
| 14 |
var accept = this.accept.replace(/,\s*/g, '|');
|
| 15 |
if (accept.length > 1 && this.value.length > 0) {
|
| 16 |
var v = new RegExp('\\.(' + accept + ')$', 'gi');
|
| 17 |
if (!v.test(this.value)) {
|
| 18 |
var error = Drupal.t("The selected file %filename cannot not be uploaded. Only files with the following extensions are allowed: %extensions.",
|
| 19 |
{ '%filename' : this.value, '%extensions' : accept.replace(/\|/g, ', ') }
|
| 20 |
);
|
| 21 |
// What do I prepend this to?
|
| 22 |
$(this).before('<div class="messages error file-upload-js-error">' + error + '</div>');
|
| 23 |
this.value = '';
|
| 24 |
return false;
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Add filesize validation where possible.
|
| 30 |
*/
|
| 31 |
/* @todo */
|
| 32 |
});
|
| 33 |
};
|
| 34 |
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Prevent FileField uploads when using buttons not intended to upload.
|
| 38 |
*/
|
| 39 |
Drupal.behaviors.filefieldButtons = function(context) {
|
| 40 |
$('input.form-submit')
|
| 41 |
.bind('mousedown', Drupal.filefield.disableFields)
|
| 42 |
.bind('mousedown', Drupal.filefield.progressBar);
|
| 43 |
};
|
| 44 |
|
| 45 |
/**
|
| 46 |
* Open links to files within the node form in a new window.
|
| 47 |
*/
|
| 48 |
Drupal.behaviors.filefieldPreviewLinks = function(context) {
|
| 49 |
$('div.filefield-element div.widget-preview a').click(Drupal.filefield.openInNewWindow).attr('target', '_blank');
|
| 50 |
}
|
| 51 |
|
| 52 |
/**
|
| 53 |
* Admin enhancement: only show the "Files listed by default" when needed.
|
| 54 |
*/
|
| 55 |
Drupal.behaviors.filefieldAdmin = function(context) {
|
| 56 |
var $listField = $('div.filefield-list-field', context);
|
| 57 |
if ($listField.size()) {
|
| 58 |
$listField.find('input').change(function() {
|
| 59 |
if (this.checked) {
|
| 60 |
if (this.value == 0) {
|
| 61 |
$('#edit-list-default-wrapper').css('display', 'none');
|
| 62 |
}
|
| 63 |
else {
|
| 64 |
$('#edit-list-default-wrapper').css('display', 'block');
|
| 65 |
}
|
| 66 |
}
|
| 67 |
}).change();
|
| 68 |
}
|
| 69 |
};
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Utility functions for use by FileField.
|
| 73 |
* @param {Object} event
|
| 74 |
*/
|
| 75 |
Drupal.filefield = {
|
| 76 |
disableFields: function(event){
|
| 77 |
var clickedButton = this;
|
| 78 |
|
| 79 |
// Only disable upload fields for AHAH buttons.
|
| 80 |
if (!$(clickedButton).hasClass('ahah-processed')) {
|
| 81 |
return;
|
| 82 |
}
|
| 83 |
|
| 84 |
// Check if we're working with an "Upload" button.
|
| 85 |
var $enabledFields = [];
|
| 86 |
if ($(this).parents('div.filefield-element').size() > 0) {
|
| 87 |
$enabledFields = $(this).parents('div.filefield-element').find('input.form-file');
|
| 88 |
}
|
| 89 |
// Otherwise we're probably dealing with CCK's "Add another item" button.
|
| 90 |
else if ($(this).parents('div.content-add-more').size() > 0) {
|
| 91 |
$enabledFields = $(this).parent().parent().find('input.form-file');
|
| 92 |
}
|
| 93 |
|
| 94 |
var $disabledFields = $('div.filefield-element input.form-file').not($enabledFields);
|
| 95 |
|
| 96 |
// Disable upload fields other than the one we're currently working with.
|
| 97 |
$disabledFields.attr('disabled', 'disabled');
|
| 98 |
|
| 99 |
// All the other mousedown handlers (like AHAH) are excuted before any
|
| 100 |
// timeout functions will be called, so this effectively re-enables
|
| 101 |
// the filefields after the AHAH process is complete even though it only
|
| 102 |
// has a 1 millisecond timeout.
|
| 103 |
setTimeout(function(){
|
| 104 |
$disabledFields.attr('disabled', '');
|
| 105 |
}, 1000);
|
| 106 |
},
|
| 107 |
progressBar: function(event) {
|
| 108 |
var clickedButton = this;
|
| 109 |
var $progressId = $(clickedButton).parents('div.filefield-element').find('input.filefield-progress');
|
| 110 |
if ($progressId.size()) {
|
| 111 |
var originalName = $progressId.attr('name');
|
| 112 |
|
| 113 |
// Replace the name with the required identifier.
|
| 114 |
$progressId.attr('name', originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]);
|
| 115 |
|
| 116 |
// Restore the original name after the upload begins.
|
| 117 |
setTimeout(function() {
|
| 118 |
$progressId.attr('name', originalName);
|
| 119 |
}, 1000);
|
| 120 |
|
| 121 |
// Show the progress bar if the upload takes longer than 3 seconds.
|
| 122 |
setTimeout(function() {
|
| 123 |
$(clickedButton).parents('div.filefield-element').find('div.ahah-progress-bar').slideDown();
|
| 124 |
}, 500);
|
| 125 |
|
| 126 |
}
|
| 127 |
},
|
| 128 |
openInNewWindow: function(event) {
|
| 129 |
window.open(this.href, 'filefieldPreview', 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=500,height=550');
|
| 130 |
return false;
|
| 131 |
}
|
| 132 |
};
|