smartrouter/ts/smartrouter.classes.queryparams.ts
2022-12-31 11:21:29 +01:00

36 lines
1.2 KiB
TypeScript

import * as plugins from './smartrouter.plugins.js';
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];
}
}