2021-05-01 12:11:22 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-05-05 20:51:59 +03:00
|
|
|
/** Removes trailing slashed from URI */
|
2021-05-01 16:52:44 +03:00
|
|
|
const normalizeUri = (uri: string) => {
|
2021-05-06 08:23:05 +03:00
|
|
|
if (typeof uri == 'string') {
|
|
|
|
if (uri.trim().length == 0) {
|
|
|
|
throw new Error('uri is empty');
|
|
|
|
}
|
2021-04-09 11:51:43 +03:00
|
|
|
|
2021-05-06 08:23:05 +03:00
|
|
|
let uriLength = uri.length;
|
|
|
|
if (uri[uriLength - 1] == '/') {
|
|
|
|
uri = uri.slice(0, uriLength - 1);
|
|
|
|
}
|
|
|
|
return uri;
|
|
|
|
} else {
|
2021-05-06 09:41:06 +03:00
|
|
|
throw new TypeError(`Only strings are permitted in URI`);
|
2021-04-09 11:51:43 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-05 20:51:59 +03:00
|
|
|
/** URI<-> Fn mapping type */
|
2021-05-01 16:52:44 +03:00
|
|
|
type routeTuple = {
|
|
|
|
uri: string;
|
|
|
|
fn: () => void;
|
|
|
|
};
|
|
|
|
|
2021-05-06 08:23:05 +03:00
|
|
|
/**
|
2021-05-05 20:51:59 +03:00
|
|
|
* Router that selectively executes fucntions
|
2021-05-06 08:23:05 +03:00
|
|
|
* based on window.location.pathname
|
2021-05-05 20:51:59 +03:00
|
|
|
* */
|
2021-04-09 11:51:43 +03:00
|
|
|
export class Router {
|
2021-05-01 16:52:44 +03:00
|
|
|
routes: Array<routeTuple>;
|
2021-04-09 11:51:43 +03:00
|
|
|
constructor() {
|
|
|
|
this.routes = [];
|
|
|
|
}
|
|
|
|
|
2021-05-06 08:23:05 +03:00
|
|
|
/**
|
|
|
|
* registers a route-function pair with Router
|
2021-05-05 20:51:59 +03:00
|
|
|
* @param {string} uri - route to be registered
|
|
|
|
* @param {function} fn: - function to be registered when window.locatin.path
|
|
|
|
* matches uri
|
|
|
|
* */
|
2021-05-01 16:52:44 +03:00
|
|
|
register(uri: string, fn: () => void) {
|
2021-04-09 11:51:43 +03:00
|
|
|
// typechecks
|
2021-05-06 08:23:05 +03:00
|
|
|
if (uri.trim().length == 0) {
|
2021-04-09 11:51:43 +03:00
|
|
|
throw new Error('uri is empty');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof uri !== 'string') {
|
|
|
|
throw new TypeError('URI must be a string');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof fn !== 'function') {
|
|
|
|
throw new TypeError('a callback fn must be provided');
|
|
|
|
}
|
|
|
|
|
2021-05-06 09:41:06 +03:00
|
|
|
uri = normalizeUri(uri);
|
|
|
|
|
|
|
|
if (this.routes.find(route => {
|
2021-04-09 11:51:43 +03:00
|
|
|
if (route.uri == uri) {
|
2021-05-06 09:41:06 +03:00
|
|
|
return true;
|
2021-04-09 11:51:43 +03:00
|
|
|
}
|
2021-05-06 09:41:06 +03:00
|
|
|
})) {
|
|
|
|
throw new Error('URI exists');
|
|
|
|
};
|
2021-04-09 11:51:43 +03:00
|
|
|
|
2021-05-01 16:52:44 +03:00
|
|
|
const route: routeTuple = {
|
2021-04-09 11:51:43 +03:00
|
|
|
uri,
|
|
|
|
fn,
|
|
|
|
};
|
|
|
|
this.routes.push(route);
|
|
|
|
}
|
|
|
|
|
2021-05-05 20:51:59 +03:00
|
|
|
/**
|
|
|
|
* executes registered function with route
|
|
|
|
* matches window.pathname.location
|
|
|
|
* */
|
2021-04-09 11:51:43 +03:00
|
|
|
route() {
|
2021-05-06 08:23:05 +03:00
|
|
|
const path = normalizeUri(window.location.pathname);
|
|
|
|
|
2021-04-09 11:51:43 +03:00
|
|
|
this.routes.forEach(route => {
|
2021-05-05 20:51:59 +03:00
|
|
|
const pattern = new RegExp(`^${route.uri}$`);
|
2021-04-09 11:51:43 +03:00
|
|
|
if (path.match(pattern)) {
|
2021-05-01 16:52:44 +03:00
|
|
|
return route.fn();
|
2021-04-09 11:51:43 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|