// Base module for common functionality class BaseModule { constructor() { this.questions = []; this.currentIndex = 0; this.loading = false; } // Common DOM utilities static getElementById(id) { return document.getElementById(id); } static show(element) { if (typeof element === 'string') { element = document.getElementById(element); } if (element) element.style.display = 'block'; } static hide(element) { if (typeof element === 'string') { element = document.getElementById(element); } if (element) element.style.display = 'none'; } static showFlex(element) { if (typeof element === 'string') { element = document.getElementById(element); } if (element) element.style.display = 'flex'; } // Common API calls async fetchQuestions(params = {}) { const url = new URL('/api/questions', window.location.origin); Object.keys(params).forEach(key => { if (params[key] !== undefined && params[key] !== null) { url.searchParams.append(key, params[key]); } }); try { const response = await fetch(url); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return await response.json(); } catch (error) { console.error('Error fetching questions:', error); this.showNotification('Error al cargar las preguntas', 'danger'); throw error; } } // Common notification wrapper showNotification(message, type = 'info', duration = 3000) { if (window.Utils) { Utils.showNotification(message, type, duration); } else { alert(message); } } // Common sound wrapper playSound(type) { if (window.Utils) { Utils.playSound(type); } } // Common loading states showLoading(loadingId = 'loading') { BaseModule.showFlex(loadingId); } hideLoading(loadingId = 'loading') { BaseModule.hide(loadingId); } // Common question display utilities createQuestionHTML(question, currentIndex, totalQuestions) { let html = `
${question.question}
`; return html; } // Common option creation createOptionsHTML(options, correctAnswer, optionClass = 'option-btn') { let html = ''; return html; } // Common navigation update updateNavigation(prevBtnId, nextBtnId, currentQuestionId) { const prevBtn = BaseModule.getElementById(prevBtnId); const nextBtn = BaseModule.getElementById(nextBtnId); const currentQuestionEl = BaseModule.getElementById(currentQuestionId); if (prevBtn) prevBtn.disabled = this.currentIndex === 0; if (nextBtn) nextBtn.disabled = this.currentIndex === this.questions.length - 1; if (currentQuestionEl) currentQuestionEl.textContent = this.currentIndex + 1; } // Common progress update updateProgress(progressBarId) { const progressBar = BaseModule.getElementById(progressBarId); if (progressBar && this.questions.length > 0) { const progress = ((this.currentIndex + 1) / this.questions.length) * 100; progressBar.style.width = progress + '%'; } } } // Export for global use window.BaseModule = BaseModule;