working single page html
This commit is contained in:
@@ -0,0 +1,890 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Croatia Friends Pub Quiz</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="group-name" id="group-name"></div>
|
||||
|
||||
<!-- Start Page -->
|
||||
<div id="start-page" class="page active">
|
||||
<h1 id="start-page-title"></h1>
|
||||
<p class="subtitle" id="start-page-subtitle"></p>
|
||||
<p id="start-page-welcome"></p>
|
||||
<button class="btn" onclick="startQuiz()">Start Quiz</button>
|
||||
</div>
|
||||
|
||||
<!-- Question Page -->
|
||||
<div id="question-page" class="page">
|
||||
<div class="progress" id="progress"></div>
|
||||
<div class="question-container">
|
||||
<div class="question-number" id="question-number"></div>
|
||||
<div class="question-text" id="question-text"></div>
|
||||
<img id="question-image" class="question-image" alt="Question image">
|
||||
<div id="audio-container" style="display: none; padding: 10px 0;">
|
||||
<div id="audio-progress-wrapper" style="flex: 1; height: 6px; background: #ddd; border-radius: 3px; position: relative; cursor: pointer;">
|
||||
<div id="audio-progress-bar" class="audio-progress-bar" style="width: 0%; height: 100%; border-radius: 3px; transition: width 0.1s;"></div>
|
||||
</div>
|
||||
<div style="display: flex; align-items: center; justify-content: space-between; gap: 15px; margin-top: 10px;">
|
||||
<div id="audio-start-time" style="font-size: 14px; min-width: 45px; text-align: left;">0:00</div>
|
||||
<div style="display: flex; align-items: center; gap: 8px;">
|
||||
<button id="audio-play-btn" class="btn btn-small" style="padding: 6px 12px; font-size: 14px;">▶</button>
|
||||
<button id="audio-mute-btn" class="btn btn-small audio-mute-btn" style="padding: 6px 12px; font-size: 14px; display: flex; align-items: center; gap: 5px;" onclick="toggleMute()">
|
||||
<span id="mute-icon">🔊</span>
|
||||
</button>
|
||||
<input type="range" id="audio-volume" min="0" max="1" step="0.01" value="1" style="width: 50px; cursor: pointer;">
|
||||
</div>
|
||||
<div id="audio-end-time" style="font-size: 14px; min-width: 45px; text-align: right;">0:00</div>
|
||||
</div>
|
||||
<audio id="question-audio" style="display: none;"></audio>
|
||||
</div>
|
||||
</div>
|
||||
<div id="answers-container" style="display: none;">
|
||||
<h3 style="color: #1a5276; margin-bottom: 15px;">Vote for your answer:</h3>
|
||||
<div class="answers-grid" id="answers-list"></div>
|
||||
</div>
|
||||
<div id="next-buttons">
|
||||
<button class="btn" onclick="prevQuestion()" id="prev-btn" style="display: none;">Previous</button>
|
||||
<button class="btn" onclick="nextQuestion()" id="next-btn">Next Question</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Recap Page -->
|
||||
<div id="recap-page" class="page">
|
||||
<h1>📝 Quiz Recap</h1>
|
||||
<p class="subtitle">All questions with answers</p>
|
||||
<div id="recap-answers"></div>
|
||||
<button class="btn btn-secondary" onclick="restartQuiz()">Play Again</button>
|
||||
</div>
|
||||
|
||||
<!-- Final Page -->
|
||||
<div id="final-page" class="page">
|
||||
<h1 id="final-page-title"></h1>
|
||||
<p class="subtitle" id="final-page-subtitle"></p>
|
||||
<div class="answers-grid" id="final-answers"></div>
|
||||
<button class="btn btn-secondary" onclick="restartQuiz()" id="play-again-btn"></button>
|
||||
</div>
|
||||
|
||||
<!-- Round Review Page - Shows previous round answers -->
|
||||
<div id="review-page" class="page">
|
||||
<h1 id="review-page-title"></h1>
|
||||
<div id="review-answers"></div>
|
||||
<p id="review-page-message"></p>
|
||||
<button class="btn" onclick="nextRound(event)" id="continue-btn"></button>
|
||||
<button class="btn btn-secondary" onclick="restartQuiz()" id="play-again-btn-review"></button>
|
||||
</div>
|
||||
|
||||
<!-- Break Page -->
|
||||
<div id="break-page" class="page">
|
||||
<h1 id="break-page-title"></h1>
|
||||
<div id="round-ranges" style="margin: 20px 0; padding: 15px; background: #f8f9fa; border-radius: 10px; font-size: 0.95rem;"></div>
|
||||
<p id="break-page-message"></p>
|
||||
<button class="btn" onclick="nextRound(event)" id="next-round-btn"></button>
|
||||
<button class="btn btn-secondary" onclick="restartQuiz()" id="play-again-btn-break"></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let questions = [];
|
||||
let answers = [];
|
||||
let currentQuestion = 0;
|
||||
let currentRound = 1;
|
||||
let reviewPageProcessing = false;
|
||||
let config = {};
|
||||
// Rounds loaded from JSON file
|
||||
let rounds = [];
|
||||
let roundQuestions = []; // Flat array of questions with round info
|
||||
let currentVolume = 1; // Persistent volume setting
|
||||
|
||||
// Set progress bar color based on current theme
|
||||
function setProgressColor(progressBar) {
|
||||
const body = document.body;
|
||||
if (!progressBar) return;
|
||||
|
||||
if (body.classList.contains('croatia-theme')) {
|
||||
progressBar.style.background = 'linear-gradient(90deg, #0077b6, #00b4d8)';
|
||||
} else if (body.classList.contains('dutch-theme')) {
|
||||
progressBar.style.background = 'linear-gradient(90deg, #ff9900, #ff6600)';
|
||||
} else if (body.classList.contains('culture-theme')) {
|
||||
progressBar.style.background = 'linear-gradient(90deg, #d2691e, #cd853f)';
|
||||
} else if (body.classList.contains('friends-theme')) {
|
||||
progressBar.style.background = 'linear-gradient(90deg, #4CAF50, #2196F3)';
|
||||
} else {
|
||||
progressBar.style.background = 'linear-gradient(90deg, #3498db, #2980b9)';
|
||||
}
|
||||
}
|
||||
|
||||
// Global mute toggle function
|
||||
let isMuted = false;
|
||||
let savedVolume = 1;
|
||||
function toggleMute() {
|
||||
const audio = document.getElementById('question-audio');
|
||||
const muteIcon = document.getElementById('mute-icon');
|
||||
|
||||
isMuted = !isMuted;
|
||||
muteIcon.textContent = isMuted ? '🔇' : '🔊';
|
||||
|
||||
if (isMuted) {
|
||||
audio.muted = true;
|
||||
const volumeSlider = document.getElementById('audio-volume');
|
||||
if (volumeSlider) volumeSlider.value = 0;
|
||||
// Save current volume for later
|
||||
savedVolume = audio.volume;
|
||||
} else {
|
||||
audio.muted = false;
|
||||
// Restore saved volume
|
||||
audio.volume = savedVolume;
|
||||
const volumeSlider = document.getElementById('audio-volume');
|
||||
if (volumeSlider) volumeSlider.value = savedVolume;
|
||||
}
|
||||
}
|
||||
|
||||
function startQuiz() {
|
||||
showPage('question-page');
|
||||
currentQuestion = 0;
|
||||
loadQuestion();
|
||||
}
|
||||
|
||||
function showPage(pageId) {
|
||||
document.querySelectorAll('.page').forEach(page => {
|
||||
page.classList.remove('active');
|
||||
});
|
||||
document.getElementById(pageId).classList.add('active');
|
||||
// Clear answer content when showing start or question pages
|
||||
if (pageId === 'start-page' || pageId === 'question-page') {
|
||||
document.getElementById('final-answers').innerHTML = '';
|
||||
document.getElementById('recap-answers').innerHTML = '';
|
||||
}
|
||||
// Clear recap answers when showing final page
|
||||
if (pageId === 'final-page') {
|
||||
document.getElementById('recap-answers').innerHTML = '';
|
||||
}
|
||||
}
|
||||
|
||||
function loadQuestion() {
|
||||
const q = questions[currentQuestion];
|
||||
const progress = document.getElementById('progress');
|
||||
const qNumber = document.getElementById('question-number');
|
||||
const qText = document.getElementById('question-text');
|
||||
const questionImage = document.getElementById('question-image');
|
||||
const answersContainer = document.getElementById('answers-container');
|
||||
const answersList = document.getElementById('answers-list');
|
||||
const nextButtons = document.getElementById('next-buttons');
|
||||
|
||||
// Set progress bar color based on current theme and reset progress to 0
|
||||
const audioContainer = document.getElementById('audio-container');
|
||||
const progressBar = document.getElementById('audio-progress-bar');
|
||||
const timeDisplay = document.getElementById('audio-time');
|
||||
const audio = document.getElementById('question-audio');
|
||||
const playBtn = document.getElementById('audio-play-btn');
|
||||
if (audioContainer) {
|
||||
audioContainer.style.display = q.audio ? 'block' : 'none';
|
||||
if (progressBar) {
|
||||
// Reset progress bar to 0%
|
||||
progressBar.style.width = '0%';
|
||||
// Reset time display to 0:00
|
||||
if (timeDisplay) {
|
||||
timeDisplay.textContent = '0:00';
|
||||
}
|
||||
// Reset audio to beginning
|
||||
if (audio && q.audio) {
|
||||
audio.currentTime = 0;
|
||||
}
|
||||
// Reset play button to play icon
|
||||
if (playBtn) {
|
||||
playBtn.textContent = '▶';
|
||||
}
|
||||
setProgressColor(progressBar);
|
||||
}
|
||||
}
|
||||
|
||||
// Update progress dots - only show current round
|
||||
progress.innerHTML = '';
|
||||
const roundIndex = getRoundIndex(currentQuestion);
|
||||
const round = rounds[roundIndex] || { name: `Round ${roundIndex + 1}` };
|
||||
|
||||
// Add round counter
|
||||
const roundCounter = document.createElement('div');
|
||||
roundCounter.style.cssText = 'display: flex; align-items: center; gap: 10px; margin-bottom: 10px;';
|
||||
roundCounter.innerHTML = `<span style="font-weight: bold; color: #1a5276;">${round.name || `Round ${roundIndex + 1}`}</span> `;
|
||||
progress.appendChild(roundCounter);
|
||||
|
||||
// Show dots only for current round
|
||||
const questionsInRound = [];
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
if (questions[i].roundIndex === roundIndex) {
|
||||
questionsInRound.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
questionsInRound.forEach(qIndex => {
|
||||
const dot = document.createElement('div');
|
||||
dot.className = 'progress-dot';
|
||||
if (qIndex < currentQuestion) dot.classList.add('completed');
|
||||
if (qIndex === currentQuestion) dot.classList.add('active');
|
||||
progress.appendChild(dot);
|
||||
});
|
||||
|
||||
qNumber.textContent = `Question ${currentQuestion + 1} of ${questions.length}`;
|
||||
qText.textContent = q.question;
|
||||
|
||||
// Show/hide question image
|
||||
if (q.image) {
|
||||
questionImage.src = q.image;
|
||||
questionImage.classList.add('show');
|
||||
} else {
|
||||
questionImage.classList.remove('show');
|
||||
}
|
||||
|
||||
// Show/hide and configure audio
|
||||
const container = document.getElementById('audio-container');
|
||||
const progressWrapper = document.getElementById('audio-progress-wrapper');
|
||||
const startTimeDisplay = document.getElementById('audio-start-time');
|
||||
const endTimeDisplay = document.getElementById('audio-end-time');
|
||||
const volumeSlider = document.getElementById('audio-volume');
|
||||
|
||||
// playBtn, audio, and progressBar are already defined above
|
||||
// Reset mute state for new question
|
||||
isMuted = false;
|
||||
savedVolume = 1;
|
||||
|
||||
// Format time helper
|
||||
function formatTime(seconds) {
|
||||
if (!seconds || isNaN(seconds)) return '0:00';
|
||||
const mins = Math.floor(seconds / 60);
|
||||
const secs = Math.floor(seconds % 60);
|
||||
return `${mins}:${secs.toString().padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
if (q.audio) {
|
||||
const audio = document.getElementById('question-audio');
|
||||
|
||||
container.style.display = 'block';
|
||||
audio.src = q.audio;
|
||||
audio.loop = q.loop || false;
|
||||
audio.volume = currentVolume;
|
||||
// Reset audio time to 0 for new question
|
||||
audio.currentTime = 0;
|
||||
// Reset time display to 0:00
|
||||
const startTimeDisplay = document.getElementById('audio-start-time');
|
||||
if (startTimeDisplay) {
|
||||
startTimeDisplay.textContent = '0:00';
|
||||
}
|
||||
|
||||
// Update volume slider to match current volume
|
||||
|
||||
// Set initial color
|
||||
setProgressColor(progressBar);
|
||||
|
||||
// Play/Pause button - remove and re-add listener to fix issue
|
||||
playBtn.textContent = '▶';
|
||||
playBtn.onclick = () => {
|
||||
if (audio.paused) {
|
||||
audio.play();
|
||||
playBtn.textContent = '⏸';
|
||||
} else {
|
||||
audio.pause();
|
||||
playBtn.textContent = '▶';
|
||||
}
|
||||
};
|
||||
|
||||
// Volume slider - remove and re-add listener to fix issue
|
||||
volumeSlider.value = currentVolume;
|
||||
volumeSlider.oninput = () => {
|
||||
audio.volume = volumeSlider.value;
|
||||
currentVolume = volumeSlider.value;
|
||||
// Unmute when adjusting volume
|
||||
if (audio.muted) {
|
||||
audio.muted = false;
|
||||
muteIcon.textContent = '🔊';
|
||||
volumeSlider.value = 1;
|
||||
}
|
||||
};
|
||||
|
||||
// Set volume immediately (before metadata loads)
|
||||
audio.volume = currentVolume;
|
||||
|
||||
// Make progress bar seekable by clicking - remove and re-add listener
|
||||
progressWrapper.onclick = (e) => {
|
||||
if (!audio || !audio.duration || isNaN(audio.duration)) return;
|
||||
const rect = progressWrapper.getBoundingClientRect();
|
||||
const clickX = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
|
||||
const width = rect.width;
|
||||
const duration = audio.duration;
|
||||
|
||||
const seekTime = Math.max(0, Math.min(duration, (clickX / width) * duration));
|
||||
audio.currentTime = seekTime;
|
||||
};
|
||||
|
||||
// Show duration when metadata loaded
|
||||
if (!audio._audio_loadedmetadata) {
|
||||
audio._audio_loadedmetadata = true;
|
||||
audio.addEventListener('loadedmetadata', () => {
|
||||
if (endTimeDisplay && !isNaN(audio.duration)) {
|
||||
endTimeDisplay.textContent = formatTime(audio.duration);
|
||||
}
|
||||
// Start listening to timeupdate after metadata is loaded
|
||||
audio._audio_timeupdate = () => {
|
||||
if (progressBar && endTimeDisplay && !isNaN(audio.duration)) {
|
||||
const pct = (audio.currentTime / audio.duration) * 100;
|
||||
progressBar.style.width = pct + '%';
|
||||
startTimeDisplay.textContent = formatTime(audio.currentTime);
|
||||
endTimeDisplay.textContent = formatTime(audio.duration);
|
||||
|
||||
// Update progress bar color based on current theme
|
||||
setProgressColor(progressBar);
|
||||
}
|
||||
};
|
||||
audio.addEventListener('timeupdate', audio._audio_timeupdate);
|
||||
|
||||
// Add ended listener
|
||||
audio._audio_ended = () => {
|
||||
if (progressBar) progressBar.style.width = '100%';
|
||||
playBtn.textContent = '▶';
|
||||
if (timeDisplay) timeDisplay.textContent = formatTime(audio.duration);
|
||||
};
|
||||
audio.addEventListener('ended', audio._audio_ended);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
container.style.display = 'none';
|
||||
const audio = document.getElementById('question-audio');
|
||||
if (audio) {
|
||||
audio.src = '';
|
||||
// Clear all our custom listeners
|
||||
if (audio._audio_timeupdate) {
|
||||
audio.removeEventListener('timeupdate', audio._audio_timeupdate);
|
||||
delete audio._audio_timeupdate;
|
||||
}
|
||||
if (audio._audio_ended) {
|
||||
audio.removeEventListener('ended', audio._audio_ended);
|
||||
delete audio._audio_ended;
|
||||
}
|
||||
// Reset volume
|
||||
audio.volume = 1;
|
||||
}
|
||||
// Clear progressWrapper onclick
|
||||
if (progressWrapper) {
|
||||
progressWrapper.onclick = null;
|
||||
}
|
||||
// Reset play button to play icon
|
||||
if (playBtn) {
|
||||
playBtn.textContent = '▶';
|
||||
}
|
||||
// Reset volume slider and audio volume
|
||||
if (volumeSlider) {
|
||||
volumeSlider.value = 1;
|
||||
}
|
||||
if (audio) {
|
||||
audio.volume = 1;
|
||||
}
|
||||
// Reset time displays
|
||||
if (endTimeDisplay) {
|
||||
endTimeDisplay.textContent = '0:00';
|
||||
}
|
||||
// Reset mute state
|
||||
isMuted = false;
|
||||
savedVolume = 1;
|
||||
}
|
||||
|
||||
// Show all answer options on question page
|
||||
answersContainer.style.display = 'block';
|
||||
answersList.innerHTML = '';
|
||||
|
||||
// Remove all theme classes before applying new one
|
||||
['croatia-theme', 'dutch-theme', 'culture-theme', 'friends-theme'].forEach(cls => document.body.classList.remove(cls));
|
||||
|
||||
// Apply round theme if exists
|
||||
if (q.roundTheme) {
|
||||
document.body.classList.add(q.roundTheme);
|
||||
}
|
||||
|
||||
// Show previous button if not on first question
|
||||
const prevBtn = document.getElementById('prev-btn');
|
||||
if (currentQuestion > 0) {
|
||||
prevBtn.style.display = 'inline-block';
|
||||
} else {
|
||||
prevBtn.style.display = 'none';
|
||||
}
|
||||
|
||||
// Create answer options only if they exist
|
||||
if (q.options && q.options.length > 0) {
|
||||
const numOptions = q.options.length;
|
||||
answersList.className = 'answers-grid';
|
||||
|
||||
// Add dynamic class based on number of options
|
||||
if (numOptions === 1) {
|
||||
answersList.classList.add('one-column');
|
||||
} else if (numOptions === 2) {
|
||||
answersList.classList.add('two-columns');
|
||||
} else if (numOptions === 3) {
|
||||
answersList.classList.add('three-columns');
|
||||
} else if (numOptions === 4) {
|
||||
answersList.classList.add('four-columns');
|
||||
} else {
|
||||
answersList.classList.add('five-plus-columns');
|
||||
}
|
||||
|
||||
for (let i = 0; i < numOptions; i++) {
|
||||
const option = ['A', 'B', 'C', 'D', 'E', 'F', 'G'][i];
|
||||
const item = document.createElement('div');
|
||||
item.className = 'answer-item';
|
||||
item.innerHTML = `<div class="answer-label">${option}. ${q.options[i]}</div>`;
|
||||
item.onclick = () => {
|
||||
answers[currentQuestion] = i;
|
||||
item.classList.add('selected');
|
||||
};
|
||||
answersList.appendChild(item);
|
||||
}
|
||||
}
|
||||
|
||||
nextButtons.style.display = 'block';
|
||||
}
|
||||
|
||||
function nextQuestion() {
|
||||
const prevQuestionIndex = currentQuestion;
|
||||
currentQuestion++;
|
||||
console.log('nextQuestion called, currentQuestion:', currentQuestion, 'prevQuestionIndex:', prevQuestionIndex);
|
||||
|
||||
// Check if we need to show review page (after completing a round)
|
||||
const prevQuestion = questions[prevQuestionIndex];
|
||||
const prevRoundIndex = prevQuestion ? prevQuestion.roundIndex : -1;
|
||||
|
||||
if (prevRoundIndex >= 0 && prevRoundIndex < rounds.length) {
|
||||
const prevRound = rounds[prevRoundIndex];
|
||||
console.log('prevRound:', prevRound);
|
||||
// Check if we just finished the last question of this round
|
||||
const questionsInRound = [];
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
if (questions[i].roundIndex === prevRoundIndex) {
|
||||
questionsInRound.push(i);
|
||||
}
|
||||
}
|
||||
const lastQuestionInRound = questionsInRound[questionsInRound.length - 1];
|
||||
|
||||
if (prevQuestionIndex === lastQuestionInRound) {
|
||||
console.log('Showing review page for round', prevRoundIndex + 1);
|
||||
showReviewPage();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentQuestion < questions.length) {
|
||||
loadQuestion();
|
||||
} else {
|
||||
showFinalPage();
|
||||
}
|
||||
}
|
||||
|
||||
function prevQuestion() {
|
||||
if (currentQuestion > 0) {
|
||||
currentQuestion--;
|
||||
loadQuestion();
|
||||
}
|
||||
}
|
||||
|
||||
function toggleAudio() {
|
||||
const audio = document.getElementById('question-audio');
|
||||
if (audio) {
|
||||
if (audio.paused) {
|
||||
audio.play();
|
||||
} else {
|
||||
audio.pause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
function showReviewPage() {
|
||||
console.log('showReviewPage called');
|
||||
// Get the round that just completed
|
||||
const currentRoundIndex = getRoundIndex(currentQuestion - 1);
|
||||
const currentRound = rounds[currentRoundIndex];
|
||||
const totalRounds = rounds.length;
|
||||
// currentQuestion is already pointing to next question from nextQuestion() call
|
||||
// No need to reset - nextRound() will load the next question
|
||||
console.log('currentQuestion already at:', currentQuestion, '(next question)');
|
||||
|
||||
// Collect all answers from this round (questions belong to their round)
|
||||
const roundAnswers = [];
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
if (questions[i].roundIndex === currentRoundIndex) {
|
||||
roundAnswers.push({
|
||||
question: questions[i],
|
||||
selectedOption: answers[i]
|
||||
});
|
||||
}
|
||||
}
|
||||
const questionsInRound = roundAnswers.length;
|
||||
|
||||
// Display review page
|
||||
const reviewAnswersDiv = document.getElementById('review-answers');
|
||||
const reviewMessage = document.getElementById('review-page-message');
|
||||
const roundName = currentRound.name || `Round ${currentRoundIndex + 1}`;
|
||||
|
||||
reviewMessage.innerHTML = `
|
||||
<strong>${roundName}</strong><br>
|
||||
Round ${currentRoundIndex + 1} of ${totalRounds}<br>
|
||||
Reviewing ${roundAnswers.length} questions
|
||||
`;
|
||||
|
||||
// Build HTML for each answer showing all options
|
||||
let answersHTML = '';
|
||||
roundAnswers.forEach((item, idx) => {
|
||||
const q = item.question;
|
||||
const selected = item.selectedOption;
|
||||
const options = q.options;
|
||||
const correct = q.correctOption;
|
||||
|
||||
// Create grid for all options
|
||||
const numOptions = options.length;
|
||||
let optionsHTML = '';
|
||||
|
||||
for (let i = 0; i < numOptions; i++) {
|
||||
const optionLabel = ['A', 'B', 'C', 'D', 'E', 'F', 'G'][i];
|
||||
const isCorrectOption = i === correct;
|
||||
const isSelectedOption = selected === i;
|
||||
const isBoth = isCorrectOption && isSelectedOption;
|
||||
|
||||
optionsHTML += `
|
||||
<div style="padding: 8px 12px; margin: 4px 0; border-radius: 6px; background: ${isCorrectOption ? '#e8f5e9' : (isSelectedOption && !isCorrectOption ? '#fff3e0' : 'white')}; border-left: 3px solid ${isCorrectOption ? '#4caf50' : (isSelectedOption ? '#ff9800' : '#ddd')};">
|
||||
<span style="color: #2c3e50; font-weight: ${isCorrectOption ? 'bold' : 'normal'};">${optionLabel}.</span> ${options[i]}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
answersHTML += `
|
||||
<div style="margin-bottom: 20px; padding: 15px; border-radius: 10px; background: #f8f9fa;">
|
||||
<p style="font-weight: 600; margin-bottom: 10px; color: #2c3e50;">${idx + 1}. ${q.question}</p>
|
||||
<div style="padding: 10px; background: white; border-radius: 8px;">
|
||||
${optionsHTML}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
|
||||
reviewAnswersDiv.innerHTML = answersHTML;
|
||||
showPage('review-page');
|
||||
}
|
||||
|
||||
function showBreakPage() {
|
||||
const breakPage = document.getElementById('break-page');
|
||||
const breakMessage = document.getElementById('break-message');
|
||||
const roundRanges = document.getElementById('round-ranges');
|
||||
|
||||
const currentRoundIndex = getRoundIndex(currentQuestion);
|
||||
const currentRound = rounds[currentRoundIndex];
|
||||
const totalRounds = rounds.length;
|
||||
|
||||
breakMessage.innerHTML = `
|
||||
<strong>${currentRound.name || `Round ${currentRoundIndex + 1}`}</strong><br>
|
||||
Round ${currentRoundIndex + 1} of ${totalRounds}<br>
|
||||
Completed question ${currentQuestion} of ${questions.length}
|
||||
`;
|
||||
|
||||
// Show all round names
|
||||
let rangesHTML = '';
|
||||
rounds.forEach((round, i) => {
|
||||
const isCurrent = i === currentRoundIndex;
|
||||
const roundName = round.name || `Round ${i + 1}`;
|
||||
rangesHTML += `<div style="padding: 8px; margin: 5px 0; border-radius: 5px; ${isCurrent ? 'background: #e3f2fd; font-weight: bold;' : ''}">
|
||||
${roundName} ${isCurrent ? '(Current)' : ''}
|
||||
</div>`;
|
||||
});
|
||||
roundRanges.innerHTML = rangesHTML;
|
||||
|
||||
showPage('break-page');
|
||||
}
|
||||
|
||||
function nextRound(event) {
|
||||
console.log('nextRound called, reviewPageProcessing:', reviewPageProcessing);
|
||||
// Prevent double-click
|
||||
if (reviewPageProcessing) {
|
||||
console.log('Already processing, returning');
|
||||
return;
|
||||
}
|
||||
reviewPageProcessing = true;
|
||||
|
||||
console.log('nextRound called! currentQuestion:', currentQuestion);
|
||||
// Prevent default and stop propagation
|
||||
if (event) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
// currentQuestion is already pointing to next question (set in nextQuestion()), just load it
|
||||
console.log('Loading next question (currentQuestion already incremented)');
|
||||
if (currentQuestion < questions.length) {
|
||||
console.log('Loading next question');
|
||||
showPage('question-page');
|
||||
loadQuestion();
|
||||
console.log('loadQuestion done, checking page state');
|
||||
} else {
|
||||
console.log('Showing final page');
|
||||
showFinalPage();
|
||||
}
|
||||
console.log('Setting reviewPageProcessing to false');
|
||||
reviewPageProcessing = false;
|
||||
}
|
||||
|
||||
function getRoundIndex(questionIndex) {
|
||||
// Returns the round index for a given question (0-based)
|
||||
if (!rounds || rounds.length === 0) {
|
||||
// Fallback: each question is its own round
|
||||
return questionIndex;
|
||||
}
|
||||
|
||||
// With new structure, questions have roundIndex property
|
||||
if (questions[questionIndex] && questions[questionIndex].roundIndex !== undefined) {
|
||||
return questions[questionIndex].roundIndex;
|
||||
}
|
||||
|
||||
// Fallback: use question index as round index
|
||||
return questionIndex;
|
||||
}
|
||||
|
||||
function showRecapPage() {
|
||||
// Clear final page content before showing recap
|
||||
document.getElementById('final-answers').innerHTML = '';
|
||||
showPage('recap-page');
|
||||
const recapContainer = document.getElementById('recap-answers');
|
||||
|
||||
questions.forEach((q, index) => {
|
||||
const item = document.createElement('div');
|
||||
item.className = 'recap-item';
|
||||
|
||||
const questionDiv = document.createElement('div');
|
||||
questionDiv.className = 'recap-question';
|
||||
questionDiv.textContent = `${index + 1}. ${q.question}`;
|
||||
|
||||
const answersDiv = document.createElement('div');
|
||||
answersDiv.className = 'recap-answers';
|
||||
|
||||
const optionsToShow = q.options ? q.options.length : 0;
|
||||
const maxOptions = Math.min(optionsToShow, 6);
|
||||
|
||||
// Add audio element if question has audio
|
||||
if (q.audio) {
|
||||
const audioContainer = document.createElement('div');
|
||||
audioContainer.id = `recap-audio-${index}`;
|
||||
audioContainer.style.cssText = 'margin-bottom: 10px; text-align: center;';
|
||||
audioContainer.innerHTML = '<audio id="recap-audio-' + index + '" style="display: none;" src="' + q.audio + '" loop="' + (q.loop || 'false') + '"></audio>';
|
||||
answersDiv.appendChild(audioContainer);
|
||||
}
|
||||
|
||||
for (let i = 0; i < maxOptions; i++) {
|
||||
const answerItem = document.createElement('div');
|
||||
answerItem.className = 'recap-answer-item';
|
||||
|
||||
const selected = answers[index];
|
||||
|
||||
// Check if this option is the correct one
|
||||
if (i === q.correctOption) {
|
||||
answerItem.classList.add('correct');
|
||||
} else if (selected === i && i !== q.correctOption) {
|
||||
answerItem.classList.add('incorrect');
|
||||
}
|
||||
|
||||
const labelDiv = document.createElement('div');
|
||||
labelDiv.className = 'recap-answer-label';
|
||||
labelDiv.textContent = `${['A', 'B', 'C', 'D', 'E', 'F'][i]}. ${q.options[i]}`;
|
||||
|
||||
const textDiv = document.createElement('div');
|
||||
textDiv.className = 'recap-answer-text';
|
||||
textDiv.textContent = q.options[i];
|
||||
|
||||
answerItem.appendChild(labelDiv);
|
||||
answerItem.appendChild(textDiv);
|
||||
|
||||
answersDiv.appendChild(answerItem);
|
||||
}
|
||||
|
||||
answersDiv.appendChild(questionDiv);
|
||||
item.appendChild(questionDiv);
|
||||
item.appendChild(answersDiv);
|
||||
recapContainer.appendChild(item);
|
||||
});
|
||||
|
||||
showPage('recap-page');
|
||||
}
|
||||
|
||||
function showFinalPage() {
|
||||
// Clear recap page content before showing final page
|
||||
document.getElementById('recap-answers').innerHTML = '';
|
||||
showPage('final-page');
|
||||
const answersContainer = document.getElementById('final-answers');
|
||||
|
||||
questions.forEach((q, index) => {
|
||||
const item = document.createElement('div');
|
||||
item.className = 'answer-item';
|
||||
|
||||
const label = document.createElement('div');
|
||||
label.className = 'answer-label';
|
||||
label.textContent = `${index + 1}. ${q.question}`;
|
||||
|
||||
const text = document.createElement('div');
|
||||
text.className = 'answer-text';
|
||||
text.style.fontWeight = 'bold';
|
||||
text.style.color = '#27ae60';
|
||||
text.textContent = `✓ ${q.options[q.correctOption]}`;
|
||||
|
||||
// Add image if question has one
|
||||
if (q.image) {
|
||||
const img = document.createElement('img');
|
||||
img.className = 'question-image';
|
||||
img.style.marginTop = '10px';
|
||||
img.src = q.image;
|
||||
img.alt = q.options[q.correctOption];
|
||||
item.appendChild(img);
|
||||
}
|
||||
|
||||
// Add audio button if question has audio
|
||||
if (q.audio) {
|
||||
const audioBtn = document.createElement('button');
|
||||
audioBtn.className = 'btn-audio';
|
||||
audioBtn.innerHTML = '<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/></svg>';
|
||||
audioBtn.title = 'Play/Pause audio';
|
||||
audioBtn.onclick = () => toggleAudio(index);
|
||||
|
||||
// Add audio element
|
||||
const audioContainer = document.createElement('div');
|
||||
audioContainer.id = `question-audio-${index}`;
|
||||
audioContainer.style.cssText = 'margin-top: 10px; text-align: center;';
|
||||
audioContainer.innerHTML = '<audio id="question-audio-' + index + '" style="display: none;"></audio>';
|
||||
|
||||
const audio = document.getElementById('question-audio-' + index);
|
||||
if (audio) {
|
||||
audio.src = q.audio;
|
||||
audio.loop = q.loop || false;
|
||||
}
|
||||
|
||||
item.appendChild(audioContainer);
|
||||
item.appendChild(audioBtn);
|
||||
}
|
||||
|
||||
item.appendChild(label);
|
||||
item.appendChild(text);
|
||||
|
||||
// Add answers grid with dynamic columns
|
||||
const answersGrid = document.createElement('div');
|
||||
answersGrid.className = 'answers-grid';
|
||||
|
||||
const numOptions = q.options ? q.options.length : 0;
|
||||
if (numOptions === 1) {
|
||||
answersGrid.classList.add('one-column');
|
||||
} else if (numOptions === 2) {
|
||||
answersGrid.classList.add('two-columns');
|
||||
} else if (numOptions === 3) {
|
||||
answersGrid.classList.add('three-columns');
|
||||
} else if (numOptions === 4) {
|
||||
answersGrid.classList.add('four-columns');
|
||||
} else {
|
||||
answersGrid.classList.add('five-plus-columns');
|
||||
}
|
||||
|
||||
// Show all options
|
||||
const maxOptions = Math.min(q.options ? q.options.length : 0, 6);
|
||||
for (let i = 0; i < maxOptions; i++) {
|
||||
const option = ['A', 'B', 'C', 'D', 'E', 'F'][i];
|
||||
const optionItem = document.createElement('div');
|
||||
optionItem.className = 'answer-item';
|
||||
|
||||
const optionLabel = document.createElement('div');
|
||||
optionLabel.className = 'answer-label';
|
||||
optionLabel.textContent = `${option}. ${q.options[i]}`;
|
||||
|
||||
optionItem.appendChild(optionLabel);
|
||||
answersGrid.appendChild(optionItem);
|
||||
}
|
||||
|
||||
item.appendChild(answersGrid);
|
||||
answersContainer.appendChild(item);
|
||||
});
|
||||
}
|
||||
|
||||
function restartQuiz() {
|
||||
currentQuestion = 0;
|
||||
currentRound = 1;
|
||||
// Clear all answer content
|
||||
document.getElementById('final-answers').innerHTML = '';
|
||||
showPage('start-page');
|
||||
}
|
||||
|
||||
// Load questions from JSON file
|
||||
// Show start page immediately
|
||||
showPage('start-page');
|
||||
|
||||
fetch('questions.json')
|
||||
.then(response => {
|
||||
console.log('Response status:', response.status);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Network response was not ok: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
console.log('JSON data received:', data);
|
||||
config = data.config || {};
|
||||
rounds = data.rounds || [];
|
||||
|
||||
// Flatten rounds into a single questions array with round info
|
||||
roundQuestions = [];
|
||||
rounds.forEach((round, roundIndex) => {
|
||||
if (round.questions && Array.isArray(round.questions)) {
|
||||
round.questions.forEach((q, qIndex) => {
|
||||
q.roundIndex = roundIndex;
|
||||
q.roundName = round.name || `Round ${roundIndex + 1}`;
|
||||
q.roundTheme = round.theme || '';
|
||||
roundQuestions.push(q);
|
||||
});
|
||||
}
|
||||
});
|
||||
questions = roundQuestions;
|
||||
|
||||
console.log('Rounds loaded:', rounds);
|
||||
console.log('Questions loaded:', questions.length);
|
||||
console.log('Config loaded:', config);
|
||||
|
||||
// Validate config
|
||||
if (!questions.length) {
|
||||
alert('No questions found in JSON file!');
|
||||
return;
|
||||
}
|
||||
|
||||
// Populate start page
|
||||
document.getElementById('group-name').textContent = config.group_name || 'Pub Quiz';
|
||||
document.getElementById('start-page-title').textContent = config.start_page_title || 'Pub Quiz Night';
|
||||
document.getElementById('start-page-subtitle').textContent = config.start_page_subtitle || '';
|
||||
document.getElementById('start-page-welcome').textContent = config.start_page_welcome || '';
|
||||
|
||||
// Populate break page
|
||||
document.getElementById('break-page-title').textContent = config.round_break_title || 'Round Break';
|
||||
document.getElementById('break-page-message').textContent = config.round_break_message || '';
|
||||
|
||||
// Populate review page
|
||||
document.getElementById('review-page-title').textContent = config.review_page_title || 'Review Round';
|
||||
document.getElementById('review-page-message').textContent = config.review_page_message || '';
|
||||
|
||||
// Populate final page
|
||||
document.getElementById('final-page-title').textContent = config.final_page_title || 'Quiz Complete!';
|
||||
document.getElementById('final-page-subtitle').textContent = config.final_page_subtitle || '';
|
||||
document.getElementById('play-again-btn').textContent = config.play_again_button || 'Play Again';
|
||||
document.getElementById('play-again-btn-review').textContent = config.play_again_button || 'Play Again';
|
||||
document.getElementById('play-again-btn-break').textContent = config.play_again_button || 'Play Again';
|
||||
document.getElementById('continue-btn').textContent = config.continue_button || 'Continue';
|
||||
document.getElementById('next-round-btn').textContent = config.next_round_button || 'Next Round';
|
||||
|
||||
startQuiz();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Failed to load questions:', error);
|
||||
console.error('Error details:', error.message);
|
||||
alert('Failed to load questions from JSON file. Please make sure you are running a local web server (python3 -m http.server 8000).');
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user