UNPKG

@kospa/router

Version:

Router component for kospa framework

163 lines (162 loc) 5.27 kB
import * as ko from "knockout"; import * as composer from "@kospa/base/composer"; import * as activator from "@kospa/base/activator"; export interface Options { mode?: string; root?: string; onError?: (err: any) => any; } export interface Route { matcher?: RegExp | null; handlers: RouteHandler[]; } export interface Routes { [key: string]: Route; none: Route; } export declare type RouteHandler = (...args: any[]) => any; export declare class BaseRouter { private static skip; private _current?; private timeout?; private _onError?; private rootRegExp?; private routes; mode: string; root: string; constructor(options?: Options); /** * Add middlewares handlers for all routes. * * @param handlers Middlewares to add on all routes */ use(...handlers: RouteHandler[]): BaseRouter; /** * Add middlewares for all routes that match the given path prefix or regex. * * @param path Path prefix or regex that enable the middlewares * @param handlers Middlewares to add on given path prefix or regex */ use(path: string | RegExp, ...handlers: RouteHandler[]): BaseRouter; /** * Remove middlewares from all routes. * * @param handlers Middlewares to remove from all routes */ unuse(...handlers: RouteHandler[]): BaseRouter; /** * Remove middlewares from all routes that match the given path prefix or regex. * * @param path Path prefix or regex on which middlewares were registered * @param handlers Middlewares to remove */ unuse(path: string | RegExp, ...handlers: RouteHandler[]): BaseRouter; /** * Register handlers for given path. * * @param path Path or regex * @param handlers Handlers for the route */ add(path: string | RegExp, ...handlers: RouteHandler[]): BaseRouter; /** * Remove handlers from given path. * * @param path Path or regex * @param handlers Handlers to unregister */ remove(path: string | RegExp, ...handlers: RouteHandler[]): BaseRouter; /** * Register handlers that are executed when no route is found. * * @param handlers Middlewares and route config to use when no route is found */ none(...handlers: RouteHandler[]): BaseRouter; /** Start listening for URL changes. */ start(): BaseRouter; /** Stop listening for route changes */ stop(): BaseRouter; /** Stop and clear all configs. */ clear(): BaseRouter; /** * Navigate to the given path. * * @param path Path to navigate to (/ if empty) */ navigate(path?: string): BaseRouter; /** * Replace the current path with the given one. * * @param path Path to navigate to (/ if empty) * @param skipHandling true to avoid handling the new path */ replace(path?: string, skipHandling?: boolean): BaseRouter; /** * Manually handle the given route. * * @param fragment Fragment to handle (current path if not specifed) */ handle(fragment?: string | null): Promise<BaseRouter>; /** Get current fragment path. */ getFragment(): string | null; /** Allows to override the error handling. */ protected onError(err: Error): void; } export interface ViewModelRoute extends composer.CompositionOptions { path: string | RegExp; href?: ko.MaybeSubscribable<string | null | undefined>; title?: ko.MaybeSubscribable<string | null | undefined>; visible?: ko.MaybeSubscribable<boolean | null | undefined>; handler?: RouteHandler; } export declare class Router extends BaseRouter { private routeHandlers; currentRoute: ko.Observable<ViewModelRoute | null | undefined>; currentViewModel: activator.ActivateObservable<activator.ViewModel | null>; isNavigating: ko.Observable<boolean>; navigation: ko.PureComputed<ViewModelRoute[]>; constructor(options?: Options); /** * Configure a route for a ViewModel. * * @param config Configuration to register */ route(config: ViewModelRoute): Router; /** * Unregister a route for a ViewModel * * @param config Configuration to remove */ deroute(config: ViewModelRoute): Router; /** * Register a ViewModel when no handlers match the current route. * * @param config Configuration for the not found handler */ notFound(config: ViewModelRoute): Router; /** * Register a child router for the given prefix. * * @param path Path prefix for the child router * @param childRouter Child router */ child(path: string, childRouter?: Router): Router; /** Stop the router and clear all configs. */ clear(): Router; /** * Manually handle the given fragment. * * @param fragment URL fragment to handle (current fragment if empty) */ handle(fragment?: string): Promise<Router>; } declare const rootRouter: Router; export default rootRouter; declare module "knockout" { interface BindingHandlers { router: { init(element: Element, valueAccessor: () => Router | { router: Router; } | undefined): BindingHandlerControlsDescendant; }; } }