/** * The {@link Navigator} service lets you navigate to a new location within the * application, or update the current location with new data. A location is * defined by a path and optional data. * * When navigating to a new location, a new entry will be pushed to the history * stack in the browser and the {@link EventDispatcher} will emit a * {@link NavigationEvent}. * * New locations can be registered using the {@link RouteRegistry} service * * @example * // Listen on navigation events * eventDispatcher.addListener('navigate', onNavigate); * navigator.navigate('/object/deal/1001'); * * function onNavigate(event: NavigationEvent) { * console.log(event.detail); * } * * @experimental */ export interface Navigator { /** * Navigate to a new location * * @emits navigate - When the location has been changed the * {@link EventDispatcher} will emit a {@link NavigationEvent} * * @param {string} path path to the location * @param query query string parameters to append to the URL */ navigate(path: string, query?: Location['query']): void; /** * Navigate to a new location or update the current location with new data * * By default, it is automatically decided whether to replace the current * history entry or push a new one. If no new path is provided, or if it * resolves to an unchanged path of the URL (not including the query string * or fragment), the current history entry will be updated with the * provided location data. * * You can also set `location.method` to push or replace instead. * When pushing a new entry, `location.state` defaults to `null`. * When replacing, state will only be changed if a new value is provided. * * @emits navigate - When the location has been changed the * {@link EventDispatcherService} will emit a {@link NavigationEvent} * * @param location a new location or parts of the location to replace */ navigate(location: LocationChange): void; /** * Get the current location * * @return {Location} */ getLocation(): Location; /** * Add a blocker that can prevent navigation away from the current page, for * example when there is some unsaved data that would otherwise be discarded. * * If the blocker function returns true navigation is blocked. If it returns * false navigation will proceed as normal, if not blocked by other blockers. * * To later on resume navigation that was blocked, you can call the supplied * {@link Transition.retry} method. * * @note Blockers are not guaranteed to be executed upon a navigation event. * Blockers will be executed until either all returns false or up until one * of them returns true. * * @example * navigator.addBlocker(myBlocker); * * function myBlocker(transition) { * showConfirmDialog().then((shouldNavigate) => { * if (shouldNavigate) { * navigator.removeBlocker(myBlocker); * transition.retry() * } * }); * return true; * } * * @param {Blocker} blocker function to be called before navigation */ addBlocker(blocker: Blocker): void; /** * Removes a previously configured blocker. * * @param {Blocker} blocker previously added blocker function */ removeBlocker(blocker: Blocker): void; /** * Create a URL for the given location * * @param {Partial} location the location to create a URL for. * Any `state` of the location will be ignored. * * @return {URL} the URL to the location */ createUrl(location: Partial): URL; } export declare type Location = { /** * The path to the location */ path: string; /** * The parsed query string parameters. The values are parsed using `JSON.parse()`. */ query: Record; /** * The URL fragment identifier, beginning with a #. */ hash: string; /** * The history state. */ state: unknown; }; export declare type LocationChange = Partial & { /** * Specifies whether to push a new entry or replace the current entry in * the history stack */ method?: 'push' | 'replace'; }; /** * @event navigate - Emitted when a new location is navigated to or the current * location is updated */ export declare type NavigationEvent = CustomEvent; /** * Function to retry navigation */ export declare type Retry = () => void; /** * {@link Transition} describes the transition when navigating from a * {@link Location} to another {@link Location}. * * @param {Location} to location to navigate to * @param {Location} from current location before navigation * @param {Retry} retry function to retry navigation */ export declare type Transition = { to: Location; from: Location; retry: Retry; }; /** * Function to determine if navigation should be blocked or not. * * @param {Transition} transition the transition to evaluate * @returns true if navigation should be blocked */ export declare type Blocker = (transition: Transition) => boolean;