2015-04-17 09:59:56 -07:00
|
|
|
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
|
2015-07-07 15:44:29 -07:00
|
|
|
import {StringMapWrapper} from 'angular2/src/facade/collection';
|
2015-05-14 13:01:48 -07:00
|
|
|
import {isBlank, isPresent} from 'angular2/src/facade/lang';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
import {Directive, Attribute} from 'angular2/src/core/annotations/decorators';
|
2015-05-15 15:55:26 -07:00
|
|
|
import {DynamicComponentLoader, ComponentRef, ElementRef} from 'angular2/core';
|
2015-06-29 11:15:49 -07:00
|
|
|
import {Injector, bind, Dependency, undefinedValue} from 'angular2/di';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
import * as routerMod from './router';
|
2015-07-07 15:44:29 -07:00
|
|
|
import {Instruction, RouteParams} from './instruction';
|
|
|
|
import * as hookMod from './lifecycle_annotations';
|
|
|
|
import {hasLifecycleHook} from './route_lifecycle_reflector';
|
2015-05-14 13:01:48 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A router outlet is a placeholder that Angular dynamically fills based on the application's route.
|
|
|
|
*
|
|
|
|
* ## Use
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* <router-outlet></router-outlet>
|
|
|
|
* ```
|
|
|
|
*/
|
2015-07-07 15:44:29 -07:00
|
|
|
@Directive({selector: 'router-outlet'})
|
2015-04-17 09:59:56 -07:00
|
|
|
export class RouterOutlet {
|
2015-07-07 15:44:29 -07:00
|
|
|
childRouter: routerMod.Router = null;
|
|
|
|
|
2015-06-29 10:37:55 +02:00
|
|
|
private _componentRef: ComponentRef = null;
|
|
|
|
private _currentInstruction: Instruction = null;
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-06-29 11:15:49 -07:00
|
|
|
constructor(private _elementRef: ElementRef, private _loader: DynamicComponentLoader,
|
|
|
|
private _parentRouter: routerMod.Router, @Attribute('name') nameAttr: string) {
|
2015-05-21 13:59:14 -07:00
|
|
|
// TODO: reintroduce with new // sibling routes
|
|
|
|
// if (isBlank(nameAttr)) {
|
|
|
|
// nameAttr = 'default';
|
|
|
|
//}
|
|
|
|
this._parentRouter.registerOutlet(this);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-05-14 13:01:48 -07:00
|
|
|
/**
|
2015-05-21 13:59:14 -07:00
|
|
|
* Given an instruction, update the contents of this outlet.
|
2015-05-14 13:01:48 -07:00
|
|
|
*/
|
2015-07-07 15:44:29 -07:00
|
|
|
commit(instruction: Instruction): Promise<any> {
|
|
|
|
var next;
|
|
|
|
if (instruction.reuse) {
|
|
|
|
next = this._reuse(instruction);
|
|
|
|
} else {
|
|
|
|
next = this.deactivate(instruction).then((_) => this._activate(instruction));
|
|
|
|
}
|
|
|
|
return next.then((_) => this._commitChild(instruction));
|
|
|
|
}
|
|
|
|
|
|
|
|
private _commitChild(instruction: Instruction): Promise<any> {
|
|
|
|
if (isPresent(this.childRouter)) {
|
|
|
|
return this.childRouter.commit(instruction.child);
|
|
|
|
} else {
|
|
|
|
return PromiseWrapper.resolve(true);
|
2015-05-14 13:01:48 -07:00
|
|
|
}
|
2015-07-07 15:44:29 -07:00
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-07-07 15:44:29 -07:00
|
|
|
private _activate(instruction: Instruction): Promise<any> {
|
|
|
|
var previousInstruction = this._currentInstruction;
|
2015-05-19 10:35:28 -07:00
|
|
|
this._currentInstruction = instruction;
|
2015-07-07 15:44:29 -07:00
|
|
|
this.childRouter = this._parentRouter.childRouter(instruction.component);
|
|
|
|
|
|
|
|
var bindings = Injector.resolve([
|
|
|
|
bind(RouteParams)
|
|
|
|
.toValue(new RouteParams(instruction.params())),
|
|
|
|
bind(routerMod.Router).toValue(this.childRouter)
|
|
|
|
]);
|
|
|
|
return this._loader.loadNextToLocation(instruction.component, this._elementRef, bindings)
|
2015-05-29 14:58:41 -07:00
|
|
|
.then((componentRef) => {
|
|
|
|
this._componentRef = componentRef;
|
2015-07-07 15:44:29 -07:00
|
|
|
if (hasLifecycleHook(hookMod.onActivate, instruction.component)) {
|
|
|
|
return this._componentRef.instance.onActivate(instruction, previousInstruction);
|
|
|
|
}
|
2015-05-29 14:58:41 -07:00
|
|
|
});
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-05-21 13:59:14 -07:00
|
|
|
|
2015-07-07 15:44:29 -07:00
|
|
|
/**
|
|
|
|
* Called by Router during recognition phase
|
|
|
|
*/
|
|
|
|
canDeactivate(nextInstruction: Instruction): Promise<boolean> {
|
|
|
|
if (isBlank(this._currentInstruction)) {
|
|
|
|
return PromiseWrapper.resolve(true);
|
|
|
|
}
|
|
|
|
if (hasLifecycleHook(hookMod.canDeactivate, this._currentInstruction.component)) {
|
|
|
|
return PromiseWrapper.resolve(
|
|
|
|
this._componentRef.instance.canDeactivate(nextInstruction, this._currentInstruction));
|
|
|
|
}
|
|
|
|
return PromiseWrapper.resolve(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by Router during recognition phase
|
|
|
|
*/
|
|
|
|
canReuse(nextInstruction: Instruction): Promise<boolean> {
|
|
|
|
var result;
|
|
|
|
if (isBlank(this._currentInstruction) ||
|
|
|
|
this._currentInstruction.component != nextInstruction.component) {
|
|
|
|
result = false;
|
|
|
|
} else if (hasLifecycleHook(hookMod.canReuse, this._currentInstruction.component)) {
|
|
|
|
result = this._componentRef.instance.canReuse(nextInstruction, this._currentInstruction);
|
|
|
|
} else {
|
|
|
|
result = nextInstruction == this._currentInstruction ||
|
|
|
|
StringMapWrapper.equals(nextInstruction.params(), this._currentInstruction.params());
|
|
|
|
}
|
|
|
|
return PromiseWrapper.resolve(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private _reuse(instruction): Promise<any> {
|
|
|
|
var previousInstruction = this._currentInstruction;
|
|
|
|
this._currentInstruction = instruction;
|
|
|
|
return PromiseWrapper.resolve(
|
|
|
|
hasLifecycleHook(hookMod.onReuse, this._currentInstruction.component) ?
|
|
|
|
this._componentRef.instance.onReuse(instruction, previousInstruction) :
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deactivate(nextInstruction: Instruction): Promise<any> {
|
|
|
|
return (isPresent(this.childRouter) ?
|
|
|
|
this.childRouter.deactivate(isPresent(nextInstruction) ? nextInstruction.child :
|
|
|
|
null) :
|
|
|
|
PromiseWrapper.resolve(true))
|
|
|
|
.then((_) => {
|
|
|
|
if (isPresent(this._componentRef) && isPresent(this._currentInstruction) &&
|
|
|
|
hasLifecycleHook(hookMod.onDeactivate, this._currentInstruction.component)) {
|
|
|
|
return this._componentRef.instance.onDeactivate(nextInstruction,
|
|
|
|
this._currentInstruction);
|
|
|
|
}
|
|
|
|
})
|
2015-05-21 13:59:14 -07:00
|
|
|
.then((_) => {
|
|
|
|
if (isPresent(this._componentRef)) {
|
|
|
|
this._componentRef.dispose();
|
|
|
|
this._componentRef = null;
|
|
|
|
}
|
|
|
|
});
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
}
|