Files
Elecciones-2025/Elecciones-Web/frontend/public/bootstrap.js

91 lines
3.3 KiB
JavaScript
Raw Normal View History

2025-09-03 14:35:18 -03:00
// frontend/public/bootstrap.js
2025-09-03 15:03:15 -03:00
(function () {
2025-09-03 14:35:18 -03:00
// El dominio donde se alojan los widgets
const WIDGETS_HOST = 'https://elecciones2025.eldia.com';
// Función para cargar dinámicamente un script
2025-09-03 13:49:35 -03:00
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.type = 'module';
script.src = src;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
2025-09-03 14:35:18 -03:00
// Función para cargar dinámicamente una hoja de estilos
2025-09-03 13:49:35 -03:00
function loadCSS(href) {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = href;
document.head.appendChild(link);
}
2025-09-03 14:35:18 -03:00
// Función principal
2025-09-03 13:49:35 -03:00
async function initWidgets() {
try {
2025-09-03 14:35:18 -03:00
// 1. Obtener el manifest.json para saber los nombres de archivo actuales
2025-09-03 13:49:35 -03:00
const response = await fetch(`${WIDGETS_HOST}/manifest.json`);
2025-09-03 14:05:06 -03:00
if (!response.ok) {
2025-09-03 14:35:18 -03:00
throw new Error('No se pudo cargar el manifest de los widgets.');
2025-09-03 14:05:06 -03:00
}
2025-09-03 13:49:35 -03:00
const manifest = await response.json();
2025-09-03 14:35:18 -03:00
// 2. Encontrar el punto de entrada principal (nuestro main.tsx)
2025-09-03 14:05:06 -03:00
const entryKey = Object.keys(manifest).find(key => manifest[key].isEntry);
if (!entryKey) {
throw new Error('No se encontró el punto de entrada en el manifest.');
}
2025-09-03 15:03:15 -03:00
2025-09-03 14:05:06 -03:00
const entry = manifest[entryKey];
2025-09-03 13:49:35 -03:00
const jsUrl = `${WIDGETS_HOST}/${entry.file}`;
2025-09-03 15:03:15 -03:00
2025-09-03 14:35:18 -03:00
// 3. Cargar el CSS si existe
2025-09-03 13:49:35 -03:00
if (entry.css && entry.css.length > 0) {
entry.css.forEach(cssFile => {
const cssUrl = `${WIDGETS_HOST}/${cssFile}`;
loadCSS(cssUrl);
});
}
2025-09-03 14:35:18 -03:00
// 4. Cargar el JS principal y esperar a que esté listo
2025-09-03 13:49:35 -03:00
await loadScript(jsUrl);
2025-09-03 15:03:15 -03:00
console.log('Bootstrap: Intentando llamar a render...');
if (window.EleccionesWidgets && typeof window.EleccionesWidgets.render === 'function') {
console.log('Bootstrap: La función render existe. Llamando ahora.');
window.EleccionesWidgets.render();
} else {
console.error('Bootstrap: La función render no se encontró en window.EleccionesWidgets.');
}
2025-09-03 14:35:18 -03:00
// 5. Una vez cargado, llamar a la función de renderizado
if (window.EleccionesWidgets && typeof window.EleccionesWidgets.render === 'function') {
2025-09-10 14:20:44 -03:00
console.log('Bootstrap: La función render existe. Llamando ahora.');
// Encontramos los contenedores aquí y pasamos sus props.
const widgetContainers = document.querySelectorAll('[data-elecciones-widget]');
widgetContainers.forEach(container => {
// 'dataset' es un objeto que contiene todos los atributos data-*
// container.dataset = { eleccionesWidget: 'mapa-municipios', focoMunicipio: 'LA PLATA' }
window.EleccionesWidgets.render(container, container.dataset);
});
2025-09-03 15:23:32 -03:00
} else {
console.error('Bootstrap: ERROR CRÍTICO - La función render() NO SE ENCONTRÓ en window.EleccionesWidgets.');
console.log('Bootstrap: Contenido de window.EleccionesWidgets:', window.EleccionesWidgets);
2025-09-03 14:35:18 -03:00
}
2025-09-03 13:49:35 -03:00
} catch (error) {
2025-09-03 14:35:18 -03:00
console.error('Error al inicializar los widgets de elecciones:', error);
2025-09-03 13:49:35 -03:00
}
}
2025-09-03 14:35:18 -03:00
2025-09-03 15:01:09 -03:00
if (document.readyState === 'loading') { // Aún cargando
document.addEventListener('DOMContentLoaded', initWidgets);
} else { // Ya cargado
initWidgets();
}
2025-09-03 14:35:18 -03:00
2025-09-03 13:49:35 -03:00
})();