Skip to content

Instantly share code, notes, and snippets.

@mailopl
Created December 11, 2013 21:16
Show Gist options
  • Save mailopl/7918587 to your computer and use it in GitHub Desktop.
Save mailopl/7918587 to your computer and use it in GitHub Desktop.
function FastClick(b){var c,a=this;this.trackingClick=false;this.trackingClickStart=0;this.targetElement=null;this.touchStartX=0;this.touchStartY=0;this.lastTouchIdentifier=0;this.touchBoundary=10;this.layer=b;if(!b||!b.nodeType){throw new TypeError("Layer must be a document node");}this.onClick=function(){return FastClick.prototype.onClick.apply(a,arguments);};this.onMouse=function(){return FastClick.prototype.onMouse.apply(a,arguments);};this.onTouchStart=function(){return FastClick.prototype.onTouchStart.apply(a,arguments);};this.onTouchMove=function(){return FastClick.prototype.onTouchMove.apply(a,arguments);};this.onTouchEnd=function(){return FastClick.prototype.onTouchEnd.apply(a,arguments);};this.onTouchCancel=function(){return FastClick.prototype.onTouchCancel.apply(a,arguments);};if(FastClick.notNeeded(b)){return;}if(this.deviceIsAndroid){b.addEventListener("mouseover",this.onMouse,true);b.addEventListener("mousedown",this.onMouse,true);b.addEventListener("mouseup",this.onMouse,true);}b.addEventListener("click",this.onClick,true);b.addEventListener("touchstart",this.onTouchStart,false);b.addEventListener("touchmove",this.onTouchMove,false);b.addEventListener("touchend",this.onTouchEnd,false);b.addEventListener("touchcancel",this.onTouchCancel,false);if(!Event.prototype.stopImmediatePropagation){b.removeEventListener=function(e,g,d){var f=Node.prototype.removeEventListener;if(e==="click"){f.call(b,e,g.hijacked||g,d);}else{f.call(b,e,g,d);}};b.addEventListener=function(f,g,e){var d=Node.prototype.addEventListener;if(f==="click"){d.call(b,f,g.hijacked||(g.hijacked=function(h){if(!h.propagationStopped){g(h);}}),e);}else{d.call(b,f,g,e);}};}if(typeof b.onclick==="function"){c=b.onclick;b.addEventListener("click",function(d){c(d);},false);b.onclick=null;}}FastClick.prototype.deviceIsAndroid=navigator.userAgent.indexOf("Android")>0;FastClick.prototype.deviceIsIOS=/iP(ad|hone|od)/.test(navigator.userAgent);FastClick.prototype.deviceIsIOS4=FastClick.prototype.deviceIsIOS&&(/OS 4_\d(_\d)?/).test(navigator.userAgent);FastClick.prototype.deviceIsIOSWithBadTarget=FastClick.prototype.deviceIsIOS&&(/OS ([6-9]|\d{2})_\d/).test(navigator.userAgent);FastClick.prototype.needsClick=function(a){switch(a.nodeName.toLowerCase()){case"button":case"select":case"textarea":if(a.disabled){return true;}break;case"input":if((this.deviceIsIOS&&a.type==="file")||a.disabled){return true;}break;case"label":case"video":return true;}return(/\bneedsclick\b/).test(a.className);};FastClick.prototype.needsFocus=function(a){switch(a.nodeName.toLowerCase()){case"textarea":return true;case"select":return !this.deviceIsAndroid;case"input":switch(a.type){case"button":case"checkbox":case"file":case"image":case"radio":case"submit":return false;}return !a.disabled&&!a.readOnly;default:return(/\bneedsfocus\b/).test(a.className);}};FastClick.prototype.sendClick=function(b,c){var a,d;if(document.activeElement&&document.activeElement!==b){document.activeElement.blur();}d=c.changedTouches[0];a=document.createEvent("MouseEvents");a.initMouseEvent(this.determineEventType(b),true,true,window,1,d.screenX,d.screenY,d.clientX,d.clientY,false,false,false,false,0,null);a.forwardedTouchEvent=true;b.dispatchEvent(a);};FastClick.prototype.determineEventType=function(a){if(this.deviceIsAndroid&&a.tagName.toLowerCase()==="select"){return"mousedown";}return"click";};FastClick.prototype.focus=function(a){var b;if(this.deviceIsIOS&&a.setSelectionRange&&a.type.indexOf("date")!==0&&a.type!=="time"){b=a.value.length;a.setSelectionRange(b,b);}else{a.focus();}};FastClick.prototype.updateScrollParent=function(b){var c,a;c=b.fastClickScrollParent;if(!c||!c.contains(b)){a=b;do{if(a.scrollHeight>a.offsetHeight){c=a;b.fastClickScrollParent=a;break;}a=a.parentElement;}while(a);}if(c){c.fastClickLastScrollTop=c.scrollTop;}};FastClick.prototype.getTargetElementFromEventTarget=function(a){if(a.nodeType===Node.TEXT_NODE){return a.parentNode;}return a;};FastClick.prototype.onTouchStart=function(c){var a,d,b;if(c.targetTouches.length>1){return true;}a=this.getTargetElementFromEventTarget(c.target);d=c.targetTouches[0];if(this.deviceIsIOS){b=window.getSelection();if(b.rangeCount&&!b.isCollapsed){return true;}if(!this.deviceIsIOS4){if(d.identifier===this.lastTouchIdentifier){c.preventDefault();return false;}this.lastTouchIdentifier=d.identifier;this.updateScrollParent(a);}}this.trackingClick=true;this.trackingClickStart=c.timeStamp;this.targetElement=a;this.touchStartX=d.pageX;this.touchStartY=d.pageY;if((c.timeStamp-this.lastClickTime)<200){c.preventDefault();}return true;};FastClick.prototype.touchHasMoved=function(a){var c=a.changedTouches[0],b=this.touchBoundary;if(Math.abs(c.pageX-this.touchStartX)>b||Math.abs(c.pageY-this.touchStartY)>b){return true;}return false;};FastClick.prototype.onTouchMove=function(a){if(!this.trackingClick){return true;}if(this.targetElement!==this.getTargetElementFromEventTarget(a.target)||this.touchHasMoved(a)){this.trackingClick=false;this.targetElement=null;}return true;};FastClick.prototype.findControl=function(a){if(a.control!==undefined){return a.control;}if(a.htmlFor){return document.getElementById(a.htmlFor);}return a.querySelector("button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea");};FastClick.prototype.onTouchEnd=function(c){var e,d,b,g,f,a=this.targetElement;if(!this.trackingClick){return true;}if((c.timeStamp-this.lastClickTime)<200){this.cancelNextClick=true;return true;}this.cancelNextClick=false;this.lastClickTime=c.timeStamp;d=this.trackingClickStart;this.trackingClick=false;this.trackingClickStart=0;if(this.deviceIsIOSWithBadTarget){f=c.changedTouches[0];a=document.elementFromPoint(f.pageX-window.pageXOffset,f.pageY-window.pageYOffset)||a;a.fastClickScrollParent=this.targetElement.fastClickScrollParent;}b=a.tagName.toLowerCase();if(b==="label"){e=this.findControl(a);if(e){this.focus(a);if(this.deviceIsAndroid){return false;}a=e;}}else{if(this.needsFocus(a)){if((c.timeStamp-d)>100||(this.deviceIsIOS&&window.top!==window&&b==="input")){this.targetElement=null;return false;}this.focus(a);if(!this.deviceIsIOS4||b!=="select"){this.targetElement=null;c.preventDefault();}return false;}}if(this.deviceIsIOS&&!this.deviceIsIOS4){g=a.fastClickScrollParent;if(g&&g.fastClickLastScrollTop!==g.scrollTop){return true;}}if(!this.needsClick(a)){c.preventDefault();this.sendClick(a,c);}return false;};FastClick.prototype.onTouchCancel=function(){this.trackingClick=false;this.targetElement=null;};FastClick.prototype.onMouse=function(a){if(!this.targetElement){return true;}if(a.forwardedTouchEvent){return true;}if(!a.cancelable){return true;}if(!this.needsClick(this.targetElement)||this.cancelNextClick){if(a.stopImmediatePropagation){a.stopImmediatePropagation();}else{a.propagationStopped=true;}a.stopPropagation();a.preventDefault();return false;}return true;};FastClick.prototype.onClick=function(a){var b;if(this.trackingClick){this.targetElement=null;this.trackingClick=false;return true;}if(a.target.type==="submit"&&a.detail===0){return true;}b=this.onMouse(a);if(!b){this.targetElement=null;}return b;};FastClick.prototype.destroy=function(){var a=this.layer;if(this.deviceIsAndroid){a.removeEventListener("mouseover",this.onMouse,true);a.removeEventListener("mousedown",this.onMouse,true);a.removeEventListener("mouseup",this.onMouse,true);}a.removeEventListener("click",this.onClick,true);a.removeEventListener("touchstart",this.onTouchStart,false);a.removeEventListener("touchmove",this.onTouchMove,false);a.removeEventListener("touchend",this.onTouchEnd,false);a.removeEventListener("touchcancel",this.onTouchCancel,false);};FastClick.notNeeded=function(b){var a;var c;if(typeof window.ontouchstart==="undefined"){return true;}c=+(/Chrome\/([0-9]+)/.exec(navigator.userAgent)||[,0])[1];if(c){if(FastClick.prototype.deviceIsAndroid){a=document.querySelector("meta[name=viewport]");if(a){if(a.content.indexOf("user-scalable=no")!==-1){return true;}if(c>31&&window.innerWidth<=window.screen.width){return true;}}}else{return true;}}if(b.style.msTouchAction==="none"){return true;}return false;};FastClick.attach=function(a){return new FastClick(a);};if(typeof define!=="undefined"&&define.amd){define(function(){return FastClick;});}else{if(typeof module!=="undefined"&&module.exports){module.exports=FastClick.attach;module.exports.FastClick=FastClick;}else{window.FastClick=FastClick;}}(function(t,v,s){var l=["top","right","bottom","left","opacity","height","width"],u=["top","right","bottom","left"],p=["-webkit-","-moz-","-o-",""],y=["avoidTransforms","useTranslate3d","leaveTransforms"],j=/^([+-]=)?([\d+-.]+)(.*)$/,D=/([A-Z])/g,z={secondary:{},meta:{top:0,right:0,bottom:0,left:0}},g="px",o="jQe",d="cubic-bezier(",C=")",c=null,x=false;var m=document.body||document.documentElement,f=m.style,b="webkitTransitionEnd oTransitionEnd transitionend",B=f.WebkitTransition!==undefined||f.MozTransition!==undefined||f.OTransition!==undefined||f.transition!==undefined,i=("WebKitCSSMatrix" in window&&"m11" in new WebKitCSSMatrix()),h=i;if(t.expr&&t.expr.filters){c=t.expr.filters.animated;t.expr.filters.animated=function(H){return t(H).data("events")&&t(H).data("events")[b]?true:c.call(this,H);};}function G(H){return H.match(/\D+$/);}function k(R,L,I,M){if(I=="d"){return;}if(!a(R)){return;}var O=j.exec(L),J=R.css(I)==="auto"?0:R.css(I),S=typeof J=="string"?E(J):J,K=typeof L=="string"?E(L):L,Q=M===true?0:S,P=R.is(":hidden"),H=R.translation();if(I=="left"){Q=parseInt(S,10)+H.x;}if(I=="right"){Q=parseInt(S,10)+H.x;}if(I=="top"){Q=parseInt(S,10)+H.y;}if(I=="bottom"){Q=parseInt(S,10)+H.y;}if(!O&&L=="show"){Q=1;if(P){elem=R[0];if(elem.style){display=elem.style.display;if(!t._data(elem,"olddisplay")&&display==="none"){display=elem.style.display="";}if(display===""&&t.css(elem,"display")==="none"){t._data(elem,"olddisplay",A(elem.context.tagName));}if(display===""||display==="none"){elem.style.display=t._data(elem,"olddisplay")||"";}}R.css("opacity",0);}}else{if(!O&&L=="hide"){Q=0;}}if(O){var N=parseFloat(O[2]);if(O[1]){N=((O[1]==="-="?-1:1)*N)+parseInt(Q,10);}if(O[3]&&O[3]!="px"){N=N+O[3];}return N;}else{return Q;}}function w(H,J,I){return((I===true||((h===true&&I!==false))&&i))?"translate3d("+H+"px, "+J+"px, 0)":"translate("+H+"px,"+J+"px)";}function n(P,U,N,Q,T,J,M,H){var I=P.data(o),O=I&&!e(I)?I:t.extend(true,{},z),L=T,S=t.inArray(U,u)>-1;if(S){var V=O.meta,K=E(P.css(U))||0,R=U+"_o";L=T-K;V[U]=L;V[R]=P.css(U)=="auto"?0+L:K+L||0;O.meta=V;if(M&&L===0){L=0-V[R];V[U]=L;V[R]=0;}}return P.data(o,F(P,O,U,N,Q,L,J,M,H));}function F(Z,T,M,H,V,W,N,K,J){var R=false,Q=N===true&&K===true;T=T||{};if(!T.original){T.original={};R=true;}T.properties=T.properties||{};T.secondary=T.secondary||{};var S=T.meta,X=T.original,O=T.properties,U=T.secondary;for(var Y=p.length-1;Y>=0;Y--){var I=p[Y]+"transition-property",P=p[Y]+"transition-duration",L=p[Y]+"transition-timing-function";M=(Q?p[Y]+"transform":M);if(R){X[I]=Z.css(I)||"";X[P]=Z.css(P)||"";X[L]=Z.css(L)||"";}U[M]=Q?w(S.left,S.top,J):W;O[I]=(O[I]?O[I]+",":"")+M;O[P]=(O[P]?O[P]+",":"")+H+"ms";O[L]=(O[L]?O[L]+",":"")+V;}return T;}function q(I){for(var H in I){if((H=="width"||H=="height")&&(I[H]=="show"||I[H]=="hide"||I[H]=="toggle")){return true;}}return false;}function e(I){for(var H in I){return false;}return true;}function A(H){H=H.toUpperCase();var I={LI:"list-item",TR:"table-row",TD:"table-cell",TH:"table-cell",CAPTION:"table-caption",COL:"table-column",COLGROUP:"table-column-group",TFOOT:"table-footer-group",THEAD:"table-header-group",TBODY:"table-row-group"};return typeof I[H]=="string"?I[H]:"block";}function E(H){return parseFloat(H.replace(G(H),""));}function a(H){var I=true;H.each(function(J,K){I=I&&K.ownerDocument;return I;});return I;}function r(K,J,H){if(!a(H)){return false;}var I=t.inArray(K,l)>-1;if((K=="width"||K=="height"||K=="opacity")&&(parseFloat(J)===parseFloat(H.css(K)))){I=false;}return I;}t.extend({toggle3DByDefault:function(){return h=!h;},toggleDisabledByDefault:function(){return x=!x;},setDisabledByDefault:function(H){return x=H;}});t.fn.translation=function(){if(!this[0]){return null;}var L=this[0],I=window.getComputedStyle(L,null),M={x:0,y:0};if(I){for(var K=p.length-1;K>=0;K--){var J=I.getPropertyValue(p[K]+"transform");if(J&&(/matrix/i).test(J)){var H=J.replace(/^matrix\(/i,"").split(/, |\)$/g);M={x:parseInt(H[4],10),y:parseInt(H[5],10)};break;}}}return M;};t.fn.animate=function(H,I,N,P){H=H||{};var J=!(typeof H.bottom!=="undefined"||typeof H.right!=="undefined"),O=t.speed(I,N,P),M=0,L=function(){M--;if(M===0){if(typeof O.complete==="function"){O.complete.apply(this,arguments);}}},K=(typeof H.avoidCSSTransitions!=="undefined")?H.avoidCSSTransitions:x;if(K===true||!B||e(H)||q(H)||O.duration<=0||O.step){return v.apply(this,arguments);}return this[O.queue===true?"queue":"each"](function(){var aa=t(this),R=t.extend({},O),W=function(ag){var af=aa.data(o)||{original:{}},ae={};if(ag.eventPhase!=2){return;}if(H.leaveTransforms!==true){for(var ad=p.length-1;ad>=0;ad--){ae[p[ad]+"transform"]="";}if(J&&typeof af.meta!=="undefined"){for(var ac=0,ab;(ab=u[ac]);++ac){ae[ab]=af.meta[ab+"_o"]+g;t(this).css(ab,ae[ab]);}}}aa.unbind(b).css(af.original).css(ae).data(o,null);if(H.opacity==="hide"){elem=aa[0];if(elem.style){display=t.css(elem,"display");if(display!=="none"&&!t._data(elem,"olddisplay")){t._data(elem,"olddisplay",display);}elem.style.display="none";}aa.css("opacity","");}L.call(this);},T={bounce:d+"0.0, 0.35, .5, 1.3"+C,linear:"linear",swing:"ease-in-out",easeInQuad:d+"0.550, 0.085, 0.680, 0.530"+C,easeInCubic:d+"0.550, 0.055, 0.675, 0.190"+C,easeInQuart:d+"0.895, 0.030, 0.685, 0.220"+C,easeInQuint:d+"0.755, 0.050, 0.855, 0.060"+C,easeInSine:d+"0.470, 0.000, 0.745, 0.715"+C,easeInExpo:d+"0.950, 0.050, 0.795, 0.035"+C,easeInCirc:d+"0.600, 0.040, 0.980, 0.335"+C,easeInBack:d+"0.600, -0.280, 0.735, 0.045"+C,easeOutQuad:d+"0.250, 0.460, 0.450, 0.940"+C,easeOutCubic:d+"0.215, 0.610, 0.355, 1.000"+C,easeOutQuart:d+"0.165, 0.840, 0.440, 1.000"+C,easeOutQuint:d+"0.230, 1.000, 0.320, 1.000"+C,easeOutSine:d+"0.390, 0.575, 0.565, 1.000"+C,easeOutExpo:d+"0.190, 1.000, 0.220, 1.000"+C,easeOutCirc:d+"0.075, 0.820, 0.165, 1.000"+C,easeOutBack:d+"0.175, 0.885, 0.320, 1.275"+C,easeInOutQuad:d+"0.455, 0.030, 0.515, 0.955"+C,easeInOutCubic:d+"0.645, 0.045, 0.355, 1.000"+C,easeInOutQuart:d+"0.770, 0.000, 0.175, 1.000"+C,easeInOutQuint:d+"0.860, 0.000, 0.070, 1.000"+C,easeInOutSine:d+"0.445, 0.050, 0.550, 0.950"+C,easeInOutExpo:d+"1.000, 0.000, 0.000, 1.000"+C,easeInOutCirc:d+"0.785, 0.135, 0.150, 0.860"+C,easeInOutBack:d+"0.680, -0.550, 0.265, 1.550"+C},V={},U=T[R.easing||"swing"]?T[R.easing||"swing"]:R.easing||"swing";for(var Q in H){if(t.inArray(Q,y)===-1){var X=t.inArray(Q,u)>-1,Z=k(aa,H[Q],Q,(X&&H.avoidTransforms!==true));if(r(Q,Z,aa)){n(aa,Q,R.duration,U,Z,X&&H.avoidTransforms!==true,J,H.useTranslate3d);}else{V[Q]=H[Q];}}}aa.unbind(b);var S=aa.data(o);if(S&&!e(S)&&!e(S.secondary)){M++;aa.css(S.properties);var Y=S.secondary;setTimeout(function(){aa.bind(b,W).css(Y);});}else{R.queue=false;}if(!e(V)){M++;v.apply(aa,[V,{duration:R.duration,easing:t.easing[R.easing]?R.easing:(t.easing.swing?"swing":"linear"),complete:L,queue:R.queue}]);}return true;});};t.fn.animate.defaults={};t.fn.stop=function(J,H,I){if(!B){return s.apply(this,[J,H]);}if(J){this.queue([]);}this.each(function(){var M=t(this),P=M.data(o);if(P&&!e(P)){var N,O={};if(H){O=P.secondary;if(!I&&typeof P.meta.left_o!==undefined||typeof P.meta.top_o!==undefined){O.left=typeof P.meta.left_o!==undefined?P.meta.left_o:"auto";O.top=typeof P.meta.top_o!==undefined?P.meta.top_o:"auto";for(N=p.length-1;N>=0;N--){O[p[N]+"transform"]="";}}}else{if(!e(P.secondary)){var L=window.getComputedStyle(M[0],null);if(L){for(var Q in P.secondary){if(P.secondary.hasOwnProperty(Q)){Q=Q.replace(D,"-$1").toLowerCase();O[Q]=L.getPropertyValue(Q);if(!I&&(/matrix/i).test(O[Q])){var K=O[Q].replace(/^matrix\(/i,"").split(/, |\)$/g);O.left=(parseFloat(K[4])+parseFloat(M.css("left"))+g)||"auto";O.top=(parseFloat(K[5])+parseFloat(M.css("top"))+g)||"auto";for(N=p.length-1;N>=0;N--){O[p[N]+"transform"]="";}}}}}}}M.unbind(b);M.css(P.original).css(O).data(o,null);}else{s.apply(M,[J,H]);}});return this;};})(jQuery,jQuery.fn.animate,jQuery.fn.stop);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment