Use configuration object for load builders

This commit is contained in:
Antoine Bartuccio 2024-10-09 12:08:48 +02:00 committed by Bartuccio Antoine
parent 93eb09887e
commit 6f4e93bb76
4 changed files with 40 additions and 24 deletions

View File

@ -159,20 +159,25 @@ function createGraph(container, data, activeUserId) {
} }
/** /**
* Create a family graph of an user * @typedef FamilyGraphConfig
* @param {string} Base url for fetching the tree as a string * @param {string} apiUrl Base url for fetching the tree as a string
* @param {string} Id of the user to fetch the tree from * @param {string} activeUser Id of the user to fetch the tree from
* @param {number} Minimum tree depth for godfathers and godchildren * @param {number} depthMin Minimum tree depth for godfathers and godchildren
* @param {number} Maximum tree depth for godfathers and godchildren * @param {number} depthMax Maximum tree depth for godfathers and godchildren
**/ **/
window.loadFamilyGraph = (apiUrl, activeUser, depthMin, depthMax) => {
/**
* Create a family graph of an user
* @param {FamilyGraphConfig} Configuration
**/
window.loadFamilyGraph = (config) => {
document.addEventListener("alpine:init", () => { document.addEventListener("alpine:init", () => {
const defaultDepth = 2; const defaultDepth = 2;
function getInitialDepth(prop) { function getInitialDepth(prop) {
// biome-ignore lint/correctness/noUndeclaredVariables: defined by script.js // biome-ignore lint/correctness/noUndeclaredVariables: defined by script.js
const value = Number.parseInt(initialUrlParams.get(prop)); const value = Number.parseInt(initialUrlParams.get(prop));
if (Number.isNaN(value) || value < depthMin || value > depthMax) { if (Number.isNaN(value) || value < config.depthMin || value > config.depthMax) {
return defaultDepth; return defaultDepth;
} }
return value; return value;
@ -193,7 +198,7 @@ window.loadFamilyGraph = (apiUrl, activeUser, depthMin, depthMax) => {
}, 100); }, 100);
for (const param of ["godfathersDepth", "godchildrenDepth"]) { for (const param of ["godfathersDepth", "godchildrenDepth"]) {
this.$watch(param, async (value) => { this.$watch(param, async (value) => {
if (value < depthMin || value > depthMax) { if (value < config.depthMin || value > config.depthMax) {
return; return;
} }
// biome-ignore lint/correctness/noUndeclaredVariables: defined by script.js // biome-ignore lint/correctness/noUndeclaredVariables: defined by script.js
@ -207,7 +212,7 @@ window.loadFamilyGraph = (apiUrl, activeUser, depthMin, depthMax) => {
await this.reverseGraph(); await this.reverseGraph();
}); });
this.$watch("graphData", async () => { this.$watch("graphData", async () => {
await this.generateGraph(); this.generateGraph();
if (this.reverse) { if (this.reverse) {
await this.reverseGraph(); await this.reverseGraph();
} }
@ -243,15 +248,19 @@ window.loadFamilyGraph = (apiUrl, activeUser, depthMin, depthMax) => {
async fetchGraphData() { async fetchGraphData() {
this.graphData = await getGraphData( this.graphData = await getGraphData(
apiUrl, config.apiUrl,
this.godfathersDepth, this.godfathersDepth,
this.godchildrenDepth, this.godchildrenDepth,
); );
}, },
async generateGraph() { generateGraph() {
this.loading = true; this.loading = true;
this.graph = await createGraph($(this.$refs.graph), this.graphData, activeUser); this.graph = createGraph(
$(this.$refs.graph),
this.graphData,
config.activeUser,
);
this.loading = false; this.loading = false;
}, },
})); }));

View File

@ -27,10 +27,15 @@ import { showSaveFilePicker } from "native-file-system-adapter";
*/ */
/** /**
* Load user picture page with a nice download bar * @typedef PicturePageConfig
* @param {string} Url of the api endpoint to fetch pictures from the user * @param {string} apiUrl Url of the api endpoint to fetch pictures from the user
**/ **/
window.loadPicturePage = (apiUrl) => {
/**
* Load user picture page with a nice download bar
* @param {PicturePageConfig} Configuration
**/
window.loadPicturePage = (config) => {
document.addEventListener("alpine:init", () => { document.addEventListener("alpine:init", () => {
Alpine.data("user_pictures", () => ({ Alpine.data("user_pictures", () => ({
isDownloading: false, isDownloading: false,
@ -40,7 +45,7 @@ window.loadPicturePage = (apiUrl) => {
async init() { async init() {
// biome-ignore lint/correctness/noUndeclaredVariables: imported from script.json // biome-ignore lint/correctness/noUndeclaredVariables: imported from script.json
this.pictures = await fetchPaginated(apiUrl); this.pictures = await fetchPaginated(config.apiUrl);
this.albums = this.pictures.reduce((acc, picture) => { this.albums = this.pictures.reduce((acc, picture) => {
if (!acc[picture.album]) { if (!acc[picture.album]) {
acc[picture.album] = []; acc[picture.album] = [];

View File

@ -91,13 +91,13 @@
<script> <script>
window.addEventListener("DOMContentLoaded", () => { window.addEventListener("DOMContentLoaded", () => {
loadFamilyGraph( loadFamilyGraph({
"{{ api_url }}", apiUrl: "{{ api_url }}",
"{{ object.id }}", activeUser: "{{ object.id }}",
{{ depth_min }}, depthMin: {{ depth_min }},
{{ depth_max }}, depthMax: {{ depth_max }},
); });
}) });
</script> </script>
{% endblock %} {% endblock %}

View File

@ -62,7 +62,9 @@
{{ super() }} {{ super() }}
<script> <script>
window.addEventListener("DOMContentLoaded", () => { window.addEventListener("DOMContentLoaded", () => {
loadPicturePage("{{ url("api:pictures") }}" + "?users_identified={{ object.id }}"); loadPicturePage({
apiUrl: "{{ url("api:pictures") }}?users_identified={{ object.id }}"
});
}) })
</script> </script>
{% endblock script %} {% endblock script %}