2016-06-23 12:47:54 -04: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
|
|
|
|
*/
|
|
|
|
|
2016-06-02 18:29:15 -04:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
2016-07-26 17:39:02 -04:00
|
|
|
|
|
|
|
import {Route} from './config';
|
2016-06-02 18:29:15 -04:00
|
|
|
import {ActivatedRouteSnapshot, RouterStateSnapshot} from './router_state';
|
|
|
|
|
2016-07-26 17:39:02 -04:00
|
|
|
|
2016-06-02 18:29:15 -04:00
|
|
|
/**
|
|
|
|
* An interface a class can implement to be a guard deciding if a route can be activated.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```
|
2016-07-13 10:27:32 -04:00
|
|
|
* @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> {
|
|
|
|
* return this.permissions.canActivate(this.currentUser, this.route.params.id);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* bootstrap(AppComponent, [
|
|
|
|
* CanActivateTeam,
|
|
|
|
*
|
|
|
|
* provideRouter([{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team,
|
|
|
|
* canActivate: [CanActivateTeam]
|
|
|
|
* }])
|
2016-07-13 10:26:26 -04:00
|
|
|
* ]);
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* You can also provide a function with the same signature instead of the class:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* bootstrap(AppComponent, [
|
|
|
|
* {provide: 'canActivateTeam', useValue: (route: ActivatedRouteSnapshot, state:
|
|
|
|
* RouterStateSnapshot) => true},
|
|
|
|
* provideRouter([{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team,
|
|
|
|
* canActivate: ['canActivateTeam']
|
|
|
|
* }])
|
2016-07-13 10:26:26 -04:00
|
|
|
* ]);
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @stable
|
2016-06-02 18:29:15 -04:00
|
|
|
*/
|
|
|
|
export interface CanActivate {
|
2016-06-08 14:13:41 -04:00
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
|
2016-07-13 21:12:59 -04:00
|
|
|
Observable<boolean>|Promise<boolean>|boolean;
|
2016-07-13 18:15:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An interface a class can implement to be a guard deciding if a child route can be activated.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Injectable()
|
|
|
|
* class CanActivateTeam implements CanActivate {
|
|
|
|
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
|
|
|
|
*
|
2016-07-13 18:25:48 -04:00
|
|
|
* canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean>
|
|
|
|
* {
|
2016-07-26 17:39:02 -04:00
|
|
|
* return this.permissions.canActivate(this.currentUser, route.params.id);
|
2016-07-13 18:15:20 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* bootstrap(AppComponent, [
|
|
|
|
* CanActivateTeam,
|
|
|
|
*
|
|
|
|
* provideRouter([
|
|
|
|
* {
|
|
|
|
* path: 'root',
|
|
|
|
* canActivateChild: [CanActivateTeam],
|
|
|
|
* children: [
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* You can also provide a function with the same signature instead of the class:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* bootstrap(AppComponent, [
|
|
|
|
* {provide: 'canActivateTeam', useValue: (route: ActivatedRouteSnapshot, state:
|
|
|
|
* RouterStateSnapshot) => true},
|
|
|
|
* provideRouter([
|
|
|
|
* {
|
|
|
|
* path: 'root',
|
2016-07-20 10:27:09 -04:00
|
|
|
* canActivateChild: ['canActivateTeam'],
|
2016-07-13 18:15:20 -04:00
|
|
|
* children: [
|
|
|
|
* {
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
|
|
|
|
export interface CanActivateChild {
|
|
|
|
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot):
|
2016-07-13 21:12:59 -04:00
|
|
|
Observable<boolean>|Promise<boolean>|boolean;
|
2016-06-02 18:29:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An interface a class can implement to be a guard deciding if a route can be deactivated.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```
|
2016-07-13 10:27:32 -04:00
|
|
|
* @Injectable()
|
2016-06-28 17:49:29 -04:00
|
|
|
* class CanDeactivateTeam implements CanDeactivate {
|
|
|
|
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
|
|
|
|
*
|
|
|
|
* canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> {
|
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
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* bootstrap(AppComponent, [
|
|
|
|
* CanDeactivateTeam,
|
|
|
|
*
|
|
|
|
* provideRouter([{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team,
|
|
|
|
* canDeactivate: [CanDeactivateTeam]
|
|
|
|
* }])
|
2016-07-13 10:26:26 -04:00
|
|
|
* ]);
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* You can also provide a function with the same signature instead of the class:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* bootstrap(AppComponent, [
|
|
|
|
* {provide: 'canDeactivateTeam', useValue: (route: ActivatedRouteSnapshot, state:
|
|
|
|
* RouterStateSnapshot) => true},
|
|
|
|
* provideRouter([{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team,
|
|
|
|
* canActivate: ['canDeactivateTeam']
|
|
|
|
* }])
|
2016-07-13 10:26:26 -04:00
|
|
|
* ]);
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @stable
|
2016-06-02 18:29:15 -04:00
|
|
|
*/
|
|
|
|
export interface CanDeactivate<T> {
|
2016-06-08 14:13:41 -04:00
|
|
|
canDeactivate(component: T, route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
|
2016-07-13 21:12:59 -04:00
|
|
|
Observable<boolean>|Promise<boolean>|boolean;
|
2016-06-27 17:00:07 -04:00
|
|
|
}
|
|
|
|
|
2016-06-27 15:27:23 -04:00
|
|
|
/**
|
2016-06-28 17:49:29 -04:00
|
|
|
* An interface a class can implement to be a data provider.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```
|
2016-07-13 10:27:32 -04:00
|
|
|
* @Injectable()
|
2016-06-28 17:49:29 -04:00
|
|
|
* class TeamResolver implements Resolve {
|
|
|
|
* constructor(private backend: Backend) {}
|
|
|
|
*
|
|
|
|
* resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<any> {
|
|
|
|
* return this.backend.fetchTeam(this.route.params.id);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* bootstrap(AppComponent, [
|
|
|
|
* TeamResolver,
|
|
|
|
*
|
|
|
|
* provideRouter([{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: TeamCmp,
|
|
|
|
* resolve: {
|
|
|
|
* team: TeamResolver
|
|
|
|
* }
|
|
|
|
* }])
|
2016-07-13 10:26:26 -04:00
|
|
|
* ]);
|
2016-06-28 17:49:29 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* You can also provide a function with the same signature instead of the class.
|
|
|
|
*
|
2016-06-27 15:27:23 -04:00
|
|
|
* @experimental
|
|
|
|
*/
|
2016-06-27 17:00:07 -04:00
|
|
|
export interface Resolve<T> {
|
2016-07-13 21:12:59 -04:00
|
|
|
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
|
|
|
|
Observable<any>|Promise<any>|any;
|
2016-06-27 15:27:23 -04:00
|
|
|
}
|
2016-07-26 17:39:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An interface a class can implement to be a guard deciding if a children can be loaded.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @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):Observable<boolean> {
|
|
|
|
* return this.permissions.canLoadChildren(this.currentUser, route);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* bootstrap(AppComponent, [
|
|
|
|
* CanLoadTeamSection,
|
|
|
|
*
|
|
|
|
* provideRouter([{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team,
|
|
|
|
* loadChildren: 'team.js',
|
|
|
|
* canLoad: [CanLoadTeamSection]
|
|
|
|
* }])
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* You can also provide a function with the same signature instead of the class:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* bootstrap(AppComponent, [
|
|
|
|
* {provide: 'canLoadTeamSection', useValue: (route: Route) => true},
|
|
|
|
* provideRouter([{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team,
|
|
|
|
* loadChildren: 'team.js',
|
|
|
|
* canLoad: ['canLoadTeamSection']
|
|
|
|
* }])
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export interface CanLoad { canLoad(route: Route): Observable<boolean>|Promise<boolean>|boolean; }
|