import { Map, MapWrapper, StringMap, StringMapWrapper, List, ListWrapper } from 'angular2/src/facade/collection'; import {isPresent, isBlank, normalizeBlank, Type} from 'angular2/src/facade/lang'; import {Promise} from 'angular2/src/facade/async'; import {PathRecognizer} from './path_recognizer'; import {Url} from './url_parser'; export class RouteParams { constructor(public params: StringMap) {} get(param: string): string { return normalizeBlank(StringMapWrapper.get(this.params, param)); } } /** * `Instruction` is a tree of `ComponentInstructions`, with all the information needed * to transition each component in the app to a given route, including all auxiliary routes. * * This is a public API. */ export class Instruction { constructor(public component: ComponentInstruction, public child: Instruction, public auxInstruction: StringMap) {} replaceChild(child: Instruction): Instruction { return new Instruction(this.component, child, this.auxInstruction); } } /** * Represents a partially completed instruction during recognition that only has the * primary (non-aux) route instructions matched. * * `PrimaryInstruction` is an internal class used by `RouteRecognizer` while it's * figuring out where to navigate. */ export class PrimaryInstruction { constructor(public component: ComponentInstruction, public child: PrimaryInstruction, public auxUrls: List) {} } export function stringifyInstruction(instruction: Instruction): string { var params = instruction.component.urlParams.length > 0 ? ('?' + instruction.component.urlParams.join('&')) : ''; return instruction.component.urlPath + stringifyAux(instruction) + stringifyPrimary(instruction.child) + params; } function stringifyPrimary(instruction: Instruction): string { if (isBlank(instruction)) { return ''; } var params = instruction.component.urlParams.length > 0 ? (';' + instruction.component.urlParams.join(';')) : ''; return '/' + instruction.component.urlPath + params + stringifyAux(instruction) + stringifyPrimary(instruction.child); } function stringifyAux(instruction: Instruction): string { var routes = []; StringMapWrapper.forEach(instruction.auxInstruction, (auxInstruction, _) => { routes.push(stringifyPrimary(auxInstruction)); }); if (routes.length > 0) { return '(' + routes.join('//') + ')'; } return ''; } /** * A `ComponentInstruction` represents the route state for a single component. An `Instruction` is * composed of a tree of these `ComponentInstruction`s. * * `ComponentInstructions` is a public API. Instances of `ComponentInstruction` are passed * to route lifecycle hooks, like {@link CanActivate}. */ export class ComponentInstruction { reuse: boolean = false; constructor(public urlPath: string, public urlParams: List, private _recognizer: PathRecognizer, public params: StringMap = null) {} get componentType() { return this._recognizer.handler.componentType; } resolveComponentType(): Promise { return this._recognizer.handler.resolveComponentType(); } get specificity() { return this._recognizer.specificity; } get terminal() { return this._recognizer.terminal; } }