2015-04-17 09:59:56 -07:00
|
|
|
import {Promise, PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
|
|
|
import {Map, MapWrapper, List, ListWrapper} from 'angular2/src/facade/collection';
|
2015-05-07 21:10:12 -07:00
|
|
|
import {isBlank, isPresent, Type} from 'angular2/src/facade/lang';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-05-01 05:53:38 -07:00
|
|
|
import {RouteRegistry} from './route_registry';
|
2015-04-17 09:59:56 -07:00
|
|
|
import {Pipeline} from './pipeline';
|
|
|
|
import {Instruction} from './instruction';
|
|
|
|
import {RouterOutlet} from './router_outlet';
|
2015-04-21 11:23:23 -07:00
|
|
|
import {Location} from './location';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* # Router
|
|
|
|
* 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.
|
|
|
|
*
|
2015-05-14 13:01:48 -07:00
|
|
|
* ## Concepts
|
|
|
|
* Routers and component instances have a 1:1 correspondence.
|
|
|
|
*
|
|
|
|
* The router holds reference to a number of "outlets." 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 recognizes it and serialize it into an `Instruction`.
|
|
|
|
* The router uses the `RouteRegistry` to get an `Instruction`.
|
|
|
|
*
|
2015-04-17 09:59:56 -07:00
|
|
|
* @exportedAs angular2/router
|
|
|
|
*/
|
|
|
|
export class Router {
|
2015-04-29 15:47:12 -07:00
|
|
|
hostComponent:any;
|
2015-04-17 09:59:56 -07:00
|
|
|
parent:Router;
|
|
|
|
navigating:boolean;
|
|
|
|
lastNavigationAttempt: string;
|
|
|
|
previousUrl:string;
|
|
|
|
|
2015-05-07 21:10:12 -07:00
|
|
|
_currentInstruction:Instruction;
|
|
|
|
|
2015-04-17 09:59:56 -07:00
|
|
|
_pipeline:Pipeline;
|
|
|
|
_registry:RouteRegistry;
|
2015-05-15 02:05:57 -07:00
|
|
|
_outlets:Map<any, RouterOutlet>;
|
2015-04-17 09:59:56 -07:00
|
|
|
_subject:EventEmitter;
|
2015-04-29 15:47:12 -07:00
|
|
|
|
2015-05-14 13:01:48 -07:00
|
|
|
|
|
|
|
constructor(registry:RouteRegistry, pipeline:Pipeline, parent:Router, hostComponent:any) {
|
2015-04-29 15:47:12 -07:00
|
|
|
this.hostComponent = hostComponent;
|
2015-04-17 09:59:56 -07:00
|
|
|
this.navigating = false;
|
|
|
|
this.parent = parent;
|
|
|
|
this.previousUrl = null;
|
|
|
|
this._outlets = MapWrapper.create();
|
|
|
|
this._registry = registry;
|
|
|
|
this._pipeline = pipeline;
|
|
|
|
this._subject = new EventEmitter();
|
2015-05-07 21:10:12 -07:00
|
|
|
this._currentInstruction = null;
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a child router. You probably don't need to use this unless you're writing a reusable component.
|
|
|
|
*/
|
2015-05-14 13:01:48 -07:00
|
|
|
childRouter(hostComponent:any): Router {
|
|
|
|
return new ChildRouter(this, hostComponent);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register an object to notify of route changes. You probably don't need to use this unless you're writing a reusable component.
|
|
|
|
*/
|
2015-05-14 13:01:48 -07:00
|
|
|
registerOutlet(outlet:RouterOutlet, name: string = 'default'): Promise {
|
2015-04-17 09:59:56 -07:00
|
|
|
MapWrapper.set(this._outlets, name, outlet);
|
2015-05-07 21:10:12 -07:00
|
|
|
if (isPresent(this._currentInstruction)) {
|
2015-05-14 13:01:48 -07:00
|
|
|
var childInstruction = this._currentInstruction.getChild(name);
|
2015-05-07 21:10:12 -07:00
|
|
|
return outlet.activate(childInstruction);
|
|
|
|
}
|
|
|
|
return PromiseWrapper.resolve(true);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-05-14 13:01:48 -07:00
|
|
|
* Dynamically update the routing configuration and trigger a navigation.
|
2015-04-17 09:59:56 -07:00
|
|
|
*
|
|
|
|
* # Usage
|
|
|
|
*
|
|
|
|
* ```
|
2015-04-29 15:47:12 -07:00
|
|
|
* router.config({ 'path': '/', 'component': IndexCmp});
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Or:
|
|
|
|
*
|
2015-04-17 09:59:56 -07:00
|
|
|
* ```
|
2015-04-29 15:47:12 -07:00
|
|
|
* router.config([
|
|
|
|
* { 'path': '/', 'component': IndexComp },
|
|
|
|
* { 'path': '/user/:id', 'component': UserComp },
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
2015-04-17 09:59:56 -07:00
|
|
|
*/
|
2015-05-14 15:24:35 +02:00
|
|
|
config(config:any): Promise {
|
2015-04-29 15:47:12 -07:00
|
|
|
if (config instanceof List) {
|
2015-05-01 05:53:38 -07:00
|
|
|
config.forEach((configObject) => {
|
2015-04-29 15:47:12 -07:00
|
|
|
this._registry.config(this.hostComponent, configObject);
|
2015-05-14 15:24:35 +02:00
|
|
|
});
|
2015-04-29 15:47:12 -07:00
|
|
|
} else {
|
|
|
|
this._registry.config(this.hostComponent, config);
|
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
return this.renavigate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate to a URL. Returns a promise that resolves to the canonical URL for the route.
|
2015-05-14 13:01:48 -07:00
|
|
|
*
|
|
|
|
* 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.
|
2015-04-17 09:59:56 -07:00
|
|
|
*/
|
|
|
|
navigate(url:string):Promise {
|
|
|
|
if (this.navigating) {
|
|
|
|
return PromiseWrapper.resolve(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastNavigationAttempt = url;
|
|
|
|
|
2015-05-07 21:10:12 -07:00
|
|
|
var matchedInstruction = this.recognize(url);
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-05-07 21:10:12 -07:00
|
|
|
if (isBlank(matchedInstruction)) {
|
2015-04-17 09:59:56 -07:00
|
|
|
return PromiseWrapper.resolve(false);
|
|
|
|
}
|
|
|
|
|
2015-05-14 13:01:48 -07:00
|
|
|
if (isPresent(this._currentInstruction)) {
|
2015-05-07 21:10:12 -07:00
|
|
|
matchedInstruction.reuseComponentsFrom(this._currentInstruction);
|
|
|
|
}
|
|
|
|
|
2015-04-17 09:59:56 -07:00
|
|
|
this._startNavigating();
|
|
|
|
|
2015-05-14 13:01:48 -07:00
|
|
|
var result = this.commit(matchedInstruction)
|
2015-04-17 09:59:56 -07:00
|
|
|
.then((_) => {
|
2015-05-14 13:01:48 -07:00
|
|
|
ObservableWrapper.callNext(this._subject, matchedInstruction.accumulatedUrl);
|
2015-05-07 21:10:12 -07:00
|
|
|
this._finishNavigating();
|
|
|
|
});
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
PromiseWrapper.catchError(result, (_) => this._finishNavigating());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-05-14 15:24:35 +02:00
|
|
|
_startNavigating(): void {
|
2015-04-17 09:59:56 -07:00
|
|
|
this.navigating = true;
|
|
|
|
}
|
|
|
|
|
2015-05-14 15:24:35 +02:00
|
|
|
_finishNavigating(): void {
|
2015-04-17 09:59:56 -07:00
|
|
|
this.navigating = false;
|
|
|
|
}
|
|
|
|
|
2015-05-14 13:01:48 -07:00
|
|
|
|
2015-04-17 09:59:56 -07:00
|
|
|
/**
|
|
|
|
* Subscribe to URL updates from the router
|
|
|
|
*/
|
2015-05-14 15:24:35 +02:00
|
|
|
subscribe(onNext): void {
|
2015-04-17 09:59:56 -07:00
|
|
|
ObservableWrapper.subscribe(this._subject, onNext);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-14 13:01:48 -07:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
commit(instruction:Instruction):Promise {
|
|
|
|
this._currentInstruction = instruction;
|
|
|
|
|
|
|
|
// collect all outlets that do not have a corresponding child instruction
|
|
|
|
// and remove them from the internal map of child outlets
|
|
|
|
var toDeactivate = ListWrapper.create();
|
|
|
|
MapWrapper.forEach(this._outlets, (outlet, outletName) => {
|
|
|
|
if (!instruction.hasChild(outletName)) {
|
|
|
|
MapWrapper.delete(this._outlets, outletName);
|
|
|
|
ListWrapper.push(toDeactivate, outlet);
|
2015-05-07 21:10:12 -07:00
|
|
|
}
|
2015-05-14 13:01:48 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return PromiseWrapper.all(ListWrapper.map(toDeactivate, (outlet) => outlet.deactivate()))
|
|
|
|
.then((_) => this.activate(instruction));
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-05-14 13:01:48 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively remove all components contained by this router's outlets.
|
|
|
|
* Calls deactivate hooks on all descendant components
|
|
|
|
*/
|
|
|
|
deactivate():Promise {
|
|
|
|
return this._eachOutletAsync((outlet) => outlet.deactivate);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-05-14 13:01:48 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively activate.
|
|
|
|
* Calls the "activate" hook on descendant components.
|
|
|
|
*/
|
|
|
|
activate(instruction:Instruction):Promise {
|
|
|
|
return this._eachOutletAsync((outlet, name) => outlet.activate(instruction.getChild(name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_eachOutletAsync(fn):Promise {
|
2015-04-17 09:59:56 -07:00
|
|
|
return mapObjAsync(this._outlets, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a URL, returns an instruction representing the component graph
|
|
|
|
*/
|
2015-05-14 15:24:35 +02:00
|
|
|
recognize(url:string): Instruction {
|
2015-05-01 05:53:38 -07:00
|
|
|
return this._registry.recognize(url, this.hostComponent);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigates to either the last URL successfully navigated to, or the last URL requested if the router has yet to successfully navigate.
|
|
|
|
*/
|
|
|
|
renavigate():Promise {
|
|
|
|
var destination = isBlank(this.previousUrl) ? this.lastNavigationAttempt : this.previousUrl;
|
|
|
|
if (this.navigating || isBlank(destination)) {
|
|
|
|
return PromiseWrapper.resolve(false);
|
|
|
|
}
|
|
|
|
return this.navigate(destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a URL from a component name and optional map of parameters. The URL is relative to the app's base href.
|
|
|
|
*/
|
2015-05-14 15:24:35 +02:00
|
|
|
generate(name:string, params:StringMap<string, string>): string {
|
2015-05-01 05:53:38 -07:00
|
|
|
return this._registry.generate(name, params, this.hostComponent);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class RootRouter extends Router {
|
2015-05-14 13:01:48 -07:00
|
|
|
_location:Location;
|
|
|
|
|
2015-05-01 05:53:38 -07:00
|
|
|
constructor(registry:RouteRegistry, pipeline:Pipeline, location:Location, hostComponent:Type) {
|
2015-05-14 13:01:48 -07:00
|
|
|
super(registry, pipeline, null, hostComponent);
|
|
|
|
this._location = location;
|
2015-05-03 20:22:41 -07:00
|
|
|
this._location.subscribe((change) => this.navigate(change['url']));
|
2015-05-01 05:53:38 -07:00
|
|
|
this._registry.configFromComponent(hostComponent);
|
2015-04-29 15:47:12 -07:00
|
|
|
this.navigate(location.path());
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
2015-05-14 13:01:48 -07:00
|
|
|
|
|
|
|
commit(instruction):Promise {
|
|
|
|
return super.commit(instruction).then((_) => {
|
|
|
|
this._location.go(instruction.accumulatedUrl);
|
|
|
|
});
|
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class ChildRouter extends Router {
|
2015-04-29 15:47:12 -07:00
|
|
|
constructor(parent:Router, hostComponent) {
|
2015-05-14 13:01:48 -07:00
|
|
|
super(parent._registry, parent._pipeline, parent, hostComponent);
|
2015-04-17 09:59:56 -07:00
|
|
|
this.parent = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 15:24:35 +02:00
|
|
|
function mapObjAsync(obj:Map, fn): Promise {
|
2015-04-17 09:59:56 -07:00
|
|
|
return PromiseWrapper.all(mapObj(obj, fn));
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapObj(obj:Map, fn):List {
|
|
|
|
var result = ListWrapper.create();
|
|
|
|
MapWrapper.forEach(obj, (value, key) => ListWrapper.push(result, fn(value, key)));
|
|
|
|
return result;
|
|
|
|
}
|