chore: router move-only
This commit is contained in:
@@ -1 +0,0 @@
|
||||
export '../core/private_export.dart';
|
||||
@@ -1 +1,21 @@
|
||||
export * from './router';
|
||||
/**
|
||||
* @module
|
||||
* @description
|
||||
* Alternative implementation of the router. Experimental.
|
||||
*/
|
||||
|
||||
export {Router, RouterOutletMap} from './src/alt_router/router';
|
||||
export {RouteSegment, UrlSegment, Tree, UrlTree, RouteTree} from './src/alt_router/segments';
|
||||
export {Routes} from './src/alt_router/metadata/decorators';
|
||||
export {Route} from './src/alt_router/metadata/metadata';
|
||||
export {
|
||||
RouterUrlSerializer,
|
||||
DefaultRouterUrlSerializer
|
||||
} from './src/alt_router/router_url_serializer';
|
||||
export {OnActivate, CanDeactivate} from './src/alt_router/interfaces';
|
||||
export {ROUTER_PROVIDERS} from './src/alt_router/router_providers';
|
||||
|
||||
import {RouterOutlet} from './src/alt_router/directives/router_outlet';
|
||||
import {RouterLink} from './src/alt_router/directives/router_link';
|
||||
|
||||
export const ROUTER_DIRECTIVES: any[] = /*@ts2dart_const*/[RouterOutlet, RouterLink];
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "@angular/router",
|
||||
"version": "$$ANGULAR_VERSION$$",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"jsnext:main": "esm/index.js",
|
||||
"typins": "index.d.ts",
|
||||
"author": "angular",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"@angular/core": "$$ANGULAR_VERSION$$",
|
||||
"@angular/common": "$$ANGULAR_VERSION$$",
|
||||
"@angular/platform-browser": "$$ANGULAR_VERSION$$"
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
|
||||
export default {
|
||||
entry: '../../../dist/packages-dist/router/esm/index.js',
|
||||
dest: '../../../dist/packages-dist/router/esm/router.umd.js',
|
||||
sourceMap: true,
|
||||
format: 'umd',
|
||||
moduleName: 'ng.router',
|
||||
globals: {
|
||||
'@angular/core': 'ng.core',
|
||||
'@angular/common': 'ng.common',
|
||||
'@angular/platform-browser': 'ng.platformBrowser',
|
||||
'rxjs/Subject': 'Rx',
|
||||
'rxjs/observable/PromiseObservable': 'Rx', // this is wrong, but this stuff has changed in rxjs b.6 so we need to fix it when we update.
|
||||
'rxjs/operator/toPromise': 'Rx.Observable.prototype',
|
||||
'rxjs/Observable': 'Rx'
|
||||
},
|
||||
plugins: [
|
||||
// nodeResolve({ jsnext: true, main: true }),
|
||||
]
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/**
|
||||
* @module
|
||||
* @description
|
||||
* Maps application URLs into application states, to support deep-linking and navigation.
|
||||
*/
|
||||
|
||||
export {Router} from './src/router';
|
||||
export {RouterOutlet} from './src/directives/router_outlet';
|
||||
export {RouterLink} from './src/directives/router_link';
|
||||
export {RouteParams, RouteData} from './src/instruction';
|
||||
export {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from './src/route_registry';
|
||||
export * from './src/route_config/route_config_decorator';
|
||||
export * from './src/route_definition';
|
||||
export {OnActivate, OnDeactivate, OnReuse, CanDeactivate, CanReuse} from './src/interfaces';
|
||||
export {CanActivate} from './src/lifecycle/lifecycle_annotations';
|
||||
export {Instruction, ComponentInstruction} from './src/instruction';
|
||||
export {OpaqueToken} from '@angular/core';
|
||||
export {ROUTER_PROVIDERS_COMMON} from './src/router_providers_common';
|
||||
export {ROUTER_PROVIDERS, ROUTER_BINDINGS} from './src/router_providers';
|
||||
|
||||
import {RouterOutlet} from './src/directives/router_outlet';
|
||||
import {RouterLink} from './src/directives/router_link';
|
||||
|
||||
/**
|
||||
* A list of directives. To use the router directives like {@link RouterOutlet} and
|
||||
* {@link RouterLink}, add this to your `directives` array in the {@link View} decorator of your
|
||||
* component.
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
|
||||
*
|
||||
* ```
|
||||
* import {Component} from '@angular/core';
|
||||
* import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig} from '@angular/router';
|
||||
*
|
||||
* @Component({directives: [ROUTER_DIRECTIVES]})
|
||||
* @RouteConfig([
|
||||
* {...},
|
||||
* ])
|
||||
* class AppCmp {
|
||||
* // ...
|
||||
* }
|
||||
*
|
||||
* bootstrap(AppCmp, [ROUTER_PROVIDERS]);
|
||||
* ```
|
||||
*/
|
||||
export const ROUTER_DIRECTIVES: any[] = /*@ts2dart_const*/[RouterOutlet, RouterLink];
|
||||
@@ -0,0 +1 @@
|
||||
export const DEFAULT_OUTLET_NAME = "__DEFAULT";
|
||||
@@ -1,83 +1,62 @@
|
||||
import {Directive} from '@angular/core';
|
||||
import {Location} from '@angular/common';
|
||||
import {isString} from '../../src/facade/lang';
|
||||
import {Router} from '../router';
|
||||
import {Instruction} from '../instruction';
|
||||
import {
|
||||
ResolvedReflectiveProvider,
|
||||
Directive,
|
||||
DynamicComponentLoader,
|
||||
ViewContainerRef,
|
||||
Attribute,
|
||||
ComponentRef,
|
||||
ComponentFactory,
|
||||
ReflectiveInjector,
|
||||
OnInit,
|
||||
HostListener,
|
||||
HostBinding,
|
||||
Input,
|
||||
OnDestroy,
|
||||
Optional
|
||||
} from '@angular/core';
|
||||
import {RouterOutletMap, Router} from '../router';
|
||||
import {RouteSegment, UrlSegment, Tree} from '../segments';
|
||||
import {isString, isPresent} from '@angular/facade/src/lang';
|
||||
import {ObservableWrapper} from '@angular/facade/src/async';
|
||||
|
||||
/**
|
||||
* The RouterLink directive lets you link to specific parts of your app.
|
||||
*
|
||||
* Consider the following route configuration:
|
||||
@Directive({selector: '[routerLink]'})
|
||||
export class RouterLink implements OnDestroy {
|
||||
@Input() target: string;
|
||||
private _changes: any[] = [];
|
||||
private _subscription: any;
|
||||
|
||||
* ```
|
||||
* @RouteConfig([
|
||||
* { path: '/user', component: UserCmp, as: 'User' }
|
||||
* ]);
|
||||
* class MyComp {}
|
||||
* ```
|
||||
*
|
||||
* When linking to this `User` route, you can write:
|
||||
*
|
||||
* ```
|
||||
* <a [routerLink]="['./User']">link to user component</a>
|
||||
* ```
|
||||
*
|
||||
* RouterLink expects the value to be an array of route names, followed by the params
|
||||
* for that level of routing. For instance `['/Team', {teamId: 1}, 'User', {userId: 2}]`
|
||||
* means that we want to generate a link for the `Team` route with params `{teamId: 1}`,
|
||||
* and with a child route `User` with params `{userId: 2}`.
|
||||
*
|
||||
* The first route name should be prepended with `/`, `./`, or `../`.
|
||||
* If the route begins with `/`, the router will look up the route from the root of the app.
|
||||
* If the route begins with `./`, the router will instead look in the current component's
|
||||
* children for the route. And if the route begins with `../`, the router will look at the
|
||||
* current component's parent.
|
||||
*/
|
||||
@Directive({
|
||||
selector: '[routerLink]',
|
||||
inputs: ['routeParams: routerLink', 'target: target'],
|
||||
host: {
|
||||
'(click)': 'onClick()',
|
||||
'[attr.href]': 'visibleHref',
|
||||
'[class.router-link-active]': 'isRouteActive'
|
||||
}
|
||||
})
|
||||
export class RouterLink {
|
||||
private _routeParams: any[];
|
||||
@HostBinding() private href: string;
|
||||
@HostBinding('class.router-link-active') private isActive: boolean = false;
|
||||
|
||||
// the url displayed on the anchor element.
|
||||
visibleHref: string;
|
||||
target: string;
|
||||
|
||||
// the instruction passed to the router to navigate
|
||||
private _navigationInstruction: Instruction;
|
||||
|
||||
constructor(private _router: Router, private _location: Location) {
|
||||
// we need to update the link whenever a route changes to account for aux routes
|
||||
this._router.subscribe((_) => this._updateLink());
|
||||
constructor(@Optional() private _routeSegment: RouteSegment, private _router: Router) {
|
||||
this._subscription =
|
||||
ObservableWrapper.subscribe(_router.changes, (_) => { this._updateTargetUrlAndHref(); });
|
||||
}
|
||||
|
||||
// because auxiliary links take existing primary and auxiliary routes into account,
|
||||
// we need to update the link whenever params or other routes change.
|
||||
private _updateLink(): void {
|
||||
this._navigationInstruction = this._router.generate(this._routeParams);
|
||||
var navigationHref = this._navigationInstruction.toLinkUrl();
|
||||
this.visibleHref = this._location.prepareExternalUrl(navigationHref);
|
||||
}
|
||||
|
||||
get isRouteActive(): boolean { return this._router.isRouteActive(this._navigationInstruction); }
|
||||
|
||||
set routeParams(changes: any[]) {
|
||||
this._routeParams = changes;
|
||||
this._updateLink();
|
||||
ngOnDestroy() { ObservableWrapper.dispose(this._subscription); }
|
||||
|
||||
@Input()
|
||||
set routerLink(data: any[]) {
|
||||
this._changes = data;
|
||||
this._updateTargetUrlAndHref();
|
||||
}
|
||||
|
||||
@HostListener("click")
|
||||
onClick(): boolean {
|
||||
// If no target, or if target is _self, prevent default browser behavior
|
||||
if (!isString(this.target) || this.target == '_self') {
|
||||
this._router.navigateByInstruction(this._navigationInstruction);
|
||||
this._router.navigate(this._changes, this._routeSegment);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private _updateTargetUrlAndHref(): void {
|
||||
let tree = this._router.createUrlTree(this._changes, this._routeSegment);
|
||||
if (isPresent(tree)) {
|
||||
this.href = this._router.serializeUrl(tree);
|
||||
this.isActive = this._router.urlTree.contains(tree);
|
||||
} else {
|
||||
this.isActive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,176 +1,42 @@
|
||||
import {PromiseWrapper, EventEmitter} from '../../src/facade/async';
|
||||
import {StringMapWrapper} from '../../src/facade/collection';
|
||||
import {isBlank, isPresent} from '../../src/facade/lang';
|
||||
import {
|
||||
ResolvedReflectiveProvider,
|
||||
Directive,
|
||||
Attribute,
|
||||
DynamicComponentLoader,
|
||||
ComponentRef,
|
||||
ViewContainerRef,
|
||||
provide,
|
||||
Attribute,
|
||||
ComponentRef,
|
||||
ComponentFactory,
|
||||
ReflectiveInjector,
|
||||
OnDestroy,
|
||||
Output
|
||||
OnInit
|
||||
} from '@angular/core';
|
||||
import * as routerMod from '../router';
|
||||
import {ComponentInstruction, RouteParams, RouteData} from '../instruction';
|
||||
import * as hookMod from '../lifecycle/lifecycle_annotations';
|
||||
import {hasLifecycleHook} from '../lifecycle/route_lifecycle_reflector';
|
||||
import {OnActivate, CanReuse, OnReuse, OnDeactivate, CanDeactivate} from '../interfaces';
|
||||
import {RouterOutletMap} from '../router';
|
||||
import {DEFAULT_OUTLET_NAME} from '../constants';
|
||||
import {isPresent, isBlank} from '@angular/facade/src/lang';
|
||||
|
||||
let _resolveToTrue = PromiseWrapper.resolve(true);
|
||||
|
||||
/**
|
||||
* A router outlet is a placeholder that Angular dynamically fills based on the application's route.
|
||||
*
|
||||
* ## Use
|
||||
*
|
||||
* ```
|
||||
* <router-outlet></router-outlet>
|
||||
* ```
|
||||
*/
|
||||
@Directive({selector: 'router-outlet'})
|
||||
export class RouterOutlet implements OnDestroy {
|
||||
name: string = null;
|
||||
private _componentRef: Promise<ComponentRef<any>> = null;
|
||||
private _currentInstruction: ComponentInstruction = null;
|
||||
export class RouterOutlet {
|
||||
private _loaded: ComponentRef<any>;
|
||||
public outletMap: RouterOutletMap;
|
||||
|
||||
@Output('activate') public activateEvents = new EventEmitter<any>();
|
||||
|
||||
constructor(private _viewContainerRef: ViewContainerRef, private _loader: DynamicComponentLoader,
|
||||
private _parentRouter: routerMod.Router, @Attribute('name') nameAttr: string) {
|
||||
if (isPresent(nameAttr)) {
|
||||
this.name = nameAttr;
|
||||
this._parentRouter.registerAuxOutlet(this);
|
||||
} else {
|
||||
this._parentRouter.registerPrimaryOutlet(this);
|
||||
}
|
||||
constructor(parentOutletMap: RouterOutletMap, private _location: ViewContainerRef,
|
||||
@Attribute('name') name: string) {
|
||||
parentOutletMap.registerOutlet(isBlank(name) ? DEFAULT_OUTLET_NAME : name, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the Router to instantiate a new component during the commit phase of a navigation.
|
||||
* This method in turn is responsible for calling the `routerOnActivate` hook of its child.
|
||||
*/
|
||||
activate(nextInstruction: ComponentInstruction): Promise<any> {
|
||||
var previousInstruction = this._currentInstruction;
|
||||
this._currentInstruction = nextInstruction;
|
||||
var componentType = nextInstruction.componentType;
|
||||
var childRouter = this._parentRouter.childRouter(componentType);
|
||||
|
||||
var providers = ReflectiveInjector.resolve([
|
||||
provide(RouteData, {useValue: nextInstruction.routeData}),
|
||||
provide(RouteParams, {useValue: new RouteParams(nextInstruction.params)}),
|
||||
provide(routerMod.Router, {useValue: childRouter})
|
||||
]);
|
||||
this._componentRef =
|
||||
this._loader.loadNextToLocation(componentType, this._viewContainerRef, providers);
|
||||
return this._componentRef.then((componentRef) => {
|
||||
this.activateEvents.emit(componentRef.instance);
|
||||
if (hasLifecycleHook(hookMod.routerOnActivate, componentType)) {
|
||||
return this._componentRef.then(
|
||||
(ref: ComponentRef<any>) =>
|
||||
(<OnActivate>ref.instance).routerOnActivate(nextInstruction, previousInstruction));
|
||||
} else {
|
||||
return componentRef;
|
||||
}
|
||||
});
|
||||
unload(): void {
|
||||
this._loaded.destroy();
|
||||
this._loaded = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the {@link Router} during the commit phase of a navigation when an outlet
|
||||
* reuses a component between different routes.
|
||||
* This method in turn is responsible for calling the `routerOnReuse` hook of its child.
|
||||
*/
|
||||
reuse(nextInstruction: ComponentInstruction): Promise<any> {
|
||||
var previousInstruction = this._currentInstruction;
|
||||
this._currentInstruction = nextInstruction;
|
||||
get loadedComponent(): Object { return isPresent(this._loaded) ? this._loaded.instance : null; }
|
||||
|
||||
// it's possible the component is removed before it can be reactivated (if nested withing
|
||||
// another dynamically loaded component, for instance). In that case, we simply activate
|
||||
// a new one.
|
||||
if (isBlank(this._componentRef)) {
|
||||
return this.activate(nextInstruction);
|
||||
} else {
|
||||
return PromiseWrapper.resolve(
|
||||
hasLifecycleHook(hookMod.routerOnReuse, this._currentInstruction.componentType) ?
|
||||
this._componentRef.then(
|
||||
(ref: ComponentRef<any>) =>
|
||||
(<OnReuse>ref.instance).routerOnReuse(nextInstruction, previousInstruction)) :
|
||||
true);
|
||||
}
|
||||
get isLoaded(): boolean { return isPresent(this._loaded); }
|
||||
|
||||
load(factory: ComponentFactory<any>, providers: ResolvedReflectiveProvider[],
|
||||
outletMap: RouterOutletMap): ComponentRef<any> {
|
||||
this.outletMap = outletMap;
|
||||
let inj = ReflectiveInjector.fromResolvedProviders(providers, this._location.parentInjector);
|
||||
this._loaded = this._location.createComponent(factory, this._location.length, inj, []);
|
||||
return this._loaded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the {@link Router} when an outlet disposes of a component's contents.
|
||||
* This method in turn is responsible for calling the `routerOnDeactivate` hook of its child.
|
||||
*/
|
||||
deactivate(nextInstruction: ComponentInstruction): Promise<any> {
|
||||
var next = _resolveToTrue;
|
||||
if (isPresent(this._componentRef) && isPresent(this._currentInstruction) &&
|
||||
hasLifecycleHook(hookMod.routerOnDeactivate, this._currentInstruction.componentType)) {
|
||||
next = this._componentRef.then(
|
||||
(ref: ComponentRef<any>) =>
|
||||
(<OnDeactivate>ref.instance)
|
||||
.routerOnDeactivate(nextInstruction, this._currentInstruction));
|
||||
}
|
||||
return next.then((_) => {
|
||||
if (isPresent(this._componentRef)) {
|
||||
var onDispose = this._componentRef.then((ref: ComponentRef<any>) => ref.destroy());
|
||||
this._componentRef = null;
|
||||
return onDispose;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the {@link Router} during recognition phase of a navigation.
|
||||
*
|
||||
* If this resolves to `false`, the given navigation is cancelled.
|
||||
*
|
||||
* This method delegates to the child component's `routerCanDeactivate` hook if it exists,
|
||||
* and otherwise resolves to true.
|
||||
*/
|
||||
routerCanDeactivate(nextInstruction: ComponentInstruction): Promise<boolean> {
|
||||
if (isBlank(this._currentInstruction)) {
|
||||
return _resolveToTrue;
|
||||
}
|
||||
if (hasLifecycleHook(hookMod.routerCanDeactivate, this._currentInstruction.componentType)) {
|
||||
return this._componentRef.then(
|
||||
(ref: ComponentRef<any>) =>
|
||||
(<CanDeactivate>ref.instance)
|
||||
.routerCanDeactivate(nextInstruction, this._currentInstruction));
|
||||
} else {
|
||||
return _resolveToTrue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the {@link Router} during recognition phase of a navigation.
|
||||
*
|
||||
* If the new child component has a different Type than the existing child component,
|
||||
* this will resolve to `false`. You can't reuse an old component when the new component
|
||||
* is of a different Type.
|
||||
*
|
||||
* Otherwise, this method delegates to the child component's `routerCanReuse` hook if it exists,
|
||||
* or resolves to true if the hook is not present.
|
||||
*/
|
||||
routerCanReuse(nextInstruction: ComponentInstruction): Promise<boolean> {
|
||||
var result;
|
||||
|
||||
if (isBlank(this._currentInstruction) ||
|
||||
this._currentInstruction.componentType != nextInstruction.componentType) {
|
||||
result = false;
|
||||
} else if (hasLifecycleHook(hookMod.routerCanReuse, this._currentInstruction.componentType)) {
|
||||
result = this._componentRef.then(
|
||||
(ref: ComponentRef<any>) =>
|
||||
(<CanReuse>ref.instance).routerCanReuse(nextInstruction, this._currentInstruction));
|
||||
} else {
|
||||
result = nextInstruction == this._currentInstruction ||
|
||||
(isPresent(nextInstruction.params) && isPresent(this._currentInstruction.params) &&
|
||||
StringMapWrapper.equals(nextInstruction.params, this._currentInstruction.params));
|
||||
}
|
||||
return <Promise<boolean>>PromiseWrapper.resolve(result);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void { this._parentRouter.unregisterPrimaryOutlet(this); }
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
../../facade/src
|
||||
@@ -1,316 +0,0 @@
|
||||
import {StringMapWrapper} from '../src/facade/collection';
|
||||
import {isPresent, isBlank, normalizeBlank} from '../src/facade/lang';
|
||||
import {PromiseWrapper} from '../src/facade/async';
|
||||
|
||||
|
||||
/**
|
||||
* `RouteParams` is an immutable map of parameters for the given route
|
||||
* based on the url matcher and optional parameters for that route.
|
||||
*
|
||||
* You can inject `RouteParams` into the constructor of a component to use it.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* import {Component} from '@angular/core';
|
||||
* import {bootstrap} from '@angular/platform-browser/browser';
|
||||
* import {Router, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, RouteParams} from
|
||||
* 'angular2/router';
|
||||
*
|
||||
* @Component({directives: [ROUTER_DIRECTIVES]})
|
||||
* @RouteConfig([
|
||||
* {path: '/user/:id', component: UserCmp, name: 'UserCmp'},
|
||||
* ])
|
||||
* class AppCmp {}
|
||||
*
|
||||
* @Component({ template: 'user: {{id}}' })
|
||||
* class UserCmp {
|
||||
* id: string;
|
||||
* constructor(params: RouteParams) {
|
||||
* this.id = params.get('id');
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* bootstrap(AppCmp, ROUTER_PROVIDERS);
|
||||
* ```
|
||||
*/
|
||||
export class RouteParams {
|
||||
constructor(public params: {[key: string]: string}) {}
|
||||
|
||||
get(param: string): string { return normalizeBlank(StringMapWrapper.get(this.params, param)); }
|
||||
}
|
||||
|
||||
/**
|
||||
* `RouteData` is an immutable map of additional data you can configure in your {@link Route}.
|
||||
*
|
||||
* You can inject `RouteData` into the constructor of a component to use it.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* import {Component} from '@angular/core';
|
||||
* import {bootstrap} from '@angular/platform-browser/browser';
|
||||
* import {Router, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, RouteData} from
|
||||
* 'angular2/router';
|
||||
*
|
||||
* @Component({directives: [ROUTER_DIRECTIVES]})
|
||||
* @RouteConfig([
|
||||
* {path: '/user/:id', component: UserCmp, name: 'UserCmp', data: {isAdmin: true}},
|
||||
* ])
|
||||
* class AppCmp {}
|
||||
*
|
||||
* @Component({
|
||||
* ...,
|
||||
* template: 'user: {{isAdmin}}'
|
||||
* })
|
||||
* class UserCmp {
|
||||
* string: isAdmin;
|
||||
* constructor(data: RouteData) {
|
||||
* this.isAdmin = data.get('isAdmin');
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* bootstrap(AppCmp, ROUTER_PROVIDERS);
|
||||
* ```
|
||||
*/
|
||||
export class RouteData {
|
||||
constructor(public data: {[key: string]: any} = /*@ts2dart_const*/ {}) {}
|
||||
|
||||
get(key: string): any { return normalizeBlank(StringMapWrapper.get(this.data, key)); }
|
||||
}
|
||||
|
||||
export var BLANK_ROUTE_DATA = new RouteData();
|
||||
|
||||
/**
|
||||
* `Instruction` is a tree of {@link ComponentInstruction}s with all the information needed
|
||||
* to transition each component in the app to a given route, including all auxiliary routes.
|
||||
*
|
||||
* `Instruction`s can be created using {@link Router#generate}, and can be used to
|
||||
* perform route changes with {@link Router#navigateByInstruction}.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* import {Component} from '@angular/core';
|
||||
* import {bootstrap} from '@angular/platform-browser/browser';
|
||||
* import {Router, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig} from '@angular/router';
|
||||
*
|
||||
* @Component({directives: [ROUTER_DIRECTIVES]})
|
||||
* @RouteConfig([
|
||||
* {...},
|
||||
* ])
|
||||
* class AppCmp {
|
||||
* constructor(router: Router) {
|
||||
* var instruction = router.generate(['/MyRoute']);
|
||||
* router.navigateByInstruction(instruction);
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* bootstrap(AppCmp, ROUTER_PROVIDERS);
|
||||
* ```
|
||||
*/
|
||||
export abstract class Instruction {
|
||||
constructor(public component: ComponentInstruction, public child: Instruction,
|
||||
public auxInstruction: {[key: string]: Instruction}) {}
|
||||
|
||||
get urlPath(): string { return isPresent(this.component) ? this.component.urlPath : ''; }
|
||||
|
||||
get urlParams(): string[] { return isPresent(this.component) ? this.component.urlParams : []; }
|
||||
|
||||
get specificity(): string {
|
||||
var total = '';
|
||||
if (isPresent(this.component)) {
|
||||
total += this.component.specificity;
|
||||
}
|
||||
if (isPresent(this.child)) {
|
||||
total += this.child.specificity;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
abstract resolveComponent(): Promise<ComponentInstruction>;
|
||||
|
||||
/**
|
||||
* converts the instruction into a URL string
|
||||
*/
|
||||
toRootUrl(): string { return this.toUrlPath() + this.toUrlQuery(); }
|
||||
|
||||
/** @internal */
|
||||
_toNonRootUrl(): string {
|
||||
return this._stringifyPathMatrixAuxPrefixed() +
|
||||
(isPresent(this.child) ? this.child._toNonRootUrl() : '');
|
||||
}
|
||||
|
||||
toUrlQuery(): string { return this.urlParams.length > 0 ? ('?' + this.urlParams.join('&')) : ''; }
|
||||
|
||||
/**
|
||||
* Returns a new instruction that shares the state of the existing instruction, but with
|
||||
* the given child {@link Instruction} replacing the existing child.
|
||||
*/
|
||||
replaceChild(child: Instruction): Instruction {
|
||||
return new ResolvedInstruction(this.component, child, this.auxInstruction);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the final URL for the instruction is ``
|
||||
*/
|
||||
toUrlPath(): string {
|
||||
return this.urlPath + this._stringifyAux() +
|
||||
(isPresent(this.child) ? this.child._toNonRootUrl() : '');
|
||||
}
|
||||
|
||||
// default instructions override these
|
||||
toLinkUrl(): string {
|
||||
return this.urlPath + this._stringifyAux() +
|
||||
(isPresent(this.child) ? this.child._toLinkUrl() : '') + this.toUrlQuery();
|
||||
}
|
||||
|
||||
// this is the non-root version (called recursively)
|
||||
/** @internal */
|
||||
_toLinkUrl(): string {
|
||||
return this._stringifyPathMatrixAuxPrefixed() +
|
||||
(isPresent(this.child) ? this.child._toLinkUrl() : '');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_stringifyPathMatrixAuxPrefixed(): string {
|
||||
var primary = this._stringifyPathMatrixAux();
|
||||
if (primary.length > 0) {
|
||||
primary = '/' + primary;
|
||||
}
|
||||
return primary;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_stringifyMatrixParams(): string {
|
||||
return this.urlParams.length > 0 ? (';' + this.urlParams.join(';')) : '';
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_stringifyPathMatrixAux(): string {
|
||||
if (isBlank(this.component)) {
|
||||
return '';
|
||||
}
|
||||
return this.urlPath + this._stringifyMatrixParams() + this._stringifyAux();
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_stringifyAux(): string {
|
||||
var routes = [];
|
||||
StringMapWrapper.forEach(this.auxInstruction, (auxInstruction: Instruction, _: string) => {
|
||||
routes.push(auxInstruction._stringifyPathMatrixAux());
|
||||
});
|
||||
if (routes.length > 0) {
|
||||
return '(' + routes.join('//') + ')';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* a resolved instruction has an outlet instruction for itself, but maybe not for...
|
||||
*/
|
||||
export class ResolvedInstruction extends Instruction {
|
||||
constructor(component: ComponentInstruction, child: Instruction,
|
||||
auxInstruction: {[key: string]: Instruction}) {
|
||||
super(component, child, auxInstruction);
|
||||
}
|
||||
|
||||
resolveComponent(): Promise<ComponentInstruction> {
|
||||
return PromiseWrapper.resolve(this.component);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Represents a resolved default route
|
||||
*/
|
||||
export class DefaultInstruction extends ResolvedInstruction {
|
||||
constructor(component: ComponentInstruction, child: DefaultInstruction) {
|
||||
super(component, child, {});
|
||||
}
|
||||
|
||||
toLinkUrl(): string { return ''; }
|
||||
|
||||
/** @internal */
|
||||
_toLinkUrl(): string { return ''; }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Represents a component that may need to do some redirection or lazy loading at a later time.
|
||||
*/
|
||||
export class UnresolvedInstruction extends Instruction {
|
||||
constructor(private _resolver: () => Promise<Instruction>, private _urlPath: string = '',
|
||||
private _urlParams: string[] = /*@ts2dart_const*/[]) {
|
||||
super(null, null, {});
|
||||
}
|
||||
|
||||
get urlPath(): string {
|
||||
if (isPresent(this.component)) {
|
||||
return this.component.urlPath;
|
||||
}
|
||||
if (isPresent(this._urlPath)) {
|
||||
return this._urlPath;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
get urlParams(): string[] {
|
||||
if (isPresent(this.component)) {
|
||||
return this.component.urlParams;
|
||||
}
|
||||
if (isPresent(this._urlParams)) {
|
||||
return this._urlParams;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
resolveComponent(): Promise<ComponentInstruction> {
|
||||
if (isPresent(this.component)) {
|
||||
return PromiseWrapper.resolve(this.component);
|
||||
}
|
||||
return this._resolver().then((instruction: Instruction) => {
|
||||
this.child = isPresent(instruction) ? instruction.child : null;
|
||||
return this.component = isPresent(instruction) ? instruction.component : null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class RedirectInstruction extends ResolvedInstruction {
|
||||
constructor(component: ComponentInstruction, child: Instruction,
|
||||
auxInstruction: {[key: string]: Instruction}, private _specificity: string) {
|
||||
super(component, child, auxInstruction);
|
||||
}
|
||||
|
||||
get specificity(): string { return this._specificity; }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A `ComponentInstruction` represents the route state for a single component.
|
||||
*
|
||||
* `ComponentInstructions` is a public API. Instances of `ComponentInstruction` are passed
|
||||
* to route lifecycle hooks, like {@link CanActivate}.
|
||||
*
|
||||
* `ComponentInstruction`s are [hash consed](https://en.wikipedia.org/wiki/Hash_consing). You should
|
||||
* never construct one yourself with "new." Instead, rely on router's internal recognizer to
|
||||
* construct `ComponentInstruction`s.
|
||||
*
|
||||
* You should not modify this object. It should be treated as immutable.
|
||||
*/
|
||||
export class ComponentInstruction {
|
||||
reuse: boolean = false;
|
||||
public routeData: RouteData;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
constructor(public urlPath: string, public urlParams: string[], data: RouteData,
|
||||
public componentType, public terminal: boolean, public specificity: string,
|
||||
public params: {[key: string]: string} = null, public routeName: string) {
|
||||
this.routeData = isPresent(data) ? data : BLANK_ROUTE_DATA;
|
||||
}
|
||||
}
|
||||
@@ -1,124 +1,10 @@
|
||||
import {ComponentInstruction} from './instruction';
|
||||
import {global} from '../src/facade/lang';
|
||||
import {RouteSegment, Tree, RouteTree} from './segments';
|
||||
|
||||
// This is here only so that after TS transpilation the file is not empty.
|
||||
// TODO(rado): find a better way to fix this, or remove if likely culprit
|
||||
// https://github.com/systemjs/systemjs/issues/487 gets closed.
|
||||
var __ignore_me = global;
|
||||
var __make_dart_analyzer_happy: Promise<any> = null;
|
||||
|
||||
/**
|
||||
* Defines route lifecycle method `routerOnActivate`, which is called by the router at the end of a
|
||||
* successful route navigation.
|
||||
*
|
||||
* For a single component's navigation, only one of either {@link OnActivate} or {@link OnReuse}
|
||||
* will be called depending on the result of {@link CanReuse}.
|
||||
*
|
||||
* The `routerOnActivate` hook is called with two {@link ComponentInstruction}s as parameters, the
|
||||
* first
|
||||
* representing the current route being navigated to, and the second parameter representing the
|
||||
* previous route or `null`.
|
||||
*
|
||||
* If `routerOnActivate` returns a promise, the route change will wait until the promise settles to
|
||||
* instantiate and activate child components.
|
||||
*
|
||||
* ### Example
|
||||
* {@example router/ts/on_activate/on_activate_example.ts region='routerOnActivate'}
|
||||
*/
|
||||
export interface OnActivate {
|
||||
routerOnActivate(nextInstruction: ComponentInstruction,
|
||||
prevInstruction: ComponentInstruction): any |
|
||||
Promise<any>;
|
||||
routerOnActivate(curr: RouteSegment, prev?: RouteSegment, currTree?: RouteTree,
|
||||
prevTree?: RouteTree): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines route lifecycle method `routerOnReuse`, which is called by the router at the end of a
|
||||
* successful route navigation when {@link CanReuse} is implemented and returns or resolves to true.
|
||||
*
|
||||
* For a single component's navigation, only one of either {@link OnActivate} or {@link OnReuse}
|
||||
* will be called, depending on the result of {@link CanReuse}.
|
||||
*
|
||||
* The `routerOnReuse` hook is called with two {@link ComponentInstruction}s as parameters, the
|
||||
* first
|
||||
* representing the current route being navigated to, and the second parameter representing the
|
||||
* previous route or `null`.
|
||||
*
|
||||
* ### Example
|
||||
* {@example router/ts/reuse/reuse_example.ts region='reuseCmp'}
|
||||
*/
|
||||
export interface OnReuse {
|
||||
routerOnReuse(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any |
|
||||
Promise<any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines route lifecycle method `routerOnDeactivate`, which is called by the router before
|
||||
* destroying
|
||||
* a component as part of a route change.
|
||||
*
|
||||
* The `routerOnDeactivate` hook is called with two {@link ComponentInstruction}s as parameters, the
|
||||
* first
|
||||
* representing the current route being navigated to, and the second parameter representing the
|
||||
* previous route.
|
||||
*
|
||||
* If `routerOnDeactivate` returns a promise, the route change will wait until the promise settles.
|
||||
*
|
||||
* ### Example
|
||||
* {@example router/ts/on_deactivate/on_deactivate_example.ts region='routerOnDeactivate'}
|
||||
*/
|
||||
export interface OnDeactivate {
|
||||
routerOnDeactivate(nextInstruction: ComponentInstruction,
|
||||
prevInstruction: ComponentInstruction): any |
|
||||
Promise<any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines route lifecycle method `routerCanReuse`, which is called by the router to determine
|
||||
* whether a
|
||||
* component should be reused across routes, or whether to destroy and instantiate a new component.
|
||||
*
|
||||
* The `routerCanReuse` hook is called with two {@link ComponentInstruction}s as parameters, the
|
||||
* first
|
||||
* representing the current route being navigated to, and the second parameter representing the
|
||||
* previous route.
|
||||
*
|
||||
* If `routerCanReuse` returns or resolves to `true`, the component instance will be reused and the
|
||||
* {@link OnDeactivate} hook will be run. If `routerCanReuse` returns or resolves to `false`, a new
|
||||
* component will be instantiated, and the existing component will be deactivated and removed as
|
||||
* part of the navigation.
|
||||
*
|
||||
* If `routerCanReuse` throws or rejects, the navigation will be cancelled.
|
||||
*
|
||||
* ### Example
|
||||
* {@example router/ts/reuse/reuse_example.ts region='reuseCmp'}
|
||||
*/
|
||||
export interface CanReuse {
|
||||
routerCanReuse(nextInstruction: ComponentInstruction,
|
||||
prevInstruction: ComponentInstruction): boolean |
|
||||
Promise<boolean>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines route lifecycle method `routerCanDeactivate`, which is called by the router to determine
|
||||
* if a component can be removed as part of a navigation.
|
||||
*
|
||||
* The `routerCanDeactivate` hook is called with two {@link ComponentInstruction}s as parameters,
|
||||
* the
|
||||
* first representing the current route being navigated to, and the second parameter
|
||||
* representing the previous route.
|
||||
*
|
||||
* If `routerCanDeactivate` returns or resolves to `false`, the navigation is cancelled. If it
|
||||
* returns or
|
||||
* resolves to `true`, then the navigation continues, and the component will be deactivated
|
||||
* (the {@link OnDeactivate} hook will be run) and removed.
|
||||
*
|
||||
* If `routerCanDeactivate` throws or rejects, the navigation is also cancelled.
|
||||
*
|
||||
* ### Example
|
||||
* {@example router/ts/can_deactivate/can_deactivate_example.ts region='routerCanDeactivate'}
|
||||
*/
|
||||
export interface CanDeactivate {
|
||||
routerCanDeactivate(nextInstruction: ComponentInstruction,
|
||||
prevInstruction: ComponentInstruction): boolean |
|
||||
Promise<boolean>;
|
||||
}
|
||||
routerCanDeactivate(currTree?: RouteTree, futureTree?: RouteTree): Promise<boolean>;
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
/**
|
||||
* This indirection is needed for TS compilation path.
|
||||
* See comment in lifecycle_annotations.ts.
|
||||
*/
|
||||
|
||||
library angular2.router.lifecycle_annotations;
|
||||
|
||||
export "./lifecycle_annotations_impl.dart";
|
||||
@@ -1,46 +0,0 @@
|
||||
/**
|
||||
* This indirection is needed to free up Component, etc symbols in the public API
|
||||
* to be used by the decorator versions of these annotations.
|
||||
*/
|
||||
|
||||
import {makeDecorator} from '../../core_private';
|
||||
import {CanActivate as CanActivateAnnotation} from './lifecycle_annotations_impl';
|
||||
import {ComponentInstruction} from '../instruction';
|
||||
|
||||
export {
|
||||
routerCanReuse,
|
||||
routerCanDeactivate,
|
||||
routerOnActivate,
|
||||
routerOnReuse,
|
||||
routerOnDeactivate
|
||||
} from './lifecycle_annotations_impl';
|
||||
|
||||
/**
|
||||
* Defines route lifecycle hook `CanActivate`, which is called by the router to determine
|
||||
* if a component can be instantiated as part of a navigation.
|
||||
*
|
||||
* <aside class="is-right">
|
||||
* Note that unlike other lifecycle hooks, this one uses an annotation rather than an interface.
|
||||
* This is because the `CanActivate` function is called before the component is instantiated.
|
||||
* </aside>
|
||||
*
|
||||
* The `CanActivate` hook is called with two {@link ComponentInstruction}s as parameters, the first
|
||||
* representing the current route being navigated to, and the second parameter representing the
|
||||
* previous route or `null`.
|
||||
*
|
||||
* ```typescript
|
||||
* @CanActivate((next, prev) => boolean | Promise<boolean>)
|
||||
* ```
|
||||
*
|
||||
* If `CanActivate` returns or resolves to `false`, the navigation is cancelled.
|
||||
* If `CanActivate` throws or rejects, the navigation is also cancelled.
|
||||
* If `CanActivate` returns or resolves to `true`, navigation continues, the component is
|
||||
* instantiated, and the {@link OnActivate} hook of that component is called if implemented.
|
||||
*
|
||||
* ### Example
|
||||
*
|
||||
* {@example router/ts/can_activate/can_activate_example.ts region='canActivate' }
|
||||
*/
|
||||
export var CanActivate: (hook: (next: ComponentInstruction, prev: ComponentInstruction) =>
|
||||
Promise<boolean>| boolean) => ClassDecorator =
|
||||
makeDecorator(CanActivateAnnotation);
|
||||
@@ -1,21 +0,0 @@
|
||||
|
||||
/* @ts2dart_const */
|
||||
export class RouteLifecycleHook {
|
||||
constructor(public name: string) {}
|
||||
}
|
||||
|
||||
/* @ts2dart_const */
|
||||
export class CanActivate {
|
||||
constructor(public fn: Function) {}
|
||||
}
|
||||
|
||||
export const routerCanReuse: RouteLifecycleHook =
|
||||
/*@ts2dart_const*/ new RouteLifecycleHook("routerCanReuse");
|
||||
export const routerCanDeactivate: RouteLifecycleHook =
|
||||
/*@ts2dart_const*/ new RouteLifecycleHook("routerCanDeactivate");
|
||||
export const routerOnActivate: RouteLifecycleHook =
|
||||
/*@ts2dart_const*/ new RouteLifecycleHook("routerOnActivate");
|
||||
export const routerOnReuse: RouteLifecycleHook =
|
||||
/*@ts2dart_const*/ new RouteLifecycleHook("routerOnReuse");
|
||||
export const routerOnDeactivate: RouteLifecycleHook =
|
||||
/*@ts2dart_const*/ new RouteLifecycleHook("routerOnDeactivate");
|
||||
@@ -1,38 +0,0 @@
|
||||
library angular.router.route_lifecycle_reflector;
|
||||
|
||||
import 'package:angular2/src/router/lifecycle/lifecycle_annotations_impl.dart';
|
||||
import 'package:angular2/src/router/interfaces.dart';
|
||||
import 'package:angular2/src/core/reflection/reflection.dart';
|
||||
|
||||
bool hasLifecycleHook(RouteLifecycleHook e, type) {
|
||||
if (type is! Type) return false;
|
||||
|
||||
final List interfaces = reflector.interfaces(type);
|
||||
var interface;
|
||||
|
||||
if (e == routerOnActivate) {
|
||||
interface = OnActivate;
|
||||
} else if (e == routerOnDeactivate) {
|
||||
interface = OnDeactivate;
|
||||
} else if (e == routerOnReuse) {
|
||||
interface = OnReuse;
|
||||
} else if (e == routerCanDeactivate) {
|
||||
interface = CanDeactivate;
|
||||
} else if (e == routerCanReuse) {
|
||||
interface = CanReuse;
|
||||
}
|
||||
|
||||
return interfaces.contains(interface);
|
||||
}
|
||||
|
||||
Function getCanActivateHook(type) {
|
||||
final List annotations = reflector.annotations(type);
|
||||
|
||||
for (var annotation in annotations) {
|
||||
if (annotation is CanActivate) {
|
||||
return annotation.fn;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
import {Type} from '@angular/core';
|
||||
import {RouteLifecycleHook, CanActivate} from './lifecycle_annotations_impl';
|
||||
import {reflector} from '@angular/core';
|
||||
|
||||
export function hasLifecycleHook(e: RouteLifecycleHook, type): boolean {
|
||||
if (!(type instanceof Type)) return false;
|
||||
return e.name in(<any>type).prototype;
|
||||
}
|
||||
|
||||
export function getCanActivateHook(type): Function {
|
||||
var annotations = reflector.annotations(type);
|
||||
for (let i = 0; i < annotations.length; i += 1) {
|
||||
let annotation = annotations[i];
|
||||
if (annotation instanceof CanActivate) {
|
||||
return annotation.fn;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import './interfaces.dart';
|
||||
bool hasLifecycleHook(String name, Object obj) {
|
||||
if (name == "routerOnActivate") return obj is OnActivate;
|
||||
if (name == "routerCanDeactivate") return obj is CanDeactivate;
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import {Type, isBlank} from '@angular/facade/src/lang';
|
||||
|
||||
export function hasLifecycleHook(name: string, obj: Object): boolean {
|
||||
if (isBlank(obj)) return false;
|
||||
let type = obj.constructor;
|
||||
if (!(type instanceof Type)) return false;
|
||||
return name in(<any>type).prototype;
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import {Tree, TreeNode, UrlSegment, RouteSegment, rootNode, UrlTree, RouteTree} from './segments';
|
||||
import {isBlank, isPresent, isString, isStringMap} from '@angular/facade/src/lang';
|
||||
import {ListWrapper} from '@angular/facade/src/collection';
|
||||
|
||||
export function link(segment: RouteSegment, routeTree: RouteTree, urlTree: UrlTree,
|
||||
change: any[]): UrlTree {
|
||||
if (change.length === 0) return urlTree;
|
||||
|
||||
let startingNode;
|
||||
let normalizedChange;
|
||||
|
||||
if (isString(change[0]) && change[0].startsWith("./")) {
|
||||
normalizedChange = ["/", change[0].substring(2)].concat(change.slice(1));
|
||||
startingNode = _findStartingNode(_findUrlSegment(segment, routeTree), rootNode(urlTree));
|
||||
|
||||
} else if (isString(change[0]) && change.length === 1 && change[0] == "/") {
|
||||
normalizedChange = change;
|
||||
startingNode = rootNode(urlTree);
|
||||
|
||||
} else if (isString(change[0]) && !change[0].startsWith("/")) {
|
||||
normalizedChange = ["/"].concat(change);
|
||||
startingNode = _findStartingNode(_findUrlSegment(segment, routeTree), rootNode(urlTree));
|
||||
|
||||
} else {
|
||||
normalizedChange = ["/"].concat(change);
|
||||
startingNode = rootNode(urlTree);
|
||||
}
|
||||
|
||||
let updated = _update(startingNode, normalizedChange);
|
||||
let newRoot = _constructNewTree(rootNode(urlTree), startingNode, updated);
|
||||
|
||||
return new UrlTree(newRoot);
|
||||
}
|
||||
|
||||
function _findUrlSegment(segment: RouteSegment, routeTree: RouteTree): UrlSegment {
|
||||
let s = segment;
|
||||
let res = null;
|
||||
while (isBlank(res)) {
|
||||
res = ListWrapper.last(s.urlSegments);
|
||||
s = routeTree.parent(s);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
function _findStartingNode(segment: UrlSegment, node: TreeNode<UrlSegment>): TreeNode<UrlSegment> {
|
||||
if (node.value === segment) return node;
|
||||
for (var c of node.children) {
|
||||
let r = _findStartingNode(segment, c);
|
||||
if (isPresent(r)) return r;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function _constructNewTree(node: TreeNode<UrlSegment>, original: TreeNode<UrlSegment>,
|
||||
updated: TreeNode<UrlSegment>): TreeNode<UrlSegment> {
|
||||
if (node === original) {
|
||||
return new TreeNode<UrlSegment>(node.value, updated.children);
|
||||
} else {
|
||||
return new TreeNode<UrlSegment>(
|
||||
node.value, node.children.map(c => _constructNewTree(c, original, updated)));
|
||||
}
|
||||
}
|
||||
|
||||
function _update(node: TreeNode<UrlSegment>, changes: any[]): TreeNode<UrlSegment> {
|
||||
let rest = changes.slice(1);
|
||||
let outlet = _outlet(changes);
|
||||
let segment = _segment(changes);
|
||||
if (isString(segment) && segment[0] == "/") segment = segment.substring(1);
|
||||
|
||||
// reach the end of the tree => create new tree nodes.
|
||||
if (isBlank(node)) {
|
||||
let urlSegment = new UrlSegment(segment, null, outlet);
|
||||
let children = rest.length === 0 ? [] : [_update(null, rest)];
|
||||
return new TreeNode<UrlSegment>(urlSegment, children);
|
||||
|
||||
// different outlet => preserve the subtree
|
||||
} else if (outlet != node.value.outlet) {
|
||||
return node;
|
||||
|
||||
// same outlet => modify the subtree
|
||||
} else {
|
||||
let urlSegment = isStringMap(segment) ? new UrlSegment(null, segment, null) :
|
||||
new UrlSegment(segment, null, outlet);
|
||||
if (rest.length === 0) {
|
||||
return new TreeNode<UrlSegment>(urlSegment, []);
|
||||
}
|
||||
|
||||
return new TreeNode<UrlSegment>(urlSegment,
|
||||
_updateMany(ListWrapper.clone(node.children), rest));
|
||||
}
|
||||
}
|
||||
|
||||
function _updateMany(nodes: TreeNode<UrlSegment>[], changes: any[]): TreeNode<UrlSegment>[] {
|
||||
let outlet = _outlet(changes);
|
||||
let nodesInRightOutlet = nodes.filter(c => c.value.outlet == outlet);
|
||||
if (nodesInRightOutlet.length > 0) {
|
||||
let nodeRightOutlet = nodesInRightOutlet[0]; // there can be only one
|
||||
nodes[nodes.indexOf(nodeRightOutlet)] = _update(nodeRightOutlet, changes);
|
||||
} else {
|
||||
nodes.push(_update(null, changes));
|
||||
}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
|
||||
function _segment(changes: any[]): any {
|
||||
if (!isString(changes[0])) return changes[0];
|
||||
let parts = changes[0].toString().split(":");
|
||||
return parts.length > 1 ? parts[1] : changes[0];
|
||||
}
|
||||
|
||||
function _outlet(changes: any[]): string {
|
||||
if (!isString(changes[0])) return null;
|
||||
let parts = changes[0].toString().split(":");
|
||||
return parts.length > 1 ? parts[0] : null;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
library angular.alt_router.decorators;
|
||||
|
||||
import 'metadata.dart';
|
||||
export 'metadata.dart';
|
||||
|
||||
class Routes extends RoutesMetadata {
|
||||
const Routes(List<RouteMetadata> routes): super(routes);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import {RoutesMetadata, RouteMetadata} from "./metadata";
|
||||
import {makeDecorator} from '../core_private';
|
||||
export interface RoutesFactory {
|
||||
(routes: RouteMetadata[]): any;
|
||||
new (routes: RouteMetadata[]): RoutesMetadata;
|
||||
}
|
||||
export var Routes: RoutesFactory = <RoutesFactory>makeDecorator(RoutesMetadata);
|
||||
@@ -0,0 +1,23 @@
|
||||
import {Type, stringify} from "@angular/facade/src/lang";
|
||||
|
||||
export abstract class RouteMetadata {
|
||||
abstract get path(): string;
|
||||
abstract get component(): Type;
|
||||
}
|
||||
|
||||
/* @ts2dart_const */
|
||||
export class Route implements RouteMetadata {
|
||||
path: string;
|
||||
component: Type;
|
||||
constructor({path, component}: {path?: string, component?: Type} = {}) {
|
||||
this.path = path;
|
||||
this.component = component;
|
||||
}
|
||||
toString(): string { return `@Route(${this.path}, ${stringify(this.component)})`; }
|
||||
}
|
||||
|
||||
/* @ts2dart_const */
|
||||
export class RoutesMetadata {
|
||||
constructor(public routes: RouteMetadata[]) {}
|
||||
toString(): string { return `@Routes(${this.routes})`; }
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"name": "@angular/router",
|
||||
"version": "0.2.0"
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
import {RouteSegment, UrlSegment, Tree, TreeNode, rootNode, UrlTree, RouteTree} from './segments';
|
||||
import {RoutesMetadata, RouteMetadata} from './metadata/metadata';
|
||||
import {Type, isBlank, isPresent, stringify} from '@angular/facade/src/lang';
|
||||
import {ListWrapper, StringMapWrapper} from '@angular/facade/src/collection';
|
||||
import {PromiseWrapper} from '@angular/facade/src/promise';
|
||||
import {BaseException} from '@angular/core';
|
||||
import {ComponentResolver} from '@angular/core';
|
||||
import {DEFAULT_OUTLET_NAME} from './constants';
|
||||
import {reflector} from '@angular/core';
|
||||
|
||||
// TODO: vsavkin: recognize should take the old tree and merge it
|
||||
export function recognize(componentResolver: ComponentResolver, type: Type,
|
||||
url: UrlTree): Promise<RouteTree> {
|
||||
let matched = new _MatchResult(type, [url.root], null, rootNode(url).children, []);
|
||||
return _constructSegment(componentResolver, matched).then(roots => new RouteTree(roots[0]));
|
||||
}
|
||||
|
||||
function _recognize(componentResolver: ComponentResolver, parentType: Type,
|
||||
url: TreeNode<UrlSegment>): Promise<TreeNode<RouteSegment>[]> {
|
||||
let metadata = _readMetadata(parentType); // should read from the factory instead
|
||||
if (isBlank(metadata)) {
|
||||
throw new BaseException(
|
||||
`Component '${stringify(parentType)}' does not have route configuration`);
|
||||
}
|
||||
|
||||
let match;
|
||||
try {
|
||||
match = _match(metadata, url);
|
||||
} catch (e) {
|
||||
return PromiseWrapper.reject(e, null);
|
||||
}
|
||||
|
||||
let main = _constructSegment(componentResolver, match);
|
||||
let aux =
|
||||
_recognizeMany(componentResolver, parentType, match.aux).then(_checkOutletNameUniqueness);
|
||||
return PromiseWrapper.all([main, aux]).then(ListWrapper.flatten);
|
||||
}
|
||||
|
||||
function _recognizeMany(componentResolver: ComponentResolver, parentType: Type,
|
||||
urls: TreeNode<UrlSegment>[]): Promise<TreeNode<RouteSegment>[]> {
|
||||
let recognized = urls.map(u => _recognize(componentResolver, parentType, u));
|
||||
return PromiseWrapper.all(recognized).then(ListWrapper.flatten);
|
||||
}
|
||||
|
||||
function _constructSegment(componentResolver: ComponentResolver,
|
||||
matched: _MatchResult): Promise<TreeNode<RouteSegment>[]> {
|
||||
return componentResolver.resolveComponent(matched.component)
|
||||
.then(factory => {
|
||||
let urlOutlet = matched.consumedUrlSegments.length === 0 ||
|
||||
isBlank(matched.consumedUrlSegments[0].outlet) ?
|
||||
DEFAULT_OUTLET_NAME :
|
||||
matched.consumedUrlSegments[0].outlet;
|
||||
|
||||
let segment = new RouteSegment(matched.consumedUrlSegments, matched.parameters, urlOutlet,
|
||||
matched.component, factory);
|
||||
|
||||
if (matched.leftOverUrl.length > 0) {
|
||||
return _recognizeMany(componentResolver, matched.component, matched.leftOverUrl)
|
||||
.then(children => [new TreeNode<RouteSegment>(segment, children)]);
|
||||
} else {
|
||||
return _recognizeLeftOvers(componentResolver, matched.component)
|
||||
.then(children => [new TreeNode<RouteSegment>(segment, children)]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function _recognizeLeftOvers(componentResolver: ComponentResolver,
|
||||
parentType: Type): Promise<TreeNode<RouteSegment>[]> {
|
||||
return componentResolver.resolveComponent(parentType)
|
||||
.then(factory => {
|
||||
let metadata = _readMetadata(parentType);
|
||||
if (isBlank(metadata)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
let r = (<any[]>metadata.routes).filter(r => r.path == "" || r.path == "/");
|
||||
if (r.length === 0) {
|
||||
return PromiseWrapper.resolve([]);
|
||||
} else {
|
||||
return _recognizeLeftOvers(componentResolver, r[0].component)
|
||||
.then(children => {
|
||||
return componentResolver.resolveComponent(r[0].component)
|
||||
.then(factory => {
|
||||
let segment =
|
||||
new RouteSegment([], null, DEFAULT_OUTLET_NAME, r[0].component, factory);
|
||||
return [new TreeNode<RouteSegment>(segment, children)];
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function _match(metadata: RoutesMetadata, url: TreeNode<UrlSegment>): _MatchResult {
|
||||
for (let r of metadata.routes) {
|
||||
let matchingResult = _matchWithParts(r, url);
|
||||
if (isPresent(matchingResult)) {
|
||||
return matchingResult;
|
||||
}
|
||||
}
|
||||
let availableRoutes = metadata.routes.map(r => `'${r.path}'`).join(", ");
|
||||
throw new BaseException(
|
||||
`Cannot match any routes. Current segment: '${url.value}'. Available routes: [${availableRoutes}].`);
|
||||
}
|
||||
|
||||
function _matchWithParts(route: RouteMetadata, url: TreeNode<UrlSegment>): _MatchResult {
|
||||
let path = route.path.startsWith("/") ? route.path.substring(1) : route.path;
|
||||
|
||||
if (path == "*") {
|
||||
return new _MatchResult(route.component, [], null, [], []);
|
||||
}
|
||||
|
||||
let parts = path.split("/");
|
||||
let positionalParams = {};
|
||||
let consumedUrlSegments = [];
|
||||
|
||||
let lastParent: TreeNode<UrlSegment> = null;
|
||||
let lastSegment: TreeNode<UrlSegment> = null;
|
||||
|
||||
let current = url;
|
||||
for (let i = 0; i < parts.length; ++i) {
|
||||
if (isBlank(current)) return null;
|
||||
|
||||
let p = parts[i];
|
||||
let isLastSegment = i === parts.length - 1;
|
||||
let isLastParent = i === parts.length - 2;
|
||||
let isPosParam = p.startsWith(":");
|
||||
|
||||
if (!isPosParam && p != current.value.segment) return null;
|
||||
if (isLastSegment) {
|
||||
lastSegment = current;
|
||||
}
|
||||
if (isLastParent) {
|
||||
lastParent = current;
|
||||
}
|
||||
|
||||
if (isPosParam) {
|
||||
positionalParams[p.substring(1)] = current.value.segment;
|
||||
}
|
||||
|
||||
consumedUrlSegments.push(current.value);
|
||||
|
||||
current = ListWrapper.first(current.children);
|
||||
}
|
||||
|
||||
if (isPresent(current) && isBlank(current.value.segment)) {
|
||||
lastParent = lastSegment;
|
||||
lastSegment = current;
|
||||
}
|
||||
|
||||
let p = lastSegment.value.parameters;
|
||||
let parameters =
|
||||
<{[key: string]: string}>StringMapWrapper.merge(isBlank(p) ? {} : p, positionalParams);
|
||||
let axuUrlSubtrees = isPresent(lastParent) ? lastParent.children.slice(1) : [];
|
||||
|
||||
return new _MatchResult(route.component, consumedUrlSegments, parameters, lastSegment.children,
|
||||
axuUrlSubtrees);
|
||||
}
|
||||
|
||||
function _checkOutletNameUniqueness(nodes: TreeNode<RouteSegment>[]): TreeNode<RouteSegment>[] {
|
||||
let names = {};
|
||||
nodes.forEach(n => {
|
||||
let segmentWithSameOutletName = names[n.value.outlet];
|
||||
if (isPresent(segmentWithSameOutletName)) {
|
||||
let p = segmentWithSameOutletName.stringifiedUrlSegments;
|
||||
let c = n.value.stringifiedUrlSegments;
|
||||
throw new BaseException(`Two segments cannot have the same outlet name: '${p}' and '${c}'.`);
|
||||
}
|
||||
names[n.value.outlet] = n.value;
|
||||
});
|
||||
return nodes;
|
||||
}
|
||||
|
||||
class _MatchResult {
|
||||
constructor(public component: Type, public consumedUrlSegments: UrlSegment[],
|
||||
public parameters: {[key: string]: string},
|
||||
public leftOverUrl: TreeNode<UrlSegment>[], public aux: TreeNode<UrlSegment>[]) {}
|
||||
}
|
||||
|
||||
function _readMetadata(componentType: Type) {
|
||||
let metadata = reflector.annotations(componentType).filter(f => f instanceof RoutesMetadata);
|
||||
return ListWrapper.first(metadata);
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
library angular2.router.route_config_decorator;
|
||||
|
||||
export './route_config_impl.dart';
|
||||
@@ -1,13 +0,0 @@
|
||||
import {RouteConfig as RouteConfigAnnotation, RouteDefinition} from './route_config_impl';
|
||||
import {makeDecorator} from '../../core_private';
|
||||
|
||||
export {Route, Redirect, AuxRoute, AsyncRoute, RouteDefinition} from './route_config_impl';
|
||||
|
||||
// Copied from RouteConfig in route_config_impl.
|
||||
/**
|
||||
* The `RouteConfig` decorator defines routes for a given component.
|
||||
*
|
||||
* It takes an array of {@link RouteDefinition}s.
|
||||
*/
|
||||
export var RouteConfig: (configs: RouteDefinition[]) => ClassDecorator =
|
||||
makeDecorator(RouteConfigAnnotation);
|
||||
@@ -1,193 +0,0 @@
|
||||
import {Type} from '../../src/facade/lang';
|
||||
import {RouteDefinition} from '../route_definition';
|
||||
import {RegexSerializer} from '../rules/route_paths/regex_route_path';
|
||||
|
||||
export {RouteDefinition} from '../route_definition';
|
||||
|
||||
var __make_dart_analyzer_happy: Promise<any> = null;
|
||||
|
||||
/**
|
||||
* The `RouteConfig` decorator defines routes for a given component.
|
||||
*
|
||||
* It takes an array of {@link RouteDefinition}s.
|
||||
* @ts2dart_const
|
||||
*/
|
||||
export class RouteConfig {
|
||||
constructor(public configs: RouteDefinition[]) {}
|
||||
}
|
||||
|
||||
/* @ts2dart_const */
|
||||
export abstract class AbstractRoute implements RouteDefinition {
|
||||
name: string;
|
||||
useAsDefault: boolean;
|
||||
path: string;
|
||||
regex: string;
|
||||
serializer: RegexSerializer;
|
||||
data: {[key: string]: any};
|
||||
|
||||
constructor({name, useAsDefault, path, regex, serializer, data}: RouteDefinition) {
|
||||
this.name = name;
|
||||
this.useAsDefault = useAsDefault;
|
||||
this.path = path;
|
||||
this.regex = regex;
|
||||
this.serializer = serializer;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* `Route` is a type of {@link RouteDefinition} used to route a path to a component.
|
||||
*
|
||||
* It has the following properties:
|
||||
* - `path` is a string that uses the route matcher DSL.
|
||||
* - `component` a component type.
|
||||
* - `name` is an optional `CamelCase` string representing the name of the route.
|
||||
* - `data` is an optional property of any type representing arbitrary route metadata for the given
|
||||
* route. It is injectable via {@link RouteData}.
|
||||
* - `useAsDefault` is a boolean value. If `true`, the child route will be navigated to if no child
|
||||
* route is specified during the navigation.
|
||||
*
|
||||
* ### Example
|
||||
* ```
|
||||
* import {RouteConfig, Route} from '@angular/router';
|
||||
*
|
||||
* @RouteConfig([
|
||||
* new Route({path: '/home', component: HomeCmp, name: 'HomeCmp' })
|
||||
* ])
|
||||
* class MyApp {}
|
||||
* ```
|
||||
* @ts2dart_const
|
||||
*/
|
||||
export class Route extends AbstractRoute {
|
||||
component: any;
|
||||
aux: string = null;
|
||||
|
||||
constructor({name, useAsDefault, path, regex, serializer, data, component}: RouteDefinition) {
|
||||
super({
|
||||
name: name,
|
||||
useAsDefault: useAsDefault,
|
||||
path: path,
|
||||
regex: regex,
|
||||
serializer: serializer,
|
||||
data: data
|
||||
});
|
||||
this.component = component;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* `AuxRoute` is a type of {@link RouteDefinition} used to define an auxiliary route.
|
||||
*
|
||||
* It takes an object with the following properties:
|
||||
* - `path` is a string that uses the route matcher DSL.
|
||||
* - `component` a component type.
|
||||
* - `name` is an optional `CamelCase` string representing the name of the route.
|
||||
* - `data` is an optional property of any type representing arbitrary route metadata for the given
|
||||
* route. It is injectable via {@link RouteData}.
|
||||
*
|
||||
* ### Example
|
||||
* ```
|
||||
* import {RouteConfig, AuxRoute} from '@angular/router';
|
||||
*
|
||||
* @RouteConfig([
|
||||
* new AuxRoute({path: '/home', component: HomeCmp})
|
||||
* ])
|
||||
* class MyApp {}
|
||||
* ```
|
||||
* @ts2dart_const
|
||||
*/
|
||||
export class AuxRoute extends AbstractRoute {
|
||||
component: any;
|
||||
|
||||
constructor({name, useAsDefault, path, regex, serializer, data, component}: RouteDefinition) {
|
||||
super({
|
||||
name: name,
|
||||
useAsDefault: useAsDefault,
|
||||
path: path,
|
||||
regex: regex,
|
||||
serializer: serializer,
|
||||
data: data
|
||||
});
|
||||
this.component = component;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* `AsyncRoute` is a type of {@link RouteDefinition} used to route a path to an asynchronously
|
||||
* loaded component.
|
||||
*
|
||||
* It has the following properties:
|
||||
* - `path` is a string that uses the route matcher DSL.
|
||||
* - `loader` is a function that returns a promise that resolves to a component.
|
||||
* - `name` is an optional `CamelCase` string representing the name of the route.
|
||||
* - `data` is an optional property of any type representing arbitrary route metadata for the given
|
||||
* route. It is injectable via {@link RouteData}.
|
||||
* - `useAsDefault` is a boolean value. If `true`, the child route will be navigated to if no child
|
||||
* route is specified during the navigation.
|
||||
*
|
||||
* ### Example
|
||||
* ```
|
||||
* import {RouteConfig, AsyncRoute} from '@angular/router';
|
||||
*
|
||||
* @RouteConfig([
|
||||
* new AsyncRoute({path: '/home', loader: () => Promise.resolve(MyLoadedCmp), name:
|
||||
* 'MyLoadedCmp'})
|
||||
* ])
|
||||
* class MyApp {}
|
||||
* ```
|
||||
* @ts2dart_const
|
||||
*/
|
||||
export class AsyncRoute extends AbstractRoute {
|
||||
loader: () => Promise<Type>;
|
||||
aux: string = null;
|
||||
|
||||
constructor({name, useAsDefault, path, regex, serializer, data, loader}: RouteDefinition) {
|
||||
super({
|
||||
name: name,
|
||||
useAsDefault: useAsDefault,
|
||||
path: path,
|
||||
regex: regex,
|
||||
serializer: serializer,
|
||||
data: data
|
||||
});
|
||||
this.loader = loader;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* `Redirect` is a type of {@link RouteDefinition} used to route a path to a canonical route.
|
||||
*
|
||||
* It has the following properties:
|
||||
* - `path` is a string that uses the route matcher DSL.
|
||||
* - `redirectTo` is an array representing the link DSL.
|
||||
*
|
||||
* Note that redirects **do not** affect how links are generated. For that, see the `useAsDefault`
|
||||
* option.
|
||||
*
|
||||
* ### Example
|
||||
* ```
|
||||
* import {RouteConfig, Route, Redirect} from '@angular/router';
|
||||
*
|
||||
* @RouteConfig([
|
||||
* new Redirect({path: '/', redirectTo: ['/Home'] }),
|
||||
* new Route({path: '/home', component: HomeCmp, name: 'Home'})
|
||||
* ])
|
||||
* class MyApp {}
|
||||
* ```
|
||||
* @ts2dart_const
|
||||
*/
|
||||
export class Redirect extends AbstractRoute {
|
||||
redirectTo: any[];
|
||||
|
||||
constructor({name, useAsDefault, path, regex, serializer, data, redirectTo}: RouteDefinition) {
|
||||
super({
|
||||
name: name,
|
||||
useAsDefault: useAsDefault,
|
||||
path: path,
|
||||
regex: regex,
|
||||
serializer: serializer,
|
||||
data: data
|
||||
});
|
||||
this.redirectTo = redirectTo;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
library angular2.src.router.route_config_normalizer;
|
||||
|
||||
import "route_config_decorator.dart";
|
||||
import "../route_definition.dart";
|
||||
import "../route_registry.dart";
|
||||
import "package:angular2/src/facade/lang.dart";
|
||||
import "package:angular2/src/facade/exceptions.dart" show BaseException;
|
||||
|
||||
RouteDefinition normalizeRouteConfig(RouteDefinition config, RouteRegistry registry) {
|
||||
if (config is AsyncRoute) {
|
||||
|
||||
configRegistryAndReturnType(componentType) {
|
||||
registry.configFromComponent(componentType);
|
||||
return componentType;
|
||||
}
|
||||
|
||||
loader() {
|
||||
return config.loader().then(configRegistryAndReturnType);
|
||||
}
|
||||
return new AsyncRoute(path: config.path, loader: loader, name: config.name, data: config.data, useAsDefault: config.useAsDefault);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
void assertComponentExists(Type component, String path) {
|
||||
if (component == null) {
|
||||
throw new BaseException(
|
||||
'Component for route "${path}" is not defined, or is not a class.');
|
||||
}
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
import {AsyncRoute, AuxRoute, Route, Redirect, RouteDefinition} from './route_config_decorator';
|
||||
import {ComponentDefinition} from '../route_definition';
|
||||
import {isType, Type} from '../../src/facade/lang';
|
||||
import {BaseException} from '../../src/facade/exceptions';
|
||||
import {RouteRegistry} from '../route_registry';
|
||||
|
||||
|
||||
/**
|
||||
* Given a JS Object that represents a route config, returns a corresponding Route, AsyncRoute,
|
||||
* AuxRoute or Redirect object.
|
||||
*
|
||||
* Also wraps an AsyncRoute's loader function to add the loaded component's route config to the
|
||||
* `RouteRegistry`.
|
||||
*/
|
||||
export function normalizeRouteConfig(config: RouteDefinition,
|
||||
registry: RouteRegistry): RouteDefinition {
|
||||
if (config instanceof AsyncRoute) {
|
||||
var wrappedLoader = wrapLoaderToReconfigureRegistry(config.loader, registry);
|
||||
return new AsyncRoute({
|
||||
path: config.path,
|
||||
loader: wrappedLoader,
|
||||
name: config.name,
|
||||
data: config.data,
|
||||
useAsDefault: config.useAsDefault
|
||||
});
|
||||
}
|
||||
if (config instanceof Route || config instanceof Redirect || config instanceof AuxRoute) {
|
||||
return <RouteDefinition>config;
|
||||
}
|
||||
|
||||
if ((+!!config.component) + (+!!config.redirectTo) + (+!!config.loader) != 1) {
|
||||
throw new BaseException(
|
||||
`Route config should contain exactly one "component", "loader", or "redirectTo" property.`);
|
||||
}
|
||||
if (config.as && config.name) {
|
||||
throw new BaseException(`Route config should contain exactly one "as" or "name" property.`);
|
||||
}
|
||||
if (config.as) {
|
||||
config.name = config.as;
|
||||
}
|
||||
if (config.loader) {
|
||||
var wrappedLoader = wrapLoaderToReconfigureRegistry(config.loader, registry);
|
||||
return new AsyncRoute({
|
||||
path: config.path,
|
||||
loader: wrappedLoader,
|
||||
name: config.name,
|
||||
data: config.data,
|
||||
useAsDefault: config.useAsDefault
|
||||
});
|
||||
}
|
||||
if (config.aux) {
|
||||
return new AuxRoute({path: config.aux, component:<Type>config.component, name: config.name});
|
||||
}
|
||||
if (config.component) {
|
||||
if (typeof config.component == 'object') {
|
||||
let componentDefinitionObject = <ComponentDefinition>config.component;
|
||||
if (componentDefinitionObject.type == 'constructor') {
|
||||
return new Route({
|
||||
path: config.path,
|
||||
component:<Type>componentDefinitionObject.constructor,
|
||||
name: config.name,
|
||||
data: config.data,
|
||||
useAsDefault: config.useAsDefault
|
||||
});
|
||||
} else if (componentDefinitionObject.type == 'loader') {
|
||||
return new AsyncRoute({
|
||||
path: config.path,
|
||||
loader: componentDefinitionObject.loader,
|
||||
name: config.name,
|
||||
data: config.data,
|
||||
useAsDefault: config.useAsDefault
|
||||
});
|
||||
} else {
|
||||
throw new BaseException(
|
||||
`Invalid component type "${componentDefinitionObject.type}". Valid types are "constructor" and "loader".`);
|
||||
}
|
||||
}
|
||||
return new Route(<{
|
||||
path: string;
|
||||
component: Type;
|
||||
name?: string;
|
||||
data?: {[key: string]: any};
|
||||
useAsDefault?: boolean;
|
||||
}>config);
|
||||
}
|
||||
|
||||
if (config.redirectTo) {
|
||||
return new Redirect({path: config.path, redirectTo: config.redirectTo});
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
function wrapLoaderToReconfigureRegistry(loader: Function, registry: RouteRegistry): () =>
|
||||
Promise<Type> {
|
||||
return () => {
|
||||
return loader().then((componentType) => {
|
||||
registry.configFromComponent(componentType);
|
||||
return componentType;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function assertComponentExists(component: Type, path: string): void {
|
||||
if (!isType(component)) {
|
||||
throw new BaseException(`Component for route "${path}" is not defined, or is not a class.`);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
library angular2.src.router.route_definition;
|
||||
|
||||
abstract class RouteDefinition {
|
||||
final String path;
|
||||
final String name;
|
||||
final bool useAsDefault;
|
||||
final String regex;
|
||||
final Function serializer;
|
||||
const RouteDefinition({this.path, this.name, this.useAsDefault : false, this.regex, this.serializer});
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
import {Type} from '../src/facade/lang';
|
||||
import {RegexSerializer} from './rules/route_paths/regex_route_path';
|
||||
|
||||
/**
|
||||
* `RouteDefinition` defines a route within a {@link RouteConfig} decorator.
|
||||
*
|
||||
* Supported keys:
|
||||
* - `path` or `aux` (requires exactly one of these)
|
||||
* - `component`, `loader`, `redirectTo` (requires exactly one of these)
|
||||
* - `name` or `as` (optional) (requires exactly one of these)
|
||||
* - `data` (optional)
|
||||
*
|
||||
* See also {@link Route}, {@link AsyncRoute}, {@link AuxRoute}, and {@link Redirect}.
|
||||
*/
|
||||
export interface RouteDefinition {
|
||||
path?: string;
|
||||
aux?: string;
|
||||
regex?: string;
|
||||
serializer?: RegexSerializer;
|
||||
component?: Type | ComponentDefinition;
|
||||
loader?: () => Promise<Type>;
|
||||
redirectTo?: any[];
|
||||
as?: string;
|
||||
name?: string;
|
||||
data?: any;
|
||||
useAsDefault?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents either a component type (`type` is `component`) or a loader function
|
||||
* (`type` is `loader`).
|
||||
*
|
||||
* See also {@link RouteDefinition}.
|
||||
*/
|
||||
export interface ComponentDefinition {
|
||||
type: string;
|
||||
loader?: () => Promise<Type>;
|
||||
component?: Type;
|
||||
}
|
||||
@@ -1,547 +0,0 @@
|
||||
import {ListWrapper, Map, StringMapWrapper} from '../src/facade/collection';
|
||||
import {PromiseWrapper} from '../src/facade/async';
|
||||
import {
|
||||
isPresent,
|
||||
isArray,
|
||||
isBlank,
|
||||
isType,
|
||||
isString,
|
||||
isStringMap,
|
||||
Type,
|
||||
StringWrapper,
|
||||
Math,
|
||||
getTypeNameForDebugging,
|
||||
} from '../src/facade/lang';
|
||||
import {BaseException} from '../src/facade/exceptions';
|
||||
import {Injectable, Inject, OpaqueToken, reflector} from '@angular/core';
|
||||
import {RouteConfig, Route, AuxRoute, RouteDefinition} from './route_config/route_config_impl';
|
||||
import {PathMatch, RedirectMatch, RouteMatch} from './rules/rules';
|
||||
import {RuleSet} from './rules/rule_set';
|
||||
import {
|
||||
Instruction,
|
||||
ResolvedInstruction,
|
||||
RedirectInstruction,
|
||||
UnresolvedInstruction,
|
||||
DefaultInstruction
|
||||
} from './instruction';
|
||||
import {normalizeRouteConfig, assertComponentExists} from './route_config/route_config_normalizer';
|
||||
import {parser, Url, convertUrlParamsToArray} from './url_parser';
|
||||
import {GeneratedUrl} from './rules/route_paths/route_path';
|
||||
|
||||
var _resolveToNull = PromiseWrapper.resolve<Instruction>(null);
|
||||
|
||||
// A LinkItemArray is an array, which describes a set of routes
|
||||
// The items in the array are found in groups:
|
||||
// - the first item is the name of the route
|
||||
// - the next items are:
|
||||
// - an object containing parameters
|
||||
// - or an array describing an aux route
|
||||
// export type LinkRouteItem = string | Object;
|
||||
// export type LinkItem = LinkRouteItem | Array<LinkRouteItem>;
|
||||
// export type LinkItemArray = Array<LinkItem>;
|
||||
|
||||
/**
|
||||
* Token used to bind the component with the top-level {@link RouteConfig}s for the
|
||||
* application.
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
|
||||
*
|
||||
* ```
|
||||
* import {Component} from '@angular/core';
|
||||
* import {
|
||||
* ROUTER_DIRECTIVES,
|
||||
* ROUTER_PROVIDERS,
|
||||
* RouteConfig
|
||||
* } from '@angular/router';
|
||||
*
|
||||
* @Component({directives: [ROUTER_DIRECTIVES]})
|
||||
* @RouteConfig([
|
||||
* {...},
|
||||
* ])
|
||||
* class AppCmp {
|
||||
* // ...
|
||||
* }
|
||||
*
|
||||
* bootstrap(AppCmp, [ROUTER_PROVIDERS]);
|
||||
* ```
|
||||
*/
|
||||
export const ROUTER_PRIMARY_COMPONENT: OpaqueToken =
|
||||
/*@ts2dart_const*/ new OpaqueToken('RouterPrimaryComponent');
|
||||
|
||||
|
||||
/**
|
||||
* The RouteRegistry holds route configurations for each component in an Angular app.
|
||||
* It is responsible for creating Instructions from URLs, and generating URLs based on route and
|
||||
* parameters.
|
||||
*/
|
||||
@Injectable()
|
||||
export class RouteRegistry {
|
||||
private _rules = new Map<any, RuleSet>();
|
||||
|
||||
constructor(@Inject(ROUTER_PRIMARY_COMPONENT) private _rootComponent: Type) {}
|
||||
|
||||
/**
|
||||
* Given a component and a configuration object, add the route to this registry
|
||||
*/
|
||||
config(parentComponent: any, config: RouteDefinition): void {
|
||||
config = normalizeRouteConfig(config, this);
|
||||
|
||||
// this is here because Dart type guard reasons
|
||||
if (config instanceof Route) {
|
||||
assertComponentExists(config.component, config.path);
|
||||
} else if (config instanceof AuxRoute) {
|
||||
assertComponentExists(config.component, config.path);
|
||||
}
|
||||
|
||||
var rules = this._rules.get(parentComponent);
|
||||
|
||||
if (isBlank(rules)) {
|
||||
rules = new RuleSet();
|
||||
this._rules.set(parentComponent, rules);
|
||||
}
|
||||
|
||||
var terminal = rules.config(config);
|
||||
|
||||
if (config instanceof Route) {
|
||||
if (terminal) {
|
||||
assertTerminalComponent(config.component, config.path);
|
||||
} else {
|
||||
this.configFromComponent(config.component);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the annotations of a component and configures the registry based on them
|
||||
*/
|
||||
configFromComponent(component: any): void {
|
||||
if (!isType(component)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't read the annotations from a type more than once –
|
||||
// this prevents an infinite loop if a component routes recursively.
|
||||
if (this._rules.has(component)) {
|
||||
return;
|
||||
}
|
||||
var annotations = reflector.annotations(component);
|
||||
if (isPresent(annotations)) {
|
||||
for (var i = 0; i < annotations.length; i++) {
|
||||
var annotation = annotations[i];
|
||||
|
||||
if (annotation instanceof RouteConfig) {
|
||||
let routeCfgs: RouteDefinition[] = annotation.configs;
|
||||
routeCfgs.forEach(config => this.config(component, config));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Given a URL and a parent component, return the most specific instruction for navigating
|
||||
* the application into the state specified by the url
|
||||
*/
|
||||
recognize(url: string, ancestorInstructions: Instruction[]): Promise<Instruction> {
|
||||
var parsedUrl = parser.parse(url);
|
||||
return this._recognize(parsedUrl, []);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Recognizes all parent-child routes, but creates unresolved auxiliary routes
|
||||
*/
|
||||
private _recognize(parsedUrl: Url, ancestorInstructions: Instruction[],
|
||||
_aux = false): Promise<Instruction> {
|
||||
var parentInstruction = ListWrapper.last(ancestorInstructions);
|
||||
var parentComponent = isPresent(parentInstruction) ? parentInstruction.component.componentType :
|
||||
this._rootComponent;
|
||||
|
||||
var rules = this._rules.get(parentComponent);
|
||||
if (isBlank(rules)) {
|
||||
return _resolveToNull;
|
||||
}
|
||||
|
||||
// Matches some beginning part of the given URL
|
||||
var possibleMatches: Promise<RouteMatch>[] =
|
||||
_aux ? rules.recognizeAuxiliary(parsedUrl) : rules.recognize(parsedUrl);
|
||||
|
||||
var matchPromises: Promise<Instruction>[] = possibleMatches.map(
|
||||
(candidate: Promise<RouteMatch>) => candidate.then((candidate: RouteMatch) => {
|
||||
|
||||
if (candidate instanceof PathMatch) {
|
||||
var auxParentInstructions: Instruction[] =
|
||||
ancestorInstructions.length > 0 ? [ListWrapper.last(ancestorInstructions)] : [];
|
||||
var auxInstructions =
|
||||
this._auxRoutesToUnresolved(candidate.remainingAux, auxParentInstructions);
|
||||
|
||||
var instruction = new ResolvedInstruction(candidate.instruction, null, auxInstructions);
|
||||
|
||||
if (isBlank(candidate.instruction) || candidate.instruction.terminal) {
|
||||
return instruction;
|
||||
}
|
||||
|
||||
var newAncestorInstructions: Instruction[] = ancestorInstructions.concat([instruction]);
|
||||
|
||||
return this._recognize(candidate.remaining, newAncestorInstructions)
|
||||
.then((childInstruction) => {
|
||||
if (isBlank(childInstruction)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// redirect instructions are already absolute
|
||||
if (childInstruction instanceof RedirectInstruction) {
|
||||
return childInstruction;
|
||||
}
|
||||
instruction.child = childInstruction;
|
||||
return instruction;
|
||||
});
|
||||
}
|
||||
|
||||
if (candidate instanceof RedirectMatch) {
|
||||
var instruction =
|
||||
this.generate(candidate.redirectTo, ancestorInstructions.concat([null]));
|
||||
return new RedirectInstruction(instruction.component, instruction.child,
|
||||
instruction.auxInstruction, candidate.specificity);
|
||||
}
|
||||
}));
|
||||
|
||||
if ((isBlank(parsedUrl) || parsedUrl.path == '') && possibleMatches.length == 0) {
|
||||
return PromiseWrapper.resolve(this.generateDefault(parentComponent));
|
||||
}
|
||||
|
||||
return PromiseWrapper.all<Instruction>(matchPromises).then(mostSpecific);
|
||||
}
|
||||
|
||||
private _auxRoutesToUnresolved(auxRoutes: Url[],
|
||||
parentInstructions: Instruction[]): {[key: string]: Instruction} {
|
||||
var unresolvedAuxInstructions: {[key: string]: Instruction} = {};
|
||||
|
||||
auxRoutes.forEach((auxUrl: Url) => {
|
||||
unresolvedAuxInstructions[auxUrl.path] = new UnresolvedInstruction(
|
||||
() => { return this._recognize(auxUrl, parentInstructions, true); });
|
||||
});
|
||||
|
||||
return unresolvedAuxInstructions;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Given a normalized list with component names and params like: `['user', {id: 3 }]`
|
||||
* generates a url with a leading slash relative to the provided `parentComponent`.
|
||||
*
|
||||
* If the optional param `_aux` is `true`, then we generate starting at an auxiliary
|
||||
* route boundary.
|
||||
*/
|
||||
generate(linkParams: any[], ancestorInstructions: Instruction[], _aux = false): Instruction {
|
||||
var params = splitAndFlattenLinkParams(linkParams);
|
||||
var prevInstruction;
|
||||
|
||||
// The first segment should be either '.' (generate from parent) or '' (generate from root).
|
||||
// When we normalize above, we strip all the slashes, './' becomes '.' and '/' becomes ''.
|
||||
if (ListWrapper.first(params) == '') {
|
||||
params.shift();
|
||||
prevInstruction = ListWrapper.first(ancestorInstructions);
|
||||
ancestorInstructions = [];
|
||||
} else {
|
||||
prevInstruction = ancestorInstructions.length > 0 ? ancestorInstructions.pop() : null;
|
||||
|
||||
if (ListWrapper.first(params) == '.') {
|
||||
params.shift();
|
||||
} else if (ListWrapper.first(params) == '..') {
|
||||
while (ListWrapper.first(params) == '..') {
|
||||
if (ancestorInstructions.length <= 0) {
|
||||
throw new BaseException(
|
||||
`Link "${ListWrapper.toJSON(linkParams)}" has too many "../" segments.`);
|
||||
}
|
||||
prevInstruction = ancestorInstructions.pop();
|
||||
params = ListWrapper.slice(params, 1);
|
||||
}
|
||||
|
||||
// we're on to implicit child/sibling route
|
||||
} else {
|
||||
// we must only peak at the link param, and not consume it
|
||||
let routeName = ListWrapper.first(params);
|
||||
let parentComponentType = this._rootComponent;
|
||||
let grandparentComponentType = null;
|
||||
|
||||
if (ancestorInstructions.length > 1) {
|
||||
let parentComponentInstruction = ancestorInstructions[ancestorInstructions.length - 1];
|
||||
let grandComponentInstruction = ancestorInstructions[ancestorInstructions.length - 2];
|
||||
|
||||
parentComponentType = parentComponentInstruction.component.componentType;
|
||||
grandparentComponentType = grandComponentInstruction.component.componentType;
|
||||
} else if (ancestorInstructions.length == 1) {
|
||||
parentComponentType = ancestorInstructions[0].component.componentType;
|
||||
grandparentComponentType = this._rootComponent;
|
||||
}
|
||||
|
||||
// For a link with no leading `./`, `/`, or `../`, we look for a sibling and child.
|
||||
// If both exist, we throw. Otherwise, we prefer whichever exists.
|
||||
var childRouteExists = this.hasRoute(routeName, parentComponentType);
|
||||
var parentRouteExists = isPresent(grandparentComponentType) &&
|
||||
this.hasRoute(routeName, grandparentComponentType);
|
||||
|
||||
if (parentRouteExists && childRouteExists) {
|
||||
let msg =
|
||||
`Link "${ListWrapper.toJSON(linkParams)}" is ambiguous, use "./" or "../" to disambiguate.`;
|
||||
throw new BaseException(msg);
|
||||
}
|
||||
|
||||
if (parentRouteExists) {
|
||||
prevInstruction = ancestorInstructions.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (params[params.length - 1] == '') {
|
||||
params.pop();
|
||||
}
|
||||
|
||||
if (params.length > 0 && params[0] == '') {
|
||||
params.shift();
|
||||
}
|
||||
|
||||
if (params.length < 1) {
|
||||
let msg = `Link "${ListWrapper.toJSON(linkParams)}" must include a route name.`;
|
||||
throw new BaseException(msg);
|
||||
}
|
||||
|
||||
var generatedInstruction =
|
||||
this._generate(params, ancestorInstructions, prevInstruction, _aux, linkParams);
|
||||
|
||||
// we don't clone the first (root) element
|
||||
for (var i = ancestorInstructions.length - 1; i >= 0; i--) {
|
||||
let ancestorInstruction = ancestorInstructions[i];
|
||||
if (isBlank(ancestorInstruction)) {
|
||||
break;
|
||||
}
|
||||
generatedInstruction = ancestorInstruction.replaceChild(generatedInstruction);
|
||||
}
|
||||
|
||||
return generatedInstruction;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Internal helper that does not make any assertions about the beginning of the link DSL.
|
||||
* `ancestorInstructions` are parents that will be cloned.
|
||||
* `prevInstruction` is the existing instruction that would be replaced, but which might have
|
||||
* aux routes that need to be cloned.
|
||||
*/
|
||||
private _generate(linkParams: any[], ancestorInstructions: Instruction[],
|
||||
prevInstruction: Instruction, _aux = false, _originalLink: any[]): Instruction {
|
||||
let parentComponentType = this._rootComponent;
|
||||
let componentInstruction = null;
|
||||
let auxInstructions: {[key: string]: Instruction} = {};
|
||||
|
||||
let parentInstruction: Instruction = ListWrapper.last(ancestorInstructions);
|
||||
if (isPresent(parentInstruction) && isPresent(parentInstruction.component)) {
|
||||
parentComponentType = parentInstruction.component.componentType;
|
||||
}
|
||||
|
||||
if (linkParams.length == 0) {
|
||||
let defaultInstruction = this.generateDefault(parentComponentType);
|
||||
if (isBlank(defaultInstruction)) {
|
||||
throw new BaseException(
|
||||
`Link "${ListWrapper.toJSON(_originalLink)}" does not resolve to a terminal instruction.`);
|
||||
}
|
||||
return defaultInstruction;
|
||||
}
|
||||
|
||||
// for non-aux routes, we want to reuse the predecessor's existing primary and aux routes
|
||||
// and only override routes for which the given link DSL provides
|
||||
if (isPresent(prevInstruction) && !_aux) {
|
||||
auxInstructions = StringMapWrapper.merge(prevInstruction.auxInstruction, auxInstructions);
|
||||
componentInstruction = prevInstruction.component;
|
||||
}
|
||||
|
||||
var rules = this._rules.get(parentComponentType);
|
||||
if (isBlank(rules)) {
|
||||
throw new BaseException(
|
||||
`Component "${getTypeNameForDebugging(parentComponentType)}" has no route config.`);
|
||||
}
|
||||
|
||||
let linkParamIndex = 0;
|
||||
let routeParams: {[key: string]: any} = {};
|
||||
|
||||
// first, recognize the primary route if one is provided
|
||||
if (linkParamIndex < linkParams.length && isString(linkParams[linkParamIndex])) {
|
||||
let routeName = linkParams[linkParamIndex];
|
||||
if (routeName == '' || routeName == '.' || routeName == '..') {
|
||||
throw new BaseException(`"${routeName}/" is only allowed at the beginning of a link DSL.`);
|
||||
}
|
||||
linkParamIndex += 1;
|
||||
if (linkParamIndex < linkParams.length) {
|
||||
let linkParam = linkParams[linkParamIndex];
|
||||
if (isStringMap(linkParam) && !isArray(linkParam)) {
|
||||
routeParams = linkParam;
|
||||
linkParamIndex += 1;
|
||||
}
|
||||
}
|
||||
var routeRecognizer = (_aux ? rules.auxRulesByName : rules.rulesByName).get(routeName);
|
||||
|
||||
if (isBlank(routeRecognizer)) {
|
||||
throw new BaseException(
|
||||
`Component "${getTypeNameForDebugging(parentComponentType)}" has no route named "${routeName}".`);
|
||||
}
|
||||
|
||||
// Create an "unresolved instruction" for async routes
|
||||
// we'll figure out the rest of the route when we resolve the instruction and
|
||||
// perform a navigation
|
||||
if (isBlank(routeRecognizer.handler.componentType)) {
|
||||
var generatedUrl: GeneratedUrl = routeRecognizer.generateComponentPathValues(routeParams);
|
||||
return new UnresolvedInstruction(() => {
|
||||
return routeRecognizer.handler.resolveComponentType().then((_) => {
|
||||
return this._generate(linkParams, ancestorInstructions, prevInstruction, _aux,
|
||||
_originalLink);
|
||||
});
|
||||
}, generatedUrl.urlPath, convertUrlParamsToArray(generatedUrl.urlParams));
|
||||
}
|
||||
|
||||
componentInstruction = _aux ? rules.generateAuxiliary(routeName, routeParams) :
|
||||
rules.generate(routeName, routeParams);
|
||||
}
|
||||
|
||||
// Next, recognize auxiliary instructions.
|
||||
// If we have an ancestor instruction, we preserve whatever aux routes are active from it.
|
||||
while (linkParamIndex < linkParams.length && isArray(linkParams[linkParamIndex])) {
|
||||
let auxParentInstruction: Instruction[] = [parentInstruction];
|
||||
let auxInstruction = this._generate(linkParams[linkParamIndex], auxParentInstruction, null,
|
||||
true, _originalLink);
|
||||
|
||||
// TODO: this will not work for aux routes with parameters or multiple segments
|
||||
auxInstructions[auxInstruction.component.urlPath] = auxInstruction;
|
||||
linkParamIndex += 1;
|
||||
}
|
||||
|
||||
var instruction = new ResolvedInstruction(componentInstruction, null, auxInstructions);
|
||||
|
||||
// If the component is sync, we can generate resolved child route instructions
|
||||
// If not, we'll resolve the instructions at navigation time
|
||||
if (isPresent(componentInstruction) && isPresent(componentInstruction.componentType)) {
|
||||
let childInstruction: Instruction = null;
|
||||
if (componentInstruction.terminal) {
|
||||
if (linkParamIndex >= linkParams.length) {
|
||||
// TODO: throw that there are extra link params beyond the terminal component
|
||||
}
|
||||
} else {
|
||||
let childAncestorComponents: Instruction[] = ancestorInstructions.concat([instruction]);
|
||||
let remainingLinkParams = linkParams.slice(linkParamIndex);
|
||||
childInstruction = this._generate(remainingLinkParams, childAncestorComponents, null, false,
|
||||
_originalLink);
|
||||
}
|
||||
instruction.child = childInstruction;
|
||||
}
|
||||
|
||||
return instruction;
|
||||
}
|
||||
|
||||
public hasRoute(name: string, parentComponent: any): boolean {
|
||||
var rules = this._rules.get(parentComponent);
|
||||
if (isBlank(rules)) {
|
||||
return false;
|
||||
}
|
||||
return rules.hasRoute(name);
|
||||
}
|
||||
|
||||
public generateDefault(componentCursor: Type): Instruction {
|
||||
if (isBlank(componentCursor)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var rules = this._rules.get(componentCursor);
|
||||
if (isBlank(rules) || isBlank(rules.defaultRule)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var defaultChild = null;
|
||||
if (isPresent(rules.defaultRule.handler.componentType)) {
|
||||
var componentInstruction = rules.defaultRule.generate({});
|
||||
if (!rules.defaultRule.terminal) {
|
||||
defaultChild = this.generateDefault(rules.defaultRule.handler.componentType);
|
||||
}
|
||||
return new DefaultInstruction(componentInstruction, defaultChild);
|
||||
}
|
||||
|
||||
return new UnresolvedInstruction(() => {
|
||||
return rules.defaultRule.handler.resolveComponentType().then(
|
||||
(_) => this.generateDefault(componentCursor));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Given: ['/a/b', {c: 2}]
|
||||
* Returns: ['', 'a', 'b', {c: 2}]
|
||||
*/
|
||||
function splitAndFlattenLinkParams(linkParams: any[]): any[] {
|
||||
var accumulation = [];
|
||||
linkParams.forEach(function(item: any) {
|
||||
if (isString(item)) {
|
||||
var strItem: string = <string>item;
|
||||
accumulation = accumulation.concat(strItem.split('/'));
|
||||
} else {
|
||||
accumulation.push(item);
|
||||
}
|
||||
});
|
||||
return accumulation;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Given a list of instructions, returns the most specific instruction
|
||||
*/
|
||||
function mostSpecific(instructions: Instruction[]): Instruction {
|
||||
instructions = instructions.filter((instruction) => isPresent(instruction));
|
||||
if (instructions.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (instructions.length == 1) {
|
||||
return instructions[0];
|
||||
}
|
||||
var first = instructions[0];
|
||||
var rest = instructions.slice(1);
|
||||
return rest.reduce((instruction: Instruction, contender: Instruction) => {
|
||||
if (compareSpecificityStrings(contender.specificity, instruction.specificity) == -1) {
|
||||
return contender;
|
||||
}
|
||||
return instruction;
|
||||
}, first);
|
||||
}
|
||||
|
||||
/*
|
||||
* Expects strings to be in the form of "[0-2]+"
|
||||
* Returns -1 if string A should be sorted above string B, 1 if it should be sorted after,
|
||||
* or 0 if they are the same.
|
||||
*/
|
||||
function compareSpecificityStrings(a: string, b: string): number {
|
||||
var l = Math.min(a.length, b.length);
|
||||
for (var i = 0; i < l; i += 1) {
|
||||
var ai = StringWrapper.charCodeAt(a, i);
|
||||
var bi = StringWrapper.charCodeAt(b, i);
|
||||
var difference = bi - ai;
|
||||
if (difference != 0) {
|
||||
return difference;
|
||||
}
|
||||
}
|
||||
return a.length - b.length;
|
||||
}
|
||||
|
||||
function assertTerminalComponent(component, path) {
|
||||
if (!isType(component)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var annotations = reflector.annotations(component);
|
||||
if (isPresent(annotations)) {
|
||||
for (var i = 0; i < annotations.length; i++) {
|
||||
var annotation = annotations[i];
|
||||
|
||||
if (annotation instanceof RouteConfig) {
|
||||
throw new BaseException(
|
||||
`Child routes are not allowed for "${path}". Use "..." on the parent's route path.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,578 +1,226 @@
|
||||
import {PromiseWrapper, EventEmitter, ObservableWrapper} from '../src/facade/async';
|
||||
import {Map, StringMapWrapper} from '../src/facade/collection';
|
||||
import {isBlank, isPresent, Type} from '../src/facade/lang';
|
||||
import {BaseException} from '../src/facade/exceptions';
|
||||
import {Location} from '@angular/common';
|
||||
import {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from './route_registry';
|
||||
import {ComponentInstruction, Instruction} from './instruction';
|
||||
import {OnInit, provide, ReflectiveInjector, ComponentResolver} from '@angular/core';
|
||||
import {RouterOutlet} from './directives/router_outlet';
|
||||
import {getCanActivateHook} from './lifecycle/route_lifecycle_reflector';
|
||||
import {RouteDefinition} from './route_config/route_config_impl';
|
||||
import {Injectable, Inject} from '@angular/core';
|
||||
import {Type, isBlank, isPresent} from '@angular/facade/src/lang';
|
||||
import {ListWrapper} from '@angular/facade/src/collection';
|
||||
import {
|
||||
EventEmitter,
|
||||
Observable,
|
||||
PromiseWrapper,
|
||||
ObservableWrapper
|
||||
} from '@angular/facade/src/async';
|
||||
import {StringMapWrapper} from '@angular/facade/src/collection';
|
||||
import {BaseException} from '@angular/core';
|
||||
import {RouterUrlSerializer} from './router_url_serializer';
|
||||
import {CanDeactivate} from './interfaces';
|
||||
import {recognize} from './recognize';
|
||||
import {Location} from '@angular/common';
|
||||
import {link} from './link';
|
||||
|
||||
let _resolveToTrue = PromiseWrapper.resolve(true);
|
||||
let _resolveToFalse = PromiseWrapper.resolve(false);
|
||||
import {
|
||||
equalSegments,
|
||||
routeSegmentComponentFactory,
|
||||
RouteSegment,
|
||||
UrlTree,
|
||||
RouteTree,
|
||||
rootNode,
|
||||
TreeNode,
|
||||
UrlSegment,
|
||||
serializeRouteSegmentTree
|
||||
} from './segments';
|
||||
import {hasLifecycleHook} from './lifecycle_reflector';
|
||||
import {DEFAULT_OUTLET_NAME} from './constants';
|
||||
|
||||
export class RouterOutletMap {
|
||||
/** @internal */
|
||||
_outlets: {[name: string]: RouterOutlet} = {};
|
||||
registerOutlet(name: string, outlet: RouterOutlet): void { this._outlets[name] = outlet; }
|
||||
}
|
||||
|
||||
/**
|
||||
* The `Router` is responsible for mapping URLs to components.
|
||||
*
|
||||
* You can see the state of the router by inspecting the read-only field `router.navigating`.
|
||||
* This may be useful for showing a spinner, for instance.
|
||||
*
|
||||
* ## Concepts
|
||||
*
|
||||
* Routers and component instances have a 1:1 correspondence.
|
||||
*
|
||||
* The router holds reference to a number of {@link RouterOutlet}.
|
||||
* An outlet is a placeholder that the router dynamically fills in depending on the current URL.
|
||||
*
|
||||
* When the router navigates from a URL, it must first recognize it and serialize it into an
|
||||
* `Instruction`.
|
||||
* The router uses the `RouteRegistry` to get an `Instruction`.
|
||||
*/
|
||||
@Injectable()
|
||||
export class Router {
|
||||
navigating: boolean = false;
|
||||
lastNavigationAttempt: string;
|
||||
/**
|
||||
* The current `Instruction` for the router
|
||||
*/
|
||||
public currentInstruction: Instruction = null;
|
||||
private _prevTree: RouteTree;
|
||||
private _urlTree: UrlTree;
|
||||
private _locationSubscription: any;
|
||||
private _changes: EventEmitter<void> = new EventEmitter<void>();
|
||||
|
||||
private _currentNavigation: Promise<any> = _resolveToTrue;
|
||||
private _outlet: RouterOutlet = null;
|
||||
|
||||
private _auxRouters = new Map<string, Router>();
|
||||
private _childRouter: Router;
|
||||
|
||||
private _subject: EventEmitter<any> = new EventEmitter();
|
||||
|
||||
|
||||
constructor(public registry: RouteRegistry, public parent: Router, public hostComponent: any,
|
||||
public root?: Router) {}
|
||||
|
||||
/**
|
||||
* Constructs a child router. You probably don't need to use this unless you're writing a reusable
|
||||
* component.
|
||||
*/
|
||||
childRouter(hostComponent: any): Router {
|
||||
return this._childRouter = new ChildRouter(this, hostComponent);
|
||||
constructor(private _rootComponent: Object, private _rootComponentType: Type,
|
||||
private _componentResolver: ComponentResolver,
|
||||
private _urlSerializer: RouterUrlSerializer,
|
||||
private _routerOutletMap: RouterOutletMap, private _location: Location) {
|
||||
this._prevTree = this._createInitialTree();
|
||||
this._setUpLocationChangeListener();
|
||||
this.navigateByUrl(this._location.path());
|
||||
}
|
||||
|
||||
get urlTree(): UrlTree { return this._urlTree; }
|
||||
|
||||
/**
|
||||
* Constructs a child router. You probably don't need to use this unless you're writing a reusable
|
||||
* component.
|
||||
*/
|
||||
auxRouter(hostComponent: any): Router { return new ChildRouter(this, hostComponent); }
|
||||
|
||||
/**
|
||||
* Register an outlet to be notified of primary route changes.
|
||||
*
|
||||
* You probably don't need to use this unless you're writing a reusable component.
|
||||
*/
|
||||
registerPrimaryOutlet(outlet: RouterOutlet): Promise<any> {
|
||||
if (isPresent(outlet.name)) {
|
||||
throw new BaseException(`registerPrimaryOutlet expects to be called with an unnamed outlet.`);
|
||||
}
|
||||
|
||||
if (isPresent(this._outlet)) {
|
||||
throw new BaseException(`Primary outlet is already registered.`);
|
||||
}
|
||||
|
||||
this._outlet = outlet;
|
||||
if (isPresent(this.currentInstruction)) {
|
||||
return this.commit(this.currentInstruction, false);
|
||||
}
|
||||
return _resolveToTrue;
|
||||
navigateByUrl(url: string): Promise<void> {
|
||||
return this._navigate(this._urlSerializer.parse(url));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister an outlet (because it was destroyed, etc).
|
||||
*
|
||||
* You probably don't need to use this unless you're writing a custom outlet implementation.
|
||||
*/
|
||||
unregisterPrimaryOutlet(outlet: RouterOutlet): void {
|
||||
if (isPresent(outlet.name)) {
|
||||
throw new BaseException(`registerPrimaryOutlet expects to be called with an unnamed outlet.`);
|
||||
}
|
||||
this._outlet = null;
|
||||
navigate(changes: any[], segment?: RouteSegment): Promise<void> {
|
||||
return this._navigate(this.createUrlTree(changes, segment));
|
||||
}
|
||||
|
||||
dispose(): void { ObservableWrapper.dispose(this._locationSubscription); }
|
||||
|
||||
/**
|
||||
* Register an outlet to notified of auxiliary route changes.
|
||||
*
|
||||
* You probably don't need to use this unless you're writing a reusable component.
|
||||
*/
|
||||
registerAuxOutlet(outlet: RouterOutlet): Promise<any> {
|
||||
var outletName = outlet.name;
|
||||
if (isBlank(outletName)) {
|
||||
throw new BaseException(`registerAuxOutlet expects to be called with an outlet with a name.`);
|
||||
}
|
||||
|
||||
var router = this.auxRouter(this.hostComponent);
|
||||
|
||||
this._auxRouters.set(outletName, router);
|
||||
router._outlet = outlet;
|
||||
|
||||
var auxInstruction;
|
||||
if (isPresent(this.currentInstruction) &&
|
||||
isPresent(auxInstruction = this.currentInstruction.auxInstruction[outletName])) {
|
||||
return router.commit(auxInstruction);
|
||||
}
|
||||
return _resolveToTrue;
|
||||
private _createInitialTree(): RouteTree {
|
||||
let root = new RouteSegment([new UrlSegment("", null, null)], null, DEFAULT_OUTLET_NAME,
|
||||
this._rootComponentType, null);
|
||||
return new RouteTree(new TreeNode<RouteSegment>(root, []));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Given an instruction, returns `true` if the instruction is currently active,
|
||||
* otherwise `false`.
|
||||
*/
|
||||
isRouteActive(instruction: Instruction): boolean {
|
||||
var router: Router = this;
|
||||
|
||||
if (isBlank(this.currentInstruction)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// `instruction` corresponds to the root router
|
||||
while (isPresent(router.parent) && isPresent(instruction.child)) {
|
||||
router = router.parent;
|
||||
instruction = instruction.child;
|
||||
}
|
||||
|
||||
if (isBlank(instruction.component) || isBlank(this.currentInstruction.component) ||
|
||||
this.currentInstruction.component.routeName != instruction.component.routeName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let paramEquals = true;
|
||||
|
||||
if (isPresent(this.currentInstruction.component.params)) {
|
||||
StringMapWrapper.forEach(instruction.component.params, (value, key) => {
|
||||
if (this.currentInstruction.component.params[key] !== value) {
|
||||
paramEquals = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return paramEquals;
|
||||
private _setUpLocationChangeListener(): void {
|
||||
this._locationSubscription = this._location.subscribe(
|
||||
(change) => { this._navigate(this._urlSerializer.parse(change['url'])); });
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Dynamically update the routing configuration and trigger a navigation.
|
||||
*
|
||||
* ### Usage
|
||||
*
|
||||
* ```
|
||||
* router.config([
|
||||
* { 'path': '/', 'component': IndexComp },
|
||||
* { 'path': '/user/:id', 'component': UserComp },
|
||||
* ]);
|
||||
* ```
|
||||
*/
|
||||
config(definitions: RouteDefinition[]): Promise<any> {
|
||||
definitions.forEach(
|
||||
(routeDefinition) => { this.registry.config(this.hostComponent, routeDefinition); });
|
||||
return this.renavigate();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Navigate based on the provided Route Link DSL. It's preferred to navigate with this method
|
||||
* over `navigateByUrl`.
|
||||
*
|
||||
* ### Usage
|
||||
*
|
||||
* This method takes an array representing the Route Link DSL:
|
||||
* ```
|
||||
* ['./MyCmp', {param: 3}]
|
||||
* ```
|
||||
* See the {@link RouterLink} directive for more.
|
||||
*/
|
||||
navigate(linkParams: any[]): Promise<any> {
|
||||
var instruction = this.generate(linkParams);
|
||||
return this.navigateByInstruction(instruction, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Navigate to a URL. Returns a promise that resolves when navigation is complete.
|
||||
* It's preferred to navigate with `navigate` instead of this method, since URLs are more brittle.
|
||||
*
|
||||
* If the given URL begins with a `/`, router will navigate absolutely.
|
||||
* If the given URL does not begin with `/`, the router will navigate relative to this component.
|
||||
*/
|
||||
navigateByUrl(url: string, _skipLocationChange: boolean = false): Promise<any> {
|
||||
return this._currentNavigation = this._currentNavigation.then((_) => {
|
||||
this.lastNavigationAttempt = url;
|
||||
this._startNavigating();
|
||||
return this._afterPromiseFinishNavigating(this.recognize(url).then((instruction) => {
|
||||
if (isBlank(instruction)) {
|
||||
return false;
|
||||
}
|
||||
return this._navigate(instruction, _skipLocationChange);
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Navigate via the provided instruction. Returns a promise that resolves when navigation is
|
||||
* complete.
|
||||
*/
|
||||
navigateByInstruction(instruction: Instruction,
|
||||
_skipLocationChange: boolean = false): Promise<any> {
|
||||
if (isBlank(instruction)) {
|
||||
return _resolveToFalse;
|
||||
}
|
||||
return this._currentNavigation = this._currentNavigation.then((_) => {
|
||||
this._startNavigating();
|
||||
return this._afterPromiseFinishNavigating(this._navigate(instruction, _skipLocationChange));
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_settleInstruction(instruction: Instruction): Promise<any> {
|
||||
return instruction.resolveComponent().then((_) => {
|
||||
var unsettledInstructions: Array<Promise<any>> = [];
|
||||
|
||||
if (isPresent(instruction.component)) {
|
||||
instruction.component.reuse = false;
|
||||
}
|
||||
|
||||
if (isPresent(instruction.child)) {
|
||||
unsettledInstructions.push(this._settleInstruction(instruction.child));
|
||||
}
|
||||
|
||||
StringMapWrapper.forEach(instruction.auxInstruction, (instruction: Instruction, _) => {
|
||||
unsettledInstructions.push(this._settleInstruction(instruction));
|
||||
});
|
||||
return PromiseWrapper.all(unsettledInstructions);
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_navigate(instruction: Instruction, _skipLocationChange: boolean): Promise<any> {
|
||||
return this._settleInstruction(instruction)
|
||||
.then((_) => this._routerCanReuse(instruction))
|
||||
.then((_) => this._canActivate(instruction))
|
||||
.then((result: boolean) => {
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
return this._routerCanDeactivate(instruction)
|
||||
.then((result: boolean) => {
|
||||
if (result) {
|
||||
return this.commit(instruction, _skipLocationChange)
|
||||
.then((_) => {
|
||||
this._emitNavigationFinish(instruction.toRootUrl());
|
||||
return true;
|
||||
});
|
||||
private _navigate(url: UrlTree): Promise<void> {
|
||||
this._urlTree = url;
|
||||
return recognize(this._componentResolver, this._rootComponentType, url)
|
||||
.then(currTree => {
|
||||
return new _LoadSegments(currTree, this._prevTree)
|
||||
.load(this._routerOutletMap, this._rootComponent)
|
||||
.then(updated => {
|
||||
if (updated) {
|
||||
this._prevTree = currTree;
|
||||
this._location.go(this._urlSerializer.serialize(this._urlTree));
|
||||
this._changes.emit(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private _emitNavigationFinish(url): void { ObservableWrapper.callEmit(this._subject, url); }
|
||||
/** @internal */
|
||||
_emitNavigationFail(url): void { ObservableWrapper.callError(this._subject, url); }
|
||||
|
||||
private _afterPromiseFinishNavigating(promise: Promise<any>): Promise<any> {
|
||||
return PromiseWrapper.catchError(promise.then((_) => this._finishNavigating()), (err) => {
|
||||
this._finishNavigating();
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Recursively set reuse flags
|
||||
*/
|
||||
/** @internal */
|
||||
_routerCanReuse(instruction: Instruction): Promise<any> {
|
||||
if (isBlank(this._outlet)) {
|
||||
return _resolveToFalse;
|
||||
}
|
||||
if (isBlank(instruction.component)) {
|
||||
return _resolveToTrue;
|
||||
}
|
||||
return this._outlet.routerCanReuse(instruction.component)
|
||||
.then((result) => {
|
||||
instruction.component.reuse = result;
|
||||
if (result && isPresent(this._childRouter) && isPresent(instruction.child)) {
|
||||
return this._childRouter._routerCanReuse(instruction.child);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private _canActivate(nextInstruction: Instruction): Promise<boolean> {
|
||||
return canActivateOne(nextInstruction, this.currentInstruction);
|
||||
}
|
||||
|
||||
private _routerCanDeactivate(instruction: Instruction): Promise<boolean> {
|
||||
if (isBlank(this._outlet)) {
|
||||
return _resolveToTrue;
|
||||
}
|
||||
var next: Promise<boolean>;
|
||||
var childInstruction: Instruction = null;
|
||||
var reuse: boolean = false;
|
||||
var componentInstruction: ComponentInstruction = null;
|
||||
if (isPresent(instruction)) {
|
||||
childInstruction = instruction.child;
|
||||
componentInstruction = instruction.component;
|
||||
reuse = isBlank(instruction.component) || instruction.component.reuse;
|
||||
}
|
||||
if (reuse) {
|
||||
next = _resolveToTrue;
|
||||
createUrlTree(changes: any[], segment?: RouteSegment): UrlTree {
|
||||
if (isPresent(this._prevTree)) {
|
||||
let s = isPresent(segment) ? segment : this._prevTree.root;
|
||||
return link(s, this._prevTree, this.urlTree, changes);
|
||||
} else {
|
||||
next = this._outlet.routerCanDeactivate(componentInstruction);
|
||||
return null;
|
||||
}
|
||||
// TODO: aux route lifecycle hooks
|
||||
return next.then<boolean>((result): boolean | Promise<boolean> => {
|
||||
if (result == false) {
|
||||
return false;
|
||||
}
|
||||
if (isPresent(this._childRouter)) {
|
||||
// TODO: ideally, this closure would map to async-await in Dart.
|
||||
// For now, casting to any to suppress an error.
|
||||
return <any>this._childRouter._routerCanDeactivate(childInstruction);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates this router and all descendant routers according to the given instruction
|
||||
*/
|
||||
commit(instruction: Instruction, _skipLocationChange: boolean = false): Promise<any> {
|
||||
this.currentInstruction = instruction;
|
||||
serializeUrl(url: UrlTree): string { return this._urlSerializer.serialize(url); }
|
||||
|
||||
var next: Promise<any> = _resolveToTrue;
|
||||
if (isPresent(this._outlet) && isPresent(instruction.component)) {
|
||||
var componentInstruction = instruction.component;
|
||||
if (componentInstruction.reuse) {
|
||||
next = this._outlet.reuse(componentInstruction);
|
||||
} else {
|
||||
next =
|
||||
this.deactivate(instruction).then((_) => this._outlet.activate(componentInstruction));
|
||||
}
|
||||
if (isPresent(instruction.child)) {
|
||||
next = next.then((_) => {
|
||||
if (isPresent(this._childRouter)) {
|
||||
return this._childRouter.commit(instruction.child);
|
||||
get changes(): Observable<void> { return this._changes; }
|
||||
|
||||
get routeTree(): RouteTree { return this._prevTree; }
|
||||
}
|
||||
|
||||
|
||||
class _LoadSegments {
|
||||
private deactivations: Object[][] = [];
|
||||
private performMutation: boolean = true;
|
||||
|
||||
constructor(private currTree: RouteTree, private prevTree: RouteTree) {}
|
||||
|
||||
load(parentOutletMap: RouterOutletMap, rootComponent: Object): Promise<boolean> {
|
||||
let prevRoot = isPresent(this.prevTree) ? rootNode(this.prevTree) : null;
|
||||
let currRoot = rootNode(this.currTree);
|
||||
|
||||
return this.canDeactivate(currRoot, prevRoot, parentOutletMap, rootComponent)
|
||||
.then(res => {
|
||||
this.performMutation = true;
|
||||
if (res) {
|
||||
this.loadChildSegments(currRoot, prevRoot, parentOutletMap, [rootComponent]);
|
||||
}
|
||||
return res;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var promises: Promise<any>[] = [];
|
||||
this._auxRouters.forEach((router, name) => {
|
||||
if (isPresent(instruction.auxInstruction[name])) {
|
||||
promises.push(router.commit(instruction.auxInstruction[name]));
|
||||
}
|
||||
private canDeactivate(currRoot: TreeNode<RouteSegment>, prevRoot: TreeNode<RouteSegment>,
|
||||
outletMap: RouterOutletMap, rootComponent: Object): Promise<boolean> {
|
||||
this.performMutation = false;
|
||||
this.loadChildSegments(currRoot, prevRoot, outletMap, [rootComponent]);
|
||||
|
||||
let allPaths = PromiseWrapper.all(this.deactivations.map(r => this.checkCanDeactivatePath(r)));
|
||||
return allPaths.then((values: boolean[]) => values.filter(v => v).length === values.length);
|
||||
}
|
||||
|
||||
private checkCanDeactivatePath(path: Object[]): Promise<boolean> {
|
||||
let curr = PromiseWrapper.resolve(true);
|
||||
for (let p of ListWrapper.reversed(path)) {
|
||||
curr = curr.then(_ => {
|
||||
if (hasLifecycleHook("routerCanDeactivate", p)) {
|
||||
return (<CanDeactivate>p).routerCanDeactivate(this.prevTree, this.currTree);
|
||||
} else {
|
||||
return _;
|
||||
}
|
||||
});
|
||||
}
|
||||
return curr;
|
||||
}
|
||||
|
||||
private loadChildSegments(currNode: TreeNode<RouteSegment>, prevNode: TreeNode<RouteSegment>,
|
||||
outletMap: RouterOutletMap, components: Object[]): void {
|
||||
let prevChildren = isPresent(prevNode) ?
|
||||
prevNode.children.reduce(
|
||||
(m, c) => {
|
||||
m[c.value.outlet] = c;
|
||||
return m;
|
||||
},
|
||||
{}) :
|
||||
{};
|
||||
|
||||
currNode.children.forEach(c => {
|
||||
this.loadSegments(c, prevChildren[c.value.outlet], outletMap, components);
|
||||
StringMapWrapper.delete(prevChildren, c.value.outlet);
|
||||
});
|
||||
|
||||
return next.then((_) => PromiseWrapper.all(promises));
|
||||
StringMapWrapper.forEach(prevChildren,
|
||||
(v, k) => this.unloadOutlet(outletMap._outlets[k], components));
|
||||
}
|
||||
|
||||
loadSegments(currNode: TreeNode<RouteSegment>, prevNode: TreeNode<RouteSegment>,
|
||||
parentOutletMap: RouterOutletMap, components: Object[]): void {
|
||||
let curr = currNode.value;
|
||||
let prev = isPresent(prevNode) ? prevNode.value : null;
|
||||
let outlet = this.getOutlet(parentOutletMap, currNode.value);
|
||||
|
||||
/** @internal */
|
||||
_startNavigating(): void { this.navigating = true; }
|
||||
|
||||
/** @internal */
|
||||
_finishNavigating(): void { this.navigating = false; }
|
||||
|
||||
|
||||
/**
|
||||
* Subscribe to URL updates from the router
|
||||
*/
|
||||
subscribe(onNext: (value: any) => void, onError?: (value: any) => void): Object {
|
||||
return ObservableWrapper.subscribe(this._subject, onNext, onError);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes the contents of this router's outlet and all descendant outlets
|
||||
*/
|
||||
deactivate(instruction: Instruction): Promise<any> {
|
||||
var childInstruction: Instruction = null;
|
||||
var componentInstruction: ComponentInstruction = null;
|
||||
if (isPresent(instruction)) {
|
||||
childInstruction = instruction.child;
|
||||
componentInstruction = instruction.component;
|
||||
if (equalSegments(curr, prev)) {
|
||||
this.loadChildSegments(currNode, prevNode, outlet.outletMap,
|
||||
components.concat([outlet.loadedComponent]));
|
||||
} else {
|
||||
this.unloadOutlet(outlet, components);
|
||||
if (this.performMutation) {
|
||||
let outletMap = new RouterOutletMap();
|
||||
let loadedComponent = this.loadNewSegment(outletMap, curr, prev, outlet);
|
||||
this.loadChildSegments(currNode, prevNode, outletMap, components.concat([loadedComponent]));
|
||||
}
|
||||
}
|
||||
var next: Promise<any> = _resolveToTrue;
|
||||
if (isPresent(this._childRouter)) {
|
||||
next = this._childRouter.deactivate(childInstruction);
|
||||
}
|
||||
|
||||
private loadNewSegment(outletMap: RouterOutletMap, curr: RouteSegment, prev: RouteSegment,
|
||||
outlet: RouterOutlet): Object {
|
||||
let resolved = ReflectiveInjector.resolve(
|
||||
[provide(RouterOutletMap, {useValue: outletMap}), provide(RouteSegment, {useValue: curr})]);
|
||||
let ref = outlet.load(routeSegmentComponentFactory(curr), resolved, outletMap);
|
||||
if (hasLifecycleHook("routerOnActivate", ref.instance)) {
|
||||
ref.instance.routerOnActivate(curr, prev, this.currTree, this.prevTree);
|
||||
}
|
||||
if (isPresent(this._outlet)) {
|
||||
next = next.then((_) => this._outlet.deactivate(componentInstruction));
|
||||
return ref.instance;
|
||||
}
|
||||
|
||||
private getOutlet(outletMap: RouterOutletMap, segment: RouteSegment): RouterOutlet {
|
||||
let outlet = outletMap._outlets[segment.outlet];
|
||||
if (isBlank(outlet)) {
|
||||
if (segment.outlet == DEFAULT_OUTLET_NAME) {
|
||||
throw new BaseException(`Cannot find default outlet`);
|
||||
} else {
|
||||
throw new BaseException(`Cannot find the outlet ${segment.outlet}`);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: handle aux routes
|
||||
|
||||
return next;
|
||||
return outlet;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Given a URL, returns an instruction representing the component graph
|
||||
*/
|
||||
recognize(url: string): Promise<Instruction> {
|
||||
var ancestorComponents = this._getAncestorInstructions();
|
||||
return this.registry.recognize(url, ancestorComponents);
|
||||
}
|
||||
|
||||
private _getAncestorInstructions(): Instruction[] {
|
||||
var ancestorInstructions: Instruction[] = [this.currentInstruction];
|
||||
var ancestorRouter: Router = this;
|
||||
while (isPresent(ancestorRouter = ancestorRouter.parent)) {
|
||||
ancestorInstructions.unshift(ancestorRouter.currentInstruction);
|
||||
}
|
||||
return ancestorInstructions;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Navigates to either the last URL successfully navigated to, or the last URL requested if the
|
||||
* router has yet to successfully navigate.
|
||||
*/
|
||||
renavigate(): Promise<any> {
|
||||
if (isBlank(this.lastNavigationAttempt)) {
|
||||
return this._currentNavigation;
|
||||
}
|
||||
return this.navigateByUrl(this.lastNavigationAttempt);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate an `Instruction` based on the provided Route Link DSL.
|
||||
*/
|
||||
generate(linkParams: any[]): Instruction {
|
||||
var ancestorInstructions = this._getAncestorInstructions();
|
||||
return this.registry.generate(linkParams, ancestorInstructions);
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class RootRouter extends Router {
|
||||
/** @internal */
|
||||
_location: Location;
|
||||
/** @internal */
|
||||
_locationSub: Object;
|
||||
|
||||
constructor(registry: RouteRegistry, location: Location,
|
||||
@Inject(ROUTER_PRIMARY_COMPONENT) primaryComponent: Type) {
|
||||
super(registry, null, primaryComponent);
|
||||
this.root = this;
|
||||
this._location = location;
|
||||
this._locationSub = this._location.subscribe((change) => {
|
||||
// we call recognize ourselves
|
||||
this.recognize(change['url'])
|
||||
.then((instruction) => {
|
||||
if (isPresent(instruction)) {
|
||||
this.navigateByInstruction(instruction, isPresent(change['pop']))
|
||||
.then((_) => {
|
||||
// this is a popstate event; no need to change the URL
|
||||
if (isPresent(change['pop']) && change['type'] != 'hashchange') {
|
||||
return;
|
||||
}
|
||||
var emitPath = instruction.toUrlPath();
|
||||
var emitQuery = instruction.toUrlQuery();
|
||||
if (emitPath.length > 0 && emitPath[0] != '/') {
|
||||
emitPath = '/' + emitPath;
|
||||
}
|
||||
|
||||
// We've opted to use pushstate and popState APIs regardless of whether you
|
||||
// an app uses HashLocationStrategy or PathLocationStrategy.
|
||||
// However, apps that are migrating might have hash links that operate outside
|
||||
// angular to which routing must respond.
|
||||
// Therefore we know that all hashchange events occur outside Angular.
|
||||
// To support these cases where we respond to hashchanges and redirect as a
|
||||
// result, we need to replace the top item on the stack.
|
||||
if (change['type'] == 'hashchange') {
|
||||
if (instruction.toRootUrl() != this._location.path()) {
|
||||
this._location.replaceState(emitPath, emitQuery);
|
||||
}
|
||||
} else {
|
||||
this._location.go(emitPath, emitQuery);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this._emitNavigationFail(change['url']);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
this.registry.configFromComponent(primaryComponent);
|
||||
this.navigateByUrl(location.path());
|
||||
}
|
||||
|
||||
commit(instruction: Instruction, _skipLocationChange: boolean = false): Promise<any> {
|
||||
var emitPath = instruction.toUrlPath();
|
||||
var emitQuery = instruction.toUrlQuery();
|
||||
if (emitPath.length > 0 && emitPath[0] != '/') {
|
||||
emitPath = '/' + emitPath;
|
||||
}
|
||||
var promise = super.commit(instruction);
|
||||
if (!_skipLocationChange) {
|
||||
promise = promise.then((_) => { this._location.go(emitPath, emitQuery); });
|
||||
}
|
||||
return promise;
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
if (isPresent(this._locationSub)) {
|
||||
ObservableWrapper.dispose(this._locationSub);
|
||||
this._locationSub = null;
|
||||
private unloadOutlet(outlet: RouterOutlet, components: Object[]): void {
|
||||
if (isPresent(outlet) && outlet.isLoaded) {
|
||||
StringMapWrapper.forEach(outlet.outletMap._outlets,
|
||||
(v, k) => this.unloadOutlet(v, components));
|
||||
if (this.performMutation) {
|
||||
outlet.unload();
|
||||
} else {
|
||||
this.deactivations.push(components.concat([outlet.loadedComponent]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ChildRouter extends Router {
|
||||
constructor(parent: Router, hostComponent) {
|
||||
super(parent.registry, parent, hostComponent, parent.root);
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
|
||||
navigateByUrl(url: string, _skipLocationChange: boolean = false): Promise<any> {
|
||||
// Delegate navigation to the root router
|
||||
return this.parent.navigateByUrl(url, _skipLocationChange);
|
||||
}
|
||||
|
||||
navigateByInstruction(instruction: Instruction,
|
||||
_skipLocationChange: boolean = false): Promise<any> {
|
||||
// Delegate navigation to the root router
|
||||
return this.parent.navigateByInstruction(instruction, _skipLocationChange);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function canActivateOne(nextInstruction: Instruction,
|
||||
prevInstruction: Instruction): Promise<boolean> {
|
||||
var next = _resolveToTrue;
|
||||
if (isBlank(nextInstruction.component)) {
|
||||
return next;
|
||||
}
|
||||
if (isPresent(nextInstruction.child)) {
|
||||
next = canActivateOne(nextInstruction.child,
|
||||
isPresent(prevInstruction) ? prevInstruction.child : null);
|
||||
}
|
||||
return next.then<boolean>((result: boolean): boolean => {
|
||||
if (result == false) {
|
||||
return false;
|
||||
}
|
||||
if (nextInstruction.component.reuse) {
|
||||
return true;
|
||||
}
|
||||
var hook = getCanActivateHook(nextInstruction.component.componentType);
|
||||
if (isPresent(hook)) {
|
||||
return hook(nextInstruction.component,
|
||||
isPresent(prevInstruction) ? prevInstruction.component : null);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,41 +1,8 @@
|
||||
import {ROUTER_PROVIDERS_COMMON} from './router_providers_common';
|
||||
import {Provider} from '@angular/core';
|
||||
import {BrowserPlatformLocation} from '@angular/platform-browser';
|
||||
import {PlatformLocation} from '@angular/common';
|
||||
|
||||
/**
|
||||
* A list of {@link Provider}s. To use the router, you must add this to your application.
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
|
||||
*
|
||||
* ```
|
||||
* import {Component} from '@angular/core';
|
||||
* import {
|
||||
* ROUTER_DIRECTIVES,
|
||||
* ROUTER_PROVIDERS,
|
||||
* RouteConfig
|
||||
* } from '@angular/router';
|
||||
*
|
||||
* @Component({directives: [ROUTER_DIRECTIVES]})
|
||||
* @RouteConfig([
|
||||
* {...},
|
||||
* ])
|
||||
* class AppCmp {
|
||||
* // ...
|
||||
* }
|
||||
*
|
||||
* bootstrap(AppCmp, [ROUTER_PROVIDERS]);
|
||||
* ```
|
||||
*/
|
||||
export const ROUTER_PROVIDERS: any[] = /*@ts2dart_const*/[
|
||||
ROUTER_PROVIDERS_COMMON,
|
||||
/*@ts2dart_const*/ (
|
||||
/* @ts2dart_Provider */ {provide: PlatformLocation, useClass: BrowserPlatformLocation}),
|
||||
/*@ts2dart_Provider*/ {provide: PlatformLocation, useClass: BrowserPlatformLocation},
|
||||
];
|
||||
|
||||
/**
|
||||
* Use {@link ROUTER_PROVIDERS} instead.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
export const ROUTER_BINDINGS = /*@ts2dart_const*/ ROUTER_PROVIDERS;
|
||||
|
||||
@@ -1,39 +1,31 @@
|
||||
import {ApplicationRef, Provider} from '@angular/core';
|
||||
import {OpaqueToken, ComponentResolver} from '@angular/core';
|
||||
import {LocationStrategy, PathLocationStrategy, Location} from '@angular/common';
|
||||
import {Router, RootRouter} from './router';
|
||||
import {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from './route_registry';
|
||||
import {Type} from '../src/facade/lang';
|
||||
import {BaseException} from '../src/facade/exceptions';
|
||||
import {Router, RouterOutletMap} from './router';
|
||||
import {RouterUrlSerializer, DefaultRouterUrlSerializer} from './router_url_serializer';
|
||||
import {ApplicationRef} from '@angular/core';
|
||||
import {BaseException} from '@angular/core';
|
||||
|
||||
/**
|
||||
* The Platform agnostic ROUTER PROVIDERS
|
||||
*/
|
||||
export const ROUTER_PROVIDERS_COMMON: any[] = /*@ts2dart_const*/[
|
||||
RouteRegistry,
|
||||
/* @ts2dart_Provider */ {provide: LocationStrategy, useClass: PathLocationStrategy},
|
||||
Location,
|
||||
{
|
||||
RouterOutletMap,
|
||||
/*@ts2dart_Provider*/ {provide: RouterUrlSerializer, useClass: DefaultRouterUrlSerializer},
|
||||
/*@ts2dart_Provider*/ {provide: LocationStrategy, useClass: PathLocationStrategy}, Location,
|
||||
/*@ts2dart_Provider*/ {
|
||||
provide: Router,
|
||||
useFactory: routerFactory,
|
||||
deps: [RouteRegistry, Location, ROUTER_PRIMARY_COMPONENT, ApplicationRef]
|
||||
deps: /*@ts2dart_const*/
|
||||
[ApplicationRef, ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location],
|
||||
},
|
||||
{
|
||||
provide: ROUTER_PRIMARY_COMPONENT,
|
||||
useFactory: routerPrimaryComponentFactory,
|
||||
deps: /*@ts2dart_const*/ ([ApplicationRef])
|
||||
}
|
||||
];
|
||||
|
||||
function routerFactory(registry: RouteRegistry, location: Location, primaryComponent: Type,
|
||||
appRef: ApplicationRef): RootRouter {
|
||||
var rootRouter = new RootRouter(registry, location, primaryComponent);
|
||||
appRef.registerDisposeListener(() => rootRouter.dispose());
|
||||
return rootRouter;
|
||||
}
|
||||
|
||||
function routerPrimaryComponentFactory(app: ApplicationRef): Type {
|
||||
function routerFactory(app: ApplicationRef, componentResolver: ComponentResolver,
|
||||
urlSerializer: RouterUrlSerializer, routerOutletMap: RouterOutletMap,
|
||||
location: Location): Router {
|
||||
if (app.componentTypes.length == 0) {
|
||||
throw new BaseException("Bootstrap at least one component before injecting Router.");
|
||||
}
|
||||
return app.componentTypes[0];
|
||||
// TODO: vsavkin this should not be null
|
||||
let router = new Router(null, app.componentTypes[0], componentResolver, urlSerializer,
|
||||
routerOutletMap, location);
|
||||
app.registerDisposeListener(() => router.dispose());
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
import {UrlSegment, Tree, TreeNode, rootNode, UrlTree} from './segments';
|
||||
import {BaseException} from '@angular/core';
|
||||
import {isBlank, isPresent, RegExpWrapper} from '@angular/facade/src/lang';
|
||||
|
||||
export abstract class RouterUrlSerializer {
|
||||
abstract parse(url: string): UrlTree;
|
||||
abstract serialize(tree: UrlTree): string;
|
||||
}
|
||||
|
||||
export class DefaultRouterUrlSerializer extends RouterUrlSerializer {
|
||||
parse(url: string): UrlTree {
|
||||
let root = new _UrlParser().parse(url);
|
||||
return new UrlTree(root);
|
||||
}
|
||||
|
||||
serialize(tree: UrlTree): string { return _serializeUrlTreeNode(rootNode(tree)); }
|
||||
}
|
||||
|
||||
function _serializeUrlTreeNode(node: TreeNode<UrlSegment>): string {
|
||||
return `${node.value}${_serializeChildren(node)}`;
|
||||
}
|
||||
|
||||
function _serializeUrlTreeNodes(nodes: TreeNode<UrlSegment>[]): string {
|
||||
let main = nodes[0].value.toString();
|
||||
let auxNodes = nodes.slice(1);
|
||||
let aux = auxNodes.length > 0 ? `(${auxNodes.map(_serializeUrlTreeNode).join("//")})` : "";
|
||||
let children = _serializeChildren(nodes[0]);
|
||||
return `${main}${aux}${children}`;
|
||||
}
|
||||
|
||||
function _serializeChildren(node: TreeNode<UrlSegment>): string {
|
||||
if (node.children.length > 0) {
|
||||
let slash = isBlank(node.children[0].value.segment) ? "" : "/";
|
||||
return `${slash}${_serializeUrlTreeNodes(node.children)}`;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
var SEGMENT_RE = RegExpWrapper.create('^[^\\/\\(\\)\\?;=&#]+');
|
||||
function matchUrlSegment(str: string): string {
|
||||
var match = RegExpWrapper.firstMatch(SEGMENT_RE, str);
|
||||
return isPresent(match) ? match[0] : '';
|
||||
}
|
||||
var QUERY_PARAM_VALUE_RE = RegExpWrapper.create('^[^\\(\\)\\?;&#]+');
|
||||
function matchUrlQueryParamValue(str: string): string {
|
||||
var match = RegExpWrapper.firstMatch(QUERY_PARAM_VALUE_RE, str);
|
||||
return isPresent(match) ? match[0] : '';
|
||||
}
|
||||
|
||||
class _UrlParser {
|
||||
private _remaining: string;
|
||||
|
||||
peekStartsWith(str: string): boolean { return this._remaining.startsWith(str); }
|
||||
|
||||
capture(str: string): void {
|
||||
if (!this._remaining.startsWith(str)) {
|
||||
throw new BaseException(`Expected "${str}".`);
|
||||
}
|
||||
this._remaining = this._remaining.substring(str.length);
|
||||
}
|
||||
|
||||
parse(url: string): TreeNode<UrlSegment> {
|
||||
this._remaining = url;
|
||||
if (url == '' || url == '/') {
|
||||
return new TreeNode<UrlSegment>(new UrlSegment('', null, null), []);
|
||||
} else {
|
||||
return this.parseRoot();
|
||||
}
|
||||
}
|
||||
|
||||
parseRoot(): TreeNode<UrlSegment> {
|
||||
let segments = this.parseSegments();
|
||||
let queryParams = this.peekStartsWith('?') ? this.parseQueryParams() : null;
|
||||
return new TreeNode<UrlSegment>(new UrlSegment('', queryParams, null), segments);
|
||||
}
|
||||
|
||||
parseSegments(outletName: string = null): TreeNode<UrlSegment>[] {
|
||||
if (this._remaining.length == 0) {
|
||||
return [];
|
||||
}
|
||||
if (this.peekStartsWith('/')) {
|
||||
this.capture('/');
|
||||
}
|
||||
var path = matchUrlSegment(this._remaining);
|
||||
this.capture(path);
|
||||
|
||||
|
||||
if (path.indexOf(":") > -1) {
|
||||
let parts = path.split(":");
|
||||
outletName = parts[0];
|
||||
path = parts[1];
|
||||
}
|
||||
|
||||
var matrixParams: {[key: string]: any} = null;
|
||||
if (this.peekStartsWith(';')) {
|
||||
matrixParams = this.parseMatrixParams();
|
||||
}
|
||||
|
||||
var aux = [];
|
||||
if (this.peekStartsWith('(')) {
|
||||
aux = this.parseAuxiliaryRoutes();
|
||||
}
|
||||
|
||||
var children: TreeNode<UrlSegment>[] = [];
|
||||
if (this.peekStartsWith('/') && !this.peekStartsWith('//')) {
|
||||
this.capture('/');
|
||||
children = this.parseSegments();
|
||||
}
|
||||
|
||||
if (isPresent(matrixParams)) {
|
||||
let matrixParamsSegment = new UrlSegment(null, matrixParams, null);
|
||||
let matrixParamsNode = new TreeNode<UrlSegment>(matrixParamsSegment, children);
|
||||
let segment = new UrlSegment(path, null, outletName);
|
||||
return [new TreeNode<UrlSegment>(segment, [matrixParamsNode].concat(aux))];
|
||||
} else {
|
||||
let segment = new UrlSegment(path, null, outletName);
|
||||
let node = new TreeNode<UrlSegment>(segment, children);
|
||||
return [node].concat(aux);
|
||||
}
|
||||
}
|
||||
|
||||
parseQueryParams(): {[key: string]: any} {
|
||||
var params: {[key: string]: any} = {};
|
||||
this.capture('?');
|
||||
this.parseQueryParam(params);
|
||||
while (this._remaining.length > 0 && this.peekStartsWith('&')) {
|
||||
this.capture('&');
|
||||
this.parseQueryParam(params);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
parseMatrixParams(): {[key: string]: any} {
|
||||
var params: {[key: string]: any} = {};
|
||||
while (this._remaining.length > 0 && this.peekStartsWith(';')) {
|
||||
this.capture(';');
|
||||
this.parseParam(params);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
parseParam(params: {[key: string]: any}): void {
|
||||
var key = matchUrlSegment(this._remaining);
|
||||
if (isBlank(key)) {
|
||||
return;
|
||||
}
|
||||
this.capture(key);
|
||||
var value: any = "true";
|
||||
if (this.peekStartsWith('=')) {
|
||||
this.capture('=');
|
||||
var valueMatch = matchUrlSegment(this._remaining);
|
||||
if (isPresent(valueMatch)) {
|
||||
value = valueMatch;
|
||||
this.capture(value);
|
||||
}
|
||||
}
|
||||
|
||||
params[key] = value;
|
||||
}
|
||||
|
||||
parseQueryParam(params: {[key: string]: any}): void {
|
||||
var key = matchUrlSegment(this._remaining);
|
||||
if (isBlank(key)) {
|
||||
return;
|
||||
}
|
||||
this.capture(key);
|
||||
var value: any = "true";
|
||||
if (this.peekStartsWith('=')) {
|
||||
this.capture('=');
|
||||
var valueMatch = matchUrlQueryParamValue(this._remaining);
|
||||
if (isPresent(valueMatch)) {
|
||||
value = valueMatch;
|
||||
this.capture(value);
|
||||
}
|
||||
}
|
||||
|
||||
params[key] = value;
|
||||
}
|
||||
|
||||
parseAuxiliaryRoutes(): TreeNode<UrlSegment>[] {
|
||||
var segments = [];
|
||||
this.capture('(');
|
||||
|
||||
while (!this.peekStartsWith(')') && this._remaining.length > 0) {
|
||||
segments = segments.concat(this.parseSegments("aux"));
|
||||
if (this.peekStartsWith('//')) {
|
||||
this.capture('//');
|
||||
}
|
||||
}
|
||||
this.capture(')');
|
||||
|
||||
return segments;
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import {isPresent, Type} from '../../../src/facade/lang';
|
||||
import {RouteHandler} from './route_handler';
|
||||
import {RouteData, BLANK_ROUTE_DATA} from '../../instruction';
|
||||
|
||||
|
||||
export class AsyncRouteHandler implements RouteHandler {
|
||||
/** @internal */
|
||||
_resolvedComponent: Promise<Type> = null;
|
||||
componentType: Type;
|
||||
public data: RouteData;
|
||||
|
||||
constructor(private _loader: () => Promise<Type>, data: {[key: string]: any} = null) {
|
||||
this.data = isPresent(data) ? new RouteData(data) : BLANK_ROUTE_DATA;
|
||||
}
|
||||
|
||||
resolveComponentType(): Promise<Type> {
|
||||
if (isPresent(this._resolvedComponent)) {
|
||||
return this._resolvedComponent;
|
||||
}
|
||||
|
||||
return this._resolvedComponent = this._loader().then((componentType) => {
|
||||
this.componentType = componentType;
|
||||
return componentType;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import {Type} from '../../../src/facade/lang';
|
||||
import {RouteData} from '../../instruction';
|
||||
|
||||
export interface RouteHandler {
|
||||
componentType: Type;
|
||||
resolveComponentType(): Promise<any>;
|
||||
data: RouteData;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import {PromiseWrapper} from '../../../src/facade/async';
|
||||
import {isPresent, Type} from '../../../src/facade/lang';
|
||||
import {RouteHandler} from './route_handler';
|
||||
import {RouteData, BLANK_ROUTE_DATA} from '../../instruction';
|
||||
|
||||
|
||||
export class SyncRouteHandler implements RouteHandler {
|
||||
public data: RouteData;
|
||||
|
||||
/** @internal */
|
||||
_resolvedComponent: Promise<any> = null;
|
||||
|
||||
constructor(public componentType: Type, data?: {[key: string]: any}) {
|
||||
this._resolvedComponent = PromiseWrapper.resolve(componentType);
|
||||
this.data = isPresent(data) ? new RouteData(data) : BLANK_ROUTE_DATA;
|
||||
}
|
||||
|
||||
resolveComponentType(): Promise<any> { return this._resolvedComponent; }
|
||||
}
|
||||
@@ -1,309 +0,0 @@
|
||||
import {RegExpWrapper, StringWrapper, isPresent, isBlank} from '../../../src/facade/lang';
|
||||
import {BaseException} from '../../../src/facade/exceptions';
|
||||
import {StringMapWrapper} from '../../../src/facade/collection';
|
||||
import {TouchMap, normalizeString} from '../../utils';
|
||||
import {Url, RootUrl, convertUrlParamsToArray} from '../../url_parser';
|
||||
import {RoutePath, GeneratedUrl, MatchedUrl} from './route_path';
|
||||
|
||||
|
||||
/**
|
||||
* `ParamRoutePath`s are made up of `PathSegment`s, each of which can
|
||||
* match a segment of a URL. Different kind of `PathSegment`s match
|
||||
* URL segments in different ways...
|
||||
*/
|
||||
interface PathSegment {
|
||||
name: string;
|
||||
generate(params: TouchMap): string;
|
||||
match(path: string): boolean;
|
||||
specificity: string;
|
||||
hash: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Identified by a `...` URL segment. This indicates that the
|
||||
* Route will continue to be matched by child `Router`s.
|
||||
*/
|
||||
class ContinuationPathSegment implements PathSegment {
|
||||
name: string = '';
|
||||
specificity = '';
|
||||
hash = '...';
|
||||
generate(params: TouchMap): string { return ''; }
|
||||
match(path: string): boolean { return true; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Identified by a string not starting with a `:` or `*`.
|
||||
* Only matches the URL segments that equal the segment path
|
||||
*/
|
||||
class StaticPathSegment implements PathSegment {
|
||||
name: string = '';
|
||||
specificity = '2';
|
||||
hash: string;
|
||||
constructor(public path: string) { this.hash = path; }
|
||||
match(path: string): boolean { return path == this.path; }
|
||||
generate(params: TouchMap): string { return this.path; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Identified by a string starting with `:`. Indicates a segment
|
||||
* that can contain a value that will be extracted and provided to
|
||||
* a matching `Instruction`.
|
||||
*/
|
||||
class DynamicPathSegment implements PathSegment {
|
||||
static paramMatcher = /^:([^\/]+)$/g;
|
||||
specificity = '1';
|
||||
hash = ':';
|
||||
constructor(public name: string) {}
|
||||
match(path: string): boolean { return path.length > 0; }
|
||||
generate(params: TouchMap): string {
|
||||
if (!StringMapWrapper.contains(params.map, this.name)) {
|
||||
throw new BaseException(
|
||||
`Route generator for '${this.name}' was not included in parameters passed.`);
|
||||
}
|
||||
return encodeDynamicSegment(normalizeString(params.get(this.name)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Identified by a string starting with `*` Indicates that all the following
|
||||
* segments match this route and that the value of these segments should
|
||||
* be provided to a matching `Instruction`.
|
||||
*/
|
||||
class StarPathSegment implements PathSegment {
|
||||
static wildcardMatcher = /^\*([^\/]+)$/g;
|
||||
specificity = '0';
|
||||
hash = '*';
|
||||
constructor(public name: string) {}
|
||||
match(path: string): boolean { return true; }
|
||||
generate(params: TouchMap): string { return normalizeString(params.get(this.name)); }
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a URL string using a given matcher DSL, and generates URLs from param maps
|
||||
*/
|
||||
export class ParamRoutePath implements RoutePath {
|
||||
specificity: string;
|
||||
terminal: boolean = true;
|
||||
hash: string;
|
||||
|
||||
private _segments: PathSegment[];
|
||||
|
||||
/**
|
||||
* Takes a string representing the matcher DSL
|
||||
*/
|
||||
constructor(public routePath: string) {
|
||||
this._assertValidPath(routePath);
|
||||
|
||||
this._parsePathString(routePath);
|
||||
this.specificity = this._calculateSpecificity();
|
||||
this.hash = this._calculateHash();
|
||||
|
||||
var lastSegment = this._segments[this._segments.length - 1];
|
||||
this.terminal = !(lastSegment instanceof ContinuationPathSegment);
|
||||
}
|
||||
|
||||
matchUrl(url: Url): MatchedUrl {
|
||||
var nextUrlSegment = url;
|
||||
var currentUrlSegment: Url;
|
||||
var positionalParams = {};
|
||||
var captured: string[] = [];
|
||||
|
||||
for (var i = 0; i < this._segments.length; i += 1) {
|
||||
var pathSegment = this._segments[i];
|
||||
|
||||
currentUrlSegment = nextUrlSegment;
|
||||
if (pathSegment instanceof ContinuationPathSegment) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (isPresent(currentUrlSegment)) {
|
||||
// the star segment consumes all of the remaining URL, including matrix params
|
||||
if (pathSegment instanceof StarPathSegment) {
|
||||
positionalParams[pathSegment.name] = currentUrlSegment.toString();
|
||||
captured.push(currentUrlSegment.toString());
|
||||
nextUrlSegment = null;
|
||||
break;
|
||||
}
|
||||
|
||||
captured.push(currentUrlSegment.path);
|
||||
|
||||
if (pathSegment instanceof DynamicPathSegment) {
|
||||
positionalParams[pathSegment.name] = decodeDynamicSegment(currentUrlSegment.path);
|
||||
} else if (!pathSegment.match(currentUrlSegment.path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
nextUrlSegment = currentUrlSegment.child;
|
||||
} else if (!pathSegment.match('')) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.terminal && isPresent(nextUrlSegment)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var urlPath = captured.join('/');
|
||||
|
||||
var auxiliary = [];
|
||||
var urlParams = [];
|
||||
var allParams = positionalParams;
|
||||
if (isPresent(currentUrlSegment)) {
|
||||
// If this is the root component, read query params. Otherwise, read matrix params.
|
||||
var paramsSegment = url instanceof RootUrl ? url : currentUrlSegment;
|
||||
|
||||
if (isPresent(paramsSegment.params)) {
|
||||
allParams = StringMapWrapper.merge(paramsSegment.params, positionalParams);
|
||||
urlParams = convertUrlParamsToArray(paramsSegment.params);
|
||||
} else {
|
||||
allParams = positionalParams;
|
||||
}
|
||||
auxiliary = currentUrlSegment.auxiliary;
|
||||
}
|
||||
|
||||
return new MatchedUrl(urlPath, urlParams, allParams, auxiliary, nextUrlSegment);
|
||||
}
|
||||
|
||||
|
||||
generateUrl(params: {[key: string]: any}): GeneratedUrl {
|
||||
var paramTokens = new TouchMap(params);
|
||||
|
||||
var path = [];
|
||||
|
||||
for (var i = 0; i < this._segments.length; i++) {
|
||||
let segment = this._segments[i];
|
||||
if (!(segment instanceof ContinuationPathSegment)) {
|
||||
path.push(segment.generate(paramTokens));
|
||||
}
|
||||
}
|
||||
var urlPath = path.join('/');
|
||||
|
||||
var nonPositionalParams = paramTokens.getUnused();
|
||||
var urlParams = nonPositionalParams;
|
||||
|
||||
return new GeneratedUrl(urlPath, urlParams);
|
||||
}
|
||||
|
||||
|
||||
toString(): string { return this.routePath; }
|
||||
|
||||
private _parsePathString(routePath: string) {
|
||||
// normalize route as not starting with a "/". Recognition will
|
||||
// also normalize.
|
||||
if (routePath.startsWith("/")) {
|
||||
routePath = routePath.substring(1);
|
||||
}
|
||||
|
||||
var segmentStrings = routePath.split('/');
|
||||
this._segments = [];
|
||||
|
||||
var limit = segmentStrings.length - 1;
|
||||
for (var i = 0; i <= limit; i++) {
|
||||
var segment = segmentStrings[i], match;
|
||||
|
||||
if (isPresent(match = RegExpWrapper.firstMatch(DynamicPathSegment.paramMatcher, segment))) {
|
||||
this._segments.push(new DynamicPathSegment(match[1]));
|
||||
} else if (isPresent(
|
||||
match = RegExpWrapper.firstMatch(StarPathSegment.wildcardMatcher, segment))) {
|
||||
this._segments.push(new StarPathSegment(match[1]));
|
||||
} else if (segment == '...') {
|
||||
if (i < limit) {
|
||||
throw new BaseException(
|
||||
`Unexpected "..." before the end of the path for "${routePath}".`);
|
||||
}
|
||||
this._segments.push(new ContinuationPathSegment());
|
||||
} else {
|
||||
this._segments.push(new StaticPathSegment(segment));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _calculateSpecificity(): string {
|
||||
// The "specificity" of a path is used to determine which route is used when multiple routes
|
||||
// match
|
||||
// a URL. Static segments (like "/foo") are the most specific, followed by dynamic segments
|
||||
// (like
|
||||
// "/:id"). Star segments add no specificity. Segments at the start of the path are more
|
||||
// specific
|
||||
// than proceeding ones.
|
||||
//
|
||||
// The code below uses place values to combine the different types of segments into a single
|
||||
// string that we can sort later. Each static segment is marked as a specificity of "2," each
|
||||
// dynamic segment is worth "1" specificity, and stars are worth "0" specificity.
|
||||
var i, length = this._segments.length, specificity;
|
||||
if (length == 0) {
|
||||
// a single slash (or "empty segment" is as specific as a static segment
|
||||
specificity += '2';
|
||||
} else {
|
||||
specificity = '';
|
||||
for (i = 0; i < length; i++) {
|
||||
specificity += this._segments[i].specificity;
|
||||
}
|
||||
}
|
||||
return specificity;
|
||||
}
|
||||
|
||||
private _calculateHash(): string {
|
||||
// this function is used to determine whether a route config path like `/foo/:id` collides with
|
||||
// `/foo/:name`
|
||||
var i, length = this._segments.length;
|
||||
var hashParts = [];
|
||||
for (i = 0; i < length; i++) {
|
||||
hashParts.push(this._segments[i].hash);
|
||||
}
|
||||
return hashParts.join('/');
|
||||
}
|
||||
|
||||
private _assertValidPath(path: string) {
|
||||
if (StringWrapper.contains(path, '#')) {
|
||||
throw new BaseException(
|
||||
`Path "${path}" should not include "#". Use "HashLocationStrategy" instead.`);
|
||||
}
|
||||
var illegalCharacter = RegExpWrapper.firstMatch(ParamRoutePath.RESERVED_CHARS, path);
|
||||
if (isPresent(illegalCharacter)) {
|
||||
throw new BaseException(
|
||||
`Path "${path}" contains "${illegalCharacter[0]}" which is not allowed in a route config.`);
|
||||
}
|
||||
}
|
||||
static RESERVED_CHARS = RegExpWrapper.create('//|\\(|\\)|;|\\?|=');
|
||||
}
|
||||
|
||||
let REGEXP_PERCENT = /%/g;
|
||||
let REGEXP_SLASH = /\//g;
|
||||
let REGEXP_OPEN_PARENT = /\(/g;
|
||||
let REGEXP_CLOSE_PARENT = /\)/g;
|
||||
let REGEXP_SEMICOLON = /;/g;
|
||||
|
||||
function encodeDynamicSegment(value: string): string {
|
||||
if (isBlank(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
value = StringWrapper.replaceAll(value, REGEXP_PERCENT, '%25');
|
||||
value = StringWrapper.replaceAll(value, REGEXP_SLASH, '%2F');
|
||||
value = StringWrapper.replaceAll(value, REGEXP_OPEN_PARENT, '%28');
|
||||
value = StringWrapper.replaceAll(value, REGEXP_CLOSE_PARENT, '%29');
|
||||
value = StringWrapper.replaceAll(value, REGEXP_SEMICOLON, '%3B');
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
let REGEXP_ENC_SEMICOLON = /%3B/ig;
|
||||
let REGEXP_ENC_CLOSE_PARENT = /%29/ig;
|
||||
let REGEXP_ENC_OPEN_PARENT = /%28/ig;
|
||||
let REGEXP_ENC_SLASH = /%2F/ig;
|
||||
let REGEXP_ENC_PERCENT = /%25/ig;
|
||||
|
||||
function decodeDynamicSegment(value: string): string {
|
||||
if (isBlank(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
value = StringWrapper.replaceAll(value, REGEXP_ENC_SEMICOLON, ';');
|
||||
value = StringWrapper.replaceAll(value, REGEXP_ENC_CLOSE_PARENT, ')');
|
||||
value = StringWrapper.replaceAll(value, REGEXP_ENC_OPEN_PARENT, '(');
|
||||
value = StringWrapper.replaceAll(value, REGEXP_ENC_SLASH, '/');
|
||||
value = StringWrapper.replaceAll(value, REGEXP_ENC_PERCENT, '%');
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import {RegExpWrapper, RegExpMatcherWrapper, isBlank} from '../../../src/facade/lang';
|
||||
import {Url} from '../../url_parser';
|
||||
import {RoutePath, GeneratedUrl, MatchedUrl} from './route_path';
|
||||
|
||||
|
||||
export interface RegexSerializer { (params: {[key: string]: any}): GeneratedUrl; }
|
||||
|
||||
export class RegexRoutePath implements RoutePath {
|
||||
public hash: string;
|
||||
public terminal: boolean = true;
|
||||
public specificity: string = '2';
|
||||
|
||||
private _regex: RegExp;
|
||||
|
||||
constructor(private _reString: string, private _serializer: RegexSerializer) {
|
||||
this.hash = this._reString;
|
||||
this._regex = RegExpWrapper.create(this._reString);
|
||||
}
|
||||
|
||||
matchUrl(url: Url): MatchedUrl {
|
||||
var urlPath = url.toString();
|
||||
var params: {[key: string]: string} = {};
|
||||
var matcher = RegExpWrapper.matcher(this._regex, urlPath);
|
||||
var match = RegExpMatcherWrapper.next(matcher);
|
||||
|
||||
if (isBlank(match)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (let i = 0; i < match.length; i += 1) {
|
||||
params[i.toString()] = match[i];
|
||||
}
|
||||
|
||||
return new MatchedUrl(urlPath, [], params, [], null);
|
||||
}
|
||||
|
||||
generateUrl(params: {[key: string]: any}): GeneratedUrl { return this._serializer(params); }
|
||||
|
||||
toString(): string { return this._reString; }
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
import {Url} from '../../url_parser';
|
||||
|
||||
export class MatchedUrl {
|
||||
constructor(public urlPath: string, public urlParams: string[],
|
||||
public allParams: {[key: string]: any}, public auxiliary: Url[], public rest: Url) {}
|
||||
}
|
||||
|
||||
|
||||
export class GeneratedUrl {
|
||||
constructor(public urlPath: string, public urlParams: {[key: string]: any}) {}
|
||||
}
|
||||
|
||||
export interface RoutePath {
|
||||
specificity: string;
|
||||
terminal: boolean;
|
||||
hash: string;
|
||||
matchUrl(url: Url): MatchedUrl;
|
||||
generateUrl(params: {[key: string]: any}): GeneratedUrl;
|
||||
toString(): string;
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
import {isBlank, isPresent, isFunction} from '../../src/facade/lang';
|
||||
import {BaseException} from '../../src/facade/exceptions';
|
||||
import {Map} from '../../src/facade/collection';
|
||||
import {PromiseWrapper} from '../../src/facade/async';
|
||||
import {AbstractRule, RouteRule, RedirectRule, RouteMatch, PathMatch} from './rules';
|
||||
import {
|
||||
Route,
|
||||
AsyncRoute,
|
||||
AuxRoute,
|
||||
Redirect,
|
||||
RouteDefinition
|
||||
} from '../route_config/route_config_impl';
|
||||
import {AsyncRouteHandler} from './route_handlers/async_route_handler';
|
||||
import {SyncRouteHandler} from './route_handlers/sync_route_handler';
|
||||
import {RoutePath} from './route_paths/route_path';
|
||||
import {ParamRoutePath} from './route_paths/param_route_path';
|
||||
import {RegexRoutePath} from './route_paths/regex_route_path';
|
||||
import {Url} from '../url_parser';
|
||||
import {ComponentInstruction} from '../instruction';
|
||||
|
||||
|
||||
/**
|
||||
* A `RuleSet` is responsible for recognizing routes for a particular component.
|
||||
* It is consumed by `RouteRegistry`, which knows how to recognize an entire hierarchy of
|
||||
* components.
|
||||
*/
|
||||
export class RuleSet {
|
||||
rulesByName = new Map<string, RouteRule>();
|
||||
|
||||
// map from name to rule
|
||||
auxRulesByName = new Map<string, RouteRule>();
|
||||
|
||||
// map from starting path to rule
|
||||
auxRulesByPath = new Map<string, RouteRule>();
|
||||
|
||||
// TODO: optimize this into a trie
|
||||
rules: AbstractRule[] = [];
|
||||
|
||||
// the rule to use automatically when recognizing or generating from this rule set
|
||||
defaultRule: RouteRule = null;
|
||||
|
||||
/**
|
||||
* Configure additional rules in this rule set from a route definition
|
||||
* @returns {boolean} true if the config is terminal
|
||||
*/
|
||||
config(config: RouteDefinition): boolean {
|
||||
let handler;
|
||||
|
||||
if (isPresent(config.name) && config.name[0].toUpperCase() != config.name[0]) {
|
||||
let suggestedName = config.name[0].toUpperCase() + config.name.substring(1);
|
||||
throw new BaseException(
|
||||
`Route "${config.path}" with name "${config.name}" does not begin with an uppercase letter. Route names should be CamelCase like "${suggestedName}".`);
|
||||
}
|
||||
|
||||
if (config instanceof AuxRoute) {
|
||||
handler = new SyncRouteHandler(config.component, config.data);
|
||||
let routePath = this._getRoutePath(config);
|
||||
let auxRule = new RouteRule(routePath, handler, config.name);
|
||||
this.auxRulesByPath.set(routePath.toString(), auxRule);
|
||||
if (isPresent(config.name)) {
|
||||
this.auxRulesByName.set(config.name, auxRule);
|
||||
}
|
||||
return auxRule.terminal;
|
||||
}
|
||||
|
||||
let useAsDefault = false;
|
||||
|
||||
if (config instanceof Redirect) {
|
||||
let routePath = this._getRoutePath(config);
|
||||
let redirector = new RedirectRule(routePath, config.redirectTo);
|
||||
this._assertNoHashCollision(redirector.hash, config.path);
|
||||
this.rules.push(redirector);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (config instanceof Route) {
|
||||
handler = new SyncRouteHandler(config.component, config.data);
|
||||
useAsDefault = isPresent(config.useAsDefault) && config.useAsDefault;
|
||||
} else if (config instanceof AsyncRoute) {
|
||||
handler = new AsyncRouteHandler(config.loader, config.data);
|
||||
useAsDefault = isPresent(config.useAsDefault) && config.useAsDefault;
|
||||
}
|
||||
let routePath = this._getRoutePath(config);
|
||||
let newRule = new RouteRule(routePath, handler, config.name);
|
||||
|
||||
this._assertNoHashCollision(newRule.hash, config.path);
|
||||
|
||||
if (useAsDefault) {
|
||||
if (isPresent(this.defaultRule)) {
|
||||
throw new BaseException(`Only one route can be default`);
|
||||
}
|
||||
this.defaultRule = newRule;
|
||||
}
|
||||
|
||||
this.rules.push(newRule);
|
||||
if (isPresent(config.name)) {
|
||||
this.rulesByName.set(config.name, newRule);
|
||||
}
|
||||
return newRule.terminal;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Given a URL, returns a list of `RouteMatch`es, which are partial recognitions for some route.
|
||||
*/
|
||||
recognize(urlParse: Url): Promise<RouteMatch>[] {
|
||||
var solutions = [];
|
||||
|
||||
this.rules.forEach((routeRecognizer: AbstractRule) => {
|
||||
var pathMatch = routeRecognizer.recognize(urlParse);
|
||||
|
||||
if (isPresent(pathMatch)) {
|
||||
solutions.push(pathMatch);
|
||||
}
|
||||
});
|
||||
|
||||
// handle cases where we are routing just to an aux route
|
||||
if (solutions.length == 0 && isPresent(urlParse) && urlParse.auxiliary.length > 0) {
|
||||
return [PromiseWrapper.resolve(new PathMatch(null, null, urlParse.auxiliary))];
|
||||
}
|
||||
|
||||
return solutions;
|
||||
}
|
||||
|
||||
recognizeAuxiliary(urlParse: Url): Promise<RouteMatch>[] {
|
||||
var routeRecognizer: RouteRule = this.auxRulesByPath.get(urlParse.path);
|
||||
if (isPresent(routeRecognizer)) {
|
||||
return [routeRecognizer.recognize(urlParse)];
|
||||
}
|
||||
|
||||
return [PromiseWrapper.resolve(null)];
|
||||
}
|
||||
|
||||
hasRoute(name: string): boolean { return this.rulesByName.has(name); }
|
||||
|
||||
componentLoaded(name: string): boolean {
|
||||
return this.hasRoute(name) && isPresent(this.rulesByName.get(name).handler.componentType);
|
||||
}
|
||||
|
||||
loadComponent(name: string): Promise<any> {
|
||||
return this.rulesByName.get(name).handler.resolveComponentType();
|
||||
}
|
||||
|
||||
generate(name: string, params: any): ComponentInstruction {
|
||||
var rule: RouteRule = this.rulesByName.get(name);
|
||||
if (isBlank(rule)) {
|
||||
return null;
|
||||
}
|
||||
return rule.generate(params);
|
||||
}
|
||||
|
||||
generateAuxiliary(name: string, params: any): ComponentInstruction {
|
||||
var rule: RouteRule = this.auxRulesByName.get(name);
|
||||
if (isBlank(rule)) {
|
||||
return null;
|
||||
}
|
||||
return rule.generate(params);
|
||||
}
|
||||
|
||||
private _assertNoHashCollision(hash: string, path) {
|
||||
this.rules.forEach((rule) => {
|
||||
if (hash == rule.hash) {
|
||||
throw new BaseException(
|
||||
`Configuration '${path}' conflicts with existing route '${rule.path}'`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private _getRoutePath(config: RouteDefinition): RoutePath {
|
||||
if (isPresent(config.regex)) {
|
||||
if (isFunction(config.serializer)) {
|
||||
return new RegexRoutePath(config.regex, config.serializer);
|
||||
} else {
|
||||
throw new BaseException(
|
||||
`Route provides a regex property, '${config.regex}', but no serializer property`);
|
||||
}
|
||||
}
|
||||
if (isPresent(config.path)) {
|
||||
// Auxiliary routes do not have a slash at the start
|
||||
let path = (config instanceof AuxRoute && config.path.startsWith('/')) ?
|
||||
config.path.substring(1) :
|
||||
config.path;
|
||||
return new ParamRoutePath(path);
|
||||
}
|
||||
throw new BaseException('Route must provide either a path or regex property');
|
||||
}
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
import {isPresent, isBlank} from '../../src/facade/lang';
|
||||
import {BaseException} from '../../src/facade/exceptions';
|
||||
import {PromiseWrapper} from '../../src/facade/promise';
|
||||
import {Map} from '../../src/facade/collection';
|
||||
import {RouteHandler} from './route_handlers/route_handler';
|
||||
import {Url, convertUrlParamsToArray} from '../url_parser';
|
||||
import {ComponentInstruction} from '../instruction';
|
||||
import {RoutePath, GeneratedUrl} from './route_paths/route_path';
|
||||
|
||||
|
||||
// RouteMatch objects hold information about a match between a rule and a URL
|
||||
export abstract class RouteMatch {}
|
||||
|
||||
export class PathMatch extends RouteMatch {
|
||||
constructor(public instruction: ComponentInstruction, public remaining: Url,
|
||||
public remainingAux: Url[]) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
export class RedirectMatch extends RouteMatch {
|
||||
constructor(public redirectTo: any[], public specificity) { super(); }
|
||||
}
|
||||
|
||||
// Rules are responsible for recognizing URL segments and generating instructions
|
||||
export interface AbstractRule {
|
||||
hash: string;
|
||||
path: string;
|
||||
recognize(beginningSegment: Url): Promise<RouteMatch>;
|
||||
generate(params: {[key: string]: any}): ComponentInstruction;
|
||||
}
|
||||
|
||||
export class RedirectRule implements AbstractRule {
|
||||
public hash: string;
|
||||
|
||||
constructor(private _pathRecognizer: RoutePath, public redirectTo: any[]) {
|
||||
this.hash = this._pathRecognizer.hash;
|
||||
}
|
||||
|
||||
get path() { return this._pathRecognizer.toString(); }
|
||||
set path(val) { throw new BaseException('you cannot set the path of a RedirectRule directly'); }
|
||||
|
||||
/**
|
||||
* Returns `null` or a `ParsedUrl` representing the new path to match
|
||||
*/
|
||||
recognize(beginningSegment: Url): Promise<RouteMatch> {
|
||||
var match = null;
|
||||
if (isPresent(this._pathRecognizer.matchUrl(beginningSegment))) {
|
||||
match = new RedirectMatch(this.redirectTo, this._pathRecognizer.specificity);
|
||||
}
|
||||
return PromiseWrapper.resolve(match);
|
||||
}
|
||||
|
||||
generate(params: {[key: string]: any}): ComponentInstruction {
|
||||
throw new BaseException(`Tried to generate a redirect.`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// represents something like '/foo/:bar'
|
||||
export class RouteRule implements AbstractRule {
|
||||
specificity: string;
|
||||
terminal: boolean;
|
||||
hash: string;
|
||||
|
||||
private _cache: Map<string, ComponentInstruction> = new Map<string, ComponentInstruction>();
|
||||
|
||||
// TODO: cache component instruction instances by params and by ParsedUrl instance
|
||||
|
||||
constructor(private _routePath: RoutePath, public handler: RouteHandler,
|
||||
private _routeName: string) {
|
||||
this.specificity = this._routePath.specificity;
|
||||
this.hash = this._routePath.hash;
|
||||
this.terminal = this._routePath.terminal;
|
||||
}
|
||||
|
||||
get path() { return this._routePath.toString(); }
|
||||
set path(val) { throw new BaseException('you cannot set the path of a RouteRule directly'); }
|
||||
|
||||
recognize(beginningSegment: Url): Promise<RouteMatch> {
|
||||
var res = this._routePath.matchUrl(beginningSegment);
|
||||
if (isBlank(res)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.handler.resolveComponentType().then((_) => {
|
||||
var componentInstruction = this._getInstruction(res.urlPath, res.urlParams, res.allParams);
|
||||
return new PathMatch(componentInstruction, res.rest, res.auxiliary);
|
||||
});
|
||||
}
|
||||
|
||||
generate(params: {[key: string]: any}): ComponentInstruction {
|
||||
var generated = this._routePath.generateUrl(params);
|
||||
var urlPath = generated.urlPath;
|
||||
var urlParams = generated.urlParams;
|
||||
return this._getInstruction(urlPath, convertUrlParamsToArray(urlParams), params);
|
||||
}
|
||||
|
||||
generateComponentPathValues(params: {[key: string]: any}): GeneratedUrl {
|
||||
return this._routePath.generateUrl(params);
|
||||
}
|
||||
|
||||
private _getInstruction(urlPath: string, urlParams: string[],
|
||||
params: {[key: string]: any}): ComponentInstruction {
|
||||
if (isBlank(this.handler.componentType)) {
|
||||
throw new BaseException(`Tried to get instruction before the type was loaded.`);
|
||||
}
|
||||
var hashKey = urlPath + '?' + urlParams.join('&');
|
||||
if (this._cache.has(hashKey)) {
|
||||
return this._cache.get(hashKey);
|
||||
}
|
||||
var instruction =
|
||||
new ComponentInstruction(urlPath, urlParams, this.handler.data, this.handler.componentType,
|
||||
this.terminal, this.specificity, params, this._routeName);
|
||||
this._cache.set(hashKey, instruction);
|
||||
|
||||
return instruction;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
import {ComponentFactory, Type} from '@angular/core';
|
||||
import {StringMapWrapper, ListWrapper} from '@angular/facade/src/collection';
|
||||
import {isBlank, isPresent, stringify} from '@angular/facade/src/lang';
|
||||
|
||||
export class Tree<T> {
|
||||
/** @internal */
|
||||
_root: TreeNode<T>;
|
||||
|
||||
constructor(root: TreeNode<T>) { this._root = root; }
|
||||
|
||||
get root(): T { return this._root.value; }
|
||||
|
||||
parent(t: T): T {
|
||||
let p = this.pathFromRoot(t);
|
||||
return p.length > 1 ? p[p.length - 2] : null;
|
||||
}
|
||||
|
||||
children(t: T): T[] {
|
||||
let n = _findNode(t, this._root);
|
||||
return isPresent(n) ? n.children.map(t => t.value) : null;
|
||||
}
|
||||
|
||||
firstChild(t: T): T {
|
||||
let n = _findNode(t, this._root);
|
||||
return isPresent(n) && n.children.length > 0 ? n.children[0].value : null;
|
||||
}
|
||||
|
||||
pathFromRoot(t: T): T[] { return _findPath(t, this._root, []).map(s => s.value); }
|
||||
|
||||
contains(tree: Tree<T>): boolean { return _contains(this._root, tree._root); }
|
||||
}
|
||||
|
||||
export class UrlTree extends Tree<UrlSegment> {
|
||||
constructor(root: TreeNode<UrlSegment>) { super(root); }
|
||||
}
|
||||
|
||||
export class RouteTree extends Tree<RouteSegment> {
|
||||
constructor(root: TreeNode<RouteSegment>) { super(root); }
|
||||
}
|
||||
|
||||
export function rootNode<T>(tree: Tree<T>): TreeNode<T> {
|
||||
return tree._root;
|
||||
}
|
||||
|
||||
function _findNode<T>(expected: T, c: TreeNode<T>): TreeNode<T> {
|
||||
// TODO: vsavkin remove it once recognize is fixed
|
||||
if (expected instanceof RouteSegment && equalSegments(<any>expected, <any>c.value)) return c;
|
||||
if (expected === c.value) return c;
|
||||
for (let cc of c.children) {
|
||||
let r = _findNode(expected, cc);
|
||||
if (isPresent(r)) return r;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function _findPath<T>(expected: T, c: TreeNode<T>, collected: TreeNode<T>[]): TreeNode<T>[] {
|
||||
collected.push(c);
|
||||
|
||||
// TODO: vsavkin remove it once recognize is fixed
|
||||
if (_equalValues(expected, c.value)) return collected;
|
||||
|
||||
for (let cc of c.children) {
|
||||
let r = _findPath(expected, cc, ListWrapper.clone(collected));
|
||||
if (isPresent(r)) return r;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function _contains<T>(tree: TreeNode<T>, subtree: TreeNode<T>): boolean {
|
||||
if (!_equalValues(tree.value, subtree.value)) return false;
|
||||
|
||||
for (let subtreeNode of subtree.children) {
|
||||
let s = tree.children.filter(child => _equalValues(child.value, subtreeNode.value));
|
||||
if (s.length === 0) return false;
|
||||
if (!_contains(s[0], subtreeNode)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function _equalValues(a: any, b: any): boolean {
|
||||
if (a instanceof RouteSegment) return equalSegments(<any>a, <any>b);
|
||||
if (a instanceof UrlSegment) return equalUrlSegments(<any>a, <any>b);
|
||||
return a === b;
|
||||
}
|
||||
|
||||
export class TreeNode<T> {
|
||||
constructor(public value: T, public children: TreeNode<T>[]) {}
|
||||
}
|
||||
|
||||
export class UrlSegment {
|
||||
constructor(public segment: any, public parameters: {[key: string]: string},
|
||||
public outlet: string) {}
|
||||
|
||||
toString(): string {
|
||||
let outletPrefix = isBlank(this.outlet) ? "" : `${this.outlet}:`;
|
||||
let segmentPrefix = isBlank(this.segment) ? "" : this.segment;
|
||||
return `${outletPrefix}${segmentPrefix}${_serializeParams(this.parameters)}`;
|
||||
}
|
||||
}
|
||||
|
||||
function _serializeParams(params: {[key: string]: string}): string {
|
||||
let res = "";
|
||||
if (isPresent(params)) {
|
||||
StringMapWrapper.forEach(params, (v, k) => res += `;${k}=${v}`);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
export class RouteSegment {
|
||||
/** @internal */
|
||||
_type: Type;
|
||||
|
||||
/** @internal */
|
||||
_componentFactory: ComponentFactory<any>;
|
||||
|
||||
constructor(public urlSegments: UrlSegment[], public parameters: {[key: string]: string},
|
||||
public outlet: string, type: Type, componentFactory: ComponentFactory<any>) {
|
||||
this._type = type;
|
||||
this._componentFactory = componentFactory;
|
||||
}
|
||||
|
||||
getParam(param: string): string {
|
||||
return isPresent(this.parameters) ? this.parameters[param] : null;
|
||||
}
|
||||
|
||||
get type(): Type { return this._type; }
|
||||
|
||||
get stringifiedUrlSegments(): string { return this.urlSegments.map(s => s.toString()).join("/"); }
|
||||
}
|
||||
|
||||
export function serializeRouteSegmentTree(tree: RouteTree): string {
|
||||
return _serializeRouteSegmentTree(tree._root);
|
||||
}
|
||||
|
||||
function _serializeRouteSegmentTree(node: TreeNode<RouteSegment>): string {
|
||||
let v = node.value;
|
||||
let children = node.children.map(c => _serializeRouteSegmentTree(c)).join(", ");
|
||||
return `${v.outlet}:${v.stringifiedUrlSegments}(${stringify(v.type)}) [${children}]`;
|
||||
}
|
||||
|
||||
export function equalSegments(a: RouteSegment, b: RouteSegment): boolean {
|
||||
if (isBlank(a) && !isBlank(b)) return false;
|
||||
if (!isBlank(a) && isBlank(b)) return false;
|
||||
if (a._type !== b._type) return false;
|
||||
if (a.outlet != b.outlet) return false;
|
||||
if (isBlank(a.parameters) && !isBlank(b.parameters)) return false;
|
||||
if (!isBlank(a.parameters) && isBlank(b.parameters)) return false;
|
||||
if (isBlank(a.parameters) && isBlank(b.parameters)) return true;
|
||||
return StringMapWrapper.equals(a.parameters, b.parameters);
|
||||
}
|
||||
|
||||
export function equalUrlSegments(a: UrlSegment, b: UrlSegment): boolean {
|
||||
if (isBlank(a) && !isBlank(b)) return false;
|
||||
if (!isBlank(a) && isBlank(b)) return false;
|
||||
if (a.segment != b.segment) return false;
|
||||
if (a.outlet != b.outlet) return false;
|
||||
if (isBlank(a.parameters) && !isBlank(b.parameters)) return false;
|
||||
if (!isBlank(a.parameters) && isBlank(b.parameters)) return false;
|
||||
if (isBlank(a.parameters) && isBlank(b.parameters)) return true;
|
||||
return StringMapWrapper.equals(a.parameters, b.parameters);
|
||||
}
|
||||
|
||||
export function routeSegmentComponentFactory(a: RouteSegment): ComponentFactory<any> {
|
||||
return a._componentFactory;
|
||||
}
|
||||
@@ -1,242 +0,0 @@
|
||||
import {StringMapWrapper} from '../src/facade/collection';
|
||||
import {isPresent, isBlank, RegExpWrapper} from '../src/facade/lang';
|
||||
import {BaseException} from '../src/facade/exceptions';
|
||||
|
||||
export function convertUrlParamsToArray(urlParams: {[key: string]: any}): string[] {
|
||||
var paramsArray = [];
|
||||
if (isBlank(urlParams)) {
|
||||
return [];
|
||||
}
|
||||
StringMapWrapper.forEach(
|
||||
urlParams, (value, key) => { paramsArray.push((value === true) ? key : key + '=' + value); });
|
||||
return paramsArray;
|
||||
}
|
||||
|
||||
// Convert an object of url parameters into a string that can be used in an URL
|
||||
export function serializeParams(urlParams: {[key: string]: any}, joiner = '&'): string {
|
||||
return convertUrlParamsToArray(urlParams).join(joiner);
|
||||
}
|
||||
|
||||
/**
|
||||
* This class represents a parsed URL
|
||||
*/
|
||||
export class Url {
|
||||
constructor(public path: string, public child: Url = null,
|
||||
public auxiliary: Url[] = /*@ts2dart_const*/[],
|
||||
public params: {[key: string]: any} = /*@ts2dart_const*/ {}) {}
|
||||
|
||||
toString(): string {
|
||||
return this.path + this._matrixParamsToString() + this._auxToString() + this._childString();
|
||||
}
|
||||
|
||||
segmentToString(): string { return this.path + this._matrixParamsToString(); }
|
||||
|
||||
/** @internal */
|
||||
_auxToString(): string {
|
||||
return this.auxiliary.length > 0 ?
|
||||
('(' + this.auxiliary.map(sibling => sibling.toString()).join('//') + ')') :
|
||||
'';
|
||||
}
|
||||
|
||||
private _matrixParamsToString(): string {
|
||||
var paramString = serializeParams(this.params, ';');
|
||||
if (paramString.length > 0) {
|
||||
return ';' + paramString;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_childString(): string { return isPresent(this.child) ? ('/' + this.child.toString()) : ''; }
|
||||
}
|
||||
|
||||
export class RootUrl extends Url {
|
||||
constructor(path: string, child: Url = null, auxiliary: Url[] = /*@ts2dart_const*/[],
|
||||
params: {[key: string]: any} = null) {
|
||||
super(path, child, auxiliary, params);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.path + this._auxToString() + this._childString() + this._queryParamsToString();
|
||||
}
|
||||
|
||||
segmentToString(): string { return this.path + this._queryParamsToString(); }
|
||||
|
||||
private _queryParamsToString(): string {
|
||||
if (isBlank(this.params)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return '?' + serializeParams(this.params);
|
||||
}
|
||||
}
|
||||
|
||||
export function pathSegmentsToUrl(pathSegments: string[]): Url {
|
||||
var url = new Url(pathSegments[pathSegments.length - 1]);
|
||||
for (var i = pathSegments.length - 2; i >= 0; i -= 1) {
|
||||
url = new Url(pathSegments[i], url);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
var SEGMENT_RE = RegExpWrapper.create('^[^\\/\\(\\)\\?;=&#]+');
|
||||
function matchUrlSegment(str: string): string {
|
||||
var match = RegExpWrapper.firstMatch(SEGMENT_RE, str);
|
||||
return isPresent(match) ? match[0] : '';
|
||||
}
|
||||
var QUERY_PARAM_VALUE_RE = RegExpWrapper.create('^[^\\(\\)\\?;&#]+');
|
||||
function matchUrlQueryParamValue(str: string): string {
|
||||
var match = RegExpWrapper.firstMatch(QUERY_PARAM_VALUE_RE, str);
|
||||
return isPresent(match) ? match[0] : '';
|
||||
}
|
||||
|
||||
export class UrlParser {
|
||||
private _remaining: string;
|
||||
|
||||
peekStartsWith(str: string): boolean { return this._remaining.startsWith(str); }
|
||||
|
||||
capture(str: string): void {
|
||||
if (!this._remaining.startsWith(str)) {
|
||||
throw new BaseException(`Expected "${str}".`);
|
||||
}
|
||||
this._remaining = this._remaining.substring(str.length);
|
||||
}
|
||||
|
||||
parse(url: string): Url {
|
||||
this._remaining = url;
|
||||
if (url == '' || url == '/') {
|
||||
return new Url('');
|
||||
}
|
||||
return this.parseRoot();
|
||||
}
|
||||
|
||||
// segment + (aux segments) + (query params)
|
||||
parseRoot(): RootUrl {
|
||||
if (this.peekStartsWith('/')) {
|
||||
this.capture('/');
|
||||
}
|
||||
var path = matchUrlSegment(this._remaining);
|
||||
this.capture(path);
|
||||
|
||||
var aux: Url[] = [];
|
||||
if (this.peekStartsWith('(')) {
|
||||
aux = this.parseAuxiliaryRoutes();
|
||||
}
|
||||
if (this.peekStartsWith(';')) {
|
||||
// TODO: should these params just be dropped?
|
||||
this.parseMatrixParams();
|
||||
}
|
||||
var child = null;
|
||||
if (this.peekStartsWith('/') && !this.peekStartsWith('//')) {
|
||||
this.capture('/');
|
||||
child = this.parseSegment();
|
||||
}
|
||||
var queryParams: {[key: string]: any} = null;
|
||||
if (this.peekStartsWith('?')) {
|
||||
queryParams = this.parseQueryParams();
|
||||
}
|
||||
return new RootUrl(path, child, aux, queryParams);
|
||||
}
|
||||
|
||||
// segment + (matrix params) + (aux segments)
|
||||
parseSegment(): Url {
|
||||
if (this._remaining.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (this.peekStartsWith('/')) {
|
||||
this.capture('/');
|
||||
}
|
||||
var path = matchUrlSegment(this._remaining);
|
||||
this.capture(path);
|
||||
|
||||
var matrixParams: {[key: string]: any} = null;
|
||||
if (this.peekStartsWith(';')) {
|
||||
matrixParams = this.parseMatrixParams();
|
||||
}
|
||||
var aux: Url[] = [];
|
||||
if (this.peekStartsWith('(')) {
|
||||
aux = this.parseAuxiliaryRoutes();
|
||||
}
|
||||
var child: Url = null;
|
||||
if (this.peekStartsWith('/') && !this.peekStartsWith('//')) {
|
||||
this.capture('/');
|
||||
child = this.parseSegment();
|
||||
}
|
||||
return new Url(path, child, aux, matrixParams);
|
||||
}
|
||||
|
||||
parseQueryParams(): {[key: string]: any} {
|
||||
var params: {[key: string]: any} = {};
|
||||
this.capture('?');
|
||||
this.parseQueryParam(params);
|
||||
while (this._remaining.length > 0 && this.peekStartsWith('&')) {
|
||||
this.capture('&');
|
||||
this.parseQueryParam(params);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
parseMatrixParams(): {[key: string]: any} {
|
||||
var params: {[key: string]: any} = {};
|
||||
while (this._remaining.length > 0 && this.peekStartsWith(';')) {
|
||||
this.capture(';');
|
||||
this.parseParam(params);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
parseParam(params: {[key: string]: any}): void {
|
||||
var key = matchUrlSegment(this._remaining);
|
||||
if (isBlank(key)) {
|
||||
return;
|
||||
}
|
||||
this.capture(key);
|
||||
var value: any = true;
|
||||
if (this.peekStartsWith('=')) {
|
||||
this.capture('=');
|
||||
var valueMatch = matchUrlSegment(this._remaining);
|
||||
if (isPresent(valueMatch)) {
|
||||
value = valueMatch;
|
||||
this.capture(value);
|
||||
}
|
||||
}
|
||||
|
||||
params[key] = value;
|
||||
}
|
||||
|
||||
parseQueryParam(params: {[key: string]: any}): void {
|
||||
var key = matchUrlSegment(this._remaining);
|
||||
if (isBlank(key)) {
|
||||
return;
|
||||
}
|
||||
this.capture(key);
|
||||
var value: any = true;
|
||||
if (this.peekStartsWith('=')) {
|
||||
this.capture('=');
|
||||
var valueMatch = matchUrlQueryParamValue(this._remaining);
|
||||
if (isPresent(valueMatch)) {
|
||||
value = valueMatch;
|
||||
this.capture(value);
|
||||
}
|
||||
}
|
||||
|
||||
params[key] = value;
|
||||
}
|
||||
|
||||
parseAuxiliaryRoutes(): Url[] {
|
||||
var routes: Url[] = [];
|
||||
this.capture('(');
|
||||
|
||||
while (!this.peekStartsWith(')') && this._remaining.length > 0) {
|
||||
routes.push(this.parseSegment());
|
||||
if (this.peekStartsWith('//')) {
|
||||
this.capture('//');
|
||||
}
|
||||
}
|
||||
this.capture(')');
|
||||
|
||||
return routes;
|
||||
}
|
||||
}
|
||||
|
||||
export var parser = new UrlParser();
|
||||
@@ -1,37 +0,0 @@
|
||||
import {isPresent, isBlank} from '../src/facade/lang';
|
||||
import {StringMapWrapper} from '../src/facade/collection';
|
||||
|
||||
export class TouchMap {
|
||||
map: {[key: string]: string} = {};
|
||||
keys: {[key: string]: boolean} = {};
|
||||
|
||||
constructor(map: {[key: string]: any}) {
|
||||
if (isPresent(map)) {
|
||||
StringMapWrapper.forEach(map, (value, key) => {
|
||||
this.map[key] = isPresent(value) ? value.toString() : null;
|
||||
this.keys[key] = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
get(key: string): string {
|
||||
StringMapWrapper.delete(this.keys, key);
|
||||
return this.map[key];
|
||||
}
|
||||
|
||||
getUnused(): {[key: string]: any} {
|
||||
var unused: {[key: string]: any} = {};
|
||||
var keys = StringMapWrapper.keys(this.keys);
|
||||
keys.forEach(key => unused[key] = StringMapWrapper.get(this.map, key));
|
||||
return unused;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function normalizeString(obj: any): string {
|
||||
if (isBlank(obj)) {
|
||||
return null;
|
||||
} else {
|
||||
return obj.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {TestComponentBuilder} from '@angular/compiler/testing';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {SpyRouter, SpyLocation} from '../spies';
|
||||
import {provide, Component} from '@angular/core';
|
||||
import {Location} from '@angular/common';
|
||||
import {
|
||||
Router,
|
||||
RouteRegistry,
|
||||
RouterLink,
|
||||
RouterOutlet,
|
||||
Route,
|
||||
RouteParams,
|
||||
ComponentInstruction
|
||||
} from '@angular/router';
|
||||
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
||||
import {ResolvedInstruction} from '@angular/router/src/instruction';
|
||||
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
||||
|
||||
let dummyInstruction = new ResolvedInstruction(
|
||||
new ComponentInstruction('detail', [], null, null, true, '0', null, 'Detail'), null, {});
|
||||
|
||||
export function main() {
|
||||
describe('routerLink directive', function() {
|
||||
var tcb: TestComponentBuilder;
|
||||
|
||||
beforeEachProviders(() => [
|
||||
provide(Location, {useValue: makeDummyLocation()}),
|
||||
provide(Router, {useValue: makeDummyRouter()})
|
||||
]);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder], (tcBuilder) => { tcb = tcBuilder; }));
|
||||
|
||||
it('should update a[href] attribute', inject([AsyncTestCompleter], (async) => {
|
||||
|
||||
tcb.createAsync(TestComponent)
|
||||
.then((testComponent) => {
|
||||
testComponent.detectChanges();
|
||||
let anchorElement =
|
||||
testComponent.debugElement.query(By.css('a.detail-view')).nativeElement;
|
||||
expect(getDOM().getAttribute(anchorElement, 'href')).toEqual('detail');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should call router.navigate when a link is clicked',
|
||||
inject([AsyncTestCompleter, Router], (async, router) => {
|
||||
|
||||
tcb.createAsync(TestComponent)
|
||||
.then((testComponent) => {
|
||||
testComponent.detectChanges();
|
||||
// TODO: shouldn't this be just 'click' rather than '^click'?
|
||||
testComponent.debugElement.query(By.css('a.detail-view'))
|
||||
.triggerEventHandler('click', null);
|
||||
expect(router.spy('navigateByInstruction')).toHaveBeenCalledWith(dummyInstruction);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should call router.navigate when a link is clicked if target is _self',
|
||||
inject([AsyncTestCompleter, Router], (async, router) => {
|
||||
|
||||
tcb.createAsync(TestComponent)
|
||||
.then((testComponent) => {
|
||||
testComponent.detectChanges();
|
||||
testComponent.debugElement.query(By.css('a.detail-view-self'))
|
||||
.triggerEventHandler('click', null);
|
||||
expect(router.spy('navigateByInstruction')).toHaveBeenCalledWith(dummyInstruction);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should NOT call router.navigate when a link is clicked if target is set to other than _self',
|
||||
inject([AsyncTestCompleter, Router], (async, router) => {
|
||||
|
||||
tcb.createAsync(TestComponent)
|
||||
.then((testComponent) => {
|
||||
testComponent.detectChanges();
|
||||
testComponent.debugElement.query(By.css('a.detail-view-blank'))
|
||||
.triggerEventHandler('click', null);
|
||||
expect(router.spy('navigateByInstruction')).not.toHaveBeenCalled();
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
@Component({selector: 'user-cmp', template: "hello {{user}}"})
|
||||
class UserCmp {
|
||||
user: string;
|
||||
constructor(params: RouteParams) { this.user = params.get('name'); }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'test-component',
|
||||
template: `
|
||||
<div>
|
||||
<a [routerLink]="['/Detail']"
|
||||
class="detail-view">
|
||||
detail view
|
||||
</a>
|
||||
<a [routerLink]="['/Detail']"
|
||||
class="detail-view-self"
|
||||
target="_self">
|
||||
detail view with _self target
|
||||
</a>
|
||||
<a [routerLink]="['/Detail']"
|
||||
class="detail-view-blank"
|
||||
target="_blank">
|
||||
detail view with _blank target
|
||||
</a>
|
||||
</div>`,
|
||||
directives: [RouterLink]
|
||||
})
|
||||
class TestComponent {
|
||||
}
|
||||
|
||||
function makeDummyLocation() {
|
||||
var dl = new SpyLocation();
|
||||
dl.spy('prepareExternalUrl').andCallFake((url) => url);
|
||||
return dl;
|
||||
}
|
||||
|
||||
function makeDummyRouter() {
|
||||
var dr = new SpyRouter();
|
||||
dr.spy('generate').andCallFake((routeParams) => dummyInstruction);
|
||||
dr.spy('isRouteActive').andCallFake((_) => false);
|
||||
dr.spy('navigateInstruction');
|
||||
return dr;
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
# Router integration tests
|
||||
|
||||
These tests only mock out `Location`, and otherwise use all the real parts of routing to ensure that
|
||||
various routing scenarios work as expected.
|
||||
|
||||
The Component Router in Angular 2 exposes only a handful of different options, but because they can
|
||||
be combined and nested in so many ways, it's difficult to rigorously test all the cases.
|
||||
|
||||
The address this problem, we introduce `describeRouter`, `describeWith`, and `describeWithout`.
|
||||
@@ -1,37 +0,0 @@
|
||||
import {
|
||||
describeRouter,
|
||||
ddescribeRouter,
|
||||
describeWith,
|
||||
describeWithout,
|
||||
describeWithAndWithout,
|
||||
itShouldRoute,
|
||||
TEST_ROUTER_PROVIDERS
|
||||
} from './util';
|
||||
|
||||
import {beforeEachProviders, describe} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {registerSpecs} from './impl/async_route_spec_impl';
|
||||
|
||||
export function main() {
|
||||
describe('async route spec', () => {
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
registerSpecs();
|
||||
|
||||
describeRouter('async routes', () => {
|
||||
describeWithout('children', () => {
|
||||
describeWith('route data', itShouldRoute);
|
||||
describeWithAndWithout('params', itShouldRoute);
|
||||
});
|
||||
|
||||
describeWith('sync children',
|
||||
() => { describeWithAndWithout('default routes', itShouldRoute); });
|
||||
|
||||
describeWith('async children', () => {
|
||||
describeWithAndWithout('params',
|
||||
() => { describeWithout('default routes', itShouldRoute); });
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import {
|
||||
describeRouter,
|
||||
ddescribeRouter,
|
||||
describeWith,
|
||||
describeWithout,
|
||||
describeWithAndWithout,
|
||||
itShouldRoute,
|
||||
TEST_ROUTER_PROVIDERS
|
||||
} from './util';
|
||||
|
||||
import {
|
||||
beforeEachProviders,
|
||||
describe,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {registerSpecs} from './impl/aux_route_spec_impl';
|
||||
|
||||
export function main() {
|
||||
describe('auxiliary route spec', () => {
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
registerSpecs();
|
||||
|
||||
describeRouter('aux routes', () => {
|
||||
itShouldRoute();
|
||||
describeWith('a primary route', itShouldRoute);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,357 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
beforeEachProviders,
|
||||
ddescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xdescribe,
|
||||
xit,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
import {TestComponentBuilder} from '@angular/compiler/testing';
|
||||
|
||||
import {bootstrap} from '@angular/platform-browser-dynamic';
|
||||
import {APP_BASE_HREF, LocationStrategy} from '@angular/common';
|
||||
import {Component, Directive} from '@angular/core/src/metadata';
|
||||
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
||||
import {Console} from '@angular/core/src/console';
|
||||
import {provide} from '@angular/core';
|
||||
import {DOCUMENT} from '@angular/platform-browser/src/dom/dom_tokens';
|
||||
import {
|
||||
RouteConfig,
|
||||
Route,
|
||||
Redirect,
|
||||
AuxRoute
|
||||
} from '../../../router/src/route_config/route_config_decorator';
|
||||
import {PromiseWrapper} from '../../src/facade/async';
|
||||
import {BaseException, WrappedException} from '../../src/facade/exceptions';
|
||||
import {
|
||||
ROUTER_PROVIDERS,
|
||||
ROUTER_PRIMARY_COMPONENT,
|
||||
RouteParams,
|
||||
Router,
|
||||
ROUTER_DIRECTIVES
|
||||
} from '@angular/router';
|
||||
|
||||
import {MockLocationStrategy} from '@angular/common/testing';
|
||||
import {ApplicationRef} from '@angular/core/src/application_ref';
|
||||
import {MockApplicationRef} from '@angular/core/testing';
|
||||
|
||||
// noinspection JSAnnotator
|
||||
class DummyConsole implements Console {
|
||||
log(message) {}
|
||||
warn(message) {}
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe('router bootstrap', () => {
|
||||
beforeEachProviders(() => [
|
||||
ROUTER_PROVIDERS,
|
||||
provide(LocationStrategy, {useClass: MockLocationStrategy}),
|
||||
provide(ApplicationRef, {useClass: MockApplicationRef})
|
||||
]);
|
||||
|
||||
// do not refactor out the `bootstrap` functionality. We still want to
|
||||
// keep this test around so we can ensure that bootstrap a router works
|
||||
it('should bootstrap a simple app', inject([AsyncTestCompleter], (async) => {
|
||||
var fakeDoc = getDOM().createHtmlDocument();
|
||||
var el = getDOM().createElement('app-cmp', fakeDoc);
|
||||
getDOM().appendChild(fakeDoc.body, el);
|
||||
|
||||
bootstrap(AppCmp,
|
||||
[
|
||||
ROUTER_PROVIDERS,
|
||||
provide(ROUTER_PRIMARY_COMPONENT, {useValue: AppCmp}),
|
||||
provide(LocationStrategy, {useClass: MockLocationStrategy}),
|
||||
provide(DOCUMENT, {useValue: fakeDoc}),
|
||||
provide(Console, {useClass: DummyConsole})
|
||||
])
|
||||
.then((applicationRef) => {
|
||||
var router = applicationRef.instance.router;
|
||||
router.subscribe((_) => {
|
||||
expect(el).toHaveText('outer { hello }');
|
||||
expect(applicationRef.instance.location.path()).toEqual('');
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
describe('broken app', () => {
|
||||
beforeEachProviders(() => [provide(ROUTER_PRIMARY_COMPONENT, {useValue: BrokenAppCmp})]);
|
||||
|
||||
it('should rethrow exceptions from component constructors',
|
||||
inject([AsyncTestCompleter, TestComponentBuilder], (async, tcb: TestComponentBuilder) => {
|
||||
tcb.createAsync(AppCmp).then((fixture) => {
|
||||
var router = fixture.debugElement.componentInstance.router;
|
||||
PromiseWrapper.catchError(router.navigateByUrl('/cause-error'), (error) => {
|
||||
expect(error).toContainError('oops!');
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
describe('back button app', () => {
|
||||
beforeEachProviders(() => [provide(ROUTER_PRIMARY_COMPONENT, {useValue: HierarchyAppCmp})]);
|
||||
|
||||
it('should change the url without pushing a new history state for back navigations',
|
||||
inject([AsyncTestCompleter, TestComponentBuilder], (async, tcb: TestComponentBuilder) => {
|
||||
|
||||
tcb.createAsync(HierarchyAppCmp)
|
||||
.then((fixture) => {
|
||||
var router = fixture.debugElement.componentInstance.router;
|
||||
var position = 0;
|
||||
var flipped = false;
|
||||
var history = [
|
||||
['/parent/child', 'root { parent { hello } }', '/super-parent/child'],
|
||||
['/super-parent/child', 'root { super-parent { hello2 } }', '/parent/child'],
|
||||
['/parent/child', 'root { parent { hello } }', false]
|
||||
];
|
||||
|
||||
router.subscribe((_) => {
|
||||
var location = fixture.debugElement.componentInstance.location;
|
||||
var element = fixture.debugElement.nativeElement;
|
||||
var path = location.path();
|
||||
|
||||
var entry = history[position];
|
||||
|
||||
expect(path).toEqual(entry[0]);
|
||||
expect(element).toHaveText(entry[1]);
|
||||
|
||||
var nextUrl = entry[2];
|
||||
if (nextUrl == false) {
|
||||
flipped = true;
|
||||
}
|
||||
|
||||
if (flipped && position == 0) {
|
||||
async.done();
|
||||
return;
|
||||
}
|
||||
|
||||
position = position + (flipped ? -1 : 1);
|
||||
if (flipped) {
|
||||
location.back();
|
||||
} else {
|
||||
router.navigateByUrl(nextUrl);
|
||||
}
|
||||
});
|
||||
|
||||
router.navigateByUrl(history[0][0]);
|
||||
});
|
||||
}), 1000);
|
||||
});
|
||||
|
||||
describe('hierarchical app', () => {
|
||||
beforeEachProviders(
|
||||
() => { return [provide(ROUTER_PRIMARY_COMPONENT, {useValue: HierarchyAppCmp})]; });
|
||||
|
||||
it('should bootstrap an app with a hierarchy',
|
||||
inject([AsyncTestCompleter, TestComponentBuilder], (async, tcb: TestComponentBuilder) => {
|
||||
|
||||
tcb.createAsync(HierarchyAppCmp)
|
||||
.then((fixture) => {
|
||||
var router = fixture.debugElement.componentInstance.router;
|
||||
router.subscribe((_) => {
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('root { parent { hello } }');
|
||||
expect(fixture.debugElement.componentInstance.location.path())
|
||||
.toEqual('/parent/child');
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/parent/child');
|
||||
});
|
||||
}));
|
||||
|
||||
// TODO(btford): mock out level lower than LocationStrategy once that level exists
|
||||
xdescribe('custom app base ref', () => {
|
||||
beforeEachProviders(() => { return [provide(APP_BASE_HREF, {useValue: '/my/app'})]; });
|
||||
it('should bootstrap',
|
||||
inject([AsyncTestCompleter, TestComponentBuilder],
|
||||
(async, tcb: TestComponentBuilder) => {
|
||||
|
||||
tcb.createAsync(HierarchyAppCmp)
|
||||
.then((fixture) => {
|
||||
var router = fixture.debugElement.componentInstance.router;
|
||||
router.subscribe((_) => {
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('root { parent { hello } }');
|
||||
expect(fixture.debugElement.componentInstance.location.path())
|
||||
.toEqual('/my/app/parent/child');
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/parent/child');
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('querystring params app', () => {
|
||||
beforeEachProviders(
|
||||
() => { return [provide(ROUTER_PRIMARY_COMPONENT, {useValue: QueryStringAppCmp})]; });
|
||||
|
||||
it('should recognize and return querystring params with the injected RouteParams',
|
||||
inject([AsyncTestCompleter, TestComponentBuilder], (async, tcb: TestComponentBuilder) => {
|
||||
tcb.createAsync(QueryStringAppCmp)
|
||||
.then((fixture) => {
|
||||
var router = fixture.debugElement.componentInstance.router;
|
||||
router.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('qParam = search-for-something');
|
||||
/*
|
||||
expect(applicationRef.hostComponent.location.path())
|
||||
.toEqual('/qs?q=search-for-something');*/
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/qs?q=search-for-something');
|
||||
fixture.detectChanges();
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
describe('activate event on outlet', () => {
|
||||
let tcb: TestComponentBuilder = null;
|
||||
|
||||
beforeEachProviders(() => [provide(ROUTER_PRIMARY_COMPONENT, {useValue: AppCmp})]);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder],
|
||||
(testComponentBuilder) => { tcb = testComponentBuilder; }));
|
||||
|
||||
it('should get a reference and pass data to components loaded inside of outlets',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
tcb.createAsync(AppWithOutletListeners)
|
||||
.then(fixture => {
|
||||
let appInstance = fixture.debugElement.componentInstance;
|
||||
let router = appInstance.router;
|
||||
|
||||
router.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(appInstance.helloCmp).toBeAnInstanceOf(HelloCmp);
|
||||
expect(appInstance.helloCmp.message).toBe('Ahoy');
|
||||
|
||||
async.done();
|
||||
});
|
||||
|
||||
// TODO(juliemr): This isn't necessary for the test to pass - figure
|
||||
// out what's going on.
|
||||
// router.navigateByUrl('/rainbow(pony)');
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Component({selector: 'hello-cmp', template: 'hello'})
|
||||
class HelloCmp {
|
||||
public message: string;
|
||||
}
|
||||
|
||||
@Component({selector: 'hello2-cmp', template: 'hello2'})
|
||||
class Hello2Cmp {
|
||||
public greeting: string;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `outer { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([new Route({path: '/', component: HelloCmp})])
|
||||
class AppCmp {
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `
|
||||
Hello routing!
|
||||
<router-outlet (activate)="activateHello($event)"></router-outlet>
|
||||
<router-outlet (activate)="activateHello2($event)" name="pony"></router-outlet>`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([
|
||||
new Route({path: '/rainbow', component: HelloCmp}),
|
||||
new AuxRoute({name: 'pony', path: 'pony', component: Hello2Cmp})
|
||||
])
|
||||
class AppWithOutletListeners {
|
||||
helloCmp: HelloCmp;
|
||||
hello2Cmp: Hello2Cmp;
|
||||
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
|
||||
activateHello(cmp: HelloCmp) {
|
||||
this.helloCmp = cmp;
|
||||
this.helloCmp.message = 'Ahoy';
|
||||
}
|
||||
|
||||
activateHello2(cmp: Hello2Cmp) { this.hello2Cmp = cmp; }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent-cmp',
|
||||
template: `parent { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([new Route({path: '/child', component: HelloCmp})])
|
||||
class ParentCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'super-parent-cmp',
|
||||
template: `super-parent { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([new Route({path: '/child', component: Hello2Cmp})])
|
||||
class SuperParentCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([
|
||||
new Route({path: '/parent/...', component: ParentCmp}),
|
||||
new Route({path: '/super-parent/...', component: SuperParentCmp})
|
||||
])
|
||||
class HierarchyAppCmp {
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
@Component({selector: 'qs-cmp', template: `qParam = {{q}}`})
|
||||
class QSCmp {
|
||||
q: string;
|
||||
constructor(params: RouteParams) { this.q = params.get('q'); }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `<router-outlet></router-outlet>`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([new Route({path: '/qs', component: QSCmp})])
|
||||
class QueryStringAppCmp {
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
@Component({selector: 'oops-cmp', template: "oh no"})
|
||||
class BrokenCmp {
|
||||
constructor() { throw new BaseException('oops!'); }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `outer { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([new Route({path: '/cause-error', component: BrokenCmp})])
|
||||
class BrokenAppCmp {
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
@@ -1,656 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
beforeEachProviders,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {Location} from '@angular/common';
|
||||
|
||||
import {specs, compile, TEST_ROUTER_PROVIDERS, clickOnElement, getHref} from '../util';
|
||||
|
||||
import {Router, AsyncRoute, Route} from '@angular/router';
|
||||
|
||||
import {
|
||||
HelloCmp,
|
||||
helloCmpLoader,
|
||||
UserCmp,
|
||||
userCmpLoader,
|
||||
TeamCmp,
|
||||
asyncTeamLoader,
|
||||
ParentCmp,
|
||||
parentCmpLoader,
|
||||
asyncParentCmpLoader,
|
||||
asyncDefaultParentCmpLoader,
|
||||
ParentWithDefaultCmp,
|
||||
parentWithDefaultCmpLoader,
|
||||
asyncRouteDataCmp
|
||||
} from './fixture_components';
|
||||
import {By} from '../../../../platform-browser/src/dom/debug/by';
|
||||
|
||||
function getLinkElement(rtc: ComponentFixture<any>) {
|
||||
return rtc.debugElement.query(By.css('a')).nativeElement;
|
||||
}
|
||||
|
||||
function asyncRoutesWithoutChildrenWithRouteData() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should inject route data into the component', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute(
|
||||
{path: '/route-data', loader: asyncRouteDataCmp, data: {isAdmin: true}})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/route-data'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('true');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should inject empty object if the route has no data property',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/route-data-default', loader: asyncRouteDataCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/route-data-default'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
function asyncRoutesWithoutChildrenWithoutParams() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/test', loader: helloCmpLoader, name: 'Hello'})]))
|
||||
.then((_) => rtr.navigateByUrl('/test'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/test', loader: helloCmpLoader, name: 'Hello'})]))
|
||||
.then((_) => rtr.navigate(['/Hello']))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `<a [routerLink]="['Hello']">go to hello</a> | <router-outlet></router-outlet>`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/test', loader: helloCmpLoader, name: 'Hello'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/test');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, `<a [routerLink]="['Hello']">go to hello</a> | <router-outlet></router-outlet>`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/test', loader: helloCmpLoader, name: 'Hello'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('go to hello | ');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('go to hello | hello');
|
||||
expect(location.urlChanges).toEqual(['/test']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function asyncRoutesWithoutChildrenWithParams() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/user/:name', loader: userCmpLoader, name: 'User'})]))
|
||||
.then((_) => rtr.navigateByUrl('/user/igor'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello igor');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/user/:name', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => rtr.navigate(['/User', {name: 'brian'}]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello brian');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `<a [routerLink]="['User', {name: 'naomi'}]">greet naomi</a> | <router-outlet></router-outlet>`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/user/:name', loader: userCmpLoader, name: 'User'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/user/naomi');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, `<a [routerLink]="['User', {name: 'naomi'}]">greet naomi</a> | <router-outlet></router-outlet>`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/user/:name', loader: userCmpLoader, name: 'User'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('greet naomi | ');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('greet naomi | hello naomi');
|
||||
expect(location.urlChanges).toEqual(['/user/naomi']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate between components with different parameters',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/user/:name', loader: userCmpLoader, name: 'User'})]))
|
||||
.then((_) => rtr.navigateByUrl('/user/brian'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello brian');
|
||||
})
|
||||
.then((_) => rtr.navigateByUrl('/user/igor'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello igor');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function asyncRoutesWithSyncChildrenWithoutDefaultRoutes() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/a/...', loader: parentCmpLoader, name: 'Parent'})]))
|
||||
.then((_) => rtr.navigateByUrl('/a/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/a/...', loader: parentCmpLoader, name: 'Parent'})]))
|
||||
.then((_) => rtr.navigate(['/Parent', 'Child']))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `<a [routerLink]="['Parent']">nav to child</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/a/...', loader: parentCmpLoader, name: 'Parent'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/a');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, `<a [routerLink]="['Parent', 'Child']">nav to child</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new AsyncRoute({path: '/a/...', loader: parentCmpLoader, name: 'Parent'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('nav to child | outer { }');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('nav to child | outer { inner { hello } }');
|
||||
expect(location.urlChanges).toEqual(['/a/b']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function asyncRoutesWithSyncChildrenWithDefaultRoutes() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/a/...', loader: parentWithDefaultCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/a'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/a/...', loader: parentWithDefaultCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => rtr.navigate(['/Parent']))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `<a [routerLink]="['/Parent']">link to inner</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/a/...', loader: parentWithDefaultCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/a');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, `<a [routerLink]="['/Parent']">link to inner</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/a/...', loader: parentWithDefaultCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('link to inner | outer { }');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('link to inner | outer { inner { hello } }');
|
||||
expect(location.urlChanges).toEqual(['/a/b']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function asyncRoutesWithAsyncChildrenWithoutParamsWithoutDefaultRoutes() {
|
||||
var rootTC;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/a/...', loader: asyncParentCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/a/b'))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/a/...', loader: asyncParentCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => rtr.navigate(['/Parent', 'Child']))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `<a [routerLink]="['Parent', 'Child']">nav to child</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/a/...', loader: asyncParentCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(getHref(getLinkElement(rootTC))).toEqual('/a');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, `<a [routerLink]="['Parent', 'Child']">nav to child</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/a/...', loader: asyncParentCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('nav to child | outer { }');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement)
|
||||
.toHaveText('nav to child | outer { inner { hello } }');
|
||||
expect(location.urlChanges).toEqual(['/a/b']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(rootTC));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function asyncRoutesWithAsyncChildrenWithoutParamsWithDefaultRoutes() {
|
||||
var rootTC;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute(
|
||||
{path: '/a/...', loader: asyncDefaultParentCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/a'))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute(
|
||||
{path: '/a/...', loader: asyncDefaultParentCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => rtr.navigate(['/Parent']))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `<a [routerLink]="['Parent']">nav to child</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute(
|
||||
{path: '/a/...', loader: asyncDefaultParentCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(getHref(getLinkElement(rootTC))).toEqual('/a');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, `<a [routerLink]="['Parent']">nav to child</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute(
|
||||
{path: '/a/...', loader: asyncDefaultParentCmpLoader, name: 'Parent'})
|
||||
]))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('nav to child | outer { }');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement)
|
||||
.toHaveText('nav to child | outer { inner { hello } }');
|
||||
expect(location.urlChanges).toEqual(['/a/b']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(rootTC));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function asyncRoutesWithAsyncChildrenWithParamsWithoutDefaultRoutes() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `{ <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/team/:id/...', loader: asyncTeamLoader, name: 'Team'})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/team/angular/user/matias'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('{ team angular | user { hello matias } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `{ <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/team/:id/...', loader: asyncTeamLoader, name: 'Team'})
|
||||
]))
|
||||
.then((_) => rtr.navigate(['/Team', {id: 'angular'}, 'User', {name: 'matias'}]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('{ team angular | user { hello matias } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(
|
||||
tcb,
|
||||
`<a [routerLink]="['/Team', {id: 'angular'}, 'User', {name: 'matias'}]">nav to matias</a> { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/team/:id/...', loader: asyncTeamLoader, name: 'Team'})
|
||||
]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/team/angular');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(
|
||||
tcb,
|
||||
`<a [routerLink]="['/Team', {id: 'angular'}, 'User', {name: 'matias'}]">nav to matias</a> { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute({path: '/team/:id/...', loader: asyncTeamLoader, name: 'Team'})
|
||||
]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('nav to matias { }');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('nav to matias { team angular | user { hello matias } }');
|
||||
expect(location.urlChanges).toEqual(['/team/angular/user/matias']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
export function registerSpecs() {
|
||||
specs['asyncRoutesWithoutChildrenWithRouteData'] = asyncRoutesWithoutChildrenWithRouteData;
|
||||
specs['asyncRoutesWithoutChildrenWithoutParams'] = asyncRoutesWithoutChildrenWithoutParams;
|
||||
specs['asyncRoutesWithoutChildrenWithParams'] = asyncRoutesWithoutChildrenWithParams;
|
||||
specs['asyncRoutesWithSyncChildrenWithoutDefaultRoutes'] =
|
||||
asyncRoutesWithSyncChildrenWithoutDefaultRoutes;
|
||||
specs['asyncRoutesWithSyncChildrenWithDefaultRoutes'] =
|
||||
asyncRoutesWithSyncChildrenWithDefaultRoutes;
|
||||
specs['asyncRoutesWithAsyncChildrenWithoutParamsWithoutDefaultRoutes'] =
|
||||
asyncRoutesWithAsyncChildrenWithoutParamsWithoutDefaultRoutes;
|
||||
specs['asyncRoutesWithAsyncChildrenWithoutParamsWithDefaultRoutes'] =
|
||||
asyncRoutesWithAsyncChildrenWithoutParamsWithDefaultRoutes;
|
||||
specs['asyncRoutesWithAsyncChildrenWithParamsWithoutDefaultRoutes'] =
|
||||
asyncRoutesWithAsyncChildrenWithParamsWithoutDefaultRoutes;
|
||||
}
|
||||
@@ -1,240 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {Location} from '@angular/common';
|
||||
import {Component} from '@angular/core';
|
||||
import {Router, ROUTER_DIRECTIVES, Route, AuxRoute, RouteConfig} from '@angular/router';
|
||||
import {specs, compile, clickOnElement, getHref} from '../util';
|
||||
import {BaseException} from '../../../src/facade/exceptions';
|
||||
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
||||
|
||||
function getLinkElement(rtc: ComponentFixture<any>, linkIndex: number = 0) {
|
||||
return rtc.debugElement.queryAll(By.css('a'))[linkIndex].nativeElement;
|
||||
}
|
||||
|
||||
function auxRoutes() {
|
||||
var tcb: TestComponentBuilder;
|
||||
var fixture: ComponentFixture<any>;
|
||||
var rtr;
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should recognize and navigate from the URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `main {<router-outlet></router-outlet>} | aux {<router-outlet name="modal"></router-outlet>}`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/hello', component: HelloCmp, name: 'Hello'}),
|
||||
new AuxRoute({path: '/modal', component: ModalCmp, name: 'Aux'})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/(modal)'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('main {} | aux {modal}');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate via the link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `main {<router-outlet></router-outlet>} | aux {<router-outlet name="modal"></router-outlet>}`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/hello', component: HelloCmp, name: 'Hello'}),
|
||||
new AuxRoute({path: '/modal', component: ModalCmp, name: 'Modal'})
|
||||
]))
|
||||
.then((_) => rtr.navigate(['/', ['Modal']]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('main {} | aux {modal}');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(
|
||||
tcb,
|
||||
`<a [routerLink]="['/', ['Modal']]">open modal</a> | main {<router-outlet></router-outlet>} | aux {<router-outlet name="modal"></router-outlet>}`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/hello', component: HelloCmp, name: 'Hello'}),
|
||||
new AuxRoute({path: '/modal', component: ModalCmp, name: 'Modal'})
|
||||
]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/(modal)');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(
|
||||
tcb,
|
||||
`<a [routerLink]="['/', ['Modal']]">open modal</a> | <a [routerLink]="['/Hello']">hello</a> | main {<router-outlet></router-outlet>} | aux {<router-outlet name="modal"></router-outlet>}`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/hello', component: HelloCmp, name: 'Hello'}),
|
||||
new AuxRoute({path: '/modal', component: ModalCmp, name: 'Modal'})
|
||||
]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('open modal | hello | main {} | aux {}');
|
||||
|
||||
var navCount = 0;
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
navCount += 1;
|
||||
fixture.detectChanges();
|
||||
if (navCount == 1) {
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('open modal | hello | main {} | aux {modal}');
|
||||
expect(location.urlChanges).toEqual(['/(modal)']);
|
||||
expect(getHref(getLinkElement(fixture, 0))).toEqual('/(modal)');
|
||||
expect(getHref(getLinkElement(fixture, 1))).toEqual('/hello(modal)');
|
||||
|
||||
// click on primary route link
|
||||
clickOnElement(getLinkElement(fixture, 1));
|
||||
} else if (navCount == 2) {
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('open modal | hello | main {hello} | aux {modal}');
|
||||
expect(location.urlChanges).toEqual(['/(modal)', '/hello(modal)']);
|
||||
expect(getHref(getLinkElement(fixture, 0))).toEqual('/hello(modal)');
|
||||
expect(getHref(getLinkElement(fixture, 1))).toEqual('/hello(modal)');
|
||||
async.done();
|
||||
} else {
|
||||
throw new BaseException(`Unexpected route change #${navCount}`);
|
||||
}
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function auxRoutesWithAPrimaryRoute() {
|
||||
var tcb: TestComponentBuilder;
|
||||
var fixture: ComponentFixture<any>;
|
||||
var rtr;
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should recognize and navigate from the URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `main {<router-outlet></router-outlet>} | aux {<router-outlet name="modal"></router-outlet>}`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/hello', component: HelloCmp, name: 'Hello'}),
|
||||
new AuxRoute({path: '/modal', component: ModalCmp, name: 'Aux'})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/hello(modal)'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('main {hello} | aux {modal}');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate via the link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `main {<router-outlet></router-outlet>} | aux {<router-outlet name="modal"></router-outlet>}`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/hello', component: HelloCmp, name: 'Hello'}),
|
||||
new AuxRoute({path: '/modal', component: ModalCmp, name: 'Modal'})
|
||||
]))
|
||||
.then((_) => rtr.navigate(['/Hello', ['Modal']]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('main {hello} | aux {modal}');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(
|
||||
tcb,
|
||||
`<a [routerLink]="['/Hello', ['Modal']]">open modal</a> | main {<router-outlet></router-outlet>} | aux {<router-outlet name="modal"></router-outlet>}`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/hello', component: HelloCmp, name: 'Hello'}),
|
||||
new AuxRoute({path: '/modal', component: ModalCmp, name: 'Modal'})
|
||||
]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/hello(modal)');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(
|
||||
tcb,
|
||||
`<a [routerLink]="['/Hello', ['Modal']]">open modal</a> | main {<router-outlet></router-outlet>} | aux {<router-outlet name="modal"></router-outlet>}`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/hello', component: HelloCmp, name: 'Hello'}),
|
||||
new AuxRoute({path: '/modal', component: ModalCmp, name: 'Modal'})
|
||||
]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('open modal | main {} | aux {}');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('open modal | main {hello} | aux {modal}');
|
||||
expect(location.urlChanges).toEqual(['/hello(modal)']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
export function registerSpecs() {
|
||||
specs['auxRoutes'] = auxRoutes;
|
||||
specs['auxRoutesWithAPrimaryRoute'] = auxRoutesWithAPrimaryRoute;
|
||||
}
|
||||
|
||||
|
||||
@Component({selector: 'hello-cmp', template: `{{greeting}}`})
|
||||
class HelloCmp {
|
||||
greeting: string;
|
||||
constructor() { this.greeting = 'hello'; }
|
||||
}
|
||||
|
||||
@Component({selector: 'modal-cmp', template: `modal`})
|
||||
class ModalCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'aux-cmp',
|
||||
template: 'main {<router-outlet></router-outlet>} | ' +
|
||||
'aux {<router-outlet name="modal"></router-outlet>}',
|
||||
directives: [ROUTER_DIRECTIVES],
|
||||
})
|
||||
@RouteConfig([
|
||||
new Route({path: '/hello', component: HelloCmp, name: 'Hello'}),
|
||||
new AuxRoute({path: '/modal', component: ModalCmp, name: 'Aux'})
|
||||
])
|
||||
class AuxCmp {
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
import {Component, ComponentRef, ViewContainerRef, ViewChild} from '@angular/core';
|
||||
import {
|
||||
AsyncRoute,
|
||||
Route,
|
||||
Redirect,
|
||||
RouteConfig,
|
||||
RouteParams,
|
||||
RouteData,
|
||||
ROUTER_DIRECTIVES
|
||||
} from '@angular/router';
|
||||
import {PromiseWrapper} from '../../../src/facade/async';
|
||||
import {isPresent} from '../../../src/facade/lang';
|
||||
import {DynamicComponentLoader} from '@angular/core/src/linker/dynamic_component_loader';
|
||||
|
||||
@Component({selector: 'goodbye-cmp', template: `{{farewell}}`})
|
||||
export class GoodbyeCmp {
|
||||
farewell: string;
|
||||
constructor() { this.farewell = 'goodbye'; }
|
||||
}
|
||||
|
||||
@Component({selector: 'hello-cmp', template: `{{greeting}}`})
|
||||
export class HelloCmp {
|
||||
greeting: string;
|
||||
constructor() { this.greeting = 'hello'; }
|
||||
}
|
||||
|
||||
export function helloCmpLoader() {
|
||||
return PromiseWrapper.resolve(HelloCmp);
|
||||
}
|
||||
|
||||
|
||||
@Component({selector: 'user-cmp', template: `hello {{user}}`})
|
||||
export class UserCmp {
|
||||
user: string;
|
||||
constructor(params: RouteParams) { this.user = params.get('name'); }
|
||||
}
|
||||
|
||||
export function userCmpLoader() {
|
||||
return PromiseWrapper.resolve(UserCmp);
|
||||
}
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'parent-cmp',
|
||||
template: `inner { <router-outlet></router-outlet> }`,
|
||||
directives: [ROUTER_DIRECTIVES],
|
||||
})
|
||||
@RouteConfig([new Route({path: '/b', component: HelloCmp, name: 'Child'})])
|
||||
export class ParentCmp {
|
||||
}
|
||||
|
||||
export function parentCmpLoader() {
|
||||
return PromiseWrapper.resolve(ParentCmp);
|
||||
}
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'parent-cmp',
|
||||
template: `inner { <router-outlet></router-outlet> }`,
|
||||
directives: [ROUTER_DIRECTIVES],
|
||||
})
|
||||
@RouteConfig([new AsyncRoute({path: '/b', loader: helloCmpLoader, name: 'Child'})])
|
||||
export class AsyncParentCmp {
|
||||
}
|
||||
|
||||
export function asyncParentCmpLoader() {
|
||||
return PromiseWrapper.resolve(AsyncParentCmp);
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent-cmp',
|
||||
template: `inner { <router-outlet></router-outlet> }`,
|
||||
directives: [ROUTER_DIRECTIVES],
|
||||
})
|
||||
@RouteConfig(
|
||||
[new AsyncRoute({path: '/b', loader: helloCmpLoader, name: 'Child', useAsDefault: true})])
|
||||
export class AsyncDefaultParentCmp {
|
||||
}
|
||||
|
||||
export function asyncDefaultParentCmpLoader() {
|
||||
return PromiseWrapper.resolve(AsyncDefaultParentCmp);
|
||||
}
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'parent-cmp',
|
||||
template: `inner { <router-outlet></router-outlet> }`,
|
||||
directives: [ROUTER_DIRECTIVES],
|
||||
})
|
||||
@RouteConfig([new Route({path: '/b', component: HelloCmp, name: 'Child', useAsDefault: true})])
|
||||
export class ParentWithDefaultCmp {
|
||||
}
|
||||
|
||||
export function parentWithDefaultCmpLoader() {
|
||||
return PromiseWrapper.resolve(ParentWithDefaultCmp);
|
||||
}
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'team-cmp',
|
||||
template: `team {{id}} | user { <router-outlet></router-outlet> }`,
|
||||
directives: [ROUTER_DIRECTIVES],
|
||||
})
|
||||
@RouteConfig([new Route({path: '/user/:name', component: UserCmp, name: 'User'})])
|
||||
export class TeamCmp {
|
||||
id: string;
|
||||
constructor(params: RouteParams) { this.id = params.get('id'); }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'team-cmp',
|
||||
template: `team {{id}} | user { <router-outlet></router-outlet> }`,
|
||||
directives: [ROUTER_DIRECTIVES],
|
||||
})
|
||||
@RouteConfig([new AsyncRoute({path: '/user/:name', loader: userCmpLoader, name: 'User'})])
|
||||
export class AsyncTeamCmp {
|
||||
id: string;
|
||||
constructor(params: RouteParams) { this.id = params.get('id'); }
|
||||
}
|
||||
|
||||
export function asyncTeamLoader() {
|
||||
return PromiseWrapper.resolve(AsyncTeamCmp);
|
||||
}
|
||||
|
||||
|
||||
@Component({selector: 'data-cmp', template: `{{myData}}`})
|
||||
export class RouteDataCmp {
|
||||
myData: boolean;
|
||||
constructor(data: RouteData) { this.myData = data.get('isAdmin'); }
|
||||
}
|
||||
|
||||
export function asyncRouteDataCmp() {
|
||||
return PromiseWrapper.resolve(RouteDataCmp);
|
||||
}
|
||||
|
||||
@Component({selector: 'redirect-to-parent-cmp', template: 'redirect-to-parent'})
|
||||
@RouteConfig([new Redirect({path: '/child-redirect', redirectTo: ['../HelloSib']})])
|
||||
export class RedirectToParentCmp {
|
||||
}
|
||||
|
||||
|
||||
@Component({selector: 'dynamic-loader-cmp', template: `{ <div #viewport></div> }`})
|
||||
@RouteConfig([new Route({path: '/', component: HelloCmp})])
|
||||
export class DynamicLoaderCmp {
|
||||
private _componentRef: ComponentRef<any> = null;
|
||||
|
||||
@ViewChild('viewport', {read: ViewContainerRef}) viewport: ViewContainerRef;
|
||||
|
||||
constructor(private _dynamicComponentLoader: DynamicComponentLoader) {}
|
||||
|
||||
onSomeAction(): Promise<any> {
|
||||
if (isPresent(this._componentRef)) {
|
||||
this._componentRef.destroy();
|
||||
this._componentRef = null;
|
||||
}
|
||||
return this._dynamicComponentLoader.loadNextToLocation(DynamicallyLoadedComponent,
|
||||
this.viewport)
|
||||
.then((cmp) => { this._componentRef = cmp; });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'loaded-cmp',
|
||||
template: '<router-outlet></router-outlet>',
|
||||
directives: [ROUTER_DIRECTIVES]
|
||||
})
|
||||
class DynamicallyLoadedComponent {
|
||||
}
|
||||
@@ -1,487 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
beforeEachProviders,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
||||
|
||||
import {specs, compile, TEST_ROUTER_PROVIDERS, clickOnElement, getHref} from '../util';
|
||||
import {Location} from '@angular/common';
|
||||
import {Router, Route} from '@angular/router';
|
||||
import {
|
||||
HelloCmp,
|
||||
UserCmp,
|
||||
TeamCmp,
|
||||
ParentCmp,
|
||||
ParentWithDefaultCmp,
|
||||
DynamicLoaderCmp
|
||||
} from './fixture_components';
|
||||
import {PromiseWrapper} from '../../../src/facade/async';
|
||||
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
||||
|
||||
|
||||
function getLinkElement(rtc: ComponentFixture<any>) {
|
||||
return rtc.debugElement.query(By.css('a')).nativeElement;
|
||||
}
|
||||
|
||||
function syncRoutesWithoutChildrenWithoutParams() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) =>
|
||||
rtr.config([new Route({path: '/test', component: HelloCmp, name: 'Hello'})]))
|
||||
.then((_) => rtr.navigateByUrl('/test'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) =>
|
||||
rtr.config([new Route({path: '/test', component: HelloCmp, name: 'Hello'})]))
|
||||
.then((_) => rtr.navigate(['/Hello']))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `<a [routerLink]="['Hello']">go to hello</a> | <router-outlet></router-outlet>`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) =>
|
||||
rtr.config([new Route({path: '/test', component: HelloCmp, name: 'Hello'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/test');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, `<a [routerLink]="['Hello']">go to hello</a> | <router-outlet></router-outlet>`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) =>
|
||||
rtr.config([new Route({path: '/test', component: HelloCmp, name: 'Hello'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('go to hello | ');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('go to hello | hello');
|
||||
expect(location.urlChanges).toEqual(['/test']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function syncRoutesWithoutChildrenWithParams() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/user/:name', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => rtr.navigateByUrl('/user/igor'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello igor');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/user/:name', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => rtr.navigate(['/User', {name: 'brian'}]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello brian');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `<a [routerLink]="['User', {name: 'naomi'}]">greet naomi</a> | <router-outlet></router-outlet>`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/user/:name', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/user/naomi');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, `<a [routerLink]="['User', {name: 'naomi'}]">greet naomi</a> | <router-outlet></router-outlet>`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/user/:name', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('greet naomi | ');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('greet naomi | hello naomi');
|
||||
expect(location.urlChanges).toEqual(['/user/naomi']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate between components with different parameters',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/user/:name', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => rtr.navigateByUrl('/user/brian'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello brian');
|
||||
})
|
||||
.then((_) => rtr.navigateByUrl('/user/igor'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello igor');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function syncRoutesWithSyncChildrenWithoutDefaultRoutesWithoutParams() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/a/...', component: ParentCmp, name: 'Parent'})]))
|
||||
.then((_) => rtr.navigateByUrl('/a/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/a/...', component: ParentCmp, name: 'Parent'})]))
|
||||
.then((_) => rtr.navigate(['/Parent', 'Child']))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `<a [routerLink]="['Parent', 'Child']">nav to child</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/a/...', component: ParentCmp, name: 'Parent'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/a/b');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, `<a [routerLink]="['Parent', 'Child']">nav to child</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/a/...', component: ParentCmp, name: 'Parent'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('nav to child | outer { }');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('nav to child | outer { inner { hello } }');
|
||||
expect(location.urlChanges).toEqual(['/a/b']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function syncRoutesWithSyncChildrenWithoutDefaultRoutesWithParams() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `{ <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/team/:id/...', component: TeamCmp, name: 'Team'})]))
|
||||
.then((_) => rtr.navigateByUrl('/team/angular/user/matias'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('{ team angular | user { hello matias } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `{ <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/team/:id/...', component: TeamCmp, name: 'Team'})]))
|
||||
.then((_) => rtr.navigate(['/Team', {id: 'angular'}, 'User', {name: 'matias'}]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('{ team angular | user { hello matias } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(
|
||||
tcb,
|
||||
`<a [routerLink]="['/Team', {id: 'angular'}, 'User', {name: 'matias'}]">nav to matias</a> { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/team/:id/...', component: TeamCmp, name: 'Team'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/team/angular/user/matias');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(
|
||||
tcb,
|
||||
`<a [routerLink]="['/Team', {id: 'angular'}, 'User', {name: 'matias'}]">nav to matias</a> { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/team/:id/...', component: TeamCmp, name: 'Team'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('nav to matias { }');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('nav to matias { team angular | user { hello matias } }');
|
||||
expect(location.urlChanges).toEqual(['/team/angular/user/matias']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
function syncRoutesWithSyncChildrenWithDefaultRoutesWithoutParams() {
|
||||
var fixture;
|
||||
var tcb;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
it('should navigate by URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then(
|
||||
(_) => rtr.config(
|
||||
[new Route({path: '/a/...', component: ParentWithDefaultCmp, name: 'Parent'})]))
|
||||
.then((_) => rtr.navigateByUrl('/a'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate by link DSL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then(
|
||||
(_) => rtr.config(
|
||||
[new Route({path: '/a/...', component: ParentWithDefaultCmp, name: 'Parent'})]))
|
||||
.then((_) => rtr.navigate(['/Parent']))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate a link URL', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, `<a [routerLink]="['/Parent']">link to inner</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then(
|
||||
(_) => rtr.config(
|
||||
[new Route({path: '/a/...', component: ParentWithDefaultCmp, name: 'Parent'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(getLinkElement(fixture))).toEqual('/a');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate from a link click',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, `<a [routerLink]="['/Parent']">link to inner</a> | outer { <router-outlet></router-outlet> }`)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then(
|
||||
(_) => rtr.config(
|
||||
[new Route({path: '/a/...', component: ParentWithDefaultCmp, name: 'Parent'})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('link to inner | outer { }');
|
||||
|
||||
rtr.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('link to inner | outer { inner { hello } }');
|
||||
expect(location.urlChanges).toEqual(['/a/b']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
clickOnElement(getLinkElement(fixture));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
function syncRoutesWithDynamicComponents() {
|
||||
var fixture: ComponentFixture<any>;
|
||||
var tcb: TestComponentBuilder;
|
||||
var rtr: Router;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
}));
|
||||
|
||||
|
||||
it('should work',
|
||||
inject([AsyncTestCompleter],
|
||||
(async) => {tcb.createAsync(DynamicLoaderCmp)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/', component: HelloCmp})]))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('{ }');
|
||||
return fixture.componentInstance.onSomeAction();
|
||||
})
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
return rtr.navigateByUrl('/');
|
||||
})
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('{ hello }');
|
||||
|
||||
return fixture.componentInstance.onSomeAction();
|
||||
})
|
||||
.then((_) => {
|
||||
|
||||
// TODO(i): This should be rewritten to use NgZone#onStable or
|
||||
// something
|
||||
// similar basically the assertion needs to run when the world is
|
||||
// stable and we don't know when that is, only zones know.
|
||||
PromiseWrapper.resolve(null).then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('{ hello }');
|
||||
async.done();
|
||||
});
|
||||
})}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function registerSpecs() {
|
||||
specs['syncRoutesWithoutChildrenWithoutParams'] = syncRoutesWithoutChildrenWithoutParams;
|
||||
specs['syncRoutesWithoutChildrenWithParams'] = syncRoutesWithoutChildrenWithParams;
|
||||
specs['syncRoutesWithSyncChildrenWithoutDefaultRoutesWithoutParams'] =
|
||||
syncRoutesWithSyncChildrenWithoutDefaultRoutesWithoutParams;
|
||||
specs['syncRoutesWithSyncChildrenWithoutDefaultRoutesWithParams'] =
|
||||
syncRoutesWithSyncChildrenWithoutDefaultRoutesWithParams;
|
||||
specs['syncRoutesWithSyncChildrenWithDefaultRoutesWithoutParams'] =
|
||||
syncRoutesWithSyncChildrenWithDefaultRoutesWithoutParams;
|
||||
specs['syncRoutesWithDynamicComponents'] = syncRoutesWithDynamicComponents;
|
||||
}
|
||||
@@ -1,607 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {provide, Component, Injector, Inject} from '@angular/core';
|
||||
import {isPresent} from '../../src/facade/lang';
|
||||
import {
|
||||
PromiseWrapper,
|
||||
PromiseCompleter,
|
||||
EventEmitter,
|
||||
ObservableWrapper
|
||||
} from '../../src/facade/async';
|
||||
|
||||
import {Router, RouterOutlet, RouterLink, RouteParams} from '@angular/router';
|
||||
import {
|
||||
RouteConfig,
|
||||
Route,
|
||||
AuxRoute,
|
||||
AsyncRoute,
|
||||
Redirect
|
||||
} from '../../../router/src/route_config/route_config_decorator';
|
||||
|
||||
import {
|
||||
OnActivate,
|
||||
OnDeactivate,
|
||||
OnReuse,
|
||||
CanDeactivate,
|
||||
CanReuse
|
||||
} from '../../../router/src/interfaces';
|
||||
import {CanActivate} from '../../../router/src/lifecycle/lifecycle_annotations';
|
||||
import {ComponentInstruction} from '../../../router/src/instruction';
|
||||
|
||||
|
||||
import {TEST_ROUTER_PROVIDERS, RootCmp, compile} from './util';
|
||||
|
||||
var cmpInstanceCount;
|
||||
var log: string[];
|
||||
var eventBus: EventEmitter<any>;
|
||||
var completer: PromiseCompleter<any>;
|
||||
|
||||
export function main() {
|
||||
describe('Router lifecycle hooks', () => {
|
||||
|
||||
var tcb: TestComponentBuilder;
|
||||
var fixture: ComponentFixture<any>;
|
||||
var rtr: Router;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router],
|
||||
(tcBuilder: TestComponentBuilder, router: Router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
cmpInstanceCount = 0;
|
||||
log = [];
|
||||
eventBus = new EventEmitter();
|
||||
}));
|
||||
|
||||
it('should call the routerOnActivate hook', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/on-activate'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('activate cmp');
|
||||
expect(log).toEqual(['activate: null -> /on-activate']);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should wait for a parent component\'s routerOnActivate hook to resolve before calling its child\'s',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => {
|
||||
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
|
||||
if (ev.startsWith('parent activate')) {
|
||||
completer.resolve(true);
|
||||
}
|
||||
});
|
||||
rtr.navigateByUrl('/parent-activate/child-activate')
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('parent {activate cmp}');
|
||||
expect(log).toEqual([
|
||||
'parent activate: null -> /parent-activate',
|
||||
'activate: null -> /child-activate'
|
||||
]);
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should call the routerOnDeactivate hook', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/on-deactivate'))
|
||||
.then((_) => rtr.navigateByUrl('/a'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('A');
|
||||
expect(log).toEqual(['deactivate: /on-deactivate -> /a']);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should wait for a child component\'s routerOnDeactivate hook to resolve before calling its parent\'s',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/parent-deactivate/child-deactivate'))
|
||||
.then((_) => {
|
||||
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
|
||||
if (ev.startsWith('deactivate')) {
|
||||
completer.resolve(true);
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('parent {deactivate cmp}');
|
||||
}
|
||||
});
|
||||
rtr.navigateByUrl('/a').then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('A');
|
||||
expect(log).toEqual([
|
||||
'deactivate: /child-deactivate -> null',
|
||||
'parent deactivate: /parent-deactivate -> /a'
|
||||
]);
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should reuse a component when the routerCanReuse hook returns true',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/on-reuse/1/a'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(log).toEqual([]);
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('reuse {A}');
|
||||
expect(cmpInstanceCount).toBe(1);
|
||||
})
|
||||
.then((_) => rtr.navigateByUrl('/on-reuse/2/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(log).toEqual(['reuse: /on-reuse/1 -> /on-reuse/2']);
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('reuse {B}');
|
||||
expect(cmpInstanceCount).toBe(1);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should not reuse a component when the routerCanReuse hook returns false',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/never-reuse/1/a'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(log).toEqual([]);
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('reuse {A}');
|
||||
expect(cmpInstanceCount).toBe(1);
|
||||
})
|
||||
.then((_) => rtr.navigateByUrl('/never-reuse/2/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(log).toEqual([]);
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('reuse {B}');
|
||||
expect(cmpInstanceCount).toBe(2);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should navigate when routerCanActivate returns true',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => {
|
||||
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
|
||||
if (ev.startsWith('routerCanActivate')) {
|
||||
completer.resolve(true);
|
||||
}
|
||||
});
|
||||
rtr.navigateByUrl('/can-activate/a')
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('routerCanActivate {A}');
|
||||
expect(log).toEqual(['routerCanActivate: null -> /can-activate']);
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should not navigate when routerCanActivate returns false',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => {
|
||||
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
|
||||
if (ev.startsWith('routerCanActivate')) {
|
||||
completer.resolve(false);
|
||||
}
|
||||
});
|
||||
rtr.navigateByUrl('/can-activate/a')
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('');
|
||||
expect(log).toEqual(['routerCanActivate: null -> /can-activate']);
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate away when routerCanDeactivate returns true',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/can-deactivate/a'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('routerCanDeactivate {A}');
|
||||
expect(log).toEqual([]);
|
||||
|
||||
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
|
||||
if (ev.startsWith('routerCanDeactivate')) {
|
||||
completer.resolve(true);
|
||||
}
|
||||
});
|
||||
|
||||
rtr.navigateByUrl('/a').then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('A');
|
||||
expect(log).toEqual(['routerCanDeactivate: /can-deactivate -> /a']);
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should not navigate away when routerCanDeactivate returns false',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/can-deactivate/a'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('routerCanDeactivate {A}');
|
||||
expect(log).toEqual([]);
|
||||
|
||||
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
|
||||
if (ev.startsWith('routerCanDeactivate')) {
|
||||
completer.resolve(false);
|
||||
}
|
||||
});
|
||||
|
||||
rtr.navigateByUrl('/a').then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('routerCanDeactivate {A}');
|
||||
expect(log).toEqual(['routerCanDeactivate: /can-deactivate -> /a']);
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should run activation and deactivation hooks in the correct order',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/activation-hooks/child'))
|
||||
.then((_) => {
|
||||
expect(log).toEqual([
|
||||
'routerCanActivate child: null -> /child',
|
||||
'routerCanActivate parent: null -> /activation-hooks',
|
||||
'routerOnActivate parent: null -> /activation-hooks',
|
||||
'routerOnActivate child: null -> /child'
|
||||
]);
|
||||
|
||||
log = [];
|
||||
return rtr.navigateByUrl('/a');
|
||||
})
|
||||
.then((_) => {
|
||||
expect(log).toEqual([
|
||||
'routerCanDeactivate parent: /activation-hooks -> /a',
|
||||
'routerCanDeactivate child: /child -> null',
|
||||
'routerOnDeactivate child: /child -> null',
|
||||
'routerOnDeactivate parent: /activation-hooks -> /a'
|
||||
]);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should only run reuse hooks when reusing', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/reuse-hooks/1'))
|
||||
.then((_) => {
|
||||
expect(log).toEqual([
|
||||
'routerCanActivate: null -> /reuse-hooks/1',
|
||||
'routerOnActivate: null -> /reuse-hooks/1'
|
||||
]);
|
||||
|
||||
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
|
||||
if (ev.startsWith('routerCanReuse')) {
|
||||
completer.resolve(true);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
log = [];
|
||||
return rtr.navigateByUrl('/reuse-hooks/2');
|
||||
})
|
||||
.then((_) => {
|
||||
expect(log).toEqual([
|
||||
'routerCanReuse: /reuse-hooks/1 -> /reuse-hooks/2',
|
||||
'routerOnReuse: /reuse-hooks/1 -> /reuse-hooks/2'
|
||||
]);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should not run reuse hooks when not reusing', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: LifecycleCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/reuse-hooks/1'))
|
||||
.then((_) => {
|
||||
expect(log).toEqual([
|
||||
'routerCanActivate: null -> /reuse-hooks/1',
|
||||
'routerOnActivate: null -> /reuse-hooks/1'
|
||||
]);
|
||||
|
||||
ObservableWrapper.subscribe<string>(eventBus, (ev) => {
|
||||
if (ev.startsWith('routerCanReuse')) {
|
||||
completer.resolve(false);
|
||||
}
|
||||
});
|
||||
|
||||
log = [];
|
||||
return rtr.navigateByUrl('/reuse-hooks/2');
|
||||
})
|
||||
.then((_) => {
|
||||
expect(log).toEqual([
|
||||
'routerCanReuse: /reuse-hooks/1 -> /reuse-hooks/2',
|
||||
'routerCanActivate: /reuse-hooks/1 -> /reuse-hooks/2',
|
||||
'routerCanDeactivate: /reuse-hooks/1 -> /reuse-hooks/2',
|
||||
'routerOnDeactivate: /reuse-hooks/1 -> /reuse-hooks/2',
|
||||
'routerOnActivate: /reuse-hooks/1 -> /reuse-hooks/2'
|
||||
]);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Component({selector: 'a-cmp', template: "A"})
|
||||
class A {
|
||||
}
|
||||
|
||||
|
||||
@Component({selector: 'b-cmp', template: "B"})
|
||||
class B {
|
||||
}
|
||||
|
||||
|
||||
function logHook(name: string, next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
var message = name + ': ' + (isPresent(prev) ? ('/' + prev.urlPath) : 'null') + ' -> ' +
|
||||
(isPresent(next) ? ('/' + next.urlPath) : 'null');
|
||||
log.push(message);
|
||||
ObservableWrapper.callEmit(eventBus, message);
|
||||
}
|
||||
|
||||
@Component({selector: 'activate-cmp', template: 'activate cmp'})
|
||||
class ActivateCmp implements OnActivate {
|
||||
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('activate', next, prev);
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent-activate-cmp',
|
||||
template: `parent {<router-outlet></router-outlet>}`,
|
||||
directives: [RouterOutlet]
|
||||
})
|
||||
@RouteConfig([new Route({path: '/child-activate', component: ActivateCmp})])
|
||||
class ParentActivateCmp implements OnActivate {
|
||||
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction): Promise<any> {
|
||||
completer = PromiseWrapper.completer();
|
||||
logHook('parent activate', next, prev);
|
||||
return completer.promise;
|
||||
}
|
||||
}
|
||||
|
||||
@Component({selector: 'deactivate-cmp', template: 'deactivate cmp'})
|
||||
class DeactivateCmp implements OnDeactivate {
|
||||
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('deactivate', next, prev);
|
||||
}
|
||||
}
|
||||
|
||||
@Component({selector: 'deactivate-cmp', template: 'deactivate cmp'})
|
||||
class WaitDeactivateCmp implements OnDeactivate {
|
||||
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction): Promise<any> {
|
||||
completer = PromiseWrapper.completer();
|
||||
logHook('deactivate', next, prev);
|
||||
return completer.promise;
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent-deactivate-cmp',
|
||||
template: `parent {<router-outlet></router-outlet>}`,
|
||||
directives: [RouterOutlet]
|
||||
})
|
||||
@RouteConfig([new Route({path: '/child-deactivate', component: WaitDeactivateCmp})])
|
||||
class ParentDeactivateCmp implements OnDeactivate {
|
||||
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('parent deactivate', next, prev);
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'reuse-cmp',
|
||||
template: `reuse {<router-outlet></router-outlet>}`,
|
||||
directives: [RouterOutlet]
|
||||
})
|
||||
@RouteConfig([new Route({path: '/a', component: A}), new Route({path: '/b', component: B})])
|
||||
class ReuseCmp implements OnReuse,
|
||||
CanReuse {
|
||||
constructor() { cmpInstanceCount += 1; }
|
||||
routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction) { return true; }
|
||||
routerOnReuse(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('reuse', next, prev);
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'never-reuse-cmp',
|
||||
template: `reuse {<router-outlet></router-outlet>}`,
|
||||
directives: [RouterOutlet]
|
||||
})
|
||||
@RouteConfig([new Route({path: '/a', component: A}), new Route({path: '/b', component: B})])
|
||||
class NeverReuseCmp implements OnReuse,
|
||||
CanReuse {
|
||||
constructor() { cmpInstanceCount += 1; }
|
||||
routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction) { return false; }
|
||||
routerOnReuse(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('reuse', next, prev);
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'can-activate-cmp',
|
||||
template: `routerCanActivate {<router-outlet></router-outlet>}`,
|
||||
directives: [RouterOutlet]
|
||||
})
|
||||
@RouteConfig([new Route({path: '/a', component: A}), new Route({path: '/b', component: B})])
|
||||
@CanActivate(CanActivateCmp.routerCanActivate)
|
||||
class CanActivateCmp {
|
||||
static routerCanActivate(next: ComponentInstruction,
|
||||
prev: ComponentInstruction): Promise<boolean> {
|
||||
completer = PromiseWrapper.completer();
|
||||
logHook('routerCanActivate', next, prev);
|
||||
return completer.promise;
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'can-deactivate-cmp',
|
||||
template: `routerCanDeactivate {<router-outlet></router-outlet>}`,
|
||||
directives: [RouterOutlet]
|
||||
})
|
||||
@RouteConfig([new Route({path: '/a', component: A}), new Route({path: '/b', component: B})])
|
||||
class CanDeactivateCmp implements CanDeactivate {
|
||||
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction): Promise<boolean> {
|
||||
completer = PromiseWrapper.completer();
|
||||
logHook('routerCanDeactivate', next, prev);
|
||||
return completer.promise;
|
||||
}
|
||||
}
|
||||
|
||||
@Component({selector: 'all-hooks-child-cmp', template: `child`})
|
||||
@CanActivate(AllHooksChildCmp.routerCanActivate)
|
||||
class AllHooksChildCmp implements CanDeactivate, OnDeactivate, OnActivate {
|
||||
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerCanDeactivate child', next, prev);
|
||||
return true;
|
||||
}
|
||||
|
||||
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerOnDeactivate child', next, prev);
|
||||
}
|
||||
|
||||
static routerCanActivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerCanActivate child', next, prev);
|
||||
return true;
|
||||
}
|
||||
|
||||
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerOnActivate child', next, prev);
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'all-hooks-parent-cmp',
|
||||
template: `<router-outlet></router-outlet>`,
|
||||
directives: [RouterOutlet]
|
||||
})
|
||||
@RouteConfig([new Route({path: '/child', component: AllHooksChildCmp})])
|
||||
@CanActivate(AllHooksParentCmp.routerCanActivate)
|
||||
class AllHooksParentCmp implements CanDeactivate,
|
||||
OnDeactivate, OnActivate {
|
||||
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerCanDeactivate parent', next, prev);
|
||||
return true;
|
||||
}
|
||||
|
||||
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerOnDeactivate parent', next, prev);
|
||||
}
|
||||
|
||||
static routerCanActivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerCanActivate parent', next, prev);
|
||||
return true;
|
||||
}
|
||||
|
||||
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerOnActivate parent', next, prev);
|
||||
}
|
||||
}
|
||||
|
||||
@Component({selector: 'reuse-hooks-cmp', template: 'reuse hooks cmp'})
|
||||
@CanActivate(ReuseHooksCmp.routerCanActivate)
|
||||
class ReuseHooksCmp implements OnActivate, OnReuse, OnDeactivate, CanReuse, CanDeactivate {
|
||||
routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction): Promise<any> {
|
||||
completer = PromiseWrapper.completer();
|
||||
logHook('routerCanReuse', next, prev);
|
||||
return completer.promise;
|
||||
}
|
||||
|
||||
routerOnReuse(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerOnReuse', next, prev);
|
||||
}
|
||||
|
||||
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerCanDeactivate', next, prev);
|
||||
return true;
|
||||
}
|
||||
|
||||
routerOnDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerOnDeactivate', next, prev);
|
||||
}
|
||||
|
||||
static routerCanActivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerCanActivate', next, prev);
|
||||
return true;
|
||||
}
|
||||
|
||||
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) {
|
||||
logHook('routerOnActivate', next, prev);
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'lifecycle-cmp',
|
||||
template: `<router-outlet></router-outlet>`,
|
||||
directives: [RouterOutlet]
|
||||
})
|
||||
@RouteConfig([
|
||||
new Route({path: '/a', component: A}),
|
||||
new Route({path: '/on-activate', component: ActivateCmp}),
|
||||
new Route({path: '/parent-activate/...', component: ParentActivateCmp}),
|
||||
new Route({path: '/on-deactivate', component: DeactivateCmp}),
|
||||
new Route({path: '/parent-deactivate/...', component: ParentDeactivateCmp}),
|
||||
new Route({path: '/on-reuse/:number/...', component: ReuseCmp}),
|
||||
new Route({path: '/never-reuse/:number/...', component: NeverReuseCmp}),
|
||||
new Route({path: '/can-activate/...', component: CanActivateCmp}),
|
||||
new Route({path: '/can-deactivate/...', component: CanDeactivateCmp}),
|
||||
new Route({path: '/activation-hooks/...', component: AllHooksParentCmp}),
|
||||
new Route({path: '/reuse-hooks/:number', component: ReuseHooksCmp})
|
||||
])
|
||||
class LifecycleCmp {
|
||||
}
|
||||
@@ -1,296 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {provide, Component, Injector, Inject} from '@angular/core';
|
||||
import {Location} from '@angular/common';
|
||||
import {PromiseWrapper, TimerWrapper} from '../../src/facade/async';
|
||||
|
||||
import {Router, RouterOutlet, RouterLink, RouteParams, RouteData} from '@angular/router';
|
||||
import {
|
||||
RouteConfig,
|
||||
Route,
|
||||
AuxRoute,
|
||||
AsyncRoute,
|
||||
Redirect
|
||||
} from '../../../router/src/route_config/route_config_decorator';
|
||||
|
||||
import {TEST_ROUTER_PROVIDERS, RootCmp, compile} from './util';
|
||||
|
||||
var cmpInstanceCount;
|
||||
var childCmpInstanceCount;
|
||||
|
||||
export function main() {
|
||||
describe('navigation', () => {
|
||||
|
||||
var tcb: TestComponentBuilder;
|
||||
var fixture: ComponentFixture<any>;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
childCmpInstanceCount = 0;
|
||||
cmpInstanceCount = 0;
|
||||
}));
|
||||
|
||||
it('should work in a simple case', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/test', component: HelloCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/test'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should navigate between components with different parameters',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/user/:name', component: UserCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/user/brian'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello brian');
|
||||
})
|
||||
.then((_) => rtr.navigateByUrl('/user/igor'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('hello igor');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate to child routes', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, 'outer { <router-outlet></router-outlet> }')
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/a/...', component: ParentCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/a/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate to child routes that capture an empty path',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
|
||||
compile(tcb, 'outer { <router-outlet></router-outlet> }')
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/a/...', component: ParentCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/a'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate to child routes when the root component has an empty path',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb, 'outer { <router-outlet></router-outlet> }')
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/...', component: ParentCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
expect(location.urlChanges).toEqual(['/b']);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate to child routes of async routes', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, 'outer { <router-outlet></router-outlet> }')
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new AsyncRoute({path: '/a/...', loader: parentLoader})]))
|
||||
.then((_) => rtr.navigateByUrl('/a/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('outer { inner { hello } }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should reuse common parent components', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/team/:id/...', component: TeamCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/team/angular/user/rado'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(cmpInstanceCount).toBe(1);
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('team angular { hello rado }');
|
||||
})
|
||||
.then((_) => rtr.navigateByUrl('/team/angular/user/victor'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(cmpInstanceCount).toBe(1);
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('team angular { hello victor }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should not reuse children when parent components change',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/team/:id/...', component: TeamCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/team/angular/user/rado'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(cmpInstanceCount).toBe(1);
|
||||
expect(childCmpInstanceCount).toBe(1);
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('team angular { hello rado }');
|
||||
})
|
||||
.then((_) => rtr.navigateByUrl('/team/dart/user/rado'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(cmpInstanceCount).toBe(2);
|
||||
expect(childCmpInstanceCount).toBe(2);
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('team dart { hello rado }');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should inject route data into component', inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/route-data', component: RouteDataCmp, data: {isAdmin: true}})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/route-data'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('true');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should inject route data into component with AsyncRoute',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new AsyncRoute(
|
||||
{path: '/route-data', loader: asyncRouteDataCmp, data: {isAdmin: true}})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/route-data'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('true');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should inject empty object if the route has no data property',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config(
|
||||
[new Route({path: '/route-data-default', component: RouteDataCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/route-data-default'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should fire an event for each activated component',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile(tcb, '<router-outlet (activate)="activatedCmp = $event"></router-outlet>')
|
||||
.then((rtc) => {fixture = rtc})
|
||||
.then((_) => rtr.config([new Route({path: '/test', component: HelloCmp})]))
|
||||
.then((_) => rtr.navigateByUrl('/test'))
|
||||
.then((_) => {
|
||||
// Note: need a timeout so that all promises are flushed
|
||||
var completer = PromiseWrapper.completer();
|
||||
TimerWrapper.setTimeout(() => { completer.resolve(null); }, 0);
|
||||
return completer.promise;
|
||||
})
|
||||
.then((_) => {
|
||||
expect(fixture.componentInstance.activatedCmp).toBeAnInstanceOf(HelloCmp);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Component({selector: 'hello-cmp', template: `{{greeting}}`})
|
||||
class HelloCmp {
|
||||
greeting: string;
|
||||
constructor() { this.greeting = 'hello'; }
|
||||
}
|
||||
|
||||
|
||||
function asyncRouteDataCmp() {
|
||||
return PromiseWrapper.resolve(RouteDataCmp);
|
||||
}
|
||||
|
||||
@Component({selector: 'data-cmp', template: `{{myData}}`})
|
||||
class RouteDataCmp {
|
||||
myData: boolean;
|
||||
constructor(data: RouteData) { this.myData = data.get('isAdmin'); }
|
||||
}
|
||||
|
||||
@Component({selector: 'user-cmp', template: `hello {{user}}`})
|
||||
class UserCmp {
|
||||
user: string;
|
||||
constructor(params: RouteParams) {
|
||||
childCmpInstanceCount += 1;
|
||||
this.user = params.get('name');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function parentLoader() {
|
||||
return PromiseWrapper.resolve(ParentCmp);
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent-cmp',
|
||||
template: `inner { <router-outlet></router-outlet> }`,
|
||||
directives: [RouterOutlet],
|
||||
})
|
||||
@RouteConfig([
|
||||
new Route({path: '/b', component: HelloCmp}),
|
||||
new Route({path: '/', component: HelloCmp}),
|
||||
])
|
||||
class ParentCmp {
|
||||
}
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'team-cmp',
|
||||
template: `team {{id}} { <router-outlet></router-outlet> }`,
|
||||
directives: [RouterOutlet],
|
||||
})
|
||||
@RouteConfig([new Route({path: '/user/:name', component: UserCmp})])
|
||||
class TeamCmp {
|
||||
id: string;
|
||||
constructor(params: RouteParams) {
|
||||
this.id = params.get('id');
|
||||
cmpInstanceCount += 1;
|
||||
}
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {Router, RouterOutlet, RouterLink, RouteParams, RouteData} from '@angular/router';
|
||||
import {
|
||||
RouteConfig,
|
||||
Route,
|
||||
AuxRoute,
|
||||
AsyncRoute,
|
||||
Redirect
|
||||
} from '../../../router/src/route_config/route_config_decorator';
|
||||
import {Location} from '@angular/common';
|
||||
|
||||
import {TEST_ROUTER_PROVIDERS, RootCmp, compile} from './util';
|
||||
import {HelloCmp, GoodbyeCmp, RedirectToParentCmp} from './impl/fixture_components';
|
||||
|
||||
var cmpInstanceCount;
|
||||
var childCmpInstanceCount;
|
||||
|
||||
export function main() {
|
||||
describe('redirects', () => {
|
||||
|
||||
var tcb: TestComponentBuilder;
|
||||
var rootTC: ComponentFixture<any>;
|
||||
var rtr;
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
|
||||
tcb = tcBuilder;
|
||||
rtr = router;
|
||||
childCmpInstanceCount = 0;
|
||||
cmpInstanceCount = 0;
|
||||
}));
|
||||
|
||||
|
||||
it('should apply when navigating by URL',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Redirect({path: '/original', redirectTo: ['Hello']}),
|
||||
new Route({path: '/redirected', component: HelloCmp, name: 'Hello'})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/original'))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('hello');
|
||||
expect(location.urlChanges).toEqual(['/redirected']);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should recognize and apply absolute redirects',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Redirect({path: '/original', redirectTo: ['/Hello']}),
|
||||
new Route({path: '/redirected', component: HelloCmp, name: 'Hello'})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/original'))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('hello');
|
||||
expect(location.urlChanges).toEqual(['/redirected']);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should recognize and apply relative child redirects',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Redirect({path: '/original', redirectTo: ['./Hello']}),
|
||||
new Route({path: '/redirected', component: HelloCmp, name: 'Hello'})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/original'))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('hello');
|
||||
expect(location.urlChanges).toEqual(['/redirected']);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should recognize and apply relative parent redirects',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/original/...', component: RedirectToParentCmp}),
|
||||
new Route({path: '/redirected', component: HelloCmp, name: 'HelloSib'})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/original/child-redirect'))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('hello');
|
||||
expect(location.urlChanges).toEqual(['/redirected']);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should not redirect when redirect is less specific than other matching routes',
|
||||
inject([AsyncTestCompleter, Location], (async, location) => {
|
||||
compile(tcb)
|
||||
.then((rtc) => {rootTC = rtc})
|
||||
.then((_) => rtr.config([
|
||||
new Route({path: '/foo', component: HelloCmp, name: 'Hello'}),
|
||||
new Route({path: '/:param', component: GoodbyeCmp, name: 'Goodbye'}),
|
||||
new Redirect({path: '/*rest', redirectTo: ['/Hello']})
|
||||
]))
|
||||
.then((_) => rtr.navigateByUrl('/bye'))
|
||||
.then((_) => {
|
||||
rootTC.detectChanges();
|
||||
expect(rootTC.debugElement.nativeElement).toHaveText('goodbye');
|
||||
expect(location.urlChanges).toEqual(['/bye']);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
});
|
||||
}
|
||||
@@ -1,472 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
||||
|
||||
import {Location} from '@angular/common';
|
||||
import {NumberWrapper} from '../../src/facade/lang';
|
||||
import {PromiseWrapper} from '../../src/facade/async';
|
||||
import {ListWrapper} from '../../src/facade/collection';
|
||||
|
||||
import {provide, Component} from '@angular/core';
|
||||
|
||||
import {
|
||||
Router,
|
||||
RouteRegistry,
|
||||
RouterLink,
|
||||
RouterOutlet,
|
||||
AsyncRoute,
|
||||
AuxRoute,
|
||||
Route,
|
||||
RouteParams,
|
||||
RouteConfig,
|
||||
ROUTER_DIRECTIVES,
|
||||
ROUTER_PRIMARY_COMPONENT
|
||||
} from '@angular/router';
|
||||
import {RootRouter} from '@angular/router/src/router';
|
||||
|
||||
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
||||
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
||||
import {SpyLocation} from '@angular/common/testing';
|
||||
|
||||
export function main() {
|
||||
describe('routerLink directive', function() {
|
||||
var tcb: TestComponentBuilder;
|
||||
var fixture: ComponentFixture<any>;
|
||||
var router: Router;
|
||||
var location: Location;
|
||||
|
||||
beforeEachProviders(() => [
|
||||
RouteRegistry,
|
||||
provide(Location, {useClass: SpyLocation}),
|
||||
provide(ROUTER_PRIMARY_COMPONENT, {useValue: MyComp7}),
|
||||
provide(Router, {useClass: RootRouter}),
|
||||
]);
|
||||
|
||||
beforeEach(inject([TestComponentBuilder, Router, Location],
|
||||
(tcBuilder, rtr: Router, loc: Location) => {
|
||||
tcb = tcBuilder;
|
||||
router = rtr;
|
||||
location = loc;
|
||||
}));
|
||||
|
||||
function compile(template: string = "<router-outlet></router-outlet>") {
|
||||
return tcb.overrideTemplate(MyComp7, ('<div>' + template + '</div>'))
|
||||
.createAsync(MyComp7)
|
||||
.then((tc) => { fixture = tc; });
|
||||
}
|
||||
|
||||
it('should generate absolute hrefs that include the base href',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
(<SpyLocation>location).setBaseHref('/my/base');
|
||||
compile('<a href="hello" [routerLink]="[\'./User\']"></a>')
|
||||
.then((_) => router.config(
|
||||
[new Route({path: '/user', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => router.navigateByUrl('/a/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(fixture)).toEqual('/my/base/user');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should generate link hrefs without params', inject([AsyncTestCompleter], (async) => {
|
||||
compile('<a href="hello" [routerLink]="[\'./User\']"></a>')
|
||||
.then((_) => router.config(
|
||||
[new Route({path: '/user', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => router.navigateByUrl('/a/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(fixture)).toEqual('/user');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should generate link hrefs with params', inject([AsyncTestCompleter], (async) => {
|
||||
compile('<a href="hello" [routerLink]="[\'./User\', {name: name}]">{{name}}</a>')
|
||||
.then((_) => router.config(
|
||||
[new Route({path: '/user/:name', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => router.navigateByUrl('/a/b'))
|
||||
.then((_) => {
|
||||
fixture.debugElement.componentInstance.name = 'brian';
|
||||
fixture.detectChanges();
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('brian');
|
||||
expect(getHref(fixture)).toEqual('/user/brian');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate link hrefs from a child to its sibling',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile()
|
||||
.then(
|
||||
(_) => router.config(
|
||||
[new Route({path: '/page/:number', component: SiblingPageCmp, name: 'Page'})]))
|
||||
.then((_) => router.navigateByUrl('/page/1'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(fixture)).toEqual('/page/2');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate link hrefs from a child to its sibling with no leading slash',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile()
|
||||
.then((_) => router.config([
|
||||
new Route(
|
||||
{path: '/page/:number', component: NoPrefixSiblingPageCmp, name: 'Page'})
|
||||
]))
|
||||
.then((_) => router.navigateByUrl('/page/1'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(fixture)).toEqual('/page/2');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate link hrefs to a child with no leading slash',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile()
|
||||
.then((_) => router.config([
|
||||
new Route({path: '/book/:title/...', component: NoPrefixBookCmp, name: 'Book'})
|
||||
]))
|
||||
.then((_) => router.navigateByUrl('/book/1984/page/1'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(fixture)).toEqual('/book/1984/page/100');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should throw when links without a leading slash are ambiguous',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile()
|
||||
.then((_) => router.config([
|
||||
new Route({path: '/book/:title/...', component: AmbiguousBookCmp, name: 'Book'})
|
||||
]))
|
||||
.then((_) => router.navigateByUrl('/book/1984/page/1'))
|
||||
.then((_) => {
|
||||
var link = ListWrapper.toJSON(['Book', {number: 100}]);
|
||||
expect(() => fixture.detectChanges())
|
||||
.toThrowErrorWith(
|
||||
`Link "${link}" is ambiguous, use "./" or "../" to disambiguate.`);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate link hrefs when asynchronously loaded',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile()
|
||||
.then((_) => router.config([
|
||||
new AsyncRoute({
|
||||
path: '/child-with-grandchild/...',
|
||||
loader: parentCmpLoader,
|
||||
name: 'ChildWithGrandchild'
|
||||
})
|
||||
]))
|
||||
.then((_) => router.navigateByUrl('/child-with-grandchild/grandchild'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(fixture)).toEqual('/child-with-grandchild/grandchild');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate relative links preserving the existing parent route',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
compile()
|
||||
.then((_) => router.config(
|
||||
[new Route({path: '/book/:title/...', component: BookCmp, name: 'Book'})]))
|
||||
.then((_) => router.navigateByUrl('/book/1984/page/1'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
// TODO(juliemr): This should be one By.css('book-cmp a') query, but the parse5
|
||||
// adapter
|
||||
// can't handle css child selectors.
|
||||
expect(getDOM().getAttribute(fixture.debugElement.query(By.css('book-cmp'))
|
||||
.query(By.css('a'))
|
||||
.nativeElement,
|
||||
'href'))
|
||||
.toEqual('/book/1984/page/100');
|
||||
|
||||
expect(getDOM().getAttribute(fixture.debugElement.query(By.css('page-cmp'))
|
||||
.query(By.css('a'))
|
||||
.nativeElement,
|
||||
'href'))
|
||||
.toEqual('/book/1984/page/2');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate links to auxiliary routes', inject([AsyncTestCompleter], (async) => {
|
||||
compile()
|
||||
.then((_) => router.config([new Route({path: '/...', component: AuxLinkCmp})]))
|
||||
.then((_) => router.navigateByUrl('/'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
expect(getHref(fixture)).toEqual('/(aside)');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
describe('router-link-active CSS class', () => {
|
||||
it('should be added to the associated element', inject([AsyncTestCompleter], (async) => {
|
||||
router.config([
|
||||
new Route({path: '/child', component: HelloCmp, name: 'Child'}),
|
||||
new Route({path: '/better-child', component: Hello2Cmp, name: 'BetterChild'})
|
||||
])
|
||||
.then((_) => compile(`<a [routerLink]="['./Child']" class="child-link">Child</a>
|
||||
<a [routerLink]="['./BetterChild']" class="better-child-link">Better Child</a>
|
||||
<router-outlet></router-outlet>`))
|
||||
.then((_) => {
|
||||
var element = fixture.debugElement.nativeElement;
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
var link1 = getDOM().querySelector(element, '.child-link');
|
||||
var link2 = getDOM().querySelector(element, '.better-child-link');
|
||||
|
||||
expect(link1).not.toHaveCssClass('router-link-active');
|
||||
expect(link2).not.toHaveCssClass('router-link-active');
|
||||
|
||||
router.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(link1).not.toHaveCssClass('router-link-active');
|
||||
expect(link2).toHaveCssClass('router-link-active');
|
||||
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/better-child?extra=0');
|
||||
});
|
||||
}));
|
||||
|
||||
it('should be added to links in child routes', inject([AsyncTestCompleter], (async) => {
|
||||
router.config([
|
||||
new Route({path: '/child', component: HelloCmp, name: 'Child'}),
|
||||
new Route({
|
||||
path: '/child-with-grandchild/...',
|
||||
component: ParentCmp,
|
||||
name: 'ChildWithGrandchild'
|
||||
})
|
||||
])
|
||||
.then((_) => compile(`<a [routerLink]="['./Child']" class="child-link">Child</a>
|
||||
<a [routerLink]="['./ChildWithGrandchild/Grandchild']" class="child-with-grandchild-link">Better Child</a>
|
||||
<router-outlet></router-outlet>`))
|
||||
.then((_) => {
|
||||
var element = fixture.debugElement.nativeElement;
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
var link1 = getDOM().querySelector(element, '.child-link');
|
||||
var link2 = getDOM().querySelector(element, '.child-with-grandchild-link');
|
||||
|
||||
expect(link1).not.toHaveCssClass('router-link-active');
|
||||
expect(link2).not.toHaveCssClass('router-link-active');
|
||||
|
||||
router.subscribe((_) => {
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(link1).not.toHaveCssClass('router-link-active');
|
||||
expect(link2).toHaveCssClass('router-link-active');
|
||||
|
||||
var link3 = getDOM().querySelector(element, '.grandchild-link');
|
||||
var link4 = getDOM().querySelector(element, '.better-grandchild-link');
|
||||
|
||||
expect(link3).toHaveCssClass('router-link-active');
|
||||
expect(link4).not.toHaveCssClass('router-link-active');
|
||||
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/child-with-grandchild/grandchild?extra=0');
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
describe('when clicked', () => {
|
||||
|
||||
var clickOnElement = function(view) {
|
||||
var anchorEl = fixture.debugElement.query(By.css('a')).nativeElement;
|
||||
var dispatchedEvent = getDOM().createMouseEvent('click');
|
||||
getDOM().dispatchEvent(anchorEl, dispatchedEvent);
|
||||
return dispatchedEvent;
|
||||
};
|
||||
|
||||
it('should navigate to link hrefs without params', inject([AsyncTestCompleter], (async) => {
|
||||
compile('<a href="hello" [routerLink]="[\'./User\']"></a>')
|
||||
.then((_) => router.config(
|
||||
[new Route({path: '/user', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => router.navigateByUrl('/a/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
|
||||
var dispatchedEvent = clickOnElement(fixture);
|
||||
expect(getDOM().isPrevented(dispatchedEvent)).toBe(true);
|
||||
|
||||
// router navigation is async.
|
||||
router.subscribe((_) => {
|
||||
expect((<SpyLocation>location).urlChanges).toEqual(['/user']);
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate to link hrefs in presence of base href',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
(<SpyLocation>location).setBaseHref('/base');
|
||||
compile('<a href="hello" [routerLink]="[\'./User\']"></a>')
|
||||
.then((_) => router.config(
|
||||
[new Route({path: '/user', component: UserCmp, name: 'User'})]))
|
||||
.then((_) => router.navigateByUrl('/a/b'))
|
||||
.then((_) => {
|
||||
fixture.detectChanges();
|
||||
|
||||
|
||||
var dispatchedEvent = clickOnElement(fixture);
|
||||
expect(getDOM().isPrevented(dispatchedEvent)).toBe(true);
|
||||
|
||||
// router navigation is async.
|
||||
router.subscribe((_) => {
|
||||
expect((<SpyLocation>location).urlChanges).toEqual(['/base/user']);
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getHref(tc: ComponentFixture<any>) {
|
||||
return getDOM().getAttribute(tc.debugElement.query(By.css('a')).nativeElement, 'href');
|
||||
}
|
||||
|
||||
@Component({selector: 'my-comp', template: '', directives: [ROUTER_DIRECTIVES]})
|
||||
class MyComp7 {
|
||||
name;
|
||||
}
|
||||
|
||||
@Component({selector: 'user-cmp', template: "hello {{user}}"})
|
||||
class UserCmp {
|
||||
user: string;
|
||||
constructor(params: RouteParams) { this.user = params.get('name'); }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'page-cmp',
|
||||
template:
|
||||
`page #{{pageNumber}} | <a href="hello" [routerLink]="[\'../Page\', {number: nextPage}]">next</a>`,
|
||||
directives: [RouterLink]
|
||||
})
|
||||
class SiblingPageCmp {
|
||||
pageNumber: number;
|
||||
nextPage: number;
|
||||
constructor(params: RouteParams) {
|
||||
this.pageNumber = NumberWrapper.parseInt(params.get('number'), 10);
|
||||
this.nextPage = this.pageNumber + 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'page-cmp',
|
||||
template:
|
||||
`page #{{pageNumber}} | <a href="hello" [routerLink]="[\'Page\', {number: nextPage}]">next</a>`,
|
||||
directives: [RouterLink]
|
||||
})
|
||||
class NoPrefixSiblingPageCmp {
|
||||
pageNumber: number;
|
||||
nextPage: number;
|
||||
constructor(params: RouteParams) {
|
||||
this.pageNumber = NumberWrapper.parseInt(params.get('number'), 10);
|
||||
this.nextPage = this.pageNumber + 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Component({selector: 'hello-cmp', template: 'hello'})
|
||||
class HelloCmp {
|
||||
}
|
||||
|
||||
@Component({selector: 'hello2-cmp', template: 'hello2'})
|
||||
class Hello2Cmp {
|
||||
}
|
||||
|
||||
function parentCmpLoader() {
|
||||
return PromiseWrapper.resolve(ParentCmp);
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent-cmp',
|
||||
template: `{ <a [routerLink]="['./Grandchild']" class="grandchild-link">Grandchild</a>
|
||||
<a [routerLink]="['./BetterGrandchild']" class="better-grandchild-link">Better Grandchild</a>
|
||||
<router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([
|
||||
new Route({path: '/grandchild', component: HelloCmp, name: 'Grandchild'}),
|
||||
new Route({path: '/better-grandchild', component: Hello2Cmp, name: 'BetterGrandchild'})
|
||||
])
|
||||
class ParentCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'book-cmp',
|
||||
template: `<a href="hello" [routerLink]="[\'./Page\', {number: 100}]">{{title}}</a> |
|
||||
<router-outlet></router-outlet>`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([new Route({path: '/page/:number', component: SiblingPageCmp, name: 'Page'})])
|
||||
class BookCmp {
|
||||
title: string;
|
||||
constructor(params: RouteParams) { this.title = params.get('title'); }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'book-cmp',
|
||||
template: `<a href="hello" [routerLink]="[\'Page\', {number: 100}]">{{title}}</a> |
|
||||
<router-outlet></router-outlet>`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([new Route({path: '/page/:number', component: SiblingPageCmp, name: 'Page'})])
|
||||
class NoPrefixBookCmp {
|
||||
title: string;
|
||||
constructor(params: RouteParams) { this.title = params.get('title'); }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'book-cmp',
|
||||
template: `<a href="hello" [routerLink]="[\'Book\', {number: 100}]">{{title}}</a> |
|
||||
<router-outlet></router-outlet>`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([new Route({path: '/page/:number', component: SiblingPageCmp, name: 'Book'})])
|
||||
class AmbiguousBookCmp {
|
||||
title: string;
|
||||
constructor(params: RouteParams) { this.title = params.get('title'); }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'aux-cmp',
|
||||
template:
|
||||
`<a [routerLink]="[\'./Hello\', [ \'Aside\' ] ]">aside</a> |
|
||||
<router-outlet></router-outlet> | aside <router-outlet name="aside"></router-outlet>`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([
|
||||
new Route({path: '/', component: HelloCmp, name: 'Hello'}),
|
||||
new AuxRoute({path: '/aside', component: Hello2Cmp, name: 'Aside'})
|
||||
])
|
||||
class AuxLinkCmp {
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
import {
|
||||
describeRouter,
|
||||
ddescribeRouter,
|
||||
describeWith,
|
||||
describeWithout,
|
||||
describeWithAndWithout,
|
||||
itShouldRoute,
|
||||
TEST_ROUTER_PROVIDERS
|
||||
} from './util';
|
||||
|
||||
import {beforeEachProviders, describe, ddescribe} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {registerSpecs} from './impl/sync_route_spec_impl';
|
||||
|
||||
export function main() {
|
||||
describe('sync route spec', () => {
|
||||
|
||||
beforeEachProviders(() => TEST_ROUTER_PROVIDERS);
|
||||
|
||||
registerSpecs();
|
||||
|
||||
describeRouter('sync routes', () => {
|
||||
describeWithout('children', () => { describeWithAndWithout('params', itShouldRoute); });
|
||||
|
||||
describeWith('sync children', () => {
|
||||
describeWithout('default routes',
|
||||
() => { describeWithAndWithout('params', itShouldRoute); });
|
||||
describeWith('default routes', () => { describeWithout('params', itShouldRoute); });
|
||||
|
||||
});
|
||||
|
||||
describeWith('dynamic components', itShouldRoute);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {provide, Component} from '@angular/core';
|
||||
import {isBlank} from '../../src/facade/lang';
|
||||
import {BaseException} from '../../src/facade/exceptions';
|
||||
import {RootRouter} from '@angular/router/src/router';
|
||||
import {Router, ROUTER_DIRECTIVES, ROUTER_PRIMARY_COMPONENT} from '@angular/router';
|
||||
import {Location} from '@angular/common';
|
||||
import {RouteRegistry} from '@angular/router/src/route_registry';
|
||||
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
||||
import {SpyLocation} from '@angular/common/testing';
|
||||
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
||||
|
||||
|
||||
/**
|
||||
* Router test helpers and fixtures
|
||||
*/
|
||||
|
||||
@Component({
|
||||
selector: 'root-comp',
|
||||
template: `<router-outlet></router-outlet>`,
|
||||
directives: [ROUTER_DIRECTIVES]
|
||||
})
|
||||
export class RootCmp {
|
||||
name: string;
|
||||
activatedCmp: any;
|
||||
}
|
||||
|
||||
export function compile(
|
||||
tcb: TestComponentBuilder,
|
||||
template: string = "<router-outlet></router-outlet>"): Promise<ComponentFixture<RootCmp>> {
|
||||
return tcb.overrideTemplate(RootCmp, ('<div>' + template + '</div>')).createAsync(RootCmp);
|
||||
}
|
||||
|
||||
export var TEST_ROUTER_PROVIDERS: any[] = [
|
||||
RouteRegistry,
|
||||
provide(Location, {useClass: SpyLocation}),
|
||||
provide(ROUTER_PRIMARY_COMPONENT, {useValue: RootCmp}),
|
||||
provide(Router, {useClass: RootRouter})
|
||||
];
|
||||
|
||||
export function clickOnElement(anchorEl) {
|
||||
var dispatchedEvent = getDOM().createMouseEvent('click');
|
||||
getDOM().dispatchEvent(anchorEl, dispatchedEvent);
|
||||
return dispatchedEvent;
|
||||
}
|
||||
|
||||
export function getHref(elt) {
|
||||
return getDOM().getAttribute(elt, 'href');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Router integration suite DSL
|
||||
*/
|
||||
|
||||
var specNameBuilder = [];
|
||||
|
||||
// we add the specs themselves onto this map
|
||||
export var specs = {};
|
||||
|
||||
export function describeRouter(description: string, fn: Function, exclusive = false): void {
|
||||
var specName = descriptionToSpecName(description);
|
||||
specNameBuilder.push(specName);
|
||||
if (exclusive) {
|
||||
ddescribe(description, fn);
|
||||
} else {
|
||||
describe(description, fn);
|
||||
}
|
||||
specNameBuilder.pop();
|
||||
}
|
||||
|
||||
export function ddescribeRouter(description: string, fn: Function, exclusive = false): void {
|
||||
describeRouter(description, fn, true);
|
||||
}
|
||||
|
||||
export function describeWithAndWithout(description: string, fn: Function): void {
|
||||
// the "without" case is usually simpler, so we opt to run this spec first
|
||||
describeWithout(description, fn);
|
||||
describeWith(description, fn);
|
||||
}
|
||||
|
||||
export function describeWith(description: string, fn: Function): void {
|
||||
var specName = 'with ' + description;
|
||||
specNameBuilder.push(specName);
|
||||
describe(specName, fn);
|
||||
specNameBuilder.pop();
|
||||
}
|
||||
|
||||
export function describeWithout(description: string, fn: Function): void {
|
||||
var specName = 'without ' + description;
|
||||
specNameBuilder.push(specName);
|
||||
describe(specName, fn);
|
||||
specNameBuilder.pop();
|
||||
}
|
||||
|
||||
function descriptionToSpecName(description: string): string {
|
||||
return spaceCaseToCamelCase(description);
|
||||
}
|
||||
|
||||
// this helper looks up the suite registered from the "impl" folder in this directory
|
||||
export function itShouldRoute() {
|
||||
var specSuiteName = spaceCaseToCamelCase(specNameBuilder.join(' '));
|
||||
|
||||
var spec = specs[specSuiteName];
|
||||
if (isBlank(spec)) {
|
||||
throw new BaseException(`Router integration spec suite "${specSuiteName}" was not found.`);
|
||||
} else {
|
||||
// todo: remove spec from map, throw if there are extra left over??
|
||||
spec();
|
||||
}
|
||||
}
|
||||
|
||||
function spaceCaseToCamelCase(str: string): string {
|
||||
var words = str.split(' ');
|
||||
var first = words.shift();
|
||||
return first + words.map(title).join('');
|
||||
}
|
||||
|
||||
function title(str: string): string {
|
||||
return str[0].toUpperCase() + str.substring(1);
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
import {
|
||||
ComponentFixture,
|
||||
AsyncTestCompleter,
|
||||
TestComponentBuilder,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit,
|
||||
fakeAsync,
|
||||
tick
|
||||
} from 'angular2/testing_internal';
|
||||
import {provide, Component, ComponentResolver} from 'angular2/core';
|
||||
import {PromiseWrapper} from 'angular2/src/facade/async';
|
||||
|
||||
|
||||
import {
|
||||
Router,
|
||||
RouterOutletMap,
|
||||
RouteSegment,
|
||||
Route,
|
||||
ROUTER_DIRECTIVES,
|
||||
Routes,
|
||||
RouterUrlSerializer,
|
||||
DefaultRouterUrlSerializer,
|
||||
OnActivate,
|
||||
CanDeactivate
|
||||
} from 'angular2/alt_router';
|
||||
import {SpyLocation} from 'angular2/src/mock/location_mock';
|
||||
import {Location} from 'angular2/platform/common';
|
||||
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
||||
|
||||
export function main() {
|
||||
describe('navigation', () => {
|
||||
beforeEachProviders(() => [
|
||||
provide(RouterUrlSerializer, {useClass: DefaultRouterUrlSerializer}),
|
||||
RouterOutletMap,
|
||||
provide(Location, {useClass: SpyLocation}),
|
||||
provide(Router,
|
||||
{
|
||||
useFactory: (resolver, urlParser, outletMap, location) => new Router(
|
||||
"RootComponent", RootCmp, resolver, urlParser, outletMap, location),
|
||||
deps: [ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location]
|
||||
})
|
||||
]);
|
||||
|
||||
it('should update location when navigating',
|
||||
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
|
||||
router.navigateByUrl('/team/22/user/victor');
|
||||
advance(fixture);
|
||||
expect(location.path()).toEqual('/team/22/user/victor');
|
||||
|
||||
router.navigateByUrl('/team/33/simple');
|
||||
advance(fixture);
|
||||
|
||||
expect(location.path()).toEqual('/team/33/simple');
|
||||
})));
|
||||
|
||||
it('should navigate when locations changes',
|
||||
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
|
||||
router.navigateByUrl('/team/22/user/victor');
|
||||
advance(fixture);
|
||||
|
||||
location.simulateHashChange("/team/22/user/fedor");
|
||||
advance(fixture);
|
||||
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello fedor, aux: }');
|
||||
})));
|
||||
|
||||
it('should support nested routes',
|
||||
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
|
||||
router.navigateByUrl('/team/22/user/victor');
|
||||
advance(fixture);
|
||||
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello victor, aux: }');
|
||||
})));
|
||||
|
||||
it('should support aux routes',
|
||||
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
|
||||
router.navigateByUrl('/team/22/user/victor(/simple)');
|
||||
advance(fixture);
|
||||
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('team 22 { hello victor, aux: simple }');
|
||||
})));
|
||||
|
||||
it('should unload outlets', fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
|
||||
router.navigateByUrl('/team/22/user/victor(/simple)');
|
||||
advance(fixture);
|
||||
|
||||
router.navigateByUrl('/team/22/user/victor');
|
||||
advance(fixture);
|
||||
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello victor, aux: }');
|
||||
})));
|
||||
|
||||
it('should unload nested outlets',
|
||||
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
|
||||
router.navigateByUrl('/team/22/user/victor(/simple)');
|
||||
advance(fixture);
|
||||
|
||||
router.navigateByUrl('/');
|
||||
advance(fixture);
|
||||
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('');
|
||||
})));
|
||||
|
||||
it('should update nested routes when url changes',
|
||||
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
|
||||
router.navigateByUrl('/team/22/user/victor');
|
||||
advance(fixture);
|
||||
let team1 = fixture.debugElement.children[1].componentInstance;
|
||||
|
||||
router.navigateByUrl('/team/22/user/fedor');
|
||||
advance(fixture);
|
||||
let team2 = fixture.debugElement.children[1].componentInstance;
|
||||
|
||||
expect(team1).toBe(team2);
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello fedor, aux: }');
|
||||
})));
|
||||
|
||||
it('should not unload the route if can deactivate returns false',
|
||||
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
|
||||
router.navigateByUrl('/team/22/cannotDeactivate');
|
||||
advance(fixture);
|
||||
|
||||
router.navigateByUrl('/team/22/user/fedor');
|
||||
advance(fixture);
|
||||
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('team 22 { cannotDeactivate, aux: }');
|
||||
|
||||
expect(location.path()).toEqual('/team/22/cannotDeactivate');
|
||||
})));
|
||||
|
||||
if (DOM.supportsDOMEvents()) {
|
||||
it("should support absolute router links",
|
||||
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
advance(fixture);
|
||||
|
||||
router.navigateByUrl('/team/22/link');
|
||||
advance(fixture);
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { link, aux: }');
|
||||
|
||||
let native = DOM.querySelector(fixture.debugElement.nativeElement, "a");
|
||||
expect(DOM.getAttribute(native, "href")).toEqual("/team/33/simple");
|
||||
DOM.dispatchEvent(native, DOM.createMouseEvent('click'));
|
||||
advance(fixture);
|
||||
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('team 33 { simple, aux: }');
|
||||
})));
|
||||
|
||||
it("should support relative router links",
|
||||
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
advance(fixture);
|
||||
|
||||
router.navigateByUrl('/team/22/relativelink');
|
||||
advance(fixture);
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('team 22 { relativelink { }, aux: }');
|
||||
|
||||
let native = DOM.querySelector(fixture.debugElement.nativeElement, "a");
|
||||
expect(DOM.getAttribute(native, "href")).toEqual("/team/22/relativelink/simple");
|
||||
DOM.dispatchEvent(native, DOM.createMouseEvent('click'));
|
||||
advance(fixture);
|
||||
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('team 22 { relativelink { simple }, aux: }');
|
||||
})));
|
||||
|
||||
it("should set the router-link-active class",
|
||||
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
advance(fixture);
|
||||
|
||||
router.navigateByUrl('/team/22/relativelink');
|
||||
advance(fixture);
|
||||
expect(fixture.debugElement.nativeElement)
|
||||
.toHaveText('team 22 { relativelink { }, aux: }');
|
||||
let link = DOM.querySelector(fixture.debugElement.nativeElement, "a");
|
||||
expect(DOM.hasClass(link, "router-link-active")).toEqual(false);
|
||||
|
||||
DOM.dispatchEvent(link, DOM.createMouseEvent('click'));
|
||||
advance(fixture);
|
||||
|
||||
expect(DOM.hasClass(link, "router-link-active")).toEqual(true);
|
||||
})));
|
||||
|
||||
it("should update router links when router changes",
|
||||
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
||||
let fixture = tcb.createFakeAsync(RootCmp);
|
||||
advance(fixture);
|
||||
|
||||
router.navigateByUrl('/team/22/link(simple)');
|
||||
advance(fixture);
|
||||
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { link, aux: simple }');
|
||||
|
||||
let native = DOM.querySelector(fixture.debugElement.nativeElement, "a");
|
||||
expect(DOM.getAttribute(native, "href")).toEqual("/team/33/simple(aux:simple)");
|
||||
|
||||
router.navigateByUrl('/team/22/link(simple2)');
|
||||
advance(fixture);
|
||||
|
||||
expect(DOM.getAttribute(native, "href")).toEqual("/team/33/simple(aux:simple2)");
|
||||
})));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function advance(fixture: ComponentFixture<any>): void {
|
||||
tick();
|
||||
fixture.detectChanges();
|
||||
}
|
||||
|
||||
function compileRoot(tcb: TestComponentBuilder): Promise<ComponentFixture<any>> {
|
||||
return tcb.createAsync(RootCmp);
|
||||
}
|
||||
|
||||
@Component({selector: 'user-cmp', template: `hello {{user}}`})
|
||||
class UserCmp implements OnActivate {
|
||||
user: string;
|
||||
routerOnActivate(s: RouteSegment, a?, b?, c?) { this.user = s.getParam('name'); }
|
||||
}
|
||||
|
||||
@Component({selector: 'cannot-deactivate', template: `cannotDeactivate`})
|
||||
class CanDeactivateCmp implements CanDeactivate {
|
||||
routerCanDeactivate(a?, b?): Promise<boolean> { return PromiseWrapper.resolve(false); }
|
||||
}
|
||||
|
||||
@Component({selector: 'simple-cmp', template: `simple`})
|
||||
class SimpleCmp {
|
||||
}
|
||||
|
||||
@Component({selector: 'simple2-cmp', template: `simple2`})
|
||||
class Simple2Cmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'link-cmp',
|
||||
template: `<a [routerLink]="['/team', '33', 'simple']">link</a>`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
class LinkCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'link-cmp',
|
||||
template: `<a [routerLink]="['./simple']">relativelink</a> { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@Routes([new Route({path: 'simple', component: SimpleCmp})])
|
||||
class RelativeLinkCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'team-cmp',
|
||||
template: `team {{id}} { <router-outlet></router-outlet>, aux: <router-outlet name="aux"></router-outlet> }`,
|
||||
directives: [ROUTER_DIRECTIVES]
|
||||
})
|
||||
@Routes([
|
||||
new Route({path: 'user/:name', component: UserCmp}),
|
||||
new Route({path: 'simple', component: SimpleCmp}),
|
||||
new Route({path: 'simple2', component: Simple2Cmp}),
|
||||
new Route({path: 'link', component: LinkCmp}),
|
||||
new Route({path: 'relativelink', component: RelativeLinkCmp}),
|
||||
new Route({path: 'cannotDeactivate', component: CanDeactivateCmp})
|
||||
])
|
||||
class TeamCmp implements OnActivate {
|
||||
id: string;
|
||||
routerOnActivate(s: RouteSegment, a?, b?, c?) { this.id = s.getParam('id'); }
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'root-cmp',
|
||||
template: `<router-outlet></router-outlet>`,
|
||||
directives: [ROUTER_DIRECTIVES]
|
||||
})
|
||||
@Routes([new Route({path: 'team/:id', component: TeamCmp})])
|
||||
class RootCmp {
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import {
|
||||
ComponentFixture,
|
||||
AsyncTestCompleter,
|
||||
TestComponentBuilder,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {
|
||||
RouteSegment,
|
||||
UrlSegment,
|
||||
UrlTree,
|
||||
TreeNode,
|
||||
RouteTree
|
||||
} from 'angular2/src/alt_router/segments';
|
||||
import {link} from 'angular2/src/alt_router/link';
|
||||
import {DefaultRouterUrlSerializer} from 'angular2/src/alt_router/router_url_serializer';
|
||||
|
||||
export function main() {
|
||||
describe('link', () => {
|
||||
let parser = new DefaultRouterUrlSerializer();
|
||||
|
||||
it("should return the original tree when given an empty array", () => {
|
||||
let p = parser.parse("/");
|
||||
let tree = s(p.root);
|
||||
let t = link(tree.root, tree, p, []);
|
||||
expect(t).toBe(p);
|
||||
});
|
||||
|
||||
it("should support going to root", () => {
|
||||
let p = parser.parse("/");
|
||||
let tree = s(p.root);
|
||||
let t = link(tree.root, tree, p, ["/"]);
|
||||
expect(parser.serialize(t)).toEqual("");
|
||||
});
|
||||
|
||||
it("should support positional params", () => {
|
||||
let p = parser.parse("/a/b");
|
||||
let tree = s(p.firstChild(p.root));
|
||||
let t = link(tree.root, tree, p, ["/one", 11, "two", 22]);
|
||||
expect(parser.serialize(t)).toEqual("/one/11/two/22");
|
||||
});
|
||||
|
||||
it("should preserve route siblings when changing the main route", () => {
|
||||
let p = parser.parse("/a/11/b(c)");
|
||||
let tree = s(p.root);
|
||||
let t = link(tree.root, tree, p, ["/a", 11, 'd']);
|
||||
expect(parser.serialize(t)).toEqual("/a/11/d(aux:c)");
|
||||
});
|
||||
|
||||
it("should preserve route siblings when changing a aux route", () => {
|
||||
let p = parser.parse("/a/11/b(c)");
|
||||
let tree = s(p.root);
|
||||
let t = link(tree.root, tree, p, ["/a", 11, 'aux:d']);
|
||||
expect(parser.serialize(t)).toEqual("/a/11/b(aux:d)");
|
||||
});
|
||||
|
||||
it('should update parameters', () => {
|
||||
let p = parser.parse("/a;aa=11");
|
||||
let tree = s(p.root);
|
||||
let t = link(tree.root, tree, p, ["/a", {aa: 22, bb: 33}]);
|
||||
expect(parser.serialize(t)).toEqual("/a;aa=22;bb=33");
|
||||
});
|
||||
|
||||
it("should update relative subtree (when starts with ./)", () => {
|
||||
let p = parser.parse("/a(ap)/c(cp)");
|
||||
let c = p.firstChild(p.root);
|
||||
let tree = s(c);
|
||||
let t = link(tree.root, tree, p, ["./c2"]);
|
||||
expect(parser.serialize(t)).toEqual("/a(aux:ap)/c2(aux:cp)");
|
||||
});
|
||||
|
||||
it("should update relative subtree (when does not start with ./)", () => {
|
||||
let p = parser.parse("/a(ap)/c(cp)");
|
||||
let c = p.firstChild(p.root);
|
||||
let tree = s(c);
|
||||
let t = link(tree.root, tree, p, ["c2"]);
|
||||
expect(parser.serialize(t)).toEqual("/a(aux:ap)/c2(aux:cp)");
|
||||
});
|
||||
|
||||
it("should update relative subtree when the provided segment doesn't have url segments", () => {
|
||||
let p = parser.parse("/a(ap)/c(cp)");
|
||||
let c = p.firstChild(p.root);
|
||||
|
||||
let child = new RouteSegment([], null, null, null, null);
|
||||
let root = new TreeNode<RouteSegment>(new RouteSegment([c], {}, null, null, null),
|
||||
[new TreeNode<RouteSegment>(child, [])]);
|
||||
let tree = new RouteTree(root);
|
||||
|
||||
let t = link(child, tree, p, ["./c2"]);
|
||||
expect(parser.serialize(t)).toEqual("/a(aux:ap)/c2(aux:cp)");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function s(u: UrlSegment): RouteTree {
|
||||
let root = new TreeNode<RouteSegment>(new RouteSegment([u], {}, null, null, null), []);
|
||||
return new RouteTree(root);
|
||||
}
|
||||
@@ -1,182 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
beforeEachProviders,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {Injector, provide} from '@angular/core';
|
||||
|
||||
import {PlatformLocation, APP_BASE_HREF, HashLocationStrategy} from '@angular/common';
|
||||
import {SpyPlatformLocation} from '../spies';
|
||||
|
||||
export function main() {
|
||||
describe('HashLocationStrategy', () => {
|
||||
var platformLocation: SpyPlatformLocation;
|
||||
var locationStrategy: HashLocationStrategy;
|
||||
|
||||
beforeEachProviders(
|
||||
() => [HashLocationStrategy, provide(PlatformLocation, {useClass: SpyPlatformLocation})]);
|
||||
|
||||
describe('without APP_BASE_HREF', () => {
|
||||
beforeEach(inject([PlatformLocation, HashLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('#foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('#foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '#foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with just query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('?bar')).toEqual('#?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with neither leading nor trailing slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: 'app'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, HashLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('#app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('#app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '#app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('#app');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#app');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with leading slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: '/app'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, HashLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('#/app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#/app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('#/app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '#/app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('#/app');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#/app');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with both leading and trailing slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: '/app/'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, HashLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('#/app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#/app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('#/app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '#/app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('#/app/');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '#/app/');
|
||||
});
|
||||
});
|
||||
|
||||
describe('hashLocationStrategy bugs', () => {
|
||||
beforeEach(inject([PlatformLocation, HashLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should not include platform search', () => {
|
||||
platformLocation.search = '?donotinclude';
|
||||
expect(locationStrategy.path()).toEqual('');
|
||||
});
|
||||
|
||||
it('should not include platform search even with hash', () => {
|
||||
platformLocation.hash = '#hashPath';
|
||||
platformLocation.search = '?donotinclude';
|
||||
expect(locationStrategy.path()).toEqual('hashPath');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
beforeEachProviders,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {Injector, provide, ReflectiveInjector} from '@angular/core';
|
||||
import {Location, LocationStrategy, APP_BASE_HREF} from '@angular/common';
|
||||
import {MockLocationStrategy} from '@angular/common/testing';
|
||||
|
||||
export function main() {
|
||||
describe('Location', () => {
|
||||
|
||||
var locationStrategy, location;
|
||||
|
||||
function makeLocation(baseHref: string = '/my/app',
|
||||
provider: any = /*@ts2dart_const*/[]): Location {
|
||||
locationStrategy = new MockLocationStrategy();
|
||||
locationStrategy.internalBaseHref = baseHref;
|
||||
let injector = ReflectiveInjector.resolveAndCreate(
|
||||
[Location, provide(LocationStrategy, {useValue: locationStrategy}), provider]);
|
||||
return location = injector.get(Location);
|
||||
}
|
||||
|
||||
beforeEach(makeLocation);
|
||||
|
||||
it('should not prepend urls with starting slash when an empty URL is provided',
|
||||
() => { expect(location.prepareExternalUrl('')).toEqual(locationStrategy.getBaseHref()); });
|
||||
|
||||
it('should not prepend path with an extra slash when a baseHref has a trailing slash', () => {
|
||||
let location = makeLocation('/my/slashed/app/');
|
||||
expect(location.prepareExternalUrl('/page')).toEqual('/my/slashed/app/page');
|
||||
});
|
||||
|
||||
it('should not append urls with leading slash on navigate', () => {
|
||||
location.go('/my/app/user/btford');
|
||||
expect(locationStrategy.path()).toEqual('/my/app/user/btford');
|
||||
});
|
||||
|
||||
it('should normalize urls on popstate', inject([AsyncTestCompleter], (async) => {
|
||||
locationStrategy.simulatePopState('/my/app/user/btford');
|
||||
location.subscribe((ev) => {
|
||||
expect(ev['url']).toEqual('/user/btford');
|
||||
async.done();
|
||||
})
|
||||
}));
|
||||
|
||||
it('should revert to the previous path when a back() operation is executed', () => {
|
||||
var locationStrategy = new MockLocationStrategy();
|
||||
var location = new Location(locationStrategy);
|
||||
|
||||
function assertUrl(path) { expect(location.path()).toEqual(path); }
|
||||
|
||||
location.go('/ready');
|
||||
assertUrl('/ready');
|
||||
|
||||
location.go('/ready/set');
|
||||
assertUrl('/ready/set');
|
||||
|
||||
location.go('/ready/set/go');
|
||||
assertUrl('/ready/set/go');
|
||||
|
||||
location.back();
|
||||
assertUrl('/ready/set');
|
||||
|
||||
location.back();
|
||||
assertUrl('/ready');
|
||||
});
|
||||
|
||||
it('should incorporate the provided query values into the location change', () => {
|
||||
var locationStrategy = new MockLocationStrategy();
|
||||
var location = new Location(locationStrategy);
|
||||
|
||||
location.go('/home', "key=value");
|
||||
expect(location.path()).toEqual("/home?key=value");
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
beforeEachProviders,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {Injector, provide} from '@angular/core';
|
||||
import {
|
||||
PlatformLocation,
|
||||
LocationStrategy,
|
||||
PathLocationStrategy,
|
||||
APP_BASE_HREF
|
||||
} from '@angular/common';
|
||||
import {SpyPlatformLocation} from '../spies';
|
||||
|
||||
export function main() {
|
||||
describe('PathLocationStrategy', () => {
|
||||
var platformLocation, locationStrategy;
|
||||
|
||||
beforeEachProviders(() => [
|
||||
PathLocationStrategy,
|
||||
provide(PlatformLocation, {useFactory: makeSpyPlatformLocation})
|
||||
]);
|
||||
|
||||
it('should throw without a base element or APP_BASE_HREF', () => {
|
||||
platformLocation = new SpyPlatformLocation();
|
||||
platformLocation.pathname = '';
|
||||
platformLocation.spy('getBaseHrefFromDOM').andReturn(null);
|
||||
|
||||
expect(() => new PathLocationStrategy(platformLocation))
|
||||
.toThrowError(
|
||||
'No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.');
|
||||
});
|
||||
|
||||
describe('without APP_BASE_HREF', () => {
|
||||
beforeEach(inject([PlatformLocation, PathLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', 'foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', 'foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with neither leading nor trailing slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: 'app'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, PathLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', 'app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', 'app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('app');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', 'app');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with leading slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: '/app'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, PathLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('/app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '/app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('/app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '/app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('/app');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '/app');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with APP_BASE_HREF with both leading and trailing slash', () => {
|
||||
beforeEachProviders(() => [provide(APP_BASE_HREF, {useValue: '/app/'})]);
|
||||
|
||||
beforeEach(inject([PlatformLocation, PathLocationStrategy], (pl, ls) => {
|
||||
platformLocation = pl;
|
||||
locationStrategy = ls;
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
}));
|
||||
|
||||
it('should prepend urls with a hash for non-empty URLs', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo')).toEqual('/app/foo');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '/app/foo');
|
||||
});
|
||||
|
||||
it('should prepend urls with a hash for URLs with query params', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('foo?bar')).toEqual('/app/foo?bar');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', 'foo', 'bar=baz');
|
||||
expect(platformLocation.spy('pushState'))
|
||||
.toHaveBeenCalledWith(null, 'Title', '/app/foo?bar=baz');
|
||||
});
|
||||
|
||||
it('should not prepend a hash to external urls for an empty internal URL', () => {
|
||||
expect(locationStrategy.prepareExternalUrl('')).toEqual('/app/');
|
||||
|
||||
locationStrategy.pushState(null, 'Title', '', '');
|
||||
expect(platformLocation.spy('pushState')).toHaveBeenCalledWith(null, 'Title', '/app/');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function makeSpyPlatformLocation() {
|
||||
var platformLocation = new SpyPlatformLocation();
|
||||
platformLocation.spy('getBaseHrefFromDOM').andReturn('');
|
||||
platformLocation.spy('pushState');
|
||||
platformLocation.pathname = '';
|
||||
return platformLocation;
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
import {
|
||||
ComponentFixture,
|
||||
AsyncTestCompleter,
|
||||
TestComponentBuilder,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {recognize} from 'angular2/src/alt_router/recognize';
|
||||
import {Routes, Route} from 'angular2/alt_router';
|
||||
import {provide, Component, ComponentResolver} from 'angular2/core';
|
||||
import {UrlSegment, RouteTree, UrlTree} from 'angular2/src/alt_router/segments';
|
||||
import {DefaultRouterUrlSerializer} from 'angular2/src/alt_router/router_url_serializer';
|
||||
import {DEFAULT_OUTLET_NAME} from 'angular2/src/alt_router/constants';
|
||||
|
||||
export function main() {
|
||||
describe('recognize', () => {
|
||||
it('should handle position args',
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentA, tree("b/paramB/c/paramC/d"))
|
||||
.then(r => {
|
||||
let a = r.root;
|
||||
expect(stringifyUrl(a.urlSegments)).toEqual([""]);
|
||||
expect(a.type).toBe(ComponentA);
|
||||
|
||||
let b = r.firstChild(r.root);
|
||||
expect(stringifyUrl(b.urlSegments)).toEqual(["b", "paramB"]);
|
||||
expect(b.type).toBe(ComponentB);
|
||||
|
||||
let c = r.firstChild(r.firstChild(r.root));
|
||||
expect(stringifyUrl(c.urlSegments)).toEqual(["c", "paramC"]);
|
||||
expect(c.type).toBe(ComponentC);
|
||||
|
||||
let d = r.firstChild(r.firstChild(r.firstChild(r.root)));
|
||||
expect(stringifyUrl(d.urlSegments)).toEqual(["d"]);
|
||||
expect(d.type).toBe(ComponentD);
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should support empty routes',
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentA, tree("f"))
|
||||
.then(r => {
|
||||
let a = r.root;
|
||||
expect(stringifyUrl(a.urlSegments)).toEqual([""]);
|
||||
expect(a.type).toBe(ComponentA);
|
||||
|
||||
let f = r.firstChild(r.root);
|
||||
expect(stringifyUrl(f.urlSegments)).toEqual(["f"]);
|
||||
expect(f.type).toBe(ComponentF);
|
||||
|
||||
let d = r.firstChild(r.firstChild(r.root));
|
||||
expect(stringifyUrl(d.urlSegments)).toEqual([]);
|
||||
expect(d.type).toBe(ComponentD);
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should handle aux routes',
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentA, tree("b/paramB(/d//right:d)"))
|
||||
.then(r => {
|
||||
let c = r.children(r.root);
|
||||
expect(stringifyUrl(c[0].urlSegments)).toEqual(["b", "paramB"]);
|
||||
expect(c[0].outlet).toEqual(DEFAULT_OUTLET_NAME);
|
||||
expect(c[0].type).toBe(ComponentB);
|
||||
|
||||
expect(stringifyUrl(c[1].urlSegments)).toEqual(["d"]);
|
||||
expect(c[1].outlet).toEqual("aux");
|
||||
expect(c[1].type).toBe(ComponentD);
|
||||
|
||||
expect(stringifyUrl(c[2].urlSegments)).toEqual(["d"]);
|
||||
expect(c[2].outlet).toEqual("right");
|
||||
expect(c[2].type).toBe(ComponentD);
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it("should error when two segments with the same outlet name",
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentA, tree("b/paramB(right:d//right:e)"))
|
||||
.catch(e => {
|
||||
expect(e.message).toEqual(
|
||||
"Two segments cannot have the same outlet name: 'right:d' and 'right:e'.");
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should handle nested aux routes',
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentA, tree("b/paramB(/d(right:e))"))
|
||||
.then(r => {
|
||||
let c = r.children(r.root);
|
||||
expect(stringifyUrl(c[0].urlSegments)).toEqual(["b", "paramB"]);
|
||||
expect(c[0].outlet).toEqual(DEFAULT_OUTLET_NAME);
|
||||
expect(c[0].type).toBe(ComponentB);
|
||||
|
||||
expect(stringifyUrl(c[1].urlSegments)).toEqual(["d"]);
|
||||
expect(c[1].outlet).toEqual("aux");
|
||||
expect(c[1].type).toBe(ComponentD);
|
||||
|
||||
expect(stringifyUrl(c[2].urlSegments)).toEqual(["e"]);
|
||||
expect(c[2].outlet).toEqual("right");
|
||||
expect(c[2].type).toBe(ComponentE);
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should handle non top-level aux routes',
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentA, tree('b/paramB/d(e)'))
|
||||
.then(r => {
|
||||
let c = r.children(r.firstChild(r.root));
|
||||
expect(stringifyUrl(c[0].urlSegments)).toEqual(["d"]);
|
||||
expect(c[0].outlet).toEqual(DEFAULT_OUTLET_NAME);
|
||||
expect(c[0].type).toBe(ComponentD);
|
||||
|
||||
expect(stringifyUrl(c[1].urlSegments)).toEqual(["e"]);
|
||||
expect(c[1].outlet).toEqual("aux");
|
||||
expect(c[1].type).toBe(ComponentE);
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should handle matrix parameters',
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentA, tree("b/paramB;b1=1;b2=2(/d;d1=1;d2=2)"))
|
||||
.then(r => {
|
||||
let c = r.children(r.root);
|
||||
expect(c[0].parameters).toEqual({'b': 'paramB', 'b1': '1', 'b2': '2'});
|
||||
expect(c[1].parameters).toEqual({'d1': '1', 'd2': '2'});
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should match a wildcard',
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentG, tree("a;aa=1/b;bb=2"))
|
||||
.then(r => {
|
||||
let c = r.children(r.root);
|
||||
expect(c.length).toEqual(1);
|
||||
expect(stringifyUrl(c[0].urlSegments)).toEqual([]);
|
||||
expect(c[0].parameters).toEqual(null);
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should error when no matching routes',
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentA, tree("invalid"))
|
||||
.catch(e => {
|
||||
expect(e.message).toContain("Cannot match any routes");
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should handle no matching routes (too short)',
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentA, tree("b"))
|
||||
.catch(e => {
|
||||
expect(e.message).toContain("Cannot match any routes");
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it("should error when a component doesn't have @Routes",
|
||||
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
||||
recognize(resolver, ComponentA, tree("d/invalid"))
|
||||
.catch(e => {
|
||||
expect(e.message)
|
||||
.toEqual("Component 'ComponentD' does not have route configuration");
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
function tree(url: string): UrlTree {
|
||||
return new DefaultRouterUrlSerializer().parse(url);
|
||||
}
|
||||
|
||||
function stringifyUrl(segments: UrlSegment[]): string[] {
|
||||
return segments.map(s => s.segment);
|
||||
}
|
||||
|
||||
@Component({selector: 'd', template: 't'})
|
||||
class ComponentD {
|
||||
}
|
||||
|
||||
@Component({selector: 'e', template: 't'})
|
||||
class ComponentE {
|
||||
}
|
||||
|
||||
@Component({selector: 'f', template: 't'})
|
||||
@Routes([new Route({path: "/", component: ComponentD})])
|
||||
class ComponentF {
|
||||
}
|
||||
|
||||
@Component({selector: 'c', template: 't'})
|
||||
@Routes([new Route({path: "d", component: ComponentD})])
|
||||
class ComponentC {
|
||||
}
|
||||
|
||||
@Component({selector: 'b', template: 't'})
|
||||
@Routes([
|
||||
new Route({path: "d", component: ComponentD}),
|
||||
new Route({path: "e", component: ComponentE}),
|
||||
new Route({path: "c/:c", component: ComponentC})
|
||||
])
|
||||
class ComponentB {
|
||||
}
|
||||
|
||||
@Component({selector: 'g', template: 't'})
|
||||
@Routes(
|
||||
[new Route({path: "d", component: ComponentD}), new Route({path: "*", component: ComponentE})])
|
||||
class ComponentG {
|
||||
}
|
||||
|
||||
@Component({selector: 'a', template: 't'})
|
||||
@Routes([
|
||||
new Route({path: "b/:b", component: ComponentB}),
|
||||
new Route({path: "d", component: ComponentD}),
|
||||
new Route({path: "e", component: ComponentE}),
|
||||
new Route({path: "f", component: ComponentF})
|
||||
])
|
||||
class ComponentA {
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
library angular2.test.router.route_config_spec;
|
||||
|
||||
/**
|
||||
* This is intentionally left blank. `route_config_spec.ts` contains tests specific
|
||||
* to using untyped objects for users writing idiomatic ES5 or TS.
|
||||
* The rest of the router tests have typed annotations, so there's no need to add
|
||||
* additional tests here for Dart.
|
||||
*/
|
||||
main() {}
|
||||
@@ -1,339 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xdescribe,
|
||||
xit,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {bootstrap} from '@angular/platform-browser-dynamic';
|
||||
import {APP_BASE_HREF, LocationStrategy} from '@angular/common';
|
||||
import {Component, Directive} from '@angular/core/src/metadata';
|
||||
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
||||
import {Console} from '@angular/core/src/console';
|
||||
import {provide} from '@angular/core';
|
||||
import {DOCUMENT} from '@angular/platform-browser/src/dom/dom_tokens';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
import {ROUTER_PROVIDERS, Router, RouteConfig, ROUTER_DIRECTIVES} from '@angular/router';
|
||||
import {ExceptionHandler} from '@angular/core';
|
||||
import {MockLocationStrategy} from '@angular/common/testing';
|
||||
|
||||
class _ArrayLogger {
|
||||
res: any[] = [];
|
||||
log(s: any): void { this.res.push(s); }
|
||||
logError(s: any): void { this.res.push(s); }
|
||||
logGroup(s: any): void { this.res.push(s); }
|
||||
logGroupEnd(){};
|
||||
}
|
||||
|
||||
class DummyConsole implements Console {
|
||||
log(message) {}
|
||||
warn(message) {}
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe('RouteConfig with POJO arguments', () => {
|
||||
var fakeDoc, el, testBindings;
|
||||
beforeEach(() => {
|
||||
fakeDoc = getDOM().createHtmlDocument();
|
||||
el = getDOM().createElement('app-cmp', fakeDoc);
|
||||
getDOM().appendChild(fakeDoc.body, el);
|
||||
var logger = new _ArrayLogger();
|
||||
var exceptionHandler = new ExceptionHandler(logger, false);
|
||||
testBindings = [
|
||||
ROUTER_PROVIDERS,
|
||||
provide(LocationStrategy, {useClass: MockLocationStrategy}),
|
||||
provide(DOCUMENT, {useValue: fakeDoc}),
|
||||
provide(ExceptionHandler, {useValue: exceptionHandler}),
|
||||
provide(Console, {useClass: DummyConsole})
|
||||
];
|
||||
});
|
||||
|
||||
it('should bootstrap an app with a hierarchy', inject([AsyncTestCompleter], (async) => {
|
||||
bootstrap(HierarchyAppCmp, testBindings)
|
||||
.then((applicationRef) => {
|
||||
var router = applicationRef.instance.router;
|
||||
router.subscribe((_) => {
|
||||
expect(el).toHaveText('root { parent { hello } }');
|
||||
expect(applicationRef.instance.location.path()).toEqual('/parent/child');
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/parent/child');
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should work in an app with redirects', inject([AsyncTestCompleter], (async) => {
|
||||
bootstrap(RedirectAppCmp, testBindings)
|
||||
.then((applicationRef) => {
|
||||
var router = applicationRef.instance.router;
|
||||
router.subscribe((_) => {
|
||||
expect(el).toHaveText('root { hello }');
|
||||
expect(applicationRef.instance.location.path()).toEqual('/after');
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/before');
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should work in an app with async components', inject([AsyncTestCompleter], (async) => {
|
||||
bootstrap(AsyncAppCmp, testBindings)
|
||||
.then((applicationRef) => {
|
||||
var router = applicationRef.instance.router;
|
||||
router.subscribe((_) => {
|
||||
expect(el).toHaveText('root { hello }');
|
||||
expect(applicationRef.instance.location.path()).toEqual('/hello');
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/hello');
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should work in an app with aux routes', inject([AsyncTestCompleter], (async) => {
|
||||
bootstrap(AuxAppCmp, testBindings)
|
||||
.then((applicationRef) => {
|
||||
var router = applicationRef.instance.router;
|
||||
router.subscribe((_) => {
|
||||
expect(el).toHaveText('root { hello } aside { hello }');
|
||||
expect(applicationRef.instance.location.path()).toEqual('/hello(aside)');
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/hello(aside)');
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should work in an app with async components defined with "loader"',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
bootstrap(ConciseAsyncAppCmp, testBindings)
|
||||
.then((applicationRef) => {
|
||||
var router = applicationRef.instance.router;
|
||||
router.subscribe((_) => {
|
||||
expect(el).toHaveText('root { hello }');
|
||||
expect(applicationRef.instance.location.path()).toEqual('/hello');
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/hello');
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should work in an app with a constructor component',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
bootstrap(ExplicitConstructorAppCmp, testBindings)
|
||||
.then((applicationRef) => {
|
||||
var router = applicationRef.instance.router;
|
||||
router.subscribe((_) => {
|
||||
expect(el).toHaveText('root { hello }');
|
||||
expect(applicationRef.instance.location.path()).toEqual('/hello');
|
||||
async.done();
|
||||
});
|
||||
router.navigateByUrl('/hello');
|
||||
});
|
||||
}));
|
||||
|
||||
it('should throw if a config is missing a target',
|
||||
inject(
|
||||
[AsyncTestCompleter],
|
||||
(async) => {
|
||||
bootstrap(WrongConfigCmp, testBindings)
|
||||
.catch((e) => {
|
||||
expect(e.originalException)
|
||||
.toContainError(
|
||||
'Route config should contain exactly one "component", "loader", or "redirectTo" property.');
|
||||
async.done();
|
||||
return null;
|
||||
})}));
|
||||
|
||||
it('should throw if a config has an invalid component type',
|
||||
inject(
|
||||
[AsyncTestCompleter],
|
||||
(async) => {
|
||||
bootstrap(WrongComponentTypeCmp, testBindings)
|
||||
.catch((e) => {
|
||||
expect(e.originalException)
|
||||
.toContainError(
|
||||
'Invalid component type "intentionallyWrongComponentType". Valid types are "constructor" and "loader".');
|
||||
async.done();
|
||||
return null;
|
||||
})}));
|
||||
|
||||
it('should throw if a config has an invalid alias name',
|
||||
inject(
|
||||
[AsyncTestCompleter],
|
||||
(async) => {
|
||||
bootstrap(BadAliasNameCmp, testBindings)
|
||||
.catch((e) => {
|
||||
expect(e.originalException)
|
||||
.toContainError(
|
||||
`Route "/child" with name "child" does not begin with an uppercase letter. Route names should be CamelCase like "Child".`);
|
||||
async.done();
|
||||
return null;
|
||||
})}));
|
||||
|
||||
it('should throw if a config has an invalid alias name with "as"',
|
||||
inject(
|
||||
[AsyncTestCompleter],
|
||||
(async) => {
|
||||
bootstrap(BadAliasCmp, testBindings)
|
||||
.catch((e) => {
|
||||
expect(e.originalException)
|
||||
.toContainError(
|
||||
`Route "/child" with name "child" does not begin with an uppercase letter. Route names should be CamelCase like "Child".`);
|
||||
async.done();
|
||||
return null;
|
||||
})}));
|
||||
|
||||
it('should throw if a config has multiple alias properties "as" and "name"',
|
||||
inject([AsyncTestCompleter],
|
||||
(async) => {
|
||||
bootstrap(MultipleAliasCmp, testBindings)
|
||||
.catch((e) => {
|
||||
expect(e.originalException)
|
||||
.toContainError(
|
||||
`Route config should contain exactly one "as" or "name" property.`);
|
||||
async.done();
|
||||
return null;
|
||||
})}));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Component({selector: 'hello-cmp', template: 'hello'})
|
||||
class HelloCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([
|
||||
{path: '/before', redirectTo: ['Hello']},
|
||||
{path: '/after', component: HelloCmp, name: 'Hello'}
|
||||
])
|
||||
class RedirectAppCmp {
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
function HelloLoader(): Promise<any> {
|
||||
return Promise.resolve(HelloCmp);
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([
|
||||
{path: '/hello', component: {type: 'loader', loader: HelloLoader}},
|
||||
])
|
||||
class AsyncAppCmp {
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([
|
||||
{path: '/hello', loader: HelloLoader},
|
||||
])
|
||||
class ConciseAsyncAppCmp {
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> } aside { <router-outlet name="aside"></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([{path: '/hello', component: HelloCmp}, {aux: 'aside', component: HelloCmp}])
|
||||
class AuxAppCmp {
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([
|
||||
{path: '/hello', component: {type: 'constructor', constructor: HelloCmp}},
|
||||
])
|
||||
class ExplicitConstructorAppCmp {
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'parent-cmp',
|
||||
template: `parent { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([{path: '/child', component: HelloCmp}])
|
||||
class ParentCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([{path: '/parent/...', component: ParentCmp}])
|
||||
class HierarchyAppCmp {
|
||||
constructor(public router: Router, public location: LocationStrategy) {}
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([{path: '/hello'}])
|
||||
class WrongConfigCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([{path: '/child', component: HelloCmp, name: 'child'}])
|
||||
class BadAliasNameCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([{path: '/child', component: HelloCmp, as: 'child'}])
|
||||
class BadAliasCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([{path: '/child', component: HelloCmp, as: 'Child', name: 'Child'}])
|
||||
class MultipleAliasCmp {
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-cmp',
|
||||
template: `root { <router-outlet></router-outlet> }`,
|
||||
directives: ROUTER_DIRECTIVES
|
||||
})
|
||||
@RouteConfig([
|
||||
{path: '/hello', component: {type: 'intentionallyWrongComponentType', constructor: HelloCmp}},
|
||||
])
|
||||
class WrongComponentTypeCmp {
|
||||
}
|
||||
@@ -1,372 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {PromiseWrapper} from '../src/facade/async';
|
||||
import {Type, IS_DART} from '../src/facade/lang';
|
||||
|
||||
import {RouteRegistry} from '../../router/src/route_registry';
|
||||
import {
|
||||
RouteConfig,
|
||||
Route,
|
||||
Redirect,
|
||||
AuxRoute,
|
||||
AsyncRoute
|
||||
} from '../../router/src/route_config/route_config_decorator';
|
||||
|
||||
|
||||
export function main() {
|
||||
describe('RouteRegistry', () => {
|
||||
var registry: RouteRegistry;
|
||||
|
||||
beforeEach(() => { registry = new RouteRegistry(RootHostCmp); });
|
||||
|
||||
it('should match the full URL', inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp, new Route({path: '/', component: DummyCmpA}));
|
||||
registry.config(RootHostCmp, new Route({path: '/test', component: DummyCmpB}));
|
||||
|
||||
registry.recognize('/test', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyCmpB);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate URLs starting at the given component', () => {
|
||||
registry.config(RootHostCmp,
|
||||
new Route({path: '/first/...', component: DummyParentCmp, name: 'FirstCmp'}));
|
||||
|
||||
var instr = registry.generate(['FirstCmp', 'SecondCmp'], []);
|
||||
expect(stringifyInstruction(instr)).toEqual('first/second');
|
||||
|
||||
expect(stringifyInstruction(registry.generate(['SecondCmp'], [instr, instr.child])))
|
||||
.toEqual('first/second');
|
||||
expect(stringifyInstruction(registry.generate(['./SecondCmp'], [instr, instr.child])))
|
||||
.toEqual('first/second');
|
||||
});
|
||||
|
||||
it('should generate URLs that account for default routes', () => {
|
||||
registry.config(
|
||||
RootHostCmp,
|
||||
new Route({path: '/first/...', component: ParentWithDefaultRouteCmp, name: 'FirstCmp'}));
|
||||
|
||||
var instruction = registry.generate(['FirstCmp'], []);
|
||||
|
||||
expect(instruction.toLinkUrl()).toEqual('first');
|
||||
expect(instruction.toRootUrl()).toEqual('first/second');
|
||||
});
|
||||
|
||||
it('should generate URLs in a hierarchy of default routes', () => {
|
||||
registry.config(
|
||||
RootHostCmp,
|
||||
new Route({path: '/first/...', component: MultipleDefaultCmp, name: 'FirstCmp'}));
|
||||
|
||||
var instruction = registry.generate(['FirstCmp'], []);
|
||||
|
||||
expect(instruction.toLinkUrl()).toEqual('first');
|
||||
expect(instruction.toRootUrl()).toEqual('first/second/third');
|
||||
});
|
||||
|
||||
it('should generate URLs with params', () => {
|
||||
registry.config(
|
||||
RootHostCmp,
|
||||
new Route({path: '/first/:param/...', component: DummyParentParamCmp, name: 'FirstCmp'}));
|
||||
|
||||
var url = stringifyInstruction(
|
||||
registry.generate(['FirstCmp', {param: 'one'}, 'SecondCmp', {param: 'two'}], []));
|
||||
expect(url).toEqual('first/one/second/two');
|
||||
});
|
||||
|
||||
it('should generate params as an empty StringMap when no params are given', () => {
|
||||
registry.config(RootHostCmp, new Route({path: '/test', component: DummyCmpA, name: 'Test'}));
|
||||
var instruction = registry.generate(['Test'], []);
|
||||
expect(instruction.component.params).toEqual({});
|
||||
});
|
||||
|
||||
it('should generate URLs with extra params in the query', () => {
|
||||
registry.config(RootHostCmp,
|
||||
new Route({path: '/first/second', component: DummyCmpA, name: 'FirstCmp'}));
|
||||
|
||||
var instruction = registry.generate(['FirstCmp', {a: 'one'}], []);
|
||||
expect(instruction.toLinkUrl()).toEqual('first/second?a=one');
|
||||
});
|
||||
|
||||
|
||||
it('should generate URLs of loaded components after they are loaded',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(
|
||||
RootHostCmp,
|
||||
new AsyncRoute({path: '/first/...', loader: asyncParentLoader, name: 'FirstCmp'}));
|
||||
|
||||
var instruction = registry.generate(['FirstCmp', 'SecondCmp'], []);
|
||||
|
||||
expect(stringifyInstruction(instruction)).toEqual('first');
|
||||
|
||||
registry.recognize('/first/second', [])
|
||||
.then((_) => {
|
||||
var instruction = registry.generate(['FirstCmp', 'SecondCmp'], []);
|
||||
expect(stringifyInstruction(instruction)).toEqual('first/second');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should throw when generating a url and a parent has no config', () => {
|
||||
expect(() => registry.generate(['FirstCmp', 'SecondCmp'], []))
|
||||
.toThrowError('Component "RootHostCmp" has no route config.');
|
||||
});
|
||||
|
||||
it('should generate URLs for aux routes', () => {
|
||||
registry.config(RootHostCmp,
|
||||
new Route({path: '/primary', component: DummyCmpA, name: 'Primary'}));
|
||||
registry.config(RootHostCmp, new AuxRoute({path: '/aux', component: DummyCmpB, name: 'Aux'}));
|
||||
|
||||
expect(stringifyInstruction(registry.generate(['Primary', ['Aux']], [])))
|
||||
.toEqual('primary(aux)');
|
||||
});
|
||||
|
||||
it('should prefer static segments to dynamic', inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp, new Route({path: '/:site', component: DummyCmpB}));
|
||||
registry.config(RootHostCmp, new Route({path: '/home', component: DummyCmpA}));
|
||||
|
||||
registry.recognize('/home', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyCmpA);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should prefer dynamic segments to star', inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp, new Route({path: '/:site', component: DummyCmpA}));
|
||||
registry.config(RootHostCmp, new Route({path: '/*site', component: DummyCmpB}));
|
||||
|
||||
registry.recognize('/home', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyCmpA);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should prefer routes with more dynamic segments', inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp, new Route({path: '/:first/*rest', component: DummyCmpA}));
|
||||
registry.config(RootHostCmp, new Route({path: '/*all', component: DummyCmpB}));
|
||||
|
||||
registry.recognize('/some/path', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyCmpA);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should prefer routes with more static segments', inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp, new Route({path: '/first/:second', component: DummyCmpA}));
|
||||
registry.config(RootHostCmp, new Route({path: '/:first/:second', component: DummyCmpB}));
|
||||
|
||||
registry.recognize('/first/second', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyCmpA);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should prefer routes with static segments before dynamic segments',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp,
|
||||
new Route({path: '/first/second/:third', component: DummyCmpB}));
|
||||
registry.config(RootHostCmp,
|
||||
new Route({path: '/first/:second/third', component: DummyCmpA}));
|
||||
|
||||
registry.recognize('/first/second/third', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyCmpB);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should prefer routes with high specificity over routes with children with lower specificity',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp, new Route({path: '/first', component: DummyCmpA}));
|
||||
|
||||
// terminates to DummyCmpB
|
||||
registry.config(RootHostCmp,
|
||||
new Route({path: '/:second/...', component: SingleSlashChildCmp}));
|
||||
|
||||
registry.recognize('/first', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyCmpA);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should match the full URL using child components', inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp, new Route({path: '/first/...', component: DummyParentCmp}));
|
||||
|
||||
registry.recognize('/first/second', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyParentCmp);
|
||||
expect(instruction.child.component.componentType).toBe(DummyCmpB);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should match the URL using async child components',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp, new Route({path: '/first/...', component: DummyAsyncCmp}));
|
||||
|
||||
registry.recognize('/first/second', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyAsyncCmp);
|
||||
|
||||
instruction.child.resolveComponent().then((childComponentInstruction) => {
|
||||
expect(childComponentInstruction.componentType).toBe(DummyCmpB);
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should match the URL using an async parent component',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp,
|
||||
new AsyncRoute({path: '/first/...', loader: asyncParentLoader}));
|
||||
|
||||
registry.recognize('/first/second', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyParentCmp);
|
||||
|
||||
instruction.child.resolveComponent().then((childType) => {
|
||||
expect(childType.componentType).toBe(DummyCmpB);
|
||||
async.done();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should throw when a parent config is missing the `...` suffix any of its children add routes',
|
||||
() => {
|
||||
expect(() =>
|
||||
registry.config(RootHostCmp, new Route({path: '/', component: DummyParentCmp})))
|
||||
.toThrowError(
|
||||
'Child routes are not allowed for "/". Use "..." on the parent\'s route path.');
|
||||
});
|
||||
|
||||
it('should throw when a parent config uses `...` suffix before the end of the route', () => {
|
||||
expect(() => registry.config(RootHostCmp,
|
||||
new Route({path: '/home/.../fun/', component: DummyParentCmp})))
|
||||
.toThrowError('Unexpected "..." before the end of the path for "home/.../fun/".');
|
||||
});
|
||||
|
||||
|
||||
it('should throw if a config has a component that is not defined', () => {
|
||||
expect(() => registry.config(RootHostCmp, new Route({path: '/', component: null})))
|
||||
.toThrowError('Component for route "/" is not defined, or is not a class.');
|
||||
expect(() => registry.config(RootHostCmp, new AuxRoute({path: '/', component: null})))
|
||||
.toThrowError('Component for route "/" is not defined, or is not a class.');
|
||||
|
||||
// This would never happen in Dart
|
||||
if (!IS_DART) {
|
||||
expect(() => registry.config(RootHostCmp, new Route({path: '/', component:<Type>(<any>4)})))
|
||||
.toThrowError('Component for route "/" is not defined, or is not a class.');
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw when linkParams are not terminal', () => {
|
||||
registry.config(RootHostCmp,
|
||||
new Route({path: '/first/...', component: DummyParentCmp, name: 'First'}));
|
||||
expect(() => { registry.generate(['First'], []); })
|
||||
.toThrowError('Link "["First"]" does not resolve to a terminal instruction.');
|
||||
});
|
||||
|
||||
it('should match matrix params on child components and query params on the root component',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
registry.config(RootHostCmp, new Route({path: '/first/...', component: DummyParentCmp}));
|
||||
|
||||
registry.recognize('/first/second;filter=odd?comments=all', [])
|
||||
.then((instruction) => {
|
||||
expect(instruction.component.componentType).toBe(DummyParentCmp);
|
||||
expect(instruction.component.params).toEqual({'comments': 'all'});
|
||||
|
||||
expect(instruction.child.component.componentType).toBe(DummyCmpB);
|
||||
expect(instruction.child.component.params).toEqual({'filter': 'odd'});
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should generate URLs with matrix and query params', () => {
|
||||
registry.config(
|
||||
RootHostCmp,
|
||||
new Route({path: '/first/:param/...', component: DummyParentParamCmp, name: 'FirstCmp'}));
|
||||
|
||||
var url = stringifyInstruction(registry.generate(
|
||||
[
|
||||
'FirstCmp',
|
||||
{param: 'one', query: 'cats'},
|
||||
'SecondCmp',
|
||||
{
|
||||
param: 'two',
|
||||
sort: 'asc',
|
||||
}
|
||||
],
|
||||
[]));
|
||||
expect(url).toEqual('first/one/second/two;sort=asc?query=cats');
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
function stringifyInstruction(instruction): string {
|
||||
return instruction.toRootUrl();
|
||||
}
|
||||
|
||||
|
||||
function asyncParentLoader() {
|
||||
return PromiseWrapper.resolve(DummyParentCmp);
|
||||
}
|
||||
|
||||
function asyncChildLoader() {
|
||||
return PromiseWrapper.resolve(DummyCmpB);
|
||||
}
|
||||
|
||||
class RootHostCmp {}
|
||||
|
||||
@RouteConfig([new AsyncRoute({path: '/second', loader: asyncChildLoader})])
|
||||
class DummyAsyncCmp {
|
||||
}
|
||||
|
||||
class DummyCmpA {}
|
||||
class DummyCmpB {}
|
||||
|
||||
@RouteConfig(
|
||||
[new Route({path: '/third', component: DummyCmpB, name: 'ThirdCmp', useAsDefault: true})])
|
||||
class DefaultRouteCmp {
|
||||
}
|
||||
|
||||
@RouteConfig([new Route({path: '/', component: DummyCmpB, name: 'ThirdCmp'})])
|
||||
class SingleSlashChildCmp {
|
||||
}
|
||||
|
||||
|
||||
@RouteConfig([
|
||||
new Route(
|
||||
{path: '/second/...', component: DefaultRouteCmp, name: 'SecondCmp', useAsDefault: true})
|
||||
])
|
||||
class MultipleDefaultCmp {
|
||||
}
|
||||
|
||||
@RouteConfig(
|
||||
[new Route({path: '/second', component: DummyCmpB, name: 'SecondCmp', useAsDefault: true})])
|
||||
class ParentWithDefaultRouteCmp {
|
||||
}
|
||||
|
||||
@RouteConfig([new Route({path: '/second', component: DummyCmpB, name: 'SecondCmp'})])
|
||||
class DummyParentCmp {
|
||||
}
|
||||
|
||||
|
||||
@RouteConfig([new Route({path: '/second/:param', component: DummyCmpB, name: 'SecondCmp'})])
|
||||
class DummyParentParamCmp {
|
||||
}
|
||||
@@ -1,334 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
xit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
beforeEachProviders
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
import {SpyRouterOutlet} from './spies';
|
||||
import {Type} from '../src/facade/lang';
|
||||
import {PromiseWrapper, ObservableWrapper} from '../src/facade/async';
|
||||
import {ListWrapper} from '../src/facade/collection';
|
||||
import {Router, RootRouter} from '@angular/router/src/router';
|
||||
import {SpyLocation} from '@angular/common/testing';
|
||||
import {Location} from '@angular/common';
|
||||
import {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from '@angular/router/src/route_registry';
|
||||
import {
|
||||
RouteConfig,
|
||||
AsyncRoute,
|
||||
Route,
|
||||
Redirect
|
||||
} from '@angular/router/src/route_config/route_config_decorator';
|
||||
import {provide} from '@angular/core';
|
||||
import {RouterOutlet} from '@angular/router/src/directives/router_outlet';
|
||||
|
||||
export function main() {
|
||||
describe('Router', () => {
|
||||
var router: Router;
|
||||
var location: Location;
|
||||
|
||||
beforeEachProviders(() => [
|
||||
RouteRegistry,
|
||||
provide(Location, {useClass: SpyLocation}),
|
||||
provide(ROUTER_PRIMARY_COMPONENT, {useValue: AppCmp}),
|
||||
provide(Router, {useClass: RootRouter})
|
||||
]);
|
||||
|
||||
|
||||
beforeEach(inject([Router, Location], (rtr: Router, loc: Location) => {
|
||||
router = rtr;
|
||||
location = loc;
|
||||
}));
|
||||
|
||||
|
||||
it('should navigate based on the initial URL state', inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.config([new Route({path: '/', component: DummyComponent})])
|
||||
.then((_) => router.registerPrimaryOutlet(outlet))
|
||||
.then((_) => {
|
||||
expect((<any>outlet).spy('activate')).toHaveBeenCalled();
|
||||
expect((<SpyLocation>location).urlChanges).toEqual([]);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should activate viewports and update URL on navigate',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.registerPrimaryOutlet(outlet)
|
||||
.then((_) => router.config([new Route({path: '/a', component: DummyComponent})]))
|
||||
.then((_) => router.navigateByUrl('/a'))
|
||||
.then((_) => {
|
||||
expect((<any>outlet).spy('activate')).toHaveBeenCalled();
|
||||
expect((<SpyLocation>location).urlChanges).toEqual(['/a']);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should activate viewports and update URL when navigating via DSL',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.registerPrimaryOutlet(outlet)
|
||||
.then((_) => router.config(
|
||||
[new Route({path: '/a', component: DummyComponent, name: 'A'})]))
|
||||
.then((_) => router.navigate(['/A']))
|
||||
.then((_) => {
|
||||
expect((<any>outlet).spy('activate')).toHaveBeenCalled();
|
||||
expect((<SpyLocation>location).urlChanges).toEqual(['/a']);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should not push a history change on when navigate is called with skipUrlChange',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.registerPrimaryOutlet(outlet)
|
||||
.then((_) => router.config([new Route({path: '/b', component: DummyComponent})]))
|
||||
.then((_) => router.navigateByUrl('/b', true))
|
||||
.then((_) => {
|
||||
expect((<any>outlet).spy('activate')).toHaveBeenCalled();
|
||||
expect((<SpyLocation>location).urlChanges).toEqual([]);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
// See https://github.com/angular/angular/issues/5590
|
||||
// This test is disabled because it is flaky.
|
||||
// TODO: bford. make this test not flaky and reenable it.
|
||||
xit('should replace history when triggered by a hashchange with a redirect',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.registerPrimaryOutlet(outlet)
|
||||
.then((_) => router.config([
|
||||
new Redirect({path: '/a', redirectTo: ['B']}),
|
||||
new Route({path: '/b', component: DummyComponent, name: 'B'})
|
||||
]))
|
||||
.then((_) => {
|
||||
router.subscribe((_) => {
|
||||
expect((<SpyLocation>location).urlChanges).toEqual(['hash: a', 'replace: /b']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
(<SpyLocation>location).simulateHashChange('a');
|
||||
});
|
||||
}));
|
||||
|
||||
it('should push history when triggered by a hashchange without a redirect',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.registerPrimaryOutlet(outlet)
|
||||
.then((_) => router.config([new Route({path: '/a', component: DummyComponent})]))
|
||||
.then((_) => {
|
||||
router.subscribe((_) => {
|
||||
expect((<SpyLocation>location).urlChanges).toEqual(['hash: a']);
|
||||
async.done();
|
||||
});
|
||||
|
||||
(<SpyLocation>location).simulateHashChange('a');
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should trigger the onError callback of a router change subscription if the URL does not match a route',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.registerPrimaryOutlet(outlet)
|
||||
.then((_) => router.config([new Route({path: '/a', component: DummyComponent})]))
|
||||
.then((_) => {
|
||||
router.subscribe((_) => {}, (url) => {
|
||||
expect(url).toEqual('b');
|
||||
async.done();
|
||||
});
|
||||
(<SpyLocation>location).simulateHashChange('b');
|
||||
});
|
||||
}));
|
||||
|
||||
it('should navigate after being configured', inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.registerPrimaryOutlet(outlet)
|
||||
.then((_) => router.navigateByUrl('/a'))
|
||||
.then((_) => {
|
||||
expect((<any>outlet).spy('activate')).not.toHaveBeenCalled();
|
||||
return router.config([new Route({path: '/a', component: DummyComponent})]);
|
||||
})
|
||||
.then((_) => {
|
||||
expect((<any>outlet).spy('activate')).toHaveBeenCalled();
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should throw when linkParams does not include a route name', () => {
|
||||
expect(() => router.generate(['./']))
|
||||
.toThrowError(`Link "${ListWrapper.toJSON(['./'])}" must include a route name.`);
|
||||
expect(() => router.generate(['/']))
|
||||
.toThrowError(`Link "${ListWrapper.toJSON(['/'])}" must include a route name.`);
|
||||
});
|
||||
|
||||
it('should, when subscribed to, return a disposable subscription', () => {
|
||||
expect(() => {
|
||||
var subscription = router.subscribe((_) => {});
|
||||
ObservableWrapper.dispose(subscription);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should generate URLs from the root component when the path starts with /', () => {
|
||||
router.config(
|
||||
[new Route({path: '/first/...', component: DummyParentComp, name: 'FirstCmp'})]);
|
||||
|
||||
var instruction = router.generate(['/FirstCmp', 'SecondCmp']);
|
||||
expect(stringifyInstruction(instruction)).toEqual('first/second');
|
||||
|
||||
instruction = router.generate(['/FirstCmp/SecondCmp']);
|
||||
expect(stringifyInstruction(instruction)).toEqual('first/second');
|
||||
});
|
||||
|
||||
it('should generate an instruction with terminal async routes',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.registerPrimaryOutlet(outlet);
|
||||
router.config([new AsyncRoute({path: '/first', loader: loader, name: 'FirstCmp'})]);
|
||||
|
||||
var instruction = router.generate(['/FirstCmp']);
|
||||
router.navigateByInstruction(instruction)
|
||||
.then((_) => {
|
||||
expect((<any>outlet).spy('activate')).toHaveBeenCalled();
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should return whether a given instruction is active with isRouteActive',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.registerPrimaryOutlet(outlet)
|
||||
.then((_) => router.config([
|
||||
new Route({path: '/a', component: DummyComponent, name: 'A'}),
|
||||
new Route({path: '/b', component: DummyComponent, name: 'B'})
|
||||
]))
|
||||
.then((_) => router.navigateByUrl('/a'))
|
||||
.then((_) => {
|
||||
var instruction = router.generate(['/A']);
|
||||
var otherInstruction = router.generate(['/B']);
|
||||
|
||||
expect(router.isRouteActive(instruction)).toEqual(true);
|
||||
expect(router.isRouteActive(otherInstruction)).toEqual(false);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should provide the current instruction', inject([AsyncTestCompleter], (async) => {
|
||||
var outlet = makeDummyOutlet();
|
||||
|
||||
router.registerPrimaryOutlet(outlet)
|
||||
.then((_) => router.config([
|
||||
new Route({path: '/a', component: DummyComponent, name: 'A'}),
|
||||
new Route({path: '/b', component: DummyComponent, name: 'B'})
|
||||
]))
|
||||
.then((_) => router.navigateByUrl('/a'))
|
||||
.then((_) => {
|
||||
var instruction = router.generate(['/A']);
|
||||
|
||||
expect(router.currentInstruction).toEqual(instruction);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should provide the root level router from child routers', () => {
|
||||
let childRouter = router.childRouter(DummyComponent);
|
||||
expect(childRouter.root).toBe(router);
|
||||
});
|
||||
|
||||
describe('query string params', () => {
|
||||
it('should use query string params for the root route', () => {
|
||||
router.config(
|
||||
[new Route({path: '/hi/how/are/you', component: DummyComponent, name: 'GreetingUrl'})]);
|
||||
|
||||
var instruction = router.generate(['/GreetingUrl', {'name': 'brad'}]);
|
||||
var path = stringifyInstruction(instruction);
|
||||
expect(path).toEqual('hi/how/are/you?name=brad');
|
||||
});
|
||||
|
||||
it('should preserve the number 1 as a query string value', () => {
|
||||
router.config(
|
||||
[new Route({path: '/hi/how/are/you', component: DummyComponent, name: 'GreetingUrl'})]);
|
||||
|
||||
var instruction = router.generate(['/GreetingUrl', {'name': 1}]);
|
||||
var path = stringifyInstruction(instruction);
|
||||
expect(path).toEqual('hi/how/are/you?name=1');
|
||||
});
|
||||
|
||||
it('should serialize parameters that are not part of the route definition as query string params',
|
||||
() => {
|
||||
router.config([
|
||||
new Route({path: '/one/two/:three', component: DummyComponent, name: 'NumberUrl'})
|
||||
]);
|
||||
|
||||
var instruction = router.generate(['/NumberUrl', {'three': 'three', 'four': 'four'}]);
|
||||
var path = stringifyInstruction(instruction);
|
||||
expect(path).toEqual('one/two/three?four=four');
|
||||
});
|
||||
});
|
||||
|
||||
describe('matrix params', () => {
|
||||
it('should generate matrix params for each non-root component', () => {
|
||||
router.config(
|
||||
[new Route({path: '/first/...', component: DummyParentComp, name: 'FirstCmp'})]);
|
||||
|
||||
var instruction =
|
||||
router.generate(['/FirstCmp', {'key': 'value'}, 'SecondCmp', {'project': 'angular'}]);
|
||||
var path = stringifyInstruction(instruction);
|
||||
expect(path).toEqual('first/second;project=angular?key=value');
|
||||
});
|
||||
|
||||
it('should work with named params', () => {
|
||||
router.config(
|
||||
[new Route({path: '/first/:token/...', component: DummyParentComp, name: 'FirstCmp'})]);
|
||||
|
||||
var instruction =
|
||||
router.generate(['/FirstCmp', {'token': 'min'}, 'SecondCmp', {'author': 'max'}]);
|
||||
var path = stringifyInstruction(instruction);
|
||||
expect(path).toEqual('first/min/second;author=max');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function stringifyInstruction(instruction): string {
|
||||
return instruction.toRootUrl();
|
||||
}
|
||||
|
||||
function loader(): Promise<Type> {
|
||||
return PromiseWrapper.resolve(DummyComponent);
|
||||
}
|
||||
|
||||
class DummyComponent {}
|
||||
|
||||
@RouteConfig([new Route({path: '/second', component: DummyComponent, name: 'SecondCmp'})])
|
||||
class DummyParentComp {
|
||||
}
|
||||
|
||||
function makeDummyOutlet(): RouterOutlet {
|
||||
var ref = new SpyRouterOutlet();
|
||||
ref.spy('canActivate').andCallFake((_) => PromiseWrapper.resolve(true));
|
||||
ref.spy('routerCanReuse').andCallFake((_) => PromiseWrapper.resolve(false));
|
||||
ref.spy('routerCanDeactivate').andCallFake((_) => PromiseWrapper.resolve(true));
|
||||
ref.spy('activate').andCallFake((_) => PromiseWrapper.resolve(true));
|
||||
return <any>ref;
|
||||
}
|
||||
|
||||
class AppCmp {}
|
||||
@@ -0,0 +1,114 @@
|
||||
import {
|
||||
ComponentFixture,
|
||||
AsyncTestCompleter,
|
||||
TestComponentBuilder,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {DefaultRouterUrlSerializer} from 'angular2/src/alt_router/router_url_serializer';
|
||||
import {UrlSegment} from 'angular2/src/alt_router/segments';
|
||||
|
||||
export function main() {
|
||||
describe('url serializer', () => {
|
||||
let url = new DefaultRouterUrlSerializer();
|
||||
|
||||
it('should parse the root url', () => {
|
||||
let tree = url.parse("/");
|
||||
expectSegment(tree.root, "");
|
||||
expect(url.serialize(tree)).toEqual("");
|
||||
});
|
||||
|
||||
it('should parse non-empty urls', () => {
|
||||
let tree = url.parse("one/two");
|
||||
expectSegment(tree.firstChild(tree.root), "one");
|
||||
expectSegment(tree.firstChild(tree.firstChild(tree.root)), "two");
|
||||
expect(url.serialize(tree)).toEqual("/one/two");
|
||||
});
|
||||
|
||||
it("should parse multiple aux routes", () => {
|
||||
let tree = url.parse("/one/two(/three//right:four)/five");
|
||||
let c = tree.children(tree.firstChild(tree.root));
|
||||
|
||||
expectSegment(c[0], "two");
|
||||
expectSegment(c[1], "aux:three");
|
||||
expectSegment(c[2], "right:four");
|
||||
|
||||
expectSegment(tree.firstChild(c[0]), "five");
|
||||
|
||||
expect(url.serialize(tree)).toEqual("/one/two(aux:three//right:four)/five");
|
||||
});
|
||||
|
||||
it("should parse aux routes that have aux routes", () => {
|
||||
let tree = url.parse("/one(/two(/three))");
|
||||
let c = tree.children(tree.root);
|
||||
|
||||
expectSegment(c[0], "one");
|
||||
expectSegment(c[1], "aux:two");
|
||||
expectSegment(c[2], "aux:three");
|
||||
|
||||
expect(url.serialize(tree)).toEqual("/one(aux:two//aux:three)");
|
||||
});
|
||||
|
||||
it("should parse aux routes that have children", () => {
|
||||
let tree = url.parse("/one(/two/three)");
|
||||
let c = tree.children(tree.root);
|
||||
|
||||
expectSegment(c[0], "one");
|
||||
expectSegment(c[1], "aux:two");
|
||||
expectSegment(tree.firstChild(c[1]), "three");
|
||||
|
||||
expect(url.serialize(tree)).toEqual("/one(aux:two/three)");
|
||||
});
|
||||
|
||||
it("should parse an empty aux route definition", () => {
|
||||
let tree = url.parse("/one()");
|
||||
let c = tree.children(tree.root);
|
||||
|
||||
expectSegment(c[0], "one");
|
||||
expect(tree.children(c[0]).length).toEqual(0);
|
||||
|
||||
expect(url.serialize(tree)).toEqual("/one");
|
||||
});
|
||||
|
||||
it("should parse key-value matrix params", () => {
|
||||
let tree = url.parse("/one;a=11a;b=11b(/two;c=22//right:three;d=33)");
|
||||
|
||||
let c = tree.firstChild(tree.root);
|
||||
expectSegment(c, "one");
|
||||
|
||||
let c2 = tree.children(c);
|
||||
expectSegment(c2[0], ";a=11a;b=11b");
|
||||
expectSegment(c2[1], "aux:two");
|
||||
expectSegment(c2[2], "right:three");
|
||||
|
||||
expectSegment(tree.firstChild(c2[1]), ";c=22");
|
||||
expectSegment(tree.firstChild(c2[2]), ";d=33");
|
||||
|
||||
expect(url.serialize(tree)).toEqual("/one;a=11a;b=11b(aux:two;c=22//right:three;d=33)");
|
||||
});
|
||||
|
||||
it("should parse key only matrix params", () => {
|
||||
let tree = url.parse("/one;a");
|
||||
|
||||
let c = tree.firstChild(tree.root);
|
||||
expectSegment(c, "one");
|
||||
expectSegment(tree.firstChild(c), ";a=true");
|
||||
|
||||
expect(url.serialize(tree)).toEqual("/one;a=true");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function expectSegment(segment: UrlSegment, expected: string): void {
|
||||
expect(segment.toString()).toEqual(expected);
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {ParamRoutePath} from '../../../../router/src/rules/route_paths/param_route_path';
|
||||
import {parser, Url} from '../../../../router/src/url_parser';
|
||||
|
||||
export function main() {
|
||||
describe('PathRecognizer', () => {
|
||||
|
||||
it('should throw when given an invalid path', () => {
|
||||
expect(() => new ParamRoutePath('/hi#'))
|
||||
.toThrowError(`Path "/hi#" should not include "#". Use "HashLocationStrategy" instead.`);
|
||||
expect(() => new ParamRoutePath('hi?'))
|
||||
.toThrowError(`Path "hi?" contains "?" which is not allowed in a route config.`);
|
||||
expect(() => new ParamRoutePath('hi;'))
|
||||
.toThrowError(`Path "hi;" contains ";" which is not allowed in a route config.`);
|
||||
expect(() => new ParamRoutePath('hi='))
|
||||
.toThrowError(`Path "hi=" contains "=" which is not allowed in a route config.`);
|
||||
expect(() => new ParamRoutePath('hi('))
|
||||
.toThrowError(`Path "hi(" contains "(" which is not allowed in a route config.`);
|
||||
expect(() => new ParamRoutePath('hi)'))
|
||||
.toThrowError(`Path "hi)" contains ")" which is not allowed in a route config.`);
|
||||
expect(() => new ParamRoutePath('hi//there'))
|
||||
.toThrowError(`Path "hi//there" contains "//" which is not allowed in a route config.`);
|
||||
});
|
||||
|
||||
describe('querystring params', () => {
|
||||
it('should parse querystring params so long as the recognizer is a root', () => {
|
||||
var rec = new ParamRoutePath('/hello/there');
|
||||
var url = parser.parse('/hello/there?name=igor');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'name': 'igor'});
|
||||
});
|
||||
|
||||
it('should return a combined map of parameters with the param expected in the URL path',
|
||||
() => {
|
||||
var rec = new ParamRoutePath('/hello/:name');
|
||||
var url = parser.parse('/hello/paul?topic=success');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'name': 'paul', 'topic': 'success'});
|
||||
});
|
||||
});
|
||||
|
||||
describe('dynamic segments', () => {
|
||||
it('should parse parameters', () => {
|
||||
var rec = new ParamRoutePath('/test/:id');
|
||||
var url = new Url('test', new Url('abc'));
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'id': 'abc'});
|
||||
});
|
||||
|
||||
it('should decode special characters when parsing', () => {
|
||||
var rec = new ParamRoutePath('/test/:id');
|
||||
var url = new Url('test', new Url('abc%25%2F%2f%28%29%3B'));
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'id': 'abc%//();'});
|
||||
});
|
||||
|
||||
it('should generate url', () => {
|
||||
var rec = new ParamRoutePath('/test/:id');
|
||||
expect(rec.generateUrl({'id': 'abc'}).urlPath).toEqual('test/abc');
|
||||
});
|
||||
|
||||
it('should encode special characters when generating', () => {
|
||||
var rec = new ParamRoutePath('/test/:id');
|
||||
expect(rec.generateUrl({'id': 'abc/def/%();'}).urlPath)
|
||||
.toEqual('test/abc%2Fdef%2F%25%28%29%3B');
|
||||
});
|
||||
});
|
||||
|
||||
describe('matrix params', () => {
|
||||
it('should be parsed along with dynamic paths', () => {
|
||||
var rec = new ParamRoutePath('/hello/:id');
|
||||
var url = new Url('hello', new Url('matias', null, null, {'key': 'value'}));
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'id': 'matias', 'key': 'value'});
|
||||
});
|
||||
|
||||
it('should be parsed on a static path', () => {
|
||||
var rec = new ParamRoutePath('/person');
|
||||
var url = new Url('person', null, null, {'name': 'dave'});
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'name': 'dave'});
|
||||
});
|
||||
|
||||
it('should be ignored on a wildcard segment', () => {
|
||||
var rec = new ParamRoutePath('/wild/*everything');
|
||||
var url = parser.parse('/wild/super;variable=value');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'everything': 'super;variable=value'});
|
||||
});
|
||||
|
||||
it('should set matrix param values to true when no value is present', () => {
|
||||
var rec = new ParamRoutePath('/path');
|
||||
var url = new Url('path', null, null, {'one': true, 'two': true, 'three': '3'});
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'one': true, 'two': true, 'three': '3'});
|
||||
});
|
||||
|
||||
it('should be parsed on the final segment of the path', () => {
|
||||
var rec = new ParamRoutePath('/one/two/three');
|
||||
|
||||
var three = new Url('three', null, null, {'c': '3'});
|
||||
var two = new Url('two', three, null, {'b': '2'});
|
||||
var one = new Url('one', two, null, {'a': '1'});
|
||||
|
||||
var match = rec.matchUrl(one);
|
||||
expect(match.allParams).toEqual({'c': '3'});
|
||||
});
|
||||
});
|
||||
|
||||
describe('wildcard segment', () => {
|
||||
it('should return a url path which matches the original url path', () => {
|
||||
var rec = new ParamRoutePath('/wild/*everything');
|
||||
var url = parser.parse('/wild/super;variable=value/anotherPartAfterSlash');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.urlPath).toEqual('wild/super;variable=value/anotherPartAfterSlash');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {GeneratedUrl} from '../../../../router/src/rules/route_paths/route_path';
|
||||
import {RegexRoutePath} from '../../../../router/src/rules/route_paths/regex_route_path';
|
||||
import {parser, Url} from '../../../../router/src/url_parser';
|
||||
|
||||
function emptySerializer(params) {
|
||||
return new GeneratedUrl('', {});
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe('RegexRoutePath', () => {
|
||||
|
||||
it('should throw when given an invalid regex',
|
||||
() => { expect(() => new RegexRoutePath('[abc', emptySerializer)).toThrowError(); });
|
||||
|
||||
it('should parse a single param using capture groups', () => {
|
||||
var rec = new RegexRoutePath('^(.+)$', emptySerializer);
|
||||
var url = parser.parse('hello');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'0': 'hello', '1': 'hello'});
|
||||
});
|
||||
|
||||
it('should parse multiple params using capture groups', () => {
|
||||
var rec = new RegexRoutePath('^(.+)\\.(.+)$', emptySerializer);
|
||||
var url = parser.parse('hello.goodbye');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'0': 'hello.goodbye', '1': 'hello', '2': 'goodbye'});
|
||||
});
|
||||
|
||||
it('should generate a url by calling the provided serializer', () => {
|
||||
function serializer(params) {
|
||||
return new GeneratedUrl(`/a/${params['a']}/b/${params['b']}`, {});
|
||||
}
|
||||
var rec = new RegexRoutePath('/a/(.+)/b/(.+)$', serializer);
|
||||
var params = {a: 'one', b: 'two'};
|
||||
var url = rec.generateUrl(params);
|
||||
expect(url.urlPath).toEqual('/a/one/b/two');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,246 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
||||
import {RouteMatch, PathMatch, RedirectMatch} from '../../../router/src/rules/rules';
|
||||
import {RuleSet} from '../../../router/src/rules/rule_set';
|
||||
import {GeneratedUrl} from '../../../router/src/rules/route_paths/route_path';
|
||||
import {Route, Redirect} from '../../../router/src/route_config/route_config_decorator';
|
||||
import {parser} from '../../../router/src/url_parser';
|
||||
import {PromiseWrapper} from '../../src/facade/promise';
|
||||
|
||||
|
||||
export function main() {
|
||||
describe('RuleSet', () => {
|
||||
var recognizer: RuleSet;
|
||||
|
||||
beforeEach(() => { recognizer = new RuleSet(); });
|
||||
|
||||
|
||||
it('should recognize a static segment', inject([AsyncTestCompleter], (async) => {
|
||||
recognizer.config(new Route({path: '/test', component: DummyCmpA}));
|
||||
recognize(recognizer, '/test')
|
||||
.then((solutions: RouteMatch[]) => {
|
||||
expect(solutions.length).toBe(1);
|
||||
expect(getComponentType(solutions[0])).toEqual(DummyCmpA);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should recognize a single slash', inject([AsyncTestCompleter], (async) => {
|
||||
recognizer.config(new Route({path: '/', component: DummyCmpA}));
|
||||
recognize(recognizer, '/')
|
||||
.then((solutions: RouteMatch[]) => {
|
||||
expect(solutions.length).toBe(1);
|
||||
expect(getComponentType(solutions[0])).toEqual(DummyCmpA);
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should recognize a dynamic segment', inject([AsyncTestCompleter], (async) => {
|
||||
recognizer.config(new Route({path: '/user/:name', component: DummyCmpA}));
|
||||
recognize(recognizer, '/user/brian')
|
||||
.then((solutions: RouteMatch[]) => {
|
||||
expect(solutions.length).toBe(1);
|
||||
expect(getComponentType(solutions[0])).toEqual(DummyCmpA);
|
||||
expect(getParams(solutions[0])).toEqual({'name': 'brian'});
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should recognize a star segment', inject([AsyncTestCompleter], (async) => {
|
||||
recognizer.config(new Route({path: '/first/*rest', component: DummyCmpA}));
|
||||
recognize(recognizer, '/first/second/third')
|
||||
.then((solutions: RouteMatch[]) => {
|
||||
expect(solutions.length).toBe(1);
|
||||
expect(getComponentType(solutions[0])).toEqual(DummyCmpA);
|
||||
expect(getParams(solutions[0])).toEqual({'rest': 'second/third'});
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should recognize a regex', inject([AsyncTestCompleter], (async) => {
|
||||
function emptySerializer(params): GeneratedUrl { return new GeneratedUrl('', {}); }
|
||||
|
||||
recognizer.config(
|
||||
new Route({regex: '^(.+)/(.+)$', serializer: emptySerializer, component: DummyCmpA}));
|
||||
recognize(recognizer, '/first/second')
|
||||
.then((solutions: RouteMatch[]) => {
|
||||
expect(solutions.length).toBe(1);
|
||||
expect(getComponentType(solutions[0])).toEqual(DummyCmpA);
|
||||
expect(getParams(solutions[0]))
|
||||
.toEqual({'0': 'first/second', '1': 'first', '2': 'second'});
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should throw when given two routes that start with the same static segment', () => {
|
||||
recognizer.config(new Route({path: '/hello', component: DummyCmpA}));
|
||||
expect(() => recognizer.config(new Route({path: '/hello', component: DummyCmpB})))
|
||||
.toThrowError('Configuration \'/hello\' conflicts with existing route \'/hello\'');
|
||||
});
|
||||
|
||||
|
||||
it('should throw when given two routes that have dynamic segments in the same order', () => {
|
||||
recognizer.config(new Route({path: '/hello/:person/how/:doyoudou', component: DummyCmpA}));
|
||||
expect(() => recognizer.config(
|
||||
new Route({path: '/hello/:friend/how/:areyou', component: DummyCmpA})))
|
||||
.toThrowError(
|
||||
'Configuration \'/hello/:friend/how/:areyou\' conflicts with existing route \'/hello/:person/how/:doyoudou\'');
|
||||
|
||||
expect(() => recognizer.config(
|
||||
new Redirect({path: '/hello/:pal/how/:goesit', redirectTo: ['/Foo']})))
|
||||
.toThrowError(
|
||||
'Configuration \'/hello/:pal/how/:goesit\' conflicts with existing route \'/hello/:person/how/:doyoudou\'');
|
||||
});
|
||||
|
||||
|
||||
it('should recognize redirects', inject([AsyncTestCompleter], (async) => {
|
||||
recognizer.config(new Route({path: '/b', component: DummyCmpA}));
|
||||
recognizer.config(new Redirect({path: '/a', redirectTo: ['B']}));
|
||||
recognize(recognizer, '/a')
|
||||
.then((solutions: RouteMatch[]) => {
|
||||
expect(solutions.length).toBe(1);
|
||||
var solution = solutions[0];
|
||||
expect(solution).toBeAnInstanceOf(RedirectMatch);
|
||||
if (solution instanceof RedirectMatch) {
|
||||
expect(solution.redirectTo).toEqual(['B']);
|
||||
}
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should generate URLs with params', () => {
|
||||
recognizer.config(new Route({path: '/app/user/:name', component: DummyCmpA, name: 'User'}));
|
||||
var instruction = recognizer.generate('User', {'name': 'misko'});
|
||||
expect(instruction.urlPath).toEqual('app/user/misko');
|
||||
});
|
||||
|
||||
|
||||
it('should generate URLs with numeric params', () => {
|
||||
recognizer.config(new Route({path: '/app/page/:number', component: DummyCmpA, name: 'Page'}));
|
||||
expect(recognizer.generate('Page', {'number': 42}).urlPath).toEqual('app/page/42');
|
||||
});
|
||||
|
||||
|
||||
it('should generate using a serializer', () => {
|
||||
function simpleSerializer(params): GeneratedUrl {
|
||||
var extra = {c: params['c']};
|
||||
return new GeneratedUrl(`/${params['a']}/${params['b']}`, extra);
|
||||
}
|
||||
|
||||
recognizer.config(new Route({
|
||||
name: 'Route1',
|
||||
regex: '^(.+)/(.+)$',
|
||||
serializer: simpleSerializer,
|
||||
component: DummyCmpA
|
||||
}));
|
||||
var params = {a: 'first', b: 'second', c: 'third'};
|
||||
var result = recognizer.generate('Route1', params);
|
||||
expect(result.urlPath).toEqual('/first/second');
|
||||
expect(result.urlParams).toEqual(['c=third']);
|
||||
});
|
||||
|
||||
|
||||
it('should throw in the absence of required params URLs', () => {
|
||||
recognizer.config(new Route({path: 'app/user/:name', component: DummyCmpA, name: 'User'}));
|
||||
expect(() => recognizer.generate('User', {}))
|
||||
.toThrowError('Route generator for \'name\' was not included in parameters passed.');
|
||||
});
|
||||
|
||||
|
||||
it('should throw if the route alias is not TitleCase', () => {
|
||||
expect(() => recognizer.config(
|
||||
new Route({path: 'app/user/:name', component: DummyCmpA, name: 'user'})))
|
||||
.toThrowError(
|
||||
`Route "app/user/:name" with name "user" does not begin with an uppercase letter. Route names should be CamelCase like "User".`);
|
||||
});
|
||||
|
||||
|
||||
describe('params', () => {
|
||||
it('should recognize parameters within the URL path',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
recognizer.config(
|
||||
new Route({path: 'profile/:name', component: DummyCmpA, name: 'User'}));
|
||||
recognize(recognizer, '/profile/matsko?comments=all')
|
||||
.then((solutions: RouteMatch[]) => {
|
||||
expect(solutions.length).toBe(1);
|
||||
expect(getParams(solutions[0])).toEqual({'name': 'matsko', 'comments': 'all'});
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should generate and populate the given static-based route with querystring params',
|
||||
() => {
|
||||
recognizer.config(
|
||||
new Route({path: 'forum/featured', component: DummyCmpA, name: 'ForumPage'}));
|
||||
|
||||
var params = {'start': 10, 'end': 100};
|
||||
|
||||
var result = recognizer.generate('ForumPage', params);
|
||||
expect(result.urlPath).toEqual('forum/featured');
|
||||
expect(result.urlParams).toEqual(['start=10', 'end=100']);
|
||||
});
|
||||
|
||||
|
||||
it('should prefer positional params over query params',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
recognizer.config(
|
||||
new Route({path: 'profile/:name', component: DummyCmpA, name: 'User'}));
|
||||
recognize(recognizer, '/profile/yegor?name=igor')
|
||||
.then((solutions: RouteMatch[]) => {
|
||||
expect(solutions.length).toBe(1);
|
||||
expect(getParams(solutions[0])).toEqual({'name': 'yegor'});
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should ignore matrix params for the top-level component',
|
||||
inject([AsyncTestCompleter], (async) => {
|
||||
recognizer.config(
|
||||
new Route({path: '/home/:subject', component: DummyCmpA, name: 'User'}));
|
||||
recognize(recognizer, '/home;sort=asc/zero;one=1?two=2')
|
||||
.then((solutions: RouteMatch[]) => {
|
||||
expect(solutions.length).toBe(1);
|
||||
expect(getParams(solutions[0])).toEqual({'subject': 'zero', 'two': '2'});
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function recognize(recognizer: RuleSet, url: string): Promise<RouteMatch[]> {
|
||||
var parsedUrl = parser.parse(url);
|
||||
return PromiseWrapper.all(recognizer.recognize(parsedUrl));
|
||||
}
|
||||
|
||||
function getComponentType(routeMatch: RouteMatch): any {
|
||||
if (routeMatch instanceof PathMatch) {
|
||||
return routeMatch.instruction.componentType;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getParams(routeMatch: RouteMatch): any {
|
||||
if (routeMatch instanceof PathMatch) {
|
||||
return routeMatch.instruction.params;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
class DummyCmpA {}
|
||||
class DummyCmpB {}
|
||||
@@ -1,20 +0,0 @@
|
||||
library router.spies;
|
||||
|
||||
import 'package:angular2/platform/common.dart' show PlatformLocation, Location;
|
||||
import 'package:angular2/router.dart';
|
||||
import 'package:angular2/testing_internal.dart';
|
||||
|
||||
@proxy
|
||||
class SpyLocation extends SpyObject implements Location {}
|
||||
|
||||
@proxy
|
||||
class SpyRouter extends SpyObject implements Router {}
|
||||
|
||||
@proxy
|
||||
class SpyRouterOutlet extends SpyObject implements RouterOutlet {}
|
||||
|
||||
class SpyPlatformLocation extends SpyObject implements PlatformLocation {
|
||||
String pathname = null;
|
||||
String search = null;
|
||||
String hash = null;
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import {Location} from '@angular/common';
|
||||
import {Router, RouterOutlet} from '@angular/router';
|
||||
import {SpyObject, proxy} from '@angular/core/testing/testing_internal';
|
||||
|
||||
export class SpyRouter extends SpyObject {
|
||||
constructor() { super(Router); }
|
||||
}
|
||||
|
||||
export class SpyRouterOutlet extends SpyObject {
|
||||
constructor() { super(RouterOutlet); }
|
||||
}
|
||||
|
||||
export class SpyLocation extends SpyObject {
|
||||
constructor() { super(Location); }
|
||||
}
|
||||
|
||||
export class SpyPlatformLocation extends SpyObject {
|
||||
pathname: string = null;
|
||||
search: string = null;
|
||||
hash: string = null;
|
||||
constructor() { super(SpyPlatformLocation); }
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import {
|
||||
ComponentFixture,
|
||||
AsyncTestCompleter,
|
||||
TestComponentBuilder,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {Tree, TreeNode} from 'angular2/src/alt_router/segments';
|
||||
|
||||
export function main() {
|
||||
describe('tree', () => {
|
||||
it("should return the root of the tree", () => {
|
||||
let t = new Tree<any>(new TreeNode<number>(1, []));
|
||||
expect(t.root).toEqual(1);
|
||||
});
|
||||
|
||||
it("should return the parent of a node", () => {
|
||||
let t = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
|
||||
expect(t.parent(1)).toEqual(null);
|
||||
expect(t.parent(2)).toEqual(1);
|
||||
});
|
||||
|
||||
it("should return the children of a node", () => {
|
||||
let t = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
|
||||
expect(t.children(1)).toEqual([2]);
|
||||
expect(t.children(2)).toEqual([]);
|
||||
});
|
||||
|
||||
it("should return the first child of a node", () => {
|
||||
let t = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
|
||||
expect(t.firstChild(1)).toEqual(2);
|
||||
expect(t.firstChild(2)).toEqual(null);
|
||||
});
|
||||
|
||||
it("should return the path to the root", () => {
|
||||
let t = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
|
||||
expect(t.pathFromRoot(2)).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
describe("contains", () => {
|
||||
it("should work", () => {
|
||||
let tree = new Tree<any>(
|
||||
new TreeNode<number>(1, [new TreeNode<number>(2, []), new TreeNode<number>(3, [])]));
|
||||
let subtree1 = new Tree<any>(new TreeNode<number>(1, []));
|
||||
let subtree2 = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
|
||||
let subtree3 = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(3, [])]));
|
||||
let notSubtree1 = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(4, [])]));
|
||||
let notSubtree2 = new Tree<any>(
|
||||
new TreeNode<number>(1, [new TreeNode<number>(2, [new TreeNode<number>(4, [])])]));
|
||||
|
||||
expect(tree.contains(subtree1)).toEqual(true);
|
||||
expect(tree.contains(subtree2)).toEqual(true);
|
||||
expect(tree.contains(subtree3)).toEqual(true);
|
||||
expect(tree.contains(notSubtree1)).toEqual(false);
|
||||
expect(tree.contains(notSubtree2)).toEqual(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {UrlParser, Url} from '../../router/src/url_parser';
|
||||
|
||||
|
||||
export function main() {
|
||||
describe('ParsedUrl', () => {
|
||||
var urlParser: UrlParser;
|
||||
|
||||
beforeEach(() => { urlParser = new UrlParser(); });
|
||||
|
||||
it('should work in a simple case', () => {
|
||||
var url = urlParser.parse('hello/there');
|
||||
expect(url.toString()).toEqual('hello/there');
|
||||
});
|
||||
|
||||
it('should remove the leading slash', () => {
|
||||
var url = urlParser.parse('/hello/there');
|
||||
expect(url.toString()).toEqual('hello/there');
|
||||
});
|
||||
|
||||
it('should parse an empty URL', () => {
|
||||
var url = urlParser.parse('');
|
||||
expect(url.toString()).toEqual('');
|
||||
});
|
||||
|
||||
it('should work with a single aux route', () => {
|
||||
var url = urlParser.parse('hello/there(a)');
|
||||
expect(url.toString()).toEqual('hello/there(a)');
|
||||
});
|
||||
|
||||
it('should work with multiple aux routes', () => {
|
||||
var url = urlParser.parse('hello/there(a//b)');
|
||||
expect(url.toString()).toEqual('hello/there(a//b)');
|
||||
});
|
||||
|
||||
it('should work with children after an aux route', () => {
|
||||
var url = urlParser.parse('hello/there(a//b)/c/d');
|
||||
expect(url.toString()).toEqual('hello/there(a//b)/c/d');
|
||||
});
|
||||
|
||||
it('should work when aux routes have children', () => {
|
||||
var url = urlParser.parse('hello(aa/bb//bb/cc)');
|
||||
expect(url.toString()).toEqual('hello(aa/bb//bb/cc)');
|
||||
});
|
||||
|
||||
it('should parse an aux route with an aux route', () => {
|
||||
var url = urlParser.parse('hello(aa(bb))');
|
||||
expect(url.toString()).toEqual('hello(aa(bb))');
|
||||
});
|
||||
|
||||
it('should simplify an empty aux route definition', () => {
|
||||
var url = urlParser.parse('hello()/there');
|
||||
expect(url.toString()).toEqual('hello/there');
|
||||
});
|
||||
|
||||
it('should parse a key-value matrix param', () => {
|
||||
var url = urlParser.parse('hello/friend;name=bob');
|
||||
expect(url.toString()).toEqual('hello/friend;name=bob');
|
||||
});
|
||||
|
||||
it('should parse multiple key-value matrix params', () => {
|
||||
var url = urlParser.parse('hello/there;greeting=hi;whats=up');
|
||||
expect(url.toString()).toEqual('hello/there;greeting=hi;whats=up');
|
||||
});
|
||||
|
||||
it('should ignore matrix params on the first segment', () => {
|
||||
var url = urlParser.parse('profile;a=1/hi');
|
||||
expect(url.toString()).toEqual('profile/hi');
|
||||
});
|
||||
|
||||
it('should parse a key-only matrix param', () => {
|
||||
var url = urlParser.parse('hello/there;hi');
|
||||
expect(url.toString()).toEqual('hello/there;hi');
|
||||
});
|
||||
|
||||
it('should parse a URL with just a query param', () => {
|
||||
var url = urlParser.parse('?name=bob');
|
||||
expect(url.toString()).toEqual('?name=bob');
|
||||
});
|
||||
|
||||
it('should parse a key-value query param', () => {
|
||||
var url = urlParser.parse('hello/friend?name=bob');
|
||||
expect(url.toString()).toEqual('hello/friend?name=bob');
|
||||
});
|
||||
|
||||
it('should parse multiple key-value query params', () => {
|
||||
var url = urlParser.parse('hello/there?greeting=hi&whats=up');
|
||||
expect(url.params).toEqual({'greeting': 'hi', 'whats': 'up'});
|
||||
expect(url.toString()).toEqual('hello/there?greeting=hi&whats=up');
|
||||
});
|
||||
|
||||
it('should parse a key-only query param', () => {
|
||||
var url = urlParser.parse('hello/there?hi');
|
||||
expect(url.toString()).toEqual('hello/there?hi');
|
||||
});
|
||||
|
||||
it('should parse a route with matrix and query params', () => {
|
||||
var url = urlParser.parse('hello/there;sort=asc;unfiltered?hi&friend=true');
|
||||
expect(url.toString()).toEqual('hello/there;sort=asc;unfiltered?hi&friend=true');
|
||||
});
|
||||
|
||||
it('should parse a route with matrix params and aux routes', () => {
|
||||
var url = urlParser.parse('hello/there;sort=asc(modal)');
|
||||
expect(url.toString()).toEqual('hello/there;sort=asc(modal)');
|
||||
});
|
||||
|
||||
it('should parse an aux route with matrix params', () => {
|
||||
var url = urlParser.parse('hello/there(modal;sort=asc)');
|
||||
expect(url.toString()).toEqual('hello/there(modal;sort=asc)');
|
||||
});
|
||||
|
||||
it('should parse a route with matrix params, aux routes, and query params', () => {
|
||||
var url = urlParser.parse('hello/there;sort=asc(modal)?friend=true');
|
||||
expect(url.toString()).toEqual('hello/there;sort=asc(modal)?friend=true');
|
||||
});
|
||||
it('should allow slashes within query parameters', () => {
|
||||
var url = urlParser.parse(
|
||||
'hello?code=4/B8o0n_Y7XZTb-pVKBw5daZyGAUbMljyLf7uNgTy6ja8&scope=https://www.googleapis.com/auth/analytics');
|
||||
expect(url.toString())
|
||||
.toEqual(
|
||||
'hello?code=4/B8o0n_Y7XZTb-pVKBw5daZyGAUbMljyLf7uNgTy6ja8&scope=https://www.googleapis.com/auth/analytics');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
// future location of testing for router.
|
||||
export var __nothing__;
|
||||
@@ -0,0 +1,27 @@
|
||||
import {SpyLocation} from '@angular/common/testing';
|
||||
import {Location} from '@angular/common';
|
||||
import {Router, RouterOutletMap} from './router';
|
||||
import {RouterUrlSerializer, DefaultRouterUrlSerializer} from './router_url_serializer';
|
||||
import {Component, ComponentResolver} from '@angular/core';
|
||||
|
||||
@Component({selector: 'fake-app-root-comp', template: `<span></span>`})
|
||||
class FakeAppRootCmp {
|
||||
}
|
||||
|
||||
function routerFactory(componentResolver: ComponentResolver, urlSerializer: RouterUrlSerializer,
|
||||
routerOutletMap: RouterOutletMap, location: Location): Router {
|
||||
return new Router(null, FakeAppRootCmp, componentResolver, urlSerializer, routerOutletMap,
|
||||
location);
|
||||
}
|
||||
|
||||
export const ROUTER_FAKE_PROVIDERS: any[] = /*@ts2dart_const*/ [
|
||||
RouterOutletMap,
|
||||
/* @ts2dart_Provider */ {provide: Location, useClass: SpyLocation},
|
||||
/* @ts2dart_Provider */ {provide: RouterUrlSerializer, useClass: DefaultRouterUrlSerializer},
|
||||
/* @ts2dart_Provider */ {
|
||||
provide: Router,
|
||||
useFactory: routerFactory,
|
||||
deps: /*@ts2dart_const*/
|
||||
[ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location]
|
||||
},
|
||||
];
|
||||
@@ -1,28 +0,0 @@
|
||||
{
|
||||
"angularCompilerOptions": {
|
||||
"skipTemplateCodegen": true
|
||||
},
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"declaration": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"module": "es2015",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../../../dist/packages-dist/router/esm",
|
||||
"paths": {
|
||||
"@angular/core": ["../../../dist/packages-dist/core/"],
|
||||
"@angular/common": ["../../../dist/packages-dist/common/"],
|
||||
"@angular/platform-browser": ["../../../dist/packages-dist/platform-browser/"]
|
||||
},
|
||||
"rootDir": ".",
|
||||
"sourceMap": true,
|
||||
"sourceRoot": ".",
|
||||
"target": "es2015"
|
||||
},
|
||||
"files": [
|
||||
"index.ts",
|
||||
"testing.ts",
|
||||
"../../../node_modules/zone.js/dist/zone.js.d.ts"
|
||||
]
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
{
|
||||
"angularCompilerOptions": {
|
||||
"skipTemplateCodegen": true
|
||||
},
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"declaration": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../../../dist/packages-dist/router/",
|
||||
"paths": {
|
||||
"@angular/core": ["../../../dist/packages-dist/core/"],
|
||||
"@angular/common": ["../../../dist/packages-dist/common/"],
|
||||
"@angular/platform-browser": ["../../../dist/packages-dist/platform-browser/"]
|
||||
},
|
||||
"rootDir": ".",
|
||||
"sourceMap": true,
|
||||
"sourceRoot": ".",
|
||||
"target": "es5"
|
||||
},
|
||||
"files": [
|
||||
"index.ts",
|
||||
"testing.ts",
|
||||
"../typings/es6-collections/es6-collections.d.ts",
|
||||
"../typings/es6-promise/es6-promise.d.ts",
|
||||
"../manual_typings/globals.d.ts",
|
||||
"../../../node_modules/zone.js/dist/zone.js.d.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user