4854: Upgrade infusion library to 1.4
[acontent.git] / docs / include / jscripts / infusion / components / uploader / js / HTML5UploaderSupport.js
1 var fluid_1_4=fluid_1_4||{};(function($,fluid){fluid.demands("fluid.uploaderImpl","fluid.uploader.html5",{funcName:"fluid.uploader.multiFileUploader"});fluid.demands("fluid.uploader.progressiveStrategy","fluid.uploader.html5",{funcName:"fluid.uploader.html5Strategy"});fluid.defaults("fluid.uploader.html5Strategy",{gradeNames:["fluid.littleComponent","autoInit"],components:{local:{type:"fluid.uploader.local",options:{queueSettings:"{multiFileUploader}.options.queueSettings",events:{onFileDialog:"{multiFileUploader}.events.onFileDialog",onFilesSelected:"{multiFileUploader}.events.onFilesSelected",afterFileDialog:"{multiFileUploader}.events.afterFileDialog",afterFileQueued:"{multiFileUploader}.events.afterFileQueued",onQueueError:"{multiFileUploader}.events.onQueueError"}}},remote:{type:"fluid.uploader.remote",options:{queueSettings:"{multiFileUploader}.options.queueSettings",events:{afterReady:"{multiFileUploader}.events.afterReady",onFileStart:"{multiFileUploader}.events.onFileStart",onFileProgress:"{multiFileUploader}.events.onFileProgress",onFileSuccess:"{multiFileUploader}.events.onFileSuccess",onFileError:"{multiFileUploader}.events.onFileError",onFileComplete:"{multiFileUploader}.events.onFileComplete"}}}},legacyBrowserFileLimit:100000});fluid.uploader.html5Strategy.fileSuccessHandler=function(file,events,xhr){events.onFileSuccess.fire(file,xhr.responseText,xhr);events.onFileComplete.fire(file)};fluid.uploader.html5Strategy.fileErrorHandler=function(file,events,xhr){events.onFileError.fire(file,fluid.uploader.errorConstants.UPLOAD_FAILED,xhr.status,xhr);events.onFileComplete.fire(file)};fluid.uploader.html5Strategy.fileStopHandler=function(file,events,xhr){events.onFileError.fire(file,fluid.uploader.errorConstants.UPLOAD_STOPPED,xhr.status,xhr);events.onFileComplete.fire(file)};fluid.uploader.html5Strategy.progressTracker=function(){var that={previousBytesLoaded:0};that.getChunkSize=function(bytesLoaded){var chunkSize=bytesLoaded-that.previousBytesLoaded;that.previousBytesLoaded=bytesLoaded;return chunkSize};return that};fluid.uploader.html5Strategy.monitorFileUploadXHR=function(file,events,xhr){xhr.onreadystatechange=function(){if(xhr.readyState===4){var status=xhr.status;if(status===200){fluid.uploader.html5Strategy.fileSuccessHandler(file,events,xhr)}else{if(status===0){fluid.uploader.html5Strategy.fileStopHandler(file,events,xhr)}else{fluid.uploader.html5Strategy.fileErrorHandler(file,events,xhr)}}}};var progressTracker=fluid.uploader.html5Strategy.progressTracker();xhr.upload.onprogress=function(pe){events.onFileProgress.fire(file,progressTracker.getChunkSize(pe.loaded),pe.total)}};fluid.uploader.html5Strategy.remote=function(queue,options){var that=fluid.initLittleComponent("fluid.uploader.html5Strategy.remote",options);that.queue=queue;that.queueSettings=that.options.queueSettings;that.uploadNextFile=function(){var batch=that.queue.currentBatch;var file=batch.files[batch.fileIdx];that.uploadFile(file)};that.uploadFile=function(file){that.events.onFileStart.fire(file);that.currentXHR=that.createXHR();fluid.uploader.html5Strategy.monitorFileUploadXHR(file,that.events,that.currentXHR);that.fileSender.send(file,that.queueSettings,that.currentXHR)};that.stop=function(){that.queue.isUploading=false;that.currentXHR.abort()};fluid.initDependents(that);that.events.afterReady.fire();return that};fluid.defaults("fluid.uploader.html5Strategy.remote",{gradeNames:["fluid.eventedComponent"],argumentMap:{options:1},components:{fileSender:{type:"fluid.uploader.html5Strategy.fileSender"}},invokers:{createXHR:"fluid.uploader.html5Strategy.createXHR"}});fluid.demands("fluid.uploader.remote",["fluid.uploader.html5Strategy","fluid.uploader.live"],{funcName:"fluid.uploader.html5Strategy.remote",args:["{multiFileUploader}.queue",fluid.COMPONENT_OPTIONS]});fluid.uploader.html5Strategy.createXHR=function(){return new XMLHttpRequest()};fluid.uploader.html5Strategy.createFormData=function(){return new FormData()};var setPostParams=function(formData,postParams){$.each(postParams,function(key,value){formData.append(key,value)})};fluid.defaults("fluid.uploader.html5Strategy.formDataSender",{gradeNames:["fluid.littleComponent","autoInit"],finalInitFunction:"fluid.uploader.html5Strategy.formDataSender.init",invokers:{createFormData:"fluid.uploader.html5Strategy.createFormData"}});fluid.uploader.html5Strategy.formDataSender.init=function(that){that.send=function(file,queueSettings,xhr){var formData=that.createFormData();formData.append("file",file);setPostParams(formData,queueSettings.postParams);xhr.open("POST",queueSettings.uploadURL,true);xhr.send(formData);return formData}};fluid.demands("fluid.uploader.html5Strategy.fileSender",["fluid.uploader.html5Strategy.remote","fluid.browser.supportsFormData"],{funcName:"fluid.uploader.html5Strategy.formDataSender"});fluid.uploader.html5Strategy.generateMultipartBoundary=function(){var boundary="---------------------------";boundary+=Math.floor(Math.random()*32768);boundary+=Math.floor(Math.random()*32768);boundary+=Math.floor(Math.random()*32768);return boundary};fluid.uploader.html5Strategy.generateMultiPartContent=function(boundary,file){var CRLF="\r\n";var multipart="";multipart+="--"+boundary+CRLF;multipart+='Content-Disposition: form-data; name="fileData"; filename="'+file.name+'"'+CRLF;multipart+="Content-Type: "+file.type+CRLF+CRLF;multipart+=file.getAsBinary();multipart+=CRLF+"--"+boundary+"--"+CRLF;return multipart};fluid.defaults("fluid.uploader.html5Strategy.rawMIMESender",{gradeNames:["fluid.littleComponent","autoInit"],finalInitFunction:"fluid.uploader.html5Strategy.rawMIMESender.init"});fluid.uploader.html5Strategy.rawMIMESender.init=function(that){that.send=function(file,queueSettings,xhr){var boundary=fluid.uploader.html5Strategy.generateMultipartBoundary();var multipart=fluid.uploader.html5Strategy.generateMultiPartContent(boundary,file);xhr.open("POST",queueSettings.uploadURL,true);xhr.setRequestHeader("Content-Type","multipart/form-data; boundary="+boundary);xhr.sendAsBinary(multipart);return multipart}};fluid.demands("fluid.uploader.html5Strategy.fileSender","fluid.uploader.html5Strategy.remote",{funcName:"fluid.uploader.html5Strategy.rawMIMESender"});fluid.uploader.html5Strategy.local=function(queue,legacyBrowserFileLimit,options){var that=fluid.initLittleComponent("fluid.uploader.html5Strategy.local",options);that.queue=queue;that.queueSettings=that.options.queueSettings;that.addFiles=function(files){var sizeLimit=(legacyBrowserFileLimit||that.queueSettings.fileSizeLimit)*1024;var fileLimit=that.queueSettings.fileUploadLimit;var uploaded=that.queue.getUploadedFiles().length;var queued=that.queue.getReadyFiles().length;var remainingUploadLimit=fileLimit-uploaded-queued;that.events.onFilesSelected.fire(files.length);var numFilesAdded=0;for(var i=0;i<files.length;i++){var file=files[i];if(fileLimit&&remainingUploadLimit===0){that.events.onQueueError.fire(file,fluid.uploader.queueErrorConstants.QUEUE_LIMIT_EXCEEDED)}else{if(file.size>=sizeLimit){file.filestatus=fluid.uploader.fileStatusConstants.ERROR;that.events.onQueueError.fire(file,fluid.uploader.queueErrorConstants.FILE_EXCEEDS_SIZE_LIMIT)}else{if(!fileLimit||remainingUploadLimit>0){file.id="file-"+fluid.allocateGuid();file.filestatus=fluid.uploader.fileStatusConstants.QUEUED;that.events.afterFileQueued.fire(file);remainingUploadLimit--;numFilesAdded++}}}}that.events.afterFileDialog.fire(numFilesAdded)};that.removeFile=function(file){};that.enableBrowseButton=function(){that.browseButtonView.enable()};that.disableBrowseButton=function(){that.browseButtonView.disable()};fluid.initDependents(that);return that};fluid.defaults("fluid.uploader.html5Strategy.local",{argumentMap:{options:2},gradeNames:["fluid.eventedComponent"],components:{browseButtonView:{type:"fluid.uploader.html5Strategy.browseButtonView",options:{queueSettings:"{multiFileUploader}.options.queueSettings",selectors:{browseButton:"{multiFileUploader}.selectors.browseButton"},listeners:{onFilesQueued:"{local}.addFiles"}}}}});fluid.demands("fluid.uploader.local","fluid.uploader.html5Strategy",{funcName:"fluid.uploader.html5Strategy.local",args:["{multiFileUploader}.queue","{html5Strategy}.options.legacyBrowserFileLimit","{options}"]});fluid.demands("fluid.uploader.local",["fluid.uploader.html5Strategy","fluid.browser.supportsFormData"],{funcName:"fluid.uploader.html5Strategy.local",args:["{multiFileUploader}.queue",undefined,"{options}"]});var bindEventsToFileInput=function(that,fileInput){fileInput.click(function(){that.events.onBrowse.fire()});fileInput.change(function(){var files=fileInput[0].files;that.renderFreshMultiFileInput();that.events.onFilesQueued.fire(files)});fileInput.focus(function(){that.browseButton.addClass("focus")});fileInput.blur(function(){that.browseButton.removeClass("focus")})};var renderMultiFileInput=function(that){var multiFileInput=$(that.options.multiFileInputMarkup);var fileTypes=that.options.queueSettings.fileTypes;if(fluid.isArrayable(fileTypes)){fileTypes=fileTypes.join();multiFileInput.attr("accept",fileTypes)}bindEventsToFileInput(that,multiFileInput);return multiFileInput};var setupBrowseButtonView=function(that){var multiFileInput=renderMultiFileInput(that);that.browseButton.append(multiFileInput);that.browseButton.attr("tabindex",-1)};fluid.uploader.html5Strategy.browseButtonView=function(container,options){var that=fluid.initView("fluid.uploader.html5Strategy.browseButtonView",container,options);that.browseButton=that.locate("browseButton");that.renderFreshMultiFileInput=function(){var previousInput=that.locate("fileInputs").last();previousInput.hide();previousInput.attr("tabindex",-1);var newInput=renderMultiFileInput(that);previousInput.after(newInput)};that.enable=function(){that.locate("fileInputs").prop("disabled",false)};that.disable=function(){that.locate("fileInputs").prop("disabled",true)};setupBrowseButtonView(that);return that};fluid.defaults("fluid.uploader.html5Strategy.browseButtonView",{gradeNames:"fluid.viewComponent",multiFileInputMarkup:"<input type='file' multiple='' class='flc-uploader-html5-input' />",queueSettings:{},selectors:{browseButton:".flc-uploader-button-browse",fileInputs:".flc-uploader-html5-input"},events:{onBrowse:null,onFilesQueued:null}});fluid.demands("fluid.uploader.html5Strategy.browseButtonView","fluid.uploader.html5Strategy.local",{container:"{multiFileUploader}.container",mergeOptions:{events:{onBrowse:"{local}.events.onFileDialog"}}})})(jQuery,fluid_1_4);