UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

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