@ionic/core
Version:
Base components for Ionic
4 lines • 3.44 kB
JavaScript
/*!
* (C) Ionic http://ionicframework.com - MIT License
*/
import{G as t}from"./p-BTEOs1at.js";const e=(t,e,n,r)=>{const s=!!o(t)&&{capture:!1,passive:!!r.passive};let i,c;return t.__zone_symbol__addEventListener?(i="__zone_symbol__addEventListener",c="__zone_symbol__removeEventListener"):(i="addEventListener",c="removeEventListener"),t[i](e,n,s),()=>{t[c](e,n,s)}},o=t=>{if(void 0===n)try{const e=Object.defineProperty({},"passive",{get:()=>{n=!0}});t.addEventListener("optsTest",(()=>{}),e)}catch(t){n=!1}return!!n};let n;const r=t=>t instanceof Document?t:t.ownerDocument,s=o=>{let n=!1,s=!1,d=!0,u=!1;const l=Object.assign({disableScroll:!1,direction:"x",gesturePriority:0,passive:!0,maxAngle:40,threshold:10},o),v=l.canStart,m=l.onWillStart,p=l.onStart,f=l.onEnd,y=l.notCaptured,h=l.onMove,_=l.threshold,b=l.passive,D=l.blurOnStart,E={type:"pan",startX:0,startY:0,startTime:0,currentX:0,currentY:0,velocityX:0,velocityY:0,deltaX:0,deltaY:0,currentTime:0,event:void 0,data:void 0},g=((t,e,o)=>{const n=o*(Math.PI/180),r="x"===t,s=Math.cos(n),i=e*e;let c=0,a=0,d=!1,u=0;return{start(t,e){c=t,a=e,u=0,d=!0},detect(t,e){if(!d)return!1;const o=t-c,n=e-a,l=o*o+n*n;if(l<i)return!1;const v=Math.sqrt(l),m=(r?o:n)/v;return u=m>s?1:m<-s?-1:0,d=!1,!0},isGesture:()=>0!==u,getDirection:()=>u}})(l.direction,l.threshold,l.maxAngle),x=t.createGesture({name:o.gestureName,priority:o.gesturePriority,disableScroll:o.disableScroll}),L=()=>{n&&(u=!1,h&&h(E))},T=()=>!!x.capture()&&(n=!0,d=!1,E.startX=E.currentX,E.startY=E.currentY,E.startTime=E.currentTime,m?m(E).then(X):X(),!0),X=()=>{D&&(()=>{if("undefined"!=typeof document){const t=document.activeElement;(null==t?void 0:t.blur)&&t.blur()}})(),p&&p(E),d=!0},Y=()=>{n=!1,s=!1,u=!1,d=!0,x.release()},j=t=>{const e=n,o=d;Y(),o&&(i(E,t),e?f&&f(E):y&&y(E))},M=((t,o,n,s,i)=>{let c,a,d,u,l,v,m,p=0;const f=r=>{p=Date.now()+2e3,o(r)&&(!a&&n&&(a=e(t,"touchmove",n,i)),d||(d=e(r.target,"touchend",h,i)),u||(u=e(r.target,"touchcancel",h,i)))},y=s=>{p>Date.now()||o(s)&&(!v&&n&&(v=e(r(t),"mousemove",n,i)),m||(m=e(r(t),"mouseup",_,i)))},h=t=>{b(),s&&s(t)},_=t=>{D(),s&&s(t)},b=()=>{a&&a(),d&&d(),u&&u(),a=d=u=void 0},D=()=>{v&&v(),m&&m(),v=m=void 0},E=()=>{b(),D()},g=(o=!0)=>{o?(c||(c=e(t,"touchstart",f,i)),l||(l=e(t,"mousedown",y,i))):(c&&c(),l&&l(),c=l=void 0,E())};return{enable:g,stop:E,destroy:()=>{g(!1),s=n=o=void 0}}})(l.el,(t=>{const e=a(t);return!(s||!d)&&(c(t,E),E.startX=E.currentX,E.startY=E.currentY,E.startTime=E.currentTime=e,E.velocityX=E.velocityY=E.deltaX=E.deltaY=0,E.event=t,(!v||!1!==v(E))&&(x.release(),!!x.start()&&(s=!0,0===_?T():(g.start(E.startX,E.startY),!0))))}),(t=>{n?!u&&d&&(u=!0,i(E,t),requestAnimationFrame(L)):(i(E,t),g.detect(E.currentX,E.currentY)&&(g.isGesture()&&T()||O()))}),j,{passive:b}),O=()=>{Y(),M.stop(),y&&y(E)};return{enable(t=!0){t||(n&&j(void 0),Y()),M.enable(t)},destroy(){x.destroy(),M.destroy()}}},i=(t,e)=>{if(!e)return;const o=t.currentX,n=t.currentY,r=t.currentTime;c(e,t);const s=t.currentX,i=t.currentY,d=(t.currentTime=a(e))-r;if(d>0&&d<100){const e=(i-n)/d;t.velocityX=(s-o)/d*.7+.3*t.velocityX,t.velocityY=.7*e+.3*t.velocityY}t.deltaX=s-t.startX,t.deltaY=i-t.startY,t.event=e},c=(t,e)=>{let o=0,n=0;if(t){const e=t.changedTouches;if(e&&e.length>0){const t=e[0];o=t.clientX,n=t.clientY}else void 0!==t.pageX&&(o=t.pageX,n=t.pageY)}e.currentX=o,e.currentY=n},a=t=>t.timeStamp||Date.now();export{t as GESTURE_CONTROLLER,s as createGesture}