@@ -6,9 +6,11 @@ import {
66 List ,
77 ListWrapper
88} from 'angular2/src/facade/collection' ;
9- import { isPresent , isBlank , normalizeBlank } from 'angular2/src/facade/lang' ;
9+ import { isPresent , isBlank , normalizeBlank , Type } from 'angular2/src/facade/lang' ;
10+ import { Promise } from 'angular2/src/facade/async' ;
1011
1112import { PathRecognizer } from './path_recognizer' ;
13+ import { Url } from './url_parser' ;
1214
1315export class RouteParams {
1416 constructor ( public params : StringMap < string , string > ) { }
@@ -18,34 +20,82 @@ export class RouteParams {
1820
1921
2022/**
21- * An `Instruction` represents the component hierarchy of the application based on a given route
23+ * `Instruction` is a tree of `ComponentInstructions`, with all the information needed
24+ * to transition each component in the app to a given route, including all auxiliary routes.
25+ *
26+ * This is a public API.
2227 */
2328export class Instruction {
24- // "capturedUrl" is the part of the URL captured by this instruction
25- // "accumulatedUrl" is the part of the URL captured by this instruction and all children
26- accumulatedUrl : string ;
27- reuse : boolean = false ;
28- specificity : number ;
29-
30- constructor ( public component : any , public capturedUrl : string ,
31- private _recognizer : PathRecognizer , public child : Instruction = null ,
32- private _params : StringMap < string , any > = null ) {
33- this . accumulatedUrl = capturedUrl ;
34- this . specificity = _recognizer . specificity ;
35- if ( isPresent ( child ) ) {
36- this . child = child ;
37- this . specificity += child . specificity ;
38- var childUrl = child . accumulatedUrl ;
39- if ( isPresent ( childUrl ) ) {
40- this . accumulatedUrl += childUrl ;
41- }
42- }
29+ constructor ( public component : ComponentInstruction , public child : Instruction ,
30+ public auxInstruction : StringMap < string , Instruction > ) { }
31+
32+ replaceChild ( child : Instruction ) : Instruction {
33+ return new Instruction ( this . component , child , this . auxInstruction ) ;
34+ }
35+ }
36+
37+ /**
38+ * Represents a partially completed instruction during recognition that only has the
39+ * primary (non-aux) route instructions matched.
40+ *
41+ * `PrimaryInstruction` is an internal class used by `RouteRecognizer` while it's
42+ * figuring out where to navigate.
43+ */
44+ export class PrimaryInstruction {
45+ constructor ( public component : ComponentInstruction , public child : PrimaryInstruction ,
46+ public auxUrls : List < Url > ) { }
47+ }
48+
49+ export function stringifyInstruction ( instruction : Instruction ) : string {
50+ var params = instruction . component . urlParams . length > 0 ?
51+ ( '?' + instruction . component . urlParams . join ( '&' ) ) :
52+ '' ;
53+
54+ return instruction . component . urlPath + stringifyAux ( instruction ) +
55+ stringifyPrimary ( instruction . child ) + params ;
56+ }
57+
58+ function stringifyPrimary ( instruction : Instruction ) : string {
59+ if ( isBlank ( instruction ) ) {
60+ return '' ;
4361 }
62+ var params = instruction . component . urlParams . length > 0 ?
63+ ( ';' + instruction . component . urlParams . join ( ';' ) ) :
64+ '' ;
65+ return '/' + instruction . component . urlPath + params + stringifyAux ( instruction ) +
66+ stringifyPrimary ( instruction . child ) ;
67+ }
4468
45- params ( ) : StringMap < string , string > {
46- if ( isBlank ( this . _params ) ) {
47- this . _params = this . _recognizer . parseParams ( this . capturedUrl ) ;
48- }
49- return this . _params ;
69+ function stringifyAux ( instruction : Instruction ) : string {
70+ var routes = [ ] ;
71+ StringMapWrapper . forEach ( instruction . auxInstruction , ( auxInstruction , _ ) => {
72+ routes . push ( stringifyPrimary ( auxInstruction ) ) ;
73+ } ) ;
74+ if ( routes . length > 0 ) {
75+ return '(' + routes . join ( '//' ) + ')' ;
5076 }
77+ return '' ;
78+ }
79+
80+
81+ /**
82+ * A `ComponentInstruction` represents the route state for a single component. An `Instruction` is
83+ * composed of a tree of these `ComponentInstruction`s.
84+ *
85+ * `ComponentInstructions` is a public API. Instances of `ComponentInstruction` are passed
86+ * to route lifecycle hooks, like {@link CanActivate}.
87+ */
88+ export class ComponentInstruction {
89+ reuse : boolean = false ;
90+
91+ constructor ( public urlPath : string , public urlParams : List < string > ,
92+ private _recognizer : PathRecognizer , public params : StringMap < string , any > = null ) { }
93+
94+ get componentType ( ) { return this . _recognizer . handler . componentType ; }
95+
96+ resolveComponentType ( ) : Promise < Type > { return this . _recognizer . handler . resolveComponentType ( ) ; }
97+
98+ get specificity ( ) { return this . _recognizer . specificity ; }
99+
100+ get terminal ( ) { return this . _recognizer . terminal ; }
51101}
0 commit comments