File

projects/core/src/routing/facade/routing.service.ts

Index

Methods

Constructor

constructor(store: Store, winRef: WindowRef, semanticPathService: SemanticPathService, routingParamsService: RoutingParamsService, router: Router, location: Location)
Parameters :
Name Type Optional
store Store<RouterState> No
winRef WindowRef No
semanticPathService SemanticPathService No
routingParamsService RoutingParamsService No
router Router No
location Location No

Methods

back
back()

Navigating back

Returns : void
changeNextPageContext
changeNextPageContext(pageContext: PageContext)

Allow to change next page context for the ongoing navigation

Parameters :
Name Type Optional
pageContext PageContext No
Returns : void
forward
forward()

Navigating forward

Returns : void
getFullUrl
getFullUrl(commands: UrlCommands, extras?: NavigationExtras)

Returns the absolute url for the given UrlCommands and NavigationExtras.

The absolute url uses the origin of the current location.

Parameters :
Name Type Optional
commands UrlCommands No
extras NavigationExtras Yes
Returns : string
getNextPageContext
getNextPageContext()

Get the next PageContext from the state

getPageContext
getPageContext()

Get the PageContext from the state

getParams
getParams()

Get the list of all parameters of the full route. This includes active child routes.

Returns : Observable<literal type>
getRouterState
getRouterState()

Get the current router state

Returns : Observable<RouterState>
getUrl
getUrl(commands: UrlCommands, extras?: NavigationExtras)

Resolves the relative url for the given UrlCommands and NavigationExtras.

The absolute url can be resolved using getFullUrl().

Parameters :
Name Type Optional
commands UrlCommands No
extras NavigationExtras Yes
Returns : string
go
go(commands: UrlCommands, extras?: NavigationExtras)

Navigation with a new state into history

     to `false` when navigation fails, or is rejected on error.
Parameters :
Name Type Optional Description
commands UrlCommands No

: url commands

extras NavigationExtras Yes

: Represents the extra options used during navigation.

Returns : Promise<boolean>

Promise that resolves to true when navigation succeeds, to false when navigation fails, or is rejected on error.

goByUrl
goByUrl(url: string, extras?: NavigationBehaviorOptions)

Navigation using absolute route path

     to `false` when navigation fails, or is rejected on error.
Parameters :
Name Type Optional Description
url string No
extras NavigationBehaviorOptions Yes

: Represents the extra options used during navigation.

Returns : Promise<boolean>

Promise that resolves to true when navigation succeeds, to false when navigation fails, or is rejected on error.

isNavigating
isNavigating()

Get the isNavigating info from the state

Returns : Observable<boolean>
Protected navigate
navigate(path: any[], extras?: NavigationExtras)

Navigation with a new state into history

     to `false` when navigation fails, or is rejected on error.
Parameters :
Name Type Optional Description
path any[] No
extras NavigationExtras Yes

: Represents the extra options used during navigation.

Returns : Promise<boolean>

Promise that resolves to true when navigation succeeds, to false when navigation fails, or is rejected on error.

import { Location } from '@angular/common';
import { Injectable } from '@angular/core';
import {
  NavigationBehaviorOptions,
  NavigationExtras,
  Router,
} from '@angular/router';
import { select, Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { WindowRef } from '../../window/window-ref';
import { SemanticPathService } from '../configurable-routes/url-translation/semantic-path.service';
import { UrlCommands } from '../configurable-routes/url-translation/url-command';
import { PageContext } from '../models/page-context.model';
import { RoutingActions } from '../store/actions/index';
import { RouterState } from '../store/routing-state';
import { RoutingSelector } from '../store/selectors/index';
import { RoutingParamsService } from './routing-params.service';

@Injectable({
  providedIn: 'root',
})
export class RoutingService {
  constructor(
    protected store: Store<RouterState>,
    protected winRef: WindowRef,
    protected semanticPathService: SemanticPathService,
    protected routingParamsService: RoutingParamsService,
    protected router: Router,
    protected location: Location
  ) {}

  /**
   * Get the list of all parameters of the full route. This includes
   * active child routes.
   */
  getParams(): Observable<{ [key: string]: string }> {
    return this.routingParamsService?.getParams();
  }

  /**
   * Get the current router state
   */
  getRouterState(): Observable<RouterState> {
    return this.store.pipe(select(RoutingSelector.getRouterState));
  }

  /**
   * Get the `PageContext` from the state
   */
  getPageContext(): Observable<PageContext> {
    return this.store.pipe(select(RoutingSelector.getPageContext));
  }

  /**
   * Get the next `PageContext` from the state
   */
  getNextPageContext(): Observable<PageContext> {
    return this.store.pipe(select(RoutingSelector.getNextPageContext));
  }

  /**
   * Allow to change next page context for the ongoing navigation
   *
   * @param pageContext
   */
  changeNextPageContext(pageContext: PageContext) {
    this.store.dispatch(new RoutingActions.ChangeNextPageContext(pageContext));
  }

  /**
   * Get the `isNavigating` info from the state
   */
  isNavigating(): Observable<boolean> {
    return this.store.pipe(select(RoutingSelector.isNavigating));
  }

  /**
   * Navigation with a new state into history
   * @param commands: url commands
   * @param extras: Represents the extra options used during navigation.
   *
   * @returns Promise that resolves to `true` when navigation succeeds,
   *          to `false` when navigation fails, or is rejected on error.
   */
  go(commands: UrlCommands, extras?: NavigationExtras): Promise<boolean> {
    const path = this.semanticPathService.transform(commands);
    return this.navigate(path, extras);
  }

  /**
   * Resolves the relative url for the given `UrlCommands` and `NavigationExtras`.
   *
   * The absolute url can be resolved using `getFullUrl()`.
   */
  getUrl(commands: UrlCommands, extras?: NavigationExtras): string {
    let url = this.router.serializeUrl(
      this.router.createUrlTree(
        this.semanticPathService.transform(commands),
        extras
      )
    );
    if (!url.startsWith('/')) {
      url = `/${url}`;
    }
    return url;
  }

  /**
   * Returns the absolute url for the given `UrlCommands` and `NavigationExtras`.
   *
   * The absolute url uses the origin of the current location.
   */
  getFullUrl(commands: UrlCommands, extras?: NavigationExtras) {
    return `${this.winRef.document.location.origin}${this.getUrl(
      commands,
      extras
    )}`;
  }

  /**
   * Navigation using absolute route path
   * @param url
   * @param extras: Represents the extra options used during navigation.
   *
   * @returns Promise that resolves to `true` when navigation succeeds,
   *          to `false` when navigation fails, or is rejected on error.
   */
  goByUrl(url: string, extras?: NavigationBehaviorOptions): Promise<boolean> {
    return this.router.navigateByUrl(url, extras);
  }

  /**
   * Navigating back
   */
  back(): void {
    const isLastPageInApp = this.winRef.document.referrer.includes(
      this.winRef.nativeWindow.location.origin
    );
    if (isLastPageInApp) {
      this.location.back();
      return;
    }
    this.go(['/']);
    return;
  }

  /**
   * Navigating forward
   */
  forward(): void {
    this.location.forward();
  }

  /**
   * Navigation with a new state into history
   * @param path
   * @param extras: Represents the extra options used during navigation.
   *
   * @returns Promise that resolves to `true` when navigation succeeds,
   *          to `false` when navigation fails, or is rejected on error.
   */
  protected navigate(path: any[], extras?: NavigationExtras): Promise<boolean> {
    return this.router.navigate(path, extras);
  }
}

result-matching ""

    No results matching ""