@viewfly/router 2.1.0 → 3.0.0-alpha.0
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/README.md +58 -112
- package/dist/hooks/_api.d.ts +1 -0
- package/dist/hooks/use-query-params.d.ts +2 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.esm.js +479 -0
- package/dist/index.js +494 -0
- package/dist/link.d.ts +12 -0
- package/dist/providers/_api.d.ts +3 -0
- package/dist/providers/navigator.d.ts +56 -0
- package/dist/providers/router.d.ts +28 -0
- package/dist/providers/url-parser.d.ts +38 -0
- package/dist/router-module.d.ts +10 -0
- package/dist/router-outlet.d.ts +6 -0
- package/package.json +27 -20
- package/bundles/index.d.ts +0 -151
- package/bundles/index.esm.js +0 -674
- package/bundles/index.js +0 -684
- package/rollup-d.config.ts +0 -14
package/bundles/index.js
DELETED
|
@@ -1,684 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var core = require('@viewfly/core');
|
|
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
|
-
}
|
|
107
|
-
|
|
108
|
-
/******************************************************************************
|
|
109
|
-
Copyright (c) Microsoft Corporation.
|
|
110
|
-
|
|
111
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
112
|
-
purpose with or without fee is hereby granted.
|
|
113
|
-
|
|
114
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
115
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
116
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
117
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
118
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
119
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
120
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
121
|
-
***************************************************************************** */
|
|
122
|
-
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
function __decorate(decorators, target, key, desc) {
|
|
126
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
127
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
128
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
129
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function __metadata(metadataKey, metadataValue) {
|
|
133
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
134
|
-
}
|
|
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
|
-
|
|
146
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
147
|
-
var e = new Error(message);
|
|
148
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
149
|
-
};
|
|
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
|
-
|
|
317
|
-
class Navigator {
|
|
318
|
-
constructor(baseUrl) {
|
|
319
|
-
Object.defineProperty(this, "baseUrl", {
|
|
320
|
-
enumerable: true,
|
|
321
|
-
configurable: true,
|
|
322
|
-
writable: true,
|
|
323
|
-
value: baseUrl
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
function formatUrl(pathname, urlFormatParams) {
|
|
328
|
-
pathname = pathname.replace(/\/+/g, '/');
|
|
329
|
-
const { queryParams, fragment } = urlFormatParams;
|
|
330
|
-
return pathname + (queryParams ? '?' + formatQueryParams(queryParams) : '') + (fragment ? '#' + fragment : '');
|
|
331
|
-
}
|
|
332
|
-
function formatQueryParams(queryParams) {
|
|
333
|
-
const params = [];
|
|
334
|
-
Object.keys(queryParams).forEach(key => {
|
|
335
|
-
const values = queryParams[key];
|
|
336
|
-
if (Array.isArray(values)) {
|
|
337
|
-
values.forEach(i => {
|
|
338
|
-
params.push(`${key}=${decodeURIComponent(i)}`);
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
else {
|
|
342
|
-
params.push(`${key}=${decodeURIComponent(values)}`);
|
|
343
|
-
}
|
|
344
|
-
});
|
|
345
|
-
return params.join('&');
|
|
346
|
-
}
|
|
347
|
-
exports.BrowserNavigator = class BrowserNavigator extends Navigator {
|
|
348
|
-
/** 挂载在 location 上的路径前缀;'' 或 '/' 表示站点根,不做剥离 */
|
|
349
|
-
get basePathPrefix() {
|
|
350
|
-
return this.baseUrl === '/' || this.baseUrl === '' ? '' : this.baseUrl;
|
|
351
|
-
}
|
|
352
|
-
get pathname() {
|
|
353
|
-
const pathname = location.pathname;
|
|
354
|
-
if (!this.basePathPrefix) {
|
|
355
|
-
return pathname;
|
|
356
|
-
}
|
|
357
|
-
return pathname.startsWith(this.basePathPrefix)
|
|
358
|
-
? pathname.substring(this.basePathPrefix.length)
|
|
359
|
-
: pathname;
|
|
360
|
-
}
|
|
361
|
-
constructor(baseUrl, hooks = {}) {
|
|
362
|
-
super(baseUrl);
|
|
363
|
-
Object.defineProperty(this, "hooks", {
|
|
364
|
-
enumerable: true,
|
|
365
|
-
configurable: true,
|
|
366
|
-
writable: true,
|
|
367
|
-
value: hooks
|
|
368
|
-
});
|
|
369
|
-
Object.defineProperty(this, "onUrlChanged", {
|
|
370
|
-
enumerable: true,
|
|
371
|
-
configurable: true,
|
|
372
|
-
writable: true,
|
|
373
|
-
value: void 0
|
|
374
|
-
});
|
|
375
|
-
Object.defineProperty(this, "urlParser", {
|
|
376
|
-
enumerable: true,
|
|
377
|
-
configurable: true,
|
|
378
|
-
writable: true,
|
|
379
|
-
value: new UrlParser()
|
|
380
|
-
});
|
|
381
|
-
Object.defineProperty(this, "urlTree", {
|
|
382
|
-
enumerable: true,
|
|
383
|
-
configurable: true,
|
|
384
|
-
writable: true,
|
|
385
|
-
value: this.getUrlTree()
|
|
386
|
-
});
|
|
387
|
-
Object.defineProperty(this, "urlChangeEvent", {
|
|
388
|
-
enumerable: true,
|
|
389
|
-
configurable: true,
|
|
390
|
-
writable: true,
|
|
391
|
-
value: new stream.Subject()
|
|
392
|
-
});
|
|
393
|
-
Object.defineProperty(this, "subscription", {
|
|
394
|
-
enumerable: true,
|
|
395
|
-
configurable: true,
|
|
396
|
-
writable: true,
|
|
397
|
-
value: new stream.Subscription()
|
|
398
|
-
});
|
|
399
|
-
this.onUrlChanged = this.urlChangeEvent.asObservable();
|
|
400
|
-
this.subscription.add(stream.fromEvent(window, 'popstate').subscribe(() => {
|
|
401
|
-
this.urlTree = this.getUrlTree();
|
|
402
|
-
this.urlChangeEvent.next();
|
|
403
|
-
}));
|
|
404
|
-
if (this.basePathPrefix && !location.pathname.startsWith(this.basePathPrefix)) {
|
|
405
|
-
history.replaceState(null, '', this.baseUrl);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
to(pathName, relative, queryParams, fragment) {
|
|
409
|
-
const url = this.join(pathName, relative, queryParams, fragment);
|
|
410
|
-
if (location.origin + url === location.href) {
|
|
411
|
-
return true;
|
|
412
|
-
}
|
|
413
|
-
this.runHooks({
|
|
414
|
-
pathname: this.pathname,
|
|
415
|
-
queryParams: this.urlTree.queryParams,
|
|
416
|
-
fragment: this.urlTree.hash
|
|
417
|
-
}, {
|
|
418
|
-
pathname: pathName,
|
|
419
|
-
queryParams: queryParams || {},
|
|
420
|
-
fragment: fragment || null
|
|
421
|
-
}, () => {
|
|
422
|
-
history.pushState(null, '', url);
|
|
423
|
-
this.urlTree = this.getUrlTree();
|
|
424
|
-
this.urlChangeEvent.next();
|
|
425
|
-
});
|
|
426
|
-
return true;
|
|
427
|
-
}
|
|
428
|
-
replace(pathName, relative, queryParams, fragment) {
|
|
429
|
-
const url = this.join(pathName, relative, queryParams, fragment);
|
|
430
|
-
if (location.origin + url === location.href) {
|
|
431
|
-
return true;
|
|
432
|
-
}
|
|
433
|
-
this.runHooks({
|
|
434
|
-
pathname: this.pathname,
|
|
435
|
-
queryParams: this.urlTree.queryParams,
|
|
436
|
-
fragment: this.urlTree.hash
|
|
437
|
-
}, {
|
|
438
|
-
pathname: pathName,
|
|
439
|
-
queryParams: queryParams || {},
|
|
440
|
-
fragment: fragment || null
|
|
441
|
-
}, () => {
|
|
442
|
-
history.replaceState(null, '', url);
|
|
443
|
-
this.urlTree = this.getUrlTree();
|
|
444
|
-
this.urlChangeEvent.next();
|
|
445
|
-
});
|
|
446
|
-
return true;
|
|
447
|
-
}
|
|
448
|
-
join(pathname, relative, queryParams, fragment) {
|
|
449
|
-
if (pathname.startsWith('/')) {
|
|
450
|
-
return formatUrl(this.baseUrl + pathname, { queryParams, fragment });
|
|
451
|
-
}
|
|
452
|
-
const beforePath = this.urlTree.paths.slice(0, relative.deep);
|
|
453
|
-
while (true) {
|
|
454
|
-
if (pathname.startsWith('./')) {
|
|
455
|
-
pathname = pathname.substring(2);
|
|
456
|
-
continue;
|
|
457
|
-
}
|
|
458
|
-
if (pathname.startsWith('../')) {
|
|
459
|
-
pathname = pathname.substring(3);
|
|
460
|
-
beforePath.pop();
|
|
461
|
-
continue;
|
|
462
|
-
}
|
|
463
|
-
break;
|
|
464
|
-
}
|
|
465
|
-
return formatUrl(this.baseUrl + '/' + beforePath.join('/') + '/' + pathname, { queryParams, fragment });
|
|
466
|
-
}
|
|
467
|
-
back() {
|
|
468
|
-
history.back();
|
|
469
|
-
}
|
|
470
|
-
forward() {
|
|
471
|
-
history.forward();
|
|
472
|
-
}
|
|
473
|
-
go(offset) {
|
|
474
|
-
history.go(offset);
|
|
475
|
-
}
|
|
476
|
-
destroy() {
|
|
477
|
-
this.subscription.unsubscribe();
|
|
478
|
-
}
|
|
479
|
-
runHooks(beforeParams, currentParams, next) {
|
|
480
|
-
var _a, _b, _c, _d;
|
|
481
|
-
if (typeof this.hooks.beforeEach === 'function') {
|
|
482
|
-
(_b = (_a = this.hooks).beforeEach) === null || _b === void 0 ? void 0 : _b.call(_a, beforeParams, currentParams, () => {
|
|
483
|
-
var _a, _b;
|
|
484
|
-
next();
|
|
485
|
-
(_b = (_a = this.hooks).afterEach) === null || _b === void 0 ? void 0 : _b.call(_a, currentParams);
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
else {
|
|
489
|
-
next();
|
|
490
|
-
(_d = (_c = this.hooks).afterEach) === null || _d === void 0 ? void 0 : _d.call(_c, currentParams);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
getUrlTree() {
|
|
494
|
-
return this.urlParser.parse(this.pathname + location.search + location.hash);
|
|
495
|
-
}
|
|
496
|
-
};
|
|
497
|
-
exports.BrowserNavigator = __decorate([
|
|
498
|
-
core.Injectable(),
|
|
499
|
-
__metadata("design:paramtypes", [String, Object])
|
|
500
|
-
], exports.BrowserNavigator);
|
|
501
|
-
|
|
502
|
-
function useQueryParams() {
|
|
503
|
-
const router = core.inject(Router);
|
|
504
|
-
const navigator = core.inject(Navigator);
|
|
505
|
-
const params = Object.assign({}, navigator.urlTree.queryParams);
|
|
506
|
-
const queryParams = new Proxy(params, core.readonlyProxyHandler);
|
|
507
|
-
const subscription = router.onRefresh.subscribe(() => {
|
|
508
|
-
core.comparePropsWithCallbacks(params, navigator.urlTree.queryParams, key => {
|
|
509
|
-
core.internalWrite(() => {
|
|
510
|
-
Reflect.deleteProperty(params, key);
|
|
511
|
-
});
|
|
512
|
-
}, (key, value) => {
|
|
513
|
-
core.internalWrite(() => {
|
|
514
|
-
params[key] = value;
|
|
515
|
-
});
|
|
516
|
-
}, (key, value) => {
|
|
517
|
-
core.internalWrite(() => {
|
|
518
|
-
params[key] = value;
|
|
519
|
-
});
|
|
520
|
-
});
|
|
521
|
-
});
|
|
522
|
-
core.onUnmounted(() => {
|
|
523
|
-
subscription.unsubscribe();
|
|
524
|
-
});
|
|
525
|
-
return queryParams;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
function Link(props) {
|
|
529
|
-
const navigator = core.inject(Navigator);
|
|
530
|
-
const router = core.inject(Router);
|
|
531
|
-
function getActive() {
|
|
532
|
-
return props.exact ?
|
|
533
|
-
(navigator.pathname === navigator.join(props.to, router) ||
|
|
534
|
-
(navigator.pathname + '/') === navigator.join(props.to, router)) :
|
|
535
|
-
navigator.pathname.startsWith(navigator.join(props.to, router));
|
|
536
|
-
}
|
|
537
|
-
const isActive = core.reactive({
|
|
538
|
-
value: getActive()
|
|
539
|
-
});
|
|
540
|
-
const subscription = navigator.onUrlChanged.subscribe(() => {
|
|
541
|
-
isActive.value = getActive();
|
|
542
|
-
});
|
|
543
|
-
core.onUnmounted(() => {
|
|
544
|
-
subscription.unsubscribe();
|
|
545
|
-
});
|
|
546
|
-
function navigate(ev) {
|
|
547
|
-
if ((!props.tag || props.tag === 'a') && props.target === '_blank') {
|
|
548
|
-
return;
|
|
549
|
-
}
|
|
550
|
-
ev.preventDefault();
|
|
551
|
-
router.navigateTo(props.to, props.queryParams, props.fragment);
|
|
552
|
-
}
|
|
553
|
-
return () => {
|
|
554
|
-
const Tag = props.tag || 'a';
|
|
555
|
-
const attrs = Object.assign({}, props, Object.assign({ onClick(ev) {
|
|
556
|
-
var _a;
|
|
557
|
-
navigate(ev);
|
|
558
|
-
(_a = props.onClick) === null || _a === void 0 ? void 0 : _a.call(props, ev);
|
|
559
|
-
} }, props));
|
|
560
|
-
if (Tag === 'a') {
|
|
561
|
-
attrs.href = navigator.join(props.to, router, props.queryParams, props.fragment);
|
|
562
|
-
}
|
|
563
|
-
if (isActive.value && props.active) {
|
|
564
|
-
attrs.class = [attrs.class, props.active];
|
|
565
|
-
}
|
|
566
|
-
return jsxRuntime.jsx(Tag, Object.assign({}, attrs, { children: props.children }));
|
|
567
|
-
};
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
class RouterModule {
|
|
571
|
-
constructor(baseUrl = '', hooks = {}) {
|
|
572
|
-
Object.defineProperty(this, "baseUrl", {
|
|
573
|
-
enumerable: true,
|
|
574
|
-
configurable: true,
|
|
575
|
-
writable: true,
|
|
576
|
-
value: baseUrl
|
|
577
|
-
});
|
|
578
|
-
Object.defineProperty(this, "subscription", {
|
|
579
|
-
enumerable: true,
|
|
580
|
-
configurable: true,
|
|
581
|
-
writable: true,
|
|
582
|
-
value: new stream.Subscription()
|
|
583
|
-
});
|
|
584
|
-
Object.defineProperty(this, "navigator", {
|
|
585
|
-
enumerable: true,
|
|
586
|
-
configurable: true,
|
|
587
|
-
writable: true,
|
|
588
|
-
value: void 0
|
|
589
|
-
});
|
|
590
|
-
this.navigator = new exports.BrowserNavigator(this.baseUrl, hooks);
|
|
591
|
-
}
|
|
592
|
-
setup(app) {
|
|
593
|
-
const navigator = this.navigator;
|
|
594
|
-
const router = new Router(navigator, null);
|
|
595
|
-
this.subscription.add(navigator.onUrlChanged.subscribe(() => {
|
|
596
|
-
router.refresh();
|
|
597
|
-
}));
|
|
598
|
-
app.provide([
|
|
599
|
-
{
|
|
600
|
-
provide: Navigator,
|
|
601
|
-
useValue: navigator
|
|
602
|
-
},
|
|
603
|
-
{
|
|
604
|
-
provide: Router,
|
|
605
|
-
useValue: router
|
|
606
|
-
}
|
|
607
|
-
]);
|
|
608
|
-
}
|
|
609
|
-
onDestroy() {
|
|
610
|
-
this.subscription.unsubscribe();
|
|
611
|
-
this.navigator.destroy();
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
const routerErrorFn = core.makeError('RouterOutlet');
|
|
616
|
-
function RouterOutlet(props) {
|
|
617
|
-
const router = core.inject(Router, null);
|
|
618
|
-
if (router === null) {
|
|
619
|
-
throw routerErrorFn('cannot found parent Router.');
|
|
620
|
-
}
|
|
621
|
-
const navigator = core.inject(Navigator);
|
|
622
|
-
const childRouter = new Router(navigator, router);
|
|
623
|
-
const Context = core.createContext([{
|
|
624
|
-
provide: Router,
|
|
625
|
-
useValue: childRouter
|
|
626
|
-
}]);
|
|
627
|
-
const children = core.shallowReactive({
|
|
628
|
-
value: null
|
|
629
|
-
});
|
|
630
|
-
const subscription = router.onRefresh.subscribe(() => {
|
|
631
|
-
updateChildren();
|
|
632
|
-
});
|
|
633
|
-
core.onUnmounted(() => {
|
|
634
|
-
subscription.unsubscribe();
|
|
635
|
-
});
|
|
636
|
-
let currentComponent = null;
|
|
637
|
-
function updateChildren() {
|
|
638
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
639
|
-
const routeConfig = router.consumeConfig(props.config);
|
|
640
|
-
if (!routeConfig) {
|
|
641
|
-
currentComponent = null;
|
|
642
|
-
children.value = props.children || null;
|
|
643
|
-
return;
|
|
644
|
-
}
|
|
645
|
-
if (typeof routeConfig.beforeEach === 'function') {
|
|
646
|
-
const is = yield routeConfig.beforeEach();
|
|
647
|
-
if (!is) {
|
|
648
|
-
return;
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
if (routeConfig.component) {
|
|
652
|
-
_updateChildren(routeConfig.component);
|
|
653
|
-
}
|
|
654
|
-
else if (routeConfig.asyncComponent) {
|
|
655
|
-
const c = yield routeConfig.asyncComponent();
|
|
656
|
-
_updateChildren(c);
|
|
657
|
-
}
|
|
658
|
-
if (typeof routeConfig.afterEach === 'function') {
|
|
659
|
-
routeConfig.afterEach();
|
|
660
|
-
}
|
|
661
|
-
});
|
|
662
|
-
}
|
|
663
|
-
function _updateChildren(Component) {
|
|
664
|
-
childRouter.refresh();
|
|
665
|
-
if (Component !== currentComponent) {
|
|
666
|
-
children.value = jsxRuntime.jsx(Component, {});
|
|
667
|
-
}
|
|
668
|
-
currentComponent = Component;
|
|
669
|
-
}
|
|
670
|
-
updateChildren();
|
|
671
|
-
return () => {
|
|
672
|
-
return jsxRuntime.jsx(Context, { children: children.value });
|
|
673
|
-
};
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
exports.Link = Link;
|
|
677
|
-
exports.Navigator = Navigator;
|
|
678
|
-
exports.Router = Router;
|
|
679
|
-
exports.RouterModule = RouterModule;
|
|
680
|
-
exports.RouterOutlet = RouterOutlet;
|
|
681
|
-
exports.UrlParser = UrlParser;
|
|
682
|
-
exports.formatQueryParams = formatQueryParams;
|
|
683
|
-
exports.formatUrl = formatUrl;
|
|
684
|
-
exports.useQueryParams = useQueryParams;
|