Spaces:
Running
Running
Update js/proa.js
Browse files- js/proa.js +124 -181
js/proa.js
CHANGED
|
@@ -1,265 +1,210 @@
|
|
| 1 |
-
// js/proa.js - Lógica para el visor de Guías PROA
|
| 2 |
|
| 3 |
// ======================================================================
|
| 4 |
// BLOCK START: DOM Elements Selection & Initial Setup
|
| 5 |
// ======================================================================
|
| 6 |
-
console.log("[proa.js] Script cargado. Esperando DOMContentLoaded...");
|
| 7 |
|
| 8 |
-
// Usamos DOMContentLoaded para asegurarnos que el HTML está completamente cargado y parseado
|
| 9 |
window.addEventListener('DOMContentLoaded', () => {
|
| 10 |
-
console.log("[proa.js] DOMContentLoaded detectado. Iniciando setup...");
|
| 11 |
|
| 12 |
-
|
| 13 |
-
const
|
| 14 |
-
const
|
| 15 |
-
const togglePediatricCheckbox = document.getElementById('togglePediatric'); // Checkbox para filtrar guías pediátricas
|
| 16 |
-
|
| 17 |
-
// URL del archivo que contiene el índice principal de las guías (el index.html con los enlaces a cada guía)
|
| 18 |
-
// Asegúrate de que este archivo esté en la raíz de tu proyecto en Hugging Face
|
| 19 |
-
const guideIndexUrl = 'proa_guide_index.html';
|
| 20 |
|
| 21 |
-
//
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
// ======================================================================
|
| 29 |
// BLOCK END: DOM Elements Selection & Initial Setup
|
| 30 |
// ======================================================================
|
| 31 |
|
| 32 |
// ======================================================================
|
| 33 |
-
// BLOCK START: State
|
| 34 |
// ======================================================================
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
let allGuidesData = [];
|
| 38 |
// ======================================================================
|
| 39 |
-
// BLOCK END: State
|
| 40 |
// ======================================================================
|
| 41 |
|
| 42 |
// ======================================================================
|
| 43 |
-
// BLOCK START: Function to Fetch
|
| 44 |
// ======================================================================
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
try {
|
| 51 |
-
|
| 52 |
-
// NOTA: Requiere que la página se sirva desde un servidor (http:// o https://),
|
| 53 |
-
// no funcionará correctamente si se abre como archivo local (file:///) por CORS.
|
| 54 |
-
const response = await fetch(guideIndexUrl);
|
| 55 |
-
|
| 56 |
-
// Verificar si la carga fue exitosa (status 200 OK)
|
| 57 |
if (!response.ok) {
|
| 58 |
-
throw new Error(`Error HTTP ${response.status} al cargar ${
|
| 59 |
}
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
// Usar DOMParser para convertir el texto HTML en un documento DOM manipulable
|
| 66 |
-
const parser = new DOMParser();
|
| 67 |
-
const doc = parser.parseFromString(htmlText, 'text/html');
|
| 68 |
-
|
| 69 |
-
// Buscar todos los enlaces ('a') que tengan la clase 'category-card' dentro del div con clase 'grid'
|
| 70 |
-
// (Basado en la estructura de tu archivo index.html de guías)
|
| 71 |
-
const guideLinks = doc.querySelectorAll('.grid a.category-card');
|
| 72 |
-
console.log(`[proa.js] Parseo completado. Encontrados ${guideLinks.length} enlaces de guías en el índice.`);
|
| 73 |
-
|
| 74 |
-
allGuidesData = []; // Limpiar datos previos por si se recarga
|
| 75 |
-
|
| 76 |
-
// Iterar sobre cada enlace encontrado para extraer la información
|
| 77 |
-
guideLinks.forEach((link, index) => {
|
| 78 |
-
const url = link.getAttribute('href'); // Obtener el archivo HTML de la guía (ej. "digestivo.html")
|
| 79 |
-
const titleElement = link.querySelector('.category-text'); // Elemento que contiene el título
|
| 80 |
-
|
| 81 |
-
// Extraer el título base y verificar si tiene la etiqueta pediátrica
|
| 82 |
-
let title = 'Título Desconocido';
|
| 83 |
-
let isPediatric = false;
|
| 84 |
-
|
| 85 |
-
if (titleElement) {
|
| 86 |
-
// Clonamos el nodo para poder manipularlo sin afectar al original durante el proceso
|
| 87 |
-
const titleClone = titleElement.cloneNode(true);
|
| 88 |
-
// Buscamos y eliminamos la etiqueta pediátrica si existe DENTRO del clon
|
| 89 |
-
const pediatricTag = titleClone.querySelector('.pediatric-tag');
|
| 90 |
-
if (pediatricTag) {
|
| 91 |
-
isPediatric = true;
|
| 92 |
-
pediatricTag.remove(); // Eliminar la etiqueta del clon para obtener solo el título base
|
| 93 |
-
}
|
| 94 |
-
title = titleClone.textContent.trim(); // Obtener el texto limpio
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
// Si tenemos URL y un título válido, guardar los datos
|
| 98 |
-
if (url && title !== 'Título Desconocido') {
|
| 99 |
-
allGuidesData.push({
|
| 100 |
-
title: title, // Nombre limpio de la guía
|
| 101 |
-
url: url, // Archivo HTML de la guía
|
| 102 |
-
isPediatric: isPediatric // Booleano indicando si es pediátrica
|
| 103 |
-
});
|
| 104 |
-
} else {
|
| 105 |
-
console.warn(`[proa.js] Enlace de guía #${index+1} omitido por falta de URL o título claro:`, link.outerHTML);
|
| 106 |
-
}
|
| 107 |
-
});
|
| 108 |
|
| 109 |
-
console.log(
|
| 110 |
-
|
| 111 |
-
// Una vez extraídos los datos, llamar a la función para mostrar la lista en la UI
|
| 112 |
-
renderGuideList();
|
| 113 |
|
| 114 |
} catch (error) {
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
<p class="text-red-600 font-semibold">Error al cargar lista de guías.</p>
|
| 119 |
<p class="text-sm text-gray-600 mt-1">Detalle: ${error.message}.</p>
|
| 120 |
-
<p class="text-xs text-gray-500 mt-2">
|
| 121 |
-
|
| 122 |
-
guideContentArea.innerHTML = '<p class="text-gray-500 text-center mt-10">Error al cargar guías.</p>';
|
| 123 |
}
|
| 124 |
}
|
| 125 |
// ======================================================================
|
| 126 |
-
// BLOCK END: Function to Fetch
|
| 127 |
// ======================================================================
|
| 128 |
|
| 129 |
// ======================================================================
|
| 130 |
-
// BLOCK START: Function to Render Guide
|
| 131 |
// ======================================================================
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
guideListContainer.innerHTML = ''; // Limpiar el contenedor antes de añadir nuevos elementos
|
| 136 |
-
|
| 137 |
-
// Verificar el estado del checkbox para saber si mostrar las guías pediátricas
|
| 138 |
const showPediatric = togglePediatricCheckbox.checked;
|
| 139 |
-
console.log(`[proa.js] Filtro pediátrico activado: ${showPediatric}`);
|
| 140 |
|
| 141 |
-
|
| 142 |
-
const filteredGuides = allGuidesData.filter(guide => {
|
| 143 |
-
// Mantener la guía si NO es pediátrica, O si ES pediátrica Y el checkbox está marcado
|
| 144 |
-
return !guide.isPediatric || showPediatric;
|
| 145 |
-
});
|
| 146 |
|
| 147 |
-
// Si no hay guías tras filtrar, mostrar un mensaje
|
| 148 |
if (filteredGuides.length === 0) {
|
| 149 |
-
|
| 150 |
-
console.log("[proa.js] No hay guías para mostrar con el filtro actual.");
|
| 151 |
return;
|
| 152 |
}
|
| 153 |
|
| 154 |
-
// Crear
|
| 155 |
filteredGuides.forEach(guide => {
|
| 156 |
-
const
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
// Si la guía es pediátrica, añadir la clase y la etiqueta visual
|
| 163 |
if (guide.isPediatric) {
|
| 164 |
-
listItem.classList.add('pediatric'); // Clase para posible estilo específico
|
| 165 |
const tag = document.createElement('span');
|
| 166 |
-
tag.className = 'pediatric-tag-inline';
|
| 167 |
tag.textContent = 'PED';
|
| 168 |
-
|
| 169 |
}
|
| 170 |
|
| 171 |
-
//
|
| 172 |
-
|
| 173 |
-
event.preventDefault();
|
| 174 |
-
console.log(`[proa.js] Clic en guía: ${guide.title} (${guide.
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
});
|
| 177 |
|
| 178 |
-
|
| 179 |
});
|
| 180 |
-
console.log(`[proa.js]
|
| 181 |
}
|
| 182 |
// ======================================================================
|
| 183 |
-
// BLOCK END: Function to Render Guide
|
| 184 |
// ======================================================================
|
| 185 |
|
| 186 |
// ======================================================================
|
| 187 |
-
// BLOCK START: Function to Load Guide Content (
|
| 188 |
// ======================================================================
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
try {
|
| 196 |
-
|
| 197 |
-
// NOTA: También requiere ejecución desde servidor (http:// o https://) por CORS.
|
| 198 |
-
const response = await fetch(guideUrl);
|
| 199 |
if (!response.ok) {
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
}
|
| 202 |
-
|
| 203 |
-
// Obtener el contenido HTML como texto
|
| 204 |
const htmlText = await response.text();
|
| 205 |
-
console.log(`[proa.js] Contenido HTML de ${
|
| 206 |
|
| 207 |
-
// Parsear el HTML de la guía
|
| 208 |
const parser = new DOMParser();
|
| 209 |
const doc = parser.parseFromString(htmlText, 'text/html');
|
| 210 |
|
| 211 |
-
//
|
| 212 |
-
//
|
| 213 |
-
// Basado en tus archivos, parece ser: <div class="max-w-4xl mx-auto px-4 py-8">
|
| 214 |
const contentNode = doc.querySelector('.max-w-4xl.mx-auto.px-4.py-8');
|
|
|
|
| 215 |
|
| 216 |
if (contentNode) {
|
| 217 |
-
// Clonamos el nodo para asegurar que no modificamos el documento parseado original
|
| 218 |
const clonedContent = contentNode.cloneNode(true);
|
| 219 |
-
|
| 220 |
-
// **Importante:** Eliminar los scripts internos de las guías cargadas.
|
| 221 |
-
// Tus guías tienen un script al final para manejar su propio selector interno.
|
| 222 |
-
// Si no lo eliminamos, podría interferir o dar errores al cargarlo aquí.
|
| 223 |
const scripts = clonedContent.querySelectorAll('script');
|
| 224 |
scripts.forEach(script => script.remove());
|
| 225 |
-
if (scripts.length > 0) {
|
| 226 |
-
console.log(`[proa.js] Eliminados ${scripts.length} script(s) internos de la guía cargada.`);
|
| 227 |
-
}
|
| 228 |
|
| 229 |
-
// Limpiar
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
|
| 234 |
} else {
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
throw new Error("Estructura interna del archivo de guía no reconocida.");
|
| 238 |
}
|
| 239 |
|
| 240 |
} catch (error) {
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
}
|
| 248 |
}
|
| 249 |
// ======================================================================
|
| 250 |
-
// BLOCK END: Function to Load Guide Content
|
| 251 |
// ======================================================================
|
| 252 |
|
| 253 |
// ======================================================================
|
| 254 |
// BLOCK START: Event Listeners Setup
|
| 255 |
// ======================================================================
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
// const btnConfigProa = document.getElementById('btnConfigProa'); ... etc ...
|
| 263 |
// ======================================================================
|
| 264 |
// BLOCK END: Event Listeners Setup
|
| 265 |
// ======================================================================
|
|
@@ -267,14 +212,12 @@ window.addEventListener('DOMContentLoaded', () => {
|
|
| 267 |
// ======================================================================
|
| 268 |
// BLOCK START: Initial Execution
|
| 269 |
// ======================================================================
|
| 270 |
-
//
|
| 271 |
-
|
| 272 |
-
loadGuideIndex();
|
| 273 |
-
console.log("[proa.js] Llamada inicial a loadGuideIndex() realizada.");
|
| 274 |
// ======================================================================
|
| 275 |
// BLOCK END: Initial Execution
|
| 276 |
// ======================================================================
|
| 277 |
|
| 278 |
-
}); // Fin
|
| 279 |
|
| 280 |
-
console.log("[proa.js] Script completamente definido.");
|
|
|
|
| 1 |
+
// js/proa.js - Lógica para el visor de Guías PROA (v3 - Basado en JSON y Carga Externa)
|
| 2 |
|
| 3 |
// ======================================================================
|
| 4 |
// BLOCK START: DOM Elements Selection & Initial Setup
|
| 5 |
// ======================================================================
|
| 6 |
+
console.log("[proa.js v3] Script cargado. Esperando DOMContentLoaded...");
|
| 7 |
|
|
|
|
| 8 |
window.addEventListener('DOMContentLoaded', () => {
|
| 9 |
+
console.log("[proa.js v3] DOMContentLoaded detectado. Iniciando setup...");
|
| 10 |
|
| 11 |
+
const guideIndexContainer = document.getElementById('guide-index-container'); // Div donde irá la lista dinámica
|
| 12 |
+
const guideContentDisplay = document.getElementById('guide-content-display'); // Div donde se mostrará el contenido
|
| 13 |
+
const togglePediatricCheckbox = document.getElementById('togglePediatric'); // Checkbox de filtro
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
// --- Ruta al archivo JSON de datos ---
|
| 16 |
+
const guidesDataUrl = 'guides_data.json'; // Asume que está en la misma carpeta (proa/) que proa.html
|
| 17 |
+
// ------------------------------------
|
| 18 |
+
|
| 19 |
+
// Verificación de elementos
|
| 20 |
+
if (!guideIndexContainer || !guideContentDisplay || !togglePediatricCheckbox) {
|
| 21 |
+
console.error("[proa.js v3] Error Crítico: No se encontraron elementos DOM (contenedor índice, display o checkbox).");
|
| 22 |
+
if(guideIndexContainer) guideIndexContainer.innerHTML = '<p class="text-red-500 font-semibold">Error: Interfaz no inicializada.</p>';
|
| 23 |
+
return;
|
| 24 |
}
|
| 25 |
// ======================================================================
|
| 26 |
// BLOCK END: DOM Elements Selection & Initial Setup
|
| 27 |
// ======================================================================
|
| 28 |
|
| 29 |
// ======================================================================
|
| 30 |
+
// BLOCK START: State Variables
|
| 31 |
// ======================================================================
|
| 32 |
+
let allGuides = []; // Almacenará los datos del JSON: [{ id, title, isPediatric, file }, ...]
|
| 33 |
+
let currentGuideUrl = null; // Para saber qué guía está activa
|
|
|
|
| 34 |
// ======================================================================
|
| 35 |
+
// BLOCK END: State Variables
|
| 36 |
// ======================================================================
|
| 37 |
|
| 38 |
// ======================================================================
|
| 39 |
+
// BLOCK START: Function to Fetch Guides Data (from JSON)
|
| 40 |
// ======================================================================
|
| 41 |
+
async function loadGuidesData() {
|
| 42 |
+
console.log(`[proa.js v3] Cargando datos de guías desde: ${guidesDataUrl}`);
|
| 43 |
+
guideIndexContainer.innerHTML = '<p class="text-gray-500 text-sm">Cargando guías...</p>';
|
| 44 |
+
allGuides = []; // Resetear
|
| 45 |
+
|
| 46 |
try {
|
| 47 |
+
const response = await fetch(guidesDataUrl); // Fetch al archivo JSON
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
if (!response.ok) {
|
| 49 |
+
throw new Error(`Error HTTP ${response.status} al cargar ${guidesDataUrl}: ${response.statusText}`);
|
| 50 |
}
|
| 51 |
+
allGuides = await response.json(); // Parsear la respuesta como JSON
|
| 52 |
|
| 53 |
+
if (!Array.isArray(allGuides)) {
|
| 54 |
+
throw new Error("El archivo JSON no contiene un array válido.");
|
| 55 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
console.log(`[proa.js v3] Datos JSON cargados (${allGuides.length} guías).`);
|
| 58 |
+
renderGuideIndex(); // Renderizar el índice inicial
|
|
|
|
|
|
|
| 59 |
|
| 60 |
} catch (error) {
|
| 61 |
+
console.error("[proa.js v3] Error fatal al cargar o parsear el JSON de guías:", error);
|
| 62 |
+
guideIndexContainer.innerHTML = `
|
| 63 |
+
<p class="text-red-600 font-semibold">Error al cargar datos.</p>
|
|
|
|
| 64 |
<p class="text-sm text-gray-600 mt-1">Detalle: ${error.message}.</p>
|
| 65 |
+
<p class="text-xs text-gray-500 mt-2">Verifica que '${guidesDataUrl}' exista en 'proa/' y sea un JSON válido.</p>`;
|
| 66 |
+
guideContentDisplay.innerHTML = '<p class="text-gray-500 text-center mt-10">Error al cargar datos de guías.</p>';
|
|
|
|
| 67 |
}
|
| 68 |
}
|
| 69 |
// ======================================================================
|
| 70 |
+
// BLOCK END: Function to Fetch Guides Data (from JSON)
|
| 71 |
// ======================================================================
|
| 72 |
|
| 73 |
// ======================================================================
|
| 74 |
+
// BLOCK START: Function to Render Guide Index (from JSON data)
|
| 75 |
// ======================================================================
|
| 76 |
+
function renderGuideIndex() {
|
| 77 |
+
console.log("[proa.js v3] Renderizando índice de guías...");
|
| 78 |
+
guideIndexContainer.innerHTML = ''; // Limpiar contenedor
|
|
|
|
|
|
|
|
|
|
| 79 |
const showPediatric = togglePediatricCheckbox.checked;
|
| 80 |
+
console.log(`[proa.js v3] Filtro pediátrico activado: ${showPediatric}`);
|
| 81 |
|
| 82 |
+
const filteredGuides = allGuides.filter(guide => !guide.isPediatric || showPediatric);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
|
|
|
| 84 |
if (filteredGuides.length === 0) {
|
| 85 |
+
guideIndexContainer.innerHTML = '<p class="text-gray-500 text-sm">No hay guías para mostrar.</p>';
|
|
|
|
| 86 |
return;
|
| 87 |
}
|
| 88 |
|
| 89 |
+
// Crear elementos del índice
|
| 90 |
filteredGuides.forEach(guide => {
|
| 91 |
+
const indexItem = document.createElement('a');
|
| 92 |
+
indexItem.href = '#';
|
| 93 |
+
indexItem.className = 'dynamic-guide-item block';
|
| 94 |
+
indexItem.dataset.file = guide.file; // Guardar el nombre del archivo HTML
|
| 95 |
+
indexItem.dataset.id = guide.id; // Guardar el ID por si acaso
|
| 96 |
+
indexItem.textContent = guide.title;
|
| 97 |
+
|
| 98 |
+
// Marcar el item activo si es el que está cargado
|
| 99 |
+
if (guide.file === currentGuideUrl) {
|
| 100 |
+
indexItem.classList.add('active');
|
| 101 |
+
}
|
| 102 |
|
|
|
|
| 103 |
if (guide.isPediatric) {
|
|
|
|
| 104 |
const tag = document.createElement('span');
|
| 105 |
+
tag.className = 'pediatric-tag-inline';
|
| 106 |
tag.textContent = 'PED';
|
| 107 |
+
indexItem.appendChild(tag);
|
| 108 |
}
|
| 109 |
|
| 110 |
+
// Listener para cargar contenido
|
| 111 |
+
indexItem.addEventListener('click', (event) => {
|
| 112 |
+
event.preventDefault();
|
| 113 |
+
console.log(`[proa.js v3] Clic en guía: ${guide.title} (Archivo: ${guide.file})`);
|
| 114 |
+
// Desmarcar el item activo anterior
|
| 115 |
+
const currentActive = guideIndexContainer.querySelector('.dynamic-guide-item.active');
|
| 116 |
+
if(currentActive) currentActive.classList.remove('active');
|
| 117 |
+
// Marcar este como activo
|
| 118 |
+
indexItem.classList.add('active');
|
| 119 |
+
currentGuideUrl = guide.file; // Guardar URL actual
|
| 120 |
+
loadGuideContent(guide.file);
|
| 121 |
});
|
| 122 |
|
| 123 |
+
guideIndexContainer.appendChild(indexItem);
|
| 124 |
});
|
| 125 |
+
console.log(`[proa.js v3] Índice renderizado con ${filteredGuides.length} guías.`);
|
| 126 |
}
|
| 127 |
// ======================================================================
|
| 128 |
+
// BLOCK END: Function to Render Guide Index
|
| 129 |
// ======================================================================
|
| 130 |
|
| 131 |
// ======================================================================
|
| 132 |
+
// BLOCK START: Function to Load External Guide Content (Similar a v1)
|
| 133 |
// ======================================================================
|
| 134 |
+
async function loadGuideContent(guideFileName) {
|
| 135 |
+
console.log(`[proa.js v3] Solicitando contenido de: ${guideFileName}`);
|
| 136 |
+
guideContentDisplay.innerHTML = '<div class="text-center py-20"><i class="fas fa-spinner fa-spin text-3xl text-gray-400"></i><p class="mt-2 text-gray-500">Cargando contenido...</p></div>';
|
| 137 |
+
|
| 138 |
+
// Asumimos que el archivo está en la misma carpeta 'proa/' que 'proa.html'
|
| 139 |
+
const guideUrl = guideFileName; // El path es relativo a proa.html
|
| 140 |
|
| 141 |
try {
|
| 142 |
+
const response = await fetch(guideUrl);
|
|
|
|
|
|
|
| 143 |
if (!response.ok) {
|
| 144 |
+
// Intenta cargar sin la extensión por si acaso el JSON la tenía por error
|
| 145 |
+
if (guideUrl.endsWith('.html')) {
|
| 146 |
+
const urlWithoutExt = guideUrl.replace('.html', '');
|
| 147 |
+
console.warn(`[proa.js v3] Fallo al cargar ${guideUrl}, intentando sin extensión: ${urlWithoutExt}`);
|
| 148 |
+
return loadGuideContent(urlWithoutExt); // Llama recursivamente
|
| 149 |
+
}
|
| 150 |
+
throw new Error(`Error HTTP ${response.status} al cargar ${guideUrl}: ${response.statusText}`);
|
| 151 |
}
|
|
|
|
|
|
|
| 152 |
const htmlText = await response.text();
|
| 153 |
+
console.log(`[proa.js v3] Contenido HTML de ${guideFileName} cargado.`);
|
| 154 |
|
|
|
|
| 155 |
const parser = new DOMParser();
|
| 156 |
const doc = parser.parseFromString(htmlText, 'text/html');
|
| 157 |
|
| 158 |
+
// Selector para el contenido principal dentro de CADA archivo de guía individual
|
| 159 |
+
// ¡¡ASEGÚRATE que este selector sea correcto para TUS archivos de guías!!
|
|
|
|
| 160 |
const contentNode = doc.querySelector('.max-w-4xl.mx-auto.px-4.py-8');
|
| 161 |
+
// Alternativa si no es ese: const contentNode = doc.body; (Carga todo el body)
|
| 162 |
|
| 163 |
if (contentNode) {
|
|
|
|
| 164 |
const clonedContent = contentNode.cloneNode(true);
|
| 165 |
+
// Eliminar scripts internos de las guías individuales
|
|
|
|
|
|
|
|
|
|
| 166 |
const scripts = clonedContent.querySelectorAll('script');
|
| 167 |
scripts.forEach(script => script.remove());
|
| 168 |
+
if (scripts.length > 0) console.log(`[proa.js v3] Eliminados ${scripts.length} script(s) de ${guideFileName}.`);
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
guideContentDisplay.innerHTML = ''; // Limpiar área
|
| 171 |
+
guideContentDisplay.appendChild(clonedContent); // Añadir contenido clonado
|
| 172 |
+
console.log(`[proa.js v3] Contenido de ${guideFileName} mostrado.`);
|
| 173 |
+
guideContentDisplay.scrollTop = 0; // Scroll al inicio del contenido
|
| 174 |
|
| 175 |
} else {
|
| 176 |
+
console.error(`[proa.js v3] No se pudo encontrar el nodo de contenido principal en ${guideFileName} usando el selector.`);
|
| 177 |
+
throw new Error(`Estructura interna de '${guideFileName}' no reconocida.`);
|
|
|
|
| 178 |
}
|
| 179 |
|
| 180 |
} catch (error) {
|
| 181 |
+
console.error(`[proa.js v3] Error al cargar/mostrar ${guideFileName}:`, error);
|
| 182 |
+
guideContentDisplay.innerHTML = `
|
| 183 |
+
<div class="text-center py-20 bg-red-50 p-4 rounded border border-red-200">
|
| 184 |
+
<i class="fas fa-exclamation-triangle text-4xl text-red-400 mb-4"></i>
|
| 185 |
+
<p class="text-red-700 font-semibold">Error al cargar contenido.</p>
|
| 186 |
+
<p class="text-sm text-gray-600 mt-1">Detalle: ${error.message}.</p>
|
| 187 |
+
<p class="text-xs text-gray-500 mt-2">Verifica que el archivo '${guideFileName}' exista en 'proa/' y revisa la consola.</p>
|
| 188 |
+
</div>`;
|
| 189 |
+
currentGuideUrl = null; // Resetear URL actual en caso de error
|
| 190 |
+
// Desmarcar item activo en la lista si hubo error
|
| 191 |
+
const currentActive = guideIndexContainer.querySelector('.dynamic-guide-item.active');
|
| 192 |
+
if(currentActive) currentActive.classList.remove('active');
|
| 193 |
}
|
| 194 |
}
|
| 195 |
// ======================================================================
|
| 196 |
+
// BLOCK END: Function to Load External Guide Content
|
| 197 |
// ======================================================================
|
| 198 |
|
| 199 |
// ======================================================================
|
| 200 |
// BLOCK START: Event Listeners Setup
|
| 201 |
// ======================================================================
|
| 202 |
+
togglePediatricCheckbox.addEventListener('change', () => {
|
| 203 |
+
// Al cambiar el filtro, simplemente re-renderizar el índice
|
| 204 |
+
// No limpiamos el contenido mostrado, podría ser confuso
|
| 205 |
+
renderGuideIndex();
|
| 206 |
+
});
|
| 207 |
+
console.log("[proa.js v3] Listener añadido al checkbox pediátrico.");
|
|
|
|
| 208 |
// ======================================================================
|
| 209 |
// BLOCK END: Event Listeners Setup
|
| 210 |
// ======================================================================
|
|
|
|
| 212 |
// ======================================================================
|
| 213 |
// BLOCK START: Initial Execution
|
| 214 |
// ======================================================================
|
| 215 |
+
loadGuidesData(); // Cargar los datos del JSON e iniciar la UI
|
| 216 |
+
console.log("[proa.js v3] Llamada inicial a loadGuidesData() realizada.");
|
|
|
|
|
|
|
| 217 |
// ======================================================================
|
| 218 |
// BLOCK END: Initial Execution
|
| 219 |
// ======================================================================
|
| 220 |
|
| 221 |
+
}); // Fin DOMContentLoaded
|
| 222 |
|
| 223 |
+
console.log("[proa.js v3] Script completamente definido.");
|