Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/angular2/src/facade/lang.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bool isBlank(obj) => obj == null;
bool isString(obj) => obj is String;
bool isFunction(obj) => obj is Function;
bool isType(obj) => obj is Type;
bool isMap(obj) => obj is Map;

String stringify(obj) => obj.toString();

Expand Down
4 changes: 4 additions & 0 deletions modules/angular2/src/facade/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export function isType(obj): boolean {
return isFunction(obj);
}

export function isMap(obj): boolean {
return typeof obj === 'object' && obj !== null;
}

export function stringify(token): string {
if (typeof token === 'string') {
return token;
Expand Down
68 changes: 14 additions & 54 deletions modules/angular2/src/router/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
List,
ListWrapper
} from 'angular2/src/facade/collection';
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
import {isPresent, normalizeBlank} from 'angular2/src/facade/lang';

export class RouteParams {
Expand All @@ -20,7 +19,7 @@ export class RouteParams {
*/
export class Instruction {
component: any;
private _children: StringMap<string, Instruction>;
child: Instruction;

// the part of the URL captured by this instruction
capturedUrl: string;
Expand All @@ -32,86 +31,47 @@ export class Instruction {
reuse: boolean;
specificity: number;

constructor({params, component, children, matchedUrl, parentSpecificity}: {
constructor({params, component, child, matchedUrl, parentSpecificity}: {
params?: StringMap<string, any>,
component?: any,
children?: StringMap<string, Instruction>,
child?: Instruction,
matchedUrl?: string,
parentSpecificity?: number
} = {}) {
this.reuse = false;
this.capturedUrl = matchedUrl;
this.accumulatedUrl = matchedUrl;
this.specificity = parentSpecificity;
if (isPresent(children)) {
this._children = children;
var childUrl;
StringMapWrapper.forEach(this._children, (child, _) => {
childUrl = child.accumulatedUrl;
this.specificity += child.specificity;
});
if (isPresent(child)) {
this.child = child;
this.specificity += child.specificity;
var childUrl = child.accumulatedUrl;
if (isPresent(childUrl)) {
this.accumulatedUrl += childUrl;
}
} else {
this._children = StringMapWrapper.create();
this.child = null;
}
this.component = component;
this.params = params;
}

hasChild(outletName: string): boolean {
return StringMapWrapper.contains(this._children, outletName);
}

/**
* Returns the child instruction with the given outlet name
*/
getChild(outletName: string): Instruction {
return StringMapWrapper.get(this._children, outletName);
}

/**
* (child:Instruction, outletName:string) => {}
*/
forEachChild(fn: Function): void { StringMapWrapper.forEach(this._children, fn); }

/**
* Does a synchronous, breadth-first traversal of the graph of instructions.
* Takes a function with signature:
* (child:Instruction, outletName:string) => {}
*/
traverseSync(fn: Function): void {
this.forEachChild(fn);
this.forEachChild((childInstruction, _) => childInstruction.traverseSync(fn));
}

hasChild(): boolean { return isPresent(this.child); }

/**
* Takes a currently active instruction and sets a reuse flag on each of this instruction's
* children
*/
reuseComponentsFrom(oldInstruction: Instruction): void {
this.traverseSync((childInstruction, outletName) => {
var oldInstructionChild = oldInstruction.getChild(outletName);
if (shouldReuseComponent(childInstruction, oldInstructionChild)) {
childInstruction.reuse = true;
}
});
var nextInstruction = this;
while (nextInstruction.reuse = shouldReuseComponent(nextInstruction, oldInstruction) &&
isPresent(oldInstruction = oldInstruction.child) &&
isPresent(nextInstruction = nextInstruction.child))
;
}
}

function shouldReuseComponent(instr1: Instruction, instr2: Instruction): boolean {
return instr1.component == instr2.component &&
StringMapWrapper.equals(instr1.params, instr2.params);
}

function mapObjAsync(obj: StringMap<string, any>, fn): Promise<List<any>> {
return PromiseWrapper.all(mapObj(obj, fn));
}

function mapObj(obj: StringMap<any, any>, fn: Function): List<any> {
var result = ListWrapper.create();
StringMapWrapper.forEach(obj, (value, key) => ListWrapper.push(result, fn(value, key)));
return result;
}
2 changes: 1 addition & 1 deletion modules/angular2/src/router/route_config_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {List, Map} from 'angular2/src/facade/collection';
*
* Supported keys:
* - `path` (required)
* - `component`, `components`, `redirectTo` (requires exactly one of these)
* - `component`, `redirectTo` (requires exactly one of these)
* - `as` (optional)
*/
@CONST()
Expand Down
Loading