This commit is contained in:
sairaj mote 2023-10-25 21:13:42 +05:30
parent d3d87ca68e
commit 48c09f401b
2 changed files with 28 additions and 29 deletions

View File

@ -394,49 +394,48 @@ class Router {
/** /**
* @param {string} route * @param {string} route
*/ */
async routeTo(path) { handleRouting = async (page) => {
if (this.routingStart) {
this.routingStart(this.state)
}
if (this.routes[page]) {
await this.routes[page](this.state)
this.lastPage = page
} else {
if (this.routes['404']) {
this.routes['404'](this.state);
} else {
console.error(`No route found for '${page}' and no '404' route is defined.`);
}
}
if (this.routingEnd) {
this.routingEnd(this.state)
}
}
async routeTo(destination) {
try { try {
let page let page
let wildcards = [] let wildcards = []
let queryString let params = {}
let params let [path, queryString] = destination.split('?');
[path, queryString] = path.split('?');
if (path.includes('#')) if (path.includes('#'))
path = path.split('#')[1]; path = path.split('#')[1];
if (path.includes('/')) if (path.includes('/'))
[, page, ...wildcards] = path.split('/') [, page, ...wildcards] = path.split('/')
else else
page = path page = path
this.state = { page, wildcards, lastPage: this.lastPage } this.state = { page, wildcards, lastPage: this.lastPage, params }
if (queryString) { if (queryString) {
params = new URLSearchParams(queryString) params = new URLSearchParams(queryString)
this.state.params = Object.fromEntries(params) this.state.params = Object.fromEntries(params)
} }
if (this.routingStart) { if (document.startViewTransition) {
this.routingStart(this.state)
}
if (this.routes[page]) {
// Fallback for browsers that don't support View transition API:
if (!document.startViewTransition) {
await this.routes[page](this.state)
this.lastPage = page
return;
}
// With a transition:
document.startViewTransition(async () => { document.startViewTransition(async () => {
await this.routes[page](this.state) await this.handleRouting(page)
this.lastPage = page })
});
} else { } else {
if (this.routes['404']) { // Fallback for browsers that don't support View transition API:
this.routes['404'](this.state); await this.handleRouting(page)
} else {
console.error(`No route found for '${page}' and no '404' route is defined.`);
}
}
if (this.routingEnd) {
this.routingEnd(this.state)
} }
} catch (e) { } catch (e) {
console.error(e) console.error(e)

2
main_UI.min.js vendored

File diff suppressed because one or more lines are too long