1
1
import { UrlMatcher } from "./urlMatcher" ;
2
2
import { isString , isDefined } from "../common/predicates" ;
3
3
import { UIRouter } from "../router" ;
4
- import { Obj , extend , identity } from "../common/common" ;
5
- import { RawParams } from "../params/interface" ;
4
+ import { extend , identity } from "../common/common" ;
6
5
import { is , pattern } from "../common/hof" ;
7
6
import { State } from "../state/stateObject" ;
8
- import { UIRouterGlobals } from "../globals" ;
7
+ import { RawParams } from "../params/interface" ;
8
+ import { UrlRule , UrlRuleMatchFn , UrlRuleHandlerFn , UrlRuleType } from "./interface" ;
9
9
import { StateService } from "../state/stateService" ;
10
+ import { UIRouterGlobals } from "../globals" ;
10
11
11
12
/**
12
13
* Creates a [[UrlRule]]
@@ -25,6 +26,9 @@ export class UrlRuleFactory {
25
26
return this . router . urlMatcherFactory . compile ( pattern ) ;
26
27
}
27
28
29
+ static isUrlRule = obj =>
30
+ obj && [ 'type' , 'match' , 'handler' , 'priority' ] . every ( key => isDefined ( obj [ key ] ) ) ;
31
+
28
32
create ( what : string | State | UrlMatcher | RegExp , handler ?) : UrlRule {
29
33
const makeRule = pattern ( [
30
34
[ isString , ( ) => this . fromString ( what as string , handler ) ] ,
@@ -38,7 +42,7 @@ export class UrlRuleFactory {
38
42
}
39
43
40
44
fromString = ( pattern : string , handler : string | UrlMatcher | UrlRuleHandlerFn ) =>
41
- extend ( this . fromMatcher ( this . compile ( pattern ) , handler ) , { type : UrlRuleType . STRING } ) ;
45
+ extend ( this . fromMatcher ( this . compile ( pattern ) , handler ) , { type : " STRING" } ) ;
42
46
43
47
fromMatcher = ( urlMatcher : UrlMatcher , handler : string | UrlMatcher | UrlRuleHandlerFn ) =>
44
48
new UrlMatcherRule ( urlMatcher , ( isString ( handler ) ? this . compile ( handler ) : handler ) ) ;
@@ -53,38 +57,6 @@ export class UrlRuleFactory {
53
57
new RawUrlRule ( match ) ;
54
58
}
55
59
56
- /** @return truthy or falsey */
57
- export interface UrlRuleMatchFn {
58
- ( path : string , search : Obj , hash : string ) : any ;
59
- }
60
-
61
- /** Handler invoked when a rule is matched */
62
- export interface UrlRuleHandlerFn {
63
- ( matchObject : any , path ?: string , search ?: Obj , hash ?: string ) : ( string | boolean | void ) ;
64
- }
65
-
66
- export enum UrlRuleType { STATE , URLMATCHER , STRING , REGEXP , RAW , OTHER }
67
- export interface UrlRule {
68
- /** The type of the rule */
69
- type : UrlRuleType ;
70
-
71
- /**
72
- * This function should match the url and return match details
73
- */
74
- match : UrlRuleMatchFn ;
75
-
76
- /**
77
- * This function is called after the rule matched the url.
78
- * This function handles the rule match event.
79
- */
80
- handler : UrlRuleHandlerFn ;
81
-
82
- priority : number ;
83
- }
84
-
85
- export const isUrlRule = obj =>
86
- obj && [ 'type' , 'match' , 'handler' , 'priority' ] . every ( key => isDefined ( obj [ key ] ) ) ;
87
-
88
60
/**
89
61
* A UrlRule which matches based on a regular expression
90
62
*
@@ -119,7 +91,7 @@ export const isUrlRule = obj =>
119
91
* ```
120
92
*/
121
93
export class RegExpRule implements UrlRule {
122
- type = UrlRuleType . REGEXP ;
94
+ type : UrlRuleType = " REGEXP" ;
123
95
handler : UrlRuleHandlerFn ;
124
96
priority = 0 ;
125
97
@@ -184,7 +156,7 @@ export class RegExpRule implements UrlRule {
184
156
* ```
185
157
*/
186
158
export class UrlMatcherRule implements UrlRule {
187
- type = UrlRuleType . URLMATCHER ;
159
+ type : UrlRuleType = " URLMATCHER" ;
188
160
handler : UrlRuleHandlerFn ;
189
161
priority = 0 ;
190
162
@@ -211,7 +183,7 @@ export class UrlMatcherRule implements UrlRule {
211
183
* ```
212
184
*/
213
185
export class StateUrlRule implements UrlRule {
214
- type = UrlRuleType . STATE ;
186
+ type : UrlRuleType = " STATE" ;
215
187
priority = 0 ;
216
188
$state : StateService ;
217
189
globals : UIRouterGlobals ;
@@ -248,10 +220,10 @@ export class StateUrlRule implements UrlRule {
248
220
* The value from the `match` function is passed through as the `handler` result.
249
221
*/
250
222
export class RawUrlRule implements UrlRule {
251
- type = UrlRuleType . RAW ;
223
+ type : UrlRuleType = " RAW" ;
252
224
priority = 0 ;
253
225
254
226
constructor ( public match : UrlRuleMatchFn ) { }
255
227
256
228
handler = identity
257
- }
229
+ }
0 commit comments