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 = `
`; for (const doc of documentation) { content += ` `; } if (publications.length) { content += ` `; for (const doc of publications) { content += ` `; } } content += `
Documentazione di archivio
TitoloLuogo di conservazioneDownload
${doc.title}${doc.conservationPlace} PDF
Pubblicazioni del progetto Carta Archeologica
TitoloAutoriDownload
${doc.title}${doc.authors} PDF
`; 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(`

${record.reference}

` ); }); } const bibliography = { citations: citations.trim().slice(0, -1), biblioElements } return bibliography; } /** * * @param {String} text - The content text from database * Parse marker strings (pseudo-shortcodes) and convert them * to Stimulus links */ Utils.parseMarkers = function(text) { const regex = /(?\[marker coords=\"(?[\d\s\.]+)\"\](?[\w\s\.;:\-]+)\[\/marker\])/mig; let matches = [...text.matchAll(regex)]; if (matches.length) { matches.forEach(match => { const replacement = ` ${match.groups.content}`; text = text.replace(match.groups.marker, replacement.trim()); }); } return text; } 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;