direito

Repositório direito
git clone git://git.sivaldodavi.com/direito.git
Log | Files | Refs

quiz-runtime.js (2530B)


      1 /**
      2  * Quiz Runtime — controla o fluxo de quiz/simulado na página dedicada
      3  */
      4 
      5 const PROGRESS_PREFIX = window.PROGRESS_PREFIX = 'quiz_progress_';
      6 
      7 // Estado global do quiz (página de quiz dedicada)
      8 let _qState = {
      9   questions: [],
     10   qIndex: 0,
     11   acertos: 0,
     12   erros: 0,
     13   answered: false,
     14   flipped: false,
     15 };
     16 
     17 function nextQuestion() {
     18   _qState.qIndex++;
     19   _qState.answered = false;
     20   _qState.flipped = false;
     21   if (_qState.qIndex >= _qState.questions.length) {
     22     showResult();
     23     return;
     24   }
     25   const body = document.getElementById('quiz-body');
     26   if (!body) return;
     27   body.classList.add('fade-out');
     28   setTimeout(() => {
     29     body.classList.remove('fade-out');
     30     _renderQuestion(_qState.questions[_qState.qIndex]);
     31     _saveProgress(window.QUIZ_KEY);
     32   }, 100);
     33 }
     34 
     35 function showResult() {
     36   const wrap   = document.getElementById('quiz-wrap');
     37   const result = document.getElementById('quiz-result');
     38   if (wrap)   wrap.style.display   = 'none';
     39   if (result) result.style.display = '';
     40   _buildResult();
     41   _clearProgress(window.QUIZ_KEY);
     42   window.scrollTo({ top: 0, behavior: 'smooth' });
     43 }
     44 
     45 function restartQuiz() {
     46   _clearProgress(window.QUIZ_KEY);
     47   _qState.questions = prepareQuizQuestions(parseQuizBody(
     48     typeof RAW_QUIZ !== 'undefined' ? RAW_QUIZ : ''
     49   ));
     50   _qState.qIndex    = 0;
     51   _qState.acertos   = 0;
     52   _qState.erros     = 0;
     53   _qState.answered  = false;
     54   _qState.flipped   = false;
     55   const wrap   = document.getElementById('quiz-wrap');
     56   const result = document.getElementById('quiz-result');
     57   if (result) result.style.display = 'none';
     58   if (wrap)   wrap.style.display   = '';
     59   _renderQuestion(_qState.questions[0]);
     60 }
     61 
     62 function initQuiz() {
     63   // Só inicializa na página de quiz (onde existe #quiz-body)
     64   if (!document.getElementById('quiz-body')) return;
     65 
     66   const raw = typeof RAW_QUIZ !== 'undefined' ? RAW_QUIZ : '';
     67   const key = window.QUIZ_KEY;
     68 
     69   if (!_loadProgress(key)) {
     70     _qState.questions = prepareQuizQuestions(parseQuizBody(raw));
     71     _qState.qIndex    = 0;
     72     _qState.acertos   = 0;
     73     _qState.erros     = 0;
     74   }
     75 
     76   _qState.answered = false;
     77   _qState.flipped  = false;
     78 
     79   const wrap   = document.getElementById('quiz-wrap');
     80   const result = document.getElementById('quiz-result');
     81   if (result) result.style.display = 'none';
     82   if (wrap)   wrap.style.display   = '';
     83 
     84   if (_qState.qIndex >= _qState.questions.length) {
     85     showResult();
     86     return;
     87   }
     88 
     89   _renderQuestion(_qState.questions[_qState.qIndex]);
     90 }
     91 
     92 document.addEventListener('DOMContentLoaded', initQuiz);