UNPKG

radjs-pointer

Version:
2 lines 7.47 kB
!function(e,t){"function"==typeof define&&define.amd?define(function(){return e.PointerTracker=t()}):"object"==typeof module&&module.exports?module.exports=e.PointerTracker=t():e.PointerTracker=t()}(this,function(){function e(e){var i=!1;if(this.version="1.0.2",this._el=e,this.isDown=!1,this.chancelId=!1,this.enableMultiTouch=!0,this.setMoveHoverState=function(e){i=e},this.getMoveHoverState=function(){return i},window.navigator.msPointerEnabled){var n=this,o=function(e){n.handleEventIE(e)},s=function(e){n.handleEvent(e)};window.navigator.pointerEnabled?(this._el.addEventListener(t.pointerDown,o,!0),this._el.addEventListener(t.pointerMove,o,!0),this._el.addEventListener(t.pointerUp,o,!0),this._el.addEventListener(t.pointerOut,o,!0),this._el.addEventListener(t.pointerLeave,o,!0),this._el.addEventListener(t.pointerEnter,o,!0),this._el.addEventListener(t.pointerOver,o,!0)):(this._el.addEventListener(t.pointerDown,s,!0),this._el.addEventListener(t.pointerMove,s,!0),this._el.addEventListener(t.pointerUp,s,!0),this._el.addEventListener(t.pointerOut,s,!1),this._el.addEventListener(t.pointerLeave,s,!1),this._el.addEventListener(t.pointerEnter,s,!1),this._el.addEventListener(t.pointerOver,s,!0)),this._el.addEventListener(t.pointerCancel,s,!1)}else this.isTouched?(this._el.addEventListener(t.touchstart,this,!1),this._el.addEventListener(t.touchend,this,!1),this._el.addEventListener(t.touchmove,this,!1),this._el.addEventListener(t.touchcancel,this,!1)):(this._el.addEventListener(t.mousedown,this,!1),this._el.addEventListener(t.mouseup,this,!1),this._el.addEventListener(t.mousemove,this,!1),this._el.addEventListener(t.mouseout,this,!1),this._el.addEventListener(t.mouseover,this,!1),this._el.addEventListener(t.mouseleave,this,!1),this._el.addEventListener(t.mouseenter,this,!1));this.destroy=function(){this.isTouched?(this._el.removeEventListener(t.touchstart,this),this._el.removeEventListener(t.touchend,this),this._el.removeEventListener(t.touchmove,this),this._el.removeEventListener(t.touchcancel,this)):(this._el.removeEventListener(t.mousedown,this),this._el.removeEventListener(t.mouseup,this),this._el.removeEventListener(t.mousemove,this),this._el.removeEventListener(t.mouseout,this),this._el.removeEventListener(t.mouseover,this),this._el.removeEventListener(t.mouseleave,this),this._el.removeEventListener(t.mouseenter,this)),delete this._el}}var t={pointerDown:"pointerdown",pointerMove:"pointermove",pointerUp:"pointerup",pointerCancel:"pointercancel",pointerOut:"pointerout",pointerLeave:"pointerleave",pointerEnter:"pointerenter",pointerOver:"pointerover",touchstart:"touchstart",touchmove:"touchmove",touchend:"touchend",touchcancel:"touchcancel",mousedown:"mousedown",mousemove:"mousemove",mouseup:"mouseup",mouseover:"mouseover",mouseout:"mouseout",mouseleave:"mouseleave",mouseenter:"mouseenter"};return window.MSPointerEvent&&!window.PointerEvent&&(t.pointerDown="MSPointerDown",t.pointerMove="MSPointerMove",t.pointerUp="MSPointerUp",t.pointerCancel="MSPointerCancel",t.pointerOut="MSPointerOut",t.pointerLeave="MSPointerLeave",t.pointerEnter="MSPointerEnter",t.pointerOver="MSPointerOver"),e.prototype={EVENTS:{up:"pointerup",down:"pointerdown",move:"pointermove",over:"pointerover",cancel:"pointercancel",out:"pointerout",leave:"pointerleave",enter:"pointerenter"},isPointerHoverEventReceived:!1,isTouched:"ontouchstart"in window||window.navigator.msPointerEnabled,handleEventIE:function(e){if(e.isPrimary)switch(e.type){case t.pointerDown:this.isDown=!0;break;case t.pointerMove:this.getMoveHoverState()||this.isDown||(e.stopImmediatePropagation(),e.stopPropagation(),e.preventDefault());break;case t.pointerUp:case t.pointerCancel:case t.pointerOut:this.isDown=!1}else e.stopImmediatePropagation(),e.stopPropagation(),e.preventDefault()},handleEvent:function(e){switch(null!==this.chancelId&&clearTimeout(this.chancelId),e.type){case t.touchmove:case t.mousemove:case t.pointerMove:(this.getMoveHoverState()||this.isDown)&&this._fireEvent(this.EVENTS.move,e);break;case t.touchstart:case t.mousedown:case t.pointerDown:this.isDown=!0,this.chancelId=!1,this.isPointerHoverEventReceived||(this._fireEvent(this.EVENTS.over,e),this._fireEvent(this.EVENTS.enter,e)),this._fireEvent(this.EVENTS.down,e);break;case t.touchend:case t.pointerUp:case t.touchcancel:case t.mouseup:this.isDown&&(this.isDown=!1,this._fireEvent(this.EVENTS.up,e),this.isPointerHoverEventReceived||(this._fireEvent(this.EVENTS.out,e),this._fireEvent(this.EVENTS.leave,e,!1,!1)));break;case t.mouseover:this.isPointerHoverEventReceived=!0,this._fireEvent(this.EVENTS.over,e);break;case t.pointerEnter:case t.mouseenter:this.isPointerHoverEventReceived=!0,this._fireEvent(this.EVENTS.enter,e,!1,!1);break;case t.pointerLeave:case t.mouseleave:this.isPointerHoverEventReceived=!0,this._fireEvent(this.EVENTS.leave,e,!1,!1);break;case t.pointerOut:case t.mouseout:this.isPointerHoverEventReceived=!0;var i=this;this._fireEvent(this.EVENTS.out,e),this.isDown&&(this.chancelId=setTimeout(function(){i.isDown=!1,i._fireEvent(i.EVENTS.cancel,e,!0,!1),i.chancelId=null},10));break;case t.pointerCancel:i.isDown=!1,i._fireEvent(i.EVENTS.cancel,e,!0,!1),this._fireEvent(this.EVENTS.out,e),this._fireEvent(this.EVENTS.leave,e,!1,!1)}},_fireEvent:function(e,t,i,n){i=arguments.length<3?!0:!!i,n=arguments.length<4?!0:!!n,this.enableMultiTouch?this._fireMultiTouchEvent(e,t,i,n):this._fireSimpleEvent(e,t,i,n)},_fireMultiTouchEvent:function(e,t,i,n){var o,s,r=t;if(this.isTouched)if(window.navigator.msPointerEnabled){if(!t.isPrimary)return!1;this.touchID=t.pointerId}else for(o=0,s=t.changedTouches.length;s>o;o++)r=t.changedTouches[o],this.touchID=r.identifier,this._sendEvent(e,t,i,n,r);else this.touchID=1,this._sendEvent(e,t,i,n,r);return!0},_sendEvent:function(e,t,i,n,o){var s=document.createEvent("MouseEvents");if(s.initMouseEvent(e,i,n,window,1,o.screenX,o.screenY,o.clientX,o.clientY,t.ctrlKey,t.altKey,t.shiftKey,t.metaKey,t.button,t.relatedTarget),s.preventDefault=function(){void 0!==t.preventDefault&&t.preventDefault()},void 0!==s.stopPropagation){var r=s.stopPropagation;s.stopPropagation=function(){void 0!==t.stopPropagation&&t.stopPropagation(),r.call(this)}}return s.pointerId=this.touchID,s.pointerType=this.isTouched?"touch":"mouse",s.isPrimary=!0,s.__defineGetter__&&s.__defineGetter__("timeStamp",function(){return t.timeStamp}),t.target.dispatchEvent(s),!0},_fireSimpleEvent:function(e,i,n,o){var s,r,h,a=i;if(this.isTouched)if(window.navigator.msPointerEnabled){if(!i.isPrimary)return!1;a=i,this.touchID=i.pointerId}else if(i.type===t.touchstart){if(i.touches.length>1)return!1;a=i.touches[0],this.touchID=i.touches[0].identifier}else{for(s=0,r=i.changedTouches.length;r>s&&(a=i.changedTouches[s],a.identifier!==this.touchID);s++);if(a.identifier!==this.touchID)return!1}else this.touchID=1;if(h=document.createEvent("MouseEvents"),h.initMouseEvent(e,n,o,window,1,a.screenX,a.screenY,a.clientX,a.clientY,i.ctrlKey,i.altKey,i.shiftKey,i.metaKey,i.button,i.relatedTarget),h.preventDefault=function(){void 0!==i.preventDefault&&i.preventDefault()},void 0!==h.stopPropagation){var v=h.stopPropagation;h.stopPropagation=function(){void 0!==i.stopPropagation&&i.stopPropagation(),v.call(this)}}return h.pointerId=this.touchID,h.pointerType=this.isTouched?"touch":"mouse",h.isPrimary=!0,h.__defineGetter__&&h.__defineGetter__("timeStamp",function(){return i.timeStamp}),i.target.dispatchEvent(h),!0}},e}); //# sourceMappingURL=release/pointer.min.js.map