UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

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