@angular-wave/angular.ts 0.0.10 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/README.md +8 -15
  2. package/dist/angular-ts.esm.js +1 -1
  3. package/dist/angular-ts.umd.js +1 -1
  4. package/package.json +4 -1
  5. package/src/core/compile.js +0 -26
  6. package/src/exts/messages.md +30 -30
  7. package/src/public.js +2 -0
  8. package/src/router/adapter/directives/stateDirectives.js +695 -0
  9. package/src/router/adapter/directives/viewDirective.js +514 -0
  10. package/src/router/adapter/injectables.js +314 -0
  11. package/src/router/adapter/interface.js +1 -0
  12. package/src/router/adapter/locationServices.js +86 -0
  13. package/src/router/adapter/services.js +132 -0
  14. package/src/router/adapter/stateFilters.js +43 -0
  15. package/src/router/adapter/stateProvider.js +137 -0
  16. package/src/router/adapter/statebuilders/onEnterExitRetain.js +30 -0
  17. package/src/router/adapter/statebuilders/views.js +146 -0
  18. package/src/router/adapter/templateFactory.js +218 -0
  19. package/src/router/adapter/urlRouterProvider.js +196 -0
  20. package/src/router/adapter/viewScroll.js +31 -0
  21. package/src/router/core/common/common.js +506 -0
  22. package/src/router/core/common/coreservices.js +15 -0
  23. package/src/router/core/common/glob.js +75 -0
  24. package/src/router/core/common/hof.js +194 -0
  25. package/src/router/core/common/predicates.js +44 -0
  26. package/src/router/core/common/queue.js +41 -0
  27. package/src/router/core/common/safeConsole.js +38 -0
  28. package/src/router/core/common/strings.js +141 -0
  29. package/src/router/core/common/trace.js +232 -0
  30. package/src/router/core/globals.js +29 -0
  31. package/src/router/core/hooks/coreResolvables.js +33 -0
  32. package/src/router/core/hooks/ignoredTransition.js +25 -0
  33. package/src/router/core/hooks/invalidTransition.js +14 -0
  34. package/src/router/core/hooks/lazyLoad.js +102 -0
  35. package/src/router/core/hooks/onEnterExitRetain.js +55 -0
  36. package/src/router/core/hooks/redirectTo.js +36 -0
  37. package/src/router/core/hooks/resolve.js +57 -0
  38. package/src/router/core/hooks/updateGlobals.js +30 -0
  39. package/src/router/core/hooks/url.js +25 -0
  40. package/src/router/core/hooks/views.js +39 -0
  41. package/src/router/core/interface.js +3 -0
  42. package/src/router/core/params/README.md +8 -0
  43. package/src/router/core/params/param.js +232 -0
  44. package/src/router/core/params/paramType.js +139 -0
  45. package/src/router/core/params/paramTypes.js +163 -0
  46. package/src/router/core/params/stateParams.js +35 -0
  47. package/src/router/core/path/pathNode.js +77 -0
  48. package/src/router/core/path/pathUtils.js +200 -0
  49. package/src/router/core/resolve/interface.js +10 -0
  50. package/src/router/core/resolve/resolvable.js +124 -0
  51. package/src/router/core/resolve/resolveContext.js +211 -0
  52. package/src/router/core/router.js +201 -0
  53. package/src/router/core/state/README.md +21 -0
  54. package/src/router/core/state/stateBuilder.js +333 -0
  55. package/src/router/core/state/stateMatcher.js +66 -0
  56. package/src/router/core/state/stateObject.js +116 -0
  57. package/src/router/core/state/stateQueueManager.js +89 -0
  58. package/src/router/core/state/stateRegistry.js +175 -0
  59. package/src/router/core/state/stateService.js +592 -0
  60. package/src/router/core/state/targetState.js +159 -0
  61. package/src/router/core/transition/hookBuilder.js +127 -0
  62. package/src/router/core/transition/hookRegistry.js +182 -0
  63. package/src/router/core/transition/interface.js +14 -0
  64. package/src/router/core/transition/rejectFactory.js +122 -0
  65. package/src/router/core/transition/transition.js +739 -0
  66. package/src/router/core/transition/transitionEventType.js +27 -0
  67. package/src/router/core/transition/transitionHook.js +199 -0
  68. package/src/router/core/transition/transitionService.js +311 -0
  69. package/src/router/core/url/interface.js +1 -0
  70. package/src/router/core/url/urlConfig.js +165 -0
  71. package/src/router/core/url/urlMatcher.js +548 -0
  72. package/src/router/core/url/urlMatcherFactory.js +123 -0
  73. package/src/router/core/url/urlRouter.js +115 -0
  74. package/src/router/core/url/urlRule.js +202 -0
  75. package/src/router/core/url/urlRules.js +348 -0
  76. package/src/router/core/url/urlService.js +268 -0
  77. package/src/router/core/vanilla/baseLocationService.js +31 -0
  78. package/src/router/core/vanilla/browserLocationConfig.js +42 -0
  79. package/src/router/core/vanilla/hashLocationService.js +19 -0
  80. package/src/router/core/vanilla/injector.js +98 -0
  81. package/src/router/core/vanilla/interface.js +1 -0
  82. package/src/router/core/vanilla/memoryLocationConfig.js +20 -0
  83. package/src/router/core/vanilla/memoryLocationService.js +13 -0
  84. package/src/router/core/vanilla/plugins.js +35 -0
  85. package/src/router/core/vanilla/pushStateLocationService.js +69 -0
  86. package/src/router/core/vanilla/q.js +54 -0
  87. package/src/router/core/vanilla/utils.js +63 -0
  88. package/src/router/core/view/interface.js +1 -0
  89. package/src/router/core/view/view.js +312 -0
  90. package/src/router/router.js +52 -0
  91. package/test/ng/compile.spec.js +43 -61
  92. package/test/ng/directive/init.spec.js +1 -1
