summaryrefslogtreecommitdiff
path: root/srs-anywhere.js
diff options
context:
space:
mode:
Diffstat (limited to 'srs-anywhere.js')
-rw-r--r--srs-anywhere.js57
1 files changed, 50 insertions, 7 deletions
diff --git a/srs-anywhere.js b/srs-anywhere.js
index 6dc82bf..0931b07 100644
--- a/srs-anywhere.js
+++ b/srs-anywhere.js
@@ -22,6 +22,25 @@ function reportException(e) {
}
/***********************************************************************
+ * Card types and dispatch *
+ ***********************************************************************/
+
+const CARD_TYPES = {
+ "default": {
+ "validate": function (card) {
+ if (!card.front) {
+ throw new Error(`malformed card ${card.id}: missing front`)
+ }
+ if (!card.back) {
+ throw new Error(`malformed card ${card.id}: missing back`)
+ }
+ },
+ "front": (card) => card.front,
+ "back": (card) => card.back
+ }
+};
+
+/***********************************************************************
* Actual FSRS5 algorithm *
***********************************************************************/
@@ -148,11 +167,26 @@ 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";
+
+ const cardType = CARD_TYPES[card.type];
+
+ window.card.front.innerHTML = cardType.front(card);
+ window.card.back.innerHTML = cardType.back(card);
+ if (typeof cardType.showFront === "function") {
+ window.card.showFront = () => cardType.showFront(card);
+ } else {
+ window.card.showFront = () => null;
+ }
+ if (typeof cardType.showBack === "function") {
+ window.card.showBack = () => cardType.showBack(card);
+ } else {
+ window.card.showBack = () => null;
+ }
+
+ window.card.showFront();
}
function flipCard() {
@@ -162,13 +196,14 @@ 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";
+ window.card.showFront();
} else {
window.card.front.style.display = "none";
window.card.back.style.display = "block";
+ window.card.showBack();
}
// Reveal the buttons.
@@ -280,11 +315,19 @@ function checkCards(cards) {
const card = cards[i];
if (!card.id) {
throw new Error("malformed card: missing id")
- } else if (!card.front) {
- throw new Error(`malformed card ${card.id}: missing front`)
- } else if (!card.back) {
- throw new Error(`malformed card ${card.id}: missing bacak`)
}
+
+ if (!card.type) {
+ card.type = "default";
+ }
+ if (typeof CARD_TYPES[card.type].front !== "function") {
+ throw new Error(`malformed card type ${card.type}: front not callable`);
+ }
+ if (typeof CARD_TYPES[card.type].back !== "function") {
+ throw new Error(`malformed card type ${card.type}: back not callable`);
+ }
+ CARD_TYPES[card.type].validate(card);
+
if (!card._scheduled) {
card._scheduled = today().toJSON();
}