File: filedrop-min.js

Recommend this page to a friend!
  Classes of Pavel   File Drop   filedrop-min.js   Download  
File: filedrop-min.js
Role: Class source
Content type: text/plain
Description: Minified script
Class: File Drop
Handle file upload drag and drop operations
Author: By
Last change: Fixed FF 13 issue.
Date: 12 years ago
Size: 8,313 bytes
 

Contents

Class file image Download
window.fd=window.fd||{logging:true,onObjCall:null,isChrome:(navigator.vendor||"").indexOf("Google")!=-1,isFirebug:!!(window.console&&window.console.dir)};fd.RandomID=function(){return"fd_"+(Math.random()*10000).toFixed()};fd.ByID=function(a){return fd.IsTag(a)?a:document.getElementById(a)};fd.IsTag=function(b,a){return typeof b=="object"&&b&&b.nodeType==1&&(!a||b.tagName.toUpperCase()==a.toUpperCase())};fd.NewXHR=function(){var c;try{c=new XMLHttpRequest()}catch(d){var a=new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");for(var b=0;b<a.length&&!c;b++){try{c=new ActiveXObject(a[b])}catch(d){}}}return c};fd.InstanceOf=function(c,a){c=c.toLowerCase();var b=(a&&a.constructor)?a.constructor:"";return b.name?(b.name.toLowerCase()==c):(b.toString().toLowerCase().indexOf(c)!=-1)};fd.IsArray=function(a){return typeof a=="object"&&fd.InstanceOf("Array",a)};fd.AddEvent=function(c,b,a){if(c.attachEvent){c["e"+b+a]=a;c[b+a]=function(){c["e"+b+a](window.event)};c.attachEvent("on"+b,c[b+a])}else{c.addEventListener(b,a,false)}};fd.StopEvent=function(a){a.cancelBubble=true;a.returnValue=false;if(a.stopPropagation){a.stopPropagation()}if(a.preventDefault){a.preventDefault()}};fd.SetClass=function(b,c,a){b=fd.ByID(b);if(fd.IsTag(b)){if(typeof a=="undefined"||a){if(!fd.HasClass(b,c)){b.className+=" "+c}}else{b.className=b.className.replace(fd.ClassRegExp(c)," ")}}};fd.HasClass=function(a,b){return fd.ClassRegExp(b).test((fd.ByID(a)||{}).className)};fd.ClassRegExp=function(a){return new RegExp("(^|\\s)"+a+"(\\s|$)","gi")};fd.Extend=function(d,b,a){for(var c in b){if(a||typeof d[c]=="undefined"){d[c]=b[c]}}};fd.CallAll=function(d,a,e){a=fd.IsArray(a)?a:[a];var c;if(fd.IsArray(d)){for(var b=0;b<d.length;b++){if(typeof d[b]=="function"){c=d[b].apply(e||this,a);if(c!=null){break}}}}else{if(typeof d!="undefined"&&d!=null&&fd.isFirebug){console.error("FileDrop event list must be either an Array or undefined/null, "+(typeof d)+" was given.")}}return c};fd.CallOf=function(e,d,b){if(fd.logging&&fd.isFirebug){var a=e.on[d]?e.on[d].length:0;console.info("FileDrop "+d+" event ("+a+"); args:");console.dir([b])}if(typeof fd.onObjCall=="function"){var c=fd.onObjCall.call(e,d,b);if(c!=null){return c}}return fd.CallAll(e.on[d],b,e)};fd.DropHandle=function(a,c){var b=this;b.zone=a=fd.ByID(a);b.opt={zoneClass:"fd-zone",inputClass:"fd-file",iframe:{url:""},input:null,fullDocDragDetect:false};fd.Extend(b.opt,c,true);if(fd.isChrome){b.opt.fullDocDragDetect=true}b.on={dragEnter:[],dragLeave:[],dragOver:[],dragEnd:[],dragExit:[],upload:[],inputSetup:[],iframeSetup:[],iframeDone:[]};b.Hook=function(d){if(b.opt.input!=false){b.opt.input=b.opt.input||b.PrepareInput(d);if(b.opt.input){fd.CallOf(b,"inputSetup",b.opt.input)}}b.HookDragOn(d);b.HookDropOn(d)};b.HookDragOn=function(d){function e(f,g){fd.AddEvent(g?document.body:d,f.toLowerCase(),function(h){fd.StopEvent(h);fd.CallOf(b,f,h)})}if(b.opt.fullDocDragDetect){e("dragEnter",true);fd.AddEvent(document,"dragleave",function(f){if((f.clientX==0&&f.clientY==0)||fd.IsTag(f.relatedTarget,"html")){fd.StopEvent(f);fd.CallOf(b,"dragLeave",f)}})}else{e("dragEnter");e("dragLeave")}if(!fd.isChrome){e("dragOver")}e("dragEnd");e("dragExit")};b.HookDropOn=function(d){function e(f){fd.StopEvent(f);fd.CallOf(b,"upload",f)}fd.AddEvent(d,"drop",e);fd.AddEvent(b.opt.input.file,"change",e)};b.PrepareInput=function(f){var d={file:b.FindInputRec(f)||b.CreateInputAt(f)};if(d.file){var e=d.file.parentNode;while(e&&!fd.IsTag(e,"form")){e=d.form.parentNode}var g=e?e.getAttribute("target"):"";if(g&&fd.IsTag(fd.ByID(g),"iframe")){d.form=e}}return d};b.FindInputRec=function(f){var e;for(var d=0;!e&&d<f.childNodes.length;d++){var g=f.childNodes[d];if(fd.IsTag(g,"input")&&g.getAttribute("type")=="file"&&fd.HasClass(g,b.opt.inputClass)){e=g}else{if(g.childNodes.length>0){e=b.FindInputRec(g)}}}return e};b.CreateInputAt=function(e){do{var g=fd.RandomID()}while(fd.ByID(g));var d=document.createElement("div");d.innerHTML='<iframe src="javascript:false;" name="'+g+'"></iframe><form method="post" enctype="multipart/form-data"><input type="hidden" name="fd-callback" /><input type="file" name="fd-file" /></form>';d.firstChild.setAttribute("id",g);d.firstChild.style.display="none";d.lastChild.setAttribute("target",g);var f=e.firstChild;while(f&&(!fd.IsTag(f)||fd.IsTag(f,"legend"))){f=f.nextSibling}if(f){e.insertBefore(d,f)}else{e.appendChild(d)}return d.lastChild.lastChild};b.AbortIFrame=function(){if(b.opt.input.form){var d=fd.ByID(b.opt.input.form.getAttribute("target"));if(d){d.setAttribute("src","javascript:false;")}}};b.SendViaIFrame=function(e){e=e||b.opt.iframe.url;var g=(b.opt.input||{}).form;if(e&&g){do{var d=fd.RandomID()}while(window[d]);window[d]=function(i){var h=typeof i=="object"?i:{response:i,responseText:i,readyState:4,status:200};fd.Extend(h,{iframe:true,url:e});fd.CallOf(b,"iframeDone",h)};var f=g.firstChild;while(f&&(!fd.IsTag(f,"input")||f.name!="fd-callback")){f=f.nextSibling}if(f){f.value=d}else{e=e.replace(/[?&]$/,"")+(e.indexOf("?")==-1?"?":"&")+"fd-callback="+d}g.setAttribute("action",e);fd.CallOf(b,"iframeSetup",g);g.submit()}};b.Multiple=function(d){if(!b.opt.input){throw"FileDrop.Multiple(): no self.opt.input assigned."}if(typeof d!="undefined"){d?b.opt.input.file.setAttribute("multiple","multiple"):b.opt.input.file.removeAttribute("multiple")}return !!b.opt.input.file.getAttribute("multiple")};b.SetupInput=function(d){d.file.className=b.opt.inputClass;var e=d.file.parentNode;if(e&&e.style.display.match(/^(static)?$/)){e.style.position="relative"}if(fd.IsTag(a,"fieldset")){var f=document.createElement("div");f.style.position="relative";f.style.overflow="hidden";a.parentNode.insertBefore(f,a);f.appendChild(a)}};b.on.inputSetup.push(b.SetupInput);fd.SetClass(a,b.opt.zoneClass);b.Hook(a)};fd.FileDrop=function(a,c){var b=this;a=fd.ByID(a);b.handle=new fd.DropHandle(a,c);b.opt=b.handle.opt;fd.Extend(b.opt,{dragOverClass:"over"});fd.Extend(b.opt.iframe,{force:false});fd.Extend(b.opt,c,true);b.on=b.handle.on;fd.Extend(b.on,{send:[],fileSetup:[]});b.SetClassesTo=function(d,e){fd.SetClass(d,b.opt.dragOverClass,e)};b.OnUpload=function(f){var d=!b.opt.iframe.force&&b.GetFilesFrom(f);if(d==false){b.handle.SendViaIFrame()}else{if(d.length>0){fd.CallOf(b,"send",[d])}}};b.GetFilesFrom=function(k){var h=k.dataTransfer?k.dataTransfer:(k.target?k.target.files:false);if(h==false){return false}if(h.files){h=h.files}var d=[];var j={};for(var g=0;g<h.length;g++){var f=new fd.File(h[g]);if(j[f.name]){continue}j[f.name]=true;fd.CallOf(b,"fileSetup",f);if(f.size>0){d.push(f)}}return d};b.on.upload.push(b.OnUpload);b.on.dragEnter.push(function(){b.SetClassesTo(a,true)});b.on.dragLeave.push(function(){b.SetClassesTo(a,false)});b.on.upload.unshift(function(){b.SetClassesTo(a,false)});fd.Extend(b,b.handle)};window.FileDrop=fd.FileDrop;fd.File=function(b){var a=this;a.nativeFile=b;a.name=b.fileName||b.name;a.size=b.fileSize||b.size;a.xhr=null;a.opt={extraHeaders:true};a.on={sendXHR:[],progress:[],done:[],error:[]};a.SendTo=function(d){if(window.FileReader){var c=new FileReader;c.onload=function(f){a.DoSendTo(d,f)};c.onerror=function(f){fd.CallOf(a,"error",[f])};c.readAsBinaryString(a.nativeFile)}else{a.DoSendTo(d)}};a.DoSendTo=function(c,g){a.Abort();a.xhr=fd.NewXHR();a.HookXHR(a.xhr);a.xhr.open("POST",c,true);a.xhr.overrideMimeType("application/octet-stream");a.xhr.setRequestHeader("Content-Type","application/octet-stream");if(a.opt.extraHeaders){a.xhr.setRequestHeader("X-File-Name",encodeURIComponent(a.name));a.xhr.setRequestHeader("X-File-Size",a.size);var d=window.FileReader?"FileAPI":"Chrome";a.xhr.setRequestHeader("X-Requested-With","FileDrop-XHR-"+d)}var f=(g&&g.target&&g.target.result)?g.target.result:a.nativeFile;fd.CallOf(a,"sendXHR",[a.xhr,f])};a.HookXHR=function(d){var c=d.upload||d;d.onreadystatechange=function(h){if(d.readyState==4){try{var g=d.status==200?"done":"error"}catch(h){var g="error"}var f=g=="error"?[h,d]:[d,h];fd.CallOf(a,g,f)}};c.onprogress=function(g){var f=g.lengthComputable?g.loaded:null;fd.CallOf(a,"progress",[f,g.total,d,g])}};a.Abort=function(){if(a.xhr&&a.xhr.abort){a.xhr.abort()}};a.SendXHR=function(d,c){d.sendAsBinary?d.sendAsBinary(c):d.send(c)};a.on.sendXHR.push(a.SendXHR)};