2015-05-29 14:58:41 -07:00
|
|
|
import {
|
|
|
|
Map,
|
|
|
|
MapWrapper,
|
|
|
|
StringMap,
|
|
|
|
StringMapWrapper,
|
|
|
|
ListWrapper
|
2015-08-20 14:28:25 -07:00
|
|
|
} from 'angular2/src/core/facade/collection';
|
|
|
|
import {isPresent, isBlank, normalizeBlank, Type} from 'angular2/src/core/facade/lang';
|
|
|
|
import {Promise} from 'angular2/src/core/facade/async';
|
2015-06-30 13:18:51 -07:00
|
|
|
|
|
|
|
import {PathRecognizer} from './path_recognizer';
|
2015-07-17 13:36:53 -07:00
|
|
|
import {Url} from './url_parser';
|
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
|
|
|
/**
|
2015-07-17 13:36:53 -07:00
|
|
|
* `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.
|
2015-05-15 02:05:57 -07:00
|
|
|
*/
|
2015-04-17 09:59:56 -07:00
|
|
|
export class Instruction {
|
2015-07-17 13:36:53 -07:00
|
|
|
constructor(public component: ComponentInstruction, public child: Instruction,
|
|
|
|
public auxInstruction: StringMap<string, Instruction>) {}
|
|
|
|
|
|
|
|
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,
|
2015-08-28 11:29:19 -07:00
|
|
|
public auxUrls: Url[]) {}
|
2015-07-17 13:36:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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 '';
|
2015-06-30 13:18:51 -07:00
|
|
|
}
|
2015-07-17 13:36:53 -07:00
|
|
|
var params = instruction.component.urlParams.length > 0 ?
|
|
|
|
(';' + instruction.component.urlParams.join(';')) :
|
|
|
|
'';
|
|
|
|
return '/' + instruction.component.urlPath + params + stringifyAux(instruction) +
|
|
|
|
stringifyPrimary(instruction.child);
|
|
|
|
}
|
2015-06-30 13:18:51 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
function stringifyAux(instruction: Instruction): string {
|
|
|
|
var routes = [];
|
|
|
|
StringMapWrapper.forEach(instruction.auxInstruction, (auxInstruction, _) => {
|
|
|
|
routes.push(stringifyPrimary(auxInstruction));
|
|
|
|
});
|
|
|
|
if (routes.length > 0) {
|
|
|
|
return '(' + routes.join('//') + ')';
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
2015-07-17 13:36:53 -07:00
|
|
|
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}.
|
2015-08-30 21:20:18 -07:00
|
|
|
*
|
|
|
|
* `ComponentInstruction`s are [https://en.wikipedia.org/wiki/Hash_consing](hash consed). You should
|
|
|
|
* never construct one yourself with "new." Instead, rely on {@link PathRecognizer} to construct
|
|
|
|
* `ComponentInstruction`s.
|
|
|
|
*
|
|
|
|
* You should not modify this object. It should be treated as immutable.
|
2015-07-17 13:36:53 -07:00
|
|
|
*/
|
|
|
|
export class ComponentInstruction {
|
|
|
|
reuse: boolean = false;
|
|
|
|
|
2015-08-28 11:29:19 -07:00
|
|
|
constructor(public urlPath: string, public urlParams: string[],
|
2015-07-17 13:36:53 -07:00
|
|
|
private _recognizer: PathRecognizer, public params: StringMap<string, any> = null) {}
|
|
|
|
|
|
|
|
get componentType() { return this._recognizer.handler.componentType; }
|
|
|
|
|
|
|
|
resolveComponentType(): Promise<Type> { return this._recognizer.handler.resolveComponentType(); }
|
|
|
|
|
|
|
|
get specificity() { return this._recognizer.specificity; }
|
|
|
|
|
|
|
|
get terminal() { return this._recognizer.terminal; }
|
2015-08-10 20:29:40 -04:00
|
|
|
|
|
|
|
routeData(): Object { return this._recognizer.handler.data; }
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|