feat(core): Add support for base paths and sub-routers.
This commit is contained in:
parent
90466ff2b6
commit
8492192e72
@ -1,5 +1,11 @@
|
|||||||
# Changelog
|
# 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)
|
## 2024-10-06 - 1.1.1 - fix(core)
|
||||||
Remove SelectionDimension functionality
|
Remove SelectionDimension functionality
|
||||||
|
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartrouter',
|
name: '@push.rocks/smartrouter',
|
||||||
version: '1.1.1',
|
version: '1.2.0',
|
||||||
description: 'A JavaScript library providing routing capabilities for web applications.'
|
description: 'A JavaScript library providing routing capabilities for web applications.'
|
||||||
}
|
}
|
||||||
|
@ -36,15 +36,21 @@ export class SmartRouter {
|
|||||||
handler: THandlerFunction;
|
handler: THandlerFunction;
|
||||||
}> = [];
|
}> = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* base path for this router
|
||||||
|
*/
|
||||||
|
private basePath: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an instance of Router.
|
* Creates an instance of Router.
|
||||||
*/
|
*/
|
||||||
constructor(optionsArg: IRouterOptions) {
|
constructor(optionsArg: IRouterOptions, basePath: string = '') {
|
||||||
// lets set the router options
|
// lets set the router options
|
||||||
this.options = {
|
this.options = {
|
||||||
...this.options,
|
...this.options,
|
||||||
...optionsArg,
|
...optionsArg,
|
||||||
};
|
};
|
||||||
|
this.basePath = basePath;
|
||||||
|
|
||||||
// lets subscribe to route changes
|
// lets subscribe to route changes
|
||||||
window.addEventListener('popstate', (popStateEventArg) => {
|
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
|
* Push route state to history stack
|
||||||
*/
|
*/
|
||||||
public async pushUrl(url: string = '/', state: any = {}) {
|
public async pushUrl(url: string = '/', state: any = {}) {
|
||||||
if (url !== window.location.pathname) {
|
const fullUrl = `${this.basePath}${url}`;
|
||||||
window.history.pushState(state, window.document.title, url);
|
if (fullUrl !== window.location.pathname) {
|
||||||
|
window.history.pushState(state, window.document.title, fullUrl);
|
||||||
} else {
|
} else {
|
||||||
window.history.replaceState(state, window.document.title, url);
|
window.history.replaceState(state, window.document.title, fullUrl);
|
||||||
}
|
}
|
||||||
await this._handleRouteState();
|
await this._handleRouteState();
|
||||||
}
|
}
|
||||||
@ -71,8 +88,9 @@ export class SmartRouter {
|
|||||||
* @param {function} handlerArg
|
* @param {function} handlerArg
|
||||||
*/
|
*/
|
||||||
public on(routeArg: string, handlerArg: THandlerFunction) {
|
public on(routeArg: string, handlerArg: THandlerFunction) {
|
||||||
|
const fullRoute = `${this.basePath}${routeArg}`;
|
||||||
const routeObject = {
|
const routeObject = {
|
||||||
matchFunction: plugins.pathToRegExp.match(routeArg),
|
matchFunction: plugins.pathToRegExp.match(fullRoute),
|
||||||
handler: handlerArg,
|
handler: handlerArg,
|
||||||
};
|
};
|
||||||
this.routes.push(routeObject);
|
this.routes.push(routeObject);
|
||||||
|
Loading…
Reference in New Issue
Block a user