@open-xchange/jquery-touch-events
Version:
Polyfill to remove click delays on browsers with touch UIs.
2 lines (1 loc) • 14.6 kB
JavaScript
;!function(e){function t(){var e=s();e!==c&&(c=e,l.trigger("orientationchange"))}function a(t,a,n,o){var i=n.type;n.type=a,e.event.dispatch.call(t,n,o),n.type=i}e.attrFn=e.attrFn||{};var n=navigator.userAgent.toLowerCase(),o=n.indexOf("chrome")>-1&&(n.indexOf("windows")>-1||n.indexOf("macintosh")>-1||n.indexOf("linux")>-1)&&n.indexOf("mobile")<0&&n.indexOf("android")<0,i={tap_pixel_range:5,swipe_h_threshold:50,swipe_v_threshold:50,taphold_threshold:750,doubletap_int:500,touch_capable:window.navigator.msPointerEnabled?!1:"ontouchstart"in window&&!o,orientation_support:"orientation"in window&&"onorientationchange"in window,startevent:window.navigator.msPointerEnabled?"MSPointerDown":"ontouchstart"in window&&!o?"touchstart":"mousedown",endevent:window.navigator.msPointerEnabled?"MSPointerUp":"ontouchstart"in window&&!o?"touchend":"mouseup",moveevent:window.navigator.msPointerEnabled?"MSPointerMove":"ontouchstart"in window&&!o?"touchmove":"mousemove",tapevent:"ontouchstart"in window&&!o?"tap":"click",scrollevent:"ontouchstart"in window&&!o?"touchmove":"scroll",hold_timer:null,tap_timer:null};e.isTouchCapable=function(){return i.touch_capable},e.getStartEvent=function(){return i.startevent},e.getEndEvent=function(){return i.endevent},e.getMoveEvent=function(){return i.moveevent},e.getTapEvent=function(){return i.tapevent},e.getScrollEvent=function(){return i.scrollevent},e.each(["tapstart","tapend","tapmove","tap","tap2","tap3","tap4","singletap","doubletap","taphold","swipe","swipeup","swiperight","swipedown","swipeleft","swipeend","scrollstart","scrollend","orientationchange"],function(t,a){e.fn[a]=function(e){return e?this.on(a,e):this.trigger(a)},e.attrFn[a]=!0}),e.event.special.tapstart={setup:function(){var t=this,n=e(t);n.on(i.startevent,function o(e){if(n.data("callee",o),e.which&&1!==e.which)return!1;var r=e.originalEvent,s={position:{x:i.touch_capable?r.touches[0].screenX:e.screenX,y:i.touch_capable?r.touches[0].screenY:e.screenY},offset:{x:Math.round(i.touch_capable?r.changedTouches[0].pageX-n.position().left:e.pageX-n.position().left),y:Math.round(i.touch_capable?r.changedTouches[0].pageY-n.position().top:e.pageY-n.position().top)},time:Date.now(),target:e.target};return a(t,"tapstart",e,s),!0})},remove:function(){e(this).off(i.startevent,e(this).data.callee)}},e.event.special.tapmove={setup:function(){var t=this,n=e(t);n.on(i.moveevent,function o(e){n.data("callee",o);var r=e.originalEvent,s={position:{x:i.touch_capable?r.touches[0].screenX:e.screenX,y:i.touch_capable?r.touches[0].screenY:e.screenY},offset:{x:Math.round(i.touch_capable?r.changedTouches[0].pageX-n.position().left:e.pageX-n.position().left),y:Math.round(i.touch_capable?r.changedTouches[0].pageY-n.position().top:e.pageY-n.position().top)},time:Date.now(),target:e.target};return a(t,"tapmove",e,s),!0})},remove:function(){e(this).off(i.moveevent,e(this).data.callee)}},e.event.special.tapend={setup:function(){var t=this,n=e(t);n.on(i.endevent,function o(e){n.data("callee",o);var r=e.originalEvent,s={position:{x:i.touch_capable?r.changedTouches[0].screenX:e.screenX,y:i.touch_capable?r.changedTouches[0].screenY:e.screenY},offset:{x:Math.round(i.touch_capable?r.changedTouches[0].pageX-n.position().left:e.pageX-n.position().left),y:Math.round(i.touch_capable?r.changedTouches[0].pageY-n.position().top:e.pageY-n.position().top)},time:Date.now(),target:e.target};return a(t,"tapend",e,s),!0})},remove:function(){e(this).off(i.endevent,e(this).data.callee)}},e.event.special.taphold={setup:function(){var t,n=this,o=e(n),r={x:0,y:0},s=0,c=0;o.on(i.startevent,function p(e){if(e.which&&1!==e.which)return!1;o.data("tapheld",!1),t=e.target;var h=e.originalEvent,l=Date.now(),u={x:i.touch_capable?h.touches[0].screenX:e.screenX,y:i.touch_capable?h.touches[0].screenY:e.screenY},g={x:i.touch_capable?h.touches[0].pageX-h.touches[0].target.offsetLeft:e.offsetX,y:i.touch_capable?h.touches[0].pageY-h.touches[0].target.offsetTop:e.offsetY};return r.x=e.originalEvent.targetTouches?e.originalEvent.targetTouches[0].pageX:e.pageX,r.y=e.originalEvent.targetTouches?e.originalEvent.targetTouches[0].pageY:e.pageY,s=r.x,c=r.y,i.hold_timer=window.setTimeout(function(){var d=r.x-s,f=r.y-c;if(e.target==t&&(r.x==s&&r.y==c||d>=-i.tap_pixel_range&&d<=i.tap_pixel_range&&f>=-i.tap_pixel_range&&f<=i.tap_pixel_range)){o.data("tapheld",!0);var v=Date.now(),w={x:i.touch_capable?h.touches[0].screenX:e.screenX,y:i.touch_capable?h.touches[0].screenY:e.screenY},_={x:Math.round(i.touch_capable?h.changedTouches[0].pageX-o.position().left:e.pageX-o.position().left),y:Math.round(i.touch_capable?h.changedTouches[0].pageY-o.position().top:e.pageY-o.position().top)},T=v-l,x={startTime:l,endTime:v,startPosition:u,startOffset:g,endPosition:w,endOffset:_,duration:T,target:e.target};o.data("callee1",p),a(n,"taphold",e,x)}},i.taphold_threshold),!0}).on(i.endevent,function h(){o.data("callee2",h),o.data("tapheld",!1),window.clearTimeout(i.hold_timer)}).on(i.moveevent,function l(e){o.data("callee3",l),s=e.originalEvent.targetTouches?e.originalEvent.targetTouches[0].pageX:e.pageX,c=e.originalEvent.targetTouches?e.originalEvent.targetTouches[0].pageY:e.pageY})},remove:function(){e(this).off(i.startevent,e(this).data.callee1).off(i.endevent,e(this).data.callee2).off(i.moveevent,e(this).data.callee3)}},e.event.special.doubletap={setup:function(){var t,n,o,r,s=this,c=e(s),p=null,h=!1;c.on(i.startevent,function l(e){return e.which&&1!==e.which?!1:(c.data("doubletapped",!1),t=e.target,c.data("callee1",l),o=e.originalEvent,p||(p={position:{x:i.touch_capable?o.touches[0].screenX:e.screenX,y:i.touch_capable?o.touches[0].screenY:e.screenY},offset:{x:Math.round(i.touch_capable?o.changedTouches[0].pageX-c.position().left:e.pageX-c.position().left),y:Math.round(i.touch_capable?o.changedTouches[0].pageY-c.position().top:e.pageY-c.position().top)},time:Date.now(),target:e.target}),!0)}).on(i.endevent,function u(e){var l=Date.now(),g=c.data("lastTouch")||l+1,d=l-g;if(window.clearTimeout(n),c.data("callee2",u),d<i.doubletap_int&&e.target==t&&d>100){c.data("doubletapped",!0),window.clearTimeout(i.tap_timer);var f={position:{x:i.touch_capable?e.originalEvent.changedTouches[0].screenX:e.screenX,y:i.touch_capable?e.originalEvent.changedTouches[0].screenY:e.screenY},offset:{x:Math.round(i.touch_capable?o.changedTouches[0].pageX-c.position().left:e.pageX-c.position().left),y:Math.round(i.touch_capable?o.changedTouches[0].pageY-c.position().top:e.pageY-c.position().top)},time:Date.now(),target:e.target},v={firstTap:p,secondTap:f,interval:f.time-p.time};h||(a(s,"doubletap",e,v),p=null),h=!0,r=window.setTimeout(function(){h=!1},i.doubletap_int)}else c.data("lastTouch",l),n=window.setTimeout(function(){p=null,window.clearTimeout(n)},i.doubletap_int,[e]);c.data("lastTouch",l)})},remove:function(){e(this).off(i.startevent,e(this).data.callee1).off(i.endevent,e(this).data.callee2)}},e.event.special.singletap={setup:function(){var t=this,n=e(t),o=null,r=null,s={x:0,y:0};n.on(i.startevent,function c(e){return e.which&&1!==e.which?!1:(r=Date.now(),o=e.target,n.data("callee1",c),s.x=e.originalEvent.targetTouches?e.originalEvent.targetTouches[0].pageX:e.pageX,s.y=e.originalEvent.targetTouches?e.originalEvent.targetTouches[0].pageY:e.pageY,!0)}).on(i.endevent,function p(e){if(n.data("callee2",p),e.target==o){var c=e.originalEvent.changedTouches?e.originalEvent.changedTouches[0].pageX:e.pageX,h=e.originalEvent.changedTouches?e.originalEvent.changedTouches[0].pageY:e.pageY;i.tap_timer=window.setTimeout(function(){if(!n.data("doubletapped")&&!n.data("tapheld")&&s.x==c&&s.y==h){var o=e.originalEvent,p={position:{x:i.touch_capable?o.changedTouches[0].screenX:e.screenX,y:i.touch_capable?o.changedTouches[0].screenY:e.screenY},offset:{x:Math.round(i.touch_capable?o.changedTouches[0].pageX-n.position().left:e.pageX-n.position().left),y:Math.round(i.touch_capable?o.changedTouches[0].pageY-n.position().top:e.pageY-n.position().top)},time:Date.now(),target:e.target};p.time-r<i.taphold_threshold&&a(t,"singletap",e,p)}},i.doubletap_int)}})},remove:function(){e(this).off(i.startevent,e(this).data.callee1).off(i.endevent,e(this).data.callee2)}},e.event.special.tap={setup:function(){var t,n,o=this,r=e(o),s=!1,c=null,p={x:0,y:0};r.on(i.startevent,function h(e){return r.data("callee1",h),e.which&&1!==e.which?!1:(s=!0,p.x=e.originalEvent.targetTouches?e.originalEvent.targetTouches[0].pageX:e.pageX,p.y=e.originalEvent.targetTouches?e.originalEvent.targetTouches[0].pageY:e.pageY,t=Date.now(),c=e.target,n=e.originalEvent.targetTouches?e.originalEvent.targetTouches:[e],!0)}).on(i.endevent,function l(e){r.data("callee2",l);var h,u=e.originalEvent.targetTouches?e.originalEvent.changedTouches[0].pageX:e.pageX,g=e.originalEvent.targetTouches?e.originalEvent.changedTouches[0].pageY:e.pageY,d=p.x-u,f=p.y-g;if(c==e.target&&s&&Date.now()-t<i.taphold_threshold&&(p.x==u&&p.y==g||d>=-i.tap_pixel_range&&d<=i.tap_pixel_range&&f>=-i.tap_pixel_range&&f<=i.tap_pixel_range)){for(var v=e.originalEvent,w=[],_=0;_<n.length;_++){var T={position:{x:i.touch_capable?v.changedTouches[_].screenX:e.screenX,y:i.touch_capable?v.changedTouches[_].screenY:e.screenY},offset:{x:Math.round(i.touch_capable?v.changedTouches[_].pageX-r.position().left:e.pageX-r.position().left),y:Math.round(i.touch_capable?v.changedTouches[_].pageY-r.position().top:e.pageY-r.position().top)},time:Date.now(),target:e.target};w.push(T)}switch(n.length){case 1:h="tap";break;case 2:h="tap2";break;case 3:h="tap3";break;case 4:h="tap4"}a(o,h,e,w)}})},remove:function(){e(this).off(i.startevent,e(this).data.callee1).off(i.endevent,e(this).data.callee2)}},e.event.special.swipe={setup:function(){function t(a){s=e(a.currentTarget),s.data("callee1",t),h.x=a.originalEvent.targetTouches?a.originalEvent.targetTouches[0].pageX:a.pageX,h.y=a.originalEvent.targetTouches?a.originalEvent.targetTouches[0].pageY:a.pageY,l.x=h.x,l.y=h.y,c=!0;var n=a.originalEvent;o={position:{x:i.touch_capable?n.touches[0].screenX:a.screenX,y:i.touch_capable?n.touches[0].screenY:a.screenY},offset:{x:Math.round(i.touch_capable?n.changedTouches[0].pageX-s.position().left:a.pageX-s.position().left),y:Math.round(i.touch_capable?n.changedTouches[0].pageY-s.position().top:a.pageY-s.position().top)},time:Date.now(),target:a.target}}function a(t){s=e(t.currentTarget),s.data("callee2",a),l.x=t.originalEvent.targetTouches?t.originalEvent.targetTouches[0].pageX:t.pageX,l.y=t.originalEvent.targetTouches?t.originalEvent.targetTouches[0].pageY:t.pageY;var n,r=s.parent().data("xthreshold")?s.parent().data("xthreshold"):s.data("xthreshold"),u=s.parent().data("ythreshold")?s.parent().data("ythreshold"):s.data("ythreshold"),g="undefined"!=typeof r&&r!==!1&&parseInt(r)?parseInt(r):i.swipe_h_threshold,d="undefined"!=typeof u&&u!==!1&&parseInt(u)?parseInt(u):i.swipe_v_threshold;if(h.y>l.y&&h.y-l.y>d&&(n="swipeup"),h.x<l.x&&l.x-h.x>g&&(n="swiperight"),h.y<l.y&&l.y-h.y>d&&(n="swipedown"),h.x>l.x&&h.x-l.x>g&&(n="swipeleft"),void 0!=n&&c){h.x=0,h.y=0,l.x=0,l.y=0,c=!1;var f=t.originalEvent,v={position:{x:i.touch_capable?f.touches[0].screenX:t.screenX,y:i.touch_capable?f.touches[0].screenY:t.screenY},offset:{x:Math.round(i.touch_capable?f.changedTouches[0].pageX-s.position().left:t.pageX-s.position().left),y:Math.round(i.touch_capable?f.changedTouches[0].pageY-s.position().top:t.pageY-s.position().top)},time:Date.now(),target:t.target},w=Math.abs(o.position.x-v.position.x),_=Math.abs(o.position.y-v.position.y),T={startEvnt:o,endEvnt:v,direction:n.replace("swipe",""),xAmount:w,yAmount:_,duration:v.time-o.time};p=!0,s.trigger("swipe",T).trigger(n,T)}}function n(t){s=e(t.currentTarget);var a="";if(s.data("callee3",n),p){var r=s.data("xthreshold"),h=s.data("ythreshold"),l="undefined"!=typeof r&&r!==!1&&parseInt(r)?parseInt(r):i.swipe_h_threshold,u="undefined"!=typeof h&&h!==!1&&parseInt(h)?parseInt(h):i.swipe_v_threshold,g=t.originalEvent,d={position:{x:i.touch_capable?g.changedTouches[0].screenX:t.screenX,y:i.touch_capable?g.changedTouches[0].screenY:t.screenY},offset:{x:Math.round(i.touch_capable?g.changedTouches[0].pageX-s.position().left:t.pageX-s.position().left),y:Math.round(i.touch_capable?g.changedTouches[0].pageY-s.position().top:t.pageY-s.position().top)},time:Date.now(),target:t.target};o.position.y>d.position.y&&o.position.y-d.position.y>u&&(a="swipeup"),o.position.x<d.position.x&&d.position.x-o.position.x>l&&(a="swiperight"),o.position.y<d.position.y&&d.position.y-o.position.y>u&&(a="swipedown"),o.position.x>d.position.x&&o.position.x-d.position.x>l&&(a="swipeleft");var f=Math.abs(o.position.x-d.position.x),v=Math.abs(o.position.y-d.position.y),w={startEvnt:o,endEvnt:d,direction:a.replace("swipe",""),xAmount:f,yAmount:v,duration:d.time-o.time};s.trigger("swipeend",w)}c=!1,p=!1}var o,r=this,s=e(r),c=!1,p=!1,h={x:0,y:0},l={x:0,y:0};s.on(i.startevent,t),s.on(i.moveevent,a),s.on(i.endevent,n)},remove:function(){e(this).off(i.startevent,e(this).data.callee1).off(i.moveevent,e(this).data.callee2).off(i.endevent,e(this).data.callee3)}},e.event.special.scrollstart={setup:function(){function t(e,t){n=t,a(r,n?"scrollstart":"scrollend",e)}var n,o,r=this,s=e(r);s.on(i.scrollevent,function c(e){s.data("callee",c),n||t(e,!0),clearTimeout(o),o=setTimeout(function(){t(e,!1)},50)})},remove:function(){e(this).off(i.scrollevent,e(this).data.callee)}};var r,s,c,p,h,l=e(window),u={0:!0,180:!0};if(i.orientation_support){var g=window.innerWidth||l.width(),d=window.innerHeight||l.height(),f=50;p=g>d&&g-d>f,h=u[window.orientation],(p&&h||!p&&!h)&&(u={"-90":!0,90:!0})}e.event.special.orientationchange=r={setup:function(){return i.orientation_support?!1:(c=s(),l.on("throttledresize",t),!0)},teardown:function(){return i.orientation_support?!1:(l.off("throttledresize",t),!0)},add:function(e){var t=e.handler;e.handler=function(e){return e.orientation=s(),t.apply(this,arguments)}}},e.event.special.orientationchange.orientation=s=function(){var e=!0,t=document.documentElement;return e=i.orientation_support?u[window.orientation]:t&&t.clientWidth/t.clientHeight<1.1,e?"portrait":"landscape"},e.event.special.throttledresize={setup:function(){e(this).on("resize",x)},teardown:function(){e(this).off("resize",x)}};var v,w,_,T=250,x=function(){w=Date.now(),_=w-y,_>=T?(y=w,e(this).trigger("throttledresize")):(v&&window.clearTimeout(v),v=window.setTimeout(t,T-_))},y=0;e.each({scrollend:"scrollstart",swipeup:"swipe",swiperight:"swipe",swipedown:"swipe",swipeleft:"swipe",swipeend:"swipe",tap2:"tap"},function(t,a){e.event.special[t]={setup:function(){e(this).on(a,e.noop)}}})}(jQuery);