1
1
/** @packageDocumentation @publicapi @module url */
2
+ import { StateDeclaration } from '../state' ;
2
3
import { UrlMatcher } from './urlMatcher' ;
3
4
import { isString , isDefined , isFunction } from '../common/predicates' ;
4
5
import { UIRouter } from '../router' ;
5
6
import { identity , extend } from '../common/common' ;
6
- import { is , pattern } from '../common/hof' ;
7
+ import { is , or , pattern } from '../common/hof' ;
7
8
import { StateObject } from '../state/stateObject' ;
8
9
import { RawParams } from '../params/interface' ;
9
10
import {
@@ -29,7 +30,7 @@ import {
29
30
* @internalapi
30
31
*/
31
32
export class UrlRuleFactory {
32
- static isUrlRule = obj => obj && [ 'type' , 'match' , 'handler' ] . every ( key => isDefined ( obj [ key ] ) ) ;
33
+ static isUrlRule = ( obj ) => obj && [ 'type' , 'match' , 'handler' ] . every ( ( key ) => isDefined ( obj [ key ] ) ) ;
33
34
34
35
constructor ( public router : UIRouter ) { }
35
36
@@ -38,14 +39,14 @@ export class UrlRuleFactory {
38
39
}
39
40
40
41
create (
41
- what : string | UrlMatcher | StateObject | RegExp | UrlRuleMatchFn ,
42
+ what : string | UrlMatcher | StateObject | StateDeclaration | RegExp | UrlRuleMatchFn ,
42
43
handler ?: string | UrlRuleHandlerFn
43
44
) : UrlRule {
44
- const isState = StateObject . isState ;
45
+ const { isState, isStateDeclaration } = StateObject ;
45
46
const makeRule = pattern ( [
46
47
[ isString , ( _what : string ) => makeRule ( this . compile ( _what ) ) ] ,
47
48
[ is ( UrlMatcher ) , ( _what : UrlMatcher ) => this . fromUrlMatcher ( _what , handler ) ] ,
48
- [ isState , ( _what : StateObject ) => this . fromState ( _what , this . router ) ] ,
49
+ [ or ( isState , isStateDeclaration ) , ( _what : StateObject | StateDeclaration ) => this . fromState ( _what , this . router ) ] ,
49
50
[ is ( RegExp ) , ( _what : RegExp ) => this . fromRegExp ( _what , handler ) ] ,
50
51
[ isFunction , ( _what : UrlRuleMatchFn ) => new BaseUrlRule ( _what , handler as UrlRuleHandlerFn ) ] ,
51
52
] ) ;
@@ -107,9 +108,9 @@ export class UrlRuleFactory {
107
108
// - Some optional parameters, some matched
108
109
// - Some optional parameters, all matched
109
110
function matchPriority ( params : RawParams ) : number {
110
- const optional = urlMatcher . parameters ( ) . filter ( param => param . isOptional ) ;
111
+ const optional = urlMatcher . parameters ( ) . filter ( ( param ) => param . isOptional ) ;
111
112
if ( ! optional . length ) return 0.000001 ;
112
- const matched = optional . filter ( param => params [ param . id ] ) ;
113
+ const matched = optional . filter ( ( param ) => params [ param . id ] ) ;
113
114
return matched . length / optional . length ;
114
115
}
115
116
@@ -128,7 +129,9 @@ export class UrlRuleFactory {
128
129
* // Starts a transition to 'foo' with params: { fooId: '123', barId: '456' }
129
130
* ```
130
131
*/
131
- fromState ( state : StateObject , router : UIRouter ) : StateRule {
132
+ fromState ( stateOrDecl : StateObject | StateDeclaration , router : UIRouter ) : StateRule {
133
+ const state = StateObject . isStateDeclaration ( stateOrDecl ) ? stateOrDecl . $$state ( ) : stateOrDecl ;
134
+
132
135
/**
133
136
* Handles match by transitioning to matched state
134
137
*
@@ -213,7 +216,7 @@ export class BaseUrlRule implements UrlRule {
213
216
_group : number ;
214
217
type : UrlRuleType = 'RAW' ;
215
218
handler : UrlRuleHandlerFn ;
216
- matchPriority = match => 0 - this . $id ;
219
+ matchPriority = ( match ) => 0 - this . $id ;
217
220
218
221
constructor ( public match : UrlRuleMatchFn , handler ?: UrlRuleHandlerFn ) {
219
222
this . handler = handler || identity ;
0 commit comments