-
Notifications
You must be signed in to change notification settings - Fork 61
/
jquery.FileReader.min.js
1 lines (1 loc) · 5.12 KB
/
jquery.FileReader.min.js
1
(function(e){var t=e.Callbacks("once unique memory"),n=0,r=null;if(window.FileReader){e.fn.fileReader=function(){return this};return}e.fn.fileReader=function(n){n=e.extend(e.fn.fileReader.defaults,n);var r=this;t.add(function(){return i(r,n)});if(e.isFunction(n.callback))t.add(n.callback);if(!FileAPIProxy.ready){FileAPIProxy.init(n)}return this};e.fn.fileReader.defaults={id:"fileReaderSWFObject",multiple:null,accept:null,label:null,extensions:null,filereader:"files/filereader.swf",expressInstall:null,debugMode:false,callback:false};var i=function(t,i){return t.each(function(t,s){s=e(s);var o=s.attr("id"),u,a;if(!o){o="flashFileInput"+n;n++}u=s.parent("label");a=u.length>0?u:s;a.attr("id",a.attr("id")||o);i.multiple=!!(i.multiple===null?s.attr("multiple"):i.multiple);i.accept=i.accept===null?s.attr("accept"):i.accept;FileAPIProxy.inputs[o]=s;FileAPIProxy.swfObject.add(a.attr("id"),i.multiple,i.accept,i.label,i.extensions);a.css("z-index",0).mouseover(function(e){if(o!==r){e=e||window.event;r=o;FileAPIProxy.swfObject.mouseover(a.attr("id"));FileAPIProxy.container.height(a.outerHeight()).width(a.outerWidth()).css(a.offset())}}).click(function(e){e.preventDefault();e.stopPropagation();e.stopImmediatePropagation();return false})})};window.FileAPIProxy={ready:false,init:function(n){var i=this;this.debugMode=n.debugMode;this.container=e("<div>").attr("id",n.id).wrap("<div>").parent().css({position:"fixed",width:"1px",height:"1px",display:"inline-block",background:"transparent","z-index":99999}).on("mouseover mouseout mousedown mouseup",function(t){if(r)e("#"+r).trigger(t.type)}).appendTo("body");swfobject.embedSWF(n.filereader,n.id,"100%","100%","10",n.expressInstall,{debugMode:n.debugMode?true:""},{wmode:"transparent",allowScriptAccess:"sameDomain"},{},function(n){i.swfObject=n.ref;e(i.swfObject).css({display:"block",outline:0}).attr("tabindex",0);if(i.ready){t.fire()}i.ready=n.success&&typeof n.ref.add==="function"})},swfObject:null,container:null,inputs:{},readers:{},onFileInputEvent:function(e){if(this.debugMode)console.info("FileInput Event ",e.type,e);if(e.target in this.inputs){var t=this.inputs[e.target];e.target=t[0];if(e.type==="change"){e.files=new FileList(e.files);e.target={files:e.files}}t.trigger(e)}window.focus()},onFileReaderEvent:function(e){if(this.debugMode)console.info("FileReader Event ",e.type,e,e.target in this.readers);if(e.target in this.readers){var t=this.readers[e.target];e.target=t;t._handleFlashEvent.call(t,e)}},onFileReaderError:function(e){if(this.debugMode)console.log(e)},onSWFReady:function(){this.container.css({position:"absolute"});this.ready=typeof this.swfObject.add==="function";if(this.ready){t.fire()}return true}};window.FileReader=function(){this.EMPTY=0;this.LOADING=1;this.DONE=2;this.readyState=0;this.result=null;this.error=null;this.onloadstart=null;this.onprogress=null;this.onload=null;this.onabort=null;this.onerror=null;this.onloadend=null;this._callbacks={loadstart:e.Callbacks("unique"),progress:e.Callbacks("unique"),abort:e.Callbacks("unique"),error:e.Callbacks("unique"),load:e.Callbacks("unique"),loadend:e.Callbacks("unique")};this._id=null};window.FileReader.prototype={readAsBinaryString:function(e){this._start(e);FileAPIProxy.swfObject.read(e.input,e.name,"readAsBinaryString")},readAsText:function(e,t){this._start(e);FileAPIProxy.swfObject.read(e.input,e.name,"readAsText")},readAsDataURL:function(e){this._start(e);FileAPIProxy.swfObject.read(e.input,e.name,"readAsDataURL")},readAsArrayBuffer:function(e){throw"Whoops FileReader.readAsArrayBuffer is unimplemented"},abort:function(){this.result=null;if(this.readyState===this.EMPTY||this.readyState===this.DONE)return;FileAPIProxy.swfObject.abort(this._id)},addEventListener:function(e,t){if(e in this._callbacks)this._callbacks[e].add(t)},removeEventListener:function(e,t){if(e in this._callbacks)this._callbacks[e].remove(t)},dispatchEvent:function(t){t.target=this;if(t.type in this._callbacks){var n=this["on"+t.type];if(e.isFunction(n))n(t);this._callbacks[t.type].fire(t)}return true},_register:function(e){this._id=e.input+"."+e.name;FileAPIProxy.readers[this._id]=this},_start:function(e){this._register(e);if(this.readyState===this.LOADING)throw{type:"InvalidStateError",code:11,message:"The object is in an invalid state."}},_handleFlashEvent:function(e){switch(e.type){case"loadstart":this.readyState=this.LOADING;break;case"loadend":this.readyState=this.DONE;break;case"load":this.readyState=this.DONE;this.result=FileAPIProxy.swfObject.result(this._id);break;case"error":this.result=null;this.error={name:"NotReadableError",message:"The File cannot be read!"}}this.dispatchEvent(new FileReaderEvent(e))}};FileReaderEvent=function(e){this.initEvent(e)};FileReaderEvent.prototype={initEvent:function(t){e.extend(this,{type:null,target:null,currentTarget:null,eventPhase:2,bubbles:false,cancelable:false,defaultPrevented:false,isTrusted:false,timeStamp:(new Date).getTime()},t)},stopPropagation:function(){},stopImmediatePropagation:function(){},preventDefault:function(){}};FileList=function(e){if(e){for(var t=0;t<e.length;t++){this[t]=e[t]}this.length=e.length}else{this.length=0}};FileList.prototype={item:function(e){if(e in this)return this[e];return null}}})(jQuery)