-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
90 lines (73 loc) · 2.75 KB
/
router.js
File metadata and controls
90 lines (73 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
let base;
// Compute base from current path if not provided
function getBaseFromPath() {
const parts = window.location.pathname.split('/');
return parts.length > 1 ? `/${parts[1]}/` : '/';
}
// Initialize the router
export function initRouter({ customBase } = {}) {
base = customBase || getBaseFromPath();
// Hook history methods to trigger locationchange
const hookHistoryMethods = () => {
const origPush = history.pushState;
const origReplace = history.replaceState;
history.pushState = function (...args) {
origPush.apply(this, args);
window.dispatchEvent(new Event('locationchange'));
};
history.replaceState = function (...args) {
origReplace.apply(this, args);
window.dispatchEvent(new Event('locationchange'));
};
window.addEventListener('popstate', () => {
window.dispatchEvent(new Event('locationchange'));
});
};
// Handle URL normalization and dispatch routechange
const handleLocationChange = () => {
const { pathname, hash } = window.location;
const normalized = pathname.toLowerCase();
const path = normalized.endsWith('/') ? normalized : `${normalized}/`;
if (window.location.pathname !== path) {
window.history.replaceState(null, '', path);
}
window.dispatchEvent(new CustomEvent('routechange', { detail: path }));
if (!hash) {
window.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
}
};
// Intercept internal link clicks
const setupLinkHandler = () => {
document.addEventListener('click', (e) => {
const anchor = e.target.closest('a');
if (!anchor) return;
const href = anchor.getAttribute('href');
if (e.metaKey || e.ctrlKey || e.shiftKey || anchor.target === '_blank') return;
setLocation(href, true, e);
});
};
hookHistoryMethods();
window.addEventListener('locationchange', handleLocationChange);
setupLinkHandler();
handleLocationChange(); //handle initial load
}
export function getLocation() {
return window.location.pathname;
}
/**
*
* @param {string} href
* @param {boolean} writeHistory Whether browser-history should be written for this navigation process
* @param {Event} e Possibly a click-event that needs intercepting
*/
export function setLocation(href, writeHistory = true, e) {
if (!href || href.startsWith('http') || href.startsWith('//')) return;
if (href.startsWith(base)) {
if(writeHistory) window.history.pushState(null, '', href);
else window.history.replaceState(null, '', href);
e?.preventDefault();
}
}
export function getBase() {
return base;
}