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