UNPKG

@ionic/core

Version:
4 lines • 17 kB
import{__awaiter,__generator}from"tslib"; /*! * (C) Ionic http://ionicframework.com - MIT License */import{r as registerInstance,c as createEvent,h,f as getElement,e as Host}from"./index-527b9e34.js";import{g as getTimeGivenProgression}from"./cubic-bezier-fe2083dc.js";import{n as assert,s as shallowEqualStringMap}from"./helpers-78efeec3.js";import{b as getIonMode,c as config}from"./ionic-global-ca86cf32.js";import{l as lifecycle,t as transition,s as setPageHidden,d as LIFECYCLE_WILL_UNLOAD,b as LIFECYCLE_WILL_LEAVE,c as LIFECYCLE_DID_LEAVE}from"./index-ecb55b8d.js";import{a as attachComponent}from"./framework-delegate-2eea1763.js";import"./index-738d7504.js";var VIEW_STATE_NEW=1;var VIEW_STATE_ATTACHED=2;var VIEW_STATE_DESTROYED=3;var ViewController=function(){function t(t,e){this.component=t;this.params=e;this.state=VIEW_STATE_NEW}t.prototype.init=function(t){return __awaiter(this,void 0,void 0,(function(){var e,i;return __generator(this,(function(n){switch(n.label){case 0:this.state=VIEW_STATE_ATTACHED;if(!!this.element)return[3,2];e=this.component;i=this;return[4,attachComponent(this.delegate,t,e,["ion-page","ion-page-invisible"],this.params)];case 1:i.element=n.sent();n.label=2;case 2:return[2]}}))}))};t.prototype._destroy=function(){assert(this.state!==VIEW_STATE_DESTROYED,"view state must be ATTACHED");var t=this.element;if(t){if(this.delegate){this.delegate.removeViewFromDom(t.parentElement,t)}else{t.remove()}}this.nav=undefined;this.state=VIEW_STATE_DESTROYED};return t}();var matches=function(t,e,i){if(!t){return false}if(t.component!==e){return false}return shallowEqualStringMap(t.params,i)};var convertToView=function(t,e){if(!t){return null}if(t instanceof ViewController){return t}return new ViewController(t,e)};var convertToViews=function(t){return t.map((function(t){if(t instanceof ViewController){return t}if("component"in t){return convertToView(t.component,t.componentProps===null?undefined:t.componentProps)}return convertToView(t,undefined)})).filter((function(t){return t!==null}))};var navCss=":host{left:0;right:0;top:0;bottom:0;position:absolute;contain:layout size style;z-index:0}";var IonNavStyle0=navCss;var Nav=function(){function t(t){registerInstance(this,t);this.ionNavWillLoad=createEvent(this,"ionNavWillLoad",7);this.ionNavWillChange=createEvent(this,"ionNavWillChange",3);this.ionNavDidChange=createEvent(this,"ionNavDidChange",3);this.transInstr=[];this.gestureOrAnimationInProgress=false;this.useRouter=false;this.isTransitioning=false;this.destroyed=false;this.views=[];this.didLoad=false;this.delegate=undefined;this.swipeGesture=undefined;this.animated=true;this.animation=undefined;this.rootParams=undefined;this.root=undefined}t.prototype.swipeGestureChanged=function(){if(this.gesture){this.gesture.enable(this.swipeGesture===true)}};t.prototype.rootChanged=function(){if(this.root===undefined){return}if(this.didLoad===false){return}if(!this.useRouter){if(this.root!==undefined){this.setRoot(this.root,this.rootParams)}}};t.prototype.componentWillLoad=function(){this.useRouter=document.querySelector("ion-router")!==null&&this.el.closest("[no-router]")===null;if(this.swipeGesture===undefined){var t=getIonMode(this);this.swipeGesture=config.getBoolean("swipeBackEnabled",t==="ios")}this.ionNavWillLoad.emit()};t.prototype.componentDidLoad=function(){return __awaiter(this,void 0,void 0,(function(){var t;return __generator(this,(function(e){switch(e.label){case 0:this.didLoad=true;this.rootChanged();t=this;return[4,import("./swipe-back-07df2095.js")];case 1:t.gesture=e.sent().createSwipeBackGesture(this.el,this.canStart.bind(this),this.onStart.bind(this),this.onMove.bind(this),this.onEnd.bind(this));this.swipeGestureChanged();return[2]}}))}))};t.prototype.connectedCallback=function(){this.destroyed=false};t.prototype.disconnectedCallback=function(){for(var t=0,e=this.views;t<e.length;t++){var i=e[t];lifecycle(i.element,LIFECYCLE_WILL_UNLOAD);i._destroy()}if(this.gesture){this.gesture.destroy();this.gesture=undefined}this.transInstr.length=0;this.views.length=0;this.destroyed=true};t.prototype.push=function(t,e,i,n){return this.insert(-1,t,e,i,n)};t.prototype.insert=function(t,e,i,n,r){return this.insertPages(t,[{component:e,componentProps:i}],n,r)};t.prototype.insertPages=function(t,e,i,n){return this.queueTrns({insertStart:t,insertViews:e,opts:i},n)};t.prototype.pop=function(t,e){return this.removeIndex(-1,1,t,e)};t.prototype.popTo=function(t,e,i){var n={removeStart:-1,removeCount:-1,opts:e};if(typeof t==="object"&&t.component){n.removeView=t;n.removeStart=1}else if(typeof t==="number"){n.removeStart=t+1}return this.queueTrns(n,i)};t.prototype.popToRoot=function(t,e){return this.removeIndex(1,-1,t,e)};t.prototype.removeIndex=function(t,e,i,n){if(e===void 0){e=1}return this.queueTrns({removeStart:t,removeCount:e,opts:i},n)};t.prototype.setRoot=function(t,e,i,n){return this.setPages([{component:t,componentProps:e}],i,n)};t.prototype.setPages=function(t,e,i){e!==null&&e!==void 0?e:e={};if(e.animated!==true){e.animated=false}return this.queueTrns({insertStart:0,insertViews:t,removeStart:0,removeCount:-1,opts:e},i)};t.prototype.setRouteId=function(t,e,i,n){var r=this;var s=this.getActiveSync();if(matches(s,t,e)){return Promise.resolve({changed:false,element:s.element})}var a;var o=new Promise((function(t){return a=t}));var u;var f={updateURL:false,viewIsReady:function(t){var e;var i=new Promise((function(t){return e=t}));a({changed:true,element:t,markVisible:function(){return __awaiter(r,void 0,void 0,(function(){return __generator(this,(function(t){switch(t.label){case 0:e();return[4,u];case 1:t.sent();return[2]}}))}))}});return i}};if(i==="root"){u=this.setRoot(t,e,f)}else{var h=this.views.find((function(i){return matches(i,t,e)}));if(h){u=this.popTo(h,Object.assign(Object.assign({},f),{direction:"back",animationBuilder:n}))}else if(i==="forward"){u=this.push(t,e,Object.assign(Object.assign({},f),{animationBuilder:n}))}else if(i==="back"){u=this.setRoot(t,e,Object.assign(Object.assign({},f),{direction:"back",animated:true,animationBuilder:n}))}}return o};t.prototype.getRouteId=function(){return __awaiter(this,void 0,void 0,(function(){var t;return __generator(this,(function(e){t=this.getActiveSync();if(t){return[2,{id:t.element.tagName,params:t.params,element:t.element}]}return[2,undefined]}))}))};t.prototype.getActive=function(){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(t){return[2,this.getActiveSync()]}))}))};t.prototype.getByIndex=function(t){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(e){return[2,this.views[t]]}))}))};t.prototype.canGoBack=function(t){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(e){return[2,this.canGoBackSync(t)]}))}))};t.prototype.getPrevious=function(t){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(e){return[2,this.getPreviousSync(t)]}))}))};t.prototype.getLength=function(){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(t){return[2,Promise.resolve(this.views.length)]}))}))};t.prototype.getActiveSync=function(){return this.views[this.views.length-1]};t.prototype.canGoBackSync=function(t){if(t===void 0){t=this.getActiveSync()}return!!(t&&this.getPreviousSync(t))};t.prototype.getPreviousSync=function(t){if(t===void 0){t=this.getActiveSync()}if(!t){return undefined}var e=this.views;var i=e.indexOf(t);return i>0?e[i-1]:undefined};t.prototype.queueTrns=function(t,e){return __awaiter(this,void 0,void 0,(function(){var i,n,r,s,a;return __generator(this,(function(o){switch(o.label){case 0:if(this.isTransitioning&&((i=t.opts)===null||i===void 0?void 0:i.skipIfBusy)){return[2,false]}r=new Promise((function(e,i){t.resolve=e;t.reject=i}));t.done=e;if(!(t.opts&&t.opts.updateURL!==false&&this.useRouter))return[3,2];s=document.querySelector("ion-router");if(!s)return[3,2];return[4,s.canTransition()];case 1:a=o.sent();if(a===false){return[2,false]}if(typeof a==="string"){s.push(a,t.opts.direction||"back");return[2,false]}o.label=2;case 2:if(((n=t.insertViews)===null||n===void 0?void 0:n.length)===0){t.insertViews=undefined}this.transInstr.push(t);this.nextTrns();return[2,r]}}))}))};t.prototype.success=function(t,e){if(this.destroyed){this.fireError("nav controller was destroyed",e);return}if(e.done){e.done(t.hasCompleted,t.requiresTransition,t.enteringView,t.leavingView,t.direction)}e.resolve(t.hasCompleted);if(e.opts.updateURL!==false&&this.useRouter){var i=document.querySelector("ion-router");if(i){var n=t.direction==="back"?"back":"forward";i.navChanged(n)}}};t.prototype.failed=function(t,e){if(this.destroyed){this.fireError("nav controller was destroyed",e);return}this.transInstr.length=0;this.fireError(t,e)};t.prototype.fireError=function(t,e){if(e.done){e.done(false,false,t)}if(e.reject&&!this.destroyed){e.reject(t)}else{e.resolve(false)}};t.prototype.nextTrns=function(){if(this.isTransitioning){return false}var t=this.transInstr.shift();if(!t){return false}this.runTransition(t);return true};t.prototype.runTransition=function(t){return __awaiter(this,void 0,void 0,(function(){var e,i,n,r,s,a;return __generator(this,(function(o){switch(o.label){case 0:o.trys.push([0,6,,7]);this.ionNavWillChange.emit();this.isTransitioning=true;this.prepareTI(t);e=this.getActiveSync();i=this.getEnteringView(t,e);if(!e&&!i){throw new Error("no views in the stack to be removed")}if(!(i&&i.state===VIEW_STATE_NEW))return[3,2];return[4,i.init(this.el)];case 1:o.sent();o.label=2;case 2:this.postViewInit(i,e,t);n=(t.enteringRequiresTransition||t.leavingRequiresTransition)&&i!==e;if(n&&t.opts&&e){r=t.opts.direction==="back";if(r){t.opts.animationBuilder=t.opts.animationBuilder||(i===null||i===void 0?void 0:i.animationBuilder)}e.animationBuilder=t.opts.animationBuilder}s=void 0;if(!n)return[3,4];return[4,this.transition(i,e,t)];case 3:s=o.sent();return[3,5];case 4:s={hasCompleted:true,requiresTransition:false};o.label=5;case 5:this.success(s,t);this.ionNavDidChange.emit();return[3,7];case 6:a=o.sent();this.failed(a,t);return[3,7];case 7:this.isTransitioning=false;this.nextTrns();return[2]}}))}))};t.prototype.prepareTI=function(t){var e,i;var n;var r=this.views.length;(e=t.opts)!==null&&e!==void 0?e:t.opts={};(i=(n=t.opts).delegate)!==null&&i!==void 0?i:n.delegate=this.delegate;if(t.removeView!==undefined){assert(t.removeStart!==undefined,"removeView needs removeStart");assert(t.removeCount!==undefined,"removeView needs removeCount");var s=this.views.indexOf(t.removeView);if(s<0){throw new Error("removeView was not found")}t.removeStart+=s}if(t.removeStart!==undefined){if(t.removeStart<0){t.removeStart=r-1}if(t.removeCount<0){t.removeCount=r-t.removeStart}t.leavingRequiresTransition=t.removeCount>0&&t.removeStart+t.removeCount===r}if(t.insertViews){if(t.insertStart<0||t.insertStart>r){t.insertStart=r}t.enteringRequiresTransition=t.insertStart===r}var a=t.insertViews;if(!a){return}assert(a.length>0,"length can not be zero");var o=convertToViews(a);if(o.length===0){throw new Error("invalid views to insert")}for(var u=0,f=o;u<f.length;u++){var h=f[u];h.delegate=t.opts.delegate;var c=h.nav;if(c&&c!==this){throw new Error("inserted view was already inserted")}if(h.state===VIEW_STATE_DESTROYED){throw new Error("inserted view was already destroyed")}}t.insertViews=o};t.prototype.getEnteringView=function(t,e){var i=t.insertViews;if(i!==undefined){return i[i.length-1]}var n=t.removeStart;if(n!==undefined){var r=this.views;var s=n+t.removeCount;for(var a=r.length-1;a>=0;a--){var o=r[a];if((a<n||a>=s)&&o!==e){return o}}}return undefined};t.prototype.postViewInit=function(t,e,i){var n,r,s;assert(e||t,"Both leavingView and enteringView are null");assert(i.resolve,"resolve must be valid");assert(i.reject,"reject must be valid");var a=i.opts;var o=i.insertViews,u=i.removeStart,f=i.removeCount;var h;if(u!==undefined&&f!==undefined){assert(u>=0,"removeStart can not be negative");assert(f>=0,"removeCount can not be negative");h=[];for(var c=u;c<u+f;c++){var v=this.views[c];if(v!==undefined&&v!==t&&v!==e){h.push(v)}}(n=a.direction)!==null&&n!==void 0?n:a.direction="back"}var d=this.views.length+((r=o===null||o===void 0?void 0:o.length)!==null&&r!==void 0?r:0)-(f!==null&&f!==void 0?f:0);assert(d>=0,"final balance can not be negative");if(d===0){console.warn("You can't remove all the pages in the navigation stack. nav.pop() is probably called too many times.",this,this.el);throw new Error("navigation stack needs at least one root page")}if(o){var l=i.insertStart;for(var _=0,m=o;_<m.length;_++){var v=m[_];this.insertViewAt(v,l);l++}if(i.enteringRequiresTransition){(s=a.direction)!==null&&s!==void 0?s:a.direction="forward"}}if(h&&h.length>0){for(var E=0,w=h;E<w.length;E++){var v=w[E];lifecycle(v.element,LIFECYCLE_WILL_LEAVE);lifecycle(v.element,LIFECYCLE_DID_LEAVE);lifecycle(v.element,LIFECYCLE_WILL_UNLOAD)}for(var g=0,b=h;g<b.length;g++){var v=b[g];this.destroyView(v)}}};t.prototype.transition=function(t,e,i){return __awaiter(this,void 0,void 0,(function(){var n,r,s,a,o,u,f;var h=this;return __generator(this,(function(c){switch(c.label){case 0:n=i.opts;r=n.progressAnimation?function(t){if(t!==undefined&&!h.gestureOrAnimationInProgress){h.gestureOrAnimationInProgress=true;t.onFinish((function(){h.gestureOrAnimationInProgress=false}),{oneTimeCallback:true});t.progressEnd(0,0,0)}else{h.sbAni=t}}:undefined;s=getIonMode(this);a=t.element;o=e&&e.element;u=Object.assign(Object.assign({mode:s,showGoBack:this.canGoBackSync(t),baseEl:this.el,progressCallback:r,animated:this.animated&&config.getBoolean("animated",true),enteringEl:a,leavingEl:o},n),{animationBuilder:n.animationBuilder||this.animation||config.get("navAnimation")});return[4,transition(u)];case 1:f=c.sent().hasCompleted;return[2,this.transitionFinish(f,t,e,n)]}}))}))};t.prototype.transitionFinish=function(t,e,i,n){var r=t?e:i;if(r){this.unmountInactiveViews(r)}return{hasCompleted:t,requiresTransition:true,enteringView:e,leavingView:i,direction:n.direction}};t.prototype.insertViewAt=function(t,e){var i=this.views;var n=i.indexOf(t);if(n>-1){assert(t.nav===this,"view is not part of the nav");i.splice(n,1);i.splice(e,0,t)}else{assert(!t.nav,"nav is used");t.nav=this;i.splice(e,0,t)}};t.prototype.removeView=function(t){assert(t.state===VIEW_STATE_ATTACHED||t.state===VIEW_STATE_DESTROYED,"view state should be loaded or destroyed");var e=this.views;var i=e.indexOf(t);assert(i>-1,"view must be part of the stack");if(i>=0){e.splice(i,1)}};t.prototype.destroyView=function(t){t._destroy();this.removeView(t)};t.prototype.unmountInactiveViews=function(t){if(this.destroyed){return}var e=this.views;var i=e.indexOf(t);for(var n=e.length-1;n>=0;n--){var r=e[n];var s=r.element;if(s){if(n>i){lifecycle(s,LIFECYCLE_WILL_UNLOAD);this.destroyView(r)}else if(n<i){setPageHidden(s,true)}}}};t.prototype.canStart=function(){return!this.gestureOrAnimationInProgress&&!!this.swipeGesture&&!this.isTransitioning&&this.transInstr.length===0&&this.canGoBackSync()};t.prototype.onStart=function(){this.gestureOrAnimationInProgress=true;this.pop({direction:"back",progressAnimation:true})};t.prototype.onMove=function(t){if(this.sbAni){this.sbAni.progressStep(t)}};t.prototype.onEnd=function(t,e,i){var n=this;if(this.sbAni){this.sbAni.onFinish((function(){n.gestureOrAnimationInProgress=false}),{oneTimeCallback:true});var r=t?-.001:.001;if(!t){this.sbAni.easing("cubic-bezier(1, 0, 0.68, 0.28)");r+=getTimeGivenProgression([0,0],[1,0],[.68,.28],[1,1],e)[0]}else{r+=getTimeGivenProgression([0,0],[.32,.72],[0,1],[1,1],e)[0]}this.sbAni.progressEnd(t?1:0,r,i)}else{this.gestureOrAnimationInProgress=false}};t.prototype.render=function(){return h("slot",{key:"71e9b5d07fc90ca5534197a63a003a4154aabd59"})};Object.defineProperty(t.prototype,"el",{get:function(){return getElement(this)},enumerable:false,configurable:true});Object.defineProperty(t,"watchers",{get:function(){return{swipeGesture:["swipeGestureChanged"],root:["rootChanged"]}},enumerable:false,configurable:true});return t}();Nav.style=IonNavStyle0;var navLink=function(t,e,i,n,r){var s=t.closest("ion-nav");if(s){if(e==="forward"){if(i!==undefined){return s.push(i,n,{skipIfBusy:true,animationBuilder:r})}}else if(e==="root"){if(i!==undefined){return s.setRoot(i,n,{skipIfBusy:true,animationBuilder:r})}}else if(e==="back"){return s.pop({skipIfBusy:true,animationBuilder:r})}}return Promise.resolve(false)};var NavLink=function(){function t(t){var e=this;registerInstance(this,t);this.onClick=function(){return navLink(e.el,e.routerDirection,e.component,e.componentProps,e.routerAnimation)};this.component=undefined;this.componentProps=undefined;this.routerDirection="forward";this.routerAnimation=undefined}t.prototype.render=function(){return h(Host,{key:"9ba170d1b10e08e8a6b5e6a30d363871d455a0a9",onClick:this.onClick})};Object.defineProperty(t.prototype,"el",{get:function(){return getElement(this)},enumerable:false,configurable:true});return t}();export{Nav as ion_nav,NavLink as ion_nav_link};