@@ -0,0 +1,592 @@
1
+ import {
2
+ createProxyFunctions,
3
+ defaults,
4
+ extend,
5
+ inArray,
6
+ noop,
7
+ removeFrom,
8
+ silenceUncaughtInPromise,
9
+ silentRejection,
10
+ } from "../common/common";
11
+ import { isDefined, isObject, isString } from "../common/predicates";
12
+ import { Queue } from "../common/queue";
13
+ import { services } from "../common/coreservices";
14
+ import { PathUtils } from "../path/pathUtils";
15
+ import { PathNode } from "../path/pathNode";
16
+ import { defaultTransOpts } from "../transition/transitionService";
17
+ import { Rejection, RejectType } from "../transition/rejectFactory";
18
+ import { TargetState } from "./targetState";
19
+ import { Param } from "../params/param";
20
+ import { Glob } from "../common/glob";
21
+ import { ResolveContext } from "../resolve/resolveContext";
22
+ import { lazyLoadState } from "../hooks/lazyLoad";
23
+ import { not, val } from "../common/hof";
24
+ /**
25
+ * Provides services related to ui-router states.
26
+ *
27
+ * This API is located at `router.stateService` ([[UIRouter.stateService]])
28
+ */
29
+ export class StateService {
30
+ /**
31
+ * The [[Transition]] currently in progress (or null)
32
+ *
33
+ * @deprecated This is a passthrough through to [[UIRouterGlobals.transition]]
34
+ */
35
+ get transition() {
36
+ return this.router.globals.transition;
37
+ }
38
+ /**
39
+ * The latest successful state parameters
40
+ *
41
+ * @deprecated This is a passthrough through to [[UIRouterGlobals.params]]
42
+ */
43
+ get params() {
44
+ return this.router.globals.params;
45
+ }
46
+ /**
47
+ * The current [[StateDeclaration]]
48
+ *
49
+ * @deprecated This is a passthrough through to [[UIRouterGlobals.current]]
50
+ */
51
+ get current() {
52
+ return this.router.globals.current;
53
+ }
54
+ /**
55
+ * The current [[StateObject]] (an internal API)
56
+ *
57
+ * @deprecated This is a passthrough through to [[UIRouterGlobals.$current]]
58
+ */
59
+ get $current() {
60
+ return this.router.globals.$current;
61
+ }
62
+ /** @internal */
63
+ constructor(/** @internal */ router) {
64
+ this.router = router;
65
+ /** @internal */
66
+ this.invalidCallbacks = [];
67
+ /** @internal */
68
+ this._defaultErrorHandler = function $defaultErrorHandler($error$) {
69
+ if ($error$ instanceof Error && $error$.stack) {
70
+ console.error($error$);
71
+ console.error($error$.stack);
72
+ } else if ($error$ instanceof Rejection) {
73
+ console.error($error$.toString());
74
+ if ($error$.detail && $error$.detail.stack)
75
+ console.error($error$.detail.stack);
76
+ } else {
77
+ console.error($error$);
78
+ }
79
+ };
80
+ const getters = ["current", "$current", "params", "transition"];
81
+ const boundFns = Object.keys(StateService.prototype).filter(
82
+ not(inArray(getters)),
83
+ );
84
+ createProxyFunctions(
85
+ val(StateService.prototype),
86
+ this,
87
+ val(this),
88
+ boundFns,
89
+ );
90
+ }
91
+ /** @internal */
92
+ dispose() {
93
+ this.defaultErrorHandler(noop);
94
+ this.invalidCallbacks = [];
95
+ }
96
+ /**
97
+ * Handler for when [[transitionTo]] is called with an invalid state.
98
+ *
99
+ * Invokes the [[onInvalid]] callbacks, in natural order.
100
+ * Each callback's return value is checked in sequence until one of them returns an instance of TargetState.
101
+ * The results of the callbacks are wrapped in $q.when(), so the callbacks may return promises.
102
+ *
103
+ * If a callback returns an TargetState, then it is used as arguments to $state.transitionTo() and the result returned.
104
+ *
105
+ * @internal
106
+ */
107
+ _handleInvalidTargetState(fromPath, toState) {
108
+ const fromState = PathUtils.makeTargetState(
109
+ this.router.stateRegistry,
110
+ fromPath,
111
+ );
112
+ const globals = this.router.globals;
113
+ const latestThing = () => globals.transitionHistory.peekTail();
114
+ const latest = latestThing();
115
+ const callbackQueue = new Queue(this.invalidCallbacks.slice());
116
+ const injector = new ResolveContext(fromPath).injector();
117
+ const checkForRedirect = (result) => {
118
+ if (!(result instanceof TargetState)) {
119
+ return;
120
+ }
121
+ let target = result;
122
+ // Recreate the TargetState, in case the state is now defined.
123
+ target = this.target(
124
+ target.identifier(),
125
+ target.params(),
126
+ target.options(),
127
+ );
128
+ if (!target.valid()) {
129
+ return Rejection.invalid(target.error()).toPromise();
130
+ }
131
+ if (latestThing() !== latest) {
132
+ return Rejection.superseded().toPromise();
133
+ }
134
+ return this.transitionTo(
135
+ target.identifier(),
136
+ target.params(),
137
+ target.options(),
138
+ );
139
+ };
140
+ function invokeNextCallback() {
141
+ const nextCallback = callbackQueue.dequeue();
142
+ if (nextCallback === undefined)
143
+ return Rejection.invalid(toState.error()).toPromise();
144
+ const callbackResult = services.$q.when(
145
+ nextCallback(toState, fromState, injector),
146
+ );
147
+ return callbackResult
148
+ .then(checkForRedirect)
149
+ .then((result) => result || invokeNextCallback());
150
+ }
151
+ return invokeNextCallback();
152
+ }
153
+ /**
154
+ * Registers an Invalid State handler
155
+ *
156
+ * Registers a [[OnInvalidCallback]] function to be invoked when [[StateService.transitionTo]]
157
+ * has been called with an invalid state reference parameter
158
+ *
159
+ * Example:
160
+ * ```js
161
+ * stateService.onInvalid(function(to, from, injector) {
162
+ * if (to.name() === 'foo') {
163
+ * let lazyLoader = injector.get('LazyLoadService');
164
+ * return lazyLoader.load('foo')
165
+ * .then(() => stateService.target('foo'));
166
+ * }
167
+ * });
168
+ * ```
169
+ *
170
+ * @param {function} callback invoked when the toState is invalid
171
+ * This function receives the (invalid) toState, the fromState, and an injector.
172
+ * The function may optionally return a [[TargetState]] or a Promise for a TargetState.
173
+ * If one is returned, it is treated as a redirect.
174
+ *
175
+ * @returns a function which deregisters the callback
176
+ */
177
+ onInvalid(callback) {
178
+ this.invalidCallbacks.push(callback);
179
+ return function deregisterListener() {
180
+ removeFrom(this.invalidCallbacks)(callback);
181
+ }.bind(this);
182
+ }
183
+ /**
184
+ * Reloads the current state
185
+ *
186
+ * A method that force reloads the current state, or a partial state hierarchy.
187
+ * All resolves are re-resolved, and components reinstantiated.
188
+ *
189
+ * #### Example:
190
+ * ```js
191
+ * let app angular.module('app', ['ui.router']);
192
+ *
193
+ * app.controller('ctrl', function ($scope, $state) {
194
+ * $scope.reload = function(){
195
+ * $state.reload();
196
+ * }
197
+ * });
198
+ * ```
199
+ *
200
+ * Note: `reload()` is just an alias for:
201
+ *
202
+ * ```js
203
+ * $state.transitionTo($state.current, $state.params, {
204
+ * reload: true, inherit: false
205
+ * });
206
+ * ```
207
+ *
208
+ * @param reloadState A state name or a state object.
209
+ * If present, this state and all its children will be reloaded, but ancestors will not reload.
210
+ *
211
+ * #### Example:
212
+ * ```js
213
+ * //assuming app application consists of 3 states: 'contacts', 'contacts.detail', 'contacts.detail.item'
214
+ * //and current state is 'contacts.detail.item'
215
+ * let app angular.module('app', ['ui.router']);
216
+ *
217
+ * app.controller('ctrl', function ($scope, $state) {
218
+ * $scope.reload = function(){
219
+ * //will reload 'contact.detail' and nested 'contact.detail.item' states
220
+ * $state.reload('contact.detail');
221
+ * }
222
+ * });
223
+ * ```
224
+ *
225
+ * @returns A promise representing the state of the new transition. See [[StateService.go]]
226
+ */
227
+ reload(reloadState) {
228
+ return this.transitionTo(this.current, this.params, {
229
+ reload: isDefined(reloadState) ? reloadState : true,
230
+ inherit: false,
231
+ notify: false,
232
+ });
233
+ }
234
+ /**
235
+ * Transition to a different state and/or parameters
236
+ *
237
+ * Convenience method for transitioning to a new state.
238
+ *
239
+ * `$state.go` calls `$state.transitionTo` internally but automatically sets options to
240
+ * `{ location: true, inherit: true, relative: router.globals.$current, notify: true }`.
241
+ * This allows you to use either an absolute or relative `to` argument (because of `relative: router.globals.$current`).
242
+ * It also allows you to specify * only the parameters you'd like to update, while letting unspecified parameters
243
+ * inherit from the current parameter values (because of `inherit: true`).
244
+ *
245
+ * #### Example:
246
+ * ```js
247
+ * let app = angular.module('app', ['ui.router']);
248
+ *
249
+ * app.controller('ctrl', function ($scope, $state) {
250
+ * $scope.changeState = function () {
251
+ * $state.go('contact.detail');
252
+ * };
253
+ * });
254
+ * ```
255
+ *
256
+ * @param to Absolute state name, state object, or relative state path (relative to current state).
257
+ *
258
+ * Some examples:
259
+ *
260
+ * - `$state.go('contact.detail')` - will go to the `contact.detail` state
261
+ * - `$state.go('^')` - will go to the parent state
262
+ * - `$state.go('^.sibling')` - if current state is `home.child`, will go to the `home.sibling` state
263
+ * - `$state.go('.child.grandchild')` - if current state is home, will go to the `home.child.grandchild` state
264
+ *
265
+ * @param params A map of the parameters that will be sent to the state, will populate $stateParams.
266
+ *
267
+ * Any parameters that are not specified will be inherited from current parameter values (because of `inherit: true`).
268
+ * This allows, for example, going to a sibling state that shares parameters defined by a parent state.
269
+ *
270
+ * @param options Transition options
271
+ *
272
+ * @returns {promise} A promise representing the state of the new transition.
273
+ */
274
+ go(to, params, options) {
275
+ const defautGoOpts = { relative: this.$current, inherit: true };
276
+ const transOpts = defaults(options, defautGoOpts, defaultTransOpts);
277
+ return this.transitionTo(to, params, transOpts);
278
+ }
279
+ /**
280
+ * Creates a [[TargetState]]
281
+ *
282
+ * This is a factory method for creating a TargetState
283
+ *
284
+ * This may be returned from a Transition Hook to redirect a transition, for example.
285
+ */
286
+ target(identifier, params, options = {}) {
287
+ // If we're reloading, find the state object to reload from
288
+ if (isObject(options.reload) && !options.reload.name)
289
+ throw new Error("Invalid reload state object");
290
+ const reg = this.router.stateRegistry;
291
+ options.reloadState =
292
+ options.reload === true
293
+ ? reg.root()
294
+ : reg.matcher.find(options.reload, options.relative);
295
+ if (options.reload && !options.reloadState)
296
+ throw new Error(
297
+ `No such reload state '${isString(options.reload) ? options.reload : options.reload.name}'`,
298
+ );
299
+ return new TargetState(
300
+ this.router.stateRegistry,
301
+ identifier,
302
+ params,
303
+ options,
304
+ );
305
+ }
306
+ /** @internal */
307
+ getCurrentPath() {
308
+ const globals = this.router.globals;
309
+ const latestSuccess = globals.successfulTransitions.peekTail();
310
+ const rootPath = () => [new PathNode(this.router.stateRegistry.root())];
311
+ return latestSuccess ? latestSuccess.treeChanges().to : rootPath();
312
+ }
313
+ /**
314
+ * Low-level method for transitioning to a new state.
315
+ *
316
+ * The [[go]] method (which uses `transitionTo` internally) is recommended in most situations.
317
+ *
318
+ * #### Example:
319
+ * ```js
320
+ * let app = angular.module('app', ['ui.router']);
321
+ *
322
+ * app.controller('ctrl', function ($scope, $state) {
323
+ * $scope.changeState = function () {
324
+ * $state.transitionTo('contact.detail');
325
+ * };
326
+ * });
327
+ * ```
328
+ *
329
+ * @param to State name or state object.
330
+ * @param toParams A map of the parameters that will be sent to the state,
331
+ * will populate $stateParams.
332
+ * @param options Transition options
333
+ *
334
+ * @returns A promise representing the state of the new transition. See [[go]]
335
+ */
336
+ transitionTo(to, toParams = {}, options = {}) {
337
+ const router = this.router;
338
+ const globals = router.globals;
339
+ options = defaults(options, defaultTransOpts);
340
+ const getCurrent = () => globals.transition;
341
+ options = extend(options, { current: getCurrent });
342
+ const ref = this.target(to, toParams, options);
343
+ const currentPath = this.getCurrentPath();
344
+ if (!ref.exists()) return this._handleInvalidTargetState(currentPath, ref);
345
+ if (!ref.valid()) return silentRejection(ref.error());
346
+ if (options.supercede === false && getCurrent()) {
347
+ return Rejection.ignored(
348
+ "Another transition is in progress and supercede has been set to false in TransitionOptions for the transition. So the transition was ignored in favour of the existing one in progress.",
349
+ ).toPromise();
350
+ }
351
+ /**
352
+ * Special handling for Ignored, Aborted, and Redirected transitions
353
+ *
354
+ * The semantics for the transition.run() promise and the StateService.transitionTo()
355
+ * promise differ. For instance, the run() promise may be rejected because it was
356
+ * IGNORED, but the transitionTo() promise is resolved because from the user perspective
357
+ * no error occurred. Likewise, the transition.run() promise may be rejected because of
358
+ * a Redirect, but the transitionTo() promise is chained to the new Transition's promise.
359
+ */
360
+ const rejectedTransitionHandler = (trans) => (error) => {
361
+ if (error instanceof Rejection) {
362
+ const isLatest = router.globals.lastStartedTransitionId <= trans.$id;
363
+ if (error.type === RejectType.IGNORED) {
364
+ isLatest && router.urlRouter.update();
365
+ // Consider ignored `Transition.run()` as a successful `transitionTo`
366
+ return services.$q.when(globals.current);
367
+ }
368
+ const detail = error.detail;
369
+ if (
370
+ error.type === RejectType.SUPERSEDED &&
371
+ error.redirected &&
372
+ detail instanceof TargetState
373
+ ) {
374
+ // If `Transition.run()` was redirected, allow the `transitionTo()` promise to resolve successfully
375
+ // by returning the promise for the new (redirect) `Transition.run()`.
376
+ const redirect = trans.redirect(detail);
377
+ return redirect.run().catch(rejectedTransitionHandler(redirect));
378
+ }
379
+ if (error.type === RejectType.ABORTED) {
380
+ isLatest && router.urlRouter.update();
381
+ return services.$q.reject(error);
382
+ }
383
+ }
384
+ const errorHandler = this.defaultErrorHandler();
385
+ errorHandler(error);
386
+ return services.$q.reject(error);
387
+ };
388
+ const transition = this.router.transitionService.create(currentPath, ref);
389
+ const transitionToPromise = transition
390
+ .run()
391
+ .catch(rejectedTransitionHandler(transition));
392
+ silenceUncaughtInPromise(transitionToPromise); // issue #2676
393
+ // Return a promise for the transition, which also has the transition object on it.
394
+ return extend(transitionToPromise, { transition });
395
+ }
396
+ /**
397
+ * Checks if the current state *is* the provided state
398
+ *
399
+ * Similar to [[includes]] but only checks for the full state name.
400
+ * If params is supplied then it will be tested for strict equality against the current
401
+ * active params object, so all params must match with none missing and no extras.
402
+ *
403
+ * #### Example:
404
+ * ```js
405
+ * $state.$current.name = 'contacts.details.item';
406
+ *
407
+ * // absolute name
408
+ * $state.is('contact.details.item'); // returns true
409
+ * $state.is(contactDetailItemStateObject); // returns true
410
+ * ```
411
+ *
412
+ * // relative name (. and ^), typically from a template
413
+ * // E.g. from the 'contacts.details' template
414
+ * ```html
415
+ * <div ng-class="{highlighted: $state.is('.item')}">Item</div>
416
+ * ```
417
+ *
418
+ * @param stateOrName The state name (absolute or relative) or state object you'd like to check.
419
+ * @param params A param object, e.g. `{sectionId: section.id}`, that you'd like
420
+ * to test against the current active state.
421
+ * @param options An options object. The options are:
422
+ * - `relative`: If `stateOrName` is a relative state name and `options.relative` is set, .is will
423
+ * test relative to `options.relative` state (or name).
424
+ *
425
+ * @returns Returns true if it is the state.
426
+ */
427
+ is(stateOrName, params, options) {
428
+ options = defaults(options, { relative: this.$current });
429
+ const state = this.router.stateRegistry.matcher.find(
430
+ stateOrName,
431
+ options.relative,
432
+ );
433
+ if (!isDefined(state)) return undefined;
434
+ if (this.$current !== state) return false;
435
+ if (!params) return true;
436
+ const schema = state.parameters({ inherit: true, matchingKeys: params });
437
+ return Param.equals(schema, Param.values(schema, params), this.params);
438
+ }
439
+ /**
440
+ * Checks if the current state *includes* the provided state
441
+ *
442
+ * A method to determine if the current active state is equal to or is the child of the
443
+ * state stateName. If any params are passed then they will be tested for a match as well.
444
+ * Not all the parameters need to be passed, just the ones you'd like to test for equality.
445
+ *
446
+ * #### Example when `$state.$current.name === 'contacts.details.item'`
447
+ * ```js
448
+ * // Using partial names
449
+ * $state.includes("contacts"); // returns true
450
+ * $state.includes("contacts.details"); // returns true
451
+ * $state.includes("contacts.details.item"); // returns true
452
+ * $state.includes("contacts.list"); // returns false
453
+ * $state.includes("about"); // returns false
454
+ * ```
455
+ *
456
+ * #### Glob Examples when `* $state.$current.name === 'contacts.details.item.url'`:
457
+ * ```js
458
+ * $state.includes("*.details.*.*"); // returns true
459
+ * $state.includes("*.details.**"); // returns true
460
+ * $state.includes("**.item.**"); // returns true
461
+ * $state.includes("*.details.item.url"); // returns true
462
+ * $state.includes("*.details.*.url"); // returns true
463
+ * $state.includes("*.details.*"); // returns false
464
+ * $state.includes("item.**"); // returns false
465
+ * ```
466
+ *
467
+ * @param stateOrName A partial name, relative name, glob pattern,
468
+ * or state object to be searched for within the current state name.
469
+ * @param params A param object, e.g. `{sectionId: section.id}`,
470
+ * that you'd like to test against the current active state.
471
+ * @param options An options object. The options are:
472
+ * - `relative`: If `stateOrName` is a relative state name and `options.relative` is set, .is will
473
+ * test relative to `options.relative` state (or name).
474
+ *
475
+ * @returns {boolean} Returns true if it does include the state
476
+ */
477
+ includes(stateOrName, params, options) {
478
+ options = defaults(options, { relative: this.$current });
479
+ const glob = isString(stateOrName) && Glob.fromString(stateOrName);
480
+ if (glob) {
481
+ if (!glob.matches(this.$current.name)) return false;
482
+ stateOrName = this.$current.name;
483
+ }
484
+ const state = this.router.stateRegistry.matcher.find(
485
+ stateOrName,
486
+ options.relative,
487
+ ),
488
+ include = this.$current.includes;
489
+ if (!isDefined(state)) return undefined;
490
+ if (!isDefined(include[state.name])) return false;
491
+ if (!params) return true;
492
+ const schema = state.parameters({ inherit: true, matchingKeys: params });
493
+ return Param.equals(schema, Param.values(schema, params), this.params);
494
+ }
495
+ /**
496
+ * Generates a URL for a state and parameters
497
+ *
498
+ * Returns the url for the given state populated with the given params.
499
+ *
500
+ * #### Example:
501
+ * ```js
502
+ * expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
503
+ * ```
504
+ *
505
+ * @param stateOrName The state name or state object you'd like to generate a url from.
506
+ * @param params An object of parameter values to fill the state's required parameters.
507
+ * @param options Options object. The options are:
508
+ *
509
+ * @returns {string} compiled state url
510
+ */
511
+ href(stateOrName, params, options) {
512
+ const defaultHrefOpts = {
513
+ lossy: true,
514
+ inherit: true,
515
+ absolute: false,
516
+ relative: this.$current,
517
+ };
518
+ options = defaults(options, defaultHrefOpts);
519
+ params = params || {};
520
+ const state = this.router.stateRegistry.matcher.find(
521
+ stateOrName,
522
+ options.relative,
523
+ );
524
+ if (!isDefined(state)) return null;
525
+ if (options.inherit)
526
+ params = this.params.$inherit(params, this.$current, state);
527
+ const nav = state && options.lossy ? state.navigable : state;
528
+ if (!nav || nav.url === undefined || nav.url === null) {
529
+ return null;
530
+ }
531
+ return this.router.urlRouter.href(nav.url, params, {
532
+ absolute: options.absolute,
533
+ });
534
+ }
535
+ /**
536
+ * Sets or gets the default [[transitionTo]] error handler.
537
+ *
538
+ * The error handler is called when a [[Transition]] is rejected or when any error occurred during the Transition.
539
+ * This includes errors caused by resolves and transition hooks.
540
+ *
541
+ * Note:
542
+ * This handler does not receive certain Transition rejections.
543
+ * Redirected and Ignored Transitions are not considered to be errors by [[StateService.transitionTo]].
544
+ *
545
+ * The built-in default error handler logs the error to the console.
546
+ *
547
+ * You can provide your own custom handler.
548
+ *
549
+ * #### Example:
550
+ * ```js
551
+ * stateService.defaultErrorHandler(function() {
552
+ * // Do not log transitionTo errors
553
+ * });
554
+ * ```
555
+ *
556
+ * @param handler a global error handler function
557
+ * @returns the current global error handler
558
+ */
559
+ defaultErrorHandler(handler) {
560
+ return (this._defaultErrorHandler = handler || this._defaultErrorHandler);
561
+ }
562
+ get(stateOrName, base) {
563
+ const reg = this.router.stateRegistry;
564
+ if (arguments.length === 0) return reg.get();
565
+ return reg.get(stateOrName, base || this.$current);
566
+ }
567
+ /**
568
+ * Lazy loads a state
569
+ *
570
+ * Explicitly runs a state's [[StateDeclaration.lazyLoad]] function.
571
+ *
572
+ * @param stateOrName the state that should be lazy loaded
573
+ * @param transition the optional Transition context to use (if the lazyLoad function requires an injector, etc)
574
+ * Note: If no transition is provided, a noop transition is created using the from the current state to the current state.
575
+ * This noop transition is not actually run.
576
+ *
577
+ * @returns a promise to lazy load
578
+ */
579
+ lazyLoad(stateOrName, transition) {
580
+ const state = this.get(stateOrName);
581
+ if (!state || !state.lazyLoad)
582
+ throw new Error("Can not lazy load " + stateOrName);
583
+ const currentPath = this.getCurrentPath();
584
+ const target = PathUtils.makeTargetState(
585
+ this.router.stateRegistry,
586
+ currentPath,
587
+ );
588
+ transition =
589
+ transition || this.router.transitionService.create(currentPath, target);
590
+ return lazyLoadState(transition, state);
591
+ }
592
+ }