UNPKG

20.1 kBTypeScriptView Raw
1import { StateDeclaration, StateOrName } from '../state/interface';
2import { TransitionOptions, TreeChanges, IHookRegistry, RegisteredHooks, HookRegOptions, HookMatchCriteria, TransitionStateHookFn, TransitionHookFn } from './interface';
3import { RegisteredHook } from './hookRegistry';
4import { PathNode } from '../path/pathNode';
5import { StateObject } from '../state/stateObject';
6import { TargetState } from '../state/targetState';
7import { Resolvable } from '../resolve/resolvable';
8import { ViewConfig } from '../view/interface';
9import { UIRouter } from '../router';
10import { UIInjector } from '../interface';
11import { ResolvableLiteral } from '../resolve/interface';
12import { Rejection } from './rejectFactory';
13/**
14 * Represents a transition between two states.
15 *
16 * When navigating to a state, we are transitioning **from** the current state **to** the new state.
17 *
18 * This object contains all contextual information about the to/from states, parameters, resolves.
19 * It has information about all states being entered and exited as a result of the transition.
20 */
21export declare class Transition implements IHookRegistry {
22 /** @internal */
23 static diToken: typeof Transition;
24 /**
25 * A unique identifier for the transition.
26 *
27 * This is an auto incrementing integer, starting from `0`.
28 */
29 $id: number;
30 /**
31 * A reference to the [[UIRouter]] instance
32 *
33 * This reference can be used to access the router services, such as the [[StateService]]
34 */
35 router: UIRouter;
36 /** @internal */
37 private _deferred;
38 /**
39 * This promise is resolved or rejected based on the outcome of the Transition.
40 *
41 * When the transition is successful, the promise is resolved
42 * When the transition is unsuccessful, the promise is rejected with the [[Rejection]] or javascript error
43 */
44 promise: Promise<any>;
45 /**
46 * A boolean which indicates if the transition was successful
47 *
48 * After a successful transition, this value is set to true.
49 * After an unsuccessful transition, this value is set to false.
50 *
51 * The value will be undefined if the transition is not complete
52 */
53 success: boolean;
54 /** @internal */
55 _aborted: boolean;
56 /** @internal */
57 private _error;
58 /** @internal Holds the hook registration functions such as those passed to Transition.onStart() */
59 _registeredHooks: RegisteredHooks;
60 /** @internal */
61 private _options;
62 /** @internal */
63 private _treeChanges;
64 /** @internal */
65 private _targetState;
66 /** @internal */
67 private _hookBuilder;
68 /** @internal */
69 onBefore(criteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): Function;
70 /** @inheritdoc */
71 onStart(criteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): Function;
72 /** @inheritdoc */
73 onExit(criteria: HookMatchCriteria, callback: TransitionStateHookFn, options?: HookRegOptions): Function;
74 /** @inheritdoc */
75 onRetain(criteria: HookMatchCriteria, callback: TransitionStateHookFn, options?: HookRegOptions): Function;
76 /** @inheritdoc */
77 onEnter(criteria: HookMatchCriteria, callback: TransitionStateHookFn, options?: HookRegOptions): Function;
78 /** @inheritdoc */
79 onFinish(criteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): Function;
80 /** @inheritdoc */
81 onSuccess(criteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): Function;
82 /** @inheritdoc */
83 onError(criteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): Function;
84 /** @internal
85 * Creates the transition-level hook registration functions
86 * (which can then be used to register hooks)
87 */
88 private createTransitionHookRegFns;
89 /** @internal */
90 getHooks(hookName: string): RegisteredHook[];
91 /**
92 * Creates a new Transition object.
93 *
94 * If the target state is not valid, an error is thrown.
95 *
96 * @internal
97 *
98 * @param fromPath The path of [[PathNode]]s from which the transition is leaving. The last node in the `fromPath`
99 * encapsulates the "from state".
100 * @param targetState The target state and parameters being transitioned to (also, the transition options)
101 * @param router The [[UIRouter]] instance
102 * @internal
103 */
104 constructor(fromPath: PathNode[], targetState: TargetState, router: UIRouter);
105 private applyViewConfigs;
106 /**
107 * @internal
108 * @returns the internal from [State] object
109 */
110 $from(): StateObject;
111 /**
112 * @internal
113 * @returns the internal to [State] object
114 */
115 $to(): StateObject;
116 /**
117 * Returns the "from state"
118 *
119 * Returns the state that the transition is coming *from*.
120 *
121 * @returns The state declaration object for the Transition's ("from state").
122 */
123 from(): StateDeclaration;
124 /**
125 * Returns the "to state"
126 *
127 * Returns the state that the transition is going *to*.
128 *
129 * @returns The state declaration object for the Transition's target state ("to state").
130 */
131 to(): StateDeclaration;
132 /**
133 * Gets the Target State
134 *
135 * A transition's [[TargetState]] encapsulates the [[to]] state, the [[params]], and the [[options]] as a single object.
136 *
137 * @returns the [[TargetState]] of this Transition
138 */
139 targetState(): TargetState;
140 /**
141 * Determines whether two transitions are equivalent.
142 * @deprecated
143 */
144 is(compare: Transition | {
145 to?: any;
146 from?: any;
147 }): boolean;
148 /**
149 * Gets transition parameter values
150 *
151 * Returns the parameter values for a transition as key/value pairs.
152 * This object is immutable.
153 *
154 * By default, returns the new parameter values (for the "to state").
155 *
156 * #### Example:
157 * ```js
158 * var toParams = transition.params();
159 * ```
160 *
161 * To return the previous parameter values, supply `'from'` as the `pathname` argument.
162 *
163 * #### Example:
164 * ```js
165 * var fromParams = transition.params('from');
166 * ```
167 *
168 * @param pathname the name of the treeChanges path to get parameter values for:
169 * (`'to'`, `'from'`, `'entering'`, `'exiting'`, `'retained'`)
170 *
171 * @returns transition parameter values for the desired path.
172 */
173 params(pathname?: string): {
174 [paramName: string]: any;
175 };
176 params<T>(pathname?: string): T;
177 /**
178 * Gets the new values of any parameters that changed during this transition.
179 *
180 * Returns any parameter values that have changed during a transition, as key/value pairs.
181 *
182 * - Any parameter values that have changed will be present on the returned object reflecting the new value.
183 * - Any parameters that *not* have changed will not be present on the returned object.
184 * - Any new parameters that weren't present in the "from" state, but are now present in the "to" state will be present on the returned object.
185 * - Any previous parameters that are no longer present (because the "to" state doesn't have them) will be included with a value of `undefined`.
186 *
187 * The returned object is immutable.
188 *
189 * #### Examples:
190 *
191 * Given:
192 * ```js
193 * var stateA = { name: 'stateA', url: '/stateA/:param1/param2' }
194 * var stateB = { name: 'stateB', url: '/stateB/:param3' }
195 * var stateC = { name: 'stateB.nest', url: '/nest/:param4' }
196 * ```
197 *
198 * #### Example 1
199 *
200 * From `/stateA/abc/def` to `/stateA/abc/xyz`
201 *
202 * ```js
203 * var changed = transition.paramsChanged()
204 * // changed is { param2: 'xyz' }
205 * ```
206 *
207 * The value of `param2` changed to `xyz`.
208 * The value of `param1` stayed the same so its value is not present.
209 *
210 * #### Example 2
211 *
212 * From `/stateA/abc/def` to `/stateB/123`
213 *
214 * ```js
215 * var changed = transition.paramsChanged()
216 * // changed is { param1: undefined, param2: undefined, param3: '123' }
217 * ```
218 *
219 * The value `param3` is present because it is a new param.
220 * Both `param1` and `param2` are no longer present so their value is undefined.
221 *
222 * #### Example 3
223 *
224 * From `/stateB/123` to `/stateB/123/nest/456`
225 *
226 * ```js
227 * var changed = transition.paramsChanged()
228 * // changed is { param4: '456' }
229 * ```
230 *
231 * The value `param4` is present because it is a new param.
232 * The value of `param3` did not change, so its value is not present.
233 *
234 * @returns an immutable object with changed parameter keys/values.
235 */
236 paramsChanged(): {
237 [paramName: string]: any;
238 };
239 paramsChanged<T>(): T;
240 /**
241 * Creates a [[UIInjector]] Dependency Injector
242 *
243 * Returns a Dependency Injector for the Transition's target state (to state).
244 * The injector provides resolve values which the target state has access to.
245 *
246 * The `UIInjector` can also provide values from the native root/global injector (ng1/ng2).
247 *
248 * #### Example:
249 * ```js
250 * .onEnter({ entering: 'myState' }, trans => {
251 * var myResolveValue = trans.injector().get('myResolve');
252 * // Inject a global service from the global/native injector (if it exists)
253 * var MyService = trans.injector().get('MyService');
254 * })
255 * ```
256 *
257 * In some cases (such as `onBefore`), you may need access to some resolve data but it has not yet been fetched.
258 * You can use [[UIInjector.getAsync]] to get a promise for the data.
259 * #### Example:
260 * ```js
261 * .onBefore({}, trans => {
262 * return trans.injector().getAsync('myResolve').then(myResolveValue =>
263 * return myResolveValue !== 'ABORT';
264 * });
265 * });
266 * ```
267 *
268 * If a `state` is provided, the injector that is returned will be limited to resolve values that the provided state has access to.
269 * This can be useful if both a parent state `foo` and a child state `foo.bar` have both defined a resolve such as `data`.
270 * #### Example:
271 * ```js
272 * .onEnter({ to: 'foo.bar' }, trans => {
273 * // returns result of `foo` state's `myResolve` resolve
274 * // even though `foo.bar` also has a `myResolve` resolve
275 * var fooData = trans.injector('foo').get('myResolve');
276 * });
277 * ```
278 *
279 * If you need resolve data from the exiting states, pass `'from'` as `pathName`.
280 * The resolve data from the `from` path will be returned.
281 * #### Example:
282 * ```js
283 * .onExit({ exiting: 'foo.bar' }, trans => {
284 * // Gets the resolve value of `myResolve` from the state being exited
285 * var fooData = trans.injector(null, 'from').get('myResolve');
286 * });
287 * ```
288 *
289 *
290 * @param state Limits the resolves provided to only the resolves the provided state has access to.
291 * @param pathName Default: `'to'`: Chooses the path for which to create the injector. Use this to access resolves for `exiting` states.
292 *
293 * @returns a [[UIInjector]]
294 */
295 injector(state?: StateOrName, pathName?: string): UIInjector;
296 /**
297 * Gets all available resolve tokens (keys)
298 *
299 * This method can be used in conjunction with [[injector]] to inspect the resolve values
300 * available to the Transition.
301 *
302 * This returns all the tokens defined on [[StateDeclaration.resolve]] blocks, for the states
303 * in the Transition's [[TreeChanges.to]] path.
304 *
305 * #### Example:
306 * This example logs all resolve values
307 * ```js
308 * let tokens = trans.getResolveTokens();
309 * tokens.forEach(token => console.log(token + " = " + trans.injector().get(token)));
310 * ```
311 *
312 * #### Example:
313 * This example creates promises for each resolve value.
314 * This triggers fetches of resolves (if any have not yet been fetched).
315 * When all promises have all settled, it logs the resolve values.
316 * ```js
317 * let tokens = trans.getResolveTokens();
318 * let promise = tokens.map(token => trans.injector().getAsync(token));
319 * Promise.all(promises).then(values => console.log("Resolved values: " + values));
320 * ```
321 *
322 * Note: Angular 1 users whould use `$q.all()`
323 *
324 * @param pathname resolve context's path name (e.g., `to` or `from`)
325 *
326 * @returns an array of resolve tokens (keys)
327 */
328 getResolveTokens(pathname?: string): any[];
329 /**
330 * Dynamically adds a new [[Resolvable]] (i.e., [[StateDeclaration.resolve]]) to this transition.
331 *
332 * Allows a transition hook to dynamically add a Resolvable to this Transition.
333 *
334 * Use the [[Transition.injector]] to retrieve the resolved data in subsequent hooks ([[UIInjector.get]]).
335 *
336 * If a `state` argument is provided, the Resolvable is processed when that state is being entered.
337 * If no `state` is provided then the root state is used.
338 * If the given `state` has already been entered, the Resolvable is processed when any child state is entered.
339 * If no child states will be entered, the Resolvable is processed during the `onFinish` phase of the Transition.
340 *
341 * The `state` argument also scopes the resolved data.
342 * The resolved data is available from the injector for that `state` and any children states.
343 *
344 * #### Example:
345 * ```js
346 * transitionService.onBefore({}, transition => {
347 * transition.addResolvable({
348 * token: 'myResolve',
349 * deps: ['MyService'],
350 * resolveFn: myService => myService.getData()
351 * });
352 * });
353 * ```
354 *
355 * @param resolvable a [[ResolvableLiteral]] object (or a [[Resolvable]])
356 * @param state the state in the "to path" which should receive the new resolve (otherwise, the root state)
357 */
358 addResolvable(resolvable: Resolvable | ResolvableLiteral, state?: StateOrName): void;
359 /**
360 * Gets the transition from which this transition was redirected.
361 *
362 * If the current transition is a redirect, this method returns the transition that was redirected.
363 *
364 * #### Example:
365 * ```js
366 * let transitionA = $state.go('A').transition
367 * transitionA.onStart({}, () => $state.target('B'));
368 * $transitions.onSuccess({ to: 'B' }, (trans) => {
369 * trans.to().name === 'B'; // true
370 * trans.redirectedFrom() === transitionA; // true
371 * });
372 * ```
373 *
374 * @returns The previous Transition, or null if this Transition is not the result of a redirection
375 */
376 redirectedFrom(): Transition;
377 /**
378 * Gets the original transition in a redirect chain
379 *
380 * A transition might belong to a long chain of multiple redirects.
381 * This method walks the [[redirectedFrom]] chain back to the original (first) transition in the chain.
382 *
383 * #### Example:
384 * ```js
385 * // states
386 * registry.register({ name: 'A', redirectTo: 'B' });
387 * registry.register({ name: 'B', redirectTo: 'C' });
388 * registry.register({ name: 'C', redirectTo: 'D' });
389 * registry.register({ name: 'D' });
390 *
391 * let transitionA = $state.go('A').transition
392 *
393 * $transitions.onSuccess({ to: 'D' }, (trans) => {
394 * trans.to().name === 'D'; // true
395 * trans.redirectedFrom().to().name === 'C'; // true
396 * trans.originalTransition() === transitionA; // true
397 * trans.originalTransition().to().name === 'A'; // true
398 * });
399 * ```
400 *
401 * @returns The original Transition that started a redirect chain
402 */
403 originalTransition(): Transition;
404 /**
405 * Get the transition options
406 *
407 * @returns the options for this Transition.
408 */
409 options(): TransitionOptions;
410 /**
411 * Gets the states being entered.
412 *
413 * @returns an array of states that will be entered during this transition.
414 */
415 entering(): StateDeclaration[];
416 /**
417 * Gets the states being exited.
418 *
419 * @returns an array of states that will be exited during this transition.
420 */
421 exiting(): StateDeclaration[];
422 /**
423 * Gets the states being retained.
424 *
425 * @returns an array of states that are already entered from a previous Transition, that will not be
426 * exited during this Transition
427 */
428 retained(): StateDeclaration[];
429 /**
430 * Get the [[ViewConfig]]s associated with this Transition
431 *
432 * Each state can define one or more views (template/controller), which are encapsulated as `ViewConfig` objects.
433 * This method fetches the `ViewConfigs` for a given path in the Transition (e.g., "to" or "entering").
434 *
435 * @param pathname the name of the path to fetch views for:
436 * (`'to'`, `'from'`, `'entering'`, `'exiting'`, `'retained'`)
437 * @param state If provided, only returns the `ViewConfig`s for a single state in the path
438 *
439 * @returns a list of ViewConfig objects for the given path.
440 */
441 views(pathname?: string, state?: StateObject): ViewConfig[];
442 /**
443 * Return the transition's tree changes
444 *
445 * A transition goes from one state/parameters to another state/parameters.
446 * During a transition, states are entered and/or exited.
447 *
448 * This function returns various branches (paths) which represent the changes to the
449 * active state tree that are caused by the transition.
450 *
451 * @param pathname The name of the tree changes path to get:
452 * (`'to'`, `'from'`, `'entering'`, `'exiting'`, `'retained'`)
453 */
454 treeChanges(pathname: string): PathNode[];
455 treeChanges(): TreeChanges;
456 /**
457 * Creates a new transition that is a redirection of the current one.
458 *
459 * This transition can be returned from a [[TransitionService]] hook to
460 * redirect a transition to a new state and/or set of parameters.
461 *
462 * @internal
463 *
464 * @returns Returns a new [[Transition]] instance.
465 */
466 redirect(targetState: TargetState): Transition;
467 /** @internal If a transition doesn't exit/enter any states, returns any [[Param]] whose value changed */
468 private _changedParams;
469 /**
470 * Returns true if the transition is dynamic.
471 *
472 * A transition is dynamic if no states are entered nor exited, but at least one dynamic parameter has changed.
473 *
474 * @returns true if the Transition is dynamic
475 */
476 dynamic(): boolean;
477 /**
478 * Returns true if the transition is ignored.
479 *
480 * A transition is ignored if no states are entered nor exited, and no parameter values have changed.
481 *
482 * @returns true if the Transition is ignored.
483 */
484 ignored(): boolean;
485 /** @internal */
486 _ignoredReason(): 'SameAsCurrent' | 'SameAsPending' | undefined;
487 /**
488 * Runs the transition
489 *
490 * This method is generally called from the [[StateService.transitionTo]]
491 *
492 * @internal
493 *
494 * @returns a promise for a successful transition.
495 */
496 run(): Promise<any>;
497 /** Checks if this transition is currently active/running. */
498 isActive: () => boolean;
499 /**
500 * Checks if the Transition is valid
501 *
502 * @returns true if the Transition is valid
503 */
504 valid(): boolean;
505 /**
506 * Aborts this transition
507 *
508 * Imperative API to abort a Transition.
509 * This only applies to Transitions that are not yet complete.
510 */
511 abort(): void;
512 /**
513 * The Transition error reason.
514 *
515 * If the transition is invalid (and could not be run), returns the reason the transition is invalid.
516 * If the transition was valid and ran, but was not successful, returns the reason the transition failed.
517 *
518 * @returns a transition rejection explaining why the transition is invalid, or the reason the transition failed.
519 */
520 error(): Rejection;
521 /**
522 * A string representation of the Transition
523 *
524 * @returns A string representation of the Transition
525 */
526 toString(): string;
527}