var UPLOAD = 'upload'; var UPLOAD_COMPLETED = 'uploadCompleted'; var UPLOAD_EDIT = 'uploadEdit'; var UPLOAD_MESSAGE = 'uploadMessage'; var UPLOAD_VIEW_CONTAINER = 'uploadViewContainer'; var UPLOAD_BUSY = 'uploadBusy'; var UPLOAD_FRAME = 'uploadFrame'; var UPLOAD_ALERT_MESSAGE = 'uploadAlertMessage'; var UPLOAD_FILE = 'uploadFile'; var UPLOAD_VIEW = 'uploadView'; var UPLOAD_REMOTE_SCRIPTING = 'uploadRemoteScripting'; var UPLOAD_GET_PROGRESS = 'getProgress'; var UPLOAD_PROGRESS_BAR = 'uploadProgressBar'; var UPLOAD_MANAGER = 'uploadManager'; var UPLOAD_CHECK = 'uploadCheck'; var UPLOAD_MESSAGE_CONTAINER = 'uploadMessageContainer'; var UPLOAD_ACTIONS_CONTAINER = 'uploadActionsContainer'; var UPLOAD_DEFAULT_KEY = 'default'; function UploadManager() { var uploadManager = { add : function( name ) { //this method adds another upload to the manager to be uploaded later this.uploads.put( name, new Upload() ); if ( !this.onsubmit ) { //only modify the submit once this.onsubmit = true; this.oldSubmit = document.forms[0].onsubmit; //change the submit of the form to first upload all the files document.forms[0].onsubmit = function() { uploadManager.oldSubmit(); uploadManager.upload(); return false; } } }, complete : function( name ) { //this method is called when an upload is complete this.uploads[ name ].complete = true; var next = this.getNext(); var errors = this.hasErrors(); if ( !next && !errors ) { //uploaded all files with no errors, submit the form //note - do not call submitForm(...) here because then //the onsubmit actions will be run again document.forms[0].submit(); } else if ( next ) { //else move on to the next upload this.upload( next ); } }, upload : function( name ) { if ( !name ) { name = this.getNext(); } //this method actually uploads a file //by calling uploadFile(...) in the frame of the upload control if ( name ) { frames[getUnique( UPLOAD_FRAME, name )].uploadFile( name ); } }, getNext : function() { var upload; var keys = this.uploads.getKeys(); for ( var i = 0; i < keys.length; i++ ) { upload = this.uploads.get( keys[i] ); if ( !upload.complete && !upload.error ) { return keys[i]; } } }, error : function( name ) { //this method is called when an upload errors this.uploads.get( name ).error = true; var next = this.getNext(); if ( next ) { //upload the next file this.upload( next ); } }, cancel : function() { if ( this.onsubmit ) { //change the submit of the form back to what it was document.forms[0].onsubmit = function() { uploadManager.oldSubmit(); } } }, hasErrors : function() { var keys = this.uploads.getKeys(); for ( var i = 0; i < keys.length; i++ ) { if ( this.uploads.get( keys[i] ).error ) { return true; } } return false; } } uploadManager.uploads = new HashMap(); return uploadManager; } var uploadManager = new UploadManager(); function Upload() { var upload = { } return upload; } function findUploadManager( count ) { //find the upload manager in this frame or a parent frame var foundUploadManager = uploadManager; if ( !foundUploadManager && count != 5 && parent ) { //keep going up until you don't have a parent or you've gone up five times foundUploadManager = parent.findUploadManager( ++count ); } return foundUploadManager; } function uploadOnLoad( uniqueIdentifier, showControls ) { if ( showControls ) { //hide the upload control $( '#' + getUnique( UPLOAD_FILE, uniqueIdentifier ) ).hide( 0 ); //show the add/replace action link $( '#' + getUnique( UPLOAD_ACTIONS_CONTAINER, uniqueIdentifier ) ).show( 0 ); $( '#' + getUnique( UPLOAD_EDIT, uniqueIdentifier ) ).show( 0 ); } else { //resize the frame because we are showing it setIFrameHeight( getUnique( UPLOAD_FRAME, uniqueIdentifier ) ); } //set busy indicator off (this is turned on when uploading) $( '#' + getUnique( UPLOAD_BUSY, uniqueIdentifier ) ).hide( 500 ); } function complete( uniqueIdentifier, repositoryFileId, viewURL, edit ) { //show the view action link $( '#' + getUnique( UPLOAD_VIEW_CONTAINER, uniqueIdentifier ) ).show( 0 ); //get the upload hidden field that stored the repositoryFileId var upload = get( getUnique( UPLOAD, uniqueIdentifier ) ); if ( upload ) { //if we have one, then set the repositoryFileId upload.value = repositoryFileId; } if ( viewURL ) { //change the view URL to the new file get( getUnique( UPLOAD_VIEW, uniqueIdentifier ) ).href = viewURL; } if ( edit ) { //we have an edit string (this changes "Add" to "Replace", for instance) showMessage( get( getUnique( UPLOAD_EDIT, uniqueIdentifier ) ), edit ); } //show success check $( '#' + getUnique( UPLOAD_CHECK, uniqueIdentifier ) ).show( 0 ); //set busy indicator off (this is turned on when uploading) $( '#' + getUnique( UPLOAD_BUSY, uniqueIdentifier ) ).hide( 500 ); } function showMessages( uniqueIdentifier, message, alertMessage ) { $( '#' + getUnique( UPLOAD_MESSAGE_CONTAINER, uniqueIdentifier ) ).show( 0 ); if ( message ) { //show the message $( '#' + getUnique( UPLOAD_MESSAGE, uniqueIdentifier ) ).show( 0 ); showMessage( get( getUnique( UPLOAD_MESSAGE, uniqueIdentifier ) ), message ); } else { //hide it $( '#' + getUnique( UPLOAD_MESSAGE, uniqueIdentifier ) ).hide( 0 ); } if ( alertMessage ) { //show the alert message $( '#' + getUnique( UPLOAD_ALERT_MESSAGE, uniqueIdentifier ) ).show( 0 ); showMessage( get( getUnique( UPLOAD_ALERT_MESSAGE, uniqueIdentifier ) ), alertMessage ); } else { //hide it $( '#' + getUnique( UPLOAD_ALERT_MESSAGE, uniqueIdentifier ) ).hide( 0 ); } } function editUpload( uniqueIdentifier ) { //hide the action links $( '#' + getUnique( UPLOAD_ACTIONS_CONTAINER, uniqueIdentifier ) ).hide( 0 ); //hide any current messages $( '#' + getUnique( UPLOAD_MESSAGE_CONTAINER, uniqueIdentifier ) ).hide( 0 ); $( '#' + getUnique( UPLOAD_MESSAGE, uniqueIdentifier ) ).hide( 0 ); //show the file control $( '#' + getUnique( UPLOAD_FILE, uniqueIdentifier ) ).show( 0 ); //hide the upload progress bar $( '#' + getUnique( UPLOAD_PROGRESS_BAR, uniqueIdentifier ) ).hide( 500 ); //resize the frame because we are showing it setIFrameHeight( getUnique( UPLOAD_FRAME, uniqueIdentifier ) ); } function removeUpload( uniqueIdentifier ) { // blank out value $( '#' + getUnique( UPLOAD, uniqueIdentifier ) ).val( '' ) //hide the action links $( '#' + getUnique( UPLOAD_ACTIONS_CONTAINER, uniqueIdentifier ) ).hide( 0 ); //hide any current messages $( '#' + getUnique( UPLOAD_MESSAGE_CONTAINER, uniqueIdentifier ) ).hide( 0 ); $( '#' + getUnique( UPLOAD_MESSAGE, uniqueIdentifier ) ).hide( 0 ); //show the file control $( '#' + getUnique( UPLOAD_FILE, uniqueIdentifier ) ).show( 0 ); //hide the upload progress bar $( '#' + getUnique( UPLOAD_PROGRESS_BAR, uniqueIdentifier ) ).hide( 500 ); //resize the frame because we are showing it setIFrameHeight( getUnique( UPLOAD_FRAME, uniqueIdentifier ) ); } function checkUploadProgress( uniqueIdentifier, start ) { if ( start ) { //we are starting the progress indicator, so show it $( '#' + getUnique( UPLOAD_PROGRESS_BAR, uniqueIdentifier ) ).progressbar({ value: 0 }); $( '#' + getUnique( UPLOAD_PROGRESS_BAR, uniqueIdentifier ) ).show( 500 ); } //get the progress for this upload var parameters = ''; if ( uniqueIdentifier ) { parameters = Array( uniqueIdentifier ); } jsrsExecute( value( UPLOAD_REMOTE_SCRIPTING ), getUploadProgress, UPLOAD_GET_PROGRESS, parameters ); } function getUploadProgress( results ) { var uploadInfo = getItems( results ); var progress = parseInt( uploadInfo[0] ); var uniqueIdentifier = uploadInfo[1]; var error = uploadInfo[3]; var instant = false; if ( uploadInfo[2] === 'true' ) { instant = true; } if ( error ) { $( '#' + getUnique( UPLOAD_PROGRESS_BAR, uniqueIdentifier ) ).hide( 500 ); if ( !instant ) { uploadManager.error( uniqueIdentifier ); //reset the buttons so the user can save again, since the form did not submit because of the error resetButtons(); } } else { if ( progress != 0 ) { //update the progress bar if it's not 0 //set the percentage of the progress in the bar $( '#' + getUnique( UPLOAD_PROGRESS_BAR, uniqueIdentifier ) ).progressbar( 'option', 'value', progress ); } if ( progress != 100 ) { //keep updating the progress until we are at 100% setTimeout( 'checkUploadProgress( \'' + uniqueIdentifier + '\' );', 1000 ); } else if ( !instant ) { //at 100% so finishing this upload AND //not an instant upload, so use the uploadManager uploadManager.complete( uniqueIdentifier ); } } } function uploadFile( uniqueIdentifier ) { //show that the upload is busy $( '#' + getUnique( UPLOAD_BUSY, uniqueIdentifier ) ).show( 500 ); //hide any messages $( '#' + getUnique( UPLOAD_MESSAGE, uniqueIdentifier ) ).hide( 500 ); //resize the frame because hiding the messages will resize the frame setIFrameHeight( getUnique( UPLOAD_FRAME, uniqueIdentifier ) ); //start the progress indicator checkUploadProgress( uniqueIdentifier, true ); } this.uploadManagerReady = true;