-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathtransition.ts
438 lines (386 loc) · 15.1 KB
/
transition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/** @module transition */ /** for typedoc */
/// <reference path='../../typings/angularjs/angular.d.ts' />
import {IPromise} from "angular";
import {trace} from "../common/trace";
import {services} from "../common/coreservices";
import {
map, find, extend, filter, mergeR, unnest, tail,
omit, toJson, abstractKey, arrayTuples, allTrueR
} from "../common/common";
import { isObject } from "../common/predicates";
import { not, prop, propEq, val } from "../common/hof";
import {StateDeclaration, StateOrName} from "../state/interface";
import {TransitionOptions, TransitionHookOptions, TreeChanges, IHookRegistry, IHookRegistration, IHookGetter} from "./interface";
import {TransitionHook, HookRegistry, matchState, HookBuilder, RejectFactory} from "./module";
import {Node} from "../path/node";
import {PathFactory} from "../path/pathFactory";
import {State, TargetState} from "../state/module";
import {Param} from "../params/module";
import {Resolvable} from "../resolve/module";
import {ViewConfig} from "../view/module";
import {TransitionService} from "./transitionService";
let transitionCount = 0, REJECT = new RejectFactory();
const stateSelf: (_state: State) => StateDeclaration = prop("self");
/**
* The representation of a transition between two states.
*
* Contains all contextual information about the to/from states, parameters, resolves, as well as the
* list of states being entered and exited as a result of this transition.
*/
export class Transition implements IHookRegistry {
$id: number;
private _deferred = services.$q.defer();
promise: IPromise<any> = this._deferred.promise;
private _options: TransitionOptions;
private _treeChanges: TreeChanges;
/**
* Registers a callback function as an `onBefore` Transition Hook
*
* The hook is only registered for this specific `Transition`. For global hooks, use [[TransitionService.onBefore]]
*
* See [[IHookRegistry.onBefore]]
*/
onBefore: IHookRegistration;
/**
* Registers a callback function as an `onStart` Transition Hook
*
* The hook is only registered for this specific `Transition`. For global hooks, use [[TransitionService.onStart]]
*
* See [[IHookRegistry.onStart]]
*/
onStart: IHookRegistration;
/**
* Registers a callback function as an `onEnter` State Hook
*
* The hook is only registered for this specific `Transition`. For global hooks, use [[TransitionService.onEnter]]
*
* See [[IHookRegistry.onEnter]]
*/
onEnter: IHookRegistration;
/**
* Registers a callback function as an `onRetain` State Hook
*
* The hook is only registered for this specific `Transition`. For global hooks, use [[TransitionService.onRetain]]
*
* See [[IHookRegistry.onRetain]]
*/
onRetain: IHookRegistration;
/**
* Registers a callback function as an `onExit` State Hook
*
* The hook is only registered for this specific `Transition`. For global hooks, use [[TransitionService.onExit]]
*
* See [[IHookRegistry.onExit]]
*/
onExit: IHookRegistration;
/**
* Registers a callback function as an `onFinish` Transition Hook
*
* The hook is only registered for this specific `Transition`. For global hooks, use [[TransitionService.onFinish]]
*
* See [[IHookRegistry.onFinish]]
*/
onFinish: IHookRegistration;
/**
* Registers a callback function as an `onSuccess` Transition Hook
*
* The hook is only registered for this specific `Transition`. For global hooks, use [[TransitionService.onSuccess]]
*
* See [[IHookRegistry.onSuccess]]
*/
onSuccess: IHookRegistration;
/**
* Registers a callback function as an `onError` Transition Hook
*
* The hook is only registered for this specific `Transition`. For global hooks, use [[TransitionService.onError]]
*
* See [[IHookRegistry.onError]]
*/
onError: IHookRegistration;
getHooks: IHookGetter;
/**
* Creates a new Transition object.
*
* If the target state is not valid, an error is thrown.
*
* @param fromPath The path of [[Node]]s from which the transition is leaving. The last node in the `fromPath`
* encapsulates the "from state".
* @param targetState The target state and parameters being transitioned to (also, the transition options)
* @param $transitions The Transition Service instance
*/
constructor(fromPath: Node[], targetState: TargetState, private $transitions: TransitionService) {
if (!targetState.valid()) {
throw new Error(targetState.error());
}
// Makes the Transition instance a hook registry (onStart, etc)
HookRegistry.mixin(new HookRegistry(), this);
// current() is assumed to come from targetState.options, but provide a naive implementation otherwise.
this._options = extend({ current: val(this) }, targetState.options());
this.$id = transitionCount++;
let toPath = PathFactory.buildToPath(fromPath, targetState);
this._treeChanges = PathFactory.treeChanges(fromPath, toPath, this._options.reloadState);
PathFactory.bindTransitionResolve(this._treeChanges, this);
}
$from() {
return tail(this._treeChanges.from).state;
}
$to() {
return tail(this._treeChanges.to).state;
}
/**
* Returns the "from state"
*
* @returns The state object for the Transition's "from state".
*/
from(): StateDeclaration {
return this.$from().self;
}
/**
* Returns the "to state"
*
* @returns The state object for the Transition's target state ("to state").
*/
to() {
return this.$to().self;
}
/**
* Determines whether two transitions are equivalent.
*/
is(compare: (Transition|{to: any, from: any})): boolean {
if (compare instanceof Transition) {
// TODO: Also compare parameters
return this.is({ to: compare.$to().name, from: compare.$from().name });
}
return !(
(compare.to && !matchState(this.$to(), compare.to)) ||
(compare.from && !matchState(this.$from(), compare.from))
);
}
/**
* Gets transition parameter values
*
* @param pathname Pick which treeChanges path to get parameters for:
* (`'to'`, `'from'`, `'entering'`, `'exiting'`, `'retained'`)
* @returns transition parameter values for the desired path.
*/
params(pathname: string = "to"): { [key: string]: any } {
return this._treeChanges[pathname].map(prop("values")).reduce(mergeR, {});
}
/**
* Get resolved data
*
* @returns an object (key/value pairs) where keys are resolve names and values are any settled resolve data,
* or `undefined` for pending resolve data
*/
resolves(): { [resolveName: string]: any } {
return map(tail(this._treeChanges.to).resolveContext.getResolvables(), res => res.data);
}
/**
* Adds new resolves to this transition.
*
* @param resolves an [[ResolveDeclarations]] object which describes the new resolves
* @param state the state in the "to path" which should receive the new resolves (otherwise, the root state)
*/
addResolves(resolves: { [key: string]: Function }, state: StateOrName = ""): void {
let stateName: string = (typeof state === "string") ? state : state.name;
let topath = this._treeChanges.to;
let targetNode = find(topath, node => node.state.name === stateName);
tail(topath).resolveContext.addResolvables(Resolvable.makeResolvables(resolves), targetNode.state);
}
/**
* Gets the previous transition, from which this transition was redirected.
*
* @returns The previous Transition, or null if this Transition is not the result of a redirection
*/
previous(): Transition {
return this._options.previous || null;
}
/**
* Get the transition options
*
* @returns the options for this Transition.
*/
options(): TransitionOptions {
return this._options;
}
/**
* Gets the states being entered.
*
* @returns an array of states that will be entered during this transition.
*/
entering(): StateDeclaration[] {
return map(this._treeChanges.entering, prop('state')).map(stateSelf);
}
/**
* Gets the states being exited.
*
* @returns an array of states that will be exited during this transition.
*/
exiting(): StateDeclaration[] {
return map(this._treeChanges.exiting, prop('state')).map(stateSelf).reverse();
}
/**
* Gets the states being retained.
*
* @returns an array of states that are already entered from a previous Transition, that will not be
* exited during this Transition
*/
retained(): StateDeclaration[] {
return map(this._treeChanges.retained, prop('state')).map(stateSelf);
}
/**
* Get the [[ViewConfig]]s associated with this Transition
*
* Each state can define one or more views (template/controller), which are encapsulated as `ViewConfig` objects.
* This method fetches the `ViewConfigs` for a given path in the Transition (e.g., "to" or "entering").
*
* @param pathname the name of the path to fetch views for:
* (`'to'`, `'from'`, `'entering'`, `'exiting'`, `'retained'`)
* @param state If provided, only returns the `ViewConfig`s for a single state in the path
*
* @returns a list of ViewConfig objects for the given path.
*/
views(pathname: string = "entering", state?: State): ViewConfig[] {
let path = this._treeChanges[pathname];
return state ? find(path, propEq('state', state)).views : unnest(path.map(prop("views")));
}
treeChanges = () => this._treeChanges;
/**
* @ngdoc function
* @name ui.router.state.type:Transition#redirect
* @methodOf ui.router.state.type:Transition
*
* @description
* Creates a new transition that is a redirection of the current one. This transition can
* be returned from a `$transitionsProvider` hook, `$state` event, or other method, to
* redirect a transition to a new state and/or set of parameters.
*
* @returns {Transition} Returns a new `Transition` instance.
*/
redirect(targetState: TargetState): Transition {
let newOptions = extend({}, this.options(), targetState.options(), { previous: this });
targetState = new TargetState(targetState.identifier(), targetState.$state(), targetState.params(), newOptions);
let redirectTo = new Transition(this._treeChanges.from, targetState, this.$transitions);
// If the current transition has already resolved any resolvables which are also in the redirected "to path", then
// add those resolvables to the redirected transition. Allows you to define a resolve at a parent level, wait for
// the resolve, then redirect to a child state based on the result, and not have to re-fetch the resolve.
let redirectedPath = this.treeChanges().to;
let matching: Node[] = Node.matching(redirectTo.treeChanges().to, redirectedPath);
const includeResolve = (resolve, key) => ['$stateParams', '$transition$'].indexOf(key) === -1;
matching.forEach((node, idx) => extend(node.resolves, filter(redirectedPath[idx].resolves, includeResolve)));
return redirectTo;
}
/**
* Checks if the transition will be ignored.
*
* Indicates whether the transition should be ignored, based on whether the to and from states are the
* same, whether the parameters are equal (or dynamic), and whether the `reload` option is set.
*
* @returns true if the Transition should be ignored.
*/
ignored(): boolean {
let {to, from} = this._treeChanges;
if (this._options.reload || tail(to).state !== tail(from).state) return false;
let nodeSchemas: Param[][] = to.map(node => node.schema.filter(not(prop('dynamic'))));
let [toValues, fromValues] = [to, from].map(path => path.map(prop('values')));
let tuples = arrayTuples(nodeSchemas, toValues, fromValues);
return tuples.map(([schema, toVals, fromVals]) => Param.equals(schema, toVals, fromVals)).reduce(allTrueR, true);
}
/**
* @hidden
*/
hookBuilder(): HookBuilder {
return new HookBuilder(this.$transitions, this, <TransitionHookOptions> {
transition: this,
current: this._options.current
});
}
/**
* Runs the transition
*
* This method is generally called from the [[StateService.transitionTo]]
*
* @returns a promise for a successful transition.
*/
run (): IPromise<any> {
let hookBuilder = this.hookBuilder();
let runSynchronousHooks = TransitionHook.runSynchronousHooks;
// TODO: nuke these in favor of chaining off the promise, i.e.,
// $transitions.onBefore({}, $transition$ => {$transition$.promise.then()}
const runSuccessHooks = () => runSynchronousHooks(hookBuilder.getOnSuccessHooks(), {}, true);
const runErrorHooks = ($error$) => runSynchronousHooks(hookBuilder.getOnErrorHooks(), { $error$ }, true);
// Run the success/error hooks *after* the Transition promise is settled.
this.promise.then(runSuccessHooks, runErrorHooks);
let syncResult = runSynchronousHooks(hookBuilder.getOnBeforeHooks());
if (TransitionHook.isRejection(syncResult)) {
let rejectReason = (<any> syncResult).reason;
this._deferred.reject(rejectReason);
return this.promise;
}
if (!this.valid()) {
let error = new Error(this.error());
this._deferred.reject(error);
return this.promise;
}
if (this.ignored()) {
trace.traceTransitionIgnored(this);
let ignored = REJECT.ignored();
this._deferred.reject(ignored.reason);
return this.promise;
}
// When the chain is complete, then resolve or reject the deferred
const resolve = () => {
this._deferred.resolve(this);
trace.traceSuccess(this.$to(), this);
};
const reject = (error) => {
this._deferred.reject(error);
trace.traceError(error, this);
return services.$q.reject(error);
};
trace.traceTransitionStart(this);
let chain = hookBuilder.asyncHooks().reduce((_chain, step) => _chain.then(step.invokeStep), syncResult);
chain.then(resolve, reject);
return this.promise;
}
isActive = () => this === this._options.current();
/**
* Checks if the Transition is valid
*
* @returns true if the Transition is valid
*/
valid() {
return !this.error();
}
/**
* The reason the Transition is invalid
*
* @returns an error message explaining why the transition is invalid
*/
error() {
let state = this.$to();
if (state.self[abstractKey])
return `Cannot transition to abstract state '${state.name}'`;
if (!Param.validates(state.parameters(), this.params()))
return `Param values not valid for state '${state.name}'`;
}
/**
* A string representation of the Transition
*
* @returns A string representation of the Transition
*/
toString () {
let fromStateOrName = this.from();
let toStateOrName = this.to();
const avoidEmptyHash = (params) =>
(params["#"] !== null && params["#"] !== undefined) ? params : omit(params, "#");
// (X) means the to state is invalid.
let id = this.$id,
from = isObject(fromStateOrName) ? fromStateOrName.name : fromStateOrName,
fromParams = toJson(avoidEmptyHash(this._treeChanges.from.map(prop('values')).reduce(mergeR, {}))),
toValid = this.valid() ? "" : "(X) ",
to = isObject(toStateOrName) ? toStateOrName.name : toStateOrName,
toParams = toJson(avoidEmptyHash(this.params()));
return `Transition#${id}( '${from}'${fromParams} -> ${toValid}'${to}'${toParams} )`;
}
}