friendica/js/ajaxupload.min.js

6 wiersze
7.2 KiB
JavaScript
Czysty Zwykły widok Historia

/**
* 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,g