2007-10-03 01:08:38 +02:00
|
|
|
/*
|
|
|
|
This class is wrapper for SWFUpload class.
|
|
|
|
If you want use SWFUpload, please use this class becuase it will take care of configuration
|
|
|
|
error handling and other things.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
Upload = Class.create();
|
|
|
|
Upload.prototype = {
|
2007-09-16 04:36:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets configuration data provided from user if smth is missing sets default value.
|
|
|
|
*
|
|
|
|
* @param params object contains all configuration data for upload.
|
|
|
|
*/
|
2007-09-16 04:34:08 +02:00
|
|
|
initialize: function(params) {
|
|
|
|
this.filesUploaded = 0;
|
|
|
|
this.filesToUpload = 0;
|
|
|
|
this.folderID = 'root';
|
|
|
|
this.uploadInProgress = false;
|
|
|
|
this.uploadMessage = '';
|
|
|
|
if(typeof params.fileSizeLimit != 'undefined') this.setFileSizeLimit = params.fileSizeLimit; else this.fileSizeLimit = '30720';
|
2007-09-16 04:34:55 +02:00
|
|
|
if(typeof params.fileTypes != 'undefined') this.fileTypes = params.fileTypes; else this.fileTypes = '*.*';
|
|
|
|
if(typeof params.fileTypesDescription != 'undefined') this.fileTypesDescription = params.fileTypesDescription; else this.fileTypesDescription = 'All Files';
|
2007-09-16 04:34:08 +02:00
|
|
|
if(typeof params.fileUploadLimit != 'undefined') this.fileUploadLimit = params.fileUploadLimit; else this.fileUploadLimit = '6';
|
|
|
|
if(typeof params.beginUploadOnQueue != 'undefined') this.beginUploadOnQueue = params.beginUploadOnQueue; else this.beginUploadOnQueue = false;
|
|
|
|
if(typeof params.fileQueued != 'undefined') this.fileQueued = params.fileQueued;
|
|
|
|
if(typeof params.fileProgress != 'undefined') this.fileProgress = params.fileProgress; else this.fileProgress = Prototype.emptyFunction;
|
|
|
|
if(typeof params.fileCancelled != 'undefined') this.fileCancelled = params.fileCancelled;
|
|
|
|
if(typeof params.fileComplete != 'undefined') this.fileComplete = params.fileComplete ;
|
|
|
|
if(typeof params.queueComplete != 'undefined') this.queueComplete = params.queueComplete;
|
2007-11-11 23:30:01 +01:00
|
|
|
if(typeof params.buildUI != 'undefined') this.customBuildUI = params.buildUI;
|
|
|
|
if(typeof params.securityID != 'undefined') this.securityID = params.securityID;
|
2007-09-16 04:34:08 +02:00
|
|
|
this.onLoad();
|
2007-09-16 03:40:26 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Creates SWFUpload object for uploading files.
|
|
|
|
*
|
|
|
|
*/
|
2007-09-16 03:40:26 +02:00
|
|
|
onLoad: function() {
|
2007-09-16 04:34:08 +02:00
|
|
|
path = this.getBasePath();
|
2007-09-16 04:36:42 +02:00
|
|
|
sessId = this.getSessionId();//Because flash doesn't send proper cookies, we need to set session id in URL.
|
2007-09-16 04:34:08 +02:00
|
|
|
this.swfu = new SWFUpload({
|
2007-11-11 23:30:01 +01:00
|
|
|
upload_target_url: path + '/assets/index/root?executeForm=UploadForm&SecurityID=' + this.securityID + '&PHPSESSID=' + sessId, // Relative to the SWF file
|
2007-09-16 03:40:26 +02:00
|
|
|
file_post_name: 'Files',
|
2007-09-16 04:36:42 +02:00
|
|
|
file_size_limit : this.fileSizeLimit,
|
|
|
|
file_types : this.fileTypes,
|
2007-09-16 04:34:55 +02:00
|
|
|
file_types_description : this.fileTypesDescription,
|
2007-09-16 04:34:08 +02:00
|
|
|
file_upload_limit : this.fileUploadLimit,
|
|
|
|
begin_upload_on_queue : this.beginUploadOnQueue,
|
2007-09-16 03:40:26 +02:00
|
|
|
use_server_data_event : true,
|
|
|
|
validate_files: false,
|
2007-09-16 04:34:08 +02:00
|
|
|
|
|
|
|
file_queued_handler : this.uploadFileQueuedCallback.bind(this),
|
|
|
|
file_complete_handler : this.uploadFileCompleteCallback.bind(this),
|
|
|
|
file_progress_handler: this.uploadFileProgressCallback.bind(this),
|
|
|
|
queue_complete_handler : this.uploadQueueCompleteCallback.bind(this),
|
|
|
|
error_handler : this.uploadErrorCallback.bind(this),
|
2007-09-16 03:40:26 +02:00
|
|
|
file_validation_handler : Prototype.emptyFunction,
|
2007-09-16 04:34:08 +02:00
|
|
|
file_cancelled_handler: Prototype.emptyFunction,
|
|
|
|
|
2007-11-15 23:30:25 +01:00
|
|
|
flash_url : 'jsparty/SWFUpload/SWFUpload.swf', // Relative to this file
|
|
|
|
ui_function: this.buildUI.bind(this),
|
2007-09-16 03:40:26 +02:00
|
|
|
debug: false
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Retrieves base path from URL.
|
|
|
|
* TODO: Use base tag.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
getBasePath: function() {
|
|
|
|
var path = 'http://' + window.location.host + window.location.pathname;
|
|
|
|
if(path[path.length-1] == '/') path = path.substring(0,path.length-1);
|
|
|
|
return path;
|
2007-09-16 03:40:26 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Retrieves sessionId from cookie.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
getSessionId: function() {
|
|
|
|
var start = document.cookie.indexOf('PHPSESSID')+10;
|
|
|
|
var end = document.cookie.indexOf(';',start);
|
|
|
|
if(end == -1) end = document.cookie.length;
|
|
|
|
return document.cookie.substring(start,end);
|
2007-09-16 03:40:26 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Calls method defined by user, method should create user interface.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
buildUI: function() {
|
|
|
|
this.customBuildUI();
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Called when new file is added to the queue
|
|
|
|
*
|
|
|
|
* @param file object
|
|
|
|
* @param queueLength int
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
uploadFileQueuedCallback: function(file,queueLength) {
|
|
|
|
this.filesToUpload++;
|
|
|
|
this.fileQueued(file, queueLength);
|
|
|
|
this.addFileParam(file);
|
2007-09-16 03:40:26 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Called when uploading of particular file has finished
|
|
|
|
*
|
|
|
|
* @param file object
|
|
|
|
* @param servedData string
|
|
|
|
*/
|
|
|
|
|
2007-09-16 03:40:26 +02:00
|
|
|
uploadFileCompleteCallback: function(file,serverData) {
|
2007-09-16 04:34:08 +02:00
|
|
|
this.filesUploaded++;
|
|
|
|
var toEval = serverData.substr(serverData.indexOf('<script'));
|
2007-09-16 03:40:26 +02:00
|
|
|
toEval = toEval.replace('<script type="text/javascript">','');
|
|
|
|
toEval = toEval.replace('</script>','');
|
2007-09-16 04:34:08 +02:00
|
|
|
this.uploadMessage = toEval;
|
|
|
|
this.fileComplete(file, serverData);
|
2007-09-16 03:40:26 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Called during uploading file.
|
|
|
|
*
|
|
|
|
* @param file object
|
|
|
|
* @param bytes_complete int
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
uploadFileProgressCallback: function(file, bytes_complete) {
|
|
|
|
this.uploadInProgress = true;
|
|
|
|
this.fileProgress(file, bytes_complete);
|
2007-09-16 03:40:26 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Called when whole queue has been uploaded or cancelled.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 03:40:26 +02:00
|
|
|
uploadQueueCompleteCallback: function() {
|
2007-09-16 04:34:08 +02:00
|
|
|
this.queueComplete();
|
|
|
|
this.uploadInProgress = false;
|
|
|
|
this.filesUploaded = 0;
|
|
|
|
this.filesToUpload = 0;
|
2007-09-16 03:40:26 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Called on error.
|
|
|
|
* @param error_code int
|
|
|
|
* @param file object
|
|
|
|
* @param message string
|
|
|
|
*/
|
|
|
|
|
2007-09-16 03:40:26 +02:00
|
|
|
uploadErrorCallback: function(error_code, file, message) {
|
2007-09-16 04:34:08 +02:00
|
|
|
this.swfu.cancelQueue();
|
2007-09-16 03:40:26 +02:00
|
|
|
switch(error_code) {
|
|
|
|
case SWFUpload.ERROR_CODE_HTTP_ERROR:
|
|
|
|
alert('You have encountered an error. File hasn\'t been uploaded. Please hit the "Refresh" button in your web browser');
|
|
|
|
break;
|
|
|
|
case SWFUpload.ERROR_CODE_IO_ERROR:
|
|
|
|
alert('You have encountered an error. File hasn\'t been uploaded. Please hit the "Refresh" button in your web browser');
|
|
|
|
break;
|
|
|
|
case SWFUpload.ERROR_CODE_SECURITY_ERROR:
|
|
|
|
alert('You have encountered an error. File hasn\'t been uploaded. Please hit the "Refresh" button in your web browser');
|
|
|
|
break;
|
|
|
|
case SWFUpload.ERROR_CODE_FILE_EXCEEDS_SIZE_LIMIT:
|
2007-09-16 04:34:08 +02:00
|
|
|
alert('Files cannot be bigger than ' + this.fileSizeLimit/1024 + ' MB.');
|
2007-09-16 03:40:26 +02:00
|
|
|
break;
|
|
|
|
case SWFUpload.ERROR_CODE_ZERO_BYTE_FILE:
|
|
|
|
alert('Files cannot be empty');
|
|
|
|
break;
|
|
|
|
case SWFUpload.ERROR_CODE_QUEUE_LIMIT_EXCEEDED:
|
|
|
|
alert('You can only have six files in queue');
|
|
|
|
break;
|
|
|
|
case SWFUpload.ERROR_CODE_UPLOAD_FAILED:
|
|
|
|
alert('You have encountered an error. File hasn\'t has been uploaded. Please hit the "Refresh" button in your web browser');
|
|
|
|
break;
|
|
|
|
case SWFUpload.ERROR_CODE_SPECIFIED_FILE_NOT_FOUND:
|
|
|
|
alert('You have encountered an error. File hasn\'t has been uploaded. Please hit the "Refresh" button in your web browser');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
alert('You have encountered an error. File hasn\'t has been uploaded. Please hit the "Refresh" button in your web browser');
|
|
|
|
}
|
|
|
|
},
|
2007-09-16 04:36:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Because we are on top of standard upload we need to add some POST vars that
|
|
|
|
* normally are being sent as part of form.
|
|
|
|
*
|
|
|
|
* @param file object
|
|
|
|
*/
|
2007-09-16 03:40:26 +02:00
|
|
|
|
|
|
|
addFileParam: function(file) {
|
2007-09-16 04:34:08 +02:00
|
|
|
this.swfu.addFileParam(file.id,'ID',this.folderID);
|
|
|
|
this.swfu.addFileParam(file.id,'action_doUpload','1');
|
|
|
|
this.swfu.addFileParam(file.id,'Files',file.name);
|
|
|
|
this.swfu.addFileParam(file.id,'MAX_FILE_SIZE','31457280');
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Starts file explorer.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
browse: function() {
|
2007-11-15 23:30:25 +01:00
|
|
|
this.swfu.browse();
|
2007-09-16 04:34:08 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Starts upload
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:55 +02:00
|
|
|
startUpload: function() {
|
|
|
|
this.swfu.startUpload();
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Cancels uploading of file.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:55 +02:00
|
|
|
cancelUpload: function(fileId) {
|
|
|
|
this.filesToUpload--;
|
|
|
|
this.swfu.cancelUpload(fileId);
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/*
|
|
|
|
* Getters and setters.
|
|
|
|
*/
|
2007-09-16 04:34:08 +02:00
|
|
|
setFolderID: function(id) {
|
|
|
|
this.folderID = id;
|
|
|
|
},
|
|
|
|
|
|
|
|
getFilesUploaded: function() {
|
|
|
|
return this.filesUploaded;
|
|
|
|
},
|
|
|
|
|
|
|
|
getFilesToUpload: function() {
|
|
|
|
return this.filesToUpload;
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:34:55 +02:00
|
|
|
getUploadMessage: function() {
|
|
|
|
return this.uploadMessage;
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
isUploadInProgress: function() {
|
|
|
|
return this.uploadInProgress;
|
2007-09-16 03:40:26 +02:00
|
|
|
}
|
2007-11-11 23:30:01 +01:00
|
|
|
}
|