@viewfly/router 1.2.3 → 1.2.5
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/bundles/index.d.ts +73 -19
- package/bundles/index.esm.js +429 -199
- package/bundles/index.js +429 -197
- package/package.json +3 -3
package/bundles/index.js
CHANGED
|
@@ -1,8 +1,109 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var jsxRuntime = require('@viewfly/core/jsx-runtime');
|
|
4
3
|
var core = require('@viewfly/core');
|
|
5
4
|
var stream = require('@tanbo/stream');
|
|
5
|
+
var jsxRuntime = require('@viewfly/core/jsx-runtime');
|
|
6
|
+
|
|
7
|
+
const routerErrorFn$1 = core.makeError('Router');
|
|
8
|
+
class Router {
|
|
9
|
+
get deep() {
|
|
10
|
+
return this.parent ? this.parent.deep + 1 : 0;
|
|
11
|
+
}
|
|
12
|
+
get path() {
|
|
13
|
+
return this.navigator.urlTree.paths.at(this.deep) || '';
|
|
14
|
+
}
|
|
15
|
+
constructor(navigator, parent) {
|
|
16
|
+
Object.defineProperty(this, "navigator", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: navigator
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, "parent", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: parent
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "onRefresh", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: void 0
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(this, "refreshEvent", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: new stream.Subject()
|
|
39
|
+
});
|
|
40
|
+
this.onRefresh = this.refreshEvent.asObservable();
|
|
41
|
+
}
|
|
42
|
+
navigateTo(path, params, fragment) {
|
|
43
|
+
this.navigator.to(path, this, params, fragment || void 0);
|
|
44
|
+
}
|
|
45
|
+
replaceTo(path, params) {
|
|
46
|
+
this.navigator.replace(path, this, params);
|
|
47
|
+
}
|
|
48
|
+
refresh() {
|
|
49
|
+
this.refreshEvent.next();
|
|
50
|
+
}
|
|
51
|
+
consumeConfig(routes) {
|
|
52
|
+
return this.matchRoute(routes, this.path);
|
|
53
|
+
}
|
|
54
|
+
back() {
|
|
55
|
+
this.navigator.back();
|
|
56
|
+
}
|
|
57
|
+
forward() {
|
|
58
|
+
this.navigator.forward();
|
|
59
|
+
}
|
|
60
|
+
go(offset) {
|
|
61
|
+
this.navigator.go(offset);
|
|
62
|
+
}
|
|
63
|
+
matchRoute(configs, pathname) {
|
|
64
|
+
let matchedConfig = null;
|
|
65
|
+
let defaultConfig = null;
|
|
66
|
+
let fallbackConfig = null;
|
|
67
|
+
for (const item of configs) {
|
|
68
|
+
if (item.path === pathname) {
|
|
69
|
+
matchedConfig = item;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
else if (item.path === '*') {
|
|
73
|
+
if (!fallbackConfig) {
|
|
74
|
+
fallbackConfig = item;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else if (item.path === '') {
|
|
78
|
+
if (!defaultConfig) {
|
|
79
|
+
defaultConfig = item;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const config = matchedConfig || defaultConfig || fallbackConfig;
|
|
84
|
+
if (!config) {
|
|
85
|
+
return config;
|
|
86
|
+
}
|
|
87
|
+
if (typeof config.redirectTo === 'function') {
|
|
88
|
+
const p = config.redirectTo(pathname);
|
|
89
|
+
if (typeof p === 'string') {
|
|
90
|
+
this.navigateTo(p);
|
|
91
|
+
}
|
|
92
|
+
else if (typeof p === 'object') {
|
|
93
|
+
this.navigateTo(p.pathname, p.queryParams, p.fragment);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
throw routerErrorFn$1(`Router redirect to '${pathname}' not supported`);
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
if (typeof config.redirectTo === 'string') {
|
|
101
|
+
this.navigateTo(config.redirectTo);
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return config;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
6
107
|
|
|
7
108
|
/******************************************************************************
|
|
8
109
|
Copyright (c) Microsoft Corporation.
|
|
@@ -32,11 +133,187 @@ function __metadata(metadataKey, metadataValue) {
|
|
|
32
133
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
33
134
|
}
|
|
34
135
|
|
|
136
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
137
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
138
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
139
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
140
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
141
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
142
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
35
146
|
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
36
147
|
var e = new Error(message);
|
|
37
148
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
38
149
|
};
|
|
39
150
|
|
|
151
|
+
class UrlParser {
|
|
152
|
+
constructor() {
|
|
153
|
+
Object.defineProperty(this, "index", {
|
|
154
|
+
enumerable: true,
|
|
155
|
+
configurable: true,
|
|
156
|
+
writable: true,
|
|
157
|
+
value: 0
|
|
158
|
+
});
|
|
159
|
+
Object.defineProperty(this, "url", {
|
|
160
|
+
enumerable: true,
|
|
161
|
+
configurable: true,
|
|
162
|
+
writable: true,
|
|
163
|
+
value: ''
|
|
164
|
+
});
|
|
165
|
+
Object.defineProperty(this, "tokens", {
|
|
166
|
+
enumerable: true,
|
|
167
|
+
configurable: true,
|
|
168
|
+
writable: true,
|
|
169
|
+
value: []
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
parse(url) {
|
|
173
|
+
this.index = 0;
|
|
174
|
+
this.url = url;
|
|
175
|
+
this.tokens = [];
|
|
176
|
+
while (this.index < this.url.length) {
|
|
177
|
+
this.ignore('/');
|
|
178
|
+
if (this.peek('../')) {
|
|
179
|
+
this.tokens.push({
|
|
180
|
+
type: 'toParent'
|
|
181
|
+
});
|
|
182
|
+
this.index += 3;
|
|
183
|
+
}
|
|
184
|
+
else if (this.peek('?')) {
|
|
185
|
+
this.index++;
|
|
186
|
+
this.tokens.push({
|
|
187
|
+
type: 'query',
|
|
188
|
+
params: this.readQuery()
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
else if (this.peek('#')) {
|
|
192
|
+
this.index++;
|
|
193
|
+
this.tokens.push({
|
|
194
|
+
type: 'hash',
|
|
195
|
+
value: this.readHash()
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
if (this.peek('./')) {
|
|
200
|
+
this.index += 2;
|
|
201
|
+
}
|
|
202
|
+
const path = this.readPath();
|
|
203
|
+
if (path) {
|
|
204
|
+
this.tokens.push({
|
|
205
|
+
type: 'toChild',
|
|
206
|
+
value: path
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const urlTree = {
|
|
212
|
+
paths: [],
|
|
213
|
+
queryParams: {},
|
|
214
|
+
hash: null
|
|
215
|
+
};
|
|
216
|
+
for (const item of this.tokens) {
|
|
217
|
+
switch (item.type) {
|
|
218
|
+
case 'toParent':
|
|
219
|
+
urlTree.paths.pop();
|
|
220
|
+
break;
|
|
221
|
+
case 'toChild':
|
|
222
|
+
urlTree.paths.push(item.value);
|
|
223
|
+
break;
|
|
224
|
+
case 'query':
|
|
225
|
+
urlTree.queryParams = item.params;
|
|
226
|
+
break;
|
|
227
|
+
case 'hash':
|
|
228
|
+
urlTree.hash = item.value;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return urlTree;
|
|
232
|
+
}
|
|
233
|
+
readHash() {
|
|
234
|
+
const hash = this.url.substring(this.index);
|
|
235
|
+
this.index = this.url.length;
|
|
236
|
+
return hash;
|
|
237
|
+
}
|
|
238
|
+
readQuery() {
|
|
239
|
+
const query = {};
|
|
240
|
+
while (this.index < this.url.length) {
|
|
241
|
+
const key = this.readQueryKey();
|
|
242
|
+
let value = '';
|
|
243
|
+
if (this.peek('=')) {
|
|
244
|
+
this.index++;
|
|
245
|
+
value = this.readQueryValue();
|
|
246
|
+
}
|
|
247
|
+
const oldValue = query[key];
|
|
248
|
+
if (oldValue) {
|
|
249
|
+
if (Array.isArray(oldValue)) {
|
|
250
|
+
oldValue.push(value);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
query[key] = [oldValue, value];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
query[key] = value;
|
|
258
|
+
}
|
|
259
|
+
if (this.peek('&')) {
|
|
260
|
+
this.index++;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
return query;
|
|
266
|
+
}
|
|
267
|
+
readQueryValue() {
|
|
268
|
+
const chars = [];
|
|
269
|
+
while (this.index < this.url.length) {
|
|
270
|
+
if (this.not('&#')) {
|
|
271
|
+
chars.push(this.url.at(this.index));
|
|
272
|
+
this.index++;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
return chars.join('');
|
|
278
|
+
}
|
|
279
|
+
readQueryKey() {
|
|
280
|
+
const chars = [];
|
|
281
|
+
while (this.index < this.url.length) {
|
|
282
|
+
if (this.not('=&#')) {
|
|
283
|
+
chars.push(this.url.at(this.index));
|
|
284
|
+
this.index++;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
return chars.join('');
|
|
290
|
+
}
|
|
291
|
+
readPath() {
|
|
292
|
+
const chars = [];
|
|
293
|
+
while (this.index < this.url.length) {
|
|
294
|
+
if (this.not('./?#')) {
|
|
295
|
+
chars.push(this.url.at(this.index));
|
|
296
|
+
this.index++;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
return chars.join('');
|
|
302
|
+
}
|
|
303
|
+
not(text) {
|
|
304
|
+
const ch = this.url.at(this.index);
|
|
305
|
+
return text.indexOf(ch) === -1;
|
|
306
|
+
}
|
|
307
|
+
peek(str) {
|
|
308
|
+
return this.url.slice(this.index, this.index + str.length) === str;
|
|
309
|
+
}
|
|
310
|
+
ignore(str) {
|
|
311
|
+
while (this.peek(str)) {
|
|
312
|
+
this.index++;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
40
317
|
class Navigator {
|
|
41
318
|
constructor(baseUrl) {
|
|
42
319
|
Object.defineProperty(this, "baseUrl", {
|
|
@@ -69,16 +346,35 @@ function formatQueryParams(queryParams) {
|
|
|
69
346
|
}
|
|
70
347
|
exports.BrowserNavigator = class BrowserNavigator extends Navigator {
|
|
71
348
|
get pathname() {
|
|
72
|
-
|
|
349
|
+
const pathname = location.pathname;
|
|
350
|
+
return pathname.startsWith(this.baseUrl) ? pathname.substring(this.baseUrl.length) : pathname;
|
|
73
351
|
}
|
|
74
|
-
constructor(baseUrl) {
|
|
352
|
+
constructor(baseUrl, hooks = {}) {
|
|
75
353
|
super(baseUrl);
|
|
354
|
+
Object.defineProperty(this, "hooks", {
|
|
355
|
+
enumerable: true,
|
|
356
|
+
configurable: true,
|
|
357
|
+
writable: true,
|
|
358
|
+
value: hooks
|
|
359
|
+
});
|
|
76
360
|
Object.defineProperty(this, "onUrlChanged", {
|
|
77
361
|
enumerable: true,
|
|
78
362
|
configurable: true,
|
|
79
363
|
writable: true,
|
|
80
364
|
value: void 0
|
|
81
365
|
});
|
|
366
|
+
Object.defineProperty(this, "urlParser", {
|
|
367
|
+
enumerable: true,
|
|
368
|
+
configurable: true,
|
|
369
|
+
writable: true,
|
|
370
|
+
value: new UrlParser()
|
|
371
|
+
});
|
|
372
|
+
Object.defineProperty(this, "urlTree", {
|
|
373
|
+
enumerable: true,
|
|
374
|
+
configurable: true,
|
|
375
|
+
writable: true,
|
|
376
|
+
value: this.getUrlTree()
|
|
377
|
+
});
|
|
82
378
|
Object.defineProperty(this, "urlChangeEvent", {
|
|
83
379
|
enumerable: true,
|
|
84
380
|
configurable: true,
|
|
@@ -93,6 +389,7 @@ exports.BrowserNavigator = class BrowserNavigator extends Navigator {
|
|
|
93
389
|
});
|
|
94
390
|
this.onUrlChanged = this.urlChangeEvent.asObservable();
|
|
95
391
|
this.subscription.add(stream.fromEvent(window, 'popstate').subscribe(() => {
|
|
392
|
+
this.urlTree = this.getUrlTree();
|
|
96
393
|
this.urlChangeEvent.next();
|
|
97
394
|
}));
|
|
98
395
|
if (!this.pathname.startsWith(this.baseUrl)) {
|
|
@@ -104,8 +401,19 @@ exports.BrowserNavigator = class BrowserNavigator extends Navigator {
|
|
|
104
401
|
if (location.origin + url === location.href) {
|
|
105
402
|
return true;
|
|
106
403
|
}
|
|
107
|
-
|
|
108
|
-
|
|
404
|
+
this.runHooks({
|
|
405
|
+
pathname: this.pathname,
|
|
406
|
+
queryParams: this.urlTree.queryParams,
|
|
407
|
+
fragment: this.urlTree.hash
|
|
408
|
+
}, {
|
|
409
|
+
pathname: pathName,
|
|
410
|
+
queryParams: queryParams || {},
|
|
411
|
+
fragment: fragment || null
|
|
412
|
+
}, () => {
|
|
413
|
+
history.pushState(null, '', url);
|
|
414
|
+
this.urlTree = this.getUrlTree();
|
|
415
|
+
this.urlChangeEvent.next();
|
|
416
|
+
});
|
|
109
417
|
return true;
|
|
110
418
|
}
|
|
111
419
|
replace(pathName, relative, queryParams, fragment) {
|
|
@@ -113,15 +421,26 @@ exports.BrowserNavigator = class BrowserNavigator extends Navigator {
|
|
|
113
421
|
if (location.origin + url === location.href) {
|
|
114
422
|
return true;
|
|
115
423
|
}
|
|
116
|
-
|
|
117
|
-
|
|
424
|
+
this.runHooks({
|
|
425
|
+
pathname: this.pathname,
|
|
426
|
+
queryParams: this.urlTree.queryParams,
|
|
427
|
+
fragment: this.urlTree.hash
|
|
428
|
+
}, {
|
|
429
|
+
pathname: pathName,
|
|
430
|
+
queryParams: queryParams || {},
|
|
431
|
+
fragment: fragment || null
|
|
432
|
+
}, () => {
|
|
433
|
+
history.replaceState(null, '', url);
|
|
434
|
+
this.urlTree = this.getUrlTree();
|
|
435
|
+
this.urlChangeEvent.next();
|
|
436
|
+
});
|
|
118
437
|
return true;
|
|
119
438
|
}
|
|
120
439
|
join(pathname, relative, queryParams, fragment) {
|
|
121
440
|
if (pathname.startsWith('/')) {
|
|
122
441
|
return formatUrl(this.baseUrl + pathname, { queryParams, fragment });
|
|
123
442
|
}
|
|
124
|
-
|
|
443
|
+
const beforePath = this.urlTree.paths.slice(0, relative.deep);
|
|
125
444
|
while (true) {
|
|
126
445
|
if (pathname.startsWith('./')) {
|
|
127
446
|
pathname = pathname.substring(2);
|
|
@@ -129,21 +448,12 @@ exports.BrowserNavigator = class BrowserNavigator extends Navigator {
|
|
|
129
448
|
}
|
|
130
449
|
if (pathname.startsWith('../')) {
|
|
131
450
|
pathname = pathname.substring(3);
|
|
132
|
-
|
|
133
|
-
beforePath = relative.parent.beforePath;
|
|
134
|
-
relative = relative.parent;
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
beforePath = '';
|
|
138
|
-
}
|
|
139
|
-
if (!beforePath) {
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
451
|
+
beforePath.pop();
|
|
142
452
|
continue;
|
|
143
453
|
}
|
|
144
454
|
break;
|
|
145
455
|
}
|
|
146
|
-
return formatUrl(this.baseUrl + '/' + beforePath + '/' + pathname, { queryParams, fragment });
|
|
456
|
+
return formatUrl(this.baseUrl + '/' + beforePath.join('/') + '/' + pathname, { queryParams, fragment });
|
|
147
457
|
}
|
|
148
458
|
back() {
|
|
149
459
|
history.back();
|
|
@@ -157,124 +467,40 @@ exports.BrowserNavigator = class BrowserNavigator extends Navigator {
|
|
|
157
467
|
destroy() {
|
|
158
468
|
this.subscription.unsubscribe();
|
|
159
469
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (this.parent) {
|
|
169
|
-
const name = this.parent.path.match(/[^\/?#]+/);
|
|
170
|
-
if (name) {
|
|
171
|
-
return name[0];
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return '';
|
|
175
|
-
}
|
|
176
|
-
get beforePath() {
|
|
177
|
-
if (this.parent) {
|
|
178
|
-
return this.parent.beforePath + '/' + this.pathname;
|
|
179
|
-
}
|
|
180
|
-
return '';
|
|
181
|
-
}
|
|
182
|
-
constructor(navigator, parent, path) {
|
|
183
|
-
Object.defineProperty(this, "navigator", {
|
|
184
|
-
enumerable: true,
|
|
185
|
-
configurable: true,
|
|
186
|
-
writable: true,
|
|
187
|
-
value: navigator
|
|
188
|
-
});
|
|
189
|
-
Object.defineProperty(this, "parent", {
|
|
190
|
-
enumerable: true,
|
|
191
|
-
configurable: true,
|
|
192
|
-
writable: true,
|
|
193
|
-
value: parent
|
|
194
|
-
});
|
|
195
|
-
Object.defineProperty(this, "path", {
|
|
196
|
-
enumerable: true,
|
|
197
|
-
configurable: true,
|
|
198
|
-
writable: true,
|
|
199
|
-
value: path
|
|
200
|
-
});
|
|
201
|
-
Object.defineProperty(this, "onRefresh", {
|
|
202
|
-
enumerable: true,
|
|
203
|
-
configurable: true,
|
|
204
|
-
writable: true,
|
|
205
|
-
value: void 0
|
|
206
|
-
});
|
|
207
|
-
Object.defineProperty(this, "refreshEvent", {
|
|
208
|
-
enumerable: true,
|
|
209
|
-
configurable: true,
|
|
210
|
-
writable: true,
|
|
211
|
-
value: new stream.Subject()
|
|
212
|
-
});
|
|
213
|
-
this.onRefresh = this.refreshEvent.asObservable();
|
|
214
|
-
}
|
|
215
|
-
navigateTo(path, params, fragment) {
|
|
216
|
-
this.navigator.to(path, this, params, fragment);
|
|
217
|
-
}
|
|
218
|
-
replaceTo(path, params) {
|
|
219
|
-
this.navigator.replace(path, this, params);
|
|
220
|
-
}
|
|
221
|
-
refresh(path) {
|
|
222
|
-
this.path = path;
|
|
223
|
-
this.refreshEvent.next();
|
|
224
|
-
}
|
|
225
|
-
consumeConfig(routes) {
|
|
226
|
-
const routeConfig = this.matchRoute(routes);
|
|
227
|
-
if (!routeConfig) {
|
|
228
|
-
return null;
|
|
229
|
-
}
|
|
230
|
-
let remainingPath = '';
|
|
231
|
-
if (routeConfig.path === '') {
|
|
232
|
-
remainingPath = this.path;
|
|
233
|
-
}
|
|
234
|
-
else if (routeConfig.path === '*') {
|
|
235
|
-
remainingPath = '';
|
|
470
|
+
runHooks(beforeParams, currentParams, next) {
|
|
471
|
+
var _a, _b, _c, _d;
|
|
472
|
+
if (typeof this.hooks.beforeEach === 'function') {
|
|
473
|
+
(_b = (_a = this.hooks).beforeEach) === null || _b === void 0 ? void 0 : _b.call(_a, beforeParams, currentParams, () => {
|
|
474
|
+
var _a, _b;
|
|
475
|
+
next();
|
|
476
|
+
(_b = (_a = this.hooks).afterEach) === null || _b === void 0 ? void 0 : _b.call(_a, currentParams);
|
|
477
|
+
});
|
|
236
478
|
}
|
|
237
479
|
else {
|
|
238
|
-
|
|
480
|
+
next();
|
|
481
|
+
(_d = (_c = this.hooks).afterEach) === null || _d === void 0 ? void 0 : _d.call(_c, currentParams);
|
|
239
482
|
}
|
|
240
|
-
return {
|
|
241
|
-
remainingPath,
|
|
242
|
-
routeConfig
|
|
243
|
-
};
|
|
244
483
|
}
|
|
245
|
-
|
|
246
|
-
this.
|
|
247
|
-
}
|
|
248
|
-
forward() {
|
|
249
|
-
this.navigator.forward();
|
|
250
|
-
}
|
|
251
|
-
go(offset) {
|
|
252
|
-
this.navigator.go(offset);
|
|
253
|
-
}
|
|
254
|
-
matchRoute(configs) {
|
|
255
|
-
var _a;
|
|
256
|
-
let matchedConfig = null;
|
|
257
|
-
let defaultConfig = null;
|
|
258
|
-
let fallbackConfig = null;
|
|
259
|
-
const pathname = ((_a = (this.path || '').match(/[^\/?#]+/)) === null || _a === void 0 ? void 0 : _a[0]) || '';
|
|
260
|
-
for (const item of configs) {
|
|
261
|
-
if (item.path === pathname) {
|
|
262
|
-
matchedConfig = item;
|
|
263
|
-
break;
|
|
264
|
-
}
|
|
265
|
-
else if (item.path === '*') {
|
|
266
|
-
if (!fallbackConfig) {
|
|
267
|
-
fallbackConfig = item;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
else if (item.path === '') {
|
|
271
|
-
if (!defaultConfig) {
|
|
272
|
-
defaultConfig = item;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
return matchedConfig || defaultConfig || fallbackConfig;
|
|
484
|
+
getUrlTree() {
|
|
485
|
+
return this.urlParser.parse(this.pathname + location.search + location.hash);
|
|
277
486
|
}
|
|
487
|
+
};
|
|
488
|
+
exports.BrowserNavigator = __decorate([
|
|
489
|
+
core.Injectable(),
|
|
490
|
+
__metadata("design:paramtypes", [String, Object])
|
|
491
|
+
], exports.BrowserNavigator);
|
|
492
|
+
|
|
493
|
+
function useQueryParams() {
|
|
494
|
+
const router = core.inject(Router);
|
|
495
|
+
const navigator = core.inject(Navigator);
|
|
496
|
+
const queryParams = core.createSignal(navigator.urlTree.queryParams);
|
|
497
|
+
const subscription = router.onRefresh.subscribe(() => {
|
|
498
|
+
queryParams.set(navigator.urlTree.queryParams);
|
|
499
|
+
});
|
|
500
|
+
core.onUnmounted(() => {
|
|
501
|
+
subscription.unsubscribe();
|
|
502
|
+
});
|
|
503
|
+
return queryParams;
|
|
278
504
|
}
|
|
279
505
|
|
|
280
506
|
function Link(props) {
|
|
@@ -317,64 +543,8 @@ function Link(props) {
|
|
|
317
543
|
};
|
|
318
544
|
}
|
|
319
545
|
|
|
320
|
-
const routerErrorFn = core.makeError('RouterOutlet');
|
|
321
|
-
const RouterOutlet = core.withAnnotation({
|
|
322
|
-
providers: [{
|
|
323
|
-
provide: Router,
|
|
324
|
-
useFactory(navigator, router) {
|
|
325
|
-
return new Router(navigator, router, '');
|
|
326
|
-
},
|
|
327
|
-
deps: [
|
|
328
|
-
[Navigator],
|
|
329
|
-
[Router, new core.SkipSelf()]
|
|
330
|
-
]
|
|
331
|
-
}]
|
|
332
|
-
}, function RouterOutlet(props) {
|
|
333
|
-
const children = core.createSignal(null);
|
|
334
|
-
const router = core.inject(Router, null, core.InjectFlags.SkipSelf);
|
|
335
|
-
const childRouter = core.inject(Router);
|
|
336
|
-
if (router === null) {
|
|
337
|
-
throw routerErrorFn('cannot found parent Router.');
|
|
338
|
-
}
|
|
339
|
-
const subscription = router.onRefresh.subscribe(() => {
|
|
340
|
-
updateChildren();
|
|
341
|
-
});
|
|
342
|
-
core.onUnmounted(() => {
|
|
343
|
-
subscription.unsubscribe();
|
|
344
|
-
});
|
|
345
|
-
let currentComponent = null;
|
|
346
|
-
function updateChildren() {
|
|
347
|
-
const result = router.consumeConfig(props.config);
|
|
348
|
-
if (!result) {
|
|
349
|
-
currentComponent = null;
|
|
350
|
-
children.set(props.children || null);
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
const { routeConfig, remainingPath } = result;
|
|
354
|
-
if (routeConfig.component) {
|
|
355
|
-
_updateChildren(routeConfig.component, remainingPath);
|
|
356
|
-
}
|
|
357
|
-
else if (routeConfig.asyncComponent) {
|
|
358
|
-
routeConfig.asyncComponent().then(c => {
|
|
359
|
-
_updateChildren(c, remainingPath);
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
function _updateChildren(Component, remainingPath) {
|
|
364
|
-
childRouter.refresh(remainingPath);
|
|
365
|
-
if (Component !== currentComponent) {
|
|
366
|
-
children.set(jsxRuntime.jsx(Component, {}));
|
|
367
|
-
}
|
|
368
|
-
currentComponent = Component;
|
|
369
|
-
}
|
|
370
|
-
updateChildren();
|
|
371
|
-
return () => {
|
|
372
|
-
return jsxRuntime.jsx(jsxRuntime.Fragment, { children: children() });
|
|
373
|
-
};
|
|
374
|
-
});
|
|
375
|
-
|
|
376
546
|
class RouterModule {
|
|
377
|
-
constructor(baseUrl = '') {
|
|
547
|
+
constructor(baseUrl = '', hooks = {}) {
|
|
378
548
|
Object.defineProperty(this, "baseUrl", {
|
|
379
549
|
enumerable: true,
|
|
380
550
|
configurable: true,
|
|
@@ -393,18 +563,13 @@ class RouterModule {
|
|
|
393
563
|
writable: true,
|
|
394
564
|
value: void 0
|
|
395
565
|
});
|
|
396
|
-
this.navigator = new exports.BrowserNavigator(this.baseUrl);
|
|
566
|
+
this.navigator = new exports.BrowserNavigator(this.baseUrl, hooks);
|
|
397
567
|
}
|
|
398
568
|
setup(app) {
|
|
399
|
-
const baseUrl = this.baseUrl;
|
|
400
569
|
const navigator = this.navigator;
|
|
401
|
-
|
|
402
|
-
const pathname = navigator.pathname;
|
|
403
|
-
return pathname.startsWith(baseUrl) ? pathname.substring(baseUrl.length) : pathname;
|
|
404
|
-
}
|
|
405
|
-
const router = new Router(navigator, null, getPath());
|
|
570
|
+
const router = new Router(navigator, null);
|
|
406
571
|
this.subscription.add(navigator.onUrlChanged.subscribe(() => {
|
|
407
|
-
router.refresh(
|
|
572
|
+
router.refresh();
|
|
408
573
|
}));
|
|
409
574
|
app.provide([
|
|
410
575
|
{
|
|
@@ -423,10 +588,77 @@ class RouterModule {
|
|
|
423
588
|
}
|
|
424
589
|
}
|
|
425
590
|
|
|
591
|
+
const routerErrorFn = core.makeError('RouterOutlet');
|
|
592
|
+
const RouterOutlet = core.withAnnotation({
|
|
593
|
+
providers: [{
|
|
594
|
+
provide: Router,
|
|
595
|
+
useFactory(navigator, router) {
|
|
596
|
+
return new Router(navigator, router);
|
|
597
|
+
},
|
|
598
|
+
deps: [
|
|
599
|
+
[Navigator],
|
|
600
|
+
[Router, new core.SkipSelf()]
|
|
601
|
+
]
|
|
602
|
+
}]
|
|
603
|
+
}, function RouterOutlet(props) {
|
|
604
|
+
const children = core.createSignal(null);
|
|
605
|
+
const router = core.inject(Router, null, core.InjectFlags.SkipSelf);
|
|
606
|
+
const childRouter = core.inject(Router);
|
|
607
|
+
if (router === null) {
|
|
608
|
+
throw routerErrorFn('cannot found parent Router.');
|
|
609
|
+
}
|
|
610
|
+
const subscription = router.onRefresh.subscribe(() => {
|
|
611
|
+
updateChildren();
|
|
612
|
+
});
|
|
613
|
+
core.onUnmounted(() => {
|
|
614
|
+
subscription.unsubscribe();
|
|
615
|
+
});
|
|
616
|
+
let currentComponent = null;
|
|
617
|
+
function updateChildren() {
|
|
618
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
619
|
+
const routeConfig = router.consumeConfig(props.config);
|
|
620
|
+
if (!routeConfig) {
|
|
621
|
+
currentComponent = null;
|
|
622
|
+
children.set(props.children || null);
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
if (typeof routeConfig.beforeEach === 'function') {
|
|
626
|
+
const is = yield routeConfig.beforeEach();
|
|
627
|
+
if (!is) {
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
if (routeConfig.component) {
|
|
632
|
+
_updateChildren(routeConfig.component);
|
|
633
|
+
}
|
|
634
|
+
else if (routeConfig.asyncComponent) {
|
|
635
|
+
const c = yield routeConfig.asyncComponent();
|
|
636
|
+
_updateChildren(c);
|
|
637
|
+
}
|
|
638
|
+
if (typeof routeConfig.afterEach === 'function') {
|
|
639
|
+
routeConfig.afterEach();
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
function _updateChildren(Component) {
|
|
644
|
+
childRouter.refresh();
|
|
645
|
+
if (Component !== currentComponent) {
|
|
646
|
+
children.set(jsxRuntime.jsx(Component, {}));
|
|
647
|
+
}
|
|
648
|
+
currentComponent = Component;
|
|
649
|
+
}
|
|
650
|
+
updateChildren();
|
|
651
|
+
return () => {
|
|
652
|
+
return jsxRuntime.jsx(jsxRuntime.Fragment, { children: children() });
|
|
653
|
+
};
|
|
654
|
+
});
|
|
655
|
+
|
|
426
656
|
exports.Link = Link;
|
|
427
657
|
exports.Navigator = Navigator;
|
|
428
658
|
exports.Router = Router;
|
|
429
659
|
exports.RouterModule = RouterModule;
|
|
430
660
|
exports.RouterOutlet = RouterOutlet;
|
|
661
|
+
exports.UrlParser = UrlParser;
|
|
431
662
|
exports.formatQueryParams = formatQueryParams;
|
|
432
663
|
exports.formatUrl = formatUrl;
|
|
664
|
+
exports.useQueryParams = useQueryParams;
|