@@ -13,20 +13,20 @@ export function StateBuilder(root, matcher, $urlMatcherFactoryProvider) {
13
13
14
14
let self = this , builders = {
15
15
16
- parent : function ( state ) {
16
+ parent : [ function ( state ) {
17
17
if ( state === root ( ) ) return null ;
18
18
return matcher . find ( self . parentName ( state ) ) || root ( ) ;
19
- } ,
19
+ } ] ,
20
20
21
- data : function ( state ) {
21
+ data : [ function ( state ) {
22
22
if ( state . parent && state . parent . data ) {
23
23
state . data = state . self . data = extend ( { } , state . parent . data , state . data ) ;
24
24
}
25
25
return state . data ;
26
- } ,
26
+ } ] ,
27
27
28
28
// Build a URLMatcher if necessary, either via a relative or absolute URL
29
- url : function ( state ) {
29
+ url : [ function ( state ) {
30
30
const parsed = parseUrl ( state . url ) , parent = state . parent ;
31
31
const url = ! parsed ? state . url : $urlMatcherFactoryProvider . compile ( parsed . val , {
32
32
params : state . params || { } ,
@@ -39,26 +39,26 @@ export function StateBuilder(root, matcher, $urlMatcherFactoryProvider) {
39
39
if ( ! url ) return null ;
40
40
if ( ! $urlMatcherFactoryProvider . isMatcher ( url ) ) throw new Error ( `Invalid url '${ url } ' in state '${ state } '` ) ;
41
41
return ( parsed && parsed . root ) ? url : ( ( parent && parent . navigable ) || root ( ) ) . url . append ( url ) ;
42
- } ,
42
+ } ] ,
43
43
44
44
// Keep track of the closest ancestor state that has a URL (i.e. is navigable)
45
- navigable : function ( state ) {
45
+ navigable : [ function ( state ) {
46
46
return ( state !== root ( ) ) && state . url ? state : ( state . parent ? state . parent . navigable : null ) ;
47
- } ,
47
+ } ] ,
48
48
49
- params : function ( state ) : { [ key : string ] : Param } {
49
+ params : [ function ( state ) : { [ key : string ] : Param } {
50
50
const makeConfigParam = ( config : any , id : string ) => Param . fromConfig ( id , null , config ) ;
51
51
let urlParams : Param [ ] = ( state . url && state . url . parameters ( { inherit : false } ) ) || [ ] ;
52
52
let nonUrlParams : Param [ ] = values ( map ( omit ( state . params || { } , urlParams . map ( prop ( 'id' ) ) ) , makeConfigParam ) ) ;
53
53
return urlParams . concat ( nonUrlParams ) . map ( p => [ p . id , p ] ) . reduce ( applyPairs , { } ) ;
54
- } ,
54
+ } ] ,
55
55
56
56
// If there is no explicit multi-view configuration, make one up so we don't have
57
57
// to handle both cases in the view directive later. Note that having an explicit
58
58
// 'views' property will mean the default unnamed view properties are ignored. This
59
59
// is also a good time to resolve view names to absolute names, so everything is a
60
60
// straight lookup at link time.
61
- views : function ( state ) {
61
+ views : [ function ( state ) {
62
62
let views = { } ,
63
63
tplKeys = [ 'templateProvider' , 'templateUrl' , 'template' , 'notify' , 'async' ] ,
64
64
ctrlKeys = [ 'controller' , 'controllerProvider' , 'controllerAs' ] ;
@@ -73,52 +73,31 @@ export function StateBuilder(root, matcher, $urlMatcherFactoryProvider) {
73
73
if ( Object . keys ( config ) . length > 0 ) views [ name ] = config ;
74
74
} ) ;
75
75
return views ;
76
- } ,
76
+ } ] ,
77
77
78
78
// Keep a full path from the root down to this state as this is needed for state activation.
79
- path : function ( state ) {
79
+ path : [ function ( state ) {
80
80
return state . parent ? state . parent . path . concat ( state ) : /*root*/ [ state ] ;
81
- } ,
81
+ } ] ,
82
82
83
83
// Speed up $state.includes() as it's used a lot
84
- includes : function ( state ) {
84
+ includes : [ function ( state ) {
85
85
let includes = state . parent ? extend ( { } , state . parent . includes ) : { } ;
86
86
includes [ state . name ] = true ;
87
87
return includes ;
88
- }
88
+ } ]
89
89
} ;
90
90
91
91
extend ( this , {
92
- builder : function ( _name , _func ) {
93
- if ( isString ( _name ) && ! isDefined ( _func ) ) return builders [ _name ] ;
94
- if ( ! isFunction ( _func ) || ! isString ( _name ) ) return ;
95
-
96
- function remove ( name , func ) {
97
- if ( ! builders [ name ] . length ) {
98
- delete builders [ name ] ;
99
- return ;
100
- }
101
- builders [ name ] . splice ( builders [ name ] . indexOf ( func , 1 ) ) ;
102
-
103
- if ( builders [ name ] . length === 1 ) {
104
- builders [ name ] = builders [ name ] [ 0 ] ;
105
- }
106
- }
107
-
108
- function add ( name , func ) {
109
- if ( ! builders [ name ] ) {
110
- builders [ name ] = func ;
111
- return function ( ) { remove ( name , func ) ; } ;
112
- }
113
-
114
- if ( ! isArray ( builders [ name ] ) ) {
115
- builders [ name ] = [ builders [ name ] ] ;
116
- }
117
- builders [ name ] . push ( func ) ;
118
- return function ( ) { remove ( name , func ) ; } ;
119
- }
120
-
121
- return add ( _name , _func ) ;
92
+ builder : function ( name , fn ) {
93
+ let array : Function [ ] = builders [ name ] || [ ] ;
94
+ // Backwards compat: if only one builder exists, return it, else return whole arary.
95
+ if ( isString ( name ) && ! isDefined ( fn ) ) return array . length > 1 ? array : array [ 0 ] ;
96
+ if ( ! isString ( name ) || ! isFunction ( fn ) ) return ;
97
+
98
+ builders [ name ] = array ;
99
+ builders [ name ] . push ( fn ) ;
100
+ return ( ) => builders [ name ] . splice ( builders [ name ] . indexOf ( fn , 1 ) )
122
101
} ,
123
102
124
103
build : function ( state ) {
0 commit comments