1 | import { StateDeclaration } from '../state/interface';
|
2 | import { PredicateBinary } from '../common/common';
|
3 | import { Transition } from './transition';
|
4 | import { StateObject } from '../state/stateObject';
|
5 | import { PathNode } from '../path/pathNode';
|
6 | import { TargetState } from '../state/targetState';
|
7 | import { RegisteredHook } from './hookRegistry';
|
8 | /**
|
9 | * The TransitionOptions object can be used to change the behavior of a transition.
|
10 | *
|
11 | * It is passed as the third argument to [[StateService.go]], [[StateService.transitionTo]].
|
12 | * It can also be used with a `uiSref`.
|
13 | */
|
14 | export interface TransitionOptions {
|
15 | /**
|
16 | * This option changes how the Transition interacts with the browser's location bar (URL).
|
17 | *
|
18 | * - If `true`, it will update the url in the location bar.
|
19 | * - If `false`, it will not update the url in the location bar.
|
20 | * - If it is the string `"replace"`, it will update the url and also replace the last history record.
|
21 | *
|
22 | * @default `true`
|
23 | */
|
24 | location?: boolean | 'replace';
|
25 | /**
|
26 | * When transitioning to relative path (e.g '`^`'), this option defines which state to be relative from.
|
27 | * @default `$state.current`
|
28 | */
|
29 | relative?: string | StateDeclaration | StateObject;
|
30 | /**
|
31 | * This option sets whether or not the transition's parameter values should be inherited from
|
32 | * the current parameter values.
|
33 | *
|
34 | * - If `true`, it will inherit parameter values from the current parameter values.
|
35 | * - If `false`, only the parameters which are provided to `transitionTo` will be used.
|
36 | *
|
37 | * @default `true`
|
38 | */
|
39 | inherit?: boolean;
|
40 | /**
|
41 | * @deprecated
|
42 | */
|
43 | notify?: boolean;
|
44 | /**
|
45 | * This option may be used to force states which are currently active to reload.
|
46 | *
|
47 | * During a normal transition, a state is "retained" if:
|
48 | * - It was previously active
|
49 | * - The state's parameter values have not changed
|
50 | * - All the parent states' parameter values have not changed
|
51 | *
|
52 | * Forcing a reload of a state will cause it to be exited and entered, which will:
|
53 | * - Refetch that state's resolve data
|
54 | * - Exit the state (onExit hook)
|
55 | * - Re-enter the state (onEnter hook)
|
56 | * - Re-render the views (controllers and templates)
|
57 | *
|
58 | * - When `true`, the destination state (and all parent states) will be reloaded.
|
59 | * - When it is a string and is the name of a state, or when it is a State object,
|
60 | * that state and any children states will be reloaded.
|
61 | *
|
62 | * @default `false`
|
63 | */
|
64 | reload?: boolean | string | StateDeclaration | StateObject;
|
65 | /**
|
66 | * You can define your own Transition Options inside this property and use them, e.g., from a Transition Hook
|
67 | */
|
68 | custom?: any;
|
69 | /**
|
70 | * This option may be used to cancel the active transition (if one is active) in favour of the this one.
|
71 | * This is the default behaviour or ui-router.
|
72 | *
|
73 | *
|
74 | * - When `true`, the active transition will be canceled and new transition will begin.
|
75 | * - when `false`, the transition will be canceled if a transition is already running. This can be useful in cases where
|
76 | * you only want to navigate to a different state if you are not already navigating somewhere.
|
77 | *
|
78 | * @default `true`
|
79 | */
|
80 | supercede?: boolean;
|
81 | /** @internal */
|
82 | reloadState?: StateObject;
|
83 | /** @internal
|
84 | * If this transition is a redirect, this property should be the original Transition (which was redirected to this one)
|
85 | */
|
86 | redirectedFrom?: Transition;
|
87 | /** @internal */
|
88 | current?: () => Transition;
|
89 | /** @internal */
|
90 | source?: 'sref' | 'url' | 'redirect' | 'otherwise' | 'unknown';
|
91 | }
|
92 | export interface TransitionHookOptions {
|
93 | current?: () => Transition;
|
94 | transition?: Transition;
|
95 | hookType?: string;
|
96 | target?: any;
|
97 | traceData?: any;
|
98 | bind?: any;
|
99 | stateHook?: boolean;
|
100 | }
|
101 | /**
|
102 | * TreeChanges encapsulates the various Paths that are involved in a Transition.
|
103 | *
|
104 | * Get a TreeChanges object using [[Transition.treeChanges]]
|
105 | *
|
106 | * A UI-Router Transition is from one Path in a State Tree to another Path. For a given Transition,
|
107 | * this object stores the "to" and "from" paths, as well as subsets of those: the "retained",
|
108 | * "exiting" and "entering" paths.
|
109 | *
|
110 | * Each path in TreeChanges is an array of [[PathNode]] objects. Each PathNode in the array corresponds to a portion
|
111 | * of a nested state.
|
112 | *
|
113 | * For example, if you had a nested state named `foo.bar.baz`, it would have three
|
114 | * portions, `foo, bar, baz`. If you transitioned **to** `foo.bar.baz` and inspected the [[TreeChanges.to]]
|
115 | * Path, you would find a node in the array for each portion: `foo`, `bar`, and `baz`.
|
116 | *
|
117 | * ---
|
118 | *
|
119 | * @todo show visual state tree
|
120 | */
|
121 | export interface TreeChanges {
|
122 | /** @nodoc */
|
123 | [key: string]: PathNode[] | undefined;
|
124 | /** The path of nodes in the state tree that the transition is coming *from* */
|
125 | from: PathNode[];
|
126 | /** The path of nodes in the state tree that the transition is going *to* */
|
127 | to: PathNode[];
|
128 | /**
|
129 | * The path of active nodes that the transition is retaining.
|
130 | *
|
131 | * These nodes are neither exited, nor entered.
|
132 | * Before and after the transition is successful, these nodes are active.
|
133 | */
|
134 | retained: PathNode[];
|
135 | /**
|
136 | * The path of active nodes that the transition is retaining with updated "to params" applied.
|
137 | *
|
138 | * These nodes are neither exited, nor entered.
|
139 | * Before and after the transition is successful, these nodes are active.
|
140 | *
|
141 | * This is a shallow copy of [[retained]], but with new (dynamic) parameter values from [[to]] applied.
|
142 | */
|
143 | retainedWithToParams: PathNode[];
|
144 | /**
|
145 | * The path of previously active nodes that the transition is exiting.
|
146 | *
|
147 | * After the Transition is successful, these nodes are no longer active.
|
148 | *
|
149 | * Note that a state that is being reloaded (due to parameter values changing, or `reload: true`) may be in both the
|
150 | * `exiting` and `entering` paths.
|
151 | */
|
152 | exiting: PathNode[];
|
153 | /**
|
154 | * The path of nodes that the transition is entering.
|
155 | *
|
156 | * After the Transition is successful, these nodes will be active.
|
157 | * Because they are entering, they have their resolves fetched, `onEnter` hooks run, and their views
|
158 | * (component(s) or controller(s)+template(s)) refreshed.
|
159 | *
|
160 | * Note that a state that is reloaded (due to parameter values changing, or `reload: true`) may be in both the
|
161 | * `exiting` and `entering` paths.
|
162 | */
|
163 | entering: PathNode[];
|
164 | }
|
165 | export declare type IHookRegistration = (matchCriteria: HookMatchCriteria, callback: HookFn, options?: HookRegOptions) => Function;
|
166 | /**
|
167 | * The signature for Transition Hooks.
|
168 | *
|
169 | * Transition hooks are callback functions that hook into the lifecycle of transitions.
|
170 | * As a transition runs, it reaches certain lifecycle events.
|
171 | * As each event occurs, the hooks which are registered for the event are called (in priority order).
|
172 | *
|
173 | * A transition hook may alter a Transition by returning a [[HookResult]].
|
174 | *
|
175 | * #### See:
|
176 | *
|
177 | * - [[IHookRegistry.onBefore]]
|
178 | * - [[IHookRegistry.onStart]]
|
179 | * - [[IHookRegistry.onFinish]]
|
180 | * - [[IHookRegistry.onSuccess]]
|
181 | * - [[IHookRegistry.onError]]
|
182 | *
|
183 | * @param transition the current [[Transition]]
|
184 | * @param injector (for ng1 or ng2 only) the injector service
|
185 | *
|
186 | * @returns a [[HookResult]] which may alter the transition
|
187 | *
|
188 | */
|
189 | export interface TransitionHookFn {
|
190 | (transition: Transition): HookResult;
|
191 | }
|
192 | /**
|
193 | * The signature for Transition State Hooks.
|
194 | *
|
195 | * A function which hooks into a lifecycle event for a specific state.
|
196 | *
|
197 | * Transition State Hooks are callback functions that hook into the lifecycle events of specific states during a transition.
|
198 | * As a transition runs, it may exit some states, retain (keep) states, and enter states.
|
199 | * As each lifecycle event occurs, the hooks which are registered for the event and that state are called (in priority order).
|
200 | *
|
201 | * #### See:
|
202 | *
|
203 | * - [[IHookRegistry.onExit]]
|
204 | * - [[IHookRegistry.onRetain]]
|
205 | * - [[IHookRegistry.onEnter]]
|
206 | *
|
207 | * @param transition the current [[Transition]]
|
208 | * @param state the [[StateObject]] that the hook is bound to
|
209 | * @param injector (for ng1 or ng2 only) the injector service
|
210 | *
|
211 | * @returns a [[HookResult]] which may alter the transition
|
212 | */
|
213 | export interface TransitionStateHookFn {
|
214 | (transition: Transition, state: StateDeclaration): HookResult;
|
215 | }
|
216 | /**
|
217 | * The signature for Transition onCreate Hooks.
|
218 | *
|
219 | * Transition onCreate Hooks are callbacks that allow customization or preprocessing of
|
220 | * a Transition before it is returned from [[TransitionService.create]]
|
221 | *
|
222 | * @param transition the [[Transition]] that was just created
|
223 | * @return a [[Transition]] which will then be returned from [[TransitionService.create]]
|
224 | */
|
225 | export interface TransitionCreateHookFn {
|
226 | (transition: Transition): void;
|
227 | }
|
228 | export declare type HookFn = TransitionHookFn | TransitionStateHookFn | TransitionCreateHookFn;
|
229 | /**
|
230 | * The return value of a [[TransitionHookFn]] or [[TransitionStateHookFn]]
|
231 | *
|
232 | * When returned from a [[TransitionHookFn]] or [[TransitionStateHookFn]], these values alter the running [[Transition]]:
|
233 | *
|
234 | * - `false`: the transition will be cancelled.
|
235 | * - [[TargetState]]: the transition will be redirected to the new target state (see: [[StateService.target]])
|
236 | * - `Promise`: the transition will wait for the promise to resolve or reject
|
237 | * - If the promise is rejected (or resolves to `false`), the transition will be cancelled
|
238 | * - If the promise resolves to a [[TargetState]], the transition will be redirected
|
239 | * - If the promise resolves to anything else, the transition will resume
|
240 | * - Anything else: the transition will resume
|
241 | */
|
242 | export declare type HookResult = boolean | TargetState | void | Promise<boolean | TargetState | void>;
|
243 | /**
|
244 | * These options may be provided when registering a Transition Hook (such as `onStart`)
|
245 | */
|
246 | export interface HookRegOptions {
|
247 | /**
|
248 | * Sets the priority of the registered hook
|
249 | *
|
250 | * Hooks of the same type (onBefore, onStart, etc) are invoked in priority order. A hook with a higher priority
|
251 | * is invoked before a hook with a lower priority.
|
252 | *
|
253 | * The default hook priority is 0
|
254 | */
|
255 | priority?: number;
|
256 | /**
|
257 | * Specifies what `this` is bound to during hook invocation.
|
258 | */
|
259 | bind?: any;
|
260 | /**
|
261 | * Limits the number of times that the hook will be invoked.
|
262 | * Once the hook has been invoked this many times, it is automatically deregistered.
|
263 | */
|
264 | invokeLimit?: number;
|
265 | }
|
266 | /**
|
267 | * This interface specifies the api for registering Transition Hooks. Both the
|
268 | * [[TransitionService]] and also the [[Transition]] object itself implement this interface.
|
269 | * Note: the Transition object only allows hooks to be registered before the Transition is started.
|
270 | */
|
271 | export interface IHookRegistry {
|
272 | /** @internal place to store the hooks */
|
273 | _registeredHooks: {
|
274 | [key: string]: RegisteredHook[];
|
275 | };
|
276 | /**
|
277 | * Registers a [[TransitionHookFn]], called *before a transition starts*.
|
278 | *
|
279 | * Registers a transition lifecycle hook, which is invoked before a transition even begins.
|
280 | * This hook can be useful to implement logic which prevents a transition from even starting, such
|
281 | * as authentication, redirection
|
282 | *
|
283 | * See [[TransitionHookFn]] for the signature of the function.
|
284 | *
|
285 | * The [[HookMatchCriteria]] is used to determine which Transitions the hook should be invoked for.
|
286 | * To match all Transitions, use an empty criteria object `{}`.
|
287 | *
|
288 | * ### Lifecycle
|
289 | *
|
290 | * `onBefore` hooks are invoked *before a Transition starts*.
|
291 | * No resolves have been fetched yet.
|
292 | * Each `onBefore` hook is invoked synchronously, in the same call stack as [[StateService.transitionTo]].
|
293 | * The registered `onBefore` hooks are invoked in priority order.
|
294 | *
|
295 | * Note: during the `onBefore` phase, additional hooks can be added to the specific [[Transition]] instance.
|
296 | * These "on-the-fly" hooks only affect the currently running transition..
|
297 | *
|
298 | * ### Return value
|
299 | *
|
300 | * The hook's return value can be used to pause, cancel, or redirect the current Transition.
|
301 | * See [[HookResult]] for more information.
|
302 | *
|
303 | * If any hook modifies the transition *synchronously* (by throwing, returning `false`, or returning
|
304 | * a [[TargetState]]), the remainder of the hooks are skipped.
|
305 | * If a hook returns a promise, the remainder of the `onBefore` hooks are still invoked synchronously.
|
306 | * All promises are resolved, and processed asynchronously before the `onStart` phase of the Transition.
|
307 | *
|
308 | * ### Examples
|
309 | *
|
310 | * #### Default Substate
|
311 | *
|
312 | * This example redirects any transition from 'home' to 'home.dashboard'. This is commonly referred to as a
|
313 | * "default substate".
|
314 | *
|
315 | * @example
|
316 | * ```js
|
317 | * // ng2
|
318 | * transitionService.onBefore({ to: 'home' }, (trans: Transition) =>
|
319 | * trans.router.stateService.target("home.dashboard"));
|
320 | * ```
|
321 | *
|
322 | * #### Data Driven Default Substate
|
323 | *
|
324 | * This example provides data-driven default substate functionality. It matches on a transition to any state
|
325 | * which has `defaultSubstate: "some.sub.state"` defined. See: [[Transition.to]] which returns the "to state"
|
326 | * definition.
|
327 | *
|
328 | * @example
|
329 | * ```js
|
330 | * // ng1
|
331 | * // state declaration
|
332 | * {
|
333 | * name: 'home',
|
334 | * template: '<div ui-view/>',
|
335 | * defaultSubstate: 'home.dashboard'
|
336 | * }
|
337 | *
|
338 | * var criteria = {
|
339 | * to: function(state) {
|
340 | * return state.defaultSubstate != null;
|
341 | * }
|
342 | * }
|
343 | *
|
344 | * $transitions.onBefore(criteria, function(trans: Transition) {
|
345 | * var substate = trans.to().defaultSubstate;
|
346 | * return trans.router.stateService.target(substate);
|
347 | * });
|
348 | * ```
|
349 | *
|
350 | *
|
351 | * #### Require authentication
|
352 | *
|
353 | * This example cancels a transition to a state which requires authentication, if the user is not currently authenticated.
|
354 | *
|
355 | * This example assumes a state tree where all states which require authentication are children of a parent `'requireauth'` state.
|
356 | * This example assumes `MyAuthService` synchronously returns a boolean from `isAuthenticated()`.
|
357 | *
|
358 | * #### Example:
|
359 | * ```js
|
360 | * // ng1
|
361 | * $transitions.onBefore( { to: 'requireauth.**' }, function(trans) {
|
362 | * var myAuthService = trans.injector().get('MyAuthService');
|
363 | * // If isAuthenticated returns false, the transition is cancelled.
|
364 | * return myAuthService.isAuthenticated();
|
365 | * });
|
366 | * ```
|
367 | *
|
368 | * @param matchCriteria defines which Transitions the Hook should be invoked for.
|
369 | * @param callback the hook function which will be invoked.
|
370 | * @returns a function which deregisters the hook.
|
371 | */
|
372 | onBefore(matchCriteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): Function;
|
373 | /**
|
374 | * Registers a [[TransitionHookFn]], called when a transition starts.
|
375 | *
|
376 | * Registers a transition lifecycle hook, which is invoked as a transition starts running.
|
377 | * This hook can be useful to perform some asynchronous action before completing a transition.
|
378 | *
|
379 | * See [[TransitionHookFn]] for the signature of the function.
|
380 | *
|
381 | * The [[HookMatchCriteria]] is used to determine which Transitions the hook should be invoked for.
|
382 | * To match all Transitions, use an empty criteria object `{}`.
|
383 | *
|
384 | * ### Lifecycle
|
385 | *
|
386 | * `onStart` hooks are invoked asynchronously when the Transition starts running.
|
387 | * This happens after the `onBefore` phase is complete.
|
388 | * At this point, the Transition has not yet exited nor entered any states.
|
389 | * The registered `onStart` hooks are invoked in priority order.
|
390 | *
|
391 | * Note: A built-in `onStart` hook with high priority is used to fetch any eager resolve data.
|
392 | *
|
393 | * ### Return value
|
394 | *
|
395 | * The hook's return value can be used to pause, cancel, or redirect the current Transition.
|
396 | * See [[HookResult]] for more information.
|
397 | *
|
398 | * ### Example
|
399 | *
|
400 | * #### Login during transition
|
401 | *
|
402 | * This example intercepts any transition to a state which requires authentication, when the user is
|
403 | * not currently authenticated. It allows the user to authenticate asynchronously, then resumes the
|
404 | * transition. If the user did not authenticate successfully, it redirects to the "guest" state, which
|
405 | * does not require authentication.
|
406 | *
|
407 | * This example assumes:
|
408 | * - a state tree where all states which require authentication are children of a parent `'auth'` state.
|
409 | * - `MyAuthService.isAuthenticated()` synchronously returns a boolean.
|
410 | * - `MyAuthService.authenticate()` presents a login dialog, and returns a promise which is resolved
|
411 | * or rejected, whether or not the login attempt was successful.
|
412 | *
|
413 | * #### Example:
|
414 | * ```js
|
415 | * // ng1
|
416 | * $transitions.onStart( { to: 'auth.**' }, function(trans) {
|
417 | * var $state = trans.router.stateService;
|
418 | * var MyAuthService = trans.injector().get('MyAuthService');
|
419 | *
|
420 | * // If the user is not authenticated
|
421 | * if (!MyAuthService.isAuthenticated()) {
|
422 | *
|
423 | * // Then return a promise for a successful login.
|
424 | * // The transition will wait for this promise to settle
|
425 | *
|
426 | * return MyAuthService.authenticate().catch(function() {
|
427 | *
|
428 | * // If the authenticate() method failed for whatever reason,
|
429 | * // redirect to a 'guest' state which doesn't require auth.
|
430 | * return $state.target("guest");
|
431 | * });
|
432 | * }
|
433 | * });
|
434 | * ```
|
435 | *
|
436 | * @param matchCriteria defines which Transitions the Hook should be invoked for.
|
437 | * @param callback the hook function which will be injected and invoked.
|
438 | * @returns a function which deregisters the hook.
|
439 | */
|
440 | onStart(matchCriteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): Function;
|
441 | /**
|
442 | * Registers a [[TransitionStateHookFn]], called when a specific state is entered.
|
443 | *
|
444 | * Registers a lifecycle hook, which is invoked (during a transition) when a specific state is being entered.
|
445 | *
|
446 | * Since this hook is run only when the specific state is being *entered*, it can be useful for
|
447 | * performing tasks when entering a submodule/feature area such as initializing a stateful service,
|
448 | * or for guarding access to a submodule/feature area.
|
449 | *
|
450 | * See [[TransitionStateHookFn]] for the signature of the function.
|
451 | *
|
452 | * The [[HookMatchCriteria]] is used to determine which Transitions the hook should be invoked for.
|
453 | * `onEnter` hooks generally specify `{ entering: 'somestate' }`.
|
454 | * To match all Transitions, use an empty criteria object `{}`.
|
455 | *
|
456 | * ### Lifecycle
|
457 | *
|
458 | * `onEnter` hooks are invoked when the Transition is entering a state.
|
459 | * States are entered after the `onRetain` phase is complete.
|
460 | * If more than one state is being entered, the parent state is entered first.
|
461 | * The registered `onEnter` hooks for a state are invoked in priority order.
|
462 | *
|
463 | * Note: A built-in `onEnter` hook with high priority is used to fetch lazy resolve data for states being entered.
|
464 | *
|
465 | * ### Return value
|
466 | *
|
467 | * The hook's return value can be used to pause, cancel, or redirect the current Transition.
|
468 | * See [[HookResult]] for more information.
|
469 | *
|
470 | * ### Inside a state declaration
|
471 | *
|
472 | * Instead of registering `onEnter` hooks using the [[TransitionService]], you may define an `onEnter` hook
|
473 | * directly on a state declaration (see: [[StateDeclaration.onEnter]]).
|
474 | *
|
475 | *
|
476 | * ### Examples
|
477 | *
|
478 | * #### Audit Log
|
479 | *
|
480 | * This example uses a service to log that a user has entered the admin section of an app.
|
481 | * This assumes that there are substates of the "admin" state, such as "admin.users", "admin.pages", etc.
|
482 | * @example
|
483 | * ```
|
484 | *
|
485 | * $transitions.onEnter({ entering: 'admin' }, function(transition, state) {
|
486 | * var AuditService = trans.injector().get('AuditService');
|
487 | * AuditService.log("Entered " + state.name + " module while transitioning to " + transition.to().name);
|
488 | * }
|
489 | * ```
|
490 | *
|
491 | * #### Audit Log (inside a state declaration)
|
492 | *
|
493 | * The `onEnter` inside this state declaration is syntactic sugar for the previous Audit Log example.
|
494 | * ```
|
495 | * {
|
496 | * name: 'admin',
|
497 | * component: 'admin',
|
498 | * onEnter: function($transition$, $state$) {
|
499 | * var AuditService = $transition$.injector().get('AuditService');
|
500 | * AuditService.log("Entered " + state.name + " module while transitioning to " + transition.to().name);
|
501 | * }
|
502 | * }
|
503 | * ```
|
504 | *
|
505 | * Note: A state declaration's `onEnter` function is injected for Angular 1 only.
|
506 | *
|
507 | * @param matchCriteria defines which Transitions the Hook should be invoked for.
|
508 | * @param callback the hook function which will be injected and invoked.
|
509 | * @returns a function which deregisters the hook.
|
510 | */
|
511 | onEnter(matchCriteria: HookMatchCriteria, callback: TransitionStateHookFn, options?: HookRegOptions): Function;
|
512 | /**
|
513 | * Registers a [[TransitionStateHookFn]], called when a specific state is retained/kept.
|
514 | *
|
515 | * Registers a lifecycle hook, which is invoked (during a transition) for
|
516 | * a specific state that was previously active will remain active (is not being entered nor exited).
|
517 | *
|
518 | * This hook is invoked when a state is "retained" or "kept".
|
519 | * It means the transition is coming *from* a substate of the retained state *to* a substate of the retained state.
|
520 | * This hook can be used to perform actions when the user moves from one substate to another, such as between steps in a wizard.
|
521 | *
|
522 | * The [[HookMatchCriteria]] is used to determine which Transitions the hook should be invoked for.
|
523 | * `onRetain` hooks generally specify `{ retained: 'somestate' }`.
|
524 | * To match all Transitions, use an empty criteria object `{}`.
|
525 | *
|
526 | * ### Lifecycle
|
527 | *
|
528 | * `onRetain` hooks are invoked after any `onExit` hooks have been fired.
|
529 | * If more than one state is retained, the child states' `onRetain` hooks are invoked first.
|
530 | * The registered `onRetain` hooks for a state are invoked in priority order.
|
531 | *
|
532 | * ### Return value
|
533 | *
|
534 | * The hook's return value can be used to pause, cancel, or redirect the current Transition.
|
535 | * See [[HookResult]] for more information.
|
536 | *
|
537 | * ### Inside a state declaration
|
538 | *
|
539 | * Instead of registering `onRetain` hooks using the [[TransitionService]], you may define an `onRetain` hook
|
540 | * directly on a state declaration (see: [[StateDeclaration.onRetain]]).
|
541 | *
|
542 | * Note: A state declaration's `onRetain` function is injected for Angular 1 only.
|
543 | *
|
544 | * @param matchCriteria defines which Transitions the Hook should be invoked for.
|
545 | * @param callback the hook function which will be injected and invoked.
|
546 | * @returns a function which deregisters the hook.
|
547 | */
|
548 | onRetain(matchCriteria: HookMatchCriteria, callback: TransitionStateHookFn, options?: HookRegOptions): Function;
|
549 | /**
|
550 | * Registers a [[TransitionStateHookFn]], called when a specific state is exited.
|
551 | *
|
552 | * Registers a lifecycle hook, which is invoked (during a transition) when a specific state is being exited.
|
553 | *
|
554 | * Since this hook is run only when the specific state is being *exited*, it can be useful for
|
555 | * performing tasks when leaving a submodule/feature area such as cleaning up a stateful service,
|
556 | * or for preventing the user from leaving a state or submodule until some criteria is satisfied.
|
557 | *
|
558 | * See [[TransitionStateHookFn]] for the signature of the function.
|
559 | *
|
560 | * The [[HookMatchCriteria]] is used to determine which Transitions the hook should be invoked for.
|
561 | * `onExit` hooks generally specify `{ exiting: 'somestate' }`.
|
562 | * To match all Transitions, use an empty criteria object `{}`.
|
563 | *
|
564 | * ### Lifecycle
|
565 | *
|
566 | * `onExit` hooks are invoked when the Transition is exiting a state.
|
567 | * States are exited after any `onStart` phase is complete.
|
568 | * If more than one state is being exited, the child states are exited first.
|
569 | * The registered `onExit` hooks for a state are invoked in priority order.
|
570 | *
|
571 | * ### Return value
|
572 | *
|
573 | * The hook's return value can be used to pause, cancel, or redirect the current Transition.
|
574 | * See [[HookResult]] for more information.
|
575 | *
|
576 | * ### Inside a state declaration
|
577 | *
|
578 | * Instead of registering `onExit` hooks using the [[TransitionService]], you may define an `onExit` hook
|
579 | * directly on a state declaration (see: [[StateDeclaration.onExit]]).
|
580 | *
|
581 | * Note: A state declaration's `onExit` function is injected for Angular 1 only.
|
582 | *
|
583 | * @param matchCriteria defines which Transitions the Hook should be invoked for.
|
584 | * @param callback the hook function which will be injected and invoked.
|
585 | * @returns a function which deregisters the hook.
|
586 | */
|
587 | onExit(matchCriteria: HookMatchCriteria, callback: TransitionStateHookFn, options?: HookRegOptions): Function;
|
588 | /**
|
589 | * Registers a [[TransitionHookFn]], called *just before a transition finishes*.
|
590 | *
|
591 | * Registers a transition lifecycle hook, which is invoked just before a transition finishes.
|
592 | * This hook is a last chance to cancel or redirect a transition.
|
593 | *
|
594 | * See [[TransitionHookFn]] for the signature of the function.
|
595 | *
|
596 | * The [[HookMatchCriteria]] is used to determine which Transitions the hook should be invoked for.
|
597 | * To match all Transitions, use an empty criteria object `{}`.
|
598 | *
|
599 | * ### Lifecycle
|
600 | *
|
601 | * `onFinish` hooks are invoked after the `onEnter` phase is complete.
|
602 | * These hooks are invoked just before the transition is "committed".
|
603 | * Each hook is invoked in priority order.
|
604 | *
|
605 | * ### Return value
|
606 | *
|
607 | * The hook's return value can be used to pause, cancel, or redirect the current Transition.
|
608 | * See [[HookResult]] for more information.
|
609 | *
|
610 | * @param matchCriteria defines which Transitions the Hook should be invoked for.
|
611 | * @param callback the hook function which will be injected and invoked.
|
612 | * @returns a function which deregisters the hook.
|
613 | */
|
614 | onFinish(matchCriteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): Function;
|
615 | /**
|
616 | * Registers a [[TransitionHookFn]], called after a successful transition completed.
|
617 | *
|
618 | * Registers a transition lifecycle hook, which is invoked after a transition successfully completes.
|
619 | *
|
620 | * See [[TransitionHookFn]] for the signature of the function.
|
621 | *
|
622 | * The [[HookMatchCriteria]] is used to determine which Transitions the hook should be invoked for.
|
623 | * To match all Transitions, use an empty criteria object `{}`.
|
624 | *
|
625 | * ### Lifecycle
|
626 | *
|
627 | * `onSuccess` hooks are chained off the Transition's promise (see [[Transition.promise]]).
|
628 | * If the Transition is successful and its promise is resolved, then the `onSuccess` hooks are invoked.
|
629 | * Since these hooks are run after the transition is over, their return value is ignored.
|
630 | * The `onSuccess` hooks are invoked in priority order.
|
631 | *
|
632 | * ### Return value
|
633 | *
|
634 | * Since the Transition is already completed, the hook's return value is ignored
|
635 | *
|
636 | * @param matchCriteria defines which Transitions the Hook should be invoked for.
|
637 | * @param callback the hook function which will be injected and invoked.
|
638 | * @returns a function which deregisters the hook.
|
639 | */
|
640 | onSuccess(matchCriteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): Function;
|
641 | /**
|
642 | * Registers a [[TransitionHookFn]], called after a transition has errored.
|
643 | *
|
644 | * Registers a transition lifecycle hook, which is invoked after a transition has been rejected for any reason.
|
645 | *
|
646 | * See [[TransitionHookFn]] for the signature of the function.
|
647 | *
|
648 | * The [[HookMatchCriteria]] is used to determine which Transitions the hook should be invoked for.
|
649 | * To match all Transitions, use an empty criteria object `{}`.
|
650 | *
|
651 | * ### Lifecycle
|
652 | *
|
653 | * The `onError` hooks are chained off the Transition's promise (see [[Transition.promise]]).
|
654 | * If a Transition fails, its promise is rejected and the `onError` hooks are invoked.
|
655 | * The `onError` hooks are invoked in priority order.
|
656 | *
|
657 | * Since these hooks are run after the transition is over, their return value is ignored.
|
658 | *
|
659 | * A transition "errors" if it was started, but failed to complete (for any reason).
|
660 | * A *non-exhaustive list* of reasons a transition can error:
|
661 | *
|
662 | * - A transition was cancelled because a new transition started while it was still running (`Transition superseded`)
|
663 | * - A transition was cancelled by a Transition Hook returning false
|
664 | * - A transition was redirected by a Transition Hook returning a [[TargetState]]
|
665 | * - A Transition Hook or resolve function threw an error
|
666 | * - A Transition Hook returned a rejected promise
|
667 | * - A resolve function returned a rejected promise
|
668 | *
|
669 | * To check the failure reason, inspect the return value of [[Transition.error]].
|
670 | *
|
671 | * Note: `onError` should be used for targeted error handling, or error recovery.
|
672 | * For simple catch-all error reporting, use [[StateService.defaultErrorHandler]].
|
673 | *
|
674 | * ### Return value
|
675 | *
|
676 | * Since the Transition is already completed, the hook's return value is ignored
|
677 | *
|
678 | * @param matchCriteria defines which Transitions the Hook should be invoked for.
|
679 | * @param callback the hook function which will be injected and invoked.
|
680 | * @returns a function which deregisters the hook.
|
681 | */
|
682 | onError(matchCriteria: HookMatchCriteria, callback: TransitionHookFn, options?: HookRegOptions): Function;
|
683 | /**
|
684 | * Returns all the registered hooks of a given `hookName` type
|
685 | *
|
686 | * #### Example:
|
687 | * ```
|
688 | * $transitions.getHooks("onEnter")
|
689 | * ```
|
690 | */
|
691 | getHooks(hookName: string): RegisteredHook[];
|
692 | }
|
693 | /** A predicate type which tests if a [[StateObject]] and [[Transition]] passes some test. Returns a boolean. */
|
694 | export declare type IStateMatch = PredicateBinary<StateObject, Transition>;
|
695 | /**
|
696 | * This object is used to configure whether or not a Transition Hook is invoked for a particular transition,
|
697 | * based on the Transition's "to state" and "from state".
|
698 | *
|
699 | * Each property (`to`, `from`, `exiting`, `retained`, and `entering`) can be a state [[Glob]] string,
|
700 | * a boolean, or a function that takes a state and returns a boolean (see [[HookMatchCriterion]])
|
701 | *
|
702 | * All properties are optional. If any property is omitted, it is replaced with the value `true`, and always matches.
|
703 | * To match any transition, use an empty criteria object `{}`.
|
704 | *
|
705 | * #### Example:
|
706 | * ```js
|
707 | * // This matches a transition coming from the `parent` state and going to the `parent.child` state.
|
708 | * var match = {
|
709 | * to: 'parent',
|
710 | * from: 'parent.child'
|
711 | * }
|
712 | * ```
|
713 | *
|
714 | * #### Example:
|
715 | * ```js
|
716 | * // This matches a transition coming from any substate of `parent` and going directly to the `parent` state.
|
717 | * var match = {
|
718 | * to: 'parent',
|
719 | * from: 'parent.**'
|
720 | * }
|
721 | * ```
|
722 | *
|
723 | * #### Example:
|
724 | * ```js
|
725 | * // This matches a transition coming from any state and going to any substate of `mymodule`
|
726 | * var match = {
|
727 | * to: 'mymodule.**'
|
728 | * }
|
729 | * ```
|
730 | *
|
731 | * #### Example:
|
732 | * ```js
|
733 | * // This matches a transition coming from any state and going to any state that has `data.authRequired`
|
734 | * // set to a truthy value.
|
735 | * var match = {
|
736 | * to: function(state) {
|
737 | * return state.data != null && state.data.authRequired === true;
|
738 | * }
|
739 | * }
|
740 | * ```
|
741 | * #### Example:
|
742 | * ```js
|
743 | * // This will match when route is just entered (initial load) or when the state is hard-refreshed
|
744 | * // by specifying `{refresh: true}` as transition options.
|
745 | * var match = {
|
746 | * from: (state, transition) => state.self.name === '' || transition.options().reload
|
747 | * }
|
748 | * ```
|
749 | *
|
750 | * #### Example:
|
751 | * ```js
|
752 | * // This matches a transition that is exiting `parent.child`
|
753 | * var match = {
|
754 | * exiting: 'parent.child'
|
755 | * }
|
756 | * ```
|
757 | */
|
758 | export interface HookMatchCriteria {
|
759 | [key: string]: HookMatchCriterion | undefined;
|
760 | /** A [[HookMatchCriterion]] to match the destination state */
|
761 | to?: HookMatchCriterion;
|
762 | /** A [[HookMatchCriterion]] to match the original (from) state */
|
763 | from?: HookMatchCriterion;
|
764 | /** A [[HookMatchCriterion]] to match any state that would be exiting */
|
765 | exiting?: HookMatchCriterion;
|
766 | /** A [[HookMatchCriterion]] to match any state that would be retained */
|
767 | retained?: HookMatchCriterion;
|
768 | /** A [[HookMatchCriterion]] to match any state that would be entering */
|
769 | entering?: HookMatchCriterion;
|
770 | }
|
771 | export interface IMatchingNodes {
|
772 | [key: string]: PathNode[];
|
773 | to: PathNode[];
|
774 | from: PathNode[];
|
775 | exiting: PathNode[];
|
776 | retained: PathNode[];
|
777 | entering: PathNode[];
|
778 | }
|
779 | /** @internal */
|
780 | export interface RegisteredHooks {
|
781 | [key: string]: RegisteredHook[];
|
782 | }
|
783 | /** @internal */
|
784 | export interface PathTypes {
|
785 | [key: string]: PathType;
|
786 | to: PathType;
|
787 | from: PathType;
|
788 | exiting: PathType;
|
789 | retained: PathType;
|
790 | entering: PathType;
|
791 | }
|
792 | /** @internal */
|
793 | export interface PathType {
|
794 | name: string;
|
795 | scope: TransitionHookScope;
|
796 | }
|
797 | /**
|
798 | * Hook Criterion used to match a transition.
|
799 | *
|
800 | * A [[Glob]] string that matches the name of a state.
|
801 | *
|
802 | * Or, a function with the signature `function(state, transition) { return matches; }`
|
803 | * which should return a boolean to indicate if a state matches.
|
804 | *
|
805 | * Or, `true` to always match
|
806 | */
|
807 | export declare type HookMatchCriterion = string | IStateMatch | boolean;
|
808 | declare enum TransitionHookPhase {
|
809 | CREATE = 0,
|
810 | BEFORE = 1,
|
811 | RUN = 2,
|
812 | SUCCESS = 3,
|
813 | ERROR = 4
|
814 | }
|
815 | declare enum TransitionHookScope {
|
816 | TRANSITION = 0,
|
817 | STATE = 1
|
818 | }
|
819 | export { TransitionHookPhase, TransitionHookScope };
|