UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

1 lines 4.52 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]"]},f=!1,d=!0,h=(e,t)=>{let n,h,v,p={...c,...t},g=[],m=[],w=[],E=!1,y=!1,T=!1,b=!1,M=0,x=0,L=0,P=0,D=0,k=0,C=0,j=0,A=0,q=[],G=0,H=0,I=new Map;function R(e){let t=u(m),o=u(w),i=Math.abs(C)>Math.abs(j)?C:j,s={srcEvent:v,isPanRecognized:E,isSwipeRecognized:y,firstTouch:g,previousTouch:w,currentTouch:m,deltaX:L,deltaY:P,offsetX:D,offsetY:k,velocityX:C,velocityY:j,velocity:i,angle:A,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=p.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(q=q.filter(e=>!e.ts||e.ts>t-100),e&&q.push(e),C=0,j=0,q.length>3){let e=q[0],t=q[q.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&&(C=Math.abs(n)>3?n/(i/30):0,j=Math.abs(o)>3?o/(i/30):0)}}}function Y(e){if(!1===S(e))return;if("u">typeof MouseEvent&&e instanceof MouseEvent){if(f)return}else f=!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),v=e,g=[...m=i(e)],w=[],x=m.length,n=r(m),1===x&&(E=!1,y=!1,T=!1),x&&X(r(m));let t=Date.now(),o=t-(M||t);b=o>0&&o<=250&&1===x,M=t,clearTimeout(G),R("start")}function z(e){if(!g.length||e.defaultPrevented||!1===S(e))return;v=e,w=[...m],m=o(e);let t=r(w),i=r(o(e));if(X(i),x=m.length,n=i,w.length===m.length?(L=i.x-t.x,P=i.y-t.y):(L=0,P=0),g.length){let e=r(g);D=i.x-e.x,k=i.y-e.y}m.length>1&&Math.abs(u(m)-u(w))>=.1&&(T=!0,R("pinch")),!E&&(E=Math.abs(D)>p.panThreshold||Math.abs(k)>p.panThreshold)&&(d=!1,clearTimeout(H),H=0,h=(A=Math.abs(180*Math.atan2(k,D)/Math.PI))>45&&A<135?"y":"x",g=[...m],w=[...m],D=0,k=0,L=0,P=0,window.getSelection()?.removeAllRanges(),R("panstart")),E&&(L||P)&&R("pan"),R("move")}function B(e){if(v=e,!g.length)return;let t=i(e),o=s(e);if(x=t.length,n=r(o),o.length&&X(r(o)),w=[...m],m=[...t],g=[...t],x>0)R("end"),E=!1,y=!1,q=[];else{let e=p.swipeThreshold;(Math.abs(C)>e||Math.abs(j)>e)&&(y=!0),E&&R("panend"),y&&R("swipe"),E||y||T||(R("tap"),b?R("doubleTap"):G=setTimeout(function(){R("singleTap")},250)),R("end"),J()}}function F(){clearTimeout(G),J(),E&&R("panend"),R("end")}function J(){f=!1,E=!1,y=!1,b=!1,x=0,q=[],m=[],w=[],g=[],L=0,P=0,D=0,k=0,C=0,j=0,A=0,h=void 0,window.removeEventListener("mousemove",z),window.removeEventListener("mouseup",B),window.removeEventListener("blur",F),d||H||(H=setTimeout(()=>{d=!0,H=0},100))}function K(e){let t=e.target;f=!1,t&&!e.defaultPrevented&&(d||(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:()=>x>0,destroy:function(){clearTimeout(G),clearTimeout(H),H=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=()=>d,e.Gestures=h,e.getChangedPosition=s,e.getCurrentPosition=o,e.getDistance=u,e.getMidpoint=r,e.getTargetPosition=i,e.isTouchEvent=t});