@viewfly/router 2.0.0-alpha.1 → 2.0.0-alpha.3
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 +74 -20
- package/bundles/index.esm.js +442 -195
- package/bundles/index.js +442 -193
- 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,53 @@ exports.BrowserNavigator = class BrowserNavigator extends Navigator {
|
|
|
157
467
|
destroy() {
|
|
158
468
|
this.subscription.unsubscribe();
|
|
159
469
|
}
|
|
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
|
+
});
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
next();
|
|
481
|
+
(_d = (_c = this.hooks).afterEach) === null || _d === void 0 ? void 0 : _d.call(_c, currentParams);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
getUrlTree() {
|
|
485
|
+
return this.urlParser.parse(this.pathname + location.search + location.hash);
|
|
486
|
+
}
|
|
160
487
|
};
|
|
161
488
|
exports.BrowserNavigator = __decorate([
|
|
162
489
|
core.Injectable(),
|
|
163
|
-
__metadata("design:paramtypes", [String])
|
|
490
|
+
__metadata("design:paramtypes", [String, Object])
|
|
164
491
|
], exports.BrowserNavigator);
|
|
165
492
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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()
|
|
493
|
+
function useQueryParams() {
|
|
494
|
+
const router = core.inject(Router);
|
|
495
|
+
const navigator = core.inject(Navigator);
|
|
496
|
+
const params = Object.assign({}, navigator.urlTree.queryParams);
|
|
497
|
+
const queryParams = new Proxy(params, core.readonlyProxyHandler);
|
|
498
|
+
const subscription = router.onRefresh.subscribe(() => {
|
|
499
|
+
core.comparePropsWithCallbacks(params, navigator.urlTree.queryParams, key => {
|
|
500
|
+
core.internalWrite(() => {
|
|
501
|
+
Reflect.deleteProperty(params, key);
|
|
502
|
+
});
|
|
503
|
+
}, (key, value) => {
|
|
504
|
+
core.internalWrite(() => {
|
|
505
|
+
params[key] = value;
|
|
506
|
+
});
|
|
507
|
+
}, (key, value) => {
|
|
508
|
+
core.internalWrite(() => {
|
|
509
|
+
params[key] = value;
|
|
510
|
+
});
|
|
212
511
|
});
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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 = '';
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
remainingPath = this.path.substring(routeConfig.path.length + 1);
|
|
239
|
-
}
|
|
240
|
-
return {
|
|
241
|
-
remainingPath,
|
|
242
|
-
routeConfig
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
back() {
|
|
246
|
-
this.navigator.back();
|
|
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;
|
|
277
|
-
}
|
|
512
|
+
});
|
|
513
|
+
core.onUnmounted(() => {
|
|
514
|
+
subscription.unsubscribe();
|
|
515
|
+
});
|
|
516
|
+
return queryParams;
|
|
278
517
|
}
|
|
279
518
|
|
|
280
519
|
function Link(props) {
|
|
@@ -286,9 +525,11 @@ function Link(props) {
|
|
|
286
525
|
(navigator.pathname + '/') === navigator.join(props.to, router)) :
|
|
287
526
|
navigator.pathname.startsWith(navigator.join(props.to, router));
|
|
288
527
|
}
|
|
289
|
-
const isActive = core.
|
|
528
|
+
const isActive = core.reactive({
|
|
529
|
+
value: getActive()
|
|
530
|
+
});
|
|
290
531
|
const subscription = navigator.onUrlChanged.subscribe(() => {
|
|
291
|
-
isActive.
|
|
532
|
+
isActive.value = getActive();
|
|
292
533
|
});
|
|
293
534
|
core.onUnmounted(() => {
|
|
294
535
|
subscription.unsubscribe();
|
|
@@ -310,65 +551,15 @@ function Link(props) {
|
|
|
310
551
|
if (Tag === 'a') {
|
|
311
552
|
attrs.href = navigator.join(props.to, router, props.queryParams, props.fragment);
|
|
312
553
|
}
|
|
313
|
-
if (isActive
|
|
554
|
+
if (isActive.value && props.active) {
|
|
314
555
|
attrs.class = [attrs.class, props.active];
|
|
315
556
|
}
|
|
316
557
|
return jsxRuntime.jsx(Tag, Object.assign({}, attrs, { children: props.children }));
|
|
317
558
|
};
|
|
318
559
|
}
|
|
319
560
|
|
|
320
|
-
const routerErrorFn = core.makeError('RouterOutlet');
|
|
321
|
-
function RouterOutlet(props) {
|
|
322
|
-
const router = core.inject(Router, null);
|
|
323
|
-
if (router === null) {
|
|
324
|
-
throw routerErrorFn('cannot found parent Router.');
|
|
325
|
-
}
|
|
326
|
-
const navigator = core.inject(Navigator);
|
|
327
|
-
const childRouter = new Router(navigator, router, '');
|
|
328
|
-
const Context = core.createContext([{
|
|
329
|
-
provide: Router,
|
|
330
|
-
useValue: childRouter
|
|
331
|
-
}]);
|
|
332
|
-
const children = core.createSignal(null);
|
|
333
|
-
const subscription = router.onRefresh.subscribe(() => {
|
|
334
|
-
updateChildren();
|
|
335
|
-
});
|
|
336
|
-
core.onUnmounted(() => {
|
|
337
|
-
subscription.unsubscribe();
|
|
338
|
-
});
|
|
339
|
-
let currentComponent = null;
|
|
340
|
-
function updateChildren() {
|
|
341
|
-
const result = router.consumeConfig(props.config);
|
|
342
|
-
if (!result) {
|
|
343
|
-
currentComponent = null;
|
|
344
|
-
children.set(props.children || null);
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
const { routeConfig, remainingPath } = result;
|
|
348
|
-
if (routeConfig.component) {
|
|
349
|
-
_updateChildren(routeConfig.component, remainingPath);
|
|
350
|
-
}
|
|
351
|
-
else if (routeConfig.asyncComponent) {
|
|
352
|
-
routeConfig.asyncComponent().then(c => {
|
|
353
|
-
_updateChildren(c, remainingPath);
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
function _updateChildren(Component, remainingPath) {
|
|
358
|
-
childRouter.refresh(remainingPath);
|
|
359
|
-
if (Component !== currentComponent) {
|
|
360
|
-
children.set(jsxRuntime.jsx(Component, {}));
|
|
361
|
-
}
|
|
362
|
-
currentComponent = Component;
|
|
363
|
-
}
|
|
364
|
-
updateChildren();
|
|
365
|
-
return () => {
|
|
366
|
-
return jsxRuntime.jsx(Context, { children: children() });
|
|
367
|
-
};
|
|
368
|
-
}
|
|
369
|
-
|
|
370
561
|
class RouterModule {
|
|
371
|
-
constructor(baseUrl = '') {
|
|
562
|
+
constructor(baseUrl = '', hooks = {}) {
|
|
372
563
|
Object.defineProperty(this, "baseUrl", {
|
|
373
564
|
enumerable: true,
|
|
374
565
|
configurable: true,
|
|
@@ -387,18 +578,13 @@ class RouterModule {
|
|
|
387
578
|
writable: true,
|
|
388
579
|
value: void 0
|
|
389
580
|
});
|
|
390
|
-
this.navigator = new exports.BrowserNavigator(this.baseUrl);
|
|
581
|
+
this.navigator = new exports.BrowserNavigator(this.baseUrl, hooks);
|
|
391
582
|
}
|
|
392
583
|
setup(app) {
|
|
393
|
-
const baseUrl = this.baseUrl;
|
|
394
584
|
const navigator = this.navigator;
|
|
395
|
-
|
|
396
|
-
const pathname = navigator.pathname;
|
|
397
|
-
return pathname.startsWith(baseUrl) ? pathname.substring(baseUrl.length) : pathname;
|
|
398
|
-
}
|
|
399
|
-
const router = new Router(navigator, null, getPath());
|
|
585
|
+
const router = new Router(navigator, null);
|
|
400
586
|
this.subscription.add(navigator.onUrlChanged.subscribe(() => {
|
|
401
|
-
router.refresh(
|
|
587
|
+
router.refresh();
|
|
402
588
|
}));
|
|
403
589
|
app.provide([
|
|
404
590
|
{
|
|
@@ -417,10 +603,73 @@ class RouterModule {
|
|
|
417
603
|
}
|
|
418
604
|
}
|
|
419
605
|
|
|
606
|
+
const routerErrorFn = core.makeError('RouterOutlet');
|
|
607
|
+
function RouterOutlet(props) {
|
|
608
|
+
const router = core.inject(Router, null);
|
|
609
|
+
if (router === null) {
|
|
610
|
+
throw routerErrorFn('cannot found parent Router.');
|
|
611
|
+
}
|
|
612
|
+
const navigator = core.inject(Navigator);
|
|
613
|
+
const childRouter = new Router(navigator, router);
|
|
614
|
+
const Context = core.createContext([{
|
|
615
|
+
provide: Router,
|
|
616
|
+
useValue: childRouter
|
|
617
|
+
}]);
|
|
618
|
+
const children = core.shallowReactive({
|
|
619
|
+
value: null
|
|
620
|
+
});
|
|
621
|
+
const subscription = router.onRefresh.subscribe(() => {
|
|
622
|
+
updateChildren();
|
|
623
|
+
});
|
|
624
|
+
core.onUnmounted(() => {
|
|
625
|
+
subscription.unsubscribe();
|
|
626
|
+
});
|
|
627
|
+
let currentComponent = null;
|
|
628
|
+
function updateChildren() {
|
|
629
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
630
|
+
const routeConfig = router.consumeConfig(props.config);
|
|
631
|
+
if (!routeConfig) {
|
|
632
|
+
currentComponent = null;
|
|
633
|
+
children.value = props.children || null;
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
if (typeof routeConfig.beforeEach === 'function') {
|
|
637
|
+
const is = yield routeConfig.beforeEach();
|
|
638
|
+
if (!is) {
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
if (routeConfig.component) {
|
|
643
|
+
_updateChildren(routeConfig.component);
|
|
644
|
+
}
|
|
645
|
+
else if (routeConfig.asyncComponent) {
|
|
646
|
+
const c = yield routeConfig.asyncComponent();
|
|
647
|
+
_updateChildren(c);
|
|
648
|
+
}
|
|
649
|
+
if (typeof routeConfig.afterEach === 'function') {
|
|
650
|
+
routeConfig.afterEach();
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
function _updateChildren(Component) {
|
|
655
|
+
childRouter.refresh();
|
|
656
|
+
if (Component !== currentComponent) {
|
|
657
|
+
children.value = jsxRuntime.jsx(Component, {});
|
|
658
|
+
}
|
|
659
|
+
currentComponent = Component;
|
|
660
|
+
}
|
|
661
|
+
updateChildren();
|
|
662
|
+
return () => {
|
|
663
|
+
return jsxRuntime.jsx(Context, { children: children.value });
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
|
|
420
667
|
exports.Link = Link;
|
|
421
668
|
exports.Navigator = Navigator;
|
|
422
669
|
exports.Router = Router;
|
|
423
670
|
exports.RouterModule = RouterModule;
|
|
424
671
|
exports.RouterOutlet = RouterOutlet;
|
|
672
|
+
exports.UrlParser = UrlParser;
|
|
425
673
|
exports.formatQueryParams = formatQueryParams;
|
|
426
674
|
exports.formatUrl = formatUrl;
|
|
675
|
+
exports.useQueryParams = useQueryParams;
|