2015-05-29 14:58:41 -07:00
|
|
|
import {
|
|
|
|
Map,
|
|
|
|
MapWrapper,
|
|
|
|
StringMap,
|
|
|
|
StringMapWrapper,
|
|
|
|
List,
|
|
|
|
ListWrapper
|
|
|
|
} from 'angular2/src/facade/collection';
|
2015-05-14 15:24:35 +02:00
|
|
|
import {isPresent, normalizeBlank} from 'angular2/src/facade/lang';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
export class RouteParams {
|
2015-05-29 14:58:41 -07:00
|
|
|
constructor(public params: StringMap<string, string>) {}
|
2015-05-14 15:24:35 +02:00
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
get(param: string): string { return normalizeBlank(StringMapWrapper.get(this.params, param)); }
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-05-15 02:05:57 -07:00
|
|
|
/**
|
|
|
|
* An `Instruction` represents the component hierarchy of the application based on a given route
|
|
|
|
*/
|
2015-04-17 09:59:56 -07:00
|
|
|
export class Instruction {
|
2015-05-29 14:58:41 -07:00
|
|
|
component: any;
|
2015-05-21 13:59:14 -07:00
|
|
|
child: Instruction;
|
2015-05-14 13:01:48 -07:00
|
|
|
|
|
|
|
// the part of the URL captured by this instruction
|
2015-05-29 14:58:41 -07:00
|
|
|
capturedUrl: string;
|
2015-05-14 13:01:48 -07:00
|
|
|
|
|
|
|
// the part of the URL captured by this instruction and all children
|
2015-05-29 14:58:41 -07:00
|
|
|
accumulatedUrl: string;
|
|
|
|
|
|
|
|
params: StringMap<string, string>;
|
|
|
|
reuse: boolean;
|
|
|
|
specificity: number;
|
|
|
|
|
2015-05-21 13:59:14 -07:00
|
|
|
constructor({params, component, child, matchedUrl, parentSpecificity}: {
|
2015-05-29 14:58:41 -07:00
|
|
|
params?: StringMap<string, any>,
|
|
|
|
component?: any,
|
2015-05-21 13:59:14 -07:00
|
|
|
child?: Instruction,
|
2015-05-29 14:58:41 -07:00
|
|
|
matchedUrl?: string,
|
|
|
|
parentSpecificity?: number
|
|
|
|
} = {}) {
|
2015-05-07 21:10:12 -07:00
|
|
|
this.reuse = false;
|
2015-05-14 13:01:48 -07:00
|
|
|
this.capturedUrl = matchedUrl;
|
|
|
|
this.accumulatedUrl = matchedUrl;
|
2015-05-15 02:05:57 -07:00
|
|
|
this.specificity = parentSpecificity;
|
2015-05-21 13:59:14 -07:00
|
|
|
if (isPresent(child)) {
|
|
|
|
this.child = child;
|
|
|
|
this.specificity += child.specificity;
|
|
|
|
var childUrl = child.accumulatedUrl;
|
2015-04-17 09:59:56 -07:00
|
|
|
if (isPresent(childUrl)) {
|
2015-05-14 13:01:48 -07:00
|
|
|
this.accumulatedUrl += childUrl;
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
} else {
|
2015-05-21 13:59:14 -07:00
|
|
|
this.child = null;
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
this.component = component;
|
|
|
|
this.params = params;
|
|
|
|
}
|
|
|
|
|
2015-05-21 13:59:14 -07:00
|
|
|
hasChild(): boolean { return isPresent(this.child); }
|
2015-05-07 21:10:12 -07:00
|
|
|
|
|
|
|
/**
|
2015-05-29 14:58:41 -07:00
|
|
|
* Takes a currently active instruction and sets a reuse flag on each of this instruction's
|
|
|
|
* children
|
2015-05-07 21:10:12 -07:00
|
|
|
*/
|
2015-05-29 14:58:41 -07:00
|
|
|
reuseComponentsFrom(oldInstruction: Instruction): void {
|
2015-05-21 13:59:14 -07:00
|
|
|
var nextInstruction = this;
|
|
|
|
while (nextInstruction.reuse = shouldReuseComponent(nextInstruction, oldInstruction) &&
|
|
|
|
isPresent(oldInstruction = oldInstruction.child) &&
|
|
|
|
isPresent(nextInstruction = nextInstruction.child))
|
|
|
|
;
|
2015-05-07 21:10:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
function shouldReuseComponent(instr1: Instruction, instr2: Instruction): boolean {
|
2015-05-07 21:10:12 -07:00
|
|
|
return instr1.component == instr2.component &&
|
2015-05-29 14:58:41 -07:00
|
|
|
StringMapWrapper.equals(instr1.params, instr2.params);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|