|
@ -54,12 +54,23 @@ export interface RouteMeta { |
|
|
route?: RouteItem; |
|
|
route?: RouteItem; |
|
|
/** Trail of parent routes */ |
|
|
/** Trail of parent routes */ |
|
|
breadcrumbs?: RouteItem[]; |
|
|
breadcrumbs?: RouteItem[]; |
|
|
|
|
|
/** Order in the section */ |
|
|
|
|
|
order?: number; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type TravesalContext = RouteMeta & { |
|
|
|
|
|
currentIndex: number; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) { |
|
|
export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) { |
|
|
const breadcrumbs = getBreadcrumbs(cleanedPath, routeTree); |
|
|
const breadcrumbs = getBreadcrumbs(cleanedPath, routeTree); |
|
|
|
|
|
const ctx: TravesalContext = { |
|
|
|
|
|
currentIndex: 0, |
|
|
|
|
|
}; |
|
|
|
|
|
buildRouteMeta(cleanedPath, routeTree, ctx); |
|
|
|
|
|
const {currentIndex, ...meta} = ctx; |
|
|
return { |
|
|
return { |
|
|
...buildRouteMeta(cleanedPath, routeTree, {}), |
|
|
...meta, |
|
|
breadcrumbs: breadcrumbs.length > 0 ? breadcrumbs : [routeTree], |
|
|
breadcrumbs: breadcrumbs.length > 0 ? breadcrumbs : [routeTree], |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
@ -68,8 +79,10 @@ export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) { |
|
|
function buildRouteMeta( |
|
|
function buildRouteMeta( |
|
|
searchPath: string, |
|
|
searchPath: string, |
|
|
currentRoute: RouteItem, |
|
|
currentRoute: RouteItem, |
|
|
ctx: RouteMeta |
|
|
ctx: TravesalContext |
|
|
): RouteMeta { |
|
|
) { |
|
|
|
|
|
ctx.currentIndex++; |
|
|
|
|
|
|
|
|
const {routes} = currentRoute; |
|
|
const {routes} = currentRoute; |
|
|
|
|
|
|
|
|
if (ctx.route && !ctx.nextRoute) { |
|
|
if (ctx.route && !ctx.nextRoute) { |
|
@ -78,6 +91,7 @@ function buildRouteMeta( |
|
|
|
|
|
|
|
|
if (currentRoute.path === searchPath) { |
|
|
if (currentRoute.path === searchPath) { |
|
|
ctx.route = currentRoute; |
|
|
ctx.route = currentRoute; |
|
|
|
|
|
ctx.order = ctx.currentIndex; |
|
|
// If we've found a deeper match, reset the previously stored next route.
|
|
|
// If we've found a deeper match, reset the previously stored next route.
|
|
|
// TODO: this only works reliably if deeper matches are first in the tree.
|
|
|
// TODO: this only works reliably if deeper matches are first in the tree.
|
|
|
// We should revamp all of this to be more explicit.
|
|
|
// We should revamp all of this to be more explicit.
|
|
@ -89,14 +103,12 @@ function buildRouteMeta( |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!routes) { |
|
|
if (!routes) { |
|
|
return ctx; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (const route of routes) { |
|
|
for (const route of routes) { |
|
|
buildRouteMeta(searchPath, route, ctx); |
|
|
buildRouteMeta(searchPath, route, ctx); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return ctx; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// iterates the route tree from the current route to find its ancestors for breadcrumbs
|
|
|
// iterates the route tree from the current route to find its ancestors for breadcrumbs
|
|
|