refactor(router): make names consistent

This commit is contained in:
vsavkin 2016-05-04 10:59:20 -07:00
parent 1a0aea67a0
commit 12637a761c
3 changed files with 44 additions and 44 deletions

View File

@ -30,7 +30,7 @@ import {isPresent, isBlank} from '../facade/lang';
*/ */
@Directive({selector: 'router-outlet'}) @Directive({selector: 'router-outlet'})
export class RouterOutlet { export class RouterOutlet {
private _loaded: ComponentRef<any>; private _activated: ComponentRef<any>;
public outletMap: RouterOutletMap; public outletMap: RouterOutletMap;
constructor(parentOutletMap: RouterOutletMap, private _location: ViewContainerRef, constructor(parentOutletMap: RouterOutletMap, private _location: ViewContainerRef,
@ -38,29 +38,29 @@ export class RouterOutlet {
parentOutletMap.registerOutlet(isBlank(name) ? DEFAULT_OUTLET_NAME : name, this); parentOutletMap.registerOutlet(isBlank(name) ? DEFAULT_OUTLET_NAME : name, this);
} }
unload(): void { deactivate(): void {
this._loaded.destroy(); this._activated.destroy();
this._loaded = null; this._activated = null;
} }
/** /**
* Returns the loaded component. * Returns the loaded component.
*/ */
get loadedComponent(): Object { return isPresent(this._loaded) ? this._loaded.instance : null; } get component(): Object { return isPresent(this._activated) ? this._activated.instance : null; }
/** /**
* Returns true is the outlet is not empty. * Returns true is the outlet is not empty.
*/ */
get isLoaded(): boolean { return isPresent(this._loaded); } get isActivated(): boolean { return isPresent(this._activated); }
/** /**
* Called by the Router to instantiate a new component. * Called by the Router to instantiate a new component.
*/ */
load(factory: ComponentFactory<any>, providers: ResolvedReflectiveProvider[], activate(factory: ComponentFactory<any>, providers: ResolvedReflectiveProvider[],
outletMap: RouterOutletMap): ComponentRef<any> { outletMap: RouterOutletMap): ComponentRef<any> {
this.outletMap = outletMap; this.outletMap = outletMap;
let inj = ReflectiveInjector.fromResolvedProviders(providers, this._location.parentInjector); let inj = ReflectiveInjector.fromResolvedProviders(providers, this._location.parentInjector);
this._loaded = this._location.createComponent(factory, this._location.length, inj, []); this._activated = this._location.createComponent(factory, this._location.length, inj, []);
return this._loaded; return this._activated;
} }
} }

View File

@ -41,7 +41,7 @@ export class RouterOutletMap {
* and `router.routeTree`. * and `router.routeTree`.
*/ */
export class Router { export class Router {
private _prevTree: RouteTree; private _routeTree: RouteTree;
private _urlTree: UrlTree; private _urlTree: UrlTree;
private _locationSubscription: any; private _locationSubscription: any;
private _changes: EventEmitter<void> = new EventEmitter<void>(); private _changes: EventEmitter<void> = new EventEmitter<void>();
@ -53,7 +53,7 @@ export class Router {
private _componentResolver: ComponentResolver, private _componentResolver: ComponentResolver,
private _urlSerializer: RouterUrlSerializer, private _urlSerializer: RouterUrlSerializer,
private _routerOutletMap: RouterOutletMap, private _location: Location) { private _routerOutletMap: RouterOutletMap, private _location: Location) {
this._prevTree = this._createInitialTree(); this._routeTree = this._createInitialTree();
this._setUpLocationChangeListener(); this._setUpLocationChangeListener();
this.navigateByUrl(this._location.path()); this.navigateByUrl(this._location.path());
} }
@ -66,7 +66,7 @@ export class Router {
/** /**
* Returns the current route tree. * Returns the current route tree.
*/ */
get routeTree(): RouteTree { return this._prevTree; } get routeTree(): RouteTree { return this._routeTree; }
/** /**
* An observable or url changes from the router. * An observable or url changes from the router.
@ -137,8 +137,8 @@ export class Router {
* ``` * ```
*/ */
createUrlTree(commands: any[], segment?: RouteSegment): UrlTree { createUrlTree(commands: any[], segment?: RouteSegment): UrlTree {
let s = isPresent(segment) ? segment : this._prevTree.root; let s = isPresent(segment) ? segment : this._routeTree.root;
return link(s, this._prevTree, this.urlTree, commands); return link(s, this._routeTree, this.urlTree, commands);
} }
/** /**
@ -161,11 +161,11 @@ export class Router {
this._urlTree = url; this._urlTree = url;
return recognize(this._componentResolver, this._rootComponentType, url) return recognize(this._componentResolver, this._rootComponentType, url)
.then(currTree => { .then(currTree => {
return new _LoadSegments(currTree, this._prevTree) return new _ActivateSegments(currTree, this._routeTree)
.load(this._routerOutletMap, this._rootComponent) .activate(this._routerOutletMap, this._rootComponent)
.then(updated => { .then(updated => {
if (updated) { if (updated) {
this._prevTree = currTree; this._routeTree = currTree;
this._location.go(this._urlSerializer.serialize(this._urlTree)); this._location.go(this._urlSerializer.serialize(this._urlTree));
this._changes.emit(null); this._changes.emit(null);
} }
@ -175,13 +175,13 @@ export class Router {
} }
class _LoadSegments { class _ActivateSegments {
private deactivations: Object[][] = []; private deactivations: Object[][] = [];
private performMutation: boolean = true; private performMutation: boolean = true;
constructor(private currTree: RouteTree, private prevTree: RouteTree) {} constructor(private currTree: RouteTree, private prevTree: RouteTree) {}
load(parentOutletMap: RouterOutletMap, rootComponent: Object): Promise<boolean> { activate(parentOutletMap: RouterOutletMap, rootComponent: Object): Promise<boolean> {
let prevRoot = isPresent(this.prevTree) ? rootNode(this.prevTree) : null; let prevRoot = isPresent(this.prevTree) ? rootNode(this.prevTree) : null;
let currRoot = rootNode(this.currTree); let currRoot = rootNode(this.currTree);
@ -189,7 +189,7 @@ class _LoadSegments {
.then(res => { .then(res => {
this.performMutation = true; this.performMutation = true;
if (res) { if (res) {
this.loadChildSegments(currRoot, prevRoot, parentOutletMap, [rootComponent]); this.activateChildSegments(currRoot, prevRoot, parentOutletMap, [rootComponent]);
} }
return res; return res;
}); });
@ -198,7 +198,7 @@ class _LoadSegments {
private canDeactivate(currRoot: TreeNode<RouteSegment>, prevRoot: TreeNode<RouteSegment>, private canDeactivate(currRoot: TreeNode<RouteSegment>, prevRoot: TreeNode<RouteSegment>,
outletMap: RouterOutletMap, rootComponent: Object): Promise<boolean> { outletMap: RouterOutletMap, rootComponent: Object): Promise<boolean> {
this.performMutation = false; this.performMutation = false;
this.loadChildSegments(currRoot, prevRoot, outletMap, [rootComponent]); this.activateChildSegments(currRoot, prevRoot, outletMap, [rootComponent]);
let allPaths = PromiseWrapper.all(this.deactivations.map(r => this.checkCanDeactivatePath(r))); let allPaths = PromiseWrapper.all(this.deactivations.map(r => this.checkCanDeactivatePath(r)));
return allPaths.then((values: boolean[]) => values.filter(v => v).length === values.length); return allPaths.then((values: boolean[]) => values.filter(v => v).length === values.length);
@ -218,8 +218,8 @@ class _LoadSegments {
return curr; return curr;
} }
private loadChildSegments(currNode: TreeNode<RouteSegment>, prevNode: TreeNode<RouteSegment>, private activateChildSegments(currNode: TreeNode<RouteSegment>, prevNode: TreeNode<RouteSegment>,
outletMap: RouterOutletMap, components: Object[]): void { outletMap: RouterOutletMap, components: Object[]): void {
let prevChildren = isPresent(prevNode) ? let prevChildren = isPresent(prevNode) ?
prevNode.children.reduce( prevNode.children.reduce(
(m, c) => { (m, c) => {
@ -230,38 +230,38 @@ class _LoadSegments {
{}; {};
currNode.children.forEach(c => { currNode.children.forEach(c => {
this.loadSegments(c, prevChildren[c.value.outlet], outletMap, components); this.activateSegments(c, prevChildren[c.value.outlet], outletMap, components);
StringMapWrapper.delete(prevChildren, c.value.outlet); StringMapWrapper.delete(prevChildren, c.value.outlet);
}); });
StringMapWrapper.forEach(prevChildren, StringMapWrapper.forEach(prevChildren,
(v, k) => this.unloadOutlet(outletMap._outlets[k], components)); (v, k) => this.deactivateOutlet(outletMap._outlets[k], components));
} }
loadSegments(currNode: TreeNode<RouteSegment>, prevNode: TreeNode<RouteSegment>, activateSegments(currNode: TreeNode<RouteSegment>, prevNode: TreeNode<RouteSegment>,
parentOutletMap: RouterOutletMap, components: Object[]): void { parentOutletMap: RouterOutletMap, components: Object[]): void {
let curr = currNode.value; let curr = currNode.value;
let prev = isPresent(prevNode) ? prevNode.value : null; let prev = isPresent(prevNode) ? prevNode.value : null;
let outlet = this.getOutlet(parentOutletMap, currNode.value); let outlet = this.getOutlet(parentOutletMap, currNode.value);
if (equalSegments(curr, prev)) { if (equalSegments(curr, prev)) {
this.loadChildSegments(currNode, prevNode, outlet.outletMap, this.activateChildSegments(currNode, prevNode, outlet.outletMap,
components.concat([outlet.loadedComponent])); components.concat([outlet.component]));
} else { } else {
this.unloadOutlet(outlet, components); this.deactivateOutlet(outlet, components);
if (this.performMutation) { if (this.performMutation) {
let outletMap = new RouterOutletMap(); let outletMap = new RouterOutletMap();
let loadedComponent = this.loadNewSegment(outletMap, curr, prev, outlet); let component = this.activateNewSegments(outletMap, curr, prev, outlet);
this.loadChildSegments(currNode, prevNode, outletMap, components.concat([loadedComponent])); this.activateChildSegments(currNode, prevNode, outletMap, components.concat([component]));
} }
} }
} }
private loadNewSegment(outletMap: RouterOutletMap, curr: RouteSegment, prev: RouteSegment, private activateNewSegments(outletMap: RouterOutletMap, curr: RouteSegment, prev: RouteSegment,
outlet: RouterOutlet): Object { outlet: RouterOutlet): Object {
let resolved = ReflectiveInjector.resolve( let resolved = ReflectiveInjector.resolve(
[provide(RouterOutletMap, {useValue: outletMap}), provide(RouteSegment, {useValue: curr})]); [provide(RouterOutletMap, {useValue: outletMap}), provide(RouteSegment, {useValue: curr})]);
let ref = outlet.load(routeSegmentComponentFactory(curr), resolved, outletMap); let ref = outlet.activate(routeSegmentComponentFactory(curr), resolved, outletMap);
if (hasLifecycleHook("routerOnActivate", ref.instance)) { if (hasLifecycleHook("routerOnActivate", ref.instance)) {
ref.instance.routerOnActivate(curr, prev, this.currTree, this.prevTree); ref.instance.routerOnActivate(curr, prev, this.currTree, this.prevTree);
} }
@ -280,14 +280,14 @@ class _LoadSegments {
return outlet; return outlet;
} }
private unloadOutlet(outlet: RouterOutlet, components: Object[]): void { private deactivateOutlet(outlet: RouterOutlet, components: Object[]): void {
if (isPresent(outlet) && outlet.isLoaded) { if (isPresent(outlet) && outlet.isActivated) {
StringMapWrapper.forEach(outlet.outletMap._outlets, StringMapWrapper.forEach(outlet.outletMap._outlets,
(v, k) => this.unloadOutlet(v, components)); (v, k) => this.deactivateOutlet(v, components));
if (this.performMutation) { if (this.performMutation) {
outlet.unload(); outlet.deactivate();
} else { } else {
this.deactivations.push(components.concat([outlet.loadedComponent])); this.deactivations.push(components.concat([outlet.component]));
} }
} }
} }

View File

@ -95,7 +95,7 @@ export function main() {
.toHaveText('team 22 { hello victor, aux: simple }'); .toHaveText('team 22 { hello victor, aux: simple }');
}))); })));
it('should unload outlets', fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { it('should deactivate outlets', fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
let fixture = tcb.createFakeAsync(RootCmp); let fixture = tcb.createFakeAsync(RootCmp);
router.navigateByUrl('/team/22/user/victor(/simple)'); router.navigateByUrl('/team/22/user/victor(/simple)');
@ -107,7 +107,7 @@ export function main() {
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello victor, aux: }'); expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello victor, aux: }');
}))); })));
it('should unload nested outlets', it('should deactivate nested outlets',
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
let fixture = tcb.createFakeAsync(RootCmp); let fixture = tcb.createFakeAsync(RootCmp);
@ -136,7 +136,7 @@ export function main() {
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello fedor, aux: }'); expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello fedor, aux: }');
}))); })));
it('should not unload the route if can deactivate returns false', it('should not deactivate the route if can deactivate returns false',
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => { fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
let fixture = tcb.createFakeAsync(RootCmp); let fixture = tcb.createFakeAsync(RootCmp);