2017-02-02 00:13:57 +03:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {Route} from './config';
|
|
|
|
import {RouterStateSnapshot} from './router_state';
|
|
|
|
|
|
|
|
/**
|
2017-07-25 11:13:15 -07:00
|
|
|
* @whatItDoes Base for events the Router goes through, as opposed to events tied to a specific
|
|
|
|
* Route. `RouterEvent`s will only be fired one time for any given navigation.
|
2017-02-02 00:13:57 +03:00
|
|
|
*
|
2017-07-25 11:13:15 -07:00
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* class MyService {
|
|
|
|
* constructor(public router: Router, logger: Logger) {
|
|
|
|
* router.events.filter(e => e instanceof RouterEvent).subscribe(e => {
|
|
|
|
* logger.log(e.id, e.url);
|
|
|
|
* });
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @experimental
|
2017-02-02 00:13:57 +03:00
|
|
|
*/
|
2017-07-25 11:13:15 -07:00
|
|
|
export class RouterEvent {
|
2017-02-02 00:13:57 +03:00
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public id: number,
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public url: string) {}
|
2017-07-25 11:13:15 -07:00
|
|
|
}
|
2017-02-02 00:13:57 +03:00
|
|
|
|
2017-07-25 11:13:15 -07:00
|
|
|
/**
|
|
|
|
* @whatItDoes Base for events tied to a specific `Route`, as opposed to events for the Router
|
|
|
|
* lifecycle. `RouteEvent`s may be fired multiple times during a single navigation and will
|
|
|
|
* always receive the `Route` they pertain to.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* class MyService {
|
|
|
|
* constructor(public router: Router, spinner: Spinner) {
|
|
|
|
* router.events.filter(e => e instanceof RouteEvent).subscribe(e => {
|
|
|
|
* if (e instanceof ChildActivationStart) {
|
|
|
|
* spinner.start(e.route);
|
|
|
|
* } else if (e instanceof ChildActivationEnd) {
|
|
|
|
* spinner.end(e.route);
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export class RouteEvent {
|
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public route: Route) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents an event triggered when a navigation starts.
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export class NavigationStart extends RouterEvent {
|
2017-02-02 00:13:57 +03:00
|
|
|
/** @docsNotRequired */
|
|
|
|
toString(): string { return `NavigationStart(id: ${this.id}, url: '${this.url}')`; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents an event triggered when a navigation ends successfully.
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
2017-07-25 11:13:15 -07:00
|
|
|
export class NavigationEnd extends RouterEvent {
|
2017-02-02 00:13:57 +03:00
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
id: number,
|
2017-02-02 00:13:57 +03:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
url: string,
|
2017-02-02 00:13:57 +03:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
public urlAfterRedirects: string) {
|
|
|
|
super(id, url);
|
|
|
|
}
|
2017-02-02 00:13:57 +03:00
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
toString(): string {
|
|
|
|
return `NavigationEnd(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}')`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents an event triggered when a navigation is canceled.
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
2017-07-25 11:13:15 -07:00
|
|
|
export class NavigationCancel extends RouterEvent {
|
2017-02-02 00:13:57 +03:00
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
id: number,
|
2017-02-02 00:13:57 +03:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
url: string,
|
2017-02-02 00:13:57 +03:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
public reason: string) {
|
|
|
|
super(id, url);
|
|
|
|
}
|
2017-02-02 00:13:57 +03:00
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
toString(): string { return `NavigationCancel(id: ${this.id}, url: '${this.url}')`; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents an event triggered when a navigation fails due to an unexpected error.
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
2017-07-25 11:13:15 -07:00
|
|
|
export class NavigationError extends RouterEvent {
|
2017-02-02 00:13:57 +03:00
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
id: number,
|
2017-02-02 00:13:57 +03:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
url: string,
|
2017-02-02 00:13:57 +03:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
public error: any) {
|
|
|
|
super(id, url);
|
|
|
|
}
|
2017-02-02 00:13:57 +03:00
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
toString(): string {
|
|
|
|
return `NavigationError(id: ${this.id}, url: '${this.url}', error: ${this.error})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents an event triggered when routes are recognized.
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
2017-07-25 11:13:15 -07:00
|
|
|
export class RoutesRecognized extends RouterEvent {
|
2017-02-02 00:13:57 +03:00
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
id: number,
|
2017-02-02 00:13:57 +03:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
url: string,
|
2017-02-02 00:13:57 +03:00
|
|
|
/** @docsNotRequired */
|
|
|
|
public urlAfterRedirects: string,
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
public state: RouterStateSnapshot) {
|
|
|
|
super(id, url);
|
|
|
|
}
|
2017-02-02 00:13:57 +03:00
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
toString(): string {
|
|
|
|
return `RoutesRecognized(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-01 10:30:17 -07:00
|
|
|
* @whatItDoes Represents the start of the Guard phase of routing.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
2017-07-25 11:13:15 -07:00
|
|
|
export class GuardsCheckStart extends RouterEvent {
|
2017-07-01 10:30:17 -07:00
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
id: number,
|
2017-07-01 10:30:17 -07:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
url: string,
|
2017-07-01 10:30:17 -07:00
|
|
|
/** @docsNotRequired */
|
|
|
|
public urlAfterRedirects: string,
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
public state: RouterStateSnapshot) {
|
|
|
|
super(id, url);
|
|
|
|
}
|
2017-07-01 10:30:17 -07:00
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return `GuardsCheckStart(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents the end of the Guard phase of routing.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
2017-07-25 11:13:15 -07:00
|
|
|
export class GuardsCheckEnd extends RouterEvent {
|
2017-07-01 10:30:17 -07:00
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
id: number,
|
2017-07-01 10:30:17 -07:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
url: string,
|
2017-07-01 10:30:17 -07:00
|
|
|
/** @docsNotRequired */
|
|
|
|
public urlAfterRedirects: string,
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public state: RouterStateSnapshot,
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
public shouldActivate: boolean) {
|
|
|
|
super(id, url);
|
|
|
|
}
|
2017-07-01 10:30:17 -07:00
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return `GuardsCheckEnd(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state}, shouldActivate: ${this.shouldActivate})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents the start of the Resolve phase of routing. The timing of this
|
|
|
|
* event may change, thus it's experimental. In the current iteration it will run
|
|
|
|
* in the "resolve" phase whether there's things to resolve or not. In the future this
|
|
|
|
* behavior may change to only run when there are things to be resolved.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
2017-07-25 11:13:15 -07:00
|
|
|
export class ResolveStart extends RouterEvent {
|
2017-07-01 10:30:17 -07:00
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
id: number,
|
2017-07-01 10:30:17 -07:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
url: string,
|
2017-07-01 10:30:17 -07:00
|
|
|
/** @docsNotRequired */
|
|
|
|
public urlAfterRedirects: string,
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
public state: RouterStateSnapshot) {
|
|
|
|
super(id, url);
|
|
|
|
}
|
2017-07-01 10:30:17 -07:00
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return `ResolveStart(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents the end of the Resolve phase of routing. See note on
|
|
|
|
* {@link ResolveStart} for use of this experimental API.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
2017-07-25 11:13:15 -07:00
|
|
|
export class ResolveEnd extends RouterEvent {
|
2017-07-01 10:30:17 -07:00
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
id: number,
|
2017-07-01 10:30:17 -07:00
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
url: string,
|
2017-07-01 10:30:17 -07:00
|
|
|
/** @docsNotRequired */
|
|
|
|
public urlAfterRedirects: string,
|
|
|
|
/** @docsNotRequired */
|
2017-07-25 11:13:15 -07:00
|
|
|
public state: RouterStateSnapshot) {
|
|
|
|
super(id, url);
|
|
|
|
}
|
2017-07-01 10:30:17 -07:00
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return `ResolveEnd(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 11:13:15 -07:00
|
|
|
/**
|
|
|
|
* @whatItDoes Represents an event triggered before lazy loading a route config.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export class RouteConfigLoadStart extends RouteEvent {
|
|
|
|
toString(): string { return `RouteConfigLoadStart(path: ${this.route.path})`; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents an event triggered when a route has been lazy loaded.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export class RouteConfigLoadEnd extends RouteEvent {
|
|
|
|
toString(): string { return `RouteConfigLoadEnd(path: ${this.route.path})`; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents the start of end of the Resolve phase of routing. See note on
|
|
|
|
* {@link ChildActivationEnd} for use of this experimental API.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export class ChildActivationStart extends RouteEvent {
|
|
|
|
toString(): string { return `ChildActivationStart(path: '${this.route.path}')`; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Represents the start of end of the Resolve phase of routing. See note on
|
|
|
|
* {@link ChildActivationStart} for use of this experimental API.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export class ChildActivationEnd extends RouteEvent {
|
|
|
|
toString(): string { return `ChildActivationEnd(path: '${this.route.path}')`; }
|
|
|
|
}
|
|
|
|
|
2017-07-01 10:30:17 -07:00
|
|
|
/**
|
|
|
|
* @whatItDoes Represents a router event, allowing you to track the lifecycle of the router.
|
|
|
|
*
|
|
|
|
* The sequence of router events is:
|
2017-02-02 00:13:57 +03:00
|
|
|
*
|
2017-02-15 10:57:03 -08:00
|
|
|
* - {@link NavigationStart},
|
2017-07-01 10:30:17 -07:00
|
|
|
* - {@link RouteConfigLoadStart},
|
|
|
|
* - {@link RouteConfigLoadEnd},
|
|
|
|
* - {@link RoutesRecognized},
|
|
|
|
* - {@link GuardsCheckStart},
|
2017-07-25 11:13:15 -07:00
|
|
|
* - {@link ChildActivationStart},
|
2017-07-01 10:30:17 -07:00
|
|
|
* - {@link GuardsCheckEnd},
|
|
|
|
* - {@link ResolveStart},
|
|
|
|
* - {@link ResolveEnd},
|
2017-07-25 11:13:15 -07:00
|
|
|
* - {@link ChildActivationEnd}
|
2017-02-15 10:57:03 -08:00
|
|
|
* - {@link NavigationEnd},
|
|
|
|
* - {@link NavigationCancel},
|
2017-07-01 10:30:17 -07:00
|
|
|
* - {@link NavigationError}
|
2017-02-02 00:13:57 +03:00
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
2017-07-25 11:13:15 -07:00
|
|
|
export type Event = RouterEvent | RouteEvent;
|