@angular-wave/angular.ts 0.0.11 → 0.0.13

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 (79) hide show
  1. package/dist/angular-ts.esm.js +1 -1
  2. package/dist/angular-ts.umd.js +1 -1
  3. package/package.json +4 -1
  4. package/src/exts/messages.md +30 -30
  5. package/src/index.js +1 -0
  6. package/src/public.js +2 -0
  7. package/src/router/adapter/directives/stateDirectives.js +695 -0
  8. package/src/router/adapter/directives/viewDirective.js +514 -0
  9. package/src/router/adapter/injectables.js +314 -0
  10. package/src/router/adapter/interface.js +1 -0
  11. package/src/router/adapter/locationServices.js +84 -0
  12. package/src/router/adapter/services.js +126 -0
  13. package/src/router/adapter/stateFilters.js +43 -0
  14. package/src/router/adapter/stateProvider.js +137 -0
  15. package/src/router/adapter/statebuilders/onEnterExitRetain.js +30 -0
  16. package/src/router/adapter/statebuilders/views.js +146 -0
  17. package/src/router/adapter/templateFactory.js +218 -0
  18. package/src/router/adapter/viewScroll.js +31 -0
  19. package/src/router/core/common/common.js +496 -0
  20. package/src/router/core/common/coreservices.js +15 -0
  21. package/src/router/core/common/glob.js +75 -0
  22. package/src/router/core/common/hof.js +194 -0
  23. package/src/router/core/common/predicates.js +44 -0
  24. package/src/router/core/common/queue.js +41 -0
  25. package/src/router/core/common/safeConsole.js +38 -0
  26. package/src/router/core/common/strings.js +141 -0
  27. package/src/router/core/common/trace.js +232 -0
  28. package/src/router/core/globals.js +29 -0
  29. package/src/router/core/hooks/coreResolvables.js +33 -0
  30. package/src/router/core/hooks/ignoredTransition.js +25 -0
  31. package/src/router/core/hooks/invalidTransition.js +14 -0
  32. package/src/router/core/hooks/lazyLoad.js +102 -0
  33. package/src/router/core/hooks/onEnterExitRetain.js +55 -0
  34. package/src/router/core/hooks/redirectTo.js +36 -0
  35. package/src/router/core/hooks/resolve.js +57 -0
  36. package/src/router/core/hooks/updateGlobals.js +30 -0
  37. package/src/router/core/hooks/url.js +25 -0
  38. package/src/router/core/hooks/views.js +39 -0
  39. package/src/router/core/interface.js +3 -0
  40. package/src/router/core/params/README.md +8 -0
  41. package/src/router/core/params/param.js +232 -0
  42. package/src/router/core/params/paramType.js +139 -0
  43. package/src/router/core/params/paramTypes.js +163 -0
  44. package/src/router/core/params/stateParams.js +35 -0
  45. package/src/router/core/path/pathNode.js +77 -0
  46. package/src/router/core/path/pathUtils.js +199 -0
  47. package/src/router/core/resolve/interface.js +10 -0
  48. package/src/router/core/resolve/resolvable.js +124 -0
  49. package/src/router/core/resolve/resolveContext.js +211 -0
  50. package/src/router/core/router.js +203 -0
  51. package/src/router/core/state/README.md +21 -0
  52. package/src/router/core/state/stateBuilder.js +332 -0
  53. package/src/router/core/state/stateMatcher.js +65 -0
  54. package/src/router/core/state/stateObject.js +117 -0
  55. package/src/router/core/state/stateQueueManager.js +89 -0
  56. package/src/router/core/state/stateRegistry.js +175 -0
  57. package/src/router/core/state/stateService.js +592 -0
  58. package/src/router/core/state/targetState.js +159 -0
  59. package/src/router/core/transition/hookBuilder.js +127 -0
  60. package/src/router/core/transition/hookRegistry.js +175 -0
  61. package/src/router/core/transition/interface.js +14 -0
  62. package/src/router/core/transition/rejectFactory.js +122 -0
  63. package/src/router/core/transition/transition.js +739 -0
  64. package/src/router/core/transition/transitionEventType.js +27 -0
  65. package/src/router/core/transition/transitionHook.js +199 -0
  66. package/src/router/core/transition/transitionService.js +311 -0
  67. package/src/router/core/url/interface.js +1 -0
  68. package/src/router/core/url/urlConfig.js +165 -0
  69. package/src/router/core/url/urlMatcher.js +548 -0
  70. package/src/router/core/url/urlMatcherFactory.js +123 -0
  71. package/src/router/core/url/urlRouter.js +115 -0
  72. package/src/router/core/url/urlRule.js +202 -0
  73. package/src/router/core/url/urlRules.js +348 -0
  74. package/src/router/core/url/urlService.js +268 -0
  75. package/src/router/core/view/interface.js +1 -0
  76. package/src/router/core/view/view.js +312 -0
  77. package/src/router/router.js +58 -0
  78. package/test/module-test.html +6 -2
  79. package/test/module-test.js +0 -0
