From 77d42a2c27d8e0ed0a56041a3467fbf8e1cdda3c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=B2=20P?=
Date: Mon, 1 Sep 2025 17:20:47 +0200
Subject: [PATCH] Refactor components (WIP)
---
webgis/index.html | 36 ++++--
webgis/js/components/Finding.js | 61 ++-------
webgis/js/components/NotConserved.js | 109 ++--------------
webgis/js/components/Underwater.js | 16 ++-
webgis/js/components/utils.js | 152 +++++++++++++++++++++++
webgis/js/controllers/menu_controller.js | 3 -
webgis/js/ui.js | 2 +
7 files changed, 214 insertions(+), 165 deletions(-)
create mode 100644 webgis/js/components/utils.js
diff --git a/webgis/index.html b/webgis/index.html
index 37fef7a..f2f481d 100644
--- a/webgis/index.html
+++ b/webgis/index.html
@@ -462,12 +462,36 @@
-
-
+
@@ -477,9 +501,7 @@
-
-
+
Scheda
diff --git a/webgis/js/components/Finding.js b/webgis/js/components/Finding.js
index 9a84eeb..f61818c 100644
--- a/webgis/js/components/Finding.js
+++ b/webgis/js/components/Finding.js
@@ -1,4 +1,4 @@
-import { GisState } from "../state.js";
+import Utils from "./utils.js";
/**
* @class Finding
*/
@@ -50,64 +50,25 @@ export class Finding {
`;
}
-
- renderImages() {
- let content = `
-
Immagini
`;
- content += `
-
-
Cliccare sull'immagine per aprire la gallery
-
-
-
-
-
-
-
-
`;
-
- return content;
- }
/**
* @param {HTMLElement} imageContainer
* @param {Function} gallery
*/
async setImages(imageContainer, gallery) {
- let record = await this.fetchData(`${GisState.apiUrl}/finding/${this._data.id}`)
-
- if (record.images.length) {
- this.images = record.images;
- imageContainer.innerHTML = this.renderImages();
- gallery('finding-gallery', this.images);
- }
+ if (this._data.images?.length) {
+ imageContainer.innerHTML = Utils.renderImages('finding-gallery', this._data.images);
+ gallery('finding-gallery', this._data.images);
+ } else
+ imageContainer.innerHTML = '
Nessuna risorsa visuale disponibile
';
}
/**
* @param {number} recordId
*/
async biblio(recordId) {
- let finding = await this.fetchData(`${GisState.apiUrl}/finding/${recordId}`);
+ let {citations, biblioElements} = await Utils.buildBibliography('not_conserved', recordId);
+ this.biblioElements = biblioElements;
- let citations = '';
-
- if (finding.bibliography.length) {
- finding.bibliography.forEach(record => {
- citations += `
-
- ${record.citation}`;
-
- citations += record.pages?.length ? `, ${record.pages};` : ';';
-
- this.biblioElements.push(`
-
- `);
- });
- }
-
- return citations.trim().slice(0, -1);
+ return citations;
}
getReference(id) {
@@ -116,8 +77,4 @@ export class Finding {
return ref.match(regex);
});
}
-
- async fetchData(url) {
- return await fetch(url).then(res => res.json());
- }
}
diff --git a/webgis/js/components/NotConserved.js b/webgis/js/components/NotConserved.js
index a991a9b..bc40d9d 100644
--- a/webgis/js/components/NotConserved.js
+++ b/webgis/js/components/NotConserved.js
@@ -1,4 +1,4 @@
-import { GisState } from "../state.js";
+import Utils from "./utils.js";
/**
* Component to render data for not conserved assets
@@ -51,116 +51,27 @@ export class NotConserved {
`;
}
- renderImages() {
- let content = `
-
Immagini
`;
- content += `
-
-
Cliccare sull'immagine per aprire la gallery
-
-
-
-
-
-
-
-
`;
-
- return content;
- }
/**
* @param {HTMLElement} imageContainer
* @param {Function} gallery
*/
- async setImages(imageContainer, gallery) {
+ setImages(imageContainer, gallery) {
if (this._data.images.length) {
- this.images = this._data.images;
- imageContainer.innerHTML = this.renderImages();
- gallery('not-conserved-gallery', this.images);
+ imageContainer.innerHTML = Utils.renderImages('not-conserved-gallery', this._data.images);
+ gallery('not-conserved-gallery', this._data.images);
} else
imageContainer.innerHTML = 'Nessuna risorsa visuale disponibile
';
}
async renderDocs() {
- let record = await this.fetchData(`${GisState.apiUrl}/not_conserved/${this._data.id}`);
-
- this.documentation = record.documents.filter(d => d.type === 'documentazione')
- this.publications = record.documents.filter(d => d.type === 'pubblicazione');
-
- let content = `
-
-
-
-
- Documentazione di archivio |
-
---|
Titolo | Luogo di conservazione | Download |
-
-
- `;
-
- for (const doc of this.documentation) {
- content += `
- ${doc.title} | ${doc.conservationPlace} |
- PDF
- |
- `;
- }
- if (this.publications.length) {
- content += `
-
-
- Pubblicazioni del progetto Carta Archeologica |
-
---|
Titolo | Autori | Download |
-
-
- `;
- for (const doc of this.publications) {
- content += `
- ${doc.title} | ${doc.authors} |
- PDF
- |
- `;
- }
- }
- content += `
-
-
-
-
- `;
-
- if (!this.publications.length && !this.documentation.length) {
- content = 'Nessun documento disponibile.
';
- }
-
- return content;
+ return await Utils.generateDocsTable(this._data, 'not_conserved');
}
async biblio(recordId) {
- let record = await this.fetchData(`${GisState.apiUrl}/not_conserved/${recordId}`);
+ let {citations, biblioElements} = await Utils.buildBibliography('not_conserved', recordId);
+ this.biblioElements = biblioElements;
- let citations = '';
-
- if (record.bibliography.length) {
- record.bibliography.forEach(record => {
- citations += `
-
- ${record.citation.trim()}`;
-
- citations += record.pages?.length ? `, ${record.pages};` : ';';
-
- this.biblioElements.push(`
-
- `
- );
- });
- }
-
- return citations.trim().slice(0, -1);
+ return citations;
}
getReference(id) {
@@ -169,8 +80,4 @@ export class NotConserved {
return ref.match(regex);
});
}
-
- async fetchData(url) {
- return await fetch(url).then(res => res.json());
- }
}
diff --git a/webgis/js/components/Underwater.js b/webgis/js/components/Underwater.js
index c6e0261..70983dd 100644
--- a/webgis/js/components/Underwater.js
+++ b/webgis/js/components/Underwater.js
@@ -1,3 +1,4 @@
+import Utils from "./utils.js";
/**
* @class Underwater
*/
@@ -34,8 +35,19 @@ export class Underwater {
`;
}
+ /**
+ * @param {HTMLElement} imageContainer
+ * @param {Function} gallery
+ */
+ setImages(imageContainer, gallery) {
+ if (this._data.images?.length) {
+ imageContainer.innerHTML = Utils.renderImages('underwater-gallery', this._data.images);
+ gallery('underwater-gallery', this._data.images);
+ } else
+ imageContainer.innerHTML = 'Nessuna risorsa visuale disponibile
';
+ }
- async fetchData(url) {
- return await fetch(url).then(res => res.json());
+ async renderDocs() {
+ return await Utils.generateDocsTable(this._data, 'underwater');
}
}
diff --git a/webgis/js/components/utils.js b/webgis/js/components/utils.js
new file mode 100644
index 0000000..fc940df
--- /dev/null
+++ b/webgis/js/components/utils.js
@@ -0,0 +1,152 @@
+import { GisState } from "../state.js";
+/**
+ * @namespace Utils
+ */
+const Utils = {};
+
+
+/**
+ *
+ * @param {string} galleryId The image gallery's id
+ * @param {Object} imagesData
+ * @returns {string}
+ */
+Utils.renderImages = function (galleryId, imagesData) {
+ let content = `
+
Immagini
`;
+ content += `
+
+
Cliccare sull'immagine per aprire la gallery
+
+
+
+
+
+
+
+
`;
+
+ return content;
+}
+/**
+ * @param {Object} imagesData
+ * @param {HTMLElement} imageContainer
+ * @param {Function} galleryGenerator The function that creates the image gallery
+ * @param {string} galleryId The image gallery's id
+ */
+Utils.setImages = function(imagesData, imageContainer, galleryGenerator, galleryId) {
+ imageContainer.innerHTML = Utils.renderImages(galleryId);
+ galleryGenerator(galleryId, imagesData);
+}
+
+/**
+ *
+ * @param {Object} data The component's data
+ * @param {String} resourceUri The resource URI to be used for API calls
+ * @returns {String} The table HTML
+ */
+Utils.generateDocsTable = async function(data, resourceUri) {
+ let record = await Utils.fetchData(`${GisState.apiUrl}/${resourceUri}/${data.id}`);
+
+ // TODO Horrible??
+ if (record instanceof Error) return 'Nessun documento disponibile.
';
+
+ const documentation = record.documents.filter(d => d.type === 'documentazione')
+ const publications = record.documents.filter(d => d.type === 'pubblicazione');
+
+ let content = `
+
+
+
+
+ Documentazione di archivio |
+
---|
Titolo | Luogo di conservazione | Download |
+
+
+ `;
+
+ for (const doc of documentation) {
+ content += `
+ ${doc.title} | ${doc.conservationPlace} |
+ PDF
+ |
+ `;
+ }
+ if (publications.length) {
+ content += `
+
+
+ Pubblicazioni del progetto Carta Archeologica |
+
---|
Titolo | Autori | Download |
+
+
+ `;
+ for (const doc of publications) {
+ content += `
+ ${doc.title} | ${doc.authors} |
+ PDF
+ |
+ `;
+ }
+ }
+ content += `
+
+
+
+
+ `;
+
+ if (publications.length === 0 && documentation.length === 0) {
+ content = 'Nessun documento disponibile.
';
+ }
+
+ return content;
+}
+
+/**
+ *
+ * @param {String} recordUri The record URI used for API calls
+ * @param {Number} recordId This record's ID
+ * @returns {{citations:String,biblioElements:String[]}}
+ */
+Utils.buildBibliography = async function(recordUri, recordId) {
+ let record = await Utils.fetchData(`${GisState.apiUrl}/${recordUri}/${recordId}`);
+ let biblioElements = [];
+
+ let citations = '';
+
+ if (record.bibliography.length) {
+ record.bibliography.forEach(record => {
+ citations += `
+
+ ${record.citation.trim()}
+
+ `;
+
+ citations += record.pages?.length ? `, ${record.pages};` : ';';
+
+ biblioElements.push(`
+
+ `
+ );
+ });
+ }
+
+ const bibliography = {
+ citations: citations.trim().slice(0, -1),
+ biblioElements
+ }
+
+ return bibliography;
+}
+
+Utils.fetchData = async function(url) {
+ return await fetch(url).then(res => res.ok ? res.json() : new Error())
+ .catch(err => console.log(err));
+}
+
+export default Utils;
\ No newline at end of file
diff --git a/webgis/js/controllers/menu_controller.js b/webgis/js/controllers/menu_controller.js
index b47729b..6ab18f8 100644
--- a/webgis/js/controllers/menu_controller.js
+++ b/webgis/js/controllers/menu_controller.js
@@ -24,9 +24,6 @@ export default class extends Controller {
let ul = this.renderMenuItems(group, municipality);
document.querySelector(`[data-list-id="${group}-${municipality.toLowerCase()}-sub"]`)
?.appendChild(ul);
-
- if (group === 'reuse' && municipality === 'Capri') console.log(ul);
-
}
}
}
diff --git a/webgis/js/ui.js b/webgis/js/ui.js
index 2bcd888..9478fc2 100644
--- a/webgis/js/ui.js
+++ b/webgis/js/ui.js
@@ -212,6 +212,8 @@ UI.openUnderwaterModal = function (data, selector) {
// For Stimulus biblio_controller
//GisState.bibliography = underwater;
underwater.render().then(html => modal.querySelector('#underwater-sheet').innerHTML = html);
+ underwater.renderDocs().then(html => modal.querySelector('#documents').innerHTML = html);
+ underwater.setImages(modal.querySelector('#photos'), this.imageGallery);
modal.classList.add('is-active');
}
/**