-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.FileReader.min.js
8 lines (8 loc) · 4.89 KB
/
jquery.FileReader.min.js
1
2
3
4
5
6
7
8
/*
* FileReader
* Copyright(c) Jahdrien
* https://github.com/Jahdrien/FileReader
* MIT Licensed
*
*/
;(function(e){var t,n=e.Callbacks("once unique memory"),r=0,i=null;e.fn.fileReader=function(r){var i;return r=e.extend({id:"fileReaderSWFObject",multiple:null,accept:null,label:null,extensions:null,filereader:"files/filereader.swf",expressInstall:null,debugMode:!1,callback:!1},r),i=this,n.add(function(){return t(i,r)}),e.isFunction(r.callback)&&n.add(r.callback),FileAPIProxy.ready||FileAPIProxy.init(r),this},t=function(t,n){return t.each(function(t,s){s=e(s);var o=s.attr("id");o||(o="flashFileInput"+r,s.attr("id",o),r++),n.multiple=n.multiple===null?!!s.attr("multiple"):!!n.multiple,n.accept=n.accept===null?s.attr("accept"):n.multiple,FileAPIProxy.inputs[o]=s,FileAPIProxy.swfObject.add(o,n.multiple,n.accept,n.label,n.extensions),s.css("z-index",0).mouseover(function(e){o!==i&&(e=e||window.event,i=o,FileAPIProxy.swfObject.mouseover(o),FileAPIProxy.container.height(s.outerHeight()).width(s.outerWidth()).position({of:s}))}).click(function(e){return e.preventDefault(),e.stopPropagation(),e.stopImmediatePropagation(),!1})})},window.FileAPIProxy={ready:!1,init:function(t){var r=this;this.debugMode=t.debugMode,this.container=e("<div>").attr("id",t.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){i&&e("#"+i).trigger(t.type)}).appendTo("body"),swfobject.embedSWF(t.filereader,t.id,"100%","100%","10",t.expressInstall,{debugMode:t.debugMode?!0:""},{wmode:"transparent",allowScriptAccess:"sameDomain"},{},function(t){r.swfObject=t.ref,e(r.swfObject).css({display:"block",outline:0}).attr("tabindex",0),r.ready&&n.fire(),r.ready=t.success})},swfObject:null,container:null,inputs:{},readers:{},onFileInputEvent:function(e){var t;this.debugMode&&console.info("FileInput Event ",e.type,e),e.target in this.inputs&&(t=this.inputs[e.target],e.target=t[0],e.type==="change"&&(e.files=new FileList(e.files),e.target={files:e.files}),t.trigger(e)),window.focus()},onFileReaderEvent:function(e){var t;this.debugMode&&console.info("FileReader Event ",e.type,e,e.target in this.readers),e.target in this.readers&&(t=this.readers[e.target],e.target=t,t._handleFlashEvent.call(t,e))},onFileReaderError:function(e){this.debugMode&&console.log(e)},onSWFReady:function(){return this.container.css({position:"absolute"}),this.ready&&n.fire(),this.ready=!0,!0}},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){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(){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){e in this._callbacks&&this._callbacks[e].add(t)},removeEventListener:function(e,t){e in this._callbacks&&this._callbacks[e].remove(t)},dispatchEvent:function(t){var n;return t.target=this,t.type in this._callbacks&&(n=this["on"+t.type],e.isFunction(n)&&n(t),this._callbacks[t.type].fire(t)),!0},_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:!1,cancelable:!1,defaultPrevented:!1,isTrusted:!1,timeStamp:(new Date).getTime()},t)},stopPropagation:function(){},stopImmediatePropagation:function(){},preventDefault:function(){}},FileList=function(e){var t;if(e){for(t=0;t<e.length;t++)this[t]=e[t];this.length=e.length}else this.length=0},FileList.prototype={item:function(e){return e in this?this[e]:null}}})(jQuery);