UNPKG

@ionic/core

Version:

Base components for Ionic

77 lines (76 loc) • 2.32 kB
import { componentOnReady } from '../../../utils/helpers'; import { ROUTER_INTENT_NONE } from './constants'; export const writeNavState = async (root, chain, direction, index, changed = false, animation) => { try { // find next navigation outlet in the DOM const outlet = searchNavNode(root); // make sure we can continue interacting the DOM, otherwise abort if (index >= chain.length || !outlet) { return changed; } await new Promise(resolve => componentOnReady(outlet, resolve)); const route = chain[index]; const result = await outlet.setRouteId(route.id, route.params, direction, animation); // if the outlet changed the page, reset navigation to neutral (no direction) // this means nested outlets will not animate if (result.changed) { direction = ROUTER_INTENT_NONE; changed = true; } // recursively set nested outlets changed = await writeNavState(result.element, chain, direction, index + 1, changed, animation); // once all nested outlets are visible let's make the parent visible too, // using markVisible prevents flickering if (result.markVisible) { await result.markVisible(); } return changed; } catch (e) { console.error(e); return false; } }; export const readNavState = async (root) => { const ids = []; let outlet; let node = root; // tslint:disable-next-line:no-constant-condition while (true) { outlet = searchNavNode(node); if (outlet) { const id = await outlet.getRouteId(); if (id) { node = id.element; id.element = undefined; ids.push(id); } else { break; } } else { break; } } return { ids, outlet }; }; export const waitUntilNavNode = () => { if (searchNavNode(document.body)) { return Promise.resolve(); } return new Promise(resolve => { window.addEventListener('ionNavWillLoad', resolve, { once: true }); }); }; const QUERY = ':not([no-router]) ion-nav, :not([no-router]) ion-tabs, :not([no-router]) ion-router-outlet'; const searchNavNode = (root) => { if (!root) { return undefined; } if (root.matches(QUERY)) { return root; } const outlet = root.querySelector(QUERY); return outlet !== null && outlet !== void 0 ? outlet : undefined; };