-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
764 lines (602 loc) · 23.9 KB
/
index.html
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
<html><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sankey's Flashcards</title>
<meta name="description" content="Turn any spreadsheet into my favorite memorization technique.">
<meta name="author" content="Jack C. Sankey">
<style>
body {
display: flex;
flex-direction: column;
padding: 0;
margin: 0;
height: 100%;
font-family: 'Times New Roman', Times, serif;
color:Red;
font-size: 1.4em;
}
.top_controls, .bottom_controls {
display: flex;
flex-direction: row;
padding-left: 3.5px;
padding-right: 3.5px;
}
.score {
padding-left: 0.2em;
font-weight: normal;
border: solid #0774 1px;
color:#077;
background-color: #00777707;
flex-grow: 1;
}
.content {
flex-grow: 1;
display: flex;
flex-direction: column;
}
.question, .answer {
flex-grow: 1;
text-align: center;
font-size: 1.5em;
margin-left: 7px;
margin-right: 7px;
background-color: white;
}
.question {
margin-bottom: 7px;
border: solid #00F4 1px;
color:blue;
background-color: #0000FF07;
}
.answer {
margin-top: 0;
border: solid #F004 1px;
color:red;
background-color: #FF000007;
}
.center {
margin: 0;
position: relative;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
.control {
flex-grow: 1;
}
button, select{
background-color:white;
color: rgb(150, 150, 150);
border: solid rgb(197, 197, 197) 1px;
padding: 0.3em;
padding-left: 0.4em;
padding-right: 0.4em;
display: inline-block;
font-size: 0.8em;
margin: 7px 3.5px;
cursor: pointer;
font-family: 'Times New Roman', Times, serif;
font-weight: normal;
}
.voice {
margin-left: 7px;
margin-right: 7px;
margin-bottom: -1px;
margin-top: 0;
font-size: 0.8em;
font-weight: normal;
}
</style>
</head>
<body onpointerdown="first_touch();" ontouchstart="first_touch();">
<!--div class='bottom_controls'>
<select onchange='load_cards();' id='select-sheet' style='margin-bottom:0; flex-grow: 1;'></select>
</div -->
<div class='bottom_controls'>
<button onclick ='reset();' style='margin-bottom:0; flex-grow: 1;'>Reset</button>
<button onclick ='shuffle_cards();' style='margin-bottom:0; flex-grow: 1;'>Shuffle</button>
<button onclick ='flip_cards();' style='margin-bottom:0; flex-grow: 1;'>Flip</button>
</div>
<div class='top_controls'>
<button id='score' class='score' onclick='set_score(0);' >Score: 0</button>
<button id='remaining' class='score' onclick='set_remaining();' >Remaining: 0</button>
</div>
<div class='content'>
<div><select id='select-qvoice' onchange='change_qvoice();' class='question voice'></select></div>
<div onclick='show_question()' class='question'>
<div id='div-question' class='center'>Loading...</div>
</div>
<div>
<select id='select-avoice' onchange='change_avoice();' class='answer voice'
style='margin-right:0;'>></select>
<select id='select-delay' class='answer voice' onchange="save_cookie('select_delay', this.value); show_start();"
style='margin-left:0; margin-right:0;'>
<option value='0'>∞</option>
<option value='250'>1/4 s</option>
<option value='500'>1/2 s</option>
<option value='1000'>1 s</option>
<option value='2000'>2 s</option>
<option value='3000'>3 s</option>
<option value='4000'>4 s</option>
<option value='5000'>5 s</option>
<option value='7000'>7 s</option>
<option value='10000'>10 s</option>
<option value='15000'>15 s</option>
</select>
<select id='select-mode' class='answer voice' onchange="save_cookie('select_mode', this.value); show_start();"
style='margin-left:0; margin-right:0;'>>
<option value='normal'>Tap</option>
<option value='auto'>Auto</option>
</select>
</div>
<div onclick='show_answer()' class='answer' >
<div id='div-answer' class='center'> </div>
</div>
</div>
<div class='bottom_controls'>
<button id='send1' onclick='send_card_to_level(1);' class='control' style='border: solid #F004 1px; color:red'>1-2</button>
<button id='send2' onclick='send_card_to_level(2);' class='control' style='border: solid #F074 1px; color:#F07'>2-4</button>
<button id='send3' onclick='send_card_to_level(3);' class='control' style='border: solid #B0F4 1px; color:#B0F'>4-8</button>
<button id='send4' onclick='send_card_to_level(4);' class='control' style='border: solid #70F4 1px; color:#70F'>8-16</button>
<button id='send5' onclick='send_card_to_level(5);' class='control' style='border: solid #40F4 1px; color:#40F'>+</button>
<button id='send0' onclick='send_card_to_level(0);' class='control' style='border: solid #00F4 1px; color:blue'>😁</button>
</div>
<script type='text/javascript'>
var button_colors = ['#00F', '#F00', '#F07', '#B0F', '#70F', '#40F'];
// Sorry for the globals. This project got bigger than I thought, and I can't be bothered to class it up.
var cards=[]; // List of card objects
var score=0; // Score
var cards_remaining=0; // Number of not-"done" cards remaining.
var reverse=false; // Whether we show the answer first
var select_qvoice = document.getElementById('select-qvoice');
var select_avoice = document.getElementById('select-avoice');
var timer_answer = false; // timeout object created if we're timing stuff.
var timer_send_card = false; // timeout object while we show the answer before sending it.
var t_delay_ms = 0; // Delay setting
var t_question_ms = 0; // Time the question was asked
var waiting_to_being = false; // Whether we're waiting for a tap to begin.
var langs = [];
var langs_loaded = false;
var waiting_to_send = false; // Whether we're waiting for the next question in automatic mode.
var sound_maintain = new Audio('maintain.ogg');
var sound_correct = new Audio('correct.ogg');
var sound_done = new Audio('done.ogg');
// Load the timeout value and mode
var x = load_cookie('select_delay'); if(x) document.getElementById('select-delay').value = x;
x = load_cookie('select_mode'); if(x) document.getElementById('select-mode' ).value = x;
// Janky sleep
function sleep(ms) {return new Promise(resolve => setTimeout(resolve, ms));}
// Clears the existing language options and repopulates using langs.
function rebuild_lang_lists() {
// Clear the selector options
while(select_qvoice.length) select_qvoice.remove(0);
while(select_avoice.length) select_avoice.remove(0);
// Now add them all again
for(var i in langs) {
var o = document.createElement('option'); o.text = langs[i]; select_qvoice.add(o);
o = document.createElement('option'); o.text = langs[i]; select_avoice.add(o);
}
}
// Load any remembered languages
x = load_cookie('langs');
if(x) {
console.log('loaded', x);
// Get it into array form
langs = x.split(',');
rebuild_lang_lists();
}
// Otherwise, do something simple
else {
langs = ['Silent'];
var o = document.createElement('option'); o.text = 'Silent'; select_qvoice.add(o);
o = document.createElement('option'); o.text = 'Silent'; select_avoice.add(o);
}
// Load the speech cookie
if(window.speechSynthesis) {
// Load the last selected language
var x = load_cookie('select_qvoice');
if(x) {
if(!langs.includes(x)) langs.push(x);
o = document.createElement('option'); o.text = x; select_qvoice.add(o); select_qvoice.value = x;
}
// Load the last selected language
x = load_cookie('select_avoice');
if(x) {
if(!langs.includes(x)) langs.push(x);
o = document.createElement('option'); o.text = x; select_avoice.add(o); select_avoice.value = x;
}
}
// Saves a cookie
function save_cookie(key, value, expire_days) { console.log('save_cookie()', key, value);
if(expire_days==undefined) expire_days=28;
// Get rid of weird key characters
key = encodeURIComponent(key+'_'+window.location.search);
value = encodeURIComponent(value);
// get the expiration date
var d = new Date();
d.setTime(d.getTime() + (expire_days*24*60*60*1000));
// now write the cookie string
document.cookie = "Sankey_Flashcards_"+key + '=' + value + '; expires=' + d.toUTCString() + '; SameSite=Lax';
}
// Returns the cookie value or undefined
function load_cookie(key) {
key = encodeURIComponent(key+'_'+window.location.search);
// get a list of the cookie elements
var cs = document.cookie.split(';');
// Default is empty string
var value = null;
// Loop over elements to find the key
for(var i=0; i<cs.length; i++) {
// split by "=" sign
s = cs[i].split('=');
// strip white space
while (s[0].charAt(0)==' ') s[0] = s[0].substring(1);
// If it's our key
if(s[0] == 'Sankey_Flashcards_'+key) {
value = decodeURIComponent(s[1]);
break;
};
}
console.log('load_cookie()', key, value);
return value;
}
// Loads the cards for the selected sheet
function load_cards(csv_string) {
// Break the csv into lines
var lines = csv_string.split(/\r?\n/);
// Pop the first line (column labels assumed)
lines.splice(0,1);
console.log(lines);
// For each line, split by comma to get the front and back
cards = []; // Messed up deck
cards0 = []; // Unshuffled / original deck
for(var l in lines) {
// Split by comma and make sure there are enough non-empty elements.
var s = lines[l].split('\t');
if(s.length < 2 || s[0] == '' || s[1] == '') continue;
// Each card is an object with a front, back, and extra data for sorting etc.
cards0.push({q:s[0], a:s[1], level:1, id:cards0.length});
}
// If there is no deck
if(!cards0.length) {
document.getElementById('div-question').innerHTML = 'No cards?';
return
}
// If we have a cookie for each card's level, load this
var x = load_cookie('levels');
if(x != null) {
// The cookie should be a list with index being id and value being card level
x = eval('['+x+']');
for(var id=0; id<x.length; id++)
// If we have this card loaded, update the level
if(id < cards0.length) cards0[id].level = x[id];
console.log(cards0);
}
// If we have a cookie, e.g., '4,2,3,1,5', load the previous order
var x = load_cookie('order');
if(x != null) {
// The cookie should be a list with index being id and value being position in deck
x = eval('['+x+']');
// Loop over cards, reordering. cards0 is ordered by id;
for(var id=0; id<x.length; id++)
// If we have this card loaded, put it into the "real" deck
if(id < cards0.length) cards[x[id]] = cards0[id]
// Now include the remainder, in case cards have since been added.
cards = cards.concat(cards0.slice(cards.length))
// Clean out any empty slots
for(n=0; n<cards.length; n++) if(cards[n]==undefined) {cards.splice(n,1); n--;}
}
// Otherwise, just use the given order
else cards = [...cards0];
// Reset the number of cards remaining to practice
set_remaining(load_cookie('remaining'));
// Show the top card
show_start();
}
function reset() {
// Reset the cards
cards = [...cards0];
// Reset remaining
set_remaining();
// Reset the score
set_score(0);
// Show the top card
show_start();
}
function flip_cards() {
reverse = !reverse;
save_cookie('reverse', reverse);
var x = select_qvoice.value;
select_qvoice.value = select_avoice.value;
select_avoice.value = x;
show_start();
}
// Update score
function set_score(n) {
score=n;
document.getElementById('score').innerHTML = 'Score: '+String(n);
}
// Update the cards remaining
function set_remaining(n) { console.log('set_remaining()', n);
if(n == undefined || n == null) n = cards.length;
n = Math.max(n,0);
n = Math.min(cards.length,n);
// Remember this
cards_remaining = n;
// Save the cookie & update the user
save_cookie('remaining', n);
document.getElementById('remaining').innerHTML = 'Remaining: '+String(n)+'/'+String(cards.length);
}
// Returns a random integer from m to n
function random_integer(m,n) {
var y = Math.floor(Math.random()*(1+n-m))+m;
// exceedingly rare case
if(y > n) y = n;
return parseInt(y);
}
// randomizes the order of the supplied array (in place)
function shuffle_array(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
var randomIndex = random_integer(0, currentIndex-1);
currentIndex -= 1;
// And swap it with the current element.
var temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// Shuffles the deck
function shuffle_cards(original_order) { console.log('shuffle_cards()');
// Do the actual shuffling
if(original_order) cards = cards0;
else cards = shuffle_array(cards);
// Reset the deck
set_remaining(cards.length);
// Reset the levels
for(var i in cards) cards[i].level=1;
// Save the levels and order
save_levels();
save_order();
// Show the starting card
show_start();
}
function change_qvoice() {
save_cookie('select_qvoice', select_qvoice.value);
show_question();
}
function change_avoice() {
save_cookie('select_avoice', select_avoice.value);
show_answer();
}
function save_levels() {
var levels = [];
for(var i=0; i<cards0.length; i++) levels[i] = cards0[i].level;
save_cookie('levels', levels);
}
function save_order() {
var order = [];
for(var i=0; i<cards.length; i++) order[cards[i].id] = i;
save_cookie('order', order);
}
// Sends the top card to index n. 0 means "bottom", undefined means "random > 2"
function send_card_to_level(n) {
console.log('send_card_to_level()', n);
waiting_to_send = false;
// Clear the timer if it exists, to avoid double-calls
if(timer_send_card) clearTimeout(timer_send_card);
// Get the top card
var c = cards.splice(0,1)[0];
// Remember the level
c.level = n;
// Button levels
var N = cards_remaining-1; // max "live" index
if (n <= 0) { set_score(score+10); cards.push(c); set_remaining(N);}
else if(n <= 1) { set_score(score+1); cards.splice(random_integer(Math.min(1,N),Math.min(2,N)), 0, c); }
else if(n == 2) { set_score(score+2); cards.splice(random_integer(Math.min(2,N),Math.min(4,N)), 0, c); }
else if(n == 3) { set_score(score+3); cards.splice(random_integer(Math.min(4,N),Math.min(8,N)), 0, c); }
else if(n == 4) { set_score(score+4); cards.splice(random_integer(Math.min(8,N),Math.min(16,N)), 0, c); }
else if(n == 5) { set_score(score+5); cards.splice(random_integer(Math.min(16,N), N), 0, c); }
// Get and save the levels
save_levels();
// Get and save order
save_order();
// Clear any button highlights
clear_buttons();
// Show the top card
show_question();
}
// Speaks the supplied div's html in the supplied language
function speak(qa, lang) {
if(lang != 'Silent') {
var utterance = new SpeechSynthesisUtterance();
utterance.lang = lang;
//utterance.voice = speechSynthesis.getVoices()[3];
utterance.text = qa.innerText;
window.speechSynthesis.speak(utterance);
}
}
// Shows the start message
function show_start() {
// Clear the timers
if(timer_answer) clearTimeout(timer_answer);
if(timer_send_card) clearTimeout(timer_send_card)
// Clear any buttons that are lit
clear_buttons();
document.getElementById('div-question').innerHTML = '(tap to begin)';
document.getElementById('div-answer' ).innerHTML = ' ';
waiting_to_begin = true;
}
// Any tap on the window loads languages;
// This function stays bound to the touch events until more than one language loads.
// Annoying, true, but the voiceschanged event doesn't work on android for some reason. :\
function first_touch() { console.log('first_touch()');
// Check for language support
if(!langs_loaded && window.speechSynthesis) {
// Get the available voice languages
langs = [];
var select_qvoice = document.getElementById('select-qvoice');
var select_avoice = document.getElementById('select-avoice');
// Get voices (and quit if none come back)
voices = window.speechSynthesis.getVoices();
if(!voices.length) return;
// Only do this once; remove event handler
document.body.ontouchstart = undefined;
document.body.onpointerdown = undefined;
// If we don't already have the language of each voice, add it to Q and A
for(var n in voices) if(!langs.includes(voices[n].lang)) langs.push(voices[n].lang);
// Alphabetize the languages
langs.sort();
langs.splice(0,0,'Silent');
// Remember the languages
save_cookie('langs', langs);
// Remember the current selection
var qvoice = select_qvoice.value;
var avoice = select_avoice.value;
// Rebuild the lists
rebuild_lang_lists();
// Put in the previous values
select_qvoice.value = qvoice;
select_avoice.value = avoice;
// No need to repeat this
langs_loaded = false;
}
// Remove this event listener
}
// Displays the top card qestion & answer
function show_question() { console.log('show_question()');
waiting_to_begin = false;
// If we're waiting for the automatic next card timeout
if(waiting_to_send) {
// Stop the timer
clearTimeout(timer_send_card);
waiting_to_send = false;
// Remove button colors
clear_buttons();
// Send it.
send_card_to_level(cards[0].level);
return;
}
var q = document.getElementById('div-question');
var a = document.getElementById('div-answer')
// If it's zero, we show a special card
if(cards_remaining<=0) {
q.innerHTML = 'All done!';
a.innerHTML = ' ';
}
// Otherwise show the top card
else if(cards[0]) {
if(reverse) q.innerHTML = cards[0].a;
else q.innerHTML = cards[0].q;
if(q.innerHTML == '') q.innerHTML = ' '
a.innerHTML = ' '
}
// Delay the show
t_question_ms = Date.now();
t_delay_ms = parseInt(document.getElementById('select-delay').value);
if(t_delay_ms) timer_answer = setTimeout(show_answer, t_delay_ms);
// And say it
speak(q, select_qvoice.value);
}
// Show the answer and say it.
function show_answer() { console.log('show_answer()');
// If we're waiting to begin, show question and poop out
if(waiting_to_begin) { show_question(); return; }
// If we're waiting to show, cancel the timer.
if(timer_answer) clearTimeout(timer_answer);
timer_answer = false;
// Get the divs containing the data
var q = document.getElementById('div-question');
var a = document.getElementById('div-answer')
// Special case, we're done!
if(cards_remaining<=0) {
set_remaining(cards.length);
show_start();
}
// Otherwise proceed
else if(cards[0]) {
if(reverse) a.innerHTML = cards[0].q;
else a.innerHTML = cards[0].a;
if(a.innerHTML == '') a.innerHTML = ' ';
}
// And say it
speak(a, select_avoice.value);
// In auto mode (if we're not already waiting to send it), decide where to put the card based on the time of the answer
if(!waiting_to_send && document.getElementById('select-mode').value == 'auto' && t_delay_ms) {
// Get the card
var c = cards[0];
// Get the fractional time it took to answer
var r = (Date.now()-t_question_ms)/t_delay_ms;
// If we time out, downgrade
if(r > 0.999) { c.level = Math.max(1, c.level-1); }
// If we're sufficiently fast, upgrade and make a happy noise
else if (r < 0.34) {
c.level = c.level+1;
if(c.level > 5) { c.level=0; sound_done .play(); }
else sound_correct.play();
}
// Maintain
else sound_maintain.play();
// Light the button for a moment
light_button(c.level);
// Send it
waiting_to_send = true;
timer_send_card = setTimeout(send_card_to_level, 4000, c.level);
}
// If we're waiting to send, reset that clock
if(waiting_to_send) {
clearTimeout(timer_send_card);
timer_send_card = setTimeout(send_card_to_level, 4000, cards[0].level);
}
}
// Lights up the button of the appropriate level.
function light_button(level) {
var b = document.getElementById('send'+String(level));
b.style.transition = 'background-color 0.1s';
b.style.setProperty('background-color', button_colors[level]+'3');
}
// Clears any button lights.
function clear_buttons() {
for(var i=0; i<=5; i++) {
var b = document.getElementById('send'+String(i));
b.style.transition = 'background-color 0.5s';
b.style.setProperty('background-color', button_colors[i]+'0');
}
}
// Actually queries the external website (any csv url)
function get_csv_data_from_url() {
// Get the csv data from the supplied URL
//
// Start with getting the URL from the part after the ? in the address bar
var url = window.location.search;
//
// And drop the ?
url = url.substr(1, url.length);
console.log(url);
//
// Now get the data from the URL in a reasonably cross-browser way
if (window.XMLHttpRequest) var xmlhttp = new XMLHttpRequest(); // Code for IE7+, Firefox, Chrome, Opera, Safari
else var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Code for IE6, IE5 (gross)
//
// Set up the handler for when the process reaches its conclusion
xmlhttp.onreadystatechange=function() {
// When we reach state 4, things are loaded.
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
// Load the cards for this value
load_cards(xmlhttp.responseText);
// If we're reversed, do this
if(load_cookie('reverse') == 'true') flip_cards();
}
};
//
// Start the process
xmlhttp.open("GET", url, false);
xmlhttp.send();
}
// Start the show!
get_csv_data_from_url();
</script>
</body></html>