@fancyapps/ui
Version:
Robust JavaScript UI Component Library
3 lines (2 loc) • 4.52 kB
JavaScript
/*! 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 d=!1,l=!0;const f=(e,t)=>{let f,h,v,g,p,m=Object.assign(Object.assign({},u),t),E=[],w=[],y=[],T=!1,b=!1,M=!1,L=!1,x=0,P=0,D=0,X=0,Y=0,j=0,k=0,R=0,z=0,A=[];const O=new Map;function S(e){const t=r(w),n=r(y),o=t&&n?t/n:0,s=Math.abs(k)>Math.abs(R)?k:R,i={srcEvent:f,isPanRecognized:T,isSwipeRecognized:b,firstTouch:E,previousTouch:y,currentTouch:w,deltaX:D,deltaY:X,offsetX:Y,offsetY:j,velocityX:k,velocityY:R,velocity:s,angle:z,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=m.ignore.join(","),s=e=>e&&e instanceof HTMLElement&&(e.matches(o)||e.closest(o));if(s(t)||s(n))return!1}function C(e){const t=Date.now();if(A=A.filter(e=>!e.ts||e.ts>t-100),e&&A.push(e),k=0,R=0,A.length>3){const e=A[0],t=A[A.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&&(k=Math.abs(n)>3?n/(s/30):0,R=Math.abs(o)>3?o/(s/30):0)}}}function H(e){if(!1===q(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",I),window.addEventListener("mouseup",B)),window.addEventListener("blur",F),f=e,w=o(e),E=[...w],y=[],P=w.length,h=i(w),1===P&&(T=!1,b=!1,M=!1),P&&C(i(w));const t=Date.now(),n=t-(x||t);L=n>0&&n<=250&&1===P,x=t,clearTimeout(g),S("start")}function I(e){var t;if(!E.length)return;if(e.defaultPrevented)return;if(!1===q(e))return;f=e,y=[...w],w=n(e);const o=i(y),s=i(n(e));if(C(s),P=w.length,h=s,y.length===w.length?(D=s.x-o.x,X=s.y-o.y):(D=0,X=0),E.length){const e=i(E);Y=s.x-e.x,j=s.y-e.y}if(w.length>1){const e=r(w),t=r(y);Math.abs(e-t)>=.1&&(M=!0,S("pinch"))}T||(T=Math.abs(Y)>=m.panThreshold||Math.abs(j)>=m.panThreshold,T&&(l=!1,clearTimeout(p),p=void 0,z=Math.abs(180*Math.atan2(j,Y)/Math.PI),v=z>45&&z<135?"y":"x",E=[...w],y=[...w],Y=0,j=0,D=0,X=0,null===(t=window.getSelection())||void 0===t||t.removeAllRanges(),S("panstart"))),T&&(D||X)&&S("pan"),S("move")}function B(e){if(f=e,!E.length)return;const t=o(e),n=s(e);if(P=t.length,h=i(n),n.length&&C(i(n)),y=[...w],w=[...t],E=[...t],P>0)S("end"),T=!1,b=!1,A=[];else{const e=m.swipeThreshold;(Math.abs(k)>e||Math.abs(R)>e)&&(b=!0),T&&S("panend"),b&&S("swipe"),T||b||M||(S("tap"),L?S("doubleTap"):g=setTimeout(function(){S("singleTap")},250)),S("end"),G()}}function F(){clearTimeout(g),G(),T&&S("panend"),S("end")}function G(){d=!1,T=!1,b=!1,L=!1,P=0,A=[],w=[],y=[],E=[],D=0,X=0,Y=0,j=0,k=0,R=0,z=0,v=void 0,window.removeEventListener("mousemove",I),window.removeEventListener("mouseup",B),window.removeEventListener("blur",F),l||p||(p=setTimeout(()=>{l=!0,p=void 0},100))}function J(e){const t=e.target;d=!1,t&&!e.defaultPrevented&&(l||(c(e),e.stopPropagation()))}const K={init:function(){return e&&(e.addEventListener("click",J,a),e.addEventListener("mousedown",H,a),e.addEventListener("touchstart",H,a),e.addEventListener("touchmove",I,a),e.addEventListener("touchend",B),e.addEventListener("touchcancel",B)),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:()=>P>0,destroy:function(){clearTimeout(g),clearTimeout(p),p=void 0,e&&(e.removeEventListener("click",J,a),e.removeEventListener("mousedown",H,a),e.removeEventListener("touchstart",H,a),e.removeEventListener("touchmove",I,a),e.removeEventListener("touchend",B),e.removeEventListener("touchcancel",B)),e=null,G()}};return K};f.isClickAllowed=()=>l;export{f as Gestures,s as getChangedPosition,n as getCurrentPosition,r as getDistance,i as getMidpoint,o as getTargetPosition,e as isTouchEvent};