/** * AJAX Upload ( http://valums.com/ajax-upload/ ) * Copyright (c) Andris Valums * Licensed under the MIT license ( http://valums.com/mit-license/ ) * Thanks to Gary Haran, David Mark, Corey Burns and others for contributions. */(function(){function log(){typeof console!="undefined"&&typeof console.log=="function"&&(Array.prototype.unshift.call(arguments,"[Ajax Upload]"),console.log(Array.prototype.join.call(arguments," ")))}function addEvent(e,t,n){if(e.addEventListener)e.addEventListener(t,n,!1);else{if(!e.attachEvent)throw new Error("not supported or DOM not loaded");e.attachEvent("on"+t,function(){n.call(e)})}}function addResizeEvent(e){var t;addEvent(window,"resize",function(){t&&clearTimeout(t),t=setTimeout(e,100)})}function getBox(e){var t,n,r,i,s=getOffset(e);return t=s.left,r=s.top,n=t+e.offsetWidth,i=r+e.offsetHeight,{left:t,right:n,top:r,bottom:i}}function addStyles(e,t){for(var n in t)t.hasOwnProperty(n)&&(e.style[n]=t[n])}function copyLayout(e,t){var n=getBox(e);addStyles(t,{position:"absolute",left:n.left+"px",top:n.top+"px",width:e.offsetWidth+"px",height:e.offsetHeight+"px"}),t.title=e.title}function fileFromPath(e){return e.replace(/.*(\/|\\)/,"")}function getExt(e){return-1!==e.indexOf(".")?e.replace(/.*[.]/,""):""}function hasClass(e,t){var n=new RegExp("\\b"+t+"\\b");return n.test(e.className)}function addClass(e,t){hasClass(e,t)||(e.className+=" "+t)}function removeClass(e,t){var n=new RegExp("\\b"+t+"\\b");e.className=e.className.replace(n,"")}function removeNode(e){e.parentNode.removeChild(e)}if(document.documentElement.getBoundingClientRect)var getOffset=function(e){var t=e.getBoundingClientRect(),n=e.ownerDocument,r=n.body,i=n.documentElement,s=i.clientTop||r.clientTop||0,o=i.clientLeft||r.clientLeft||0,u=1;if(r.getBoundingClientRect){var a=r.getBoundingClientRect();u=(a.right-a.left)/r.clientWidth}u>1&&(s=0,o=0);var f=t.top/u+(window.pageYOffset||i&&i.scrollTop/u||r.scrollTop/u)-s,l=t.left/u+(window.pageXOffset||i&&i.scrollLeft/u||r.scrollLeft/u)-o;return{top:f,left:l}};else var getOffset=function(e){var t=0,n=0;do t+=e.offsetTop||0,n+=e.offsetLeft||0,e=e.offsetParent;while(e);return{left:n,top:t}};var toElement=function(){var e=document.createElement("div");return function(t){e.innerHTML=t;var n=e.firstChild;return e.removeChild(n)}}(),getUID=function(){var e=0;return function(){return"ValumsAjaxUpload"+e++}}();window.AjaxUpload=function(e,t){this._settings={action:"upload.php",name:"userfile",data:{},autoSubmit:!0,responseType:!1,hoverClass:"hover",focusClass:"focus",disabledClass:"disabled",onChange:function(e,t){},onSubmit:function(e,t){},onComplete:function(e,t){}};for(var n in t)t.hasOwnProperty(n)&&(this._settings[n]=t[n]);e.jquery?e=e[0]:typeof e=="string"&&(/^#.*/.test(e)&&(e=e.slice(1)),e=document.getElementById(e));if(!e||e.nodeType!==1)throw new Error("Please make sure that you're passing a valid element");e.nodeName.toUpperCase()=="A"&&addEvent(e,"click",function(e){e&&e.preventDefault?e.preventDefault():window.event&&(window.event.returnValue=!1)}),this._button=e,this._input=null,this._disabled=!1,this.enable(),this._rerouteClicks()},AjaxUpload.prototype={setData:function(e){this._settings.data=e},disable:function(){addClass(this._button,this._settings.disabledClass),this._disabled=!0;var e=this._button.nodeName.toUpperCase();(e=="INPUT"||e=="BUTTON")&&this._button.setAttribute("disabled","disabled"),this._input&&(this._input.parentNode.style.visibility="hidden")},enable:function(){removeClass(this._button,this._settings.disabledClass),this._button.removeAttribute("disabled"),this._disabled=!1},_createInput:function(){var e=this,t=document.createElement("input");t.setAttribute("type","file"),t.setAttribute("name",this._settings.name),addStyles(t,{position:"absolute",right:0,margin:0,padding:0,fontSize:"480px",fontFamily:"sans-serif",cursor:"pointer"});var n=document.createElement("div");addStyles(n,{display:"block",position:"absolute",overflow:"hidden",margin:0,padding:0,opacity:0,direction:"ltr",zIndex:2147483583,cursor:"pointer"});if(n.style.opacity!=="0"){if(typeof n.filters=="undefined")throw new Error("Opacity not supported by the browser");n.style.filter="alpha(opacity=0)"}addEvent(t,"change",function(){if(!t||t.value==="")return;var n=fileFromPath(t.value);if(!1===e._settings.onChange.call(e,n,getExt(n))){e._clearInput();return}e._settings.autoSubmit&&e.submit()}),addEvent(t,"mouseover",function(){addClass(e._button,e._settings.hoverClass)}),addEvent(t,"mouseout",function(){removeClass(e._button,e._settings.hoverClass),removeClass(e._button,e._settings.focusClass),t.parentNode.style.visibility="hidden"}),addEvent(t,"focus",function(){addClass(e._button,e._settings.focusClass)}),addEvent(t,"blur",function(){removeClass(e._button,e._settings.focusClass)}),n.appendChild(t),document.body.appendChild(n),this._input=t},_clearInput:function(){if(!this._input)return;removeNode(this._input.parentNode),this._input=null,this._createInput(),removeClass(this._button,this._settings.hoverClass),removeClass(this._button,this._settings.focusClass)},_rerouteClicks:function(){var e=this;addEvent(e._button,"mouseover",function(){if(e._disabled)return;e._input||e._createInput();var t=e._input.parentNode;copyLayout(e._button,t),t.style.visibility="visible"})},_createIframe:function(){var e=getUID(),t=toElement('');return t.setAttribute("id",e),t.style.display="none",document.body.appendChild(t),t},_createForm:function(e){var t=this._settings,n=toElement('
');n.setAttribute("action",t.action),n.setAttribute("target",e.name),n.style.display="none",document.body.appendChild(n);for(var r in t.data)if(t.data.hasOwnProperty(r)){var i=document.createElement("input");i.setAttribute("type","hidden"),i.setAttribute("name",r),i.setAttribute("value",t.data[r]),n.appendChild(i)}return n},_getResponse:function(iframe,file){var toDeleteFlag=!1,self=this,settings=this._settings;addEvent(iframe,"load",function(){if(iframe.src=="javascript:'%3Chtml%3E%3C/html%3E';"||iframe.src=="javascript:'';"){toDeleteFlag&&setTimeout(function(){removeNode(iframe)},0);return}var doc=iframe.contentDocument?iframe.contentDocument:window.frames[iframe.id].document;if(doc.readyState&&doc.readyState!="complete")return;if(doc.body&&doc.body.innerHTML=="false")return;var response;doc.XMLDocument?response=doc.XMLDocument:doc.body?(response=doc.body.innerHTML,settings.responseType&&settings.responseType.toLowerCase()=="json"&&(doc.body.firstChild&&doc.body.firstChild.nodeName.toUpperCase()=="PRE"&&(doc.normalize(),response=doc.body.firstChild.firstChild.nodeValue),response?response=eval("("+response+")"):response={})):response=doc,settings.onComplete.call(self,file,response),toDeleteFlag=!0,iframe.src="javascript:'';"})},submit:function(){var e=this,t=this._settings;if(!this._input||this._input.value==="")return;var n=fileFromPath(this._input.value);if(!1===t.onSubmit.call(this,n,getExt(n))){this._clearInput();return}var r=this._createIframe(),i=this._createForm(r);removeNode(this._input.parentNode),removeClass(e._button,e._settings.hoverClass),removeClass(e._button,e._settings.focusClass),i.appendChild(this._input),i.submit(),removeNode(i),i=null,removeNode(this._input),this._input=null,this._getResponse(r,n),this._createInput()}}})();