/*
 * jQuery File Upload User Interface Plugin 9.6.0
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */
(function(factory){if(typeof define==="function"&&define.amd){define(["jquery","tmpl","jquery.iframe-transport","jquery.fileupload-validate"],factory);}else{factory(window.jQuery,window.tmpl);}}(function($,tmpl){$.blueimp.fileupload.prototype._specialOptions.push("filesContainer","uploadTemplateId","downloadTemplateId");$.widget("blueimp.fileupload",$.blueimp.fileupload,{options:{autoUpload:false,uploadTemplateId:"template-upload",downloadTemplateId:"template-download",filesContainer:undefined,prependFiles:false,dataType:"json",messages:{unknownError:"Unknown error"},getNumberOfFiles:function(){return this.filesContainer.children().not(".processing").length;},getFilesFromResponse:function(data){if(data.result&&$.isArray(data.result.files)){return data.result.files;}return[];},add:function(e,data){if(e.isDefaultPrevented()){return false;}var $this=$(this),that=$this.data("blueimp-fileupload")||$this.data("fileupload"),options=that.options;data.context=that._renderUpload(data.files).data("data",data).addClass("processing");options.filesContainer[options.prependFiles?"prepend":"append"](data.context);that._forceReflow(data.context);that._transition(data.context);data.process(function(){return $this.fileupload("process",data);}).always(function(){data.context.each(function(index){$(this).find(".size").text(that._formatFileSize(data.files[index].size));}).removeClass("processing");that._renderPreviews(data);}).done(function(){data.context.find(".start").prop("disabled",false);if((that._trigger("added",e,data)!==false)&&(options.autoUpload||data.autoUpload)&&data.autoUpload!==false){data.submit();}}).fail(function(){if(data.files.error){data.context.each(function(index){var error=data.files[index].error;if(error){$(this).find(".error").text(error);}});}});},send:function(e,data){if(e.isDefaultPrevented()){return false;}var that=$(this).data("blueimp-fileupload")||$(this).data("fileupload");if(data.context&&data.dataType&&data.dataType.substr(0,6)==="iframe"){data.context.find(".progress").addClass(!$.support.transition&&"progress-animated").attr("aria-valuenow",100).children().first().css("width","100%");}return that._trigger("sent",e,data);},done:function(e,data){if(e.isDefaultPrevented()){return false;}var that=$(this).data("blueimp-fileupload")||$(this).data("fileupload"),getFilesFromResponse=data.getFilesFromResponse||that.options.getFilesFromResponse,files=getFilesFromResponse(data),template,deferred;if(data.context){data.context.each(function(index){var file=files[index]||{error:"Empty file upload result"};deferred=that._addFinishedDeferreds();that._transition($(this)).done(function(){var node=$(this);template=that._renderDownload([file]).replaceAll(node);that._forceReflow(template);that._transition(template).done(function(){data.context=$(this);that._trigger("completed",e,data);that._trigger("finished",e,data);deferred.resolve();});});});}else{template=that._renderDownload(files)[that.options.prependFiles?"prependTo":"appendTo"](that.options.filesContainer);that._forceReflow(template);deferred=that._addFinishedDeferreds();that._transition(template).done(function(){data.context=$(this);that._trigger("completed",e,data);that._trigger("finished",e,data);deferred.resolve();});}},
    // modified 'abort/cancel', by lj. Mon Dec 15 15:44:04 CST 2014
    fail:function(e,data){if(e.isDefaultPrevented()){return false;}var that=$(this).data("blueimp-fileupload")||$(this).data("fileupload"),template,deferred;if(data.context){data.context.each(function(index){
    var file=data.files[index];
    if (data.errorThrown !== "abort") {
        if (!file.error &&
            data.jqXHR &&
            data.jqXHR.responseJSON) {
            file.error = data.jqXHR.responseJSON.error; // e.g. for '443 DERP'
        }
        if (!file.error) {
            file.error = data.i18n("unknownError");
        }
    } else {
        file.canceled = true;
    }
        deferred=that._addFinishedDeferreds();that._transition($(this)).done(function(){var node=$(this);template=that._renderDownload([file]).replaceAll(node);that._forceReflow(template);that._transition(template).done(function(){data.context=$(this);that._trigger("failed",e,data);that._trigger("finished",e,data);deferred.resolve();});});});}else{if(data.errorThrown!=="abort"){data.context=that._renderUpload(data.files)[that.options.prependFiles?"prependTo":"appendTo"](that.options.filesContainer).data("data",data);that._forceReflow(data.context);deferred=that._addFinishedDeferreds();that._transition(data.context).done(function(){data.context=$(this);that._trigger("failed",e,data);that._trigger("finished",e,data);deferred.resolve();});}else{that._trigger("failed",e,data);that._trigger("finished",e,data);that._addFinishedDeferreds().resolve();}}},progress:function(e,data){if(e.isDefaultPrevented()){return false;}var progress=Math.floor(data.loaded/data.total*100);if(data.context){data.context.each(function(){$(this).find(".progress").attr("aria-valuenow",progress).children().first().css("width",progress+"%");});}},progressall:function(e,data){if(e.isDefaultPrevented()){return false;}var $this=$(this),progress=Math.floor(data.loaded/data.total*100),globalProgressNode=$this.find(".fileupload-progress"),extendedProgressNode=globalProgressNode.find(".progress-extended");if(extendedProgressNode.length){extendedProgressNode.html(($this.data("blueimp-fileupload")||$this.data("fileupload"))._renderExtendedProgress(data));}globalProgressNode.find(".progress").attr("aria-valuenow",progress).children().first().css("width",progress+"%");},start:function(e){if(e.isDefaultPrevented()){return false;}var that=$(this).data("blueimp-fileupload")||$(this).data("fileupload");that._resetFinishedDeferreds();that._transition($(this).find(".fileupload-progress")).done(function(){that._trigger("started",e);});},stop:function(e){if(e.isDefaultPrevented()){return false;}var that=$(this).data("blueimp-fileupload")||$(this).data("fileupload"),deferred=that._addFinishedDeferreds();$.when.apply($,that._getFinishedDeferreds()).done(function(){that._trigger("stopped",e);});that._transition($(this).find(".fileupload-progress")).done(function(){$(this).find(".progress").attr("aria-valuenow","0").children().first().css("width","0%");$(this).find(".progress-extended").html("&nbsp;");deferred.resolve();});},processstart:function(e){if(e.isDefaultPrevented()){return false;}$(this).addClass("fileupload-processing");},processstop:function(e){if(e.isDefaultPrevented()){return false;}$(this).removeClass("fileupload-processing");},destroy:function(e,data){if(e.isDefaultPrevented()){return false;}var that=$(this).data("blueimp-fileupload")||$(this).data("fileupload"),removeNode=function(){that._transition(data.context).done(function(){$(this).remove();that._trigger("destroyed",e,data);});};if(data.url){data.dataType=data.dataType||that.options.dataType;$.ajax(data).done(removeNode).fail(function(){that._trigger("destroyfailed",e,data);});}else{removeNode();}}},_resetFinishedDeferreds:function(){this._finishedUploads=[];},_addFinishedDeferreds:function(deferred){if(!deferred){deferred=$.Deferred();}this._finishedUploads.push(deferred);return deferred;},_getFinishedDeferreds:function(){return this._finishedUploads;},_enableDragToDesktop:function(){var link=$(this),url=link.prop("href"),name=link.prop("download"),type="application/octet-stream";link.bind("dragstart",function(e){try{e.originalEvent.dataTransfer.setData("DownloadURL",[type,name,url].join(":"));}catch(ignore){}});},_formatFileSize:function(bytes){if(typeof bytes!=="number"){return"";}if(bytes>=1000000000){return(bytes/1000000000).toFixed(2)+" GB";}if(bytes>=1000000){return(bytes/1000000).toFixed(2)+" MB";}return(bytes/1000).toFixed(2)+" KB";},_formatBitrate:function(bits){if(typeof bits!=="number"){return"";}bits = bits/8;if(bits>=1000000000){return(bits/1000000000).toFixed(2)+" GB/s";}if(bits>=1000000){return(bits/1000000).toFixed(2)+" MB/s";}if(bits>=1000){return(bits/1000).toFixed(2)+" KB/s";}return bits.toFixed(2)+" B/s";},_formatTime:function(seconds){var date=new Date(seconds*1000),days=Math.floor(seconds/86400);days=days?days+"d ":"";return days+("0"+date.getUTCHours()).slice(-2)+":"+("0"+date.getUTCMinutes()).slice(-2)+":"+("0"+date.getUTCSeconds()).slice(-2);},_formatPercentage:function(floatValue){return(floatValue*100).toFixed(2)+" %";},_renderExtendedProgress:function(data){return this._formatBitrate(data.bitrate)+" | "+this._formatTime((data.total-data.loaded)*8/data.bitrate)+" | "+this._formatPercentage(data.loaded/data.total)+" | "+this._formatFileSize(data.loaded)+" / "+this._formatFileSize(data.total);},_renderTemplate:function(func,files){if(!func){return $();}var result=func({files:files,formatFileSize:this._formatFileSize,options:this.options});if(result instanceof $){return result;}return $(this.options.templatesContainer).html(result).children();},_renderPreviews:function(data){data.context.find(".preview").each(function(index,elm){$(elm).append(data.files[index].preview);});},_renderUpload:function(files){return this._renderTemplate(this.options.uploadTemplate,files);},_renderDownload:function(files){return this._renderTemplate(this.options.downloadTemplate,files).find("a[download]").each(this._enableDragToDesktop).end();},_startHandler:function(e){e.preventDefault();var button=$(e.currentTarget),template=button.closest(".template-upload"),data=template.data("data");button.prop("disabled",true);if(data&&data.submit){data.submit();}},_cancelHandler:function(e){e.preventDefault();var template=$(e.currentTarget).closest(".template-upload,.template-download"),data=template.data("data")||{};data.context=data.context||template;if(data.abort){data.abort();}else{data.errorThrown="abort";this._trigger("fail",e,data);}},_deleteHandler:function(e){e.preventDefault();var button=$(e.currentTarget);this._trigger("destroy",e,$.extend({context:button.closest(".template-download"),type:"DELETE"},button.data()));},_forceReflow:function(node){return $.support.transition&&node.length&&node[0].offsetWidth;},_transition:function(node){var dfd=$.Deferred();if($.support.transition&&node.hasClass("fade")&&node.is(":visible")){node.bind($.support.transition.end,function(e){if(e.target===node[0]){node.unbind($.support.transition.end);dfd.resolveWith(node);}}).toggleClass("in");}else{node.toggleClass("in");dfd.resolveWith(node);}return dfd;},_initButtonBarEventHandlers:function(){var fileUploadButtonBar=this.element.find(".fileupload-buttonbar"),filesList=this.options.filesContainer;this._on(fileUploadButtonBar.find(".start"),{click:function(e){e.preventDefault();filesList.find(".start").click();}});this._on(fileUploadButtonBar.find(".cancel"),{click:function(e){e.preventDefault();filesList.find(".cancel").click();}});this._on(fileUploadButtonBar.find(".delete"),{click:function(e){e.preventDefault();filesList.find(".toggle:checked").closest(".template-download").find(".delete").click();fileUploadButtonBar.find(".toggle").prop("checked",false);}});this._on(fileUploadButtonBar.find(".toggle"),{change:function(e){filesList.find(".toggle").prop("checked",$(e.currentTarget).is(":checked"));}});},_destroyButtonBarEventHandlers:function(){this._off(this.element.find(".fileupload-buttonbar").find(".start, .cancel, .delete"),"click");this._off(this.element.find(".fileupload-buttonbar .toggle"),"change.");},_initEventHandlers:function(){this._super();this._on(this.options.filesContainer,{"click .start":this._startHandler,"click .cancel":this._cancelHandler,"click .delete":this._deleteHandler});this._initButtonBarEventHandlers();},_destroyEventHandlers:function(){this._destroyButtonBarEventHandlers();this._off(this.options.filesContainer,"click");this._super();},_enableFileInputButton:function(){this.element.find(".fileinput-button input").prop("disabled",false).parent().removeClass("disabled");},_disableFileInputButton:function(){this.element.find(".fileinput-button input").prop("disabled",true).parent().addClass("disabled");},_initTemplates:function(){var options=this.options;options.templatesContainer=this.document[0].createElement(options.filesContainer.prop("nodeName"));if(tmpl){if(options.uploadTemplateId){options.uploadTemplate=tmpl(options.uploadTemplateId);}if(options.downloadTemplateId){options.downloadTemplate=tmpl(options.downloadTemplateId);}}},_initFilesContainer:function(){var options=this.options;if(options.filesContainer===undefined){options.filesContainer=this.element.find(".files");}else{if(!(options.filesContainer instanceof $)){options.filesContainer=$(options.filesContainer);}}},_initSpecialOptions:function(){this._super();this._initFilesContainer();this._initTemplates();},_create:function(){this._super();this._resetFinishedDeferreds();if(!$.support.fileInput){this._disableFileInputButton();}},enable:function(){var wasDisabled=false;if(this.options.disabled){wasDisabled=true;}this._super();if(wasDisabled){this.element.find("input, button").prop("disabled",false);this._enableFileInputButton();}},disable:function(){if(!this.options.disabled){this.element.find("input, button").prop("disabled",true);this._disableFileInputButton();}this._super();}});}));
