angular-cn/packages/router/src/interfaces.ts

407 lines
10 KiB
TypeScript
Raw Normal View History

/**
* @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 {Observable} from 'rxjs';
2016-07-26 17:39:02 -04:00
import {Route} from './config';
import {ActivatedRouteSnapshot, RouterStateSnapshot} from './router_state';
import {UrlSegment, UrlTree} from './url_tree';
2016-07-26 17:39:02 -04:00
/**
* @description
2016-06-28 17:49:29 -04:00
*
* Interface that a class can implement to be a guard deciding if a route can be activated.
* If all guards return `true`, navigation will continue. If any guard returns `false`,
* navigation will be cancelled. If any guard returns a `UrlTree`, current navigation will
* be cancelled and a new navigation will be kicked off to the `UrlTree` returned from the
* guard.
*
2016-06-28 17:49:29 -04:00
* ```
2016-08-30 18:57:24 -04:00
* class UserToken {}
* class Permissions {
* canActivate(user: UserToken, id: string): boolean {
* return true;
* }
* }
*
* @Injectable()
2016-06-28 17:49:29 -04:00
* class CanActivateTeam implements CanActivate {
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
*
* canActivate(
* route: ActivatedRouteSnapshot,
* state: RouterStateSnapshot
* ): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree {
* return this.permissions.canActivate(this.currentUser, route.params.id);
2016-06-28 17:49:29 -04:00
* }
* }
*
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'team/:id',
* component: TeamCmp,
* canActivate: [CanActivateTeam]
* }
* ])
* ],
2016-08-30 18:57:24 -04:00
* providers: [CanActivateTeam, UserToken, Permissions]
* })
2016-08-30 18:57:24 -04:00
* class AppModule {}
2016-06-28 17:49:29 -04:00
* ```
*
* You can alternatively provide a function with the `canActivate` signature:
2016-06-28 17:49:29 -04:00
*
* ```
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'team/:id',
* component: TeamCmp,
* canActivate: ['canActivateTeam']
* }
* ])
* ],
* providers: [
* {
* provide: 'canActivateTeam',
* useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => true
* }
* ]
* })
2016-08-30 18:57:24 -04:00
* class AppModule {}
2016-06-28 17:49:29 -04:00
* ```
*
* @publicApi
*/
export interface CanActivate {
2016-06-08 14:13:41 -04:00
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree;
}
export type CanActivateFn = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) =>
Observable<boolean|UrlTree>| Promise<boolean|UrlTree>| boolean | UrlTree;
/**
* @description
*
* Interface that a class can implement to be a guard deciding if a child route can be activated.
* If all guards return `true`, navigation will continue. If any guard returns `false`,
* navigation will be cancelled. If any guard returns a `UrlTree`, current navigation will
* be cancelled and a new navigation will be kicked off to the `UrlTree` returned from the
* guard.
*
* ```
2016-08-30 18:57:24 -04:00
* class UserToken {}
* class Permissions {
* canActivate(user: UserToken, id: string): boolean {
* return true;
* }
* }
*
* @Injectable()
* class CanActivateTeam implements CanActivateChild {
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
*
* canActivateChild(
* route: ActivatedRouteSnapshot,
* state: RouterStateSnapshot
* ): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree {
2016-07-26 17:39:02 -04:00
* return this.permissions.canActivate(this.currentUser, route.params.id);
* }
* }
*
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'root',
* canActivateChild: [CanActivateTeam],
* children: [
* {
* path: 'team/:id',
* component: Team
* }
* ]
* }
* ])
* ],
2016-08-30 18:57:24 -04:00
* providers: [CanActivateTeam, UserToken, Permissions]
* })
2016-08-30 18:57:24 -04:00
* class AppModule {}
* ```
*
* You can alternatively provide a function with the `canActivateChild` signature:
*
* ```
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'root',
* canActivateChild: ['canActivateTeam'],
* children: [
* {
* path: 'team/:id',
* component: Team
* }
* ]
* }
* ])
* ],
* providers: [
* {
* provide: 'canActivateTeam',
* useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => true
* }
* ]
* })
2016-08-30 18:57:24 -04:00
* class AppModule {}
* ```
*
* @publicApi
*/
export interface CanActivateChild {
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot):
Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree;
}
export type CanActivateChildFn = (childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot) =>
Observable<boolean|UrlTree>| Promise<boolean|UrlTree>| boolean | UrlTree;
/**
* @description
2016-06-28 17:49:29 -04:00
*
* Interface that a class can implement to be a guard deciding if a route can be deactivated.
* If all guards return `true`, navigation will continue. If any guard returns `false`,
* navigation will be cancelled. If any guard returns a `UrlTree`, current navigation will
* be cancelled and a new navigation will be kicked off to the `UrlTree` returned from the
* guard.
*
2016-06-28 17:49:29 -04:00
* ```
2016-08-30 18:57:24 -04:00
* class UserToken {}
* class Permissions {
* canDeactivate(user: UserToken, id: string): boolean {
* return true;
* }
* }
*
* @Injectable()
* class CanDeactivateTeam implements CanDeactivate<TeamComponent> {
2016-06-28 17:49:29 -04:00
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
*
* canDeactivate(
* component: TeamComponent,
* currentRoute: ActivatedRouteSnapshot,
* currentState: RouterStateSnapshot,
* nextState: RouterStateSnapshot
* ): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree {
2016-07-26 17:39:02 -04:00
* return this.permissions.canDeactivate(this.currentUser, route.params.id);
2016-06-28 17:49:29 -04:00
* }
* }
*
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'team/:id',
* component: TeamCmp,
* canDeactivate: [CanDeactivateTeam]
* }
* ])
* ],
2016-08-30 18:57:24 -04:00
* providers: [CanDeactivateTeam, UserToken, Permissions]
* })
2016-08-30 18:57:24 -04:00
* class AppModule {}
2016-06-28 17:49:29 -04:00
* ```
*
* You can alternatively provide a function with the `canDeactivate` signature:
2016-06-28 17:49:29 -04:00
*
* ```
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'team/:id',
* component: TeamCmp,
* canDeactivate: ['canDeactivateTeam']
* }
* ])
* ],
* providers: [
* {
* provide: 'canDeactivateTeam',
* useValue: (component: TeamComponent, currentRoute: ActivatedRouteSnapshot, currentState:
* RouterStateSnapshot, nextState: RouterStateSnapshot) => true
* }
* ]
* })
2016-08-30 18:57:24 -04:00
* class AppModule {}
2016-06-28 17:49:29 -04:00
* ```
*
* @publicApi
*/
export interface CanDeactivate<T> {
canDeactivate(
component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot,
nextState?: RouterStateSnapshot): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean
|UrlTree;
}
export type CanDeactivateFn<T> =
(component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot,
nextState?: RouterStateSnapshot) =>
Observable<boolean|UrlTree>| Promise<boolean|UrlTree>| boolean | UrlTree;
/**
* @description
2016-06-28 17:49:29 -04:00
*
* Interface that class can implement to be a data provider.
*
2016-06-28 17:49:29 -04:00
* ```
2016-08-30 18:57:24 -04:00
* class Backend {
* fetchTeam(id: string) {
* return 'someTeam';
* }
* }
*
* @Injectable()
* class TeamResolver implements Resolve<Team> {
2016-06-28 17:49:29 -04:00
* constructor(private backend: Backend) {}
*
* resolve(
* route: ActivatedRouteSnapshot,
* state: RouterStateSnapshot
* ): Observable<any>|Promise<any>|any {
* return this.backend.fetchTeam(route.params.id);
2016-06-28 17:49:29 -04:00
* }
* }
*
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'team/:id',
* component: TeamCmp,
* resolve: {
* team: TeamResolver
* }
* }
* ])
* ],
* providers: [TeamResolver]
* })
2016-08-30 18:57:24 -04:00
* class AppModule {}
2016-06-28 17:49:29 -04:00
* ```
*
* You can alternatively provide a function with the `resolve` signature:
2016-06-28 17:49:29 -04:00
*
* ```
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'team/:id',
* component: TeamCmp,
* resolve: {
* team: 'teamResolver'
* }
* }
* ])
* ],
* providers: [
* {
* provide: 'teamResolver',
* useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => 'team'
* }
* ]
* })
2016-08-30 18:57:24 -04:00
* class AppModule {}
* ```
*
* @publicApi
*/
export interface Resolve<T> {
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<T>|Promise<T>|T;
}
2016-07-26 17:39:02 -04:00
/**
* @description
2016-07-26 17:39:02 -04:00
*
* Interface that a class can implement to be a guard deciding if a children can be loaded.
*
2016-07-26 17:39:02 -04:00
* ```
2016-08-30 18:57:24 -04:00
* class UserToken {}
* class Permissions {
* canLoadChildren(user: UserToken, id: string, segments: UrlSegment[]): boolean {
2016-08-30 18:57:24 -04:00
* return true;
* }
* }
*
2016-07-26 17:39:02 -04:00
* @Injectable()
2016-08-01 18:48:40 -04:00
* class CanLoadTeamSection implements CanLoad {
2016-07-26 17:39:02 -04:00
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
*
* canLoad(route: Route, segments: UrlSegment[]): Observable<boolean>|Promise<boolean>|boolean {
* return this.permissions.canLoadChildren(this.currentUser, route, segments);
2016-07-26 17:39:02 -04:00
* }
* }
*
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'team/:id',
* component: TeamCmp,
* loadChildren: 'team.js',
* canLoad: [CanLoadTeamSection]
* }
* ])
* ],
2016-08-30 18:57:24 -04:00
* providers: [CanLoadTeamSection, UserToken, Permissions]
* })
2016-08-30 18:57:24 -04:00
* class AppModule {}
2016-07-26 17:39:02 -04:00
* ```
*
* You can alternatively provide a function with the `canLoad` signature:
2016-07-26 17:39:02 -04:00
*
* ```
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'team/:id',
* component: TeamCmp,
* loadChildren: 'team.js',
* canLoad: ['canLoadTeamSection']
* }
* ])
* ],
* providers: [
* {
* provide: 'canLoadTeamSection',
* useValue: (route: Route, segments: UrlSegment[]) => true
* }
* ]
* })
2016-08-30 18:57:24 -04:00
* class AppModule {}
2016-07-26 17:39:02 -04:00
* ```
*
* @publicApi
2016-07-26 17:39:02 -04:00
*/
export interface CanLoad {
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean>|Promise<boolean>|boolean;
}
export type CanLoadFn = (route: Route, segments: UrlSegment[]) =>
Observable<boolean>| Promise<boolean>| boolean;