UNPKG

@ionic/core

Version:

Base components for Ionic

64 lines (63 loc) • 1.61 kB
import { parsePath } from './path'; const readProp = (el, prop) => { if (prop in el) { return el[prop]; } if (el.hasAttribute(prop)) { return el.getAttribute(prop); } return null; }; export const readRedirects = (root) => { return Array.from(root.children) .filter(el => el.tagName === 'ION-ROUTE-REDIRECT') .map(el => { const to = readProp(el, 'to'); return { from: parsePath(readProp(el, 'from')).segments, to: to == null ? undefined : parsePath(to), }; }); }; export const readRoutes = (root) => { return flattenRouterTree(readRouteNodes(root)); }; export const readRouteNodes = (node) => { return Array.from(node.children) .filter(el => el.tagName === 'ION-ROUTE' && el.component) .map(el => { const component = readProp(el, 'component'); return { path: parsePath(readProp(el, 'url')).segments, id: component.toLowerCase(), params: el.componentProps, beforeLeave: el.beforeLeave, beforeEnter: el.beforeEnter, children: readRouteNodes(el) }; }); }; export const flattenRouterTree = (nodes) => { const chains = []; for (const node of nodes) { flattenNode([], chains, node); } return chains; }; const flattenNode = (chain, chains, node) => { chain = chain.slice(); chain.push({ id: node.id, path: node.path, params: node.params, beforeLeave: node.beforeLeave, beforeEnter: node.beforeEnter }); if (node.children.length === 0) { chains.push(chain); return; } for (const child of node.children) { flattenNode(chain, chains, child); } };