@reidelsaltres/pureper 0.1.93 → 0.1.95
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/out/foundation/Fetcher.js +2 -2
- package/out/foundation/Fetcher.js.map +1 -1
- package/out/foundation/worker/Router.d.ts +0 -5
- package/out/foundation/worker/Router.d.ts.map +1 -1
- package/out/foundation/worker/Router.js +38 -124
- package/out/foundation/worker/Router.js.map +1 -1
- package/package.json +1 -1
- package/src/foundation/Fetcher.ts +2 -2
- package/src/foundation/worker/Router.ts +44 -157
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HOSTING_ORIGIN } from "../index.js";
|
|
1
|
+
import { HOSTING, HOSTING_ORIGIN } from "../index.js";
|
|
2
2
|
export default class Fetcher {
|
|
3
3
|
static async fetchText(url) {
|
|
4
4
|
const response = await this.internalFetch(url);
|
|
@@ -9,7 +9,7 @@ export default class Fetcher {
|
|
|
9
9
|
return await response.json();
|
|
10
10
|
}
|
|
11
11
|
static async internalFetch(url) {
|
|
12
|
-
const URLObj = new URL(url, HOSTING_ORIGIN);
|
|
12
|
+
const URLObj = new URL(HOSTING.substring(0, HOSTING.length - 1) + url, HOSTING_ORIGIN);
|
|
13
13
|
const response = await fetch(URLObj.href, { cache: 'default' });
|
|
14
14
|
if (!response.ok) {
|
|
15
15
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Fetcher.js","sourceRoot":"","sources":["../../src/foundation/Fetcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"Fetcher.js","sourceRoot":"","sources":["../../src/foundation/Fetcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAEtD,MAAM,CAAC,OAAO,OAAO,OAAO;IACxB,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,GAAW;QAC9B,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;QAE/C,OAAO,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IACjC,CAAC;IACD,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,GAAW;QAC9B,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;QAE/C,OAAO,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IACjC,CAAC;IAEO,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,GAAW;QAC1C,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,GAAG,EAAE,cAAc,CAAC,CAAC;QACvF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;QAChE,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,uBAAuB,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;QAC9D,CAAC;QACD,OAAO,QAAQ,CAAC;IACpB,CAAC;CACJ"}
|
|
@@ -20,10 +20,5 @@ export declare abstract class Router {
|
|
|
20
20
|
static tryFindRoute(url: URL): Route;
|
|
21
21
|
static registerRoute<T extends UniHtml>(path: string, route: string, pageFactory: (search?: URLSearchParams) => T, inheritedRoute?: Route): Promise<Route>;
|
|
22
22
|
private static createPage;
|
|
23
|
-
private static normalizeIncomingUrl;
|
|
24
|
-
private static normalizeRoutePath;
|
|
25
|
-
private static extractRelativePath;
|
|
26
|
-
private static resolveRouteHref;
|
|
27
|
-
private static buildHostedUrl;
|
|
28
23
|
}
|
|
29
24
|
//# sourceMappingURL=Router.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../../src/foundation/worker/Router.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../../src/foundation/worker/Router.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,6BAA6B,CAAC;AAElD,MAAM,WAAW,KAAK,CAAC,CAAC,SAAS,OAAO,GAAG,OAAO;IAChD,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IAEb,WAAW,EAAE,CAAC,MAAM,CAAC,EAAE,eAAe,KAAK,CAAC,CAAC;CAC9C;AACD,oBAAY,UAAU;IACpB,OAAO,IAAA;IACP,MAAM,IAAA;IACN,IAAI,IAAA;CACL;AAED,eAAO,MAAM,MAAM,EAAE,KAAK,EAAO,CAAC;AAClC,eAAO,MAAM,QAAQ,EAAE,MAAM,EAAO,CAAC;AAErC,8BAAsB,MAAM;WACZ,kBAAkB,CAAC,GAAG,EAAE,GAAG;WAO3B,iBAAiB,IAAI,GAAG,GAAG,IAAI;WAS/B,mBAAmB;WASnB,aAAa,CAAC,KAAK,EAAE,MAAM;WAM3B,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,SAAS,GAAE,OAAc;WAc9C,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,KAAK;WASvB,aAAa,CAAC,CAAC,SAAS,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC,MAAM,CAAC,EAAE,eAAe,KAAK,CAAC,EAC5H,cAAc,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;IAezC,OAAO,CAAC,MAAM,CAAC,UAAU;CAG1B"}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { HOSTING, HOSTING_ORIGIN } from "../../index.js";
|
|
2
|
-
const HOSTING_BASE = HOSTING.endsWith("/") ? HOSTING.slice(0, -1) : HOSTING;
|
|
3
|
-
const ABSOLUTE_URL_PATTERN = /^[a-zA-Z][a-zA-Z\d+\-.]*:/;
|
|
4
2
|
export var AccessType;
|
|
5
3
|
(function (AccessType) {
|
|
6
4
|
AccessType[AccessType["OFFLINE"] = 0] = "OFFLINE";
|
|
@@ -37,160 +35,76 @@ export class Router {
|
|
|
37
35
|
}
|
|
38
36
|
}
|
|
39
37
|
static legacyRouteTo(route) {
|
|
40
|
-
|
|
41
|
-
if (window.location.
|
|
42
|
-
window.location.replace(
|
|
38
|
+
let url = new URL(route, HOSTING_ORIGIN);
|
|
39
|
+
if (window.location.pathname !== route) {
|
|
40
|
+
window.location.replace(url.href);
|
|
43
41
|
}
|
|
44
42
|
}
|
|
45
43
|
static tryRouteTo(url, pushState = true) {
|
|
44
|
+
const urlH = new URL(url.href, HOSTING_ORIGIN);
|
|
46
45
|
try {
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
page.load(document.getElementById("page"));
|
|
51
|
-
const hostedUrl = Router.buildHostedUrl(normalizedUrl);
|
|
46
|
+
const found = this.tryFindRoute(urlH);
|
|
47
|
+
const page = this.createPage(found, url.searchParams);
|
|
48
|
+
page.load(document.getElementById('page'));
|
|
52
49
|
if (pushState && typeof window !== "undefined" && window.location) {
|
|
53
|
-
window.history.pushState(page,
|
|
50
|
+
window.history.pushState(page, '', urlH.href);
|
|
54
51
|
}
|
|
55
|
-
window.dispatchEvent(new CustomEvent("spa:navigated", { detail: { url: hostedUrl } }));
|
|
56
52
|
}
|
|
57
53
|
catch (error) {
|
|
58
|
-
console.error("[Router]: Unable to route to ",
|
|
54
|
+
console.error("[Router]: Unable to route to ", urlH.href, error);
|
|
59
55
|
}
|
|
60
56
|
}
|
|
61
57
|
static tryFindRoute(url) {
|
|
62
|
-
const
|
|
63
|
-
const found = ROUTES.find(
|
|
58
|
+
const tt = HOSTING.substring(0, HOSTING.length - 1) + url.pathname;
|
|
59
|
+
const found = ROUTES.find(r => r.route === tt);
|
|
64
60
|
if (!found) {
|
|
65
61
|
throw new Error(`[Router]: Route not found: ${url.pathname}`);
|
|
66
62
|
}
|
|
67
63
|
return found;
|
|
68
64
|
}
|
|
69
65
|
static async registerRoute(path, route, pageFactory, inheritedRoute) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const routeObj = { route: compositeRoute, path, pageFactory };
|
|
66
|
+
let prepRoute = route;
|
|
67
|
+
let fullRoute = inheritedRoute ? inheritedRoute.route + prepRoute : prepRoute;
|
|
68
|
+
const tt = HOSTING.substring(0, HOSTING.length - 1) + fullRoute;
|
|
69
|
+
let routeObj = { route: tt, path, pageFactory };
|
|
75
70
|
ROUTES.push(routeObj);
|
|
76
|
-
|
|
77
|
-
console.log(`[Router]: Registered route: ${hostedPreview.pathname} -> ${path}`);
|
|
71
|
+
console.log(`[Router]: Registered route: ${tt} -> ${path}`);
|
|
78
72
|
return Promise.resolve(routeObj);
|
|
79
73
|
}
|
|
80
74
|
static createPage(route, search) {
|
|
81
75
|
return route.pageFactory(search);
|
|
82
76
|
}
|
|
83
|
-
static normalizeIncomingUrl(url) {
|
|
84
|
-
if (url.origin && url.origin !== window.location.origin) {
|
|
85
|
-
return url;
|
|
86
|
-
}
|
|
87
|
-
return new URL(url.href, window.location.origin);
|
|
88
|
-
}
|
|
89
|
-
static normalizeRoutePath(route) {
|
|
90
|
-
if (!route || route === "/") {
|
|
91
|
-
return "/";
|
|
92
|
-
}
|
|
93
|
-
const withSlash = route.startsWith("/") ? route : `/${route}`;
|
|
94
|
-
const collapsed = withSlash.replace(/\/{2,}/g, "/");
|
|
95
|
-
const withoutTrailing = collapsed.length > 1 && collapsed.endsWith("/") ? collapsed.slice(0, -1) : collapsed;
|
|
96
|
-
return withoutTrailing === "" ? "/" : withoutTrailing;
|
|
97
|
-
}
|
|
98
|
-
static extractRelativePath(pathname) {
|
|
99
|
-
if (!pathname) {
|
|
100
|
-
return "/";
|
|
101
|
-
}
|
|
102
|
-
const ensuredPath = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
103
|
-
if (HOSTING_BASE && ensuredPath.startsWith(HOSTING_BASE)) {
|
|
104
|
-
const trimmed = ensuredPath.slice(HOSTING_BASE.length);
|
|
105
|
-
if (!trimmed || trimmed === "") {
|
|
106
|
-
return "/";
|
|
107
|
-
}
|
|
108
|
-
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
109
|
-
}
|
|
110
|
-
return ensuredPath;
|
|
111
|
-
}
|
|
112
|
-
static resolveRouteHref(route) {
|
|
113
|
-
if (!route || route === "/") {
|
|
114
|
-
return new URL("./", HOSTING_ORIGIN);
|
|
115
|
-
}
|
|
116
|
-
if (ABSOLUTE_URL_PATTERN.test(route)) {
|
|
117
|
-
return new URL(route);
|
|
118
|
-
}
|
|
119
|
-
const normalized = route.startsWith("/") ? `.${route}` : route;
|
|
120
|
-
return new URL(normalized, HOSTING_ORIGIN);
|
|
121
|
-
}
|
|
122
|
-
static buildHostedUrl(url) {
|
|
123
|
-
if (url.origin && url.origin !== window.location.origin) {
|
|
124
|
-
return url;
|
|
125
|
-
}
|
|
126
|
-
const relativePath = Router.normalizeRoutePath(Router.extractRelativePath(url.pathname));
|
|
127
|
-
const routeSegment = relativePath === "/" ? "./" : `.${relativePath}`;
|
|
128
|
-
const hosted = new URL(routeSegment, HOSTING_ORIGIN);
|
|
129
|
-
hosted.search = url.search;
|
|
130
|
-
hosted.hash = url.hash;
|
|
131
|
-
return hosted;
|
|
132
|
-
}
|
|
133
77
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return null;
|
|
137
|
-
}
|
|
138
|
-
const trimmed = rawHref.trim();
|
|
139
|
-
if (trimmed === "" || trimmed.toLowerCase().startsWith("javascript:")) {
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
try {
|
|
143
|
-
const candidate = ABSOLUTE_URL_PATTERN.test(trimmed)
|
|
144
|
-
? new URL(trimmed)
|
|
145
|
-
: new URL(trimmed, window.location.href);
|
|
146
|
-
if (candidate.origin !== window.location.origin) {
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
return candidate;
|
|
150
|
-
}
|
|
151
|
-
catch {
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
// For SPA navigation
|
|
156
|
-
document.addEventListener("click", (e) => {
|
|
78
|
+
//For SPA navigation
|
|
79
|
+
document.addEventListener('click', e => {
|
|
157
80
|
const target = e.target;
|
|
158
|
-
if (
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (!resolved) {
|
|
167
|
-
return;
|
|
81
|
+
if (target) {
|
|
82
|
+
const link = target.closest('a[data-link]') ?? target.closest('re-button[data-link]');
|
|
83
|
+
if (link) {
|
|
84
|
+
e.preventDefault();
|
|
85
|
+
const hr = HOSTING_ORIGIN.substring(0, HOSTING_ORIGIN.length - 1) + link.getAttribute('href');
|
|
86
|
+
const url = new URL(hr, HOSTING_ORIGIN);
|
|
87
|
+
Router.tryRouteTo(url);
|
|
88
|
+
}
|
|
168
89
|
}
|
|
169
|
-
e.preventDefault();
|
|
170
|
-
Router.tryRouteTo(resolved);
|
|
171
90
|
});
|
|
172
|
-
//
|
|
173
|
-
window.addEventListener(
|
|
91
|
+
//For back/forward navigation
|
|
92
|
+
window.addEventListener('popstate', e => {
|
|
174
93
|
try {
|
|
175
|
-
const url = new URL(window.location.href);
|
|
94
|
+
const url = new URL(window.location.href, HOSTING_ORIGIN);
|
|
176
95
|
Router.tryRouteTo(url, false);
|
|
177
96
|
}
|
|
178
97
|
catch (error) {
|
|
179
|
-
console.error(
|
|
98
|
+
console.error('[Router] (popstate): failed to route to current location', error);
|
|
180
99
|
}
|
|
181
100
|
});
|
|
182
|
-
window.addEventListener(
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
101
|
+
window.addEventListener('DOMContentLoaded', () => {
|
|
102
|
+
const checkRoutes = () => {
|
|
103
|
+
const routes = ROUTES;
|
|
104
|
+
if (routes) {
|
|
105
|
+
console.log('[Init] [Router]: available routes =', routes.map(r => r.route).join(', '));
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
checkRoutes();
|
|
188
109
|
});
|
|
189
|
-
const describeHostedPath = (route) => {
|
|
190
|
-
if (!route || route === "/") {
|
|
191
|
-
return new URL("./", HOSTING_ORIGIN).pathname;
|
|
192
|
-
}
|
|
193
|
-
const segment = route.startsWith("/") ? `.${route}` : route;
|
|
194
|
-
return new URL(segment, HOSTING_ORIGIN).pathname;
|
|
195
|
-
};
|
|
196
110
|
//# sourceMappingURL=Router.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Router.js","sourceRoot":"","sources":["../../../src/foundation/worker/Router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"Router.js","sourceRoot":"","sources":["../../../src/foundation/worker/Router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AASzD,MAAM,CAAN,IAAY,UAIX;AAJD,WAAY,UAAU;IACpB,iDAAO,CAAA;IACP,+CAAM,CAAA;IACN,2CAAI,CAAA;AACN,CAAC,EAJW,UAAU,KAAV,UAAU,QAIrB;AAED,MAAM,CAAC,MAAM,MAAM,GAAY,EAAE,CAAC;AAClC,MAAM,CAAC,MAAM,QAAQ,GAAa,EAAE,CAAC;AAErC,MAAM,OAAgB,MAAM;IACnB,MAAM,CAAC,kBAAkB,CAAC,GAAQ;QACvC,IAAI,CAAC;YACH,cAAc,CAAC,OAAO,CAAC,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;QAC9D,CAAC;QAAC,OA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AAC,IAAI,EAAE,cAAc,CAAC,CAAC;QAC1D,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,0DAA0D,EAAE,KAAK,CAAC,CAAC;IACnF,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,MAAM,CAAC,gBAAgB,CAAC,kBAAkB,EAAE,GAAG,EAAE;IAC/C,MAAM,WAAW,GAAG,GAAG,EAAE;QACvB,MAAM,MAAM,GAAG,MAAM,CAAC;QACtB,IAAI,MAAM,EAAE,CAAC;YACX,OAAO,CAAC,GAAG,CAAC,qCAAqC,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAC1F,CAAC;IACH,CAAC,CAAC;IAEF,WAAW,EAAE,CAAC;AAChB,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HOSTING_ORIGIN } from "../index.js";
|
|
1
|
+
import { HOSTING, HOSTING_ORIGIN } from "../index.js";
|
|
2
2
|
|
|
3
3
|
export default class Fetcher {
|
|
4
4
|
static async fetchText(url: string): Promise<string> {
|
|
@@ -13,7 +13,7 @@ export default class Fetcher {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
private static async internalFetch(url: string): Promise<Response> {
|
|
16
|
-
const URLObj = new URL(url, HOSTING_ORIGIN);
|
|
16
|
+
const URLObj = new URL(HOSTING.substring(0, HOSTING.length - 1) + url, HOSTING_ORIGIN);
|
|
17
17
|
const response = await fetch(URLObj.href, { cache: 'default' });
|
|
18
18
|
if (!response.ok) {
|
|
19
19
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { HOSTING, HOSTING_ORIGIN } from "../../index.js";
|
|
2
2
|
import UniHtml from "../component_api/UniHtml.js";
|
|
3
3
|
|
|
4
|
-
const HOSTING_BASE = HOSTING.endsWith("/") ? HOSTING.slice(0, -1) : HOSTING;
|
|
5
|
-
const ABSOLUTE_URL_PATTERN = /^[a-zA-Z][a-zA-Z\d+\-.]*:/;
|
|
6
|
-
|
|
7
4
|
export interface Route<T extends UniHtml = UniHtml> {
|
|
8
5
|
route: string;
|
|
9
6
|
path: string;
|
|
@@ -44,57 +41,48 @@ export abstract class Router {
|
|
|
44
41
|
}
|
|
45
42
|
}
|
|
46
43
|
|
|
44
|
+
|
|
47
45
|
public static legacyRouteTo(route: string) {
|
|
48
|
-
|
|
49
|
-
if (window.location.
|
|
50
|
-
window.location.replace(
|
|
46
|
+
let url = new URL(route, HOSTING_ORIGIN);
|
|
47
|
+
if (window.location.pathname !== route) {
|
|
48
|
+
window.location.replace(url.href);
|
|
51
49
|
}
|
|
52
50
|
}
|
|
53
|
-
|
|
54
51
|
public static tryRouteTo(url: URL, pushState: boolean = true) {
|
|
52
|
+
const urlH = new URL(url.href, HOSTING_ORIGIN);
|
|
55
53
|
try {
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const page: UniHtml = Router.createPage(found, normalizedUrl.searchParams);
|
|
59
|
-
|
|
60
|
-
page.load(document.getElementById("page")!);
|
|
54
|
+
const found: Route = this.tryFindRoute(urlH);
|
|
55
|
+
const page: UniHtml = this.createPage(found, url.searchParams);
|
|
61
56
|
|
|
62
|
-
|
|
57
|
+
page.load(document.getElementById('page')!);
|
|
63
58
|
if (pushState && typeof window !== "undefined" && window.location) {
|
|
64
|
-
window.history.pushState(page,
|
|
59
|
+
window.history.pushState(page, '', urlH.href);
|
|
65
60
|
}
|
|
66
|
-
|
|
67
|
-
window.dispatchEvent(new CustomEvent("spa:navigated", { detail: { url: hostedUrl } }));
|
|
68
61
|
} catch (error) {
|
|
69
|
-
console.error("[Router]: Unable to route to ",
|
|
62
|
+
console.error("[Router]: Unable to route to ", urlH.href, error);
|
|
70
63
|
}
|
|
71
64
|
}
|
|
72
|
-
|
|
73
65
|
public static tryFindRoute(url: URL): Route {
|
|
74
|
-
const
|
|
75
|
-
const found = ROUTES.find(
|
|
66
|
+
const tt = HOSTING.substring(0, HOSTING.length - 1) + url.pathname;
|
|
67
|
+
const found = ROUTES.find(r => r.route === tt);
|
|
76
68
|
if (!found) {
|
|
77
69
|
throw new Error(`[Router]: Route not found: ${url.pathname}`);
|
|
78
70
|
}
|
|
79
71
|
return found;
|
|
80
72
|
}
|
|
81
73
|
|
|
82
|
-
public static async registerRoute<T extends UniHtml>(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
inheritedRoute
|
|
87
|
-
): Promise<Route> {
|
|
88
|
-
const normalizedRouteSegment = Router.normalizeRoutePath(route);
|
|
89
|
-
const compositeRoute = inheritedRoute
|
|
90
|
-
? Router.normalizeRoutePath(`${inheritedRoute.route}${normalizedRouteSegment}`)
|
|
91
|
-
: normalizedRouteSegment;
|
|
74
|
+
public static async registerRoute<T extends UniHtml>(path: string, route: string, pageFactory: (search?: URLSearchParams) => T,
|
|
75
|
+
inheritedRoute?: Route): Promise<Route> {
|
|
76
|
+
|
|
77
|
+
let prepRoute = route
|
|
78
|
+
let fullRoute = inheritedRoute ? inheritedRoute.route + prepRoute : prepRoute;
|
|
92
79
|
|
|
93
|
-
const
|
|
80
|
+
const tt = HOSTING.substring(0, HOSTING.length - 1) + fullRoute;
|
|
81
|
+
|
|
82
|
+
let routeObj: Route = { route: tt, path, pageFactory };
|
|
94
83
|
|
|
95
84
|
ROUTES.push(routeObj);
|
|
96
|
-
|
|
97
|
-
console.log(`[Router]: Registered route: ${hostedPreview.pathname} -> ${path}`);
|
|
85
|
+
console.log(`[Router]: Registered route: ${tt} -> ${path}`);
|
|
98
86
|
|
|
99
87
|
return Promise.resolve(routeObj);
|
|
100
88
|
}
|
|
@@ -102,140 +90,39 @@ export abstract class Router {
|
|
|
102
90
|
private static createPage(route: Route, search?: URLSearchParams): UniHtml {
|
|
103
91
|
return route.pageFactory(search);
|
|
104
92
|
}
|
|
105
|
-
|
|
106
|
-
private static normalizeIncomingUrl(url: URL): URL {
|
|
107
|
-
if (url.origin && url.origin !== window.location.origin) {
|
|
108
|
-
return url;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return new URL(url.href, window.location.origin);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
private static normalizeRoutePath(route: string): string {
|
|
115
|
-
if (!route || route === "/") {
|
|
116
|
-
return "/";
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const withSlash = route.startsWith("/") ? route : `/${route}`;
|
|
120
|
-
const collapsed = withSlash.replace(/\/{2,}/g, "/");
|
|
121
|
-
const withoutTrailing = collapsed.length > 1 && collapsed.endsWith("/") ? collapsed.slice(0, -1) : collapsed;
|
|
122
|
-
return withoutTrailing === "" ? "/" : withoutTrailing;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
private static extractRelativePath(pathname: string): string {
|
|
126
|
-
if (!pathname) {
|
|
127
|
-
return "/";
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const ensuredPath = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
131
|
-
|
|
132
|
-
if (HOSTING_BASE && ensuredPath.startsWith(HOSTING_BASE)) {
|
|
133
|
-
const trimmed = ensuredPath.slice(HOSTING_BASE.length);
|
|
134
|
-
if (!trimmed || trimmed === "") {
|
|
135
|
-
return "/";
|
|
136
|
-
}
|
|
137
|
-
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return ensuredPath;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
private static resolveRouteHref(route: string): URL {
|
|
144
|
-
if (!route || route === "/") {
|
|
145
|
-
return new URL("./", HOSTING_ORIGIN);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (ABSOLUTE_URL_PATTERN.test(route)) {
|
|
149
|
-
return new URL(route);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const normalized = route.startsWith("/") ? `.${route}` : route;
|
|
153
|
-
return new URL(normalized, HOSTING_ORIGIN);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
private static buildHostedUrl(url: URL): URL {
|
|
157
|
-
if (url.origin && url.origin !== window.location.origin) {
|
|
158
|
-
return url;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const relativePath = Router.normalizeRoutePath(Router.extractRelativePath(url.pathname));
|
|
162
|
-
const routeSegment = relativePath === "/" ? "./" : `.${relativePath}`;
|
|
163
|
-
const hosted = new URL(routeSegment, HOSTING_ORIGIN);
|
|
164
|
-
hosted.search = url.search;
|
|
165
|
-
hosted.hash = url.hash;
|
|
166
|
-
return hosted;
|
|
167
|
-
}
|
|
168
93
|
}
|
|
169
94
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return null;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const trimmed = rawHref.trim();
|
|
176
|
-
if (trimmed === "" || trimmed.toLowerCase().startsWith("javascript:")) {
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
try {
|
|
181
|
-
const candidate = ABSOLUTE_URL_PATTERN.test(trimmed)
|
|
182
|
-
? new URL(trimmed)
|
|
183
|
-
: new URL(trimmed, window.location.href);
|
|
184
|
-
|
|
185
|
-
if (candidate.origin !== window.location.origin) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
return candidate;
|
|
190
|
-
} catch {
|
|
191
|
-
return null;
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
// For SPA navigation
|
|
196
|
-
document.addEventListener("click", (e) => {
|
|
95
|
+
//For SPA navigation
|
|
96
|
+
document.addEventListener('click', e => {
|
|
197
97
|
const target = e.target as Element | null;
|
|
198
|
-
if (
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
const resolved = resolveLocalNavigationUrl(link.getAttribute("href"));
|
|
208
|
-
if (!resolved) {
|
|
209
|
-
return;
|
|
98
|
+
if (target) {
|
|
99
|
+
const link = target.closest('a[data-link]') ?? target.closest('re-button[data-link]');
|
|
100
|
+
if (link) {
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
const hr = HOSTING_ORIGIN.substring(0, HOSTING_ORIGIN.length - 1) + link.getAttribute('href');
|
|
103
|
+
const url : URL = new URL(hr, HOSTING_ORIGIN);
|
|
104
|
+
Router.tryRouteTo(url);
|
|
105
|
+
}
|
|
210
106
|
}
|
|
211
|
-
|
|
212
|
-
e.preventDefault();
|
|
213
|
-
Router.tryRouteTo(resolved);
|
|
214
107
|
});
|
|
215
108
|
|
|
216
|
-
//
|
|
217
|
-
window.addEventListener(
|
|
109
|
+
//For back/forward navigation
|
|
110
|
+
window.addEventListener('popstate', e => {
|
|
218
111
|
try {
|
|
219
|
-
const url = new URL(window.location.href);
|
|
112
|
+
const url = new URL(window.location.href, HOSTING_ORIGIN);
|
|
220
113
|
Router.tryRouteTo(url, false);
|
|
221
114
|
} catch (error) {
|
|
222
|
-
console.error(
|
|
115
|
+
console.error('[Router] (popstate): failed to route to current location', error);
|
|
223
116
|
}
|
|
224
117
|
});
|
|
225
118
|
|
|
226
|
-
window.addEventListener(
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const describeHostedPath = (route: string): string => {
|
|
235
|
-
if (!route || route === "/") {
|
|
236
|
-
return new URL("./", HOSTING_ORIGIN).pathname;
|
|
237
|
-
}
|
|
119
|
+
window.addEventListener('DOMContentLoaded', () => {
|
|
120
|
+
const checkRoutes = () => {
|
|
121
|
+
const routes = ROUTES;
|
|
122
|
+
if (routes) {
|
|
123
|
+
console.log('[Init] [Router]: available routes =', routes.map(r => r.route).join(', '));
|
|
124
|
+
}
|
|
125
|
+
};
|
|
238
126
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
};
|
|
127
|
+
checkRoutes();
|
|
128
|
+
});
|