fastclick.min.1.0.js 7.64 KB
function FastClick(t){"use strict";var e,i=this;this.trackingClick=false;this.trackingClickStart=0;this.targetElement=null;this.touchStartX=0;this.touchStartY=0;this.lastTouchIdentifier=0;this.touchBoundary=10;this.layer=t;if(!t||!t.nodeType){throw new TypeError("Layer must be a document node")}this.onClick=function(){return FastClick.prototype.onClick.apply(i,arguments)};this.onMouse=function(){return FastClick.prototype.onMouse.apply(i,arguments)};this.onTouchStart=function(){return FastClick.prototype.onTouchStart.apply(i,arguments)};this.onTouchEnd=function(){return FastClick.prototype.onTouchEnd.apply(i,arguments)};this.onTouchCancel=function(){return FastClick.prototype.onTouchCancel.apply(i,arguments)};if(FastClick.notNeeded(t)){return}if(this.deviceIsAndroid){t.addEventListener("mouseover",this.onMouse,true);t.addEventListener("mousedown",this.onMouse,true);t.addEventListener("mouseup",this.onMouse,true)}t.addEventListener("click",this.onClick,true);t.addEventListener("touchstart",this.onTouchStart,false);t.addEventListener("touchend",this.onTouchEnd,false);t.addEventListener("touchcancel",this.onTouchCancel,false);if(!Event.prototype.stopImmediatePropagation){t.removeEventListener=function(e,i,n){var r=Node.prototype.removeEventListener;if(e==="click"){r.call(t,e,i.hijacked||i,n)}else{r.call(t,e,i,n)}};t.addEventListener=function(e,i,n){var r=Node.prototype.addEventListener;if(e==="click"){r.call(t,e,i.hijacked||(i.hijacked=function(t){if(!t.propagationStopped){i(t)}}),n)}else{r.call(t,e,i,n)}}}if(typeof t.onclick==="function"){e=t.onclick;t.addEventListener("click",function(t){e(t)},false);t.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(t){"use strict";switch(t.nodeName.toLowerCase()){case"button":case"select":case"textarea":if(t.disabled){return true}break;case"input":if(this.deviceIsIOS&&t.type==="file"||t.disabled){return true}break;case"label":case"video":return true}return/\bneedsclick\b/.test(t.className)};FastClick.prototype.needsFocus=function(t){"use strict";switch(t.nodeName.toLowerCase()){case"textarea":case"select":return true;case"input":switch(t.type){case"button":case"checkbox":case"file":case"image":case"radio":case"submit":return false}return!t.disabled&&!t.readOnly;default:return/\bneedsfocus\b/.test(t.className)}};FastClick.prototype.sendClick=function(t,e){"use strict";var i,n;if(document.activeElement&&document.activeElement!==t){document.activeElement.blur()}n=e.changedTouches[0];i=document.createEvent("MouseEvents");i.initMouseEvent("click",true,true,window,1,n.screenX,n.screenY,n.clientX,n.clientY,false,false,false,false,0,null);i.forwardedTouchEvent=true;t.dispatchEvent(i)};FastClick.prototype.focus=function(t){"use strict";var e;if(this.deviceIsIOS&&t.setSelectionRange){e=t.value.length;t.setSelectionRange(e,e)}else{t.focus()}};FastClick.prototype.updateScrollParent=function(t){"use strict";var e,i;e=t.fastClickScrollParent;if(!e||!e.contains(t)){i=t;do{if(i.scrollHeight>i.offsetHeight){e=i;t.fastClickScrollParent=i;break}i=i.parentElement}while(i)}if(e){e.fastClickLastScrollTop=e.scrollTop}};FastClick.prototype.getTargetElementFromEventTarget=function(t){"use strict";if(t.nodeType===Node.TEXT_NODE){return t.parentNode}return t};FastClick.prototype.onTouchStart=function(t){"use strict";var e,i,n;if(t.targetTouches.length>1){return true}e=this.getTargetElementFromEventTarget(t.target);i=t.targetTouches[0];if(this.deviceIsIOS){n=window.getSelection();if(n.rangeCount&&!n.isCollapsed){return true}if(!this.deviceIsIOS4){if(i.identifier===this.lastTouchIdentifier){t.preventDefault();return false}this.lastTouchIdentifier=i.identifier;this.updateScrollParent(e)}}this.trackingClick=true;this.trackingClickStart=t.timeStamp;this.targetElement=e;this.touchStartX=i.pageX;this.touchStartY=i.pageY;if(t.timeStamp-this.lastClickTime<200){t.preventDefault()}return true};FastClick.prototype.touchHasMoved=function(t){"use strict";var e=t.changedTouches[0],i=this.touchBoundary;if(Math.abs(e.pageX-this.touchStartX)>i||Math.abs(e.pageY-this.touchStartY)>i){return true}return false};FastClick.prototype.findControl=function(t){"use strict";if(t.control!==undefined){return t.control}if(t.htmlFor){return document.getElementById(t.htmlFor)}return t.querySelector("button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea")};FastClick.prototype.onTouchEnd=function(t){"use strict";var e,i,n,r,s,o=this.targetElement;if(this.touchHasMoved(t)){this.trackingClick=false;this.targetElement=null}if(!this.trackingClick){return true}if(t.timeStamp-this.lastClickTime<200){this.cancelNextClick=true;return true}this.lastClickTime=t.timeStamp;i=this.trackingClickStart;this.trackingClick=false;this.trackingClickStart=0;if(this.deviceIsIOSWithBadTarget){s=t.changedTouches[0];o=document.elementFromPoint(s.pageX-window.pageXOffset,s.pageY-window.pageYOffset)||o;o.fastClickScrollParent=this.targetElement.fastClickScrollParent}n=o.tagName.toLowerCase();if(n==="label"){e=this.findControl(o);if(e){this.focus(o);if(this.deviceIsAndroid){return false}o=e}}else if(this.needsFocus(o)){if(t.timeStamp-i>100||this.deviceIsIOS&&window.top!==window&&n==="input"){this.targetElement=null;return false}this.focus(o);if(!this.deviceIsIOS4||n!=="select"){this.targetElement=null;t.preventDefault()}return false}if(this.deviceIsIOS&&!this.deviceIsIOS4){r=o.fastClickScrollParent;if(r&&r.fastClickLastScrollTop!==r.scrollTop){return true}}if(!this.needsClick(o)){t.preventDefault();this.sendClick(o,t)}return false};FastClick.prototype.onTouchCancel=function(){"use strict";this.trackingClick=false;this.targetElement=null};FastClick.prototype.onMouse=function(t){"use strict";if(!this.targetElement){return true}if(t.forwardedTouchEvent){return true}if(!t.cancelable){return true}if(!this.needsClick(this.targetElement)||this.cancelNextClick){if(t.stopImmediatePropagation){t.stopImmediatePropagation()}else{t.propagationStopped=true}t.stopPropagation();t.preventDefault();return false}return true};FastClick.prototype.onClick=function(t){"use strict";var e;if(this.trackingClick){this.targetElement=null;this.trackingClick=false;return true}if(t.target.type==="submit"&&t.detail===0){return true}e=this.onMouse(t);if(!e){this.targetElement=null}return e};FastClick.prototype.destroy=function(){"use strict";var t=this.layer;if(this.deviceIsAndroid){t.removeEventListener("mouseover",this.onMouse,true);t.removeEventListener("mousedown",this.onMouse,true);t.removeEventListener("mouseup",this.onMouse,true)}t.removeEventListener("click",this.onClick,true);t.removeEventListener("touchstart",this.onTouchStart,false);t.removeEventListener("touchend",this.onTouchEnd,false);t.removeEventListener("touchcancel",this.onTouchCancel,false)};FastClick.notNeeded=function(t){"use strict";var e;if(typeof window.ontouchstart==="undefined"){return true}if(/Chrome\/[0-9]+/.test(navigator.userAgent)){if(FastClick.prototype.deviceIsAndroid){e=document.querySelector("meta[name=viewport]");if(e&&e.content.indexOf("user-scalable=no")!==-1){return true}}else{return true}}if(t.style.msTouchAction==="none"){return true}return false};FastClick.attach=function(t){"use strict";return new FastClick(t)};if(typeof define!=="undefined"&&define.amd){define(function(){"use strict";return FastClick})}else if(typeof module!=="undefined"&&module.exports){module.exports=FastClick.attach;module.exports.FastClick=FastClick}else{window.FastClick=FastClick}