/*********************************************************************** * State file processing * ***********************************************************************/ function statusString() { const now = Date.now(); const elapsedSeconds = Math.floor((now - window.started) / 1000); const minutes = Math.floor(elapsedSeconds / 60); const seconds = elapsedSeconds % 60; const pad = (n) => n < 10 ? '0' + n : n; return `Card ${window.index + 1} of ${window.cards.length} scheduled for review, ${window.allCards.length} total Current session: ${pad(minutes)}:${pad(seconds)}`; } function loadCard(card) { window.card.revealed = false; window.card.front.innerHTML = card.front; window.card.front.style.display = "block"; window.card.back.innerHTML = card.back; window.card.back.style.display = "none"; window.buttons.style.display = "none"; } function flipCard() { window.card.revealed = true; // Reveal the "back". if (window.card.front.style.display !== "block") { window.card.front.style.display = "block"; window.card.back.style.display = "none"; } else { window.card.front.style.display = "none"; window.card.back.style.display = "block"; } // Reveal the buttons. window.buttons.style.display = "flex"; } function rateCard(difficulty) { if (!window.card.revealed) { return; } // if (difficulty === "forgot") { // } if (++window.index < window.cards.length) { loadCard(window.cards[window.index]); } else { clearInterval(window.updateInterval); window.statusInfo.innerHTML = "Session complete!"; window.flashcard.style.display = "none"; window.buttons.style.display = "none"; } } // Todo: check if card is revealed before allowing button inpt. function loadCards(data) { document.getElementById('flashcard').style.display = "block"; window.allCards = data; window.index = 0; window.started = Date.now(); window.statusInfo = document.getElementById('status-info'); window.buttons = document.getElementById('button-container'); window.card = { front: document.getElementById('flashcard-front'), back: document.getElementById('flashcard-back') }; document.getElementById('rating-button-forgot') .addEventListener('click', () => rateCard('forgot')); document.getElementById('rating-button-hard') .addEventListener('click', () => rateCard('hard')); document.getElementById('rating-button-medium') .addEventListener('click', () => rateCard('medium')); document.getElementById('rating-button-easy') .addEventListener('click', () => rateCard('easy')); let today = new Date(); // Granularity only at the day level. today.setHours(0, 0, 0, 0); window.cards = data.filter((card) => { return (new Date(card.scheduled)) <= today; }); // Temporary loadCard(window.cards[0]); window.updateInterval = setInterval(() => { window.statusInfo.innerText = statusString() }, 1000); } /*********************************************************************** * Status display * ***********************************************************************/ function showToast(message, duration = 3000) { const toast = document.getElementById("toast"); toast.textContent = message; // Show the toast toast.classList.add("show"); // Hide the toast after specified time setTimeout(() => { toast.classList.remove("show"); }, duration); } window.addEventListener("load", (event) => { document.getElementById('cards-input').addEventListener('change', function(event) { const file = event.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = function(e) { try { const content = e.target.result; loadCards(JSON.parse(content)); } catch (error) { console.error("JSON Parsing Error:", error); } }; reader.onerror = function(error) { document.getElementById('result').textContent = "Error reading file: " + error; console.error("File Reading Error:", error); }; reader.readAsText(file); // Read the file as text }); }); window.addEventListener('keydown', (event) => { // console.log(event.code); if (event.code === 'Space') { flipCard(); } else if (event.code === 'Digit1' || event.code === "Numpad1") { rateCard('forgot') } else if (event.code === 'Digit2' || event.code === "Numpad2") { rateCard('easy') } else if (event.code === 'Digit3' || event.code === "Numpad3") { rateCard('medium') } else if (event.code === 'Digit4' || event.code === "Numpad4") { rateCard('hard') } });