mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-11 04:19:25 +00:00
Remove history management from script.js and migrate sas albums to webpack
This commit is contained in:
40
core/static/webpack/utils/history.ts
Normal file
40
core/static/webpack/utils/history.ts
Normal file
@ -0,0 +1,40 @@
|
||||
export enum History {
|
||||
None = 0,
|
||||
Push = 1,
|
||||
Replace = 2,
|
||||
}
|
||||
|
||||
export const initialUrlParams = new URLSearchParams(window.location.search);
|
||||
export const getCurrentUrlParams = () => {
|
||||
return new URLSearchParams(window.location.search);
|
||||
};
|
||||
|
||||
export function updateQueryString(
|
||||
key: string,
|
||||
value?: string | string[],
|
||||
action?: History,
|
||||
url?: string,
|
||||
) {
|
||||
const historyAction = action ?? History.Replace;
|
||||
const ret = new URL(url ?? window.location.href);
|
||||
|
||||
if (value === undefined || value === null || value === "") {
|
||||
// If the value is null, undefined or empty => delete it
|
||||
ret.searchParams.delete(key);
|
||||
} else if (Array.isArray(value)) {
|
||||
ret.searchParams.delete(key);
|
||||
for (const v of value) {
|
||||
ret.searchParams.append(key, v);
|
||||
}
|
||||
} else {
|
||||
ret.searchParams.set(key, value);
|
||||
}
|
||||
|
||||
if (historyAction === History.Push) {
|
||||
window.history.pushState(null, "", ret.toString());
|
||||
} else if (historyAction === History.Replace) {
|
||||
window.history.replaceState(null, "", ret.toString());
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
Reference in New Issue
Block a user