mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
Remove history management from script.js and migrate sas albums to webpack
This commit is contained in:
@ -74,52 +74,3 @@ function displayNotif() {
|
||||
function getCSRFToken() {
|
||||
return $("[name=csrfmiddlewaretoken]").val();
|
||||
}
|
||||
|
||||
// biome-ignore lint/correctness/noUnusedVariables: used in other scripts
|
||||
const initialUrlParams = new URLSearchParams(window.location.search);
|
||||
|
||||
/**
|
||||
* @readonly
|
||||
* @enum {number}
|
||||
*/
|
||||
const History = {
|
||||
// biome-ignore lint/style/useNamingConvention: this feels more like an enum
|
||||
NONE: 0,
|
||||
// biome-ignore lint/style/useNamingConvention: this feels more like an enum
|
||||
PUSH: 1,
|
||||
// biome-ignore lint/style/useNamingConvention: this feels more like an enum
|
||||
REPLACE: 2,
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @param {string | string[] | null} value
|
||||
* @param {History} action
|
||||
* @param {URL | null} url
|
||||
*/
|
||||
// biome-ignore lint/correctness/noUnusedVariables: used in other scripts
|
||||
function updateQueryString(key, value, action = History.REPLACE, url = null) {
|
||||
let ret = url;
|
||||
if (!ret) {
|
||||
ret = new 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 (action === History.PUSH) {
|
||||
window.history.pushState(null, "", ret.toString());
|
||||
} else if (action === History.REPLACE) {
|
||||
window.history.replaceState(null, "", ret.toString());
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { History, initialUrlParams, updateQueryString } from "#core:utils/history";
|
||||
import cytoscape from "cytoscape";
|
||||
import cxtmenu from "cytoscape-cxtmenu";
|
||||
import klay from "cytoscape-klay";
|
||||
@ -184,7 +185,6 @@ window.loadFamilyGraph = (config) => {
|
||||
const defaultDepth = 2;
|
||||
|
||||
function getInitialDepth(prop) {
|
||||
// biome-ignore lint/correctness/noUndeclaredVariables: defined by script.js
|
||||
const value = Number.parseInt(initialUrlParams.get(prop));
|
||||
if (Number.isNaN(value) || value < config.depthMin || value > config.depthMax) {
|
||||
return defaultDepth;
|
||||
@ -196,7 +196,6 @@ window.loadFamilyGraph = (config) => {
|
||||
loading: false,
|
||||
godfathersDepth: getInitialDepth("godfathersDepth"),
|
||||
godchildrenDepth: getInitialDepth("godchildrenDepth"),
|
||||
// biome-ignore lint/correctness/noUndeclaredVariables: defined by script.js
|
||||
reverse: initialUrlParams.get("reverse")?.toLowerCase?.() === "true",
|
||||
graph: undefined,
|
||||
graphData: {},
|
||||
@ -210,14 +209,12 @@ window.loadFamilyGraph = (config) => {
|
||||
if (value < config.depthMin || value > config.depthMax) {
|
||||
return;
|
||||
}
|
||||
// biome-ignore lint/correctness/noUndeclaredVariables: defined by script.js
|
||||
updateQueryString(param, value, History.REPLACE);
|
||||
updateQueryString(param, value, History.Replace);
|
||||
await delayedFetch();
|
||||
});
|
||||
}
|
||||
this.$watch("reverse", async (value) => {
|
||||
// biome-ignore lint/correctness/noUndeclaredVariables: defined by script.js
|
||||
updateQueryString("reverse", value, History.REPLACE);
|
||||
updateQueryString("reverse", value, History.Replace);
|
||||
await this.reverseGraph();
|
||||
});
|
||||
this.$watch("graphData", async () => {
|
||||
|
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