fix(core): update
This commit is contained in:
100
ts/index.ts
100
ts/index.ts
@ -1,99 +1 @@
|
||||
import * as plugins from './smartrouter.plugins';
|
||||
|
||||
const routeLog = (message) => {
|
||||
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,
|
||||
};
|
||||
|
||||
/**
|
||||
* the routes we are handling
|
||||
*/
|
||||
public routes: Array<{
|
||||
matchFunction: plugins.pathToRegExp.MatchFunction;
|
||||
handler: THandlerFunction;
|
||||
}> = [];
|
||||
|
||||
/**
|
||||
* Creates an instance of Router.
|
||||
*/
|
||||
constructor(optionsArg: IRouterOptions) {
|
||||
// lets set the router options
|
||||
this.options = {
|
||||
...this.options,
|
||||
...optionsArg,
|
||||
};
|
||||
|
||||
// lets subscribe to route changes
|
||||
window.addEventListener('popstate', (popStateEventArg) => {
|
||||
popStateEventArg.preventDefault();
|
||||
this._handleRouteState();
|
||||
});
|
||||
window.addEventListener('DOMContentLoaded', () => {
|
||||
this._handleRouteState();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
} else {
|
||||
window.history.replaceState(state, window.document.title, url);
|
||||
}
|
||||
await this._handleRouteState();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach route with handler
|
||||
* @param {string|RegExp} routeArg
|
||||
* @param {function} handlerArg
|
||||
*/
|
||||
public on(routeArg: string, handlerArg: THandlerFunction) {
|
||||
this.routes.push({
|
||||
matchFunction: plugins.pathToRegExp.match(routeArg),
|
||||
handler: handlerArg,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply routes handler to current route
|
||||
*/
|
||||
async _handleRouteState() {
|
||||
const currentLocation = window.location.pathname;
|
||||
const urlSearchParams = new URLSearchParams(window.location.search);
|
||||
|
||||
// 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: Object.fromEntries((urlSearchParams as any).entries()), // TODO check wether entries is supported in typings
|
||||
} as IRouteInfo); // not waiting here
|
||||
}
|
||||
}
|
||||
}
|
||||
export * from './smartrouter.classes.smartrouter';
|
31
ts/smartrouter.classes.queryparams.ts
Normal file
31
ts/smartrouter.classes.queryparams.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import * as plugins from './smartrouter.plugins';
|
||||
|
||||
export class QueryParams {
|
||||
constructor() {}
|
||||
|
||||
public getAllAsObject() {
|
||||
const urlSearchParams = new URLSearchParams(window.location.search);
|
||||
return Object.fromEntries((urlSearchParams as any).entries());
|
||||
}
|
||||
|
||||
public setQueryParam(queryKeyArg: string, queryContentArg: string, pushOrReplaceArg: 'push' | 'replace' = 'replace') {
|
||||
var queryParams = new URLSearchParams(window.location.search);
|
||||
queryParams.set(queryKeyArg, queryContentArg);
|
||||
pushOrReplaceArg === 'push'
|
||||
? history.pushState(null, null, '?' + queryParams.toString())
|
||||
: history.replaceState(null, null, '?' + queryParams.toString());
|
||||
}
|
||||
|
||||
public deleteQueryParam(queryKeyArg: string, pushOrReplaceArg: 'push' | 'replace' = 'replace') {
|
||||
var queryParams = new URLSearchParams(window.location.search);
|
||||
queryParams.delete(queryKeyArg);
|
||||
pushOrReplaceArg === 'push'
|
||||
? history.pushState(null, null, '?' + queryParams.toString())
|
||||
: history.replaceState(null, null, '?' + queryParams.toString());
|
||||
}
|
||||
|
||||
public getQueryParam(queryParamName: string) {
|
||||
const queryParams = this.getAllAsObject();
|
||||
return queryParams[queryParamName];
|
||||
}
|
||||
}
|
105
ts/smartrouter.classes.smartrouter.ts
Normal file
105
ts/smartrouter.classes.smartrouter.ts
Normal file
@ -0,0 +1,105 @@
|
||||
import * as plugins from './smartrouter.plugins';
|
||||
|
||||
import { QueryParams } from './smartrouter.classes.queryparams';
|
||||
|
||||
const routeLog = (message) => {
|
||||
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<{
|
||||
matchFunction: plugins.pathToRegExp.MatchFunction;
|
||||
handler: THandlerFunction;
|
||||
}> = [];
|
||||
|
||||
/**
|
||||
* Creates an instance of Router.
|
||||
*/
|
||||
constructor(optionsArg: IRouterOptions) {
|
||||
// lets set the router options
|
||||
this.options = {
|
||||
...this.options,
|
||||
...optionsArg,
|
||||
};
|
||||
|
||||
// lets subscribe to route changes
|
||||
window.addEventListener('popstate', (popStateEventArg) => {
|
||||
popStateEventArg.preventDefault();
|
||||
this._handleRouteState();
|
||||
});
|
||||
window.addEventListener('DOMContentLoaded', () => {
|
||||
this._handleRouteState();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
} else {
|
||||
window.history.replaceState(state, window.document.title, url);
|
||||
}
|
||||
await this._handleRouteState();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach route with handler
|
||||
* @param {string|RegExp} routeArg
|
||||
* @param {function} handlerArg
|
||||
*/
|
||||
public on(routeArg: string, handlerArg: THandlerFunction) {
|
||||
this.routes.push({
|
||||
matchFunction: plugins.pathToRegExp.match(routeArg),
|
||||
handler: handlerArg,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user