4
4
*/ /** for typedoc */
5
5
6
6
import { StateDeclaration , StateOrName , TargetStateDef } from "./interface" ;
7
- import { ParamsOrArray } from "../params/interface" ;
8
7
import { TransitionOptions } from "../transition/interface" ;
9
8
import { StateObject } from "./stateObject" ;
10
9
import { isString } from "../common/predicates" ;
11
10
import { stringify } from '../common/strings' ;
11
+ import { extend } from '../common' ;
12
+ import { StateRegistry } from './stateRegistry' ;
13
+ import { RawParams } from '../params' ;
12
14
13
15
/**
14
16
* Encapsulate the target (destination) state/params/options of a [[Transition]].
@@ -40,29 +42,34 @@ import { stringify } from '../common/strings';
40
42
* or invalid (the state being targeted is not registered).
41
43
*/
42
44
export class TargetState {
43
- private _params : ParamsOrArray ;
45
+ private _definition : StateObject ;
46
+ private _params : RawParams ;
47
+ private _options : TransitionOptions ;
44
48
45
49
/**
46
50
* The TargetState constructor
47
51
*
48
52
* Note: Do not construct a `TargetState` manually.
49
53
* To create a `TargetState`, use the [[StateService.target]] factory method.
50
54
*
55
+ * @param _stateRegistry The StateRegistry to use to look up the _definition
51
56
* @param _identifier An identifier for a state.
52
57
* Either a fully-qualified state name, or the object used to define the state.
53
- * @param _definition The internal state representation, if exists.
54
58
* @param _params Parameters for the target state
55
59
* @param _options Transition options.
56
60
*
57
61
* @internalapi
58
62
*/
59
63
constructor (
64
+ private _stateRegistry : StateRegistry ,
60
65
private _identifier : StateOrName ,
61
- private _definition ?: StateObject ,
62
- _params ?: ParamsOrArray ,
63
- private _options : TransitionOptions = { }
66
+ _params ?: RawParams ,
67
+ _options ?: TransitionOptions ,
64
68
) {
65
- this . _params = _params || { } ;
69
+ this . _identifier = _identifier ;
70
+ this . _params = extend ( { } , _params || { } ) ;
71
+ this . _options = extend ( { } , _options || { } ) ;
72
+ this . _definition = _stateRegistry . matcher . find ( _identifier , this . _options . relative ) ;
66
73
}
67
74
68
75
/** The name of the state this object targets */
@@ -76,7 +83,7 @@ export class TargetState {
76
83
}
77
84
78
85
/** The target parameter values */
79
- params ( ) : ParamsOrArray {
86
+ params ( ) : RawParams {
80
87
return this . _params ;
81
88
}
82
89
@@ -126,16 +133,37 @@ export class TargetState {
126
133
static isDef = ( obj ) : obj is TargetStateDef =>
127
134
obj && obj . state && ( isString ( obj . state ) || isString ( obj . state . name ) ) ;
128
135
129
- // /** Returns a new TargetState based on this one, but using the specified options */
130
- // withOptions(_options: TransitionOptions): TargetState {
131
- // return extend(this._clone(), { _options });
132
- // }
133
- //
134
- // /** Returns a new TargetState based on this one, but using the specified params */
135
- // withParams(_params: ParamsOrArray): TargetState {
136
- // return extend(this._clone(), { _params });
137
- // }
138
-
139
- // private _clone = () =>
140
- // new TargetState(this._identifier, this._definition, this._params, this._options);
136
+ /**
137
+ * Returns a copy of this TargetState which targets a different state.
138
+ * The new TargetState has the same parameter values and transition options.
139
+ *
140
+ * @param state The new state that should be targeted
141
+ */
142
+ withState ( state : StateOrName ) : TargetState {
143
+ return new TargetState ( this . _stateRegistry , state , this . _params , this . _options ) ;
144
+ }
145
+
146
+ /**
147
+ * Returns a copy of this TargetState, using the specified parameter values.
148
+ *
149
+ * @param params the new parameter values to use
150
+ * @param replace When false (default) the new parameter values will be merged with the current values.
151
+ * When true the parameter values will be used instead of the current values.
152
+ */
153
+ withParams ( params : RawParams , replace = false ) : TargetState {
154
+ const newParams : RawParams = replace ? params : extend ( { } , this . _params , params ) ;
155
+ return new TargetState ( this . _stateRegistry , this . _identifier , newParams , this . _options ) ;
156
+ }
157
+
158
+ /**
159
+ * Returns a copy of this TargetState, using the specified Transition Options.
160
+ *
161
+ * @param options the new options to use
162
+ * @param replace When false (default) the new options will be merged with the current options.
163
+ * When true the options will be used instead of the current options.
164
+ */
165
+ withOptions ( options : TransitionOptions , replace = false ) : TargetState {
166
+ const newOpts = replace ? options : extend ( { } , this . _options , options ) ;
167
+ return new TargetState ( this . _stateRegistry , this . _identifier , this . _params , newOpts ) ;
168
+ }
141
169
}
0 commit comments