smartrouter/ts/smartrouter.classes.queryparams.ts

36 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2022-12-31 10:21:29 +00:00
import * as plugins from './smartrouter.plugins.js';
2022-01-22 12:29:23 +00:00
export class QueryParams {
constructor() {}
public getAllAsObject() {
const urlSearchParams = new URLSearchParams(window.location.search);
return Object.fromEntries((urlSearchParams as any).entries());
}
2022-12-31 10:21:29 +00:00
public setQueryParam(
queryKeyArg: string,
queryContentArg: string,
pushOrReplaceArg: 'push' | 'replace' = 'replace'
) {
2022-01-22 12:29:23 +00:00
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];
}
}