diff options
| author | Sivaldo <sivaldodavi@disroot.org> | 2026-06-13 19:20:23 -0300 |
|---|---|---|
| committer | Sivaldo <sivaldodavi@disroot.org> | 2026-06-13 19:20:23 -0300 |
| commit | 7316238f16655bd127b04476bbf7864b27f940f9 (patch) | |
| tree | 61f25290b3e5fd14894e52754cfe6dd4e85845ef /static/quiz-runtime.js | |
Diffstat (limited to 'static/quiz-runtime.js')
| -rw-r--r-- | static/quiz-runtime.js | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/static/quiz-runtime.js b/static/quiz-runtime.js new file mode 100644 index 0000000..d6e824d --- /dev/null +++ b/static/quiz-runtime.js @@ -0,0 +1,92 @@ +/** + * Quiz Runtime — controla o fluxo de quiz/simulado na página dedicada + */ + +const PROGRESS_PREFIX = window.PROGRESS_PREFIX = 'quiz_progress_'; + +// Estado global do quiz (página de quiz dedicada) +let _qState = { + questions: [], + qIndex: 0, + acertos: 0, + erros: 0, + answered: false, + flipped: false, +}; + +function nextQuestion() { + _qState.qIndex++; + _qState.answered = false; + _qState.flipped = false; + if (_qState.qIndex >= _qState.questions.length) { + showResult(); + return; + } + const body = document.getElementById('quiz-body'); + if (!body) return; + body.classList.add('fade-out'); + setTimeout(() => { + body.classList.remove('fade-out'); + _renderQuestion(_qState.questions[_qState.qIndex]); + _saveProgress(window.QUIZ_KEY); + }, 100); +} + +function showResult() { + const wrap = document.getElementById('quiz-wrap'); + const result = document.getElementById('quiz-result'); + if (wrap) wrap.style.display = 'none'; + if (result) result.style.display = ''; + _buildResult(); + _clearProgress(window.QUIZ_KEY); + window.scrollTo({ top: 0, behavior: 'smooth' }); +} + +function restartQuiz() { + _clearProgress(window.QUIZ_KEY); + _qState.questions = prepareQuizQuestions(parseQuizBody( + typeof RAW_QUIZ !== 'undefined' ? RAW_QUIZ : '' + )); + _qState.qIndex = 0; + _qState.acertos = 0; + _qState.erros = 0; + _qState.answered = false; + _qState.flipped = false; + const wrap = document.getElementById('quiz-wrap'); + const result = document.getElementById('quiz-result'); + if (result) result.style.display = 'none'; + if (wrap) wrap.style.display = ''; + _renderQuestion(_qState.questions[0]); +} + +function initQuiz() { + // Só inicializa na página de quiz (onde existe #quiz-body) + if (!document.getElementById('quiz-body')) return; + + const raw = typeof RAW_QUIZ !== 'undefined' ? RAW_QUIZ : ''; + const key = window.QUIZ_KEY; + + if (!_loadProgress(key)) { + _qState.questions = prepareQuizQuestions(parseQuizBody(raw)); + _qState.qIndex = 0; + _qState.acertos = 0; + _qState.erros = 0; + } + + _qState.answered = false; + _qState.flipped = false; + + const wrap = document.getElementById('quiz-wrap'); + const result = document.getElementById('quiz-result'); + if (result) result.style.display = 'none'; + if (wrap) wrap.style.display = ''; + + if (_qState.qIndex >= _qState.questions.length) { + showResult(); + return; + } + + _renderQuestion(_qState.questions[_qState.qIndex]); +} + +document.addEventListener('DOMContentLoaded', initQuiz); |
