summaryrefslogtreecommitdiff
path: root/script.js
blob: d6f341527015e390c8604ec87500b21e0cc800e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/***********************************************************************
 * 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')
  }
});