2009-06-18 01:14:01 +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 .
* /
Upload = Class . create ( ) ;
Upload . prototype = {
/ * *
* Sets configuration data provided from user if smth is missing sets default value .
*
* @ param params object contains all configuration data for upload .
* /
initialize : function ( params ) {
2009-11-11 08:06:45 +01:00
2009-06-18 01:14:01 +02:00
this . filesUploaded = 0 ;
this . filesToUpload = 0 ;
this . folderID = 'root' ;
this . uploadInProgress = false ;
this . uploadMessage = '' ;
2009-11-11 08:06:45 +01:00
this . queueComplete = this . uploadQueueCompleteCallback . bind ( this ) ;
this . fileComplete = this . uploadFileCompleteCallback . bind ( this ) ;
this . fileQueued = this . uploadFileQueuedCallback ;
2009-06-18 01:14:01 +02:00
if ( typeof params . fileSizeLimit != 'undefined' ) this . setFileSizeLimit = params . fileSizeLimit ; else this . fileSizeLimit = '30720' ;
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' ;
if ( typeof params . fileUploadLimit != 'undefined' ) this . fileUploadLimit = params . fileUploadLimit ; else this . fileUploadLimit = '6' ;
2009-11-11 08:06:45 +01:00
if ( typeof params . beginUploadOnQueue != 'undefined' ) this . beginUploadOnQueue = params . beginUploadOnQueue ; else this . beginUploadOnQueue = true ;
2009-06-18 01:14:01 +02:00
if ( typeof params . fileQueued != 'undefined' ) this . fileQueued = params . fileQueued ;
2009-11-11 08:06:45 +01:00
2009-06-18 01:14:01 +02:00
if ( typeof params . fileProgress != 'undefined' ) this . fileProgress = params . fileProgress ; else this . fileProgress = Prototype . emptyFunction ;
if ( typeof params . fileCancelled != 'undefined' ) this . fileCancelled = params . fileCancelled ;
2009-11-11 08:06:45 +01:00
if ( typeof params . fileComplete != 'undefined' ) this . fileComplete = params . fileComplete ;
2009-06-18 01:14:01 +02:00
if ( typeof params . queueComplete != 'undefined' ) this . queueComplete = params . queueComplete ;
if ( typeof params . buildUI != 'undefined' ) this . customBuildUI = params . buildUI ;
if ( typeof params . securityID != 'undefined' ) this . securityID = params . securityID ;
2009-11-11 08:06:45 +01:00
if ( typeof params . button _image _url != 'undefined' ) this . buttonImageURL = params . button _image _url ;
if ( typeof params . button _width != 'undefined' ) this . buttonWidth = params . button _width ;
if ( typeof params . button _height != 'undefined' ) this . buttonHeight = params . button _height ;
2009-06-18 01:14:01 +02:00
this . onLoad ( ) ;
} ,
/ * *
* Creates SWFUpload object for uploading files .
*
* /
onLoad : function ( ) {
2009-11-11 08:06:45 +01:00
2009-06-18 01:14:01 +02:00
path = this . getBasePath ( ) ;
sessId = this . getSessionId ( ) ; //Because flash doesn't send proper cookies, we need to set session id in URL.
this . swfu = new SWFUpload ( {
2009-11-12 23:06:37 +01:00
upload _url : path + 'admin/assets/UploadForm?action_doUpload=1&PHPSESSID=' + sessId , // Relative to the SWF file
2009-06-18 01:14:01 +02:00
file _post _name : 'Files' ,
file _size _limit : this . fileSizeLimit ,
file _types : this . fileTypes ,
file _types _description : this . fileTypesDescription ,
file _upload _limit : this . fileUploadLimit ,
begin _upload _on _queue : this . beginUploadOnQueue ,
use _server _data _event : true ,
validate _files : false ,
2009-11-11 08:06:45 +01:00
button _placeholder _id : "SWFUploadButton" ,
file _queued _handler : this . fileQueued ,
upload _success _handler : this . queueComplete ,
file _dialog _complete _handler : this . fileCompleted ,
2009-06-18 01:14:01 +02:00
upload _progress _handler : this . uploadFileProgressCallback . bind ( this ) ,
error _handler : this . uploadErrorCallback . bind ( this ) ,
file _validation _handler : Prototype . emptyFunction ,
file _cancelled _handler : Prototype . emptyFunction ,
2009-11-11 08:06:45 +01:00
button _image _url : this . buttonImageURL ,
2009-11-12 23:06:37 +01:00
button _window _mode : "transparent" ,
2009-11-11 08:06:45 +01:00
button _width : this . buttonWidth ,
button _height : this . buttonHeight ,
2009-11-21 04:21:19 +01:00
flash _url : 'sapphire/thirdparty/swfupload/swfupload/swfupload.swf' ,
2009-06-18 01:14:01 +02:00
swfupload _loaded _handler : this . buildUI . bind ( this ) ,
2009-11-21 03:00:47 +01:00
debug : false ,
2009-11-12 23:06:37 +01:00
preserve _relative _urls : true
2009-06-18 01:14:01 +02:00
} ) ;
2009-11-11 08:06:45 +01:00
} ,
2009-06-18 01:14:01 +02:00
/ * *
* Retrieves base path from URL .
* TODO : Use base tag .
* /
getBasePath : function ( ) {
var path = 'http://' + window . location . host + window . location . pathname ;
if ( path . match ( /^(.*\/)admin/i ) ) return RegExp . $1 ;
else return path ;
} ,
/ * *
* Retrieves sessionId from cookie .
*
* /
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 ) ;
} ,
/ * *
* Calls method defined by user , method should create user interface .
*
* /
buildUI : function ( ) {
2009-11-11 08:06:45 +01:00
return ;
2009-06-18 01:14:01 +02:00
} ,
/ * *
* Called when new file is added to the queue
*
* @ param file object
* @ param queueLength int
* /
uploadFileQueuedCallback : function ( file , queueLength ) {
this . filesToUpload ++ ;
2009-11-11 08:06:45 +01:00
// this.swfu.fileQueued(file, queueLength);
2009-06-18 01:14:01 +02:00
this . addFileParam ( file ) ;
} ,
/ * *
* Called when uploading of particular file has finished
*
* @ param file object
* @ param servedData string
* /
uploadFileCompleteCallback : function ( file , serverData ) {
this . filesUploaded ++ ;
if ( serverData ) {
var toEval = serverData . substr ( serverData . indexOf ( '<script' ) ) ;
toEval = toEval . replace ( '<script type="text/javascript">' , '' ) ;
toEval = toEval . replace ( '</script>' , '' ) ;
this . uploadMessage = toEval ;
}
this . fileComplete ( file , serverData ) ;
// Run the next file in the queue, if there is one
if ( this . swfu . getStats ( ) . files _queued > 0 ) this . startUpload ( ) ;
// Otherwise indicate that the queue is finished
else {
this . queueComplete ( ) ;
this . uploadInProgress = false ;
this . filesUploaded = 0 ;
this . filesToUpload = 0 ;
}
} ,
/ * *
* Called during uploading file .
*
* @ param file object
* @ param bytes _complete int
* /
uploadFileProgressCallback : function ( file , bytes _complete ) {
this . uploadInProgress = true ;
this . fileProgress ( file , bytes _complete ) ;
} ,
2009-11-11 08:06:45 +01:00
uploadQueueCompleteCallback : function ( event ) {
} ,
2009-06-18 01:14:01 +02:00
/ * *
* Called on error .
* @ param error _code int
* @ param file object
* @ param message string
* /
uploadErrorCallback : function ( error _code , file , message ) {
this . swfu . cancelQueue ( ) ;
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. Error Code: HTTP Error, File name: ' + file . name + ', Message: ' + msg ) ;
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. Error Code: IO Error, File name: ' + file . name + ', Message: ' + msg ) ;
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. Error Code: Security Error, File name: ' + file . name + ', Message: ' + msg ) ;
break ;
case SWFUpload . ERROR _CODE _FILE _EXCEEDS _SIZE _LIMIT :
alert ( 'Files cannot be bigger than ' + this . fileSizeLimit / 1024 + ' MB.' ) ;
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' ) ;
}
} ,
/ * *
* 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
* /
addFileParam : function ( file ) {
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' ) ;
} ,
/ * *
* Starts file explorer .
*
* /
browse : function ( ) {
this . swfu . selectFiles ( ) ;
} ,
/ * *
* Starts upload
*
* /
startUpload : function ( ) {
this . swfu . startUpload ( ) ;
} ,
/ * *
* Cancels uploading of file .
* /
cancelUpload : function ( fileId ) {
this . filesToUpload -- ;
this . swfu . cancelUpload ( fileId ) ;
} ,
/ *
* Getters and setters .
* /
setFolderID : function ( id ) {
this . folderID = id ;
} ,
getFilesUploaded : function ( ) {
return this . filesUploaded ;
} ,
getFilesToUpload : function ( ) {
return this . filesToUpload ;
} ,
getUploadMessage : function ( ) {
return this . uploadMessage ;
} ,
isUploadInProgress : function ( ) {
return this . uploadInProgress ;
}
}