UNPKG

@0x30/vue-navigation

Version:
2 lines (1 loc) 8.82 kB
(function(a,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],r):(a=typeof globalThis<"u"?globalThis:a||self,r(a.vueNavigation={},a.Vue))})(this,function(a,r){"use strict";const O=()=>document.querySelector("#___disable_all_pointerevents"),P=()=>{const e=O();e.style.display="block"},A=()=>{const e=O();e.style.display="none"},le=()=>{const e=document.createElement("div");e.id="___disable_all_pointerevents",e.style.position="fixed",e.style.top="0",e.style.right="0",e.style.bottom="0",e.style.left="0",e.style.opacity="0",e.style.display="none",e.style.zIndex="99999999",document.body.appendChild(e)},T=()=>`_${Math.random().toString(32).slice(2)}`,U=(e,n)=>{e==null||e.forEach(t=>t==null?void 0:t(...n??[]))},$=T(),l=[];let J;const I=e=>J=e,K=()=>J;let W;const X=e=>W=e,de=()=>W,w=()=>l[l.length-1],N={},ue=(e,n)=>{X(e),N[e]=()=>{delete N[e],W=void 0,n()}},fe=e=>{var n;e&&((n=N[e])==null||n.apply(null))};var s=(e=>(e.onEnter="_vn_oe",e.onLeave="_vn_ol",e.close="_vn_c",e.onLeaveBefore="_vn_olb",e.onWillAppear="_vn_owa",e.onWillDisappear="_vn_owd",e.onDidAppear="_vn_oda",e.onDidDisappear="_vn_odd",e.cancelBatchId="_vn_cbi",e.ExtData="_vn_ed",e.onAfterEnter="_vn_oae",e.onAfterLeave="_vn_oal",e.onBeforeEnter="_vn_obe",e.onBeforeLeave="_vn_obl",e.ProgressExitAnimated="_vn_p_e_a",e))(s||{});const B=(e,n,t)=>{e&&(e[n]=t)},v=(e,n,t)=>{if(e){const c=e[n]??[];e[n]=[...c,t]}},m=(e,n)=>{if(e)return e[n]},Y=(e,n)=>{const t=V(e)??{};B(e,"_vn_ed",{...t,...n})},V=e=>m(e,"_vn_ed"),Ce=e=>{var n;z((n=r.getCurrentInstance())==null?void 0:n.appContext,e),r.onUnmounted(()=>{var t;z((t=r.getCurrentInstance())==null?void 0:t.appContext,void 0)})},Z=e=>m(e,s.onLeaveBefore),z=(e,n)=>B(e,s.onLeaveBefore,n),_e=()=>{var e;Y((e=r.getCurrentInstance())==null?void 0:e.appContext,{__is_quiet_page:!0})},x=e=>{var n;return((n=V(e))==null?void 0:n.__is_quiet_page)===!0},he=e=>{var n;v((n=r.getCurrentInstance())==null?void 0:n.appContext,s.onWillAppear,e)},ge=e=>{var n;v((n=r.getCurrentInstance())==null?void 0:n.appContext,s.onWillDisappear,e)},ve=e=>{var n;v((n=r.getCurrentInstance())==null?void 0:n.appContext,s.onDidAppear,e)},me=e=>{var n;v((n=r.getCurrentInstance())==null?void 0:n.appContext,s.onDidDisappear,e)},y=(e,n,t)=>{const c=m(e,n);U(c,[t])},ye=e=>{var c;const n=(c=r.getCurrentInstance())==null?void 0:c.appContext,t=L(n)??{};Y(n,{__pageMateInfo:{...t,...e}})},L=e=>{var n;return(n=V(e))==null?void 0:n.__pageMateInfo},q=new Set,we=(e,n)=>{const t=[e,n];return q.add(t),()=>q.delete(t)},H=(e,n)=>{q.forEach(([t,c])=>{const i=L(e),p=L(n);(((c==null?void 0:c.isEvery)??!1)||i!==void 0&&p!==void 0)&&t.apply(null,[i,p])})},Be=()=>{var e;return L((e=w())==null?void 0:e._context)},Ee=(e,n)=>(B(e,s.cancelBatchId,n),n),Pe=e=>m(e,s.cancelBatchId),Ae=e=>{var n;B((n=r.getCurrentInstance())==null?void 0:n.appContext,s.onEnter,e)},Ie=e=>{var n;B((n=r.getCurrentInstance())==null?void 0:n.appContext,s.onLeave,e)},be=(e,n)=>{B(e,s.close,n)},xe=e=>m(e,s.close),ee=(e,n,t,c)=>new Promise(i=>{const p=m(n,e);p?p==null||p.apply(null,[{from:t,to:c},i]):i()}),Le=(e,n,t)=>ee(s.onEnter,e,n,t),De=(e,n,t)=>ee(s.onLeave,e,n,t),G=e=>{window.dispatchEvent(new CustomEvent("onNativateIosScreenEdgePanEvent",{detail:e}))};function ne(){window.ScreenEdgePanGestureRecognizerStart=()=>{P(),G({state:0})},window.ScreenEdgePanGestureRecognizerChange=n=>{G({state:1,progress:n})},window.ScreenEdgePanGestureRecognizerEnded=n=>{G({state:2,isFinish:n}),A()};const e=n=>(n==null?void 0:n.childElementCount)===1?n.children[0]:n;window.addEventListener("onNativateIosScreenEdgePanEvent",n=>{if(l.length<2)return;const t=l[l.length-1];if(Z(t._context)!==void 0){ie();return}const i=e(t._container),p=e(l[l.length-2]._container);switch(n.detail.state){case 0:break;case 1:te({from:i,to:p},n.detail.progress);break;default:te({from:i,to:p},0,n.detail.isFinish);break}})}const ke=e=>{var n;B((n=r.getCurrentInstance())==null?void 0:n.appContext,s.ProgressExitAnimated,e)},te=(e,n,t)=>{var i;const c=m((i=l[l.length-1])==null?void 0:i._context,s.ProgressExitAnimated);c==null||c.apply(null,[e,n,t])},Se=e=>{var n;v((n=r.getCurrentInstance())==null?void 0:n.appContext,s.onAfterEnter,e)},Me=e=>{var n;v((n=r.getCurrentInstance())==null?void 0:n.appContext,s.onAfterLeave,e)},Te=e=>{var n;v((n=r.getCurrentInstance())==null?void 0:n.appContext,s.onBeforeEnter,e)},We=e=>{var n;v((n=r.getCurrentInstance())==null?void 0:n.appContext,s.onBeforeLeave,e)},D=(e,n)=>{const t=m(e,n);U(t)};let k;const Ne=e=>{k=e},R=(e,n,t,c)=>{var p;if(t===void 0)return;const i=()=>{var d;t.unmount(),t._container instanceof Element&&((d=t._container.parentElement)==null||d.removeChild(t._container)),n&&(A(),fe(c))};e?(p=xe(t._context))==null||p(()=>i()):i()},oe=e=>(e==null?void 0:e.childElementCount)===1?e.children[0]:e,ae=(e,n,t)=>new Promise(c=>{const i=document.createElement("div");document.body.appendChild(i);const p=()=>{n!==!1&&R(!1,!1,l.splice(l.length-2,1)[0])},{onAfterEnter:d,onAfterLeave:_,onBeforeEnter:h,onBeforeLeave:g,...C}=t??{},f=r.createApp({setup:()=>{var pe;const E=r.ref(!0);let b;const M=(pe=w())==null?void 0:pe._context;let o=r.getCurrentInstance();return r.onMounted(()=>{o=r.getCurrentInstance(),be(o==null?void 0:o.appContext,u=>{b=u,E.value=!1})}),()=>r.createVNode(r.Transition,r.mergeProps({appear:!0,onEnter:async(u,Q)=>{const j=oe(M.app._container);await Le(o==null?void 0:o.appContext,j,u),p(),c(f),Q()},onLeave:async(u,Q)=>{P();const j=oe(w()._container);await De(o==null?void 0:o.appContext,u,j),b?b():A(),Q()},onBeforeEnter:u=>{h==null||h(u),D(o==null?void 0:o.appContext,s.onBeforeEnter),!x(o==null?void 0:o.appContext)&&(y(M,s.onWillDisappear),y(o==null?void 0:o.appContext,s.onWillAppear,!0))},onAfterEnter:u=>{d==null||d(u),D(o==null?void 0:o.appContext,s.onAfterEnter),!x(o==null?void 0:o.appContext)&&(y(M,s.onDidDisappear),y(o==null?void 0:o.appContext,s.onDidAppear,!0),H(o==null?void 0:o.appContext,M))},onBeforeLeave:u=>{g==null||g(u),D(o==null?void 0:o.appContext,s.onBeforeLeave),!x(o==null?void 0:o.appContext)&&(y(o==null?void 0:o.appContext,s.onWillDisappear),y(w()._context,s.onWillAppear,!1))},onAfterLeave:u=>{_==null||_(u),D(o==null?void 0:o.appContext,s.onAfterLeave),!x(o==null?void 0:o.appContext)&&(y(o==null?void 0:o.appContext,s.onDidDisappear),y(w()._context,s.onDidAppear,!1),H(o==null?void 0:o.appContext,w()._context))}},C),{default:()=>[E.value?e:null]})}});k==null||k(f),f.mount(i),n===!1&&window.history.pushState(I({index:l.length,session:$}),""),l.push(f)}),Ve=(e,n)=>{const t=w(),c=t==null?void 0:t._context,i=Ee(c,T()),p=()=>Pe(c)===i,d=Z(c);if(d===void 0)return Promise.resolve();const{index:_,session:h}=K(),g=_-e+1;for(let C=0;C<e;C++)window.history.pushState(I({index:g+C,session:h}),"");return new Promise(async()=>{const C=()=>{X(n),z(c,void 0),window.history.go(-e)},f=d();typeof f=="boolean"?f&&p()&&C():await f===!0&&p()&&C()})};let F=!1;const ze=()=>{F=!0},se=(e,n=!1)=>{le(),l.push(e);const t=I({index:0,session:$});n?window.history.replaceState(t,""):window.history.pushState(t,"");const c=async i=>{const p=K();if(!p||i.state.session!==p.session)return;const d=i.state.index-p.index;if(d===0)return;if(d>0)return window.history.go(-d);const _=de();if(F){F=!1;const h=Math.abs(d),g=l.length-h-1;l.splice(g,h).forEach((f,E,b)=>{R(!1,E===b.length-1,f,_)})}else await Ve(-d,_),l.splice(l.length-Math.abs(d)).forEach((g,C,f)=>{const E=C===f.length-1;R(E,E,g,_)});l.length===0?I():I(window.history.state)};return{add:()=>window.addEventListener("popstate",c),remove:()=>window.removeEventListener("popstate",c)}},re=async(e,n)=>{P();const t=await ae(e,!1,n);return A(),t},ce=async(e,n)=>{P();const t=await ae(e,!0,n);return A(),t},S=(e=1)=>(P(),new Promise(n=>{ue(T(),n);const t=typeof e=="number"?e:1;window.history.go(-Math.abs(t))})),ie=()=>S(1),qe=e=>e?ce:re,Ge=async e=>{ze(),await S(e)},Re=async()=>{await S(l.length-1)},Fe=r.defineComponent({name:"NavigatorController",setup:(e,{slots:n})=>{const{add:t}=se(r.getCurrentInstance().appContext.app,!0);return r.onMounted(t),r.onMounted(ne),()=>{var c;return(c=n.default)==null?void 0:c.call(n)}}}),Qe={install(e){const{add:n}=se(e,!0);n(),ne()}};a.Navigator=Fe,a.back=ie,a.backToHome=Re,a.blackBoxBack=Ge,a.disableBodyPointerEvents=P,a.enableBodyPointerEvents=A,a.getCurrentPageMate=Be,a.goBack=S,a.navigation=Qe,a.onDidAppear=ve,a.onDidDisappear=me,a.onPageAfterEnter=Se,a.onPageAfterLeave=Me,a.onPageBeforeEnter=Te,a.onPageBeforeLeave=We,a.onPageChange=we,a.onWillAppear=he,a.onWillDisappear=ge,a.push=re,a.replace=ce,a.to=qe,a.useAppBeforeMount=Ne,a.useLeaveBefore=Ce,a.usePageMate=ye,a.useProgressExitAnimated=ke,a.useQuietPage=_e,a.useTransitionEnter=Ae,a.useTransitionLeave=Ie,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});