UNPKG

@ionic/core

Version:
4 lines • 10.3 kB
/*! * (C) Ionic http://ionicframework.com - MIT License */ import{d as t,H as i,i as s,e,p as o,h as n,t as r}from"./p-Ct2aBEue.js";import{g as h}from"./p-hHmYLOfE.js";import{o as a}from"./p-B4IBdPVJ.js";import{l as c,t as d,s as u,f as v,d as l,b as p,c as m}from"./p-DfDIY2oP.js";import{b as f}from"./p-HthJZjZ6.js";import{a as w}from"./p-Ba9_OzSY.js";class g{constructor(t,i){this.component=t,this.params=i,this.state=1}async init(t){if(this.state=2,!this.element){const i=this.component;this.element=await w(this.delegate,t,i,["ion-page","ion-page-invisible"],this.params)}}_destroy(){a(3!==this.state,"view state must be ATTACHED");const t=this.element;t&&(this.delegate?this.delegate.removeViewFromDom(t.parentElement,t):t.remove()),this.nav=void 0,this.state=3}}const b=(t,i)=>t?t instanceof g?t:new g(t,i):null,y=t(class extends i{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.ionNavWillLoad=s(this,"ionNavWillLoad",7),this.ionNavWillChange=s(this,"ionNavWillChange",3),this.ionNavDidChange=s(this,"ionNavDidChange",3),this.transInstr=[],this.gestureOrAnimationInProgress=!1,this.isTransitioning=!1,this.destroyed=!1,this.views=[],this.didLoad=!1,this.animated=!0}swipeGestureChanged(){this.gesture&&this.gesture.enable(!0===this.swipeGesture)}rootChanged(){void 0!==this.root&&!1!==this.didLoad&&this.setRoot(this.root,this.rootParams)}componentWillLoad(){if(void 0===this.swipeGesture){const t=f(this);this.swipeGesture=e.getBoolean("swipeBackEnabled","ios"===t)}this.ionNavWillLoad.emit()}async componentDidLoad(){this.didLoad=!0,this.rootChanged(),this.gesture=(await import("./p-BVNUhOOf.js")).createSwipeBackGesture(this.el,this.canStart.bind(this),this.onStart.bind(this),this.onMove.bind(this),this.onEnd.bind(this)),this.swipeGestureChanged()}connectedCallback(){this.destroyed=!1}disconnectedCallback(){for(const t of this.views)c(t.element,l),t._destroy();this.gesture&&(this.gesture.destroy(),this.gesture=void 0),this.unblockSwipeClicks&&(this.unblockSwipeClicks(),this.unblockSwipeClicks=void 0,this.gestureOrAnimationInProgress=!1),this.transInstr.length=0,this.views.length=0,this.destroyed=!0}push(t,i,s,e){return this.insert(-1,t,i,s,e)}insert(t,i,s,e,o){return this.insertPages(t,[{component:i,componentProps:s}],e,o)}insertPages(t,i,s,e){return this.queueTrns({insertStart:t,insertViews:i,opts:s},e)}pop(t,i){return this.removeIndex(-1,1,t,i)}popTo(t,i,s){const e={removeStart:-1,removeCount:-1,opts:i};return"object"==typeof t&&t.component?(e.removeView=t,e.removeStart=1):"number"==typeof t&&(e.removeStart=t+1),this.queueTrns(e,s)}popToRoot(t,i){return this.removeIndex(1,-1,t,i)}removeIndex(t,i=1,s,e){return this.queueTrns({removeStart:t,removeCount:i,opts:s},e)}setRoot(t,i,s,e){return this.setPages([{component:t,componentProps:i}],s,e)}setPages(t,i,s){return i??={},!0!==i.animated&&(i.animated=!1),this.queueTrns({insertStart:0,insertViews:t,removeStart:0,removeCount:-1,opts:i},s)}async getActive(){return this.getActiveSync()}async getByIndex(t){return this.views[t]}async canGoBack(t){return this.canGoBackSync(t)}async getPrevious(t){return this.getPreviousSync(t)}async getLength(){return Promise.resolve(this.views.length)}getActiveSync(){return this.views[this.views.length-1]}canGoBackSync(t=this.getActiveSync()){return!(!t||!this.getPreviousSync(t))}getPreviousSync(t=this.getActiveSync()){if(!t)return;const i=this.views,s=i.indexOf(t);return s>0?i[s-1]:void 0}async queueTrns(t,i){if(this.isTransitioning&&t.opts?.skipIfBusy)return!1;const s=new Promise(((i,s)=>{t.resolve=i,t.reject=s}));return t.done=i,0===t.insertViews?.length&&(t.insertViews=void 0),this.transInstr.push(t),this.nextTrns(),s}success(t,i){this.destroyed?this.fireError("nav controller was destroyed",i):(i.done&&i.done(t.hasCompleted,t.requiresTransition,t.enteringView,t.leavingView,t.direction),i.resolve(t.hasCompleted))}failed(t,i){this.destroyed?this.fireError("nav controller was destroyed",i):(this.transInstr.length=0,this.fireError(t,i))}fireError(t,i){i.done&&i.done(!1,!1,t),i.reject&&!this.destroyed?i.reject(t):i.resolve(!1)}nextTrns(){if(this.isTransitioning)return!1;const t=this.transInstr.shift();return!!t&&(this.runTransition(t),!0)}async runTransition(t){try{this.ionNavWillChange.emit(),this.isTransitioning=!0,this.prepareTI(t);const i=this.getActiveSync(),s=this.getEnteringView(t,i);if(!i&&!s)throw Error("no views in the stack to be removed");s&&1===s.state&&await s.init(this.el),this.postViewInit(s,i,t);const e=(t.enteringRequiresTransition||t.leavingRequiresTransition)&&s!==i;let o;e&&t.opts&&i&&("back"===t.opts.direction&&(t.opts.animationBuilder=t.opts.animationBuilder||s?.animationBuilder),i.animationBuilder=t.opts.animationBuilder),o=e?await this.transition(s,i,t):{hasCompleted:!0,requiresTransition:!1},this.success(o,t),this.ionNavDidChange.emit()}catch(i){this.failed(i,t)}this.isTransitioning=!1,this.nextTrns()}prepareTI(t){const i=this.views.length;if(t.opts??={},t.opts.delegate??=this.delegate,void 0!==t.removeView){a(void 0!==t.removeStart,"removeView needs removeStart"),a(void 0!==t.removeCount,"removeView needs removeCount");const i=this.views.indexOf(t.removeView);if(i<0)throw Error("removeView was not found");t.removeStart+=i}void 0!==t.removeStart&&(t.removeStart<0&&(t.removeStart=i-1),t.removeCount<0&&(t.removeCount=i-t.removeStart),t.leavingRequiresTransition=t.removeCount>0&&t.removeStart+t.removeCount===i),t.insertViews&&((t.insertStart<0||t.insertStart>i)&&(t.insertStart=i),t.enteringRequiresTransition=t.insertStart===i);const s=t.insertViews;if(!s)return;a(s.length>0,"length can not be zero");const e=(t=>t.map((t=>t instanceof g?t:"component"in t?b(t.component,null===t.componentProps?void 0:t.componentProps):b(t,void 0))).filter((t=>null!==t)))(s);if(0===e.length)throw Error("invalid views to insert");for(const i of e){i.delegate=t.opts.delegate;const s=i.nav;if(s&&s!==this)throw Error("inserted view was already inserted");if(3===i.state)throw Error("inserted view was already destroyed")}t.insertViews=e}getEnteringView(t,i){const s=t.insertViews;if(void 0!==s)return s[s.length-1];const e=t.removeStart;if(void 0!==e){const s=this.views,o=e+t.removeCount;for(let t=s.length-1;t>=0;t--){const n=s[t];if((t<e||t>=o)&&n!==i)return n}}}postViewInit(t,i,s){a(i||t,"Both leavingView and enteringView are null"),a(s.resolve,"resolve must be valid"),a(s.reject,"reject must be valid");const e=s.opts,{insertViews:n,removeStart:r,removeCount:h}=s;let d;if(void 0!==r&&void 0!==h){a(r>=0,"removeStart can not be negative"),a(h>=0,"removeCount can not be negative"),d=[];for(let s=r;s<r+h;s++){const e=this.views[s];void 0!==e&&e!==t&&e!==i&&d.push(e)}e.direction??="back"}const u=this.views.length+(n?.length??0)-(h??0);if(a(u>=0,"final balance can not be negative"),0===u)throw o("[ion-nav] - You can't remove all the pages in the navigation stack. nav.pop() is probably called too many times.",this,this.el),Error("navigation stack needs at least one root page");if(n){let t=s.insertStart;for(const i of n)this.insertViewAt(i,t),t++;s.enteringRequiresTransition&&(e.direction??="forward")}if(d&&d.length>0){for(const t of d)c(t.element,p),c(t.element,m),c(t.element,l);for(const t of d)this.destroyView(t)}}async transition(t,i,s){const o=s.opts,n=o.progressAnimation?t=>{void 0===t||this.gestureOrAnimationInProgress?this.sbAni=t:(this.gestureOrAnimationInProgress=!0,t.onFinish((()=>{this.gestureOrAnimationInProgress=!1}),{oneTimeCallback:!0}),t.progressEnd(0,0,0))}:void 0,r=f(this),h=t.element,a=i&&i.element,c={mode:r,showGoBack:this.canGoBackSync(t),baseEl:this.el,progressCallback:n,animated:this.animated&&e.getBoolean("animated",!0),enteringEl:h,leavingEl:a,...o,animationBuilder:o.animationBuilder||this.animation||e.get("navAnimation")},{hasCompleted:u}=await d(c);return this.transitionFinish(u,t,i,o)}transitionFinish(t,i,s,e){const o=t?i:s;return o&&this.unmountInactiveViews(o),{hasCompleted:t,requiresTransition:!0,enteringView:i,leavingView:s,direction:e.direction}}insertViewAt(t,i){const s=this.views,e=s.indexOf(t);e>-1?(a(t.nav===this,"view is not part of the nav"),s.splice(e,1),s.splice(i,0,t)):(a(!t.nav,"nav is used"),t.nav=this,s.splice(i,0,t))}removeView(t){a(2===t.state||3===t.state,"view state should be loaded or destroyed");const i=this.views,s=i.indexOf(t);a(s>-1,"view must be part of the stack"),s>=0&&i.splice(s,1)}destroyView(t){t._destroy(),this.removeView(t)}unmountInactiveViews(t){if(this.destroyed)return;const i=this.views,s=i.indexOf(t);for(let t=i.length-1;t>=0;t--){const e=i[t],o=e.element;o&&(t>s?(c(o,l),this.destroyView(e)):t<s&&u(o,!0))}}canStart(){return!this.gestureOrAnimationInProgress&&!!this.swipeGesture&&!this.isTransitioning&&0===this.transInstr.length&&this.canGoBackSync()}onStart(){this.gestureOrAnimationInProgress=!0,this.animated&&e.getBoolean("animated",!0)?this.pop({direction:"back",progressAnimation:!0}):this.unblockSwipeClicks=v(this.el)}onMove(t){this.sbAni&&this.sbAni.progressStep(t)}onEnd(t,i,s){if(this.sbAni){this.sbAni.onFinish((()=>{this.gestureOrAnimationInProgress=!1}),{oneTimeCallback:!0});let e=t?-.001:.001;t?e+=h([0,0],[.32,.72],[0,1],[1,1],i)[0]:(this.sbAni.easing("cubic-bezier(1, 0, 0.68, 0.28)"),e+=h([0,0],[1,0],[.68,.28],[1,1],i)[0]),this.sbAni.progressEnd(t?1:0,e,s)}else{this.gestureOrAnimationInProgress=!1;const{unblockSwipeClicks:i}=this;i&&(this.unblockSwipeClicks=void 0,t&&this.pop({direction:"back"}),i())}}render(){return n("slot",{key:"0aea554c0cf00dd3829757ec74c3855eccf1e068"})}get el(){return this}static get watchers(){return{swipeGesture:[{swipeGestureChanged:0}],root:[{rootChanged:0}]}}static get style(){return":host{left:0;right:0;top:0;bottom:0;position:absolute;contain:layout size style;z-index:0}"}},[257,"ion-nav",{delegate:[16],swipeGesture:[1028,"swipe-gesture"],animated:[4],animation:[16],rootParams:[16],root:[1],push:[64],insert:[64],insertPages:[64],pop:[64],popTo:[64],popToRoot:[64],removeIndex:[64],setRoot:[64],setPages:[64],getActive:[64],getByIndex:[64],canGoBack:[64],getPrevious:[64],getLength:[64]},void 0,{swipeGesture:[{swipeGestureChanged:0}],root:[{rootChanged:0}]}]),C=y,k=function(){"undefined"!=typeof customElements&&["ion-nav"].forEach((t=>{"ion-nav"===t&&(customElements.get(r(t))||customElements.define(r(t),y))}))};export{C as IonNav,k as defineCustomElement}