-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.event.pointer.min.js
More file actions
9 lines (9 loc) · 7.73 KB
/
jquery.event.pointer.min.js
File metadata and controls
9 lines (9 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
/*!
* Polyfill and normalization support for pointer events, based on jQuery's special event system.
* https://github.com/NetMatch/jquery-event-pointer
*
* Copyright 2014 NetMatch
* Released under the MIT license
* https://github.com/NetMatch/jquery-event-pointer/blob/master/LICENSE
*/
(function(d){"function"===typeof define&&define.amd?define(["jquery"],d):d(window.jQuery)})(function(d){var k="screenX screenY clientX clientY ctrlKey shiftKey altKey metaKey relatedTarget detail button buttons pointerId pointerType width height pressure tiltX tiltY isPrimary".split(" "),m={0:1,1:4,2:2};d.each({pointerenter:"pointerover",pointerleave:"pointerout"},function(a,b){d.event.special[a]={bindType:b,delegateType:b,handle:function(a){var l=a.relatedTarget,e=a.handleObj,g;if(!l||l!==this&& !d.contains(this,l))a.type=e.origType,g=e.handler.apply(this,arguments),a.type=b;return g}}});if(!0===window.navigator.pointerEnabled)d.each("pointerdown pointerup pointercancel pointermove pointerover pointerout".split(" "),function(a,b){d.event.fixHooks[b]={props:k}});else if(!0===window.navigator.msPointerEnabled)d.each({pointerdown:"MSPointerDown",pointerup:"MSPointerUp",pointercancel:"MSPointerCancel",pointermove:"MSPointerMove",pointerover:"MSPointerOver",pointerout:"MSPointerOut"},function(a, b){d.event.special[a]={bindType:b,delegateType:b,handle:function(a){var d=a.handleObj;a.type=d.origType;d=d.handler.apply(this,arguments);a.type=b;return d}};d.event.fixHooks[b]={props:k,filter:function(b,a){b.pointerType={2:"touch",3:"pen",4:"mouse"}[a.pointerType]||a.pointerType;b.pressure=a.pressure||(b.buttons?0.5:0);return b}}}),d.each({pointerenter:"MSPointerOver",pointerleave:"MSPointerOut"},function(a,b){d.event.special[a].bindType=b;d.event.special[a].delegateType=b});else{var f=function(a){this._document= a;this._refCounters={all:0};this._activePointers={length:0};void 0===a.ontouchstart&&(this._isSimulatedMouseEvent=function(b){return!1})};d.extend(f,{recentTouchStarts:[],typemap:{pointerdown:{mousedown:"_proxyMouseEvent",touchstart:"_proxyTouchStartEvent"},pointerup:{mouseup:"_proxyMouseEvent",touchend:"_proxyTouchEndEvent"},pointermove:{mousemove:"_proxyMouseEvent",touchstart:"_proxyTouchStartEvent",touchmove:"_proxyTouchMoveEvent",touchend:"_proxyTouchEndEvent"},pointerover:{mouseover:"_proxyMouseEvent", touchstart:"_proxyTouchStartEvent",touchmove:"_proxyTouchMoveEvent"},pointerout:{mouseout:"_proxyMouseEvent",touchmove:"_proxyTouchMoveEvent",touchend:"_proxyTouchEndEvent",touchcancel:"_proxyTouchCancelEvent"},pointercancel:{touchcancel:"_proxyTouchCancelEvent"}},bind:function(a,b){var c=d.data(a);c.__pointerproxy instanceof f||(c.__pointerproxy=new f(a));c.__pointerproxy.bind(b)},unbind:function(a,b){var c=d.data(a);c.__pointerproxy instanceof f&&(c.__pointerproxy.unbind(b),c.__pointerproxy.isBound()&& delete c.__pointerproxy)}});f.prototype={constructor:f,isBound:function(){return 0===this._refCounters.all},bind:function(a){var b=this,c=b._refCounters;c[a]=(c[a]||0)+1;c.all+=1;d.each(f.typemap[a],function(a,e){c[a]||(c[a]=0,d.event.add(b._document,a,d.proxy(b[e],b),null,null));c[a]+=1})},unbind:function(a){var b=this,c=b._refCounters;c[a]&&(c[a]-=1,c.all-=1);d.each(f.typemap[a],function(a,e){c[a]&&(1===c[a]&&d.event.remove(b._document,a,b[e],null,null),c[a]-=1)})},_proxyMouseEvent:function(a){var b= a.type.replace(/^mouse/,"pointer");this._shouldDispatch(b)&&!this._isSimulatedMouseEvent(a)&&(b=this._wrapMouseEvent(a,{type:b,relatedTarget:a.relatedTarget}),d.event.trigger(b,null,a.target,!1))},_proxyTouchStartEvent:function(a){var b=this,c=b._getNativeEvent(a);null!=c.changedTouches&&d.each(c.changedTouches,function(c,e){var g=f.recentTouchStarts;b._trackPointer(e);var h;b._shouldDispatch("pointerdown")&&(h=b._wrapTouchEvent(a,e,{type:"pointerdown",relatedTarget:null}),d.event.trigger(h,null, e.target,!1));b._shouldDispatch("pointerover")&&(h=b._wrapTouchEvent(a,e,{type:"pointerover",relatedTarget:null}),d.event.trigger(h,null,e.target,!1));g.push(e);setTimeout(function(){var a=d.inArray(g,e);-1!==a&&g.splice(a,1)},1550)})},_proxyTouchMoveEvent:function(a){var b=this,c=b._getNativeEvent(a);null!=c.changedTouches&&d.each(c.changedTouches,function(c,e){var g,h,f;b._shouldDispatch(["pointermove","pointerover","pointerout"])&&(f=b._fetchOrTrackPointer(e),h=b._getActualTouchTarget(e),f.target=== h?b._shouldDispatch("pointermove")&&(f=b._wrapTouchEvent(a,e,{type:"pointermove",relatedTarget:null}),d.event.trigger(f,null,h,!1)):(g=f.target,f.target=h,b._shouldDispatch("pointerout")&&(f=b._wrapTouchEvent(a,e,{type:"pointerout",relatedTarget:h}),d.event.trigger(f,null,g,!1)),b._shouldDispatch("pointermove")&&(f=b._wrapTouchEvent(a,e,{type:"pointermove",relatedTarget:null}),d.event.trigger(f,null,h,!1)),b._shouldDispatch("pointerover")&&(f=b._wrapTouchEvent(a,e,{type:"pointerover",relatedTarget:g}), d.event.trigger(f,null,h,!1))))})},_proxyTouchEndEvent:function(a){var b=this,c=b._getNativeEvent(a);null!=c.changedTouches&&d.each(c.changedTouches,function(c,e){var g=null,f;b._shouldDispatch("pointerup")&&(g=g||b._getActualTouchTarget(e),f=b._wrapTouchEvent(a,e,{type:"pointerup",relatedTarget:null}),d.event.trigger(f,null,g,!1));b._shouldDispatch("pointerout")&&(g=g||b._getActualTouchTarget(e),f=b._wrapTouchEvent(a,e,{type:"pointerout",relatedTarget:null}),d.event.trigger(f,null,g,!1));b._untrackPointer(e)})}, _proxyTouchCancelEvent:function(a){var b=this,c=b._getNativeEvent(a);null!=c.changedTouches&&d.each(c.changedTouches,function(c,e){var f=null,h;b._shouldDispatch("pointercancel")&&(f=f||b._getActualTouchTarget(e),h=b._wrapTouchEvent(a,e,{type:"pointercancel",relatedTarget:null}),d.event.trigger(h,null,f,!1));b._shouldDispatch("pointerout")&&(f=f||b._getActualTouchTarget(e),h=b._wrapTouchEvent(a,e,{type:"pointerout",relatedTarget:null}),d.event.trigger(h,null,f,!1));b._untrackPointer(e)})},_trackPointer:function(a){var b; this._activePointers[a.identifier]&&(this._activePointers.length-=1);b={target:a.target,isPrimary:0===this._activePointers.length};this._activePointers[a.identifier]=b;this._activePointers.length+=1;return b},_fetchOrTrackPointer:function(a){var b;return(b=this._activePointers[a.identifier])?b:this._trackPointer(a)},_untrackPointer:function(a){this._activePointers[a.identifier]&&(delete this._activePointers[a.identifier],this._activePointers.length-=1)},_isSimulatedMouseEvent:function(a){var b=f.recentTouchStarts, c,d;for(d=b.length;-1!==--d;)if(c=b[d],20>Math.abs(a.clientX-c.clientX)&&20>Math.abs(a.clientY-c.clientY))return!0},_getActualTouchTarget:function(a){return a.target.ownerDocument.elementFromPoint(a.clientX,a.clientY)},_wrapMouseEvent:function(a,b){var c=this._getNativeEvent(a),f,e;void 0!==c.buttons?e=(f=c.buttons)?c.button:-1:0===a.which?(e=-1,f=0):(e=c.button,f=m[e]);d.extend(b,{button:e,buttons:f,pointerId:1,pointerType:"mouse",width:0,height:0,pressure:c.pressure||c.mozPressure||(0!==f?0.5:0), tiltX:0,tiltY:0,isPrimary:!0});return this._wrapEvent(a,b)},_wrapTouchEvent:function(a,b,c){d.extend(c,{button:0,buttons:1,clientX:b.clientX,clientY:b.clientY,pointerId:b.identifier+2,pointerType:"touch",width:20,height:20,pressure:0.5,tiltX:0,tiltY:0,isPrimary:this._activePointers[b.identifier].isPrimary});return this._wrapEvent(a,c)},_wrapEvent:function(a,b){var c=new d.Event(a,b),f=this._getNativeEvent(a),e,g;for(g=k.length;-1!==--g;)e=k[g],e in c||(c[e]=e in a?a[e]:f[e]);return c},_getNativeEvent:function(a){for(;null!= a.originalEvent;a=a.originalEvent);return a},_shouldDispatch:function(a){var b=this,c;return d.isArray(a)?(d.each(a,function(a,d){c=!!b._refCounters[d];return!c}),c):!!b._refCounters[a]}};d.each("pointerdown pointerup pointercancel pointermove pointerover pointerout".split(" "),function(a,b){d.event.special[b]={setup:function(a,d,e){f.bind(this.ownerDocument||this,b);return!0},teardown:function(){f.unbind(this.ownerDocument||this,b);return!0}}})}});