import { InjectionToken } from '@angular/core'; import { NavigationExtras, Params, Route, Router, UrlTree } from '@angular/router'; import { LfStorage } from './storage.service'; import * as i0 from "@angular/core"; /** * List of LF routes. */ export declare type LfRoutes = LfRoute[]; /** * Function to indicate whether an LF route can be activated. */ export declare type LfRouteCanActivate = (ctx: LfStorage) => boolean; /** * An Angular Router route extended with LF-specific options. */ export interface LfRoute extends Route { /** * Flag that indicates whether the route is an LF route. */ isLfRoute?: boolean; /** * Schema path that this route represents. If not absolute, this path is * relative to their parent route's LF path. These paths support the syntax * used for defining parameters within Angular routes (e.g. `:param`). */ lfPath?: string; /** * Function used to determine whether the route can be activated for a given * path. By default the route can be activated for existing non-`null` paths. */ lfCanActivate?: LfRouteCanActivate; children?: LfRoutes; } /** * Extra options used during LF navigation. */ export interface LfNavigationExtras extends NavigationExtras { /** * If, when attempting to navigate to a path which has no associated route, * the router should instead navigate to its first child that has an * associated route. Defaults to `true`. * * Example: if attempting to navigate to `/a` but there is no route associated * with `/a`, then the router will check if there exists a child path (e.g. * `/a/b`) with an associated route and navigate to the first such path * instead. When both `navigateToChild` and `navigateToParent` are `true`, the * router will first attempt to navigate to a child before attempting to * navigate to the parent. */ navigateToChild?: boolean; /** * Whether, when attempting to navigate to a path which has no associated * route, the router should instead navigate to its closest parent path with * an associated route. Defaults to `true`. * * Example: if attempting to navigate to `/a/b/c` but a route only exists for * `/a`, then the router should navigate to `/a`. When both `navigateToChild` * and `navigateToParent` are `true`, the router will first attempt to * navigate to a child before attempting to navigate to the parent. */ navigateToParent?: boolean; /** * When `navigateToParent` is `true`, this setting sets the part of the path * that didn't match the route as the URL's fragment. Defaults to `true`. * * Example: if attempting to navigate to `/a/b/c` but a route only exists for * `/a`, then the router should navigate to `/a#b/c`. */ extraPathAsFragment?: boolean; /** * Extra route parameters to use when navigating to an LF path. This is useful * if there is an LF route with, for example, `lfPath` set to `/a` but whose * Angular router path is `:param/a`. Unless `param` is specified, the LF * router will not know what path to navigate to when navigating to `/a`. */ extraParams?: Params; } /** * Result of matching a provided LF path with the `lfPath` of a route. */ export interface MatchingPath { /** * Parts of the provided path that match the route's path. */ matching: string[]; /** * Parameters used when matching the provided path with the route's path. */ params: Params; /** * Parts of the route's path missing in the provided path. */ missing: string[]; /** * Parts of the provided path that are children of the route's path. */ extra: string[]; } /** * Information on a route that matches a provided LF path. */ export interface LfMatchingRoute { /** * List of routes that match the provided path. */ routes: LfRoutes; /** * Information on the matching of the provided path with the route's `lfPath`. * `null` when the route isn't an LF route (because it is a parent of an LF * route). */ matchingPath: MatchingPath; } /** * Injection token used to set the base Angular path on which to find the LF * routes configuration. Routes not within the provided path are ignored by the * service. This makes it possible to have multiple LF apps within an Angular * app without them conflicting with each other or to simply have an LF app * within a regular Angular app without the need to add `isLfRoute: false` to * all non-LF related routes. */ export declare const LF_ROUTER_BASE_PATH: InjectionToken; /** * LF router options. */ export interface LfRouterOptions { /** * Default function that determines whether a certain LF route can activate. * An `lfCanActivate` can still be set on each LF route. By default, a given * route can activate if there exists a value in the storage for the route's * `lfPath` that isn't `null`. */ defaultCanActivate: LfRouteCanActivate; } /** * Injection token used to set the LF router options. */ export declare const LF_ROUTER_OPTIONS: InjectionToken; /** * Default function to check whether a given LF route can activate. It returns * `true` when the LF path of the route to activate corresponds to an existing * non-`null` value in the storage. */ export declare function defaultLfCanActivate(ctx: LfStorage): boolean; /** * Service used to navigate throughout the application. */ export declare class LfRouter { private lfStorage; private router; private lfRouterBasePath; private options; /** * Configuration of the LfRouter (copy of the Angular router's config but with * only LF-relevant routes and normalised access to a route's `children`). The * observable is updated whenever a new lazy-loaded route is loaded. */ private lfRouterConfig; /** * LF route currently activated. */ private activatedLfRoute; /** * Parameters of the Angular route currently activated. */ private _params; /** * URL query parameters in object notation of the Angular route currently * activated. */ private _queryParams; /** * Fragment of the Angular route currently activated. */ private _fragment; /** * LF path currently activated. */ private _path; /** * Index of the id of the current active LF path in the list of children ids * of its parent path, or `-1` when the id doesn't exist. */ private idIndex; constructor(lfStorage: LfStorage, router: Router, lfRouterBasePath: string, options: LfRouterOptions | null); /** * Currently activated LF path (`null` if no LF path is activated). */ get path(): string | null; /** * Currently activated LF path with the URL's fragment resolved onto it. If * there is no fragment, or if the resolved path is not valid, it returns the * activated path itself. */ get pathWithFragment(): string | null; /** * Parameters of the activated route. */ get params(): Readonly; /** * Query parameters of the activated route. */ get queryParams(): Readonly; /** * Fragment of the activated route (what comes after the `#`). */ get fragment(): string | null; /** * Helper function to set query parameters in the URL. * @param queryParams Query parameters to set. * @param queryParamsHandling Strategy on how to handle the parameters. * @returns Promise that resolves to `true`/`false` when the navigation * succeeds/fails or is rejected when an error happens. */ setQueryParams(queryParams: Params, queryParamsHandling?: '' | 'merge' | 'preserve' | null): Promise; /** * Returns the LF routes that match a given path, ordered from most specific * to least specific. * @param path Path to match against the routes (relative to the current * path). * @param extras Navigation extras. * @returns Routes matching the provided path. */ matchingRoutes(path: string, extras?: LfNavigationExtras): LfMatchingRoute[]; /** * Given a list of matching routes, returns the first route that is * navigateable and, if necessary, adds missing parameters to the match. * Returns `null` when no matching route is navigateable. * @param matchingRoutes List of matching routes. * @returns The first navigateable route or `null` when none was found. */ navigateableRouteFromMatchingRoutes(matchingRoutes: LfMatchingRoute[]): LfMatchingRoute | null; /** * Given a path, returns the most specific route of its matching routes that * is navigateable. Returns `null` when no matching route is navigateable. * @param path Path for which to find a matching navigateable route (relative * to the current path). * @param extras Navigation extras. * @returns The most specific navigateable route or `null` when none was * found. */ navigateableRoute(path: string, extras?: LfNavigationExtras): LfMatchingRoute | null; /** * Given a navigateable `LfMatchingRoute`, creates a URL tree for the matched * Angular path. * @param navigateableRoute Information on a navigateable LF route that * matched against some path. * @param extras Navigation extras. * @returns URL tree associated with the provided match. */ createUrlTreeFromNavigateableRoute(navigateableRoute: LfMatchingRoute, extras?: LfNavigationExtras): UrlTree; /** * Given a navigateable `LfMatchingRoute`, creates a URL for the matched * Angular path. * @param navigateableRoute Information on a navigateable LF route that * matched against some path. * @param extras Navigation extras. * @returns URL associated with the provided match. */ createUrlFromNavigateableRoute(navigateableRoute: LfMatchingRoute, extras?: LfNavigationExtras): string; /** * Navigates to the URL associated to the provided navigateable * `LfMatchingRoute`. * @param navigateableRoute Information on a navigateable LF route that * matched against some path. * @param extras Navigation extras. * @returns Promise that resolves to `true`/`false` when the navigation * succeeds/fails or is rejected when an error happens. */ navigateFromNavigateableRoute(navigateableRoute: LfMatchingRoute, extras?: LfNavigationExtras): Promise; /** * URL tree associated with a given schema path. Throws an error if no * matching navigateable route was found. * @param path Schema path for which to fetch the associated URL tree. * @param extras Navigation extras. * @returns URL tree for the provided schema path. */ createUrlTree(path: string, extras?: LfNavigationExtras): UrlTree; /** * URL associated with a given schema path. Throws an error if no matching * navigateable route was found. * @param path Schema path for which to fetch associated URL. * @param extras Navigation extras. * @returns URL for the provided schema path. */ createUrl(path: string, extras?: LfNavigationExtras): string; /** * Navigates to the URL associated to the provided schema path. Throws an * error if no matching navigateable route was found. * @param path Path whose associated URL we will navigate to. * @param extras Navigation extras. * @returns Promise that resolves to `true`/`false` when the navigation * succeeds/fails or is rejected when an error happens. */ navigate(path: string, extras?: LfNavigationExtras): Promise; /** * Whether the given path is currently active (if it is the currently * activated path or a parent of it). * @param path Path to check if is active. * @param exact Whether to check if the activated path is exactly the one * provided (and not simply a child of it). * @returns Whether the given path is active. */ isActive(path: string, exact?: boolean): boolean; /** * Whether the given path is currently active in respect with the activated * path with fragment (if it is the currently activated path with fragment or * a parent of it). * @param path Path to check if is active. * @param exact Whether to check if the activated path with fragment is * exactly the one provided (and not simply a child of it). * @returns Whether the given path is active in respect with the activated * path with fragment. */ isActiveWithFragment(path: string, exact?: boolean): boolean; /** * Transforms the Angular router's routes into LF routes and updates the * service's inner "relevant" config object. `updateConfig` should be called * manually if the Angular router's config is being changed dynamically via * `resetConfig`. */ updateConfig(): void; /** * Check for LF specific errors in the configured routes (for example, verify * that all LF routes have valid LF paths). */ private validateRoutes; /** * Convert a series of Angular routes into routes that the `LfRouter` can * understand (LF routes). This is done by setting `isLfRoute` to `true` on * each relevant route (a route that is or is a child of the base path) and * adding an `lfPath` attribute to each route which maps a given Angular route * to the LF schema path it represents. Routes with `isLfRoute` set to `false` * and their children are ignored. Wildcard paths (`'**'`) will have * `isLfRoute` set to `false` by default. Routes which define their `lfPath` * are changed to make their `lfPath` absolute. Example (with the base path * set to `'/'`): * ```typescript * toLfRoutes([ * {path: '', redirectTo: 'a', pathMatch: 'full'}, * { * path: 'a', * children: [ * {path: '', redirectTo: 'aa', pathMatch: 'full'}, * {path: 'aa', component: AA}, * {path: 'ab', component: AC, lfPath: 'ac'} * ], * }, * {path: 'b', component: B}, * {path: 'l/:index', component: L}, * {path: 'nonLf', component: NonLF, isLfRoute: false}, * {path: '**', component: NotFound} * ]) * ``` * Returns the provided routes, but transformed to the following: * ```typescript * [ * {path: '', redirectTo: 'a', pathMatch: 'full', isLfRoute: true, * lfPath: '/'}, * { * path: 'a', * children: [ * {path: '', redirectTo: 'aa', pathMatch: 'full', isLfRoute: true, * lfPath: '/a'}, * {path: 'aa', component: AA, isLfRoute: true, lfPath: '/a/aa'}, * {path: 'ab', component: AC, isLfRoute: true, lfPath: '/a/ac'} * ], * isLfRoute: true, * lfPath: '/a' * }, * {path: 'b', component: B, isLfRoute: true, lfPath: '/b'}, * {path: 'l/:index', component: F, isLfRoute: true, lfPath: '/l/:index'}, * {path: 'nonLf', component: NonLF, isLfRoute: false}, * {path: '**', component: NotFound, isLfRoute: false} * ] * ``` * @param routes Angular routes to transform into routes that the LF router * service understands. * @param parentLfPath LF schema path of the parent route of the routes being * transformed. * @returns Provided routes mutated with LF routing information. */ private toLfRoutes; /** * Routes of the router configuration which are relevant to the `LfRouter`. * Ignores non-LF routes with no children that are LF routes. Further * simplifies lazy-loaded routes by setting their `children` property with the * routes that were lazily fetched. * @param routes Routes from which to remove non-LF related routes. * @param parentPath Path of the route that precedes the `routes` being * handled. * @returns Copy of passed routes with non-LF routes removed and lazy-loaded * routes simplified. */ private relevantRoutes; /** * Whether a given route can activate for a matching path depending on its * `lfCanActivate` function. * @param route Route to check if can be activated. * @param path Path that matches the given route. * @returns Whether the given route can be activated. */ private routeCanActivate; /** * Returns the LF routes that match a given path, ordered from most specific * to least specific. * @param path Path to match against the routes. * @param extras Navigation extras. * @param routes Routes from which to fetch a matching route (router * configuration by default). * @returns Routes matching the provided path. */ private _matchingRoutes; /** * Returns whether a given path partially matches against an LF route path. * Returns `null` when the two paths don't match or, when the two paths do * match, an object containing the matching parts, matching parameters, * missing parts of the LF route path that didn't match, or extra parts of the * given path which didn't match. * @param routePath Path of the LF route. * @param path Path to check if it matches against the LF route path. * @returns Matching between an LF route path and a provided path. */ private matchPaths; /** * Given a matching of paths, returns a version of the match where the * matching path is navigateable and without missing parts or `null` when no * such match can be found. * @param matchingPath Matching between two paths, possibly with `missing` * parts. * @param route Route where such matching occurred which may possibly define * an `lfCanActivate`. * @returns Matching of paths where the matching path is navigateable and with * missing parts, or `null` when no such matching exists. */ private navigateablePathOfMatchingPath; /** * Replaces the parameters of a given path with whatever value they have * within the `params` record. Throws an error in the event of a missing * parameter. * @param path Path with parameters to replace. * @param params Params to replace on the path. * @returns Path with parameters replaced with the respective values. */ private replaceParams; /** * Saves the index of the current path's id in respect to the list of children * ids of its parent. */ private saveIdIndex; /** * Finds the previous navigateable route in respect to the current route * (which has become invalid). * @returns Previous navigateable route. */ private previousNavigateablePath; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵprov: i0.ɵɵInjectableDeclaration; }