UNPKG

@ionic/core

Version:
4 lines • 10.5 kB
/*! * (C) Ionic http://ionicframework.com - MIT License */ import{r as t,d as o,o as n,m as e,k as r,e as i,h as s,j as a}from"./p-B_U9CtaY.js";import{c,p as h}from"./p-Do-uqmtX.js";import{o as u,c as l}from"./p-DiVJyqlX.js";const f=class{constructor(n){t(this,n),this.ionRouteDataChanged=o(this,"ionRouteDataChanged",7),this.url=""}onUpdate(t){this.ionRouteDataChanged.emit(t)}onComponentProps(t,o){if(t===o)return;const n=t?Object.keys(t):[],e=o?Object.keys(o):[];if(n.length===e.length){for(const e of n)if(t[e]!==o[e])return void this.onUpdate(t)}else this.onUpdate(t)}connectedCallback(){this.ionRouteDataChanged.emit()}static get watchers(){return{url:["onUpdate"],component:["onUpdate"],componentProps:["onComponentProps"]}}},d=class{constructor(n){t(this,n),this.ionRouteRedirectChanged=o(this,"ionRouteRedirectChanged",7)}propDidChange(){this.ionRouteRedirectChanged.emit()}connectedCallback(){this.ionRouteRedirectChanged.emit()}static get watchers(){return{from:["propDidChange"],to:["propDidChange"]}}},w="root",p="forward",m=t=>"/"+t.filter((t=>t.length>0)).join("/"),b=t=>{let o,n=[""];if(null!=t){const e=t.indexOf("?");e>-1&&(o=t.substring(e+1),t=t.substring(0,e)),n=t.split("/").map((t=>t.trim())).filter((t=>t.length>0)),0===n.length&&(n=[""])}return{segments:n,queryString:o}},g=async(t,o,e,r,i=!1,s)=>{try{const n=y(t);if(r>=o.length||!n)return i;await new Promise((t=>c(n,t)));const a=o[r],h=await n.setRouteId(a.id,a.params,e,s);return h.changed&&(e=w,i=!0),i=await g(h.element,o,e,r+1,i,s),h.markVisible&&await h.markVisible(),i}catch(t){return n("[ion-router] - Exception in writeNavState:",t),!1}},v=":not([no-router]) ion-nav, :not([no-router]) ion-tabs, :not([no-router]) ion-router-outlet",y=t=>{if(!t)return;if(t.matches(v))return t;const o=t.querySelector(v);return null!=o?o:void 0},R=(t,o)=>o.find((o=>((t,o)=>{const{from:n,to:e}=o;if(void 0===e)return!1;if(n.length>t.length)return!1;for(let o=0;o<n.length;o++){const e=n[o];if("*"===e)return!0;if(e!==t[o])return!1}return n.length===t.length})(t,o))),C=(t,o)=>{const n=Math.min(t.length,o.length);let e=0;for(let r=0;r<n;r++){const n=t[r],i=o[r];if(n.id.toLowerCase()!==i.id)break;if(n.params){const t=Object.keys(n.params);if(t.length===i.segments.length){const o=t.map((t=>`:${t}`));for(let t=0;t<o.length&&o[t].toLowerCase()===i.segments[t];t++)e++}}e++}return e},k=(t,o)=>{const n=new O(t);let e,r=!1;for(let t=0;t<o.length;t++){const i=o[t].segments;if(""===i[0])r=!0;else{for(const o of i){const r=n.next();if(":"===o[0]){if(""===r)return null;e=e||[],(e[t]||(e[t]={}))[o.slice(1)]=r}else if(r!==o)return null}r=!1}}return r&&r!==(""===n.next())?null:e?o.map(((t,o)=>({id:t.id,segments:t.segments,params:j(t.params,e[o]),beforeEnter:t.beforeEnter,beforeLeave:t.beforeLeave}))):o},j=(t,o)=>t||o?Object.assign(Object.assign({},t),o):void 0,D=(t,o)=>{let n=null,e=0;for(const r of o){const o=k(t,r);if(null!==o){const t=E(o);t>e&&(e=t,n=o)}}return n},E=t=>{let o=1,n=1;for(const e of t)for(const t of e.segments)":"===t[0]?o+=Math.pow(1,n):""!==t&&(o+=Math.pow(2,n)),n++;return o};class O{constructor(t){this.segments=t.slice()}next(){return this.segments.length>0?this.segments.shift():""}}const S=(t,o)=>o in t?t[o]:t.hasAttribute(o)?t.getAttribute(o):null,x=t=>Array.from(t.children).filter((t=>"ION-ROUTE-REDIRECT"===t.tagName)).map((t=>{const o=S(t,"to");return{from:b(S(t,"from")).segments,to:null==o?void 0:b(o)}})),U=t=>L($(t)),$=t=>Array.from(t.children).filter((t=>"ION-ROUTE"===t.tagName&&t.component)).map((t=>{const o=S(t,"component");return{segments:b(S(t,"url")).segments,id:o.toLowerCase(),params:t.componentProps,beforeLeave:t.beforeLeave,beforeEnter:t.beforeEnter,children:$(t)}})),L=t=>{const o=[];for(const n of t)N([],o,n);return o},N=(t,o,n)=>{if(t=[...t,{id:n.id,segments:n.segments,params:n.params,beforeLeave:n.beforeLeave,beforeEnter:n.beforeEnter}],0!==n.children.length)for(const e of n.children)N(t,o,e);else o.push(t)},P=class{constructor(n){t(this,n),this.ionRouteWillChange=o(this,"ionRouteWillChange",7),this.ionRouteDidChange=o(this,"ionRouteDidChange",7),this.previousPath=null,this.busy=!1,this.state=0,this.lastState=0,this.root="/",this.useHash=!0}async componentWillLoad(){await(y(document.body)?Promise.resolve():new Promise((t=>{window.addEventListener("ionNavWillLoad",(()=>t()),{once:!0})})));const t=await this.runGuards(this.getSegments());if(!0!==t){if("object"==typeof t){const{redirect:o}=t,n=b(o);this.setSegments(n.segments,w,n.queryString),await this.writeNavStateRoot(n.segments,w)}}else await this.onRoutesChanged()}componentDidLoad(){window.addEventListener("ionRouteRedirectChanged",h(this.onRedirectChanged.bind(this),10)),window.addEventListener("ionRouteDataChanged",h(this.onRoutesChanged.bind(this),100))}async onPopState(){const t=this.historyDirection();let o=this.getSegments();const n=await this.runGuards(o);if(!0!==n){if("object"!=typeof n)return!1;o=b(n.redirect).segments}return this.writeNavStateRoot(o,t)}onBackButton(t){t.detail.register(0,(t=>{this.back(),t()}))}async canTransition(){const t=await this.runGuards();return!0===t||"object"==typeof t&&t.redirect}async push(t,o="forward",n){var e;if(t.startsWith(".")){const o=null!==(e=this.previousPath)&&void 0!==e?e:"/",n=new URL(t,`https://host/${o}`);t=n.pathname+n.search}let r=b(t);const i=await this.runGuards(r.segments);if(!0!==i){if("object"!=typeof i)return!1;r=b(i.redirect)}return this.setSegments(r.segments,o,r.queryString),this.writeNavStateRoot(r.segments,o,n)}back(){return window.history.back(),Promise.resolve(this.waitPromise)}async printDebug(){(t=>{console.group(`[ion-core] ROUTES[${t.length}]`);for(const o of t){const t=[];o.forEach((o=>t.push(...o.segments)));const n=o.map((t=>t.id));console.debug(`%c ${m(t)}`,"font-weight: bold; padding-left: 20px","=>\t",`(${n.join(", ")})`)}console.groupEnd()})(U(this.el)),(t=>{console.group(`[ion-core] REDIRECTS[${t.length}]`);for(const o of t)o.to&&console.debug("FROM: ",`$c ${m(o.from)}`,"font-weight: bold"," TO: ",`$c ${m(o.to.segments)}`,"font-weight: bold");console.groupEnd()})(x(this.el))}async navChanged(t){if(this.busy)return e("[ion-router] - Router is busy, navChanged was cancelled."),!1;const{ids:o,outlet:n}=await(async()=>{const t=[];let o,n=window.document.body;for(;o=y(n);){const e=await o.getRouteId();if(!e)break;n=e.element,e.element=void 0,t.push(e)}return{ids:t,outlet:o}})(),r=((t,o)=>{let n=null,e=0;for(const r of o){const o=C(t,r);o>e&&(n=r,e=o)}return n?n.map(((o,n)=>{var e;return{id:o.id,segments:o.segments,params:j(o.params,null===(e=t[n])||void 0===e?void 0:e.params)}})):null})(o,U(this.el));if(!r)return e("[ion-router] - No matching URL for",o.map((t=>t.id))),!1;const i=(t=>{const o=[];for(const n of t)for(const t of n.segments)if(":"===t[0]){const e=n.params&&n.params[t.slice(1)];if(!e)return null;o.push(e)}else""!==t&&o.push(t);return o})(r);return i?(this.setSegments(i,t),await this.safeWriteNavState(n,r,w,i,null,o.length),!0):(e("[ion-router] - Router could not match path because some required param is missing."),!1)}onRedirectChanged(){const t=this.getSegments();t&&R(t,x(this.el))&&this.writeNavStateRoot(t,w)}onRoutesChanged(){return this.writeNavStateRoot(this.getSegments(),w)}historyDirection(){var t;const o=window;null===o.history.state&&(this.state++,o.history.replaceState(this.state,o.document.title,null===(t=o.document.location)||void 0===t?void 0:t.href));const n=o.history.state,e=this.lastState;return this.lastState=n,n>e||n>=e&&e>0?p:n<e?"back":w}async writeNavStateRoot(t,o,e){if(!t)return n("[ion-router] - URL is not part of the routing set."),!1;const r=x(this.el),i=R(t,r);let s=null;if(i){const{segments:n,queryString:e}=i.to;this.setSegments(n,o,e),s=i.from,t=n}const a=U(this.el),c=D(t,a);return c?this.safeWriteNavState(document.body,c,o,t,s,0,e):(n("[ion-router] - The path does not match any route."),!1)}async safeWriteNavState(t,o,e,r,i,s=0,a){const c=await this.lock();let h=!1;try{h=await this.writeNavState(t,o,e,r,i,s,a)}catch(t){n("[ion-router] - Exception in safeWriteNavState:",t)}return c(),h}async lock(){const t=this.waitPromise;let o;return this.waitPromise=new Promise((t=>o=t)),void 0!==t&&await t,o}async runGuards(t=this.getSegments(),o){if(void 0===o&&(o=b(this.previousPath).segments),!t||!o)return!0;const n=U(this.el),e=D(o,n),r=e&&e[e.length-1].beforeLeave,i=!r||await r();if(!1===i||"object"==typeof i)return i;const s=D(t,n),a=s&&s[s.length-1].beforeEnter;return!a||a()}async writeNavState(t,o,n,r,i,s=0,a){if(this.busy)return e("[ion-router] - Router is busy, transition was cancelled."),!1;this.busy=!0;const c=this.routeChangeEvent(r,i);c&&this.ionRouteWillChange.emit(c);const h=await g(t,o,n,s,!1,a);return this.busy=!1,c&&this.ionRouteDidChange.emit(c),h}setSegments(t,o,n){this.state++,((t,o,n,e,r,i,s)=>{const a=((t,o,n)=>{let e=m(t);return o&&(e="#"+e),void 0!==n&&(e+="?"+n),e})([...b(o).segments,...e],n,s);r===p?t.pushState(i,"",a):t.replaceState(i,"",a)})(window.history,this.root,this.useHash,t,o,this.state,n)}getSegments(){return((t,o,n)=>{const e=b(this.root).segments,r=n?t.hash.slice(1):t.pathname;return((t,o)=>{if(t.length>o.length)return null;if(t.length<=1&&""===t[0])return o;for(let n=0;n<t.length;n++)if(t[n]!==o[n])return null;return o.length===t.length?[""]:o.slice(t.length)})(e,b(r).segments)})(window.location,0,this.useHash)}routeChangeEvent(t,o){const n=this.previousPath,e=m(t);return this.previousPath=e,e===n?null:{from:n,redirectedFrom:o?m(o):null,to:e}}get el(){return r(this)}},T=class{constructor(o){t(this,o),this.routerDirection="forward",this.onClick=t=>{u(this.href,t,this.routerDirection,this.routerAnimation)}}render(){const t=i(this),o={href:this.href,rel:this.rel,target:this.target};return s(a,{key:"d7f2affcde45c5fbb6cb46cd1c30008ee92a68c5",onClick:this.onClick,class:l(this.color,{[t]:!0,"ion-activatable":!0})},s("a",Object.assign({key:"babafae85ca5c6429958d383feff0493ff8cf33e"},o),s("slot",{key:"50314e9555bbf6dffa0c50c3f763009dee59b10b"})))}};T.style=":host{--background:transparent;--color:var(--ion-color-primary, #0054e9);background:var(--background);color:var(--color)}:host(.ion-color){color:var(--ion-color-base)}a{font-family:inherit;font-size:inherit;font-style:inherit;font-weight:inherit;letter-spacing:inherit;text-decoration:inherit;text-indent:inherit;text-overflow:inherit;text-transform:inherit;text-align:inherit;white-space:inherit;color:inherit}";export{f as ion_route,d as ion_route_redirect,P as ion_router,T as ion_router_link}