2022-12-31 10:21:29 +00:00
|
|
|
import * as plugins from './smartrouter.plugins.js';
|
2022-01-22 12:29:23 +00:00
|
|
|
|
2022-12-31 10:21:29 +00:00
|
|
|
import { QueryParams } from './smartrouter.classes.queryparams.js';
|
2022-01-22 12:29:23 +00:00
|
|
|
|
2022-12-31 10:21:29 +00:00
|
|
|
const routeLog = (message: string) => {
|
2022-01-22 12:29:23 +00:00
|
|
|
console.log(`%c[Router]%c ${message}`, 'color: rgb(255, 105, 100);', 'color: inherit');
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface IRouterOptions {
|
|
|
|
debug?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type THandlerFunction = <T extends object>(routeArg: IRouteInfo) => Promise<any>;
|
|
|
|
export interface IRouteInfo {
|
|
|
|
path: string;
|
|
|
|
index: number;
|
|
|
|
params: { [key: string]: string };
|
|
|
|
queryParams: { [key: string]: string };
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Router
|
|
|
|
*/
|
|
|
|
export class SmartRouter {
|
|
|
|
public options: IRouterOptions = {
|
|
|
|
debug: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
public queryParams = new QueryParams();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the routes we are handling
|
|
|
|
*/
|
|
|
|
public routes: Array<{
|
2024-10-06 20:39:05 +00:00
|
|
|
matchFunction: plugins.pathToRegExp.MatchFunction<any>;
|
2022-01-22 12:29:23 +00:00
|
|
|
handler: THandlerFunction;
|
|
|
|
}> = [];
|
|
|
|
|
2024-10-06 18:15:21 +00:00
|
|
|
/**
|
|
|
|
* base path for this router
|
|
|
|
*/
|
|
|
|
private basePath: string;
|
|
|
|
|
2024-10-06 20:27:53 +00:00
|
|
|
/**
|
|
|
|
* Reference to the event listener function for cleanup
|
|
|
|
*/
|
|
|
|
private popstateListener: (event: PopStateEvent) => void;
|
|
|
|
|
2022-01-22 12:29:23 +00:00
|
|
|
/**
|
|
|
|
* Creates an instance of Router.
|
|
|
|
*/
|
2024-10-06 18:15:21 +00:00
|
|
|
constructor(optionsArg: IRouterOptions, basePath: string = '') {
|
2022-01-22 12:29:23 +00:00
|
|
|
// lets set the router options
|
|
|
|
this.options = {
|
|
|
|
...this.options,
|
|
|
|
...optionsArg,
|
|
|
|
};
|
2024-10-06 18:15:21 +00:00
|
|
|
this.basePath = basePath;
|
2022-01-22 12:29:23 +00:00
|
|
|
|
|
|
|
// lets subscribe to route changes
|
2024-10-06 20:27:53 +00:00
|
|
|
this.popstateListener = (popStateEventArg) => {
|
2022-01-22 12:29:23 +00:00
|
|
|
popStateEventArg.preventDefault();
|
|
|
|
this._handleRouteState();
|
2024-10-06 20:27:53 +00:00
|
|
|
};
|
|
|
|
window.addEventListener('popstate', this.popstateListener);
|
2022-01-22 12:29:23 +00:00
|
|
|
}
|
|
|
|
|
2024-10-06 18:15:21 +00:00
|
|
|
/**
|
|
|
|
* Create a sub-router with a specific prefix
|
|
|
|
* @param {string} subPath
|
|
|
|
* @param {IRouterOptions} [options]
|
|
|
|
*/
|
|
|
|
public createSubRouter(subPath: string, options?: IRouterOptions): SmartRouter {
|
|
|
|
const newBasePath = `${this.basePath}${subPath}`;
|
|
|
|
return new SmartRouter({ ...this.options, ...options }, newBasePath);
|
|
|
|
}
|
|
|
|
|
2022-01-22 12:29:23 +00:00
|
|
|
/**
|
|
|
|
* Push route state to history stack
|
|
|
|
*/
|
|
|
|
public async pushUrl(url: string = '/', state: any = {}) {
|
2024-10-06 18:15:21 +00:00
|
|
|
const fullUrl = `${this.basePath}${url}`;
|
|
|
|
if (fullUrl !== window.location.pathname) {
|
|
|
|
window.history.pushState(state, window.document.title, fullUrl);
|
2022-01-22 12:29:23 +00:00
|
|
|
} else {
|
2024-10-06 18:15:21 +00:00
|
|
|
window.history.replaceState(state, window.document.title, fullUrl);
|
2022-01-22 12:29:23 +00:00
|
|
|
}
|
|
|
|
await this._handleRouteState();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach route with handler
|
|
|
|
* @param {string|RegExp} routeArg
|
|
|
|
* @param {function} handlerArg
|
|
|
|
*/
|
|
|
|
public on(routeArg: string, handlerArg: THandlerFunction) {
|
2024-10-06 18:15:21 +00:00
|
|
|
const fullRoute = `${this.basePath}${routeArg}`;
|
2024-10-06 04:57:15 +00:00
|
|
|
const routeObject = {
|
2024-10-06 18:15:21 +00:00
|
|
|
matchFunction: plugins.pathToRegExp.match(fullRoute),
|
2022-01-22 12:29:23 +00:00
|
|
|
handler: handlerArg,
|
2024-10-06 04:57:15 +00:00
|
|
|
};
|
|
|
|
this.routes.push(routeObject);
|
|
|
|
const removeFunction = () => {
|
|
|
|
this.routes.splice(this.routes.indexOf(routeObject), 1);
|
|
|
|
};
|
|
|
|
return removeFunction;
|
2022-01-22 12:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply routes handler to current route
|
|
|
|
*/
|
|
|
|
async _handleRouteState() {
|
|
|
|
const currentLocation = window.location.pathname;
|
|
|
|
|
|
|
|
// lets find all wanted routes.
|
|
|
|
const wantedRoutes = this.routes.filter((routeArg) => {
|
|
|
|
return !!routeArg.matchFunction(currentLocation);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const wantedRoute of wantedRoutes) {
|
|
|
|
const routeResult = wantedRoute.matchFunction(currentLocation);
|
|
|
|
wantedRoute.handler({
|
|
|
|
...(routeResult.valueOf() as Object),
|
|
|
|
queryParams: this.queryParams.getAllAsObject(), // TODO check wether entries is supported in typings
|
|
|
|
} as IRouteInfo); // not waiting here
|
|
|
|
}
|
|
|
|
}
|
2024-10-06 20:27:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the router instance, removing all external references
|
|
|
|
*/
|
|
|
|
public destroy() {
|
|
|
|
// Remove the event listener for popstate
|
|
|
|
window.removeEventListener('popstate', this.popstateListener);
|
|
|
|
// Clear all routes
|
|
|
|
this.routes = [];
|
|
|
|
}
|
2024-10-06 04:57:15 +00:00
|
|
|
}
|