UNPKG

@ionic/core

Version:
4 lines • 12.3 kB
/*! * (C) Ionic http://ionicframework.com - MIT License */ import{r as t,d as o,c as n,a as e,g as i,h as r,f as s}from"./p-BpRUsN_W.js";import{c as a,r as c,u as h}from"./p-Co7eql8D.js";import{a as u,i as l,g as f}from"./p-CXs4J83x.js";import{o as d,c as w}from"./p-byZM6qHV.js";import{b as m}from"./p-SEecsUh7.js";import"./p-Dojwmvde.js";const p=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:0}],component:[{onUpdate:0}],componentProps:[{onComponentProps:0}]}}},g=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:0}],to:[{propDidChange:0}]}}},b="root",y="forward",v=t=>"/"+t.filter((t=>t.length>0)).join("/"),C=t=>{let o,n,e=[""];if(null!=t){const i=t.indexOf("#");i>-1&&(n=t.substring(i+1),t=t.substring(0,i));const r=t.indexOf("?");r>-1&&(o=t.substring(r+1),t=t.substring(0,r)),e=t.split("/").map((t=>t.trim())).filter((t=>t.length>0)),0===e.length&&(e=[""])}return{segments:e,queryString:o,fragment:n}},R=async(t,o,e,i,r=!1,s)=>{try{const n=E(t);if(i>=o.length||!n)return r;await new Promise((t=>a(n,t)));const c=o[i],h=await n.setRouteId(c.id,c.params,e,s);return h.changed&&(e=b,r=!0),r=await R(h.element,o,e,i+1,r,s),h.markVisible&&await h.markVisible(),r}catch(t){return n("[ion-router] - Exception in writeNavState:",t),!1}},S=()=>new Promise((t=>c((()=>t())))),k=t=>{const o=t.closest(".ion-page");return null===o?null===document.querySelector(".ion-page"):null===o.closest(".ion-page-hidden, .tab-hidden")},j=":not([no-router]) ion-tabs, :not([no-router]) ion-router-outlet",E=t=>{if(t)return t.matches(j)?t:t.querySelector(j)??void 0},D=(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))),$=(t,o)=>{const n=Math.min(t.length,o.length);let e=0;for(let i=0;i<n;i++){const n=t[i],r=o[i];if(n.id.toLowerCase()!==r.id)break;if(n.params){const t=Object.keys(n.params);if(t.length===r.segments.length){const o=t.map((t=>`:${t}`));for(let t=0;t<o.length&&o[t].toLowerCase()===r.segments[t];t++)e++}}e++}return e},x=(t,o)=>{const n=new L(t);let e,i=!1;for(let t=0;t<o.length;t++){const r=o[t].segments;if(""===r[0])i=!0;else{for(const o of r){const i=n.next();if(":"===o[0]){if(""===i)return null;e=e||[],(e[t]||(e[t]={}))[o.slice(1)]=i}else if(i!==o)return null}i=!1}}return i&&i!==(""===n.next())?null:e?o.map(((t,o)=>({id:t.id,segments:t.segments,params:T(t.params,e[o]),beforeEnter:t.beforeEnter,beforeLeave:t.beforeLeave}))):o},T=(t,o)=>t||o?{...t,...o}:void 0,U=(t,o)=>{let n=null,e=0;for(const i of o){const o=x(t,i);if(null!==o){const t=N(o);t>e&&(e=t,n=o)}}return n},N=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 L{constructor(t){this.segments=t.slice()}next(){return this.segments.length>0?this.segments.shift():""}}const O=(t,o)=>o in t?t[o]:t.hasAttribute(o)?t.getAttribute(o):null,P=t=>Array.from(t.children).filter((t=>"ION-ROUTE-REDIRECT"===t.tagName)).map((t=>{const o=O(t,"to");return{from:C(O(t,"from")).segments,to:null==o?void 0:C(o)}})),W=t=>q(_(t)),_=t=>Array.from(t.children).filter((t=>"ION-ROUTE"===t.tagName&&Boolean(t.component))).map((t=>{const o=O(t,"component");return{segments:C(O(t,"url")).segments,id:o.toLowerCase(),params:t.componentProps,beforeLeave:t.beforeLeave,beforeEnter:t.beforeEnter,children:_(t)}})),q=t=>{const o=[];for(const n of t)F([],o,n);return o},F=(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)F(t,o,e);else o.push(t)},I=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.fragmentScrollToken=0,this.root="/",this.useHash=!0}async componentWillLoad(){await(E(document.body)?Promise.resolve():new Promise((t=>{const o=()=>{window.removeEventListener("ionNavWillLoad",o),clearTimeout(n),t()},n=setTimeout(o,500);window.addEventListener("ionNavWillLoad",o,{once:!0})})));const t=await this.runGuards(this.getSegments());if(!0===t)await this.onRoutesChanged()&&this.maybeScrollToFragment();else if("object"==typeof t){const{redirect:o}=t,n=C(o);this.setSegments(n.segments,b,n.queryString,n.fragment),await this.writeNavStateRoot(n.segments,b)&&this.maybeScrollToFragment()}}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=C(n.redirect).segments}const e=await this.writeNavStateRoot(o,t);return e&&this.maybeScrollToFragment(),e}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){if(t.startsWith(".")){const o=new URL(t,`https://host/${this.previousPath??"/"}`);t=o.pathname+o.search+o.hash}let e=C(t);const i=await this.runGuards(e.segments);if(!0!==i){if("object"!=typeof i)return!1;e=C(i.redirect)}this.setSegments(e.segments,o,e.queryString,e.fragment);const r=await this.writeNavStateRoot(e.segments,o,n);return r&&this.maybeScrollToFragment(),r}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 ${v(t)}`,"font-weight: bold; padding-left: 20px","=>\t",`(${n.join(", ")})`)}console.groupEnd()})(W(this.el)),(t=>{console.group(`[ion-core] REDIRECTS[${t.length}]`);for(const o of t)o.to&&console.debug("FROM: ",`$c ${v(o.from)}`,"font-weight: bold"," TO: ",`$c ${v(o.to.segments)}`,"font-weight: bold");console.groupEnd()})(P(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=E(n);){const e=await o.getRouteId();if(!e)break;n=e.element,e.element=void 0,t.push(e)}return{ids:t,outlet:o}})(),i=((t,o)=>{let n=null,e=0;for(const i of o){const o=$(t,i);o>e&&(n=i,e=o)}return n?n.map(((o,n)=>({id:o.id,segments:o.segments,params:T(o.params,t[n]?.params)}))):null})(o,W(this.el));if(!i)return e("[ion-router] - No matching URL for",o.map((t=>t.id))),!1;const r=(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})(i);if(!r)return e("[ion-router] - Router could not match path because some required param is missing."),!1;const s=v(r)===this.previousPath?this.getFragment():void 0;return this.setSegments(r,t,void 0,s),await this.safeWriteNavState(n,i,b,r,null,o.length),!0}onRedirectChanged(){const t=this.getSegments();t&&D(t,P(this.el))&&this.writeNavStateRoot(t,b)}onRoutesChanged(){return this.writeNavStateRoot(this.getSegments(),b)}historyDirection(){const t=window;null===t.history.state&&(this.state++,t.history.replaceState(this.state,t.document.title,t.document.location?.href));const o=t.history.state,n=this.lastState;return this.lastState=o,o>n||o>=n&&n>0?y:o<n?"back":b}async writeNavStateRoot(t,o,e){if(!t)return n("[ion-router] - URL is not part of the routing set."),!1;const i=P(this.el),r=D(t,i);let s=null;if(r){const{segments:n,queryString:e,fragment:i}=r.to;this.setSegments(n,o,e,i),s=r.from,t=n}const a=W(this.el),c=U(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,i,r,s=0,a){const c=await this.lock();let h=!1;try{h=await this.writeNavState(t,o,e,i,r,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=C(this.previousPath).segments),!t||!o)return!0;const n=W(this.el),e=U(o,n),i=e&&e[e.length-1].beforeLeave,r=!i||await i();if(!1===r||"object"==typeof r)return r;const s=U(t,n),a=s&&s[s.length-1].beforeEnter;return!a||a()}async writeNavState(t,o,n,i,r,s=0,a){if(this.busy)return e("[ion-router] - Router is busy, transition was cancelled."),!1;this.busy=!0;const c=this.routeChangeEvent(i,r);c&&this.ionRouteWillChange.emit(c);const h=await R(t,o,n,s,!1,a);return this.busy=!1,c&&this.ionRouteDidChange.emit(c),h}setSegments(t,o,n,e){this.state++,this.fragmentScrollToken++,((t,o,n,e,i,r,s,a)=>{const c=((t,o,n,e)=>{let i=v(t);return o&&(i="#"+i),void 0!==n&&(i+="?"+n),void 0!==e&&(i+="#"+e),i})([...C(o).segments,...e],n,s,a);i===y?t.pushState(r,"",c):t.replaceState(r,"",c)})(window.history,this.root,this.useHash,t,o,this.state,n,e)}getSegments(){return((t,o,n)=>{const e=C(this.root).segments,i=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,C(i).segments)})(window.location,0,this.useHash)}getFragment(){return(this.useHash?C(window.location.hash.slice(1)).fragment:window.location.hash.slice(1))||void 0}maybeScrollToFragment(){const t=this.getFragment();if(!t)return;const o=this.fragmentScrollToken;(async(t,o=()=>!0)=>{if(null==t||""===t)return!1;let e;try{e=decodeURIComponent(t)}catch{e=t}const i=await(async(t,o)=>{const n="undefined"!=typeof CSS&&"function"==typeof CSS.escape?CSS.escape(t):null;for(let e=0;e<30;e++){if(!o())return null;let e=[];if(null!==n)try{e=[...document.querySelectorAll(`#${n}, a[name="${n}"]`)]}catch{e=[...document.querySelectorAll(`#${n}`)]}else{const o=document.getElementById(t);null!==o&&(e=[o])}for(let t=e.length-1;t>=0;t--)if(k(e[t]))return e[t];await S()}return null})(e,o);if(!i||!o())return!1;try{const t=u(i);if(t&&l(t)){const n=t,e=await f(n);if(await S(),!o())return!1;const r=i.getBoundingClientRect(),s=e.getBoundingClientRect(),a=r.top-s.top+e.scrollTop;await n.scrollToPoint(e.scrollLeft,a,300)}else i.scrollIntoView({behavior:"smooth"});return!0}catch(t){return n("[ion-router] - Exception in scrollToFragment:",t),!1}})(t,(()=>o===this.fragmentScrollToken)).catch((()=>{}))}routeChangeEvent(t,o){const n=this.previousPath,e=v(t);return this.previousPath=e,e===n?null:{from:n,redirectedFrom:o?v(o):null,to:e}}get el(){return i(this)}},M=class{constructor(o){t(this,o),this.routerDirection="forward",this.onClick=t=>{d(this.href,t,this.routerDirection,this.routerAnimation)}}render(){const t=m(this),o={href:this.href,rel:this.rel,target:this.target};return r(s,{key:"8aaf732d8f5cc4ced351e2b3164605c10bd90cad",onClick:this.onClick,class:w(this.color,{[t]:!0,"ion-activatable":!0})},r("a",{key:"8f291ffbbce5b08dd10720d855eb020880bf1be1",...o},r("slot",{key:"24dc388a49204011e1c1f0a1131c9a1b0a4ae4da"})))}};M.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{p as ion_route,g as ion_route_redirect,I as ion_router,M as ion_router_link}