UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

3 lines (2 loc) 4.49 kB
/*! License details at fancyapps.com/license */ function e(e){return"undefined"!=typeof TouchEvent&&e instanceof TouchEvent}function t(t,n){const o=[],s=e(t)?t[n]:t instanceof MouseEvent&&("changedTouches"===n||"mouseup"!==t.type)?[t]:[];for(const e of s)o.push({x:e.clientX,y:e.clientY,ts:Date.now()});return o}function n(e){return t(e,"touches")}function o(e){return t(e,"targetTouches")}function s(e){return t(e,"changedTouches")}function i(e){const t=e[0],n=e[1]||t;return{x:(t.x+n.x)/2,y:(t.y+n.y)/2,ts:n.ts}}function r(e){const t=e[0],n=e[1]||e[0];return t&&n?-1*Math.sqrt((n.x-t.x)*(n.x-t.x)+(n.y-t.y)*(n.y-t.y)):0}const c=e=>{e.cancelable&&e.preventDefault()},a={passive:!1},u={panThreshold:5,swipeThreshold:3,ignore:["textarea","input","select","[contenteditable]","[data-selectable]","[data-draggable]"]};let l=!1,d=!0;const f=(e,t)=>{let f,h,v,g=Object.assign(Object.assign({},u),t),p=[],m=[],E=[],w=!1,y=!1,T=!1,b=!1,M=0,x=0,L=0,P=0,D=0,X=0,Y=0,j=0,k=0,R=[],z=0,A=0;const O=new Map;function S(e){const t=r(m),n=r(E),o=t&&n?t/n:0,s=Math.abs(Y)>Math.abs(j)?Y:j,i={srcEvent:f,isPanRecognized:w,isSwipeRecognized:y,firstTouch:p,previousTouch:E,currentTouch:m,deltaX:L,deltaY:P,offsetX:D,offsetY:X,velocityX:Y,velocityY:j,velocity:s,angle:k,axis:v,scale:o,center:h};for(const t of O.get(e)||[])t(i)}function q(e){const t=e.target,n=e.composedPath()[0],o=g.ignore.join(","),s=e=>e&&(e.matches(o)||e.closest(o));if(s(t)||s(n))return!1}function C(e){const t=Date.now();if(R=R.filter((e=>!e.ts||e.ts>t-100)),e&&R.push(e),Y=0,j=0,R.length>3){const e=R[0],t=R[R.length-1];if(e&&t){const n=t.x-e.x,o=t.y-e.y,s=e.ts&&t.ts?t.ts-e.ts:0;s>0&&(Y=Math.abs(n)>3?n/(s/30):0,j=Math.abs(o)>3?o/(s/30):0)}}}function I(e){if(!1===q(e))return;if("undefined"!=typeof MouseEvent&&e instanceof MouseEvent){if(l)return}else l=!0;if("undefined"!=typeof MouseEvent&&e instanceof MouseEvent){if(!e.buttons||0!==e.button)return;c(e)}e instanceof MouseEvent&&(window.addEventListener("mousemove",B),window.addEventListener("mouseup",F)),window.addEventListener("blur",G),f=e,m=o(e),p=[...m],E=[],x=m.length,h=i(m),1===x&&(w=!1,y=!1,T=!1),x&&C(i(m));const t=Date.now(),n=t-(M||t);b=n>0&&n<=250&&1===x,M=t,clearTimeout(z),S("start")}function B(e){var t;if(!p.length)return;if(e.defaultPrevented)return;if(!1===q(e))return;f=e,E=[...m],m=n(e);const o=i(E),s=i(n(e));if(C(s),x=m.length,h=s,E.length===m.length?(L=s.x-o.x,P=s.y-o.y):(L=0,P=0),p.length){const e=i(p);D=s.x-e.x,X=s.y-e.y}if(m.length>1){const e=r(m),t=r(E);Math.abs(e-t)>=.1&&(T=!0,S("pinch"))}w||(w=Math.abs(D)>g.panThreshold||Math.abs(X)>g.panThreshold,w&&(d=!1,clearTimeout(A),A=0,k=Math.abs(180*Math.atan2(X,D)/Math.PI),v=k>45&&k<135?"y":"x",p=[...m],E=[...m],D=0,X=0,L=0,P=0,null===(t=window.getSelection())||void 0===t||t.removeAllRanges(),S("panstart"))),w&&(L||P)&&S("pan"),S("move")}function F(e){if(f=e,!p.length)return;const t=o(e),n=s(e);if(x=t.length,h=i(n),n.length&&C(i(n)),E=[...m],m=[...t],p=[...t],x>0)S("end"),w=!1,y=!1,R=[];else{const e=g.swipeThreshold;(Math.abs(Y)>e||Math.abs(j)>e)&&(y=!0),w&&S("panend"),y&&S("swipe"),w||y||T||(S("tap"),b?S("doubleTap"):z=setTimeout((function(){S("singleTap")}),250)),S("end"),H()}}function G(){clearTimeout(z),H(),w&&S("panend"),S("end")}function H(){l=!1,w=!1,y=!1,b=!1,x=0,R=[],m=[],E=[],p=[],L=0,P=0,D=0,X=0,Y=0,j=0,k=0,v=void 0,window.removeEventListener("mousemove",B),window.removeEventListener("mouseup",F),window.removeEventListener("blur",G),d||A||(A=setTimeout((()=>{d=!0,A=0}),100))}function J(e){const t=e.target;l=!1,t&&!e.defaultPrevented&&(d||(c(e),e.stopPropagation()))}const K={init:function(){return e&&(e.addEventListener("click",J,a),e.addEventListener("mousedown",I,a),e.addEventListener("touchstart",I,a),e.addEventListener("touchmove",B,a),e.addEventListener("touchend",F),e.addEventListener("touchcancel",F)),K},on:function(e,t){return function(e,t){O.set(e,[...O.get(e)||[],t])}(e,t),K},off:function(e,t){return O.has(e)&&O.set(e,O.get(e).filter((e=>e!==t))),K},isPointerDown:()=>x>0,destroy:function(){clearTimeout(z),clearTimeout(A),A=0,e&&(e.removeEventListener("click",J,a),e.removeEventListener("mousedown",I,a),e.removeEventListener("touchstart",I,a),e.removeEventListener("touchmove",B,a),e.removeEventListener("touchend",F),e.removeEventListener("touchcancel",F)),e=null,H()}};return K};f.isClickAllowed=()=>d;export{f as Gestures,s as getChangedPosition,n as getCurrentPosition,r as getDistance,i as getMidpoint,o as getTargetPosition,e as isTouchEvent};