@ktjs/router 0.14.5 → 0.14.8
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/index.d.ts +0 -6
- package/dist/index.iife.js +67 -147
- package/dist/index.legacy.js +71 -153
- package/dist/index.mjs +67 -147
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -124,10 +124,6 @@ interface RouterConfig {
|
|
|
124
124
|
* Default is `true`
|
|
125
125
|
*/
|
|
126
126
|
asyncGuards?: boolean;
|
|
127
|
-
/**
|
|
128
|
-
* Router mode: 'history' uses HTML5 history API, 'hash' uses URL hash. Default is 'hash'.
|
|
129
|
-
*/
|
|
130
|
-
mode?: 'history' | 'hash';
|
|
131
127
|
}
|
|
132
128
|
|
|
133
129
|
/**
|
|
@@ -157,8 +153,6 @@ interface Router {
|
|
|
157
153
|
|
|
158
154
|
/** Navigate forward in history */
|
|
159
155
|
forward(): void;
|
|
160
|
-
|
|
161
|
-
initCurrentRoute(): void;
|
|
162
156
|
}
|
|
163
157
|
|
|
164
158
|
interface RouteMatch {
|
package/dist/index.iife.js
CHANGED
|
@@ -169,15 +169,13 @@ var __ktjs_router__ = (function (exports) {
|
|
|
169
169
|
const onNotFound = config.onNotFound ?? defaultHook;
|
|
170
170
|
const onError = config.onError ?? defaultHook;
|
|
171
171
|
const asyncGuards = config.asyncGuards ?? true;
|
|
172
|
-
// default to 'hash' mode
|
|
173
|
-
const mode = config.mode ?? 'hash';
|
|
174
172
|
const baseUrl = config.baseUrl ?? '';
|
|
175
173
|
// # private values
|
|
176
174
|
const routes = [];
|
|
175
|
+
const history = [];
|
|
177
176
|
let routerView = null;
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
*/
|
|
177
|
+
let current = null;
|
|
178
|
+
// # methods
|
|
181
179
|
const normalize = (rawRoutes, parentPath) => rawRoutes.map((route) => {
|
|
182
180
|
const path = normalizePath(parentPath, route.path);
|
|
183
181
|
const normalized = {
|
|
@@ -194,56 +192,6 @@ var __ktjs_router__ = (function (exports) {
|
|
|
194
192
|
routes.push(normalized);
|
|
195
193
|
return normalized;
|
|
196
194
|
});
|
|
197
|
-
// Normalize routes with default guards
|
|
198
|
-
normalize(config.routes, '/');
|
|
199
|
-
const { findByName, match } = createMatcher(routes);
|
|
200
|
-
/**
|
|
201
|
-
* Initialize current route from URL
|
|
202
|
-
*/
|
|
203
|
-
const initCurrentRoute = () => {
|
|
204
|
-
if (mode === 'hash') {
|
|
205
|
-
const hash = window.location.hash.slice(1); // Remove '#'
|
|
206
|
-
if (!hash) {
|
|
207
|
-
return (current = null);
|
|
208
|
-
}
|
|
209
|
-
// Parse path and query
|
|
210
|
-
const [path, queryString] = hash.split('?');
|
|
211
|
-
const normalizedPath = normalizePath(path);
|
|
212
|
-
// Match route
|
|
213
|
-
const matched = match(normalizedPath);
|
|
214
|
-
if (!matched) {
|
|
215
|
-
return (current = null);
|
|
216
|
-
}
|
|
217
|
-
// Build route context
|
|
218
|
-
return (current = {
|
|
219
|
-
path: normalizedPath,
|
|
220
|
-
name: matched.route.name,
|
|
221
|
-
params: matched.params,
|
|
222
|
-
query: queryString ? parseQuery(queryString) : {},
|
|
223
|
-
meta: matched.route.meta ?? {},
|
|
224
|
-
matched: matched.result,
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
// history mode
|
|
228
|
-
const pathWithQuery = window.location.pathname + window.location.search;
|
|
229
|
-
const [path, queryString] = pathWithQuery.split('?');
|
|
230
|
-
const normalizedPath = normalizePath(path);
|
|
231
|
-
const matched = match(normalizedPath);
|
|
232
|
-
if (!matched) {
|
|
233
|
-
return (current = null);
|
|
234
|
-
}
|
|
235
|
-
return (current = {
|
|
236
|
-
path: normalizedPath,
|
|
237
|
-
name: matched.route.name,
|
|
238
|
-
params: matched.params,
|
|
239
|
-
query: queryString ? parseQuery(queryString) : {},
|
|
240
|
-
meta: matched.route.meta ?? {},
|
|
241
|
-
matched: matched.result,
|
|
242
|
-
});
|
|
243
|
-
};
|
|
244
|
-
let current = null;
|
|
245
|
-
const history = current ? [current] : [];
|
|
246
|
-
// # methods
|
|
247
195
|
const executeGuardsSync = (to, from, guardLevel) => {
|
|
248
196
|
try {
|
|
249
197
|
if (guardLevel === 0 /* GuardLevel.None */) {
|
|
@@ -387,22 +335,12 @@ var __ktjs_router__ = (function (exports) {
|
|
|
387
335
|
}
|
|
388
336
|
// Update browser history depending on mode
|
|
389
337
|
const url = fullPath;
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
}
|
|
394
|
-
else {
|
|
395
|
-
window.history.pushState({ path: to.path }, '', url);
|
|
396
|
-
}
|
|
338
|
+
const hashUrl = '#' + fullPath;
|
|
339
|
+
if (replace) {
|
|
340
|
+
window.location.replace(hashUrl);
|
|
397
341
|
}
|
|
398
342
|
else {
|
|
399
|
-
|
|
400
|
-
if (replace) {
|
|
401
|
-
window.location.replace(hashUrl);
|
|
402
|
-
}
|
|
403
|
-
else {
|
|
404
|
-
window.location.hash = fullPath;
|
|
405
|
-
}
|
|
343
|
+
window.location.hash = fullPath;
|
|
406
344
|
}
|
|
407
345
|
// Update current route in memory
|
|
408
346
|
current = to;
|
|
@@ -464,23 +402,12 @@ var __ktjs_router__ = (function (exports) {
|
|
|
464
402
|
return false;
|
|
465
403
|
}
|
|
466
404
|
// ---- Guards passed ----
|
|
467
|
-
const
|
|
468
|
-
if (
|
|
469
|
-
|
|
470
|
-
window.history.replaceState({ path: to.path }, '', url);
|
|
471
|
-
}
|
|
472
|
-
else {
|
|
473
|
-
window.history.pushState({ path: to.path }, '', url);
|
|
474
|
-
}
|
|
405
|
+
const hashUrl = '#' + fullPath;
|
|
406
|
+
if (replace) {
|
|
407
|
+
window.location.replace(hashUrl);
|
|
475
408
|
}
|
|
476
409
|
else {
|
|
477
|
-
|
|
478
|
-
if (replace) {
|
|
479
|
-
window.location.replace(hashUrl);
|
|
480
|
-
}
|
|
481
|
-
else {
|
|
482
|
-
window.location.hash = fullPath;
|
|
483
|
-
}
|
|
410
|
+
window.location.hash = fullPath;
|
|
484
411
|
}
|
|
485
412
|
current = to;
|
|
486
413
|
if (replace) {
|
|
@@ -526,74 +453,61 @@ var __ktjs_router__ = (function (exports) {
|
|
|
526
453
|
* Normalize navigation argument
|
|
527
454
|
*/
|
|
528
455
|
const normalizeLocation = (loc) => {
|
|
529
|
-
if (typeof loc
|
|
530
|
-
|
|
531
|
-
const [path, queryString] = loc.split('?');
|
|
532
|
-
return {
|
|
533
|
-
path,
|
|
534
|
-
query: queryString ? parseQuery(queryString) : undefined,
|
|
535
|
-
};
|
|
456
|
+
if (typeof loc !== 'string') {
|
|
457
|
+
return loc;
|
|
536
458
|
}
|
|
537
|
-
|
|
459
|
+
const [path, queryString] = loc.split('?');
|
|
460
|
+
return {
|
|
461
|
+
path,
|
|
462
|
+
query: queryString ? parseQuery(queryString) : undefined,
|
|
463
|
+
};
|
|
538
464
|
};
|
|
539
465
|
// # register events
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
matched: matched.result,
|
|
572
|
-
};
|
|
573
|
-
// apply without modifying browser history
|
|
574
|
-
current = to;
|
|
575
|
-
history.push(to);
|
|
576
|
-
if (routerView && to.matched.length > 0) {
|
|
577
|
-
const route = to.matched[to.matched.length - 1];
|
|
578
|
-
if (route.component) {
|
|
579
|
-
const element = route.component();
|
|
580
|
-
if (element instanceof Promise) {
|
|
581
|
-
element.then((el) => {
|
|
582
|
-
routerView.innerHTML = '';
|
|
583
|
-
routerView.appendChild(el);
|
|
584
|
-
});
|
|
585
|
-
}
|
|
586
|
-
else {
|
|
466
|
+
window.addEventListener('hashchange', () => {
|
|
467
|
+
const hash = window.location.hash.slice(1);
|
|
468
|
+
const [path] = hash.split('?');
|
|
469
|
+
const normalizedPath = normalizePath(path);
|
|
470
|
+
if (current && current.path === normalizedPath) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
// render route for new hash without adding extra history entry
|
|
474
|
+
const matched = match(normalizedPath);
|
|
475
|
+
if (!matched) {
|
|
476
|
+
onNotFound(normalizedPath);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const queryString = window.location.hash.slice(1).split('?')[1];
|
|
480
|
+
const to = {
|
|
481
|
+
path: normalizedPath,
|
|
482
|
+
name: matched.route.name,
|
|
483
|
+
params: matched.params,
|
|
484
|
+
query: queryString ? parseQuery(queryString) : {},
|
|
485
|
+
meta: matched.route.meta ?? {},
|
|
486
|
+
matched: matched.result,
|
|
487
|
+
};
|
|
488
|
+
// apply without modifying browser history
|
|
489
|
+
current = to;
|
|
490
|
+
history.push(to);
|
|
491
|
+
if (routerView && to.matched.length > 0) {
|
|
492
|
+
const route = to.matched[to.matched.length - 1];
|
|
493
|
+
if (route.component) {
|
|
494
|
+
const element = route.component();
|
|
495
|
+
if (element instanceof Promise) {
|
|
496
|
+
element.then((el) => {
|
|
587
497
|
routerView.innerHTML = '';
|
|
588
|
-
routerView.appendChild(
|
|
589
|
-
}
|
|
498
|
+
routerView.appendChild(el);
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
routerView.innerHTML = '';
|
|
503
|
+
routerView.appendChild(element);
|
|
590
504
|
}
|
|
591
505
|
}
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
}
|
|
595
|
-
//
|
|
596
|
-
|
|
506
|
+
}
|
|
507
|
+
executeAfterHooksSync(to, history[history.length - 2] ?? null);
|
|
508
|
+
});
|
|
509
|
+
// # initialize
|
|
510
|
+
const instance = {
|
|
597
511
|
get current() {
|
|
598
512
|
return current;
|
|
599
513
|
},
|
|
@@ -621,8 +535,14 @@ var __ktjs_router__ = (function (exports) {
|
|
|
621
535
|
forward() {
|
|
622
536
|
window.history.forward();
|
|
623
537
|
},
|
|
624
|
-
initCurrentRoute,
|
|
625
538
|
};
|
|
539
|
+
normalize(config.routes, '/');
|
|
540
|
+
const { findByName, match } = createMatcher(routes);
|
|
541
|
+
const currentHash = window.location.hash.slice(1);
|
|
542
|
+
if (currentHash) {
|
|
543
|
+
instance.push(currentHash);
|
|
544
|
+
}
|
|
545
|
+
return instance;
|
|
626
546
|
};
|
|
627
547
|
|
|
628
548
|
exports.KTRouter = KTRouter;
|
package/dist/index.legacy.js
CHANGED
|
@@ -253,22 +253,20 @@ var __ktjs_router__ = (function (exports) {
|
|
|
253
253
|
* Create a new router instance
|
|
254
254
|
*/
|
|
255
255
|
var createRouter = function (config) {
|
|
256
|
-
var _a, _b, _c, _d, _e, _f
|
|
256
|
+
var _a, _b, _c, _d, _e, _f;
|
|
257
257
|
// # default configs
|
|
258
258
|
var beforeEach = (_a = config.beforeEach) !== null && _a !== void 0 ? _a : defaultHook;
|
|
259
259
|
var afterEach = (_b = config.afterEach) !== null && _b !== void 0 ? _b : defaultHook;
|
|
260
260
|
var onNotFound = (_c = config.onNotFound) !== null && _c !== void 0 ? _c : defaultHook;
|
|
261
261
|
var onError = (_d = config.onError) !== null && _d !== void 0 ? _d : defaultHook;
|
|
262
262
|
var asyncGuards = (_e = config.asyncGuards) !== null && _e !== void 0 ? _e : true;
|
|
263
|
-
|
|
264
|
-
var mode = (_f = config.mode) !== null && _f !== void 0 ? _f : 'hash';
|
|
265
|
-
var baseUrl = (_g = config.baseUrl) !== null && _g !== void 0 ? _g : '';
|
|
263
|
+
var baseUrl = (_f = config.baseUrl) !== null && _f !== void 0 ? _f : '';
|
|
266
264
|
// # private values
|
|
267
265
|
var routes = [];
|
|
266
|
+
var history = [];
|
|
268
267
|
var routerView = null;
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
*/
|
|
268
|
+
var current = null;
|
|
269
|
+
// # methods
|
|
272
270
|
var normalize = function (rawRoutes, parentPath) {
|
|
273
271
|
return rawRoutes.map(function (route) {
|
|
274
272
|
var _a, _b, _c, _d;
|
|
@@ -288,57 +286,6 @@ var __ktjs_router__ = (function (exports) {
|
|
|
288
286
|
return normalized;
|
|
289
287
|
});
|
|
290
288
|
};
|
|
291
|
-
// Normalize routes with default guards
|
|
292
|
-
normalize(config.routes, '/');
|
|
293
|
-
var _h = createMatcher(routes), findByName = _h.findByName, match = _h.match;
|
|
294
|
-
/**
|
|
295
|
-
* Initialize current route from URL
|
|
296
|
-
*/
|
|
297
|
-
var initCurrentRoute = function () {
|
|
298
|
-
var _a, _b;
|
|
299
|
-
if (mode === 'hash') {
|
|
300
|
-
var hash = window.location.hash.slice(1); // Remove '#'
|
|
301
|
-
if (!hash) {
|
|
302
|
-
return (current = null);
|
|
303
|
-
}
|
|
304
|
-
// Parse path and query
|
|
305
|
-
var _c = hash.split('?'), path_1 = _c[0], queryString_1 = _c[1];
|
|
306
|
-
var normalizedPath_1 = normalizePath(path_1);
|
|
307
|
-
// Match route
|
|
308
|
-
var matched_1 = match(normalizedPath_1);
|
|
309
|
-
if (!matched_1) {
|
|
310
|
-
return (current = null);
|
|
311
|
-
}
|
|
312
|
-
// Build route context
|
|
313
|
-
return (current = {
|
|
314
|
-
path: normalizedPath_1,
|
|
315
|
-
name: matched_1.route.name,
|
|
316
|
-
params: matched_1.params,
|
|
317
|
-
query: queryString_1 ? parseQuery(queryString_1) : {},
|
|
318
|
-
meta: (_a = matched_1.route.meta) !== null && _a !== void 0 ? _a : {},
|
|
319
|
-
matched: matched_1.result,
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
// history mode
|
|
323
|
-
var pathWithQuery = window.location.pathname + window.location.search;
|
|
324
|
-
var _d = pathWithQuery.split('?'), path = _d[0], queryString = _d[1];
|
|
325
|
-
var normalizedPath = normalizePath(path);
|
|
326
|
-
var matched = match(normalizedPath);
|
|
327
|
-
if (!matched) {
|
|
328
|
-
return (current = null);
|
|
329
|
-
}
|
|
330
|
-
return (current = {
|
|
331
|
-
path: normalizedPath,
|
|
332
|
-
name: matched.route.name,
|
|
333
|
-
params: matched.params,
|
|
334
|
-
query: queryString ? parseQuery(queryString) : {},
|
|
335
|
-
meta: (_b = matched.route.meta) !== null && _b !== void 0 ? _b : {},
|
|
336
|
-
matched: matched.result,
|
|
337
|
-
});
|
|
338
|
-
};
|
|
339
|
-
var current = null;
|
|
340
|
-
var history = current ? [current] : [];
|
|
341
|
-
// # methods
|
|
342
289
|
var executeGuardsSync = function (to, from, guardLevel) {
|
|
343
290
|
try {
|
|
344
291
|
if (guardLevel === 0 /* GuardLevel.None */) {
|
|
@@ -496,22 +443,12 @@ var __ktjs_router__ = (function (exports) {
|
|
|
496
443
|
}
|
|
497
444
|
// Update browser history depending on mode
|
|
498
445
|
var url = fullPath;
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
}
|
|
503
|
-
else {
|
|
504
|
-
window.history.pushState({ path: to.path }, '', url);
|
|
505
|
-
}
|
|
446
|
+
var hashUrl = '#' + fullPath;
|
|
447
|
+
if (replace) {
|
|
448
|
+
window.location.replace(hashUrl);
|
|
506
449
|
}
|
|
507
450
|
else {
|
|
508
|
-
|
|
509
|
-
if (replace) {
|
|
510
|
-
window.location.replace(hashUrl);
|
|
511
|
-
}
|
|
512
|
-
else {
|
|
513
|
-
window.location.hash = fullPath;
|
|
514
|
-
}
|
|
451
|
+
window.location.hash = fullPath;
|
|
515
452
|
}
|
|
516
453
|
// Update current route in memory
|
|
517
454
|
current = to;
|
|
@@ -558,7 +495,7 @@ var __ktjs_router__ = (function (exports) {
|
|
|
558
495
|
args_1[_i - 1] = arguments[_i];
|
|
559
496
|
}
|
|
560
497
|
return __awaiter(void 0, __spreadArray([options_1], args_1, true), void 0, function (options, redirectCount) {
|
|
561
|
-
var prep, guardLevel, replace, to, fullPath, guardResult,
|
|
498
|
+
var prep, guardLevel, replace, to, fullPath, guardResult, hashUrl, route, element, error_2;
|
|
562
499
|
var _a;
|
|
563
500
|
if (redirectCount === void 0) { redirectCount = 0; }
|
|
564
501
|
return __generator(this, function (_b) {
|
|
@@ -585,23 +522,12 @@ var __ktjs_router__ = (function (exports) {
|
|
|
585
522
|
}
|
|
586
523
|
return [2 /*return*/, false];
|
|
587
524
|
}
|
|
588
|
-
|
|
589
|
-
if (
|
|
590
|
-
|
|
591
|
-
window.history.replaceState({ path: to.path }, '', url);
|
|
592
|
-
}
|
|
593
|
-
else {
|
|
594
|
-
window.history.pushState({ path: to.path }, '', url);
|
|
595
|
-
}
|
|
525
|
+
hashUrl = '#' + fullPath;
|
|
526
|
+
if (replace) {
|
|
527
|
+
window.location.replace(hashUrl);
|
|
596
528
|
}
|
|
597
529
|
else {
|
|
598
|
-
|
|
599
|
-
if (replace) {
|
|
600
|
-
window.location.replace(hashUrl);
|
|
601
|
-
}
|
|
602
|
-
else {
|
|
603
|
-
window.location.hash = fullPath;
|
|
604
|
-
}
|
|
530
|
+
window.location.hash = fullPath;
|
|
605
531
|
}
|
|
606
532
|
current = to;
|
|
607
533
|
if (replace) {
|
|
@@ -662,76 +588,62 @@ var __ktjs_router__ = (function (exports) {
|
|
|
662
588
|
* Normalize navigation argument
|
|
663
589
|
*/
|
|
664
590
|
var normalizeLocation = function (loc) {
|
|
665
|
-
if (typeof loc
|
|
666
|
-
|
|
667
|
-
var _a = loc.split('?'), path = _a[0], queryString = _a[1];
|
|
668
|
-
return {
|
|
669
|
-
path: path,
|
|
670
|
-
query: queryString ? parseQuery(queryString) : undefined,
|
|
671
|
-
};
|
|
591
|
+
if (typeof loc !== 'string') {
|
|
592
|
+
return loc;
|
|
672
593
|
}
|
|
673
|
-
|
|
594
|
+
var _a = loc.split('?'), path = _a[0], queryString = _a[1];
|
|
595
|
+
return {
|
|
596
|
+
path: path,
|
|
597
|
+
query: queryString ? parseQuery(queryString) : undefined,
|
|
598
|
+
};
|
|
674
599
|
};
|
|
675
600
|
// # register events
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
window.
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
var
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
meta: (_a = matched.route.meta) !== null && _a !== void 0 ? _a : {},
|
|
709
|
-
matched: matched.result,
|
|
710
|
-
};
|
|
711
|
-
// apply without modifying browser history
|
|
712
|
-
current = to;
|
|
713
|
-
history.push(to);
|
|
714
|
-
if (routerView && to.matched.length > 0) {
|
|
715
|
-
var route = to.matched[to.matched.length - 1];
|
|
716
|
-
if (route.component) {
|
|
717
|
-
var element = route.component();
|
|
718
|
-
if (element instanceof Promise) {
|
|
719
|
-
element.then(function (el) {
|
|
720
|
-
routerView.innerHTML = '';
|
|
721
|
-
routerView.appendChild(el);
|
|
722
|
-
});
|
|
723
|
-
}
|
|
724
|
-
else {
|
|
601
|
+
window.addEventListener('hashchange', function () {
|
|
602
|
+
var _a, _b;
|
|
603
|
+
var hash = window.location.hash.slice(1);
|
|
604
|
+
var path = hash.split('?')[0];
|
|
605
|
+
var normalizedPath = normalizePath(path);
|
|
606
|
+
if (current && current.path === normalizedPath) {
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
// render route for new hash without adding extra history entry
|
|
610
|
+
var matched = match(normalizedPath);
|
|
611
|
+
if (!matched) {
|
|
612
|
+
onNotFound(normalizedPath);
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
var queryString = window.location.hash.slice(1).split('?')[1];
|
|
616
|
+
var to = {
|
|
617
|
+
path: normalizedPath,
|
|
618
|
+
name: matched.route.name,
|
|
619
|
+
params: matched.params,
|
|
620
|
+
query: queryString ? parseQuery(queryString) : {},
|
|
621
|
+
meta: (_a = matched.route.meta) !== null && _a !== void 0 ? _a : {},
|
|
622
|
+
matched: matched.result,
|
|
623
|
+
};
|
|
624
|
+
// apply without modifying browser history
|
|
625
|
+
current = to;
|
|
626
|
+
history.push(to);
|
|
627
|
+
if (routerView && to.matched.length > 0) {
|
|
628
|
+
var route = to.matched[to.matched.length - 1];
|
|
629
|
+
if (route.component) {
|
|
630
|
+
var element = route.component();
|
|
631
|
+
if (element instanceof Promise) {
|
|
632
|
+
element.then(function (el) {
|
|
725
633
|
routerView.innerHTML = '';
|
|
726
|
-
routerView.appendChild(
|
|
727
|
-
}
|
|
634
|
+
routerView.appendChild(el);
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
routerView.innerHTML = '';
|
|
639
|
+
routerView.appendChild(element);
|
|
728
640
|
}
|
|
729
641
|
}
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
}
|
|
733
|
-
//
|
|
734
|
-
|
|
642
|
+
}
|
|
643
|
+
executeAfterHooksSync(to, (_b = history[history.length - 2]) !== null && _b !== void 0 ? _b : null);
|
|
644
|
+
});
|
|
645
|
+
// # initialize
|
|
646
|
+
var instance = {
|
|
735
647
|
get current() {
|
|
736
648
|
return current;
|
|
737
649
|
},
|
|
@@ -759,8 +671,14 @@ var __ktjs_router__ = (function (exports) {
|
|
|
759
671
|
forward: function () {
|
|
760
672
|
window.history.forward();
|
|
761
673
|
},
|
|
762
|
-
initCurrentRoute: initCurrentRoute,
|
|
763
674
|
};
|
|
675
|
+
normalize(config.routes, '/');
|
|
676
|
+
var _g = createMatcher(routes), findByName = _g.findByName, match = _g.match;
|
|
677
|
+
var currentHash = window.location.hash.slice(1);
|
|
678
|
+
if (currentHash) {
|
|
679
|
+
instance.push(currentHash);
|
|
680
|
+
}
|
|
681
|
+
return instance;
|
|
764
682
|
};
|
|
765
683
|
|
|
766
684
|
exports.KTRouter = KTRouter;
|
package/dist/index.mjs
CHANGED
|
@@ -166,15 +166,13 @@ const createRouter = (config) => {
|
|
|
166
166
|
const onNotFound = config.onNotFound ?? defaultHook;
|
|
167
167
|
const onError = config.onError ?? defaultHook;
|
|
168
168
|
const asyncGuards = config.asyncGuards ?? true;
|
|
169
|
-
// default to 'hash' mode
|
|
170
|
-
const mode = config.mode ?? 'hash';
|
|
171
169
|
const baseUrl = config.baseUrl ?? '';
|
|
172
170
|
// # private values
|
|
173
171
|
const routes = [];
|
|
172
|
+
const history = [];
|
|
174
173
|
let routerView = null;
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
*/
|
|
174
|
+
let current = null;
|
|
175
|
+
// # methods
|
|
178
176
|
const normalize = (rawRoutes, parentPath) => rawRoutes.map((route) => {
|
|
179
177
|
const path = normalizePath(parentPath, route.path);
|
|
180
178
|
const normalized = {
|
|
@@ -191,56 +189,6 @@ const createRouter = (config) => {
|
|
|
191
189
|
routes.push(normalized);
|
|
192
190
|
return normalized;
|
|
193
191
|
});
|
|
194
|
-
// Normalize routes with default guards
|
|
195
|
-
normalize(config.routes, '/');
|
|
196
|
-
const { findByName, match } = createMatcher(routes);
|
|
197
|
-
/**
|
|
198
|
-
* Initialize current route from URL
|
|
199
|
-
*/
|
|
200
|
-
const initCurrentRoute = () => {
|
|
201
|
-
if (mode === 'hash') {
|
|
202
|
-
const hash = window.location.hash.slice(1); // Remove '#'
|
|
203
|
-
if (!hash) {
|
|
204
|
-
return (current = null);
|
|
205
|
-
}
|
|
206
|
-
// Parse path and query
|
|
207
|
-
const [path, queryString] = hash.split('?');
|
|
208
|
-
const normalizedPath = normalizePath(path);
|
|
209
|
-
// Match route
|
|
210
|
-
const matched = match(normalizedPath);
|
|
211
|
-
if (!matched) {
|
|
212
|
-
return (current = null);
|
|
213
|
-
}
|
|
214
|
-
// Build route context
|
|
215
|
-
return (current = {
|
|
216
|
-
path: normalizedPath,
|
|
217
|
-
name: matched.route.name,
|
|
218
|
-
params: matched.params,
|
|
219
|
-
query: queryString ? parseQuery(queryString) : {},
|
|
220
|
-
meta: matched.route.meta ?? {},
|
|
221
|
-
matched: matched.result,
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
// history mode
|
|
225
|
-
const pathWithQuery = window.location.pathname + window.location.search;
|
|
226
|
-
const [path, queryString] = pathWithQuery.split('?');
|
|
227
|
-
const normalizedPath = normalizePath(path);
|
|
228
|
-
const matched = match(normalizedPath);
|
|
229
|
-
if (!matched) {
|
|
230
|
-
return (current = null);
|
|
231
|
-
}
|
|
232
|
-
return (current = {
|
|
233
|
-
path: normalizedPath,
|
|
234
|
-
name: matched.route.name,
|
|
235
|
-
params: matched.params,
|
|
236
|
-
query: queryString ? parseQuery(queryString) : {},
|
|
237
|
-
meta: matched.route.meta ?? {},
|
|
238
|
-
matched: matched.result,
|
|
239
|
-
});
|
|
240
|
-
};
|
|
241
|
-
let current = null;
|
|
242
|
-
const history = current ? [current] : [];
|
|
243
|
-
// # methods
|
|
244
192
|
const executeGuardsSync = (to, from, guardLevel) => {
|
|
245
193
|
try {
|
|
246
194
|
if (guardLevel === 0 /* GuardLevel.None */) {
|
|
@@ -384,22 +332,12 @@ const createRouter = (config) => {
|
|
|
384
332
|
}
|
|
385
333
|
// Update browser history depending on mode
|
|
386
334
|
const url = fullPath;
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
}
|
|
391
|
-
else {
|
|
392
|
-
window.history.pushState({ path: to.path }, '', url);
|
|
393
|
-
}
|
|
335
|
+
const hashUrl = '#' + fullPath;
|
|
336
|
+
if (replace) {
|
|
337
|
+
window.location.replace(hashUrl);
|
|
394
338
|
}
|
|
395
339
|
else {
|
|
396
|
-
|
|
397
|
-
if (replace) {
|
|
398
|
-
window.location.replace(hashUrl);
|
|
399
|
-
}
|
|
400
|
-
else {
|
|
401
|
-
window.location.hash = fullPath;
|
|
402
|
-
}
|
|
340
|
+
window.location.hash = fullPath;
|
|
403
341
|
}
|
|
404
342
|
// Update current route in memory
|
|
405
343
|
current = to;
|
|
@@ -461,23 +399,12 @@ const createRouter = (config) => {
|
|
|
461
399
|
return false;
|
|
462
400
|
}
|
|
463
401
|
// ---- Guards passed ----
|
|
464
|
-
const
|
|
465
|
-
if (
|
|
466
|
-
|
|
467
|
-
window.history.replaceState({ path: to.path }, '', url);
|
|
468
|
-
}
|
|
469
|
-
else {
|
|
470
|
-
window.history.pushState({ path: to.path }, '', url);
|
|
471
|
-
}
|
|
402
|
+
const hashUrl = '#' + fullPath;
|
|
403
|
+
if (replace) {
|
|
404
|
+
window.location.replace(hashUrl);
|
|
472
405
|
}
|
|
473
406
|
else {
|
|
474
|
-
|
|
475
|
-
if (replace) {
|
|
476
|
-
window.location.replace(hashUrl);
|
|
477
|
-
}
|
|
478
|
-
else {
|
|
479
|
-
window.location.hash = fullPath;
|
|
480
|
-
}
|
|
407
|
+
window.location.hash = fullPath;
|
|
481
408
|
}
|
|
482
409
|
current = to;
|
|
483
410
|
if (replace) {
|
|
@@ -523,74 +450,61 @@ const createRouter = (config) => {
|
|
|
523
450
|
* Normalize navigation argument
|
|
524
451
|
*/
|
|
525
452
|
const normalizeLocation = (loc) => {
|
|
526
|
-
if (typeof loc
|
|
527
|
-
|
|
528
|
-
const [path, queryString] = loc.split('?');
|
|
529
|
-
return {
|
|
530
|
-
path,
|
|
531
|
-
query: queryString ? parseQuery(queryString) : undefined,
|
|
532
|
-
};
|
|
453
|
+
if (typeof loc !== 'string') {
|
|
454
|
+
return loc;
|
|
533
455
|
}
|
|
534
|
-
|
|
456
|
+
const [path, queryString] = loc.split('?');
|
|
457
|
+
return {
|
|
458
|
+
path,
|
|
459
|
+
query: queryString ? parseQuery(queryString) : undefined,
|
|
460
|
+
};
|
|
535
461
|
};
|
|
536
462
|
// # register events
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
matched: matched.result,
|
|
569
|
-
};
|
|
570
|
-
// apply without modifying browser history
|
|
571
|
-
current = to;
|
|
572
|
-
history.push(to);
|
|
573
|
-
if (routerView && to.matched.length > 0) {
|
|
574
|
-
const route = to.matched[to.matched.length - 1];
|
|
575
|
-
if (route.component) {
|
|
576
|
-
const element = route.component();
|
|
577
|
-
if (element instanceof Promise) {
|
|
578
|
-
element.then((el) => {
|
|
579
|
-
routerView.innerHTML = '';
|
|
580
|
-
routerView.appendChild(el);
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
else {
|
|
463
|
+
window.addEventListener('hashchange', () => {
|
|
464
|
+
const hash = window.location.hash.slice(1);
|
|
465
|
+
const [path] = hash.split('?');
|
|
466
|
+
const normalizedPath = normalizePath(path);
|
|
467
|
+
if (current && current.path === normalizedPath) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
// render route for new hash without adding extra history entry
|
|
471
|
+
const matched = match(normalizedPath);
|
|
472
|
+
if (!matched) {
|
|
473
|
+
onNotFound(normalizedPath);
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
const queryString = window.location.hash.slice(1).split('?')[1];
|
|
477
|
+
const to = {
|
|
478
|
+
path: normalizedPath,
|
|
479
|
+
name: matched.route.name,
|
|
480
|
+
params: matched.params,
|
|
481
|
+
query: queryString ? parseQuery(queryString) : {},
|
|
482
|
+
meta: matched.route.meta ?? {},
|
|
483
|
+
matched: matched.result,
|
|
484
|
+
};
|
|
485
|
+
// apply without modifying browser history
|
|
486
|
+
current = to;
|
|
487
|
+
history.push(to);
|
|
488
|
+
if (routerView && to.matched.length > 0) {
|
|
489
|
+
const route = to.matched[to.matched.length - 1];
|
|
490
|
+
if (route.component) {
|
|
491
|
+
const element = route.component();
|
|
492
|
+
if (element instanceof Promise) {
|
|
493
|
+
element.then((el) => {
|
|
584
494
|
routerView.innerHTML = '';
|
|
585
|
-
routerView.appendChild(
|
|
586
|
-
}
|
|
495
|
+
routerView.appendChild(el);
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
routerView.innerHTML = '';
|
|
500
|
+
routerView.appendChild(element);
|
|
587
501
|
}
|
|
588
502
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
}
|
|
592
|
-
//
|
|
593
|
-
|
|
503
|
+
}
|
|
504
|
+
executeAfterHooksSync(to, history[history.length - 2] ?? null);
|
|
505
|
+
});
|
|
506
|
+
// # initialize
|
|
507
|
+
const instance = {
|
|
594
508
|
get current() {
|
|
595
509
|
return current;
|
|
596
510
|
},
|
|
@@ -618,8 +532,14 @@ const createRouter = (config) => {
|
|
|
618
532
|
forward() {
|
|
619
533
|
window.history.forward();
|
|
620
534
|
},
|
|
621
|
-
initCurrentRoute,
|
|
622
535
|
};
|
|
536
|
+
normalize(config.routes, '/');
|
|
537
|
+
const { findByName, match } = createMatcher(routes);
|
|
538
|
+
const currentHash = window.location.hash.slice(1);
|
|
539
|
+
if (currentHash) {
|
|
540
|
+
instance.push(currentHash);
|
|
541
|
+
}
|
|
542
|
+
return instance;
|
|
623
543
|
};
|
|
624
544
|
|
|
625
545
|
export { KTRouter, createRouter };
|