-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.js
288 lines (260 loc) Β· 8.61 KB
/
learn.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
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
function Learn() {
this.viewButton = document.getElementById("view-button");
this.trainButton = document.getElementById("train-button");
this.volumeSlider = document.getElementById("volume-slider");
this.courseSelect = document.getElementById("course-select");
this.viewContent = document.getElementById("view-content");
this.trainContent = document.getElementById("train-content");
this.timers = [];
this.volumeSlider.value = window.localStorage.volume || this.volumeSlider.value;
this.courseSelect.value = window.localStorage.lastCourse || this.courseSelect.value;
this.jsLocation = "./extracted/";
this.bindEvents();
}
Learn.prototype.bindEvents = function() {
this.viewButton.onclick = (e) => {
this.clearCourse();
this.loadCourse((e) => this.viewCourse(window[this.courseSelect.value]));
};
this.trainButton.onclick = (e) => {
this.clearCourse();
this.loadCourse((e) => this.trainCourse(window[this.courseSelect.value]));
};
this.volumeSlider.onchange = (e) => {
window.localStorage.volume = this.volumeSlider.value;
};
this.courseSelect.onchange = (e) => {
window.localStorage.lastCourse = this.courseSelect.value;
};
};
Learn.prototype.loadCourse = function(cb) {
let script = document.createElement("script");
script.onload = cb;
script.src = this.jsLocation + this.courseSelect.value + ".js";
document.head.appendChild(script);
};
Learn.prototype.trainCourse = async function(json) {
let courseId = json.id;
let items = json.goal_items;
//Shuffle items
for (let i = items.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[items[i], items[j]] = [items[j], items[i]];
}
//Show one at a time
for(let i = 0; i < items.length; i++) {
this.clearCourse();
await this.trainItem(courseId, items[i]);
}
let finished = document.createElement("p");
finished.innerText = "Finished [" + json.title + "].";
this.trainContent.appendChild(finished);
};
Learn.prototype.trainItem = async function(courseId, item) {
return new Promise((resolve, reject) => {
//Show single item
let word = document.createElement("word");
word.className = "row";
let basic = document.createElement("basic");
basic.className = "col-4";
let basicDiv = document.createElement("div");
//Sound
let sound = document.createElement("sound");
let audio = new Audio(
this.jsLocation + courseId + "/" +
encodeURIComponent(encodeURIComponent(item.sound))
);
sound.innerText = "π";
audio.oncanplay = (e) => {
audio.volume = parseInt(this.volumeSlider.value) / 100;
audio.play();
};
basic.onclick = (e) => audio.play();
basic.appendChild(sound);
//Div
let jp1 = document.createElement("japanese");
jp1.innerText = item.item.cue.text;
basicDiv.appendChild(jp1);
let hira = item.item.cue.transliterations.Hira;
let hrkt = item.item.cue.transliterations.Hrkt;
if(hrkt && hrkt !== item.item.cue.text) {
let h = document.createElement("japanese");
h.innerText = hrkt;
basicDiv.appendChild(h);
} else if(hira && hira !== item.item.cue.text) {
let h = document.createElement("japanese");
h.innerText = hira;
basicDiv.appendChild(h);
}
let eng = document.createElement("english");
eng.className = "text-hidden";
eng.innerText = this.capitalise(item.item.response.text);
basicDiv.appendChild(eng);
basic.appendChild(basicDiv);
//Type
let part = document.createElement("type");
part.innerText = item.item.cue.part_of_speech;
basic.appendChild(part);
//sentences
let sentences = document.createElement("sentences");
sentences.className = "col";
for(let i = 0; i < item.sentences.length; i++) {
let sentence = document.createElement("sentence");
//Sound
let ss = document.createElement("sound");
let sa = new Audio(
this.jsLocation + courseId + "/" +
encodeURIComponent(encodeURIComponent(item.sentences[i].sound))
);
ss.innerText = "π";
sentence.onclick = (e) => {
sa.volume = parseInt(this.volumeSlider.value) / 100;
sa.play();
};
sentence.appendChild(ss);
//Div
let sentenceDiv = document.createElement("div");
let jp = document.createElement("japanese");
jp.innerHTML = item.sentences[i].cue.text;
sentenceDiv.appendChild(jp);
let eng = document.createElement("english");
eng.className = "text-hidden";
eng.innerHTML = item.sentences[i].response.text;
sentenceDiv.appendChild(eng);
sentence.appendChild(sentenceDiv);
sentences.appendChild(sentence);
}
word.appendChild(basic);
word.appendChild(sentences);
this.trainContent.appendChild(word);
//Next button
let btn = document.createElement("button");
btn.innerText = "Show answer";
btn.onclick = () => {
if(btn.innerText === "Show answer") {
let hidden = word.querySelectorAll(".text-hidden");
for(let i = 0; i < hidden.length; i++) {
hidden[i].classList.remove("text-hidden");
}
btn.innerText = "Show next";
} else if(btn.innerText === "Show next") {
resolve();
btn.parentElement.removeChild(btn);
}
};
this.trainContent.appendChild(word);
this.trainContent.appendChild(btn);
});
};
Learn.prototype.viewCourse = function(json) {
let courseId = json.id;
let items = json.goal_items;
this.timers = [];
for(let i = 0; i < items.length; i++) {
this.timers[i] = setTimeout(() => this.viewItem(courseId, items[i]), i * 60);
}
};
Learn.prototype.clearCourse = function() {
//Clear timers
for(let i = 0; i < this.timers.length; i++) {
clearTimeout(this.timers[i]);
}
this.timers = [];
while(this.viewContent.firstChild) {
this.viewContent.removeChild(this.viewContent.firstChild);
}
while(this.trainContent.firstChild) {
this.trainContent.removeChild(this.trainContent.firstChild);
}
};
Learn.prototype.viewItem = function(courseId, item) {
let word = document.createElement("word");
word.className = "row";
let basic = document.createElement("basic");
basic.className = "col-4";
let basicDiv = document.createElement("div");
//Sound
let sound = document.createElement("sound");
let audio = new Audio(
this.jsLocation + courseId + "/" +
encodeURIComponent(encodeURIComponent(item.sound))
);
sound.innerText = "π";
audio.oncanplay = (e) => sound.innerText = "π";
basic.onclick = (e) => {
audio.volume = parseInt(this.volumeSlider.value) / 100;
audio.play();
};
basic.appendChild(sound);
//Div
let jp1 = document.createElement("japanese");
jp1.innerText = item.item.cue.text;
basicDiv.appendChild(jp1);
let hira = item.item.cue.transliterations.Hira;
let hrkt = item.item.cue.transliterations.Hrkt;
if(hrkt && hrkt !== item.item.cue.text) {
let h = document.createElement("japanese");
h.innerText = hrkt;
basicDiv.appendChild(h);
} else if(hira && hira !== item.item.cue.text) {
let h = document.createElement("japanese");
h.innerText = hira;
basicDiv.appendChild(h);
}
let eng = document.createElement("english");
eng.innerText = this.capitalise(item.item.response.text);
basicDiv.appendChild(eng);
basic.appendChild(basicDiv);
//Type
let part = document.createElement("type");
part.innerText = item.item.cue.part_of_speech;
basic.appendChild(part);
//sentences
let sentences = document.createElement("sentences");
sentences.className = "col";
for(let i = 0; i < item.sentences.length; i++) {
let sentence = document.createElement("sentence");
//Sound
let ss = document.createElement("sound");
let sa = new Audio(
this.jsLocation + courseId + "/" +
encodeURIComponent(encodeURIComponent(item.sentences[i].sound))
);
ss.innerText = "π";
sa.oncanplay = (e) => {
ss.innerText = "π";
};
sentence.onclick = (e) => {
sa.volume = parseInt(this.volumeSlider.value) / 100;
sa.play();
};
sentence.appendChild(ss);
//Div
let sentenceDiv = document.createElement("div");
let jp = document.createElement("japanese");
jp.innerHTML = item.sentences[i].cue.text;
sentenceDiv.appendChild(jp);
let hira = item.sentences[i].cue.transliterations.Hira;
let hrkt = item.sentences[i].cue.transliterations.Hrkt;
if(hrkt && hrkt.replace(/\s/g, "") !== item.sentences[i].cue.text) {
let h = document.createElement("japanese");
h.innerHTML = hrkt;
sentenceDiv.appendChild(h);
} else if(hira && hira.replace(/\s/g, "") !== item.sentences[i].cue.text) {
let h = document.createElement("japanese");
h.innerHTML = hira;
sentenceDiv.appendChild(h);
}
let eng = document.createElement("english");
eng.innerHTML = item.sentences[i].response.text;
sentenceDiv.appendChild(eng);
sentence.appendChild(sentenceDiv);
sentences.appendChild(sentence);
}
word.appendChild(basic);
word.appendChild(sentences);
this.viewContent.appendChild(word);
};
Learn.prototype.capitalise = function(str) {
return str[0].toUpperCase() + str.slice(1);
};