@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
62 lines (61 loc) • 2.3 kB
JavaScript
export function updateConfigPaths(config) {
const baseApiUrl = getBaseApiUrl();
for (const translation of Object.values(config.languages.translations)) {
for (let i = 0; i < translation.length; ++i) {
const filepath = translation[i];
if (isRelativePath(filepath)) {
const absolutePath = new URL(filepath, baseApiUrl).href;
translation[i] = absolutePath;
}
}
}
}
export function updateRessourcesPaths(shadow) {
const baseApiUrl = getBaseApiUrl();
updateElements(shadow, baseApiUrl, 'img', 'src');
updateElements(shadow, baseApiUrl, 'link', 'href');
}
function updateElements(shadow, baseApiUrl, elementTagname, attributeName) {
const elements = shadow.querySelectorAll(`${elementTagname}[${attributeName}]`);
for (const element of elements) {
const path = element.getAttribute(attributeName);
if (path && isRelativePath(path)) {
const absolutePath = new URL(path, baseApiUrl).href;
element.setAttribute(attributeName, absolutePath);
}
}
}
function getBaseApiUrl() {
let baseApiUrl = import.meta.url;
// Example when imported locally in dev mode: 'https://app.localhost:8080/src/base/GirafeHTMLElement.ts?t=1782368741013'
// Example when imported from the published application: 'https://demo.geogirafe.org/mapbs/assets/component-UojAUdda.js'
// Exemple when imported as an api: https://app.localhost:4173/assets/component-UojAUdda.js
let index = baseApiUrl.indexOf('/src/');
if (index <= 0) {
index = baseApiUrl.indexOf('/assets/');
}
if (index > 0) {
baseApiUrl = baseApiUrl.substring(0, index + 1);
return baseApiUrl;
}
else {
throw new Error('API: Cannot calculate the baseUrl for importing assets.');
}
}
function isRelativePath(path) {
if (path.startsWith('http:'))
return false;
if (path.startsWith('https:'))
return false;
if (path.startsWith('data:'))
return false;
if (path.startsWith('blob:'))
return false;
if (path.startsWith('javascript:'))
return false;
if (path.startsWith('mailto:'))
return false;
if (path.startsWith('#'))
return false;
return true;
}