@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.
- package/dist/angular-ts.esm.js +1 -1
- package/dist/angular-ts.umd.js +1 -1
- package/package.json +4 -1
- package/src/exts/messages.md +30 -30
- package/src/index.js +1 -0
- package/src/public.js +2 -0
- package/src/router/adapter/directives/stateDirectives.js +695 -0
- package/src/router/adapter/directives/viewDirective.js +514 -0
- package/src/router/adapter/injectables.js +314 -0
- package/src/router/adapter/interface.js +1 -0
- package/src/router/adapter/locationServices.js +84 -0
- package/src/router/adapter/services.js +126 -0
- package/src/router/adapter/stateFilters.js +43 -0
- package/src/router/adapter/stateProvider.js +137 -0
- package/src/router/adapter/statebuilders/onEnterExitRetain.js +30 -0
- package/src/router/adapter/statebuilders/views.js +146 -0
- package/src/router/adapter/templateFactory.js +218 -0
- package/src/router/adapter/viewScroll.js +31 -0
- package/src/router/core/common/common.js +496 -0
- package/src/router/core/common/coreservices.js +15 -0
- package/src/router/core/common/glob.js +75 -0
- package/src/router/core/common/hof.js +194 -0
- package/src/router/core/common/predicates.js +44 -0
- package/src/router/core/common/queue.js +41 -0
- package/src/router/core/common/safeConsole.js +38 -0
- package/src/router/core/common/strings.js +141 -0
- package/src/router/core/common/trace.js +232 -0
- package/src/router/core/globals.js +29 -0
- package/src/router/core/hooks/coreResolvables.js +33 -0
- package/src/router/core/hooks/ignoredTransition.js +25 -0
- package/src/router/core/hooks/invalidTransition.js +14 -0
- package/src/router/core/hooks/lazyLoad.js +102 -0
- package/src/router/core/hooks/onEnterExitRetain.js +55 -0
- package/src/router/core/hooks/redirectTo.js +36 -0
- package/src/router/core/hooks/resolve.js +57 -0
- package/src/router/core/hooks/updateGlobals.js +30 -0
- package/src/router/core/hooks/url.js +25 -0
- package/src/router/core/hooks/views.js +39 -0
- package/src/router/core/interface.js +3 -0
- package/src/router/core/params/README.md +8 -0
- package/src/router/core/params/param.js +232 -0
- package/src/router/core/params/paramType.js +139 -0
- package/src/router/core/params/paramTypes.js +163 -0
- package/src/router/core/params/stateParams.js +35 -0
- package/src/router/core/path/pathNode.js +77 -0
- package/src/router/core/path/pathUtils.js +199 -0
- package/src/router/core/resolve/interface.js +10 -0
- package/src/router/core/resolve/resolvable.js +124 -0
- package/src/router/core/resolve/resolveContext.js +211 -0
- package/src/router/core/router.js +203 -0
- package/src/router/core/state/README.md +21 -0
- package/src/router/core/state/stateBuilder.js +332 -0
- package/src/router/core/state/stateMatcher.js +65 -0
- package/src/router/core/state/stateObject.js +117 -0
- package/src/router/core/state/stateQueueManager.js +89 -0
- package/src/router/core/state/stateRegistry.js +175 -0
- package/src/router/core/state/stateService.js +592 -0
- package/src/router/core/state/targetState.js +159 -0
- package/src/router/core/transition/hookBuilder.js +127 -0
- package/src/router/core/transition/hookRegistry.js +175 -0
- package/src/router/core/transition/interface.js +14 -0
- package/src/router/core/transition/rejectFactory.js +122 -0
- package/src/router/core/transition/transition.js +739 -0
- package/src/router/core/transition/transitionEventType.js +27 -0
- package/src/router/core/transition/transitionHook.js +199 -0
- package/src/router/core/transition/transitionService.js +311 -0
- package/src/router/core/url/interface.js +1 -0
- package/src/router/core/url/urlConfig.js +165 -0
- package/src/router/core/url/urlMatcher.js +548 -0
- package/src/router/core/url/urlMatcherFactory.js +123 -0
- package/src/router/core/url/urlRouter.js +115 -0
- package/src/router/core/url/urlRule.js +202 -0
- package/src/router/core/url/urlRules.js +348 -0
- package/src/router/core/url/urlService.js +268 -0
- package/src/router/core/view/interface.js +1 -0
- package/src/router/core/view/view.js +312 -0
- package/src/router/router.js +58 -0
- package/test/module-test.html +6 -2
- package/test/module-test.js +0 -0
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
import {
|
|
2
|
+
map,
|
|
3
|
+
inherit,
|
|
4
|
+
identity,
|
|
5
|
+
unnest,
|
|
6
|
+
tail,
|
|
7
|
+
find,
|
|
8
|
+
allTrueR,
|
|
9
|
+
unnestR,
|
|
10
|
+
arrayTuples,
|
|
11
|
+
defaults,
|
|
12
|
+
} from "../common/common";
|
|
13
|
+
import { prop, propEq } from "../common/hof";
|
|
14
|
+
import { isArray, isString, isDefined } from "../common/predicates";
|
|
15
|
+
import { Param, DefType } from "../params/param";
|
|
16
|
+
import { joinNeighborsR, splitOnDelim } from "../common/strings";
|
|
17
|
+
function quoteRegExp(str, param) {
|
|
18
|
+
let surroundPattern = ["", ""],
|
|
19
|
+
result = str.replace(/[\\\[\]\^$*+?.()|{}]/g, "\\$&");
|
|
20
|
+
if (!param) return result;
|
|
21
|
+
switch (param.squash) {
|
|
22
|
+
case false:
|
|
23
|
+
surroundPattern = ["(", ")" + (param.isOptional ? "?" : "")];
|
|
24
|
+
break;
|
|
25
|
+
case true:
|
|
26
|
+
result = result.replace(/\/$/, "");
|
|
27
|
+
surroundPattern = ["(?:/(", ")|/)?"];
|
|
28
|
+
break;
|
|
29
|
+
default:
|
|
30
|
+
surroundPattern = [`(${param.squash}|`, ")?"];
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
return (
|
|
34
|
+
result + surroundPattern[0] + param.type.pattern.source + surroundPattern[1]
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
const memoizeTo = (obj, _prop, fn) => (obj[_prop] = obj[_prop] || fn());
|
|
38
|
+
const splitOnSlash = splitOnDelim("/");
|
|
39
|
+
const defaultConfig = {
|
|
40
|
+
state: { params: {} },
|
|
41
|
+
strict: true,
|
|
42
|
+
caseInsensitive: true,
|
|
43
|
+
decodeParams: true,
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Matches URLs against patterns.
|
|
47
|
+
*
|
|
48
|
+
* Matches URLs against patterns and extracts named parameters from the path or the search
|
|
49
|
+
* part of the URL.
|
|
50
|
+
*
|
|
51
|
+
* A URL pattern consists of a path pattern, optionally followed by '?' and a list of search (query)
|
|
52
|
+
* parameters. Multiple search parameter names are separated by '&'. Search parameters
|
|
53
|
+
* do not influence whether or not a URL is matched, but their values are passed through into
|
|
54
|
+
* the matched parameters returned by [[UrlMatcher.exec]].
|
|
55
|
+
*
|
|
56
|
+
* - *Path parameters* are defined using curly brace placeholders (`/somepath/{param}`)
|
|
57
|
+
* or colon placeholders (`/somePath/:param`).
|
|
58
|
+
*
|
|
59
|
+
* - *A parameter RegExp* may be defined for a param after a colon
|
|
60
|
+
* (`/somePath/{param:[a-zA-Z0-9]+}`) in a curly brace placeholder.
|
|
61
|
+
* The regexp must match for the url to be matched.
|
|
62
|
+
* Should the regexp itself contain curly braces, they must be in matched pairs or escaped with a backslash.
|
|
63
|
+
*
|
|
64
|
+
* Note: a RegExp parameter will encode its value using either [[ParamTypes.path]] or [[ParamTypes.query]].
|
|
65
|
+
*
|
|
66
|
+
* - *Custom parameter types* may also be specified after a colon (`/somePath/{param:int}`) in curly brace parameters.
|
|
67
|
+
* See [[UrlMatcherFactory.type]] for more information.
|
|
68
|
+
*
|
|
69
|
+
* - *Catch-all parameters* are defined using an asterisk placeholder (`/somepath/*catchallparam`).
|
|
70
|
+
* A catch-all * parameter value will contain the remainder of the URL.
|
|
71
|
+
*
|
|
72
|
+
* ---
|
|
73
|
+
*
|
|
74
|
+
* Parameter names may contain only word characters (latin letters, digits, and underscore) and
|
|
75
|
+
* must be unique within the pattern (across both path and search parameters).
|
|
76
|
+
* A path parameter matches any number of characters other than '/'. For catch-all
|
|
77
|
+
* placeholders the path parameter matches any number of characters.
|
|
78
|
+
*
|
|
79
|
+
* Examples:
|
|
80
|
+
*
|
|
81
|
+
* * `'/hello/'` - Matches only if the path is exactly '/hello/'. There is no special treatment for
|
|
82
|
+
* trailing slashes, and patterns have to match the entire path, not just a prefix.
|
|
83
|
+
* * `'/user/:id'` - Matches '/user/bob' or '/user/1234!!!' or even '/user/' but not '/user' or
|
|
84
|
+
* '/user/bob/details'. The second path segment will be captured as the parameter 'id'.
|
|
85
|
+
* * `'/user/{id}'` - Same as the previous example, but using curly brace syntax.
|
|
86
|
+
* * `'/user/{id:[^/]*}'` - Same as the previous example.
|
|
87
|
+
* * `'/user/{id:[0-9a-fA-F]{1,8}}'` - Similar to the previous example, but only matches if the id
|
|
88
|
+
* parameter consists of 1 to 8 hex digits.
|
|
89
|
+
* * `'/files/{path:.*}'` - Matches any URL starting with '/files/' and captures the rest of the
|
|
90
|
+
* path into the parameter 'path'.
|
|
91
|
+
* * `'/files/*path'` - ditto.
|
|
92
|
+
* * `'/calendar/{start:date}'` - Matches "/calendar/2014-11-12" (because the pattern defined
|
|
93
|
+
* in the built-in `date` ParamType matches `2014-11-12`) and provides a Date object in $stateParams.start
|
|
94
|
+
*
|
|
95
|
+
*/
|
|
96
|
+
export class UrlMatcher {
|
|
97
|
+
/** @internal */
|
|
98
|
+
static encodeDashes(str) {
|
|
99
|
+
// Replace dashes with encoded "\-"
|
|
100
|
+
return encodeURIComponent(str).replace(
|
|
101
|
+
/-/g,
|
|
102
|
+
(c) => `%5C%${c.charCodeAt(0).toString(16).toUpperCase()}`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
/** @internal Given a matcher, return an array with the matcher's path segments and path params, in order */
|
|
106
|
+
static pathSegmentsAndParams(matcher) {
|
|
107
|
+
const staticSegments = matcher._segments;
|
|
108
|
+
const pathParams = matcher._params.filter(
|
|
109
|
+
(p) => p.location === DefType.PATH,
|
|
110
|
+
);
|
|
111
|
+
return arrayTuples(staticSegments, pathParams.concat(undefined))
|
|
112
|
+
.reduce(unnestR, [])
|
|
113
|
+
.filter((x) => x !== "" && isDefined(x));
|
|
114
|
+
}
|
|
115
|
+
/** @internal Given a matcher, return an array with the matcher's query params */
|
|
116
|
+
static queryParams(matcher) {
|
|
117
|
+
return matcher._params.filter((p) => p.location === DefType.SEARCH);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Compare two UrlMatchers
|
|
121
|
+
*
|
|
122
|
+
* This comparison function converts a UrlMatcher into static and dynamic path segments.
|
|
123
|
+
* Each static path segment is a static string between a path separator (slash character).
|
|
124
|
+
* Each dynamic segment is a path parameter.
|
|
125
|
+
*
|
|
126
|
+
* The comparison function sorts static segments before dynamic ones.
|
|
127
|
+
*/
|
|
128
|
+
static compare(a, b) {
|
|
129
|
+
/**
|
|
130
|
+
* Turn a UrlMatcher and all its parent matchers into an array
|
|
131
|
+
* of slash literals '/', string literals, and Param objects
|
|
132
|
+
*
|
|
133
|
+
* This example matcher matches strings like "/foo/:param/tail":
|
|
134
|
+
* var matcher = $umf.compile("/foo").append($umf.compile("/:param")).append($umf.compile("/")).append($umf.compile("tail"));
|
|
135
|
+
* var result = segments(matcher); // [ '/', 'foo', '/', Param, '/', 'tail' ]
|
|
136
|
+
*
|
|
137
|
+
* Caches the result as `matcher._cache.segments`
|
|
138
|
+
*/
|
|
139
|
+
const segments = (matcher) =>
|
|
140
|
+
(matcher._cache.segments =
|
|
141
|
+
matcher._cache.segments ||
|
|
142
|
+
matcher._cache.path
|
|
143
|
+
.map(UrlMatcher.pathSegmentsAndParams)
|
|
144
|
+
.reduce(unnestR, [])
|
|
145
|
+
.reduce(joinNeighborsR, [])
|
|
146
|
+
.map((x) => (isString(x) ? splitOnSlash(x) : x))
|
|
147
|
+
.reduce(unnestR, []));
|
|
148
|
+
/**
|
|
149
|
+
* Gets the sort weight for each segment of a UrlMatcher
|
|
150
|
+
*
|
|
151
|
+
* Caches the result as `matcher._cache.weights`
|
|
152
|
+
*/
|
|
153
|
+
const weights = (matcher) =>
|
|
154
|
+
(matcher._cache.weights =
|
|
155
|
+
matcher._cache.weights ||
|
|
156
|
+
segments(matcher).map((segment) => {
|
|
157
|
+
// Sort slashes first, then static strings, the Params
|
|
158
|
+
if (segment === "/") return 1;
|
|
159
|
+
if (isString(segment)) return 2;
|
|
160
|
+
if (segment instanceof Param) return 3;
|
|
161
|
+
}));
|
|
162
|
+
/**
|
|
163
|
+
* Pads shorter array in-place (mutates)
|
|
164
|
+
*/
|
|
165
|
+
const padArrays = (l, r, padVal) => {
|
|
166
|
+
const len = Math.max(l.length, r.length);
|
|
167
|
+
while (l.length < len) l.push(padVal);
|
|
168
|
+
while (r.length < len) r.push(padVal);
|
|
169
|
+
};
|
|
170
|
+
const weightsA = weights(a),
|
|
171
|
+
weightsB = weights(b);
|
|
172
|
+
padArrays(weightsA, weightsB, 0);
|
|
173
|
+
const _pairs = arrayTuples(weightsA, weightsB);
|
|
174
|
+
let cmp, i;
|
|
175
|
+
for (i = 0; i < _pairs.length; i++) {
|
|
176
|
+
cmp = _pairs[i][0] - _pairs[i][1];
|
|
177
|
+
if (cmp !== 0) return cmp;
|
|
178
|
+
}
|
|
179
|
+
return 0;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* @param pattern The pattern to compile into a matcher.
|
|
183
|
+
* @param paramTypes The [[ParamTypes]] registry
|
|
184
|
+
* @param paramFactory A [[ParamFactory]] object
|
|
185
|
+
* @param config A [[UrlMatcherCompileConfig]] configuration object
|
|
186
|
+
*/
|
|
187
|
+
constructor(pattern, paramTypes, paramFactory, config) {
|
|
188
|
+
/** @internal */
|
|
189
|
+
this._cache = { path: [this] };
|
|
190
|
+
/** @internal */
|
|
191
|
+
this._children = [];
|
|
192
|
+
/** @internal */
|
|
193
|
+
this._params = [];
|
|
194
|
+
/** @internal */
|
|
195
|
+
this._segments = [];
|
|
196
|
+
/** @internal */
|
|
197
|
+
this._compiled = [];
|
|
198
|
+
this.config = config = defaults(config, defaultConfig);
|
|
199
|
+
this.pattern = pattern;
|
|
200
|
+
// Find all placeholders and create a compiled pattern, using either classic or curly syntax:
|
|
201
|
+
// '*' name
|
|
202
|
+
// ':' name
|
|
203
|
+
// '{' name '}'
|
|
204
|
+
// '{' name ':' regexp '}'
|
|
205
|
+
// The regular expression is somewhat complicated due to the need to allow curly braces
|
|
206
|
+
// inside the regular expression. The placeholder regexp breaks down as follows:
|
|
207
|
+
// ([:*])([\w\[\]]+) - classic placeholder ($1 / $2) (search version has - for snake-case)
|
|
208
|
+
// \{([\w\[\]]+)(?:\:\s*( ... ))?\} - curly brace placeholder ($3) with optional regexp/type ... ($4) (search version has - for snake-case
|
|
209
|
+
// (?: ... | ... | ... )+ - the regexp consists of any number of atoms, an atom being either
|
|
210
|
+
// [^{}\\]+ - anything other than curly braces or backslash
|
|
211
|
+
// \\. - a backslash escape
|
|
212
|
+
// \{(?:[^{}\\]+|\\.)*\} - a matched set of curly braces containing other atoms
|
|
213
|
+
const placeholder =
|
|
214
|
+
/([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g;
|
|
215
|
+
const searchPlaceholder =
|
|
216
|
+
/([:]?)([\w\[\].-]+)|\{([\w\[\].-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g;
|
|
217
|
+
const patterns = [];
|
|
218
|
+
let last = 0;
|
|
219
|
+
let matchArray;
|
|
220
|
+
const checkParamErrors = (id) => {
|
|
221
|
+
if (!UrlMatcher.nameValidator.test(id))
|
|
222
|
+
throw new Error(
|
|
223
|
+
`Invalid parameter name '${id}' in pattern '${pattern}'`,
|
|
224
|
+
);
|
|
225
|
+
if (find(this._params, propEq("id", id)))
|
|
226
|
+
throw new Error(
|
|
227
|
+
`Duplicate parameter name '${id}' in pattern '${pattern}'`,
|
|
228
|
+
);
|
|
229
|
+
};
|
|
230
|
+
// Split into static segments separated by path parameter placeholders.
|
|
231
|
+
// The number of segments is always 1 more than the number of parameters.
|
|
232
|
+
const matchDetails = (m, isSearch) => {
|
|
233
|
+
// IE[78] returns '' for unmatched groups instead of null
|
|
234
|
+
const id = m[2] || m[3];
|
|
235
|
+
const regexp = isSearch
|
|
236
|
+
? m[4]
|
|
237
|
+
: m[4] || (m[1] === "*" ? "[\\s\\S]*" : null);
|
|
238
|
+
const makeRegexpType = (str) =>
|
|
239
|
+
inherit(paramTypes.type(isSearch ? "query" : "path"), {
|
|
240
|
+
pattern: new RegExp(
|
|
241
|
+
str,
|
|
242
|
+
this.config.caseInsensitive ? "i" : undefined,
|
|
243
|
+
),
|
|
244
|
+
});
|
|
245
|
+
return {
|
|
246
|
+
id,
|
|
247
|
+
regexp,
|
|
248
|
+
segment: pattern.substring(last, m.index),
|
|
249
|
+
type: !regexp
|
|
250
|
+
? null
|
|
251
|
+
: paramTypes.type(regexp) || makeRegexpType(regexp),
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
let details;
|
|
255
|
+
let segment;
|
|
256
|
+
// tslint:disable-next-line:no-conditional-assignment
|
|
257
|
+
while ((matchArray = placeholder.exec(pattern))) {
|
|
258
|
+
details = matchDetails(matchArray, false);
|
|
259
|
+
if (details.segment.indexOf("?") >= 0) break; // we're into the search part
|
|
260
|
+
checkParamErrors(details.id);
|
|
261
|
+
this._params.push(
|
|
262
|
+
paramFactory.fromPath(details.id, details.type, config.state),
|
|
263
|
+
);
|
|
264
|
+
this._segments.push(details.segment);
|
|
265
|
+
patterns.push([details.segment, tail(this._params)]);
|
|
266
|
+
last = placeholder.lastIndex;
|
|
267
|
+
}
|
|
268
|
+
segment = pattern.substring(last);
|
|
269
|
+
// Find any search parameter names and remove them from the last segment
|
|
270
|
+
const i = segment.indexOf("?");
|
|
271
|
+
if (i >= 0) {
|
|
272
|
+
const search = segment.substring(i);
|
|
273
|
+
segment = segment.substring(0, i);
|
|
274
|
+
if (search.length > 0) {
|
|
275
|
+
last = 0;
|
|
276
|
+
// tslint:disable-next-line:no-conditional-assignment
|
|
277
|
+
while ((matchArray = searchPlaceholder.exec(search))) {
|
|
278
|
+
details = matchDetails(matchArray, true);
|
|
279
|
+
checkParamErrors(details.id);
|
|
280
|
+
this._params.push(
|
|
281
|
+
paramFactory.fromSearch(details.id, details.type, config.state),
|
|
282
|
+
);
|
|
283
|
+
last = placeholder.lastIndex;
|
|
284
|
+
// check if ?&
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
this._segments.push(segment);
|
|
289
|
+
this._compiled = patterns
|
|
290
|
+
.map((_pattern) => quoteRegExp.apply(null, _pattern))
|
|
291
|
+
.concat(quoteRegExp(segment));
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Creates a new concatenated UrlMatcher
|
|
295
|
+
*
|
|
296
|
+
* Builds a new UrlMatcher by appending another UrlMatcher to this one.
|
|
297
|
+
*
|
|
298
|
+
* @param url A `UrlMatcher` instance to append as a child of the current `UrlMatcher`.
|
|
299
|
+
*/
|
|
300
|
+
append(url) {
|
|
301
|
+
this._children.push(url);
|
|
302
|
+
url._cache = {
|
|
303
|
+
path: this._cache.path.concat(url),
|
|
304
|
+
parent: this,
|
|
305
|
+
pattern: null,
|
|
306
|
+
};
|
|
307
|
+
return url;
|
|
308
|
+
}
|
|
309
|
+
/** @internal */
|
|
310
|
+
isRoot() {
|
|
311
|
+
return this._cache.path[0] === this;
|
|
312
|
+
}
|
|
313
|
+
/** Returns the input pattern string */
|
|
314
|
+
toString() {
|
|
315
|
+
return this.pattern;
|
|
316
|
+
}
|
|
317
|
+
_getDecodedParamValue(value, param) {
|
|
318
|
+
if (isDefined(value)) {
|
|
319
|
+
if (this.config.decodeParams && !param.type.raw) {
|
|
320
|
+
if (isArray(value)) {
|
|
321
|
+
value = value.map((paramValue) => decodeURIComponent(paramValue));
|
|
322
|
+
} else {
|
|
323
|
+
value = decodeURIComponent(value);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
value = param.type.decode(value);
|
|
327
|
+
}
|
|
328
|
+
return param.value(value);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Tests the specified url/path against this matcher.
|
|
332
|
+
*
|
|
333
|
+
* Tests if the given url matches this matcher's pattern, and returns an object containing the captured
|
|
334
|
+
* parameter values. Returns null if the path does not match.
|
|
335
|
+
*
|
|
336
|
+
* The returned object contains the values
|
|
337
|
+
* of any search parameters that are mentioned in the pattern, but their value may be null if
|
|
338
|
+
* they are not present in `search`. This means that search parameters are always treated
|
|
339
|
+
* as optional.
|
|
340
|
+
*
|
|
341
|
+
* #### Example:
|
|
342
|
+
* ```js
|
|
343
|
+
* new UrlMatcher('/user/{id}?q&r').exec('/user/bob', {
|
|
344
|
+
* x: '1', q: 'hello'
|
|
345
|
+
* });
|
|
346
|
+
* // returns { id: 'bob', q: 'hello', r: null }
|
|
347
|
+
* ```
|
|
348
|
+
*
|
|
349
|
+
* @param path The URL path to match, e.g. `$location.path()`.
|
|
350
|
+
* @param search URL search parameters, e.g. `$location.search()`.
|
|
351
|
+
* @param hash URL hash e.g. `$location.hash()`.
|
|
352
|
+
* @param options
|
|
353
|
+
*
|
|
354
|
+
* @returns The captured parameter values.
|
|
355
|
+
*/
|
|
356
|
+
exec(path, search = {}, hash, options = {}) {
|
|
357
|
+
const match = memoizeTo(this._cache, "pattern", () => {
|
|
358
|
+
return new RegExp(
|
|
359
|
+
[
|
|
360
|
+
"^",
|
|
361
|
+
unnest(this._cache.path.map(prop("_compiled"))).join(""),
|
|
362
|
+
this.config.strict === false ? "/?" : "",
|
|
363
|
+
"$",
|
|
364
|
+
].join(""),
|
|
365
|
+
this.config.caseInsensitive ? "i" : undefined,
|
|
366
|
+
);
|
|
367
|
+
}).exec(path);
|
|
368
|
+
if (!match) return null;
|
|
369
|
+
// options = defaults(options, { isolate: false });
|
|
370
|
+
const allParams = this.parameters(),
|
|
371
|
+
pathParams = allParams.filter((param) => !param.isSearch()),
|
|
372
|
+
searchParams = allParams.filter((param) => param.isSearch()),
|
|
373
|
+
nPathSegments = this._cache.path
|
|
374
|
+
.map((urlm) => urlm._segments.length - 1)
|
|
375
|
+
.reduce((a, x) => a + x),
|
|
376
|
+
values = {};
|
|
377
|
+
if (nPathSegments !== match.length - 1)
|
|
378
|
+
throw new Error(`Unbalanced capture group in route '${this.pattern}'`);
|
|
379
|
+
function decodePathArray(paramVal) {
|
|
380
|
+
const reverseString = (str) => str.split("").reverse().join("");
|
|
381
|
+
const unquoteDashes = (str) => str.replace(/\\-/g, "-");
|
|
382
|
+
const split = reverseString(paramVal).split(/-(?!\\)/);
|
|
383
|
+
const allReversed = map(split, reverseString);
|
|
384
|
+
return map(allReversed, unquoteDashes).reverse();
|
|
385
|
+
}
|
|
386
|
+
for (let i = 0; i < nPathSegments; i++) {
|
|
387
|
+
const param = pathParams[i];
|
|
388
|
+
let value = match[i + 1];
|
|
389
|
+
// if the param value matches a pre-replace pair, replace the value before decoding.
|
|
390
|
+
for (let j = 0; j < param.replace.length; j++) {
|
|
391
|
+
if (param.replace[j].from === value) value = param.replace[j].to;
|
|
392
|
+
}
|
|
393
|
+
if (value && param.array === true) value = decodePathArray(value);
|
|
394
|
+
values[param.id] = this._getDecodedParamValue(value, param);
|
|
395
|
+
}
|
|
396
|
+
searchParams.forEach((param) => {
|
|
397
|
+
let value = search[param.id];
|
|
398
|
+
for (let j = 0; j < param.replace.length; j++) {
|
|
399
|
+
if (param.replace[j].from === value) value = param.replace[j].to;
|
|
400
|
+
}
|
|
401
|
+
values[param.id] = this._getDecodedParamValue(value, param);
|
|
402
|
+
});
|
|
403
|
+
if (hash) values["#"] = hash;
|
|
404
|
+
return values;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* @internal
|
|
408
|
+
* Returns all the [[Param]] objects of all path and search parameters of this pattern in order of appearance.
|
|
409
|
+
*
|
|
410
|
+
* @returns {Array.<Param>} An array of [[Param]] objects. Must be treated as read-only. If the
|
|
411
|
+
* pattern has no parameters, an empty array is returned.
|
|
412
|
+
*/
|
|
413
|
+
parameters(opts = {}) {
|
|
414
|
+
if (opts.inherit === false) return this._params;
|
|
415
|
+
return unnest(this._cache.path.map((matcher) => matcher._params));
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* @internal
|
|
419
|
+
* Returns a single parameter from this UrlMatcher by id
|
|
420
|
+
*
|
|
421
|
+
* @param id
|
|
422
|
+
* @param opts
|
|
423
|
+
* @returns {T|Param|any|boolean|UrlMatcher|null}
|
|
424
|
+
*/
|
|
425
|
+
parameter(id, opts = {}) {
|
|
426
|
+
const findParam = () => {
|
|
427
|
+
for (const param of this._params) {
|
|
428
|
+
if (param.id === id) return param;
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
const parent = this._cache.parent;
|
|
432
|
+
return (
|
|
433
|
+
findParam() ||
|
|
434
|
+
(opts.inherit !== false && parent && parent.parameter(id, opts)) ||
|
|
435
|
+
null
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Validates the input parameter values against this UrlMatcher
|
|
440
|
+
*
|
|
441
|
+
* Checks an object hash of parameters to validate their correctness according to the parameter
|
|
442
|
+
* types of this `UrlMatcher`.
|
|
443
|
+
*
|
|
444
|
+
* @param params The object hash of parameters to validate.
|
|
445
|
+
* @returns Returns `true` if `params` validates, otherwise `false`.
|
|
446
|
+
*/
|
|
447
|
+
validates(params) {
|
|
448
|
+
const validParamVal = (param, val) => !param || param.validates(val);
|
|
449
|
+
params = params || {};
|
|
450
|
+
// I'm not sure why this checks only the param keys passed in, and not all the params known to the matcher
|
|
451
|
+
const paramSchema = this.parameters().filter((paramDef) =>
|
|
452
|
+
params.hasOwnProperty(paramDef.id),
|
|
453
|
+
);
|
|
454
|
+
return paramSchema
|
|
455
|
+
.map((paramDef) => validParamVal(paramDef, params[paramDef.id]))
|
|
456
|
+
.reduce(allTrueR, true);
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Given a set of parameter values, creates a URL from this UrlMatcher.
|
|
460
|
+
*
|
|
461
|
+
* Creates a URL that matches this pattern by substituting the specified values
|
|
462
|
+
* for the path and search parameters.
|
|
463
|
+
*
|
|
464
|
+
* #### Example:
|
|
465
|
+
* ```js
|
|
466
|
+
* new UrlMatcher('/user/{id}?q').format({ id:'bob', q:'yes' });
|
|
467
|
+
* // returns '/user/bob?q=yes'
|
|
468
|
+
* ```
|
|
469
|
+
*
|
|
470
|
+
* @param values the values to substitute for the parameters in this pattern.
|
|
471
|
+
* @returns the formatted URL (path and optionally search part).
|
|
472
|
+
*/
|
|
473
|
+
format(values = {}) {
|
|
474
|
+
// Build the full path of UrlMatchers (including all parent UrlMatchers)
|
|
475
|
+
const urlMatchers = this._cache.path;
|
|
476
|
+
// Extract all the static segments and Params (processed as ParamDetails)
|
|
477
|
+
// into an ordered array
|
|
478
|
+
const pathSegmentsAndParams = urlMatchers
|
|
479
|
+
.map(UrlMatcher.pathSegmentsAndParams)
|
|
480
|
+
.reduce(unnestR, [])
|
|
481
|
+
.map((x) => (isString(x) ? x : getDetails(x)));
|
|
482
|
+
// Extract the query params into a separate array
|
|
483
|
+
const queryParams = urlMatchers
|
|
484
|
+
.map(UrlMatcher.queryParams)
|
|
485
|
+
.reduce(unnestR, [])
|
|
486
|
+
.map(getDetails);
|
|
487
|
+
const isInvalid = (param) => param.isValid === false;
|
|
488
|
+
if (pathSegmentsAndParams.concat(queryParams).filter(isInvalid).length) {
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Given a Param, applies the parameter value, then returns detailed information about it
|
|
493
|
+
*/
|
|
494
|
+
function getDetails(param) {
|
|
495
|
+
// Normalize to typed value
|
|
496
|
+
const value = param.value(values[param.id]);
|
|
497
|
+
const isValid = param.validates(value);
|
|
498
|
+
const isDefaultValue = param.isDefaultValue(value);
|
|
499
|
+
// Check if we're in squash mode for the parameter
|
|
500
|
+
const squash = isDefaultValue ? param.squash : false;
|
|
501
|
+
// Allow the Parameter's Type to encode the value
|
|
502
|
+
const encoded = param.type.encode(value);
|
|
503
|
+
return { param, value, isValid, isDefaultValue, squash, encoded };
|
|
504
|
+
}
|
|
505
|
+
// Build up the path-portion from the list of static segments and parameters
|
|
506
|
+
const pathString = pathSegmentsAndParams.reduce((acc, x) => {
|
|
507
|
+
// The element is a static segment (a raw string); just append it
|
|
508
|
+
if (isString(x)) return acc + x;
|
|
509
|
+
// Otherwise, it's a ParamDetails.
|
|
510
|
+
const { squash, encoded, param } = x;
|
|
511
|
+
// If squash is === true, try to remove a slash from the path
|
|
512
|
+
if (squash === true) return acc.match(/\/$/) ? acc.slice(0, -1) : acc;
|
|
513
|
+
// If squash is a string, use the string for the param value
|
|
514
|
+
if (isString(squash)) return acc + squash;
|
|
515
|
+
if (squash !== false) return acc; // ?
|
|
516
|
+
if (encoded == null) return acc;
|
|
517
|
+
// If this parameter value is an array, encode the value using encodeDashes
|
|
518
|
+
if (isArray(encoded))
|
|
519
|
+
return acc + map(encoded, UrlMatcher.encodeDashes).join("-");
|
|
520
|
+
// If the parameter type is "raw", then do not encodeURIComponent
|
|
521
|
+
if (param.raw) return acc + encoded;
|
|
522
|
+
// Encode the value
|
|
523
|
+
return acc + encodeURIComponent(encoded);
|
|
524
|
+
}, "");
|
|
525
|
+
// Build the query string by applying parameter values (array or regular)
|
|
526
|
+
// then mapping to key=value, then flattening and joining using "&"
|
|
527
|
+
const queryString = queryParams
|
|
528
|
+
.map((paramDetails) => {
|
|
529
|
+
let { param, squash, encoded, isDefaultValue } = paramDetails;
|
|
530
|
+
if (encoded == null || (isDefaultValue && squash !== false)) return;
|
|
531
|
+
if (!isArray(encoded)) encoded = [encoded];
|
|
532
|
+
if (encoded.length === 0) return;
|
|
533
|
+
if (!param.raw) encoded = map(encoded, encodeURIComponent);
|
|
534
|
+
return encoded.map((val) => `${param.id}=${val}`);
|
|
535
|
+
})
|
|
536
|
+
.filter(identity)
|
|
537
|
+
.reduce(unnestR, [])
|
|
538
|
+
.join("&");
|
|
539
|
+
// Concat the pathstring with the queryString (if exists) and the hashString (if exists)
|
|
540
|
+
return (
|
|
541
|
+
pathString +
|
|
542
|
+
(queryString ? `?${queryString}` : "") +
|
|
543
|
+
(values["#"] ? "#" + values["#"] : "")
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
/** @internal */
|
|
548
|
+
UrlMatcher.nameValidator = /^\w+([-.]+\w+)*(?:\[\])?$/;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
extend,
|
|
3
|
+
forEach,
|
|
4
|
+
isDefined,
|
|
5
|
+
isFunction,
|
|
6
|
+
isObject,
|
|
7
|
+
} from "../../../core/utils";
|
|
8
|
+
import { UrlMatcher } from "./urlMatcher";
|
|
9
|
+
import { DefType, Param } from "../params/param";
|
|
10
|
+
|
|
11
|
+
export class ParamFactory {
|
|
12
|
+
fromConfig(id, type, state) {
|
|
13
|
+
return new Param(
|
|
14
|
+
id,
|
|
15
|
+
type,
|
|
16
|
+
DefType.CONFIG,
|
|
17
|
+
this.router.urlService.config,
|
|
18
|
+
state,
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
fromPath(id, type, state) {
|
|
22
|
+
return new Param(
|
|
23
|
+
id,
|
|
24
|
+
type,
|
|
25
|
+
DefType.PATH,
|
|
26
|
+
this.router.urlService.config,
|
|
27
|
+
state,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
fromSearch(id, type, state) {
|
|
31
|
+
return new Param(
|
|
32
|
+
id,
|
|
33
|
+
type,
|
|
34
|
+
DefType.SEARCH,
|
|
35
|
+
this.router.urlService.config,
|
|
36
|
+
state,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
constructor(router) {
|
|
40
|
+
this.router = router;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Factory for [[UrlMatcher]] instances.
|
|
45
|
+
*
|
|
46
|
+
* The factory is available to ng1 services as
|
|
47
|
+
* `$urlMatcherFactory` or ng1 providers as `$urlMatcherFactoryProvider`.
|
|
48
|
+
*/
|
|
49
|
+
export class UrlMatcherFactory {
|
|
50
|
+
// TODO: move implementations to UrlConfig (urlService.config)
|
|
51
|
+
constructor(/** @internal */ router) {
|
|
52
|
+
this.router = router;
|
|
53
|
+
/** Creates a new [[Param]] for a given location (DefType) */
|
|
54
|
+
this.paramFactory = new ParamFactory(this.router);
|
|
55
|
+
// TODO: Check if removal of this will break anything, then remove these
|
|
56
|
+
this.UrlMatcher = UrlMatcher;
|
|
57
|
+
this.Param = Param;
|
|
58
|
+
/** @deprecated use [[UrlConfig.caseInsensitive]] */
|
|
59
|
+
this.caseInsensitive = (value) =>
|
|
60
|
+
this.router.urlService.config.caseInsensitive(value);
|
|
61
|
+
/** @deprecated use [[UrlConfig.defaultSquashPolicy]] */
|
|
62
|
+
this.defaultSquashPolicy = (value) =>
|
|
63
|
+
this.router.urlService.config.defaultSquashPolicy(value);
|
|
64
|
+
/** @deprecated use [[UrlConfig.strictMode]] */
|
|
65
|
+
this.strictMode = (value) =>
|
|
66
|
+
this.router.urlService.config.strictMode(value);
|
|
67
|
+
/** @deprecated use [[UrlConfig.type]] */
|
|
68
|
+
this.type = (name, definition, definitionFn) => {
|
|
69
|
+
return (
|
|
70
|
+
this.router.urlService.config.type(name, definition, definitionFn) ||
|
|
71
|
+
this
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Creates a [[UrlMatcher]] for the specified pattern.
|
|
77
|
+
*
|
|
78
|
+
* @param pattern The URL pattern.
|
|
79
|
+
* @param config The config object hash.
|
|
80
|
+
* @returns The UrlMatcher.
|
|
81
|
+
*/
|
|
82
|
+
compile(pattern, config) {
|
|
83
|
+
const urlConfig = this.router.urlService.config;
|
|
84
|
+
// backward-compatible support for config.params -> config.state.params
|
|
85
|
+
const params = config && !config.state && config.params;
|
|
86
|
+
config = params ? Object.assign({ state: { params } }, config) : config;
|
|
87
|
+
const globalConfig = {
|
|
88
|
+
strict: urlConfig._isStrictMode,
|
|
89
|
+
caseInsensitive: urlConfig._isCaseInsensitive,
|
|
90
|
+
decodeParams: urlConfig._decodeParams,
|
|
91
|
+
};
|
|
92
|
+
return new UrlMatcher(
|
|
93
|
+
pattern,
|
|
94
|
+
urlConfig.paramTypes,
|
|
95
|
+
this.paramFactory,
|
|
96
|
+
extend(globalConfig, config),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns true if the specified object is a [[UrlMatcher]], or false otherwise.
|
|
101
|
+
*
|
|
102
|
+
* @param object The object to perform the type check against.
|
|
103
|
+
* @returns `true` if the object matches the `UrlMatcher` interface, by
|
|
104
|
+
* implementing all the same methods.
|
|
105
|
+
*/
|
|
106
|
+
isMatcher(object) {
|
|
107
|
+
// TODO: typeof?
|
|
108
|
+
if (!isObject(object)) return false;
|
|
109
|
+
let result = true;
|
|
110
|
+
forEach(UrlMatcher.prototype, (val, name) => {
|
|
111
|
+
if (isFunction(val))
|
|
112
|
+
result = result && isDefined(object[name]) && isFunction(object[name]);
|
|
113
|
+
});
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
/** @internal */
|
|
117
|
+
$get() {
|
|
118
|
+
const urlConfig = this.router.urlService.config;
|
|
119
|
+
urlConfig.paramTypes.enqueue = false;
|
|
120
|
+
urlConfig.paramTypes._flushTypeQueue();
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
}
|