From 8492192e724695dee8ef11f3f91c5e6cce7e7e65 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sun, 6 Oct 2024 20:15:21 +0200 Subject: [PATCH] feat(core): Add support for base paths and sub-routers. --- changelog.md | 6 ++++++ ts/00_commitinfo_data.ts | 2 +- ts/smartrouter.classes.smartrouter.ts | 28 ++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/changelog.md b/changelog.md index 8fc6c0a..c02d68d 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## 2024-10-06 - 1.2.0 - feat(core) +Add support for base paths and sub-routers. + +- Added basePath feature to SmartRouter for handling base paths. +- Introduced createSubRouter method to create a sub-router with a specific prefix. + ## 2024-10-06 - 1.1.1 - fix(core) Remove SelectionDimension functionality diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 3055208..a4ccdf6 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartrouter', - version: '1.1.1', + version: '1.2.0', description: 'A JavaScript library providing routing capabilities for web applications.' } diff --git a/ts/smartrouter.classes.smartrouter.ts b/ts/smartrouter.classes.smartrouter.ts index 2e47951..cbac939 100644 --- a/ts/smartrouter.classes.smartrouter.ts +++ b/ts/smartrouter.classes.smartrouter.ts @@ -36,15 +36,21 @@ export class SmartRouter { handler: THandlerFunction; }> = []; + /** + * base path for this router + */ + private basePath: string; + /** * Creates an instance of Router. */ - constructor(optionsArg: IRouterOptions) { + constructor(optionsArg: IRouterOptions, basePath: string = '') { // lets set the router options this.options = { ...this.options, ...optionsArg, }; + this.basePath = basePath; // lets subscribe to route changes window.addEventListener('popstate', (popStateEventArg) => { @@ -53,14 +59,25 @@ export class SmartRouter { }); } + /** + * 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); + } + /** * Push route state to history stack */ public async pushUrl(url: string = '/', state: any = {}) { - if (url !== window.location.pathname) { - window.history.pushState(state, window.document.title, url); + const fullUrl = `${this.basePath}${url}`; + if (fullUrl !== window.location.pathname) { + window.history.pushState(state, window.document.title, fullUrl); } else { - window.history.replaceState(state, window.document.title, url); + window.history.replaceState(state, window.document.title, fullUrl); } await this._handleRouteState(); } @@ -71,8 +88,9 @@ export class SmartRouter { * @param {function} handlerArg */ public on(routeArg: string, handlerArg: THandlerFunction) { + const fullRoute = `${this.basePath}${routeArg}`; const routeObject = { - matchFunction: plugins.pathToRegExp.match(routeArg), + matchFunction: plugins.pathToRegExp.match(fullRoute), handler: handlerArg, }; this.routes.push(routeObject);