1
1
/** @module params */ /** for typedoc */
2
2
import { IServiceProviderFactory } from "angular" ;
3
- import { forEach , ancestors , extend , copy } from "../common/common" ;
3
+ import { forEach , ancestors , extend , copy , pick , omit } from "../common/common" ;
4
4
5
- export function StateParams ( ) { }
5
+ export class StateParams {
6
+ constructor ( params : Object = { } ) {
7
+ extend ( this , params ) ;
8
+ }
9
+
10
+ $digest ( ) { }
11
+ $inherit ( newParams , $current , $to ) { }
12
+ $set ( params , url ) { }
13
+ $sync ( ) { }
14
+ $off ( ) { }
15
+ $raw ( ) { }
16
+ $localize ( state , params ) { }
17
+ $observe ( key : string , fn : Function ) { }
18
+ }
6
19
7
20
$StateParamsProvider . $inject = [ ] ;
8
21
function $StateParamsProvider ( ) {
@@ -11,29 +24,23 @@ function $StateParamsProvider() {
11
24
let observers = { } , current = { } ;
12
25
13
26
function unhook ( key , func ) {
14
- return function ( ) {
15
- forEach ( key . split ( " " ) , function ( k ) {
16
- observers [ k ] . splice ( observers [ k ] . indexOf ( func ) , 1 ) ;
17
- } ) ;
27
+ return ( ) => {
28
+ forEach ( key . split ( " " ) , k => observers [ k ] . splice ( observers [ k ] . indexOf ( func ) , 1 ) ) ;
18
29
} ;
19
30
}
20
31
21
32
function observeChange ( key , val ?: any ) {
22
33
if ( ! observers [ key ] || ! observers [ key ] . length ) return ;
23
-
24
- forEach ( observers [ key ] , function ( func ) {
25
- func ( val ) ;
26
- } ) ;
34
+ forEach ( observers [ key ] , func => func ( val ) ) ;
27
35
}
28
36
29
37
30
38
StateParams . prototype . $digest = function ( ) {
31
- function updateValue ( val , key ) {
39
+ forEach ( this , ( val , key ) => {
32
40
if ( val === current [ key ] || ! this . hasOwnProperty ( key ) ) return ;
33
41
current [ key ] = val ;
34
42
observeChange ( key , val ) ;
35
- }
36
- forEach ( this , updateValue , this ) ;
43
+ } ) ;
37
44
} ;
38
45
39
46
/**
@@ -71,14 +78,13 @@ function $StateParamsProvider() {
71
78
}
72
79
if ( abort ) return false ;
73
80
74
- function updateValue ( val , key ) {
81
+ forEach ( params , ( val , key ) => {
75
82
if ( val !== this [ key ] ) {
76
83
this [ key ] = val ;
77
84
observeChange ( key ) ;
78
85
hasChanged = true ;
79
86
}
80
- }
81
- forEach ( params , updateValue , this ) ;
87
+ } ) ;
82
88
83
89
this . $sync ( ) ;
84
90
return hasChanged ;
@@ -95,29 +101,19 @@ function $StateParamsProvider() {
95
101
} ;
96
102
97
103
StateParams . prototype . $raw = function ( ) {
98
- let raw = { } ;
99
- for ( let key in this ) {
100
- if ( ! StateParams . prototype . hasOwnProperty ( key ) )
101
- raw [ key ] = this [ key ] ;
102
- }
103
- return raw ;
104
+ return omit (
105
+ this ,
106
+ Object . keys ( this ) . filter ( StateParams . prototype . hasOwnProperty . bind ( StateParams . prototype ) )
107
+ ) ;
104
108
} ;
105
109
106
110
StateParams . prototype . $localize = function ( state , params ) {
107
- let localized = new StateParams ( ) ;
108
- params = params || this ;
109
-
110
- forEach ( state . params , function ( val , key ) {
111
- localized [ key ] = params [ key ] ;
112
- } ) ;
113
- return localized ;
111
+ return new StateParams ( pick ( params || this , Object . keys ( state . params ) ) ) ;
114
112
} ;
115
113
116
- StateParams . prototype . $observe = function ( key , func ) {
117
- forEach ( key . split ( " " ) , function ( k ) {
118
- ( observers [ k ] || ( observers [ k ] = [ ] ) ) . push ( func ) ;
119
- } ) ;
120
- return unhook ( key , func ) ;
114
+ StateParams . prototype . $observe = function ( key : string , fn : Function ) {
115
+ forEach ( key . split ( " " ) , k => ( observers [ k ] || ( observers [ k ] = [ ] ) ) . push ( fn ) ) ;
116
+ return unhook ( key , fn ) ;
121
117
} ;
122
118
123
119
return new StateParams ( ) ;
0 commit comments