UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

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