summaryrefslogtreecommitdiff
path: root/script.js
diff options
context:
space:
mode:
Diffstat (limited to 'script.js')
-rw-r--r--script.js107
1 files changed, 107 insertions, 0 deletions
diff --git a/script.js b/script.js
new file mode 100644
index 0000000..94a2fa3
--- /dev/null
+++ b/script.js
@@ -0,0 +1,107 @@
+/***********************************************************************
+ * State file processing *
+ ***********************************************************************/
+
+function makeDeck(cards) {
+ let today = new Date();
+ // Granularity only at the day level.
+ today.setHours(0, 0, 0, 0);
+ return cards.filter((card) => {
+ return (new Date(card.scheduled)) <= today;
+ });
+}
+
+function pad(n) {
+ return n < 10 ? '0' + n : n;
+}
+
+function statusString() {
+ const now = Date.now();
+ const elapsedSeconds = Math.floor((now - window.started) / 1000);
+ const minutes = Math.floor(elapsedSeconds / 60);
+ const seconds = elapsedSeconds % 60;
+ return `Card ${window.index + 1} of ${window.deck.length} scheduled for review, ${window.cards.length} total
+Current session: ${pad(minutes)}:${pad(seconds)}`;
+}
+
+function showCard(card) {
+ const front = document.getElementById('flashcard-front');
+ const back = document.getElementById('flashcard-back');
+
+ front.innerHTML = card.front;
+ back.innerHTML = card.back;
+}
+
+function toggleCard() {
+ const front = document.getElementById('flashcard-front');
+ const back = document.getElementById('flashcard-back');
+
+ if (front.style.display === "block") {
+ front.style.display = "none";
+ back.style.display = "block";
+ } else {
+ back.style.display = "none";
+ front.style.display = "block";
+ }
+}
+
+function loadCards(data) {
+ const statusInfo = document.getElementById('status-info');
+
+ window.cards = data;
+ window.deck = makeDeck(window.cards);
+ window.index = 0;
+ window.started = Date.now();
+
+ // Temporary
+ showCard(window.cards[0]);
+
+ setInterval(() => { statusInfo.innerText = statusString() }, 1000);
+ // Add an event listener for the spacebar press
+ window.addEventListener('keydown', (event) => {
+ if (event.code === 'Space') {
+ toggleCard();
+ }
+ });
+}
+
+/***********************************************************************
+ * 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
+ });
+});