Class Router

Targeted Singleton Router class extending the built-in Set. This Singleton class provides routing and rendering capabilities. Routing is primarily realized by maintaining the inherited Set of Routes and (recursively) matching paths against those Routes, when instructed so by the navigate method. When a matching Segment is found, the corresponding Components are rendered by the handle method (which is part of the implemented Queue contract).

Decorator

Target

Decorator

Singleton

Hierarchy

Implements

Constructors

Properties

Private static ReplaySubject used as the Router loader. This loader emits every time Routes are added, whilst the size being 0, so either for the first time after construction or after the Router was cleared.

[iterator]: never

declared well-known Symbol.iterator property. This declaration enforces correct typing when retrieving the Subscribable from the well-known Symbol.observable method by voiding the inherited Symbol.iterator.

baseHref: string

An absolute baseHref for navigation.

Default Value

'/'

hashBased: boolean

Whether to employ hashBased routing.

Default Value

false

The rendering outlet for navigated Routes.

Default Value

document.body

changes: BehaviorSubject<Router.State<string>>

Internally used BehaviorSubject containing and emitting every navigated State.

Accessors

Methods

  • connecting helper method. Calling this method will connect a handler to the global onpopstate event, invoking navigate with the appropriate arguments. This method furthermore allows the properties Router.baseHref, Router.hashBased and Router.outlet to be overridden. Invoking the connect method throws an error if called more than once, without invoking the disconnect method in between invocations.

    Parameters

    • this: Mutable<Router>

      The Mutable explicit polymorphic this parameter.

    • outlet: Element | DocumentFragment = ...

      The rendering outlet for Routes.

    • baseHref: string = ...

      An absolute baseHref for navigation.

    • hashBased: boolean = ...

      Whether to employ hashBased routing.

    Returns void

    Throws

    A ReferenceError if already connected.

  • Segment joining helper. The supplied segment is converted to a string by spooling to its top-most parent and iterating through all children while concatenating every encountered path. If said path is an (optional) parameter, this portion of the returned string is replaced by the respective Params value.

    Parameters

    Returns string

    The joined Segment in string form.

  • Lookup helper method. Calling this method while supplying a selector and optionally an array of routes to iterate will return the lookuped Route path for the supplied selector or undefined, if it does not occur within at least one route. When multiple occurrences of the same selector exist, the Route path to its first occurrence is returned.

    Parameters

    • selector: string

      The Component selector to lookup.

    • routes: Route<string>[] = ...

      An array of routes to use for lookup.

    Returns undefined | string

    The lookuped Route path or undefined.

  • Main Router matching method. Calling this method while supplying a path and optionally an array of routes will return the first matching Segment or undefined, if nothing matches. If no routes are supplied, routes previously added to the Router will be used. The match method represents the backbone of this Router class, as it, given a list of routes and a path, will determine whether this path represents a match within the list of routes, thereby effectively determining navigational integrity.

    Parameters

    • path: string

      The path to match routes against.

    • routes: Route<string>[] = ...

      An array of routes to use for matching.

    Returns undefined | Segment<string>

    The first matching Segment or undefined.

    Example

    Test if path 'example/route' matches child or route:

    import { Router } from '@sgrud/shell';

    const path = 'example/route';
    const router = new Router();

    const child = {
    path: 'route'
    };

    const route = {
    path: 'example',
    children: [child]
    };

    router.match(path, [child]); // false
    router.match(path, [route]); // true
  • rebase helper method. rebases the supplied path against the current baseHref, by either prefixing the baseHref to the supplied path or stripping it, depending on the prefix argument.

    Parameters

    • path: string

      The path to rebase against the baseHref.

    • prefix: boolean = true

      Whether to prefix or strip the baseHref.

    Returns string

    The path rebased against the baseHref.

Generated using TypeDoc