1- import { RegExp , RegExpWrapper , StringWrapper , isPresent } from 'angular2/src/facade/lang' ;
1+ import { RegExp , RegExpWrapper , StringWrapper , isPresent , BaseException } from 'angular2/src/facade/lang' ;
22import { Map , MapWrapper , List , ListWrapper , StringMap , StringMapWrapper } from 'angular2/src/facade/collection' ;
33
44import { PathRecognizer } from './path_recognizer' ;
55
6+ /**
7+ * `RouteRecognizer` is responsible for recognizing routes for a single component.
8+ * It is consumed by `RouteRegistry`, which knows how to recognize an entire hierarchy of components.
9+ */
610export class RouteRecognizer {
711 names :Map < string , PathRecognizer > ;
812 redirects:Map < string , string > ;
@@ -20,14 +24,25 @@ export class RouteRecognizer {
2024
2125 addConfig ( path :string , handler :any , alias :string = null ) : void {
2226 var recognizer = new PathRecognizer ( path , handler ) ;
27+ MapWrapper . forEach ( this . matchers , ( matcher , _ ) => {
28+ if ( recognizer . regex . toString ( ) == matcher . regex . toString ( ) ) {
29+ throw new BaseException ( `Configuration '${ path } ' conflicts with existing route '${ matcher . path } '` ) ;
30+ }
31+ } ) ;
2332 MapWrapper . set ( this . matchers , recognizer . regex , recognizer ) ;
2433 if ( isPresent ( alias ) ) {
2534 MapWrapper . set ( this . names , alias , recognizer ) ;
2635 }
2736 }
2837
29- recognize ( url :string ) :List < StringMap > {
30- var solutions = [ ] ;
38+
39+ /**
40+ * Given a URL, returns a list of `RouteMatch`es, which are partial recognitions for some route.
41+ *
42+ */
43+ recognize ( url :string ) :List < RouteMatch > {
44+ var solutions = ListWrapper . create ( ) ;
45+
3146 MapWrapper . forEach ( this . redirects , ( target , path ) => {
3247 //TODO: "/" redirect case
3348 if ( StringWrapper . startsWith ( url , path ) ) {
@@ -38,21 +53,20 @@ export class RouteRecognizer {
3853 MapWrapper . forEach ( this . matchers , ( pathRecognizer , regex ) => {
3954 var match ;
4055 if ( isPresent ( match = RegExpWrapper . firstMatch ( regex , url ) ) ) {
41- var solution = StringMapWrapper . create ( ) ;
42- StringMapWrapper . set ( solution , 'cost' , pathRecognizer . cost ) ;
43- StringMapWrapper . set ( solution , 'handler' , pathRecognizer . handler ) ;
44- StringMapWrapper . set ( solution , 'params' , pathRecognizer . parseParams ( url ) ) ;
45-
4656 //TODO(btford): determine a good generic way to deal with terminal matches
47- if ( url == '/' ) {
48- StringMapWrapper . set ( solution , 'matchedUrl' , '/' ) ;
49- StringMapWrapper . set ( solution , 'unmatchedUrl' , '' ) ;
50- } else {
51- StringMapWrapper . set ( solution , 'matchedUrl' , match [ 0 ] ) ;
52- var unmatchedUrl = StringWrapper . substring ( url , match [ 0 ] . length ) ;
53- StringMapWrapper . set ( solution , 'unmatchedUrl' , unmatchedUrl ) ;
57+ var matchedUrl = '/' ;
58+ var unmatchedUrl = '' ;
59+ if ( url != '/' ) {
60+ matchedUrl = match [ 0 ] ;
61+ unmatchedUrl = StringWrapper . substring ( url , match [ 0 ] . length ) ;
5462 }
55- ListWrapper . push ( solutions , solution ) ;
63+ ListWrapper . push ( solutions , new RouteMatch ( {
64+ specificity : pathRecognizer . specificity ,
65+ handler : pathRecognizer . handler ,
66+ params : pathRecognizer . parseParams ( url ) ,
67+ matchedUrl : matchedUrl ,
68+ unmatchedUrl : unmatchedUrl
69+ } ) ) ;
5670 }
5771 } ) ;
5872
@@ -68,3 +82,20 @@ export class RouteRecognizer {
6882 return isPresent ( pathRecognizer ) ? pathRecognizer . generate ( params ) : null ;
6983 }
7084}
85+
86+ export class RouteMatch {
87+ specificity :number ;
88+ handler:StringMap < string , any > ;
89+ params:StringMap < string , string > ;
90+ matchedUrl:string ;
91+ unmatchedUrl:string ;
92+ constructor ( { specificity, handler, params, matchedUrl, unmatchedUrl} :
93+ { specificity :number , handler :StringMap , params :StringMap , matchedUrl :string , unmatchedUrl :string } = { } ) {
94+
95+ this . specificity = specificity ;
96+ this . handler = handler ;
97+ this . params = params ;
98+ this . matchedUrl = matchedUrl ;
99+ this . unmatchedUrl = unmatchedUrl ;
100+ }
101+ }
0 commit comments