View file upload/js/flow.js/fusty-flow.min.js

File size: 5.63Kb
(function(e,m,f,n){function h(a,b,c){a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent?a.attachEvent("on"+b,c):a["on"+b]=c}function g(a){this.support=!1;this.files=[];this.events=[];this.defaults={simultaneousUploads:3,fileParameterName:"file",query:{},target:"/",generateUniqueIdentifier:null,matchJSON:!1};var b=this;this.inputChangeEvent=function(c){var a=c.target||c.srcElement,d=b.inputChangeEvent;a.removeEventListener?a.removeEventListener("change",d,!1):a.detachEvent?a.detachEvent("onchange",
d):a.onchange=null;d=a.cloneNode(!1);a.parentNode.replaceChild(d,a);b.addFile(a,c);d.value="";h(d,"change",b.inputChangeEvent)};this.opts=e.extend({},this.defaults,a||{})}function k(a,b){this.flowObj=a;this.element=b;this.relativePath=this.name=b.value&&b.value.replace(/.*(\/|\\)/,"");this.uniqueIdentifier=a.generateUniqueIdentifier(b);this.iFrame=null;this.paused=this.error=this.finished=!1;var c=this;this.iFrameLoaded=function(a){if(c.iFrame&&c.iFrame.parentNode){c.finished=!0;try{if(c.iFrame.contentDocument&&
c.iFrame.contentDocument.body&&"false"==c.iFrame.contentDocument.body.innerHTML)return}catch(b){c.error=!0;c.abort();c.flowObj.fire("fileError",c,b);return}a=(c.iFrame.contentDocument||c.iFrame.contentWindow.document).body.innerHTML;c.flowObj.opts.matchJSON&&(a=/(\{.*\})/.exec(a)[0]);c.abort();c.flowObj.fire("fileSuccess",c,a);c.flowObj.upload()}};this.bootstrap()}var l=e.extend,d=e.each;g.prototype={on:e.prototype.on,off:e.prototype.off,fire:e.prototype.fire,cancel:e.prototype.cancel,assignBrowse:function(a){"undefined"==
typeof a.length&&(a=[a]);d(a,function(a){var c;"INPUT"===a.tagName&&"file"===a.type?c=a:(c=f.createElement("input"),c.setAttribute("type","file"),l(a.style,{display:"inline-block",position:"relative",overflow:"hidden",verticalAlign:"top"}),l(c.style,{position:"absolute",top:0,right:0,fontFamily:"Arial",fontSize:"118px",margin:0,padding:0,opacity:0,filter:"alpha(opacity=0)",cursor:"pointer"}),a.appendChild(c));h(c,"change",this.inputChangeEvent)},this)},assignDrop:function(){},unAssignDrop:function(){},
isUploading:function(){var a=!1;d(this.files,function(b){if(b.isUploading())return a=!0,!1});return a},upload:function(){var a=0;d(this.files,function(b){if(1!=b.progress()&&!b.isPaused())if(b.isUploading())a++;else{if(a++>=this.opts.simultaneousUploads)return!1;1==a&&this.fire("uploadStart");b.send()}},this);a||this.fire("complete")},pause:function(){d(this.files,function(a){a.pause()})},resume:function(){d(this.files,function(a){a.resume()})},progress:function(){var a=0,b=0;d(this.files,function(c){a+=
c.progress();b++});return 0<b?a/b:0},addFiles:function(a,b){var c=[];d(a,function(a){1===a.nodeType&&a.value&&(a=new k(this,a),this.fire("fileAdded",a,b)&&c.push(a))},this);this.fire("filesAdded",c,b)&&d(c,function(a){this.opts.singleFile&&0<this.files.length&&this.removeFile(this.files[0]);this.files.push(a)},this);this.fire("filesSubmitted",c,b)},addFile:function(a,b){this.addFiles([a],b)},generateUniqueIdentifier:function(a){var b=this.opts.generateUniqueIdentifier;return"function"===typeof b?
b(a):"xxxxxxxx-xxxx-yxxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a){var b=16*Math.random()|0;return("x"==a?b:b&3|8).toString(16)})},getFromUniqueIdentifier:function(a){var b=!1;d(this.files,function(c){c.uniqueIdentifier==a&&(b=c)});return b},removeFile:function(a){for(var b=this.files.length-1;0<=b;b--)this.files[b]===a&&this.files.splice(b,1)},getSize:function(){},timeRemaining:function(){},sizeUploaded:function(){}};k.prototype={getExtension:e.FlowFile.prototype.getExtension,getType:function(){},
send:function(){if(!this.finished){var a=this.flowObj.opts,b=this.createForm(),c=a.query,d={};c&&"[object Function]"===d.toString.call(c)&&(c=c(this));c[a.fileParameterName]=this.element;c.flowFilename=this.name;c.flowRelativePath=this.relativePath;c.flowIdentifier=this.uniqueIdentifier;this.addFormParams(b,c);h(this.iFrame,"load",this.iFrameLoaded);b.submit();b.parentNode.removeChild(b)}},abort:function(a){if(this.iFrame){this.iFrame.setAttribute("src","java"+String.fromCharCode(115)+"cript:false;");
var b=this.iFrame;b.parentNode.removeChild(b);this.iFrame=null;!a&&this.flowObj.upload()}},cancel:function(){this.flowObj.removeFile(this);this.abort()},retry:function(){this.bootstrap();this.flowObj.upload()},bootstrap:function(){this.abort(!0);this.error=this.finished=!1},timeRemaining:function(){},sizeUploaded:function(){},resume:function(){this.paused=!1;this.flowObj.upload()},pause:function(){this.paused=!0;this.abort()},isUploading:function(){return null!==this.iFrame},isPaused:function(){return this.paused},
isComplete:function(){return 1===this.progress()},progress:function(){return this.error?1:this.finished?1:0},createIframe:function(){var a=/MSIE (6|7|8)/.test(navigator.userAgent)?f.createElement('<iframe name="'+this.uniqueIdentifier+'_iframe">'):f.createElement("iframe");a.setAttribute("id",this.uniqueIdentifier+"_iframe_id");a.setAttribute("name",this.uniqueIdentifier+"_iframe");a.style.display="none";f.body.appendChild(a);return a},createForm:function(){var a=this.flowObj.opts.target;"function"===
typeof a&&(a=a.apply(null));var b=f.createElement("form");b.encoding="multipart/form-data";b.method="POST";b.setAttribute("action",a);this.iFrame||(this.iFrame=this.createIframe());b.setAttribute("target",this.iFrame.name);b.style.display="none";f.body.appendChild(b);return b},addFormParams:function(a,b){var c;d(b,function(b,d){b&&1===b.nodeType?c=b:(c=f.createElement("input"),c.setAttribute("value",b));c.setAttribute("name",d);a.appendChild(c)})}};g.FustyFlowFile=k;"undefined"!==typeof module?module.exports=
g:"function"===typeof define&&define.amd?define(function(){return g}):m.FustyFlow=g})(window.Flow,window,document);