@@ -0,0 +1,115 @@
1
+ import { stripLastPathElement } from "../common/strings";
2
+ import { UrlRuleFactory } from "./urlRule";
3
+ function appendBasePath(url, isHtml5, absolute, baseHref) {
4
+ if (baseHref === "/") return url;
5
+ if (isHtml5) return stripLastPathElement(baseHref) + url;
6
+ if (absolute) return baseHref.slice(1) + url;
7
+ return url;
8
+ }
9
+ /**
10
+ * Updates URL and responds to URL changes
11
+ *
12
+ * ### Deprecation warning:
13
+ * This class is now considered to be an internal API
14
+ * Use the [[UrlService]] instead.
15
+ * For configuring URL rules, use the [[UrlRules]] which can be found as [[UrlService.rules]].
16
+ */
17
+ export class UrlRouter {
18
+ /** @internal */
19
+ constructor(/** @internal */ router) {
20
+ this.router = router;
21
+ // Delegate these calls to [[UrlService]]
22
+ /** @deprecated use [[UrlService.sync]]*/
23
+ this.sync = (evt) => this.router.urlService.sync(evt);
24
+ /** @deprecated use [[UrlService.listen]]*/
25
+ this.listen = (enabled) => this.router.urlService.listen(enabled);
26
+ /** @deprecated use [[UrlService.deferIntercept]]*/
27
+ this.deferIntercept = (defer) =>
28
+ this.router.urlService.deferIntercept(defer);
29
+ /** @deprecated use [[UrlService.match]]*/
30
+ this.match = (urlParts) => this.router.urlService.match(urlParts);
31
+ // Delegate these calls to [[UrlRules]]
32
+ /** @deprecated use [[UrlRules.initial]]*/
33
+ this.initial = (handler) => this.router.urlService.rules.initial(handler);
34
+ /** @deprecated use [[UrlRules.otherwise]]*/
35
+ this.otherwise = (handler) =>
36
+ this.router.urlService.rules.otherwise(handler);
37
+ /** @deprecated use [[UrlRules.removeRule]]*/
38
+ this.removeRule = (rule) => this.router.urlService.rules.removeRule(rule);
39
+ /** @deprecated use [[UrlRules.rule]]*/
40
+ this.rule = (rule) => this.router.urlService.rules.rule(rule);
41
+ /** @deprecated use [[UrlRules.rules]]*/
42
+ this.rules = () => this.router.urlService.rules.rules();
43
+ /** @deprecated use [[UrlRules.sort]]*/
44
+ this.sort = (compareFn) => this.router.urlService.rules.sort(compareFn);
45
+ /** @deprecated use [[UrlRules.when]]*/
46
+ this.when = (matcher, handler, options) =>
47
+ this.router.urlService.rules.when(matcher, handler, options);
48
+ this.urlRuleFactory = new UrlRuleFactory(router);
49
+ }
50
+ /** Internal API. */
51
+ update(read) {
52
+ const $url = this.router.locationService;
53
+ if (read) {
54
+ this.location = $url.url();
55
+ return;
56
+ }
57
+ if ($url.url() === this.location) return;
58
+ $url.url(this.location, true);
59
+ }
60
+ /**
61
+ * Internal API.
62
+ *
63
+ * Pushes a new location to the browser history.
64
+ *
65
+ * @internal
66
+ * @param urlMatcher
67
+ * @param params
68
+ * @param options
69
+ */
70
+ push(urlMatcher, params, options) {
71
+ const replace = options && !!options.replace;
72
+ this.router.urlService.url(urlMatcher.format(params || {}), replace);
73
+ }
74
+ /**
75
+ * Builds and returns a URL with interpolated parameters
76
+ *
77
+ * #### Example:
78
+ * ```js
79
+ * matcher = $umf.compile("/about/:person");
80
+ * params = { person: "bob" };
81
+ * $bob = $urlRouter.href(matcher, params);
82
+ * // $bob == "/about/bob";
83
+ * ```
84
+ *
85
+ * @param urlMatcher The [[UrlMatcher]] object which is used as the template of the URL to generate.
86
+ * @param params An object of parameter values to fill the matcher's required parameters.
87
+ * @param options Options object. The options are:
88
+ *
89
+ * - **`absolute`** - {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl".
90
+ *
91
+ * @returns Returns the fully compiled URL, or `null` if `params` fail validation against `urlMatcher`
92
+ */
93
+ href(urlMatcher, params, options) {
94
+ let url = urlMatcher.format(params);
95
+ if (url == null) return null;
96
+ options = options || { absolute: false };
97
+ const cfg = this.router.urlService.config;
98
+ const isHtml5 = cfg.html5Mode();
99
+ if (!isHtml5 && url !== null) {
100
+ url = "#" + cfg.hashPrefix() + url;
101
+ }
102
+ url = appendBasePath(url, isHtml5, options.absolute, cfg.baseHref());
103
+ if (!options.absolute || !url) {
104
+ return url;
105
+ }
106
+ const slash = !isHtml5 && url ? "/" : "";
107
+ const cfgPort = cfg.port();
108
+ const port = cfgPort === 80 || cfgPort === 443 ? "" : ":" + cfgPort;
109
+ return [cfg.protocol(), "://", cfg.host(), port, slash, url].join("");
110
+ }
111
+ /** @deprecated use [[UrlService.interceptDeferred]]*/
112
+ get interceptDeferred() {
113
+ return this.router.urlService.interceptDeferred;
114
+ }
115
+ }
@@ -0,0 +1,202 @@
1
+ import { UrlMatcher } from "./urlMatcher";
2
+ import { isString, isDefined, isFunction } from "../common/predicates";
3
+ import { identity, extend } from "../common/common";
4
+ import { is, or, pattern } from "../common/hof";
5
+ import { StateObject } from "../state/stateObject";
6
+ /**
7
+ * Creates a [[UrlRule]]
8
+ *
9
+ * Creates a [[UrlRule]] from a:
10
+ *
11
+ * - `string`
12
+ * - [[UrlMatcher]]
13
+ * - `RegExp`
14
+ * - [[StateObject]]
15
+ */
16
+ export class UrlRuleFactory {
17
+ constructor(router) {
18
+ this.router = router;
19
+ }
20
+ compile(str) {
21
+ return this.router.urlMatcherFactory.compile(str);
22
+ }
23
+ create(what, handler) {
24
+ const { isState, isStateDeclaration } = StateObject;
25
+ const makeRule = pattern([
26
+ [isString, (_what) => makeRule(this.compile(_what))],
27
+ [is(UrlMatcher), (_what) => this.fromUrlMatcher(_what, handler)],
28
+ [
29
+ or(isState, isStateDeclaration),
30
+ (_what) => this.fromState(_what, this.router),
31
+ ],
32
+ [is(RegExp), (_what) => this.fromRegExp(_what, handler)],
33
+ [isFunction, (_what) => new BaseUrlRule(_what, handler)],
34
+ ]);
35
+ const rule = makeRule(what);
36
+ if (!rule) throw new Error("invalid 'what' in when()");
37
+ return rule;
38
+ }
39
+ /**
40
+ * A UrlRule which matches based on a UrlMatcher
41
+ *
42
+ * The `handler` may be either a `string`, a [[UrlRuleHandlerFn]] or another [[UrlMatcher]]
43
+ *
44
+ * ## Handler as a function
45
+ *
46
+ * If `handler` is a function, the function is invoked with:
47
+ *
48
+ * - matched parameter values ([[RawParams]] from [[UrlMatcher.exec]])
49
+ * - url: the current Url ([[UrlParts]])
50
+ * - router: the router object ([[UIRouter]])
51
+ *
52
+ * #### Example:
53
+ * ```js
54
+ * var urlMatcher = $umf.compile("/foo/:fooId/:barId");
55
+ * var rule = factory.fromUrlMatcher(urlMatcher, match => "/home/" + match.fooId + "/" + match.barId);
56
+ * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
57
+ * var result = rule.handler(match); // '/home/123/456'
58
+ * ```
59
+ *
60
+ * ## Handler as UrlMatcher
61
+ *
62
+ * If `handler` is a UrlMatcher, the handler matcher is used to create the new url.
63
+ * The `handler` UrlMatcher is formatted using the matched param from the first matcher.
64
+ * The url is replaced with the result.
65
+ *
66
+ * #### Example:
67
+ * ```js
68
+ * var urlMatcher = $umf.compile("/foo/:fooId/:barId");
69
+ * var handler = $umf.compile("/home/:fooId/:barId");
70
+ * var rule = factory.fromUrlMatcher(urlMatcher, handler);
71
+ * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
72
+ * var result = rule.handler(match); // '/home/123/456'
73
+ * ```
74
+ */
75
+ fromUrlMatcher(urlMatcher, handler) {
76
+ let _handler = handler;
77
+ if (isString(handler))
78
+ handler = this.router.urlMatcherFactory.compile(handler);
79
+ if (is(UrlMatcher)(handler)) _handler = (match) => handler.format(match);
80
+ function matchUrlParamters(url) {
81
+ const params = urlMatcher.exec(url.path, url.search, url.hash);
82
+ return urlMatcher.validates(params) && params;
83
+ }
84
+ // Prioritize URLs, lowest to highest:
85
+ // - Some optional URL parameters, but none matched
86
+ // - No optional parameters in URL
87
+ // - Some optional parameters, some matched
88
+ // - Some optional parameters, all matched
89
+ function matchPriority(params) {
90
+ const optional = urlMatcher
91
+ .parameters()
92
+ .filter((param) => param.isOptional);
93
+ if (!optional.length) return 0.000001;
94
+ const matched = optional.filter((param) => params[param.id]);
95
+ return matched.length / optional.length;
96
+ }
97
+ const details = { urlMatcher, matchPriority, type: "URLMATCHER" };
98
+ return extend(new BaseUrlRule(matchUrlParamters, _handler), details);
99
+ }
100
+ /**
101
+ * A UrlRule which matches a state by its url
102
+ *
103
+ * #### Example:
104
+ * ```js
105
+ * var rule = factory.fromState($state.get('foo'), router);
106
+ * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
107
+ * var result = rule.handler(match);
108
+ * // Starts a transition to 'foo' with params: { fooId: '123', barId: '456' }
109
+ * ```
110
+ */
111
+ fromState(stateOrDecl, router) {
112
+ const state = StateObject.isStateDeclaration(stateOrDecl)
113
+ ? stateOrDecl.$$state()
114
+ : stateOrDecl;
115
+ /**
116
+ * Handles match by transitioning to matched state
117
+ *
118
+ * First checks if the router should start a new transition.
119
+ * A new transition is not required if the current state's URL
120
+ * and the new URL are already identical
121
+ */
122
+ const handler = (match) => {
123
+ const $state = router.stateService;
124
+ const globals = router.globals;
125
+ if (
126
+ $state.href(state, match) !==
127
+ $state.href(globals.current, globals.params)
128
+ ) {
129
+ $state.transitionTo(state, match, { inherit: true, source: "url" });
130
+ }
131
+ };
132
+ const details = { state, type: "STATE" };
133
+ return extend(this.fromUrlMatcher(state.url, handler), details);
134
+ }
135
+ /**
136
+ * A UrlRule which matches based on a regular expression
137
+ *
138
+ * The `handler` may be either a [[UrlRuleHandlerFn]] or a string.
139
+ *
140
+ * ## Handler as a function
141
+ *
142
+ * If `handler` is a function, the function is invoked with:
143
+ *
144
+ * - regexp match array (from `regexp`)
145
+ * - url: the current Url ([[UrlParts]])
146
+ * - router: the router object ([[UIRouter]])
147
+ *
148
+ * #### Example:
149
+ * ```js
150
+ * var rule = factory.fromRegExp(/^\/foo\/(bar|baz)$/, match => "/home/" + match[1])
151
+ * var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]
152
+ * var result = rule.handler(match); // '/home/bar'
153
+ * ```
154
+ *
155
+ * ## Handler as string
156
+ *
157
+ * If `handler` is a string, the url is *replaced by the string* when the Rule is invoked.
158
+ * The string is first interpolated using `string.replace()` style pattern.
159
+ *
160
+ * #### Example:
161
+ * ```js
162
+ * var rule = factory.fromRegExp(/^\/foo\/(bar|baz)$/, "/home/$1")
163
+ * var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]
164
+ * var result = rule.handler(match); // '/home/bar'
165
+ * ```
166
+ */
167
+ fromRegExp(regexp, handler) {
168
+ if (regexp.global || regexp.sticky)
169
+ throw new Error("Rule RegExp must not be global or sticky");
170
+ /**
171
+ * If handler is a string, the url will be replaced by the string.
172
+ * If the string has any String.replace() style variables in it (like `$2`),
173
+ * they will be replaced by the captures from [[match]]
174
+ */
175
+ const redirectUrlTo = (match) =>
176
+ // Interpolates matched values into $1 $2, etc using a String.replace()-style pattern
177
+ handler.replace(
178
+ /\$(\$|\d{1,2})/,
179
+ (m, what) => match[what === "$" ? 0 : Number(what)],
180
+ );
181
+ const _handler = isString(handler) ? redirectUrlTo : handler;
182
+ const matchParamsFromRegexp = (url) => regexp.exec(url.path);
183
+ const details = { regexp, type: "REGEXP" };
184
+ return extend(new BaseUrlRule(matchParamsFromRegexp, _handler), details);
185
+ }
186
+ }
187
+ UrlRuleFactory.isUrlRule = (obj) =>
188
+ obj && ["type", "match", "handler"].every((key) => isDefined(obj[key]));
189
+ /**
190
+ * A base rule which calls `match`
191
+ *
192
+ * The value from the `match` function is passed through to the `handler`.
193
+ * @internal
194
+ */
195
+ export class BaseUrlRule {
196
+ constructor(match, handler) {
197
+ this.match = match;
198
+ this.type = "RAW";
199
+ this.matchPriority = (match) => 0 - this.$id;
200
+ this.handler = handler || identity;
201
+ }
202
+ }
@@ -0,0 +1,348 @@
1
+ import { TargetState } from "../state/targetState";
2
+ import { UrlMatcher } from "./urlMatcher";
3
+ import { is, val } from "../common/hof";
4
+ import { isDefined, isFunction, isString } from "../common/predicates";
5
+ import { removeFrom } from "../common/common";
6
+ import { UrlRuleFactory } from "./urlRule";
7
+ const prioritySort = (a, b) => (b.priority || 0) - (a.priority || 0);
8
+ const typeSort = (a, b) => {
9
+ const weights = { STATE: 4, URLMATCHER: 4, REGEXP: 3, RAW: 2, OTHER: 1 };
10
+ return (weights[a.type] || 0) - (weights[b.type] || 0);
11
+ };
12
+ const urlMatcherSort = (a, b) =>
13
+ !a.urlMatcher || !b.urlMatcher
14
+ ? 0
15
+ : UrlMatcher.compare(a.urlMatcher, b.urlMatcher);
16
+ const idSort = (a, b) => {
17
+ // Identically sorted STATE and URLMATCHER best rule will be chosen by `matchPriority` after each rule matches the URL
18
+ const useMatchPriority = { STATE: true, URLMATCHER: true };
19
+ const equal = useMatchPriority[a.type] && useMatchPriority[b.type];
20
+ return equal ? 0 : (a.$id || 0) - (b.$id || 0);
21
+ };
22
+ /**
23
+ * Default rule priority sorting function.
24
+ *
25
+ * Sorts rules by:
26
+ *
27
+ * - Explicit priority (set rule priority using [[UrlRules.when]])
28
+ * - Rule type (STATE: 4, URLMATCHER: 4, REGEXP: 3, RAW: 2, OTHER: 1)
29
+ * - `UrlMatcher` specificity ([[UrlMatcher.compare]]): works for STATE and URLMATCHER types to pick the most specific rule.
30
+ * - Rule registration order (for rule types other than STATE and URLMATCHER)
31
+ * - Equally sorted State and UrlMatcher rules will each match the URL.
32
+ * Then, the *best* match is chosen based on how many parameter values were matched.
33
+ */
34
+ let defaultRuleSortFn;
35
+ defaultRuleSortFn = (a, b) => {
36
+ let cmp = prioritySort(a, b);
37
+ if (cmp !== 0) return cmp;
38
+ cmp = typeSort(a, b);
39
+ if (cmp !== 0) return cmp;
40
+ cmp = urlMatcherSort(a, b);
41
+ if (cmp !== 0) return cmp;
42
+ return idSort(a, b);
43
+ };
44
+ function getHandlerFn(handler) {
45
+ if (
46
+ !isFunction(handler) &&
47
+ !isString(handler) &&
48
+ !is(TargetState)(handler) &&
49
+ !TargetState.isDef(handler)
50
+ ) {
51
+ throw new Error(
52
+ "'handler' must be a string, function, TargetState, or have a state: 'newtarget' property",
53
+ );
54
+ }
55
+ return isFunction(handler) ? handler : val(handler);
56
+ }
57
+ /**
58
+ * API for managing URL rules
59
+ *
60
+ * This API is used to create and manage URL rules.
61
+ * URL rules are a mechanism to respond to specific URL patterns.
62
+ *
63
+ * The most commonly used methods are [[otherwise]] and [[when]].
64
+ *
65
+ * This API is found at `router.urlService.rules` (see: [[UIRouter.urlService]], [[URLService.rules]])
66
+ */
67
+ export class UrlRules {
68
+ /** @internal */
69
+ constructor(/** @internal */ router) {
70
+ this.router = router;
71
+ /** @internal */ this._sortFn = defaultRuleSortFn;
72
+ /** @internal */ this._rules = [];
73
+ /** @internal */ this._id = 0;
74
+ this.urlRuleFactory = new UrlRuleFactory(router);
75
+ }
76
+ /** @internal */
77
+ dispose(router) {
78
+ this._rules = [];
79
+ delete this._otherwiseFn;
80
+ }
81
+ /**
82
+ * Defines the initial state, path, or behavior to use when the app starts.
83
+ *
84
+ * This rule defines the initial/starting state for the application.
85
+ *
86
+ * This rule is triggered the first time the URL is checked (when the app initially loads).
87
+ * The rule is triggered only when the url matches either `""` or `"/"`.
88
+ *
89
+ * Note: The rule is intended to be used when the root of the application is directly linked to.
90
+ * When the URL is *not* `""` or `"/"` and doesn't match other rules, the [[otherwise]] rule is triggered.
91
+ * This allows 404-like behavior when an unknown URL is deep-linked.
92
+ *
93
+ * #### Example:
94
+ * Start app at `home` state.
95
+ * ```js
96
+ * .initial({ state: 'home' });
97
+ * ```
98
+ *
99
+ * #### Example:
100
+ * Start app at `/home` (by url)
101
+ * ```js
102
+ * .initial('/home');
103
+ * ```
104
+ *
105
+ * #### Example:
106
+ * When no other url rule matches, go to `home` state
107
+ * ```js
108
+ * .initial((matchValue, url, router) => {
109
+ * console.log('initial state');
110
+ * return { state: 'home' };
111
+ * })
112
+ * ```
113
+ *
114
+ * @param handler The initial state or url path, or a function which returns the state or url path (or performs custom logic).
115
+ */
116
+ initial(handler) {
117
+ const handlerFn = getHandlerFn(handler);
118
+ const matchFn = (urlParts, router) =>
119
+ router.globals.transitionHistory.size() === 0 &&
120
+ !!/^\/?$/.exec(urlParts.path);
121
+ this.rule(this.urlRuleFactory.create(matchFn, handlerFn));
122
+ }
123
+ /**
124
+ * Defines the state, url, or behavior to use when no other rule matches the URL.
125
+ *
126
+ * This rule is matched when *no other rule* matches.
127
+ * It is generally used to handle unknown URLs (similar to "404" behavior, but on the client side).
128
+ *
129
+ * - If `handler` a string, it is treated as a url redirect
130
+ *
131
+ * #### Example:
132
+ * When no other url rule matches, redirect to `/index`
133
+ * ```js
134
+ * .otherwise('/index');
135
+ * ```
136
+ *
137
+ * - If `handler` is an object with a `state` property, the state is activated.
138
+ *
139
+ * #### Example:
140
+ * When no other url rule matches, redirect to `home` and provide a `dashboard` parameter value.
141
+ * ```js
142
+ * .otherwise({ state: 'home', params: { dashboard: 'default' } });
143
+ * ```
144
+ *
145
+ * - If `handler` is a function, the function receives the current url ([[UrlParts]]) and the [[UIRouter]] object.
146
+ * The function can perform actions, and/or return a value.
147
+ *
148
+ * #### Example:
149
+ * When no other url rule matches, manually trigger a transition to the `home` state
150
+ * ```js
151
+ * .otherwise((matchValue, urlParts, router) => {
152
+ * router.stateService.go('home');
153
+ * });
154
+ * ```
155
+ *
156
+ * #### Example:
157
+ * When no other url rule matches, go to `home` state
158
+ * ```js
159
+ * .otherwise((matchValue, urlParts, router) => {
160
+ * return { state: 'home' };
161
+ * });
162
+ * ```
163
+ *
164
+ * @param handler The url path to redirect to, or a function which returns the url path (or performs custom logic).
165
+ */
166
+ otherwise(handler) {
167
+ const handlerFn = getHandlerFn(handler);
168
+ this._otherwiseFn = this.urlRuleFactory.create(val(true), handlerFn);
169
+ this._sorted = false;
170
+ }
171
+ /**
172
+ * Remove a rule previously registered
173
+ *
174
+ * @param rule the matcher rule that was previously registered using [[rule]]
175
+ */
176
+ removeRule(rule) {
177
+ removeFrom(this._rules, rule);
178
+ }
179
+ /**
180
+ * Manually adds a URL Rule.
181
+ *
182
+ * Usually, a url rule is added using [[StateDeclaration.url]] or [[when]].
183
+ * This api can be used directly for more control (to register a [[BaseUrlRule]], for example).
184
+ * Rules can be created using [[urlRuleFactory]], or created manually as simple objects.
185
+ *
186
+ * A rule should have a `match` function which returns truthy if the rule matched.
187
+ * It should also have a `handler` function which is invoked if the rule is the best match.
188
+ *
189
+ * @return a function that deregisters the rule
190
+ */
191
+ rule(rule) {
192
+ if (!UrlRuleFactory.isUrlRule(rule)) throw new Error("invalid rule");
193
+ rule.$id = this._id++;
194
+ rule.priority = rule.priority || 0;
195
+ this._rules.push(rule);
196
+ this._sorted = false;
197
+ return () => this.removeRule(rule);
198
+ }
199
+ /**
200
+ * Gets all registered rules
201
+ *
202
+ * @returns an array of all the registered rules
203
+ */
204
+ rules() {
205
+ this.ensureSorted();
206
+ return this._rules.concat(this._otherwiseFn ? [this._otherwiseFn] : []);
207
+ }
208
+ /**
209
+ * Defines URL Rule priorities
210
+ *
211
+ * More than one rule ([[UrlRule]]) might match a given URL.
212
+ * This `compareFn` is used to sort the rules by priority.
213
+ * Higher priority rules should sort earlier.
214
+ *
215
+ * The [[defaultRuleSortFn]] is used by default.
216
+ *
217
+ * You only need to call this function once.
218
+ * The `compareFn` will be used to sort the rules as each is registered.
219
+ *
220
+ * If called without any parameter, it will re-sort the rules.
221
+ *
222
+ * ---
223
+ *
224
+ * Url rules may come from multiple sources: states's urls ([[StateDeclaration.url]]), [[when]], and [[rule]].
225
+ * Each rule has a (user-provided) [[UrlRule.priority]], a [[UrlRule.type]], and a [[UrlRule.$id]]
226
+ * The `$id` is is the order in which the rule was registered.
227
+ *
228
+ * The sort function should use these data, or data found on a specific type
229
+ * of [[UrlRule]] (such as [[StateRule.state]]), to order the rules as desired.
230
+ *
231
+ * #### Example:
232
+ * This compare function prioritizes rules by the order in which the rules were registered.
233
+ * A rule registered earlier has higher priority.
234
+ *
235
+ * ```js
236
+ * function compareFn(a, b) {
237
+ * return a.$id - b.$id;
238
+ * }
239
+ * ```
240
+ *
241
+ * @param compareFn a function that compares to [[UrlRule]] objects.
242
+ * The `compareFn` should abide by the `Array.sort` compare function rules.
243
+ * Given two rules, `a` and `b`, return a negative number if `a` should be higher priority.
244
+ * Return a positive number if `b` should be higher priority.
245
+ * Return `0` if the rules are identical.
246
+ *
247
+ * See the [mozilla reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description)
248
+ * for details.
249
+ */
250
+ sort(compareFn) {
251
+ const sorted = this.stableSort(
252
+ this._rules,
253
+ (this._sortFn = compareFn || this._sortFn),
254
+ );
255
+ // precompute _sortGroup values and apply to each rule
256
+ let group = 0;
257
+ for (let i = 0; i < sorted.length; i++) {
258
+ sorted[i]._group = group;
259
+ if (
260
+ i < sorted.length - 1 &&
261
+ this._sortFn(sorted[i], sorted[i + 1]) !== 0
262
+ ) {
263
+ group++;
264
+ }
265
+ }
266
+ this._rules = sorted;
267
+ this._sorted = true;
268
+ }
269
+ /** @internal */
270
+ ensureSorted() {
271
+ this._sorted || this.sort();
272
+ }
273
+ /** @internal */
274
+ stableSort(arr, compareFn) {
275
+ const arrOfWrapper = arr.map((elem, idx) => ({ elem, idx }));
276
+ arrOfWrapper.sort((wrapperA, wrapperB) => {
277
+ const cmpDiff = compareFn(wrapperA.elem, wrapperB.elem);
278
+ return cmpDiff === 0 ? wrapperA.idx - wrapperB.idx : cmpDiff;
279
+ });
280
+ return arrOfWrapper.map((wrapper) => wrapper.elem);
281
+ }
282
+ /**
283
+ * Registers a `matcher` and `handler` for custom URLs handling.
284
+ *
285
+ * The `matcher` can be:
286
+ *
287
+ * - a [[UrlMatcher]]: See: [[UrlMatcherFactory.compile]]
288
+ * - a `string`: The string is compiled to a [[UrlMatcher]]
289
+ * - a `RegExp`: The regexp is used to match the url.
290
+ *
291
+ * The `handler` can be:
292
+ *
293
+ * - a string: The url is redirected to the value of the string.
294
+ * - a function: The url is redirected to the return value of the function.
295
+ *
296
+ * ---
297
+ *
298
+ * When the `handler` is a `string` and the `matcher` is a `UrlMatcher` (or string), the redirect
299
+ * string is interpolated with parameter values.
300
+ *
301
+ * #### Example:
302
+ * When the URL is `/foo/123` the rule will redirect to `/bar/123`.
303
+ * ```js
304
+ * .when("/foo/:param1", "/bar/:param1")
305
+ * ```
306
+ *
307
+ * ---
308
+ *
309
+ * When the `handler` is a string and the `matcher` is a `RegExp`, the redirect string is
310
+ * interpolated with capture groups from the RegExp.
311
+ *
312
+ * #### Example:
313
+ * When the URL is `/foo/123` the rule will redirect to `/bar/123`.
314
+ * ```js
315
+ * .when(new RegExp("^/foo/(.*)$"), "/bar/$1");
316
+ * ```
317
+ *
318
+ * ---
319
+ *
320
+ * When the handler is a function, it receives the matched value, the current URL, and the `UIRouter` object (See [[UrlRuleHandlerFn]]).
321
+ * The "matched value" differs based on the `matcher`.
322
+ * For [[UrlMatcher]]s, it will be the matched state params.
323
+ * For `RegExp`, it will be the match array from `regexp.exec()`.
324
+ *
325
+ * If the handler returns a string, the URL is redirected to the string.
326
+ *
327
+ * #### Example:
328
+ * When the URL is `/foo/123` the rule will redirect to `/bar/123`.
329
+ * ```js
330
+ * .when(new RegExp("^/foo/(.*)$"), match => "/bar/" + match[1]);
331
+ * ```
332
+ *
333
+ * Note: the `handler` may also invoke arbitrary code, such as `$state.go()`
334
+ *
335
+ * @param matcher A pattern `string` to match, compiled as a [[UrlMatcher]], or a `RegExp`.
336
+ * @param handler The path to redirect to, or a function that returns the path.
337
+ * @param options `{ priority: number }`
338
+ *
339
+ * @return the registered [[UrlRule]]
340
+ */
341
+ when(matcher, handler, options) {
342
+ const rule = this.urlRuleFactory.create(matcher, handler);
343
+ if (isDefined(options && options.priority))
344
+ rule.priority = options.priority;
345
+ this.rule(rule);
346
+ return rule;
347
+ }
348
+ }