2
0
mirror of https://github.com/frappe/books.git synced 2024-12-26 20:30:25 +00:00
books/common/router.js

140 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-02-09 12:55:55 +00:00
const Observable = require('frappejs/utils/observable');
2018-02-09 12:55:55 +00:00
module.exports = class Router extends Observable {
constructor() {
2018-02-09 12:55:55 +00:00
super();
2018-01-31 10:13:33 +00:00
this.last_route = null;
this.current_page = null;
this.static_routes = [];
this.dynamic_routes = [];
}
add(route, handler) {
let page = {handler: handler, route: route};
// '/todo/:name/:place'.match(/:([^/]+)/g);
page.param_keys = route.match(/:([^/]+)/g);
if (page.param_keys) {
// make expression
2018-01-23 08:00:29 +00:00
// '/todo/:name/:place'.replace(/\/:([^/]+)/g, "\/([^/]+)");
2018-01-16 06:09:17 +00:00
page.depth = route.split('/').length;
2018-01-23 08:00:29 +00:00
page.expression = route.replace(/\/:([^/]+)/g, "\/([^/]+)");
this.dynamic_routes.push(page);
this.sort_dynamic_routes();
} else {
this.static_routes.push(page);
this.sort_static_routes();
}
}
sort_dynamic_routes() {
2018-01-16 06:09:17 +00:00
// routes with more parts first
this.dynamic_routes = this.dynamic_routes.sort((a, b) => {
2018-01-16 06:09:17 +00:00
if (a.depth < b.depth) {
return 1;
2018-01-16 06:09:17 +00:00
} else if (a.depth > b.depth) {
return -1;
} else {
2018-01-16 06:09:17 +00:00
if (a.param_keys.length !== b.param_keys.length) {
return a.param_keys.length > b.param_keys.length ? 1 : -1;
} else {
return a.route.length > b.route.length ? 1 : -1;
}
}
})
}
sort_static_routes() {
// longer routes on first
this.static_routes = this.static_routes.sort((a, b) => {
return a.route.length > b.route.length ? 1 : -1;
});
}
listen() {
window.addEventListener('hashchange', (event) => {
2018-03-07 10:37:58 +00:00
let route = this.getRoute_string();
2018-01-31 10:13:33 +00:00
if (this.last_route !== route) {
this.show(route);
}
});
}
2018-01-31 10:13:33 +00:00
// split and get routes
2018-03-07 10:37:58 +00:00
getRoute() {
let route = this.getRoute_string();
2018-01-31 10:13:33 +00:00
if (route) {
return route.split('/');
} else {
return [];
}
}
2018-02-08 09:38:47 +00:00
async setRoute(...parts) {
const route = parts.join('/');
2018-01-31 10:13:33 +00:00
// setting this first, does not trigger show via hashchange,
// since we want to this with async/await, we need to trigger
// the show method
this.last_route = route;
window.location.hash = route;
2018-01-31 10:13:33 +00:00
await this.show(route);
}
async show(route) {
if (route && route[0]==='#') {
route = route.substr(1);
}
2018-01-31 10:13:33 +00:00
this.last_route = route;
if (!route) {
route = this.default;
}
let page = this.match(route);
if (page) {
if (typeof page.handler==='function') {
await page.handler(page.params);
} else {
await page.handler.show(page.params);
}
2018-01-23 08:00:29 +00:00
} else {
await this.match('not-found').handler({route: route});
}
2018-02-09 12:55:55 +00:00
await this.trigger('change');
}
match(route) {
// match static
for(let page of this.static_routes) {
if (page.route === route) {
return {handler: page.handler};
}
}
// match dynamic
for(let page of this.dynamic_routes) {
let matches = route.match(new RegExp(page.expression));
if (matches && matches.length == page.param_keys.length + 1) {
let params = {}
for (let i=0; i < page.param_keys.length; i++) {
params[page.param_keys[i].substr(1)] = matches[i + 1];
}
return {handler:page.handler, params: params};
}
}
}
2018-01-31 10:13:33 +00:00
2018-03-07 10:37:58 +00:00
getRoute_string() {
2018-01-31 10:13:33 +00:00
let route = window.location.hash;
if (route && route[0]==='#') {
route = route.substr(1);
}
return route;
}
}