UNPKG

jquery-real-tap

Version:

Old but reliable jquery tap.

1 lines 3.52 kB
(function(l,n){"use strict";var r,a,i,c,s,h,u;n.support.touch="ontouchstart"in window||window.DocumentTouch&&l instanceof DocumentTouch,r=10,a=".__tap-helper",i="tap",c=0,s=["clientX","clientY","screenX","screenY","pageX","pageY"],h=function(){var e,t;return e=function(t,n){t!==void 0&&this.init(t,n)},t=e.prototype,t.init=function(n,t){return this.$target=n,this.selector=t.selector,this.data=t.data,this.namespace=a+c++,this.startX=0,this.startY=0,this.isTracking=!1,this.setupHandlers().reset().enable()},t.setupHandlers=function(){return this.onDown=n.proxy(this._onDown,this),this.onMove=n.proxy(this._onMove,this),this.onUp=n.proxy(this._onUp,this),this.onCancel=n.proxy(this._onCancel,this),this},t.enable=function(){return this.$target.on("mousedown"+this.namespace,this.selector,this.onDown),this},t.disable=function(){return this.$target.off("mousedown"+this.namespace,this.selector,this.onDown),this},t.reset=function(){return this.startX=0,this.startY=0,this.isTracking=!1,this},t.destroy=function(){return this.onCancel(),this.disable()},t.getCoordinates=function(t){return[t.clientX,t.clientY]},t._onDown=function(n){if(!this.isTracking){this.isTracking=!0;var t=this.getCoordinates(n);this.startX=t[0],this.startY=t[1],this.$target.on("mousemove"+this.namespace,this.onMove).on("click"+this.namespace,this.selector,this.onUp)}},t._onMove=function(n){var t=this.getCoordinates(n);(Math.abs(t[0]-this.startX)>r||Math.abs(t[1]-this.startY)>r)&&this.onCancel()},t._onUp=function(t){t.isTrigger||t.originalEvent.triggeredTap||(t.originalEvent.triggeredTap=!0,t.type="tap",this.$target.trigger(t)),this.onCancel()},t._onCancel=function(t){this.reset(),this.$target.off("mousemove"+this.namespace,this.onMove).off("click"+this.namespace,this.selector,this.onUp)},e}(),u=function(h){var a,o,t,r;return a=function(o,a){var t,r,h,e;for(t=0,r=s.length,h=o.originalEvent.changedTouches[0],e=n.Event(i,o),e.type=i,e.data=a;r>t;t++)e[s[t]]=h[s[t]];return e},o=function(t,n){t!==void 0&&this.init(t,n)},t=o.prototype=new h,t.constructor=o,r=h.prototype,t.init=function(t,n){return this.touchStartCount=0,r.init.call(this,t,n)},t.enable=function(){return this.$target.on("touchstart"+this.namespace,this.selector,this.onDown),this},t.disable=function(){return this.$target.off("touchstart"+this.namespace,this.selector,this.onDown),this},t.reset=function(){return this.touchStartCount=0,r.reset.call(this)},t.getCoordinates=function(t){return[t.originalEvent.touches[0].clientX,t.originalEvent.touches[0].clientY]},t._onDown=function(t){this.touchStartCount=t.originalEvent.touches.length,this.isTracking||(this.isTracking=!0,this.startX=t.originalEvent.touches[0].clientX,this.startY=t.originalEvent.touches[0].clientY,this.$target.on("touchmove"+this.namespace,this.selector,this.onMove).on("touchend"+this.namespace,this.selector,this.onUp).on("touchcancel"+this.namespace,this.selector,this.onCancel))},t._onUp=function(t){!this.touchStartCount||t.originalEvent.touches.length>0||this.touchStartCount>1||(t.originalEvent.triggeredTap||(t.originalEvent.triggeredTap=!0,this.$target.trigger(a(t,this.data))),this.onCancel())},t._onCancel=function(t){this.reset(),this.$target.off("touchmove"+this.namespace,this.selector,this.onMove).off("touchend"+this.namespace,this.selector,this.onUp).off("touchcancel"+this.namespace,this.selector,this.onCancel)},o}(h),n.event.special[i]={add:function(t){var e=n.support.touch?u:h;t.tap=new e(n(this),t)},remove:function(t){t.tap&&t.tap.destroy&&t.tap.destroy()}}})(document,jQuery)