-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
109 lines (103 loc) · 2.71 KB
/
main.js
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
marked.setOptions({
breaks: true,
headerIds: false,
});
import * as controller from './controller.js';
controller.ready.then(showCard);
function now() {
return Math.floor(Date.now() / 1000);
}
// sync
import * as gauth from './gauth.js';
gauth.execOnSignIn((auth) => {
fetch('sync', {
method: 'POST',
body: JSON.stringify(auth),
});
});
// refresh cards at least every hour
var timeout;
function refresh() {
clearTimeout(timeout);
timeout = setTimeout(() => {
showCard();
}, 60 * 60 * 1000);
}
var channel = new BroadcastChannel('sync');
async function showCard(card_id, old_card) {
refresh();
let cards = await (await fetch('cards.json')).json();
let t = now();
if (card_id &&
cards[card_id] &&
cards[card_id].d < t &&
!cards[card_id].to_delete &&
cards[card_id].e === old_card.e) {
return;
}
let id = 0;
for (let c in cards) {
if (c === 's') {
continue;
}
if (cards[c].d <= t && !cards[c].to_delete &&
(id === 0 || cards[c].i > cards[id].i)) {
id = c;
}
}
let main = document.getElementById('main');
let placeholder = document.getElementById('placeholder');
let edit = document.getElementById('edit');
let delete_ = document.getElementById('delete');
if (id === 0) {
main.style.display = 'none';
placeholder.style.display = 'block';
edit.style.display = 'none';
delete_.style.display = 'none';
channel.onmessage = (msg) => {
if (msg.data === 'change') {
showCard();
}
};
return;
}
let card = await (await fetch('card/' + id)).json();
console.log('showing card ' + id);
document.getElementById('question').innerHTML = marked(card.q);
document.getElementById('answer').innerHTML = marked(card.a);
let back = document.getElementById('back');
back.style.visibility = 'hidden';
main.onclick = () => {
document.getElementById('time').value = now();
back.style.visibility = 'visible';
main.style.cursor = 'auto';
};
main.style.cursor = 'pointer';
document.getElementById('id').value = id;
placeholder.style.display = 'none';
main.style.display = 'block';
edit.onclick = () => {
location.href =
'edit.html?id=' + id;
};
edit.style.display = 'block';
delete_.onclick = async () => {
if (window.confirm('The current card will be deleted.')) {
await fetch('card/' + id, {method: 'DELETE'});
gauth.exec((auth) => {
fetch('sync', {
method: 'POST',
body: JSON.stringify(auth),
});
});
showCard();
}
};
delete_.style.display = 'block';
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
channel.onmessage = (msg) => {
if (msg.data === 'change') {
showCard(id, cards[id]);
}
};
}