-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeclgrid.js
149 lines (119 loc) · 4 KB
/
declgrid.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
function paramVal(name, def) {
return url.searchParams.has(name) ? url.searchParams.get(name) : def;
}
function paramNumVal(name, def) {
return url.searchParams.has(name) ? Number.parseInt(url.searchParams.get(name)) : def;
}
var url = new URL(location.href);
var decl = paramVal("decl", 0);
var start = paramNumVal("start", 1);
var end = paramNumVal("end", 40);
if (end < start)
end = start;
function buildHeader(word) {
var header = document.createElement("tr");
var td = document.createElement("td");
td.innerText = word.latin;
header.appendChild(td);
td = document.createElement("td");
td.innerText = "Singular";
header.appendChild(td);
td = document.createElement("td");
td.innerText = "Plural";
header.appendChild(td);
return header;
}
function createForm(word, count, type, plural) {
var form = document.createElement("form");
form.autocomplete = false;
form.autocorrect = false;
form.spellcheck = false;
form.onsubmit = (() => checkAnswer(count, type, plural, word.latin, word.declension, word.gender, false));
var input = document.createElement("input");
input.id = `${plural}${type}${count}`;
input.type = "text";
input.autocomplete = false;
input.autocorrect = false;
input.spellcheck = false;
form.appendChild(input);
return form;
}
function createResult(word, count, type, plural) {
var label = document.createElement("label");
label.id = `result${plural}${type}${count}`;
label.innerHTML = '<font color="white">Incorrect!</font>';
return label;
}
function buildTypes(word, count) {
return ["Nom", "Gen", "Dat", "Acc", "Abl", "Voc"].map((type) => {
var tr, td, form;
tr = document.createElement("tr");
td = document.createElement("td");
td.innerText = type + ".";
tr.appendChild(td);
td = document.createElement("td");
if (!word.plural) {
td.appendChild(createForm(word, count, type, 1));
}
tr.appendChild(td);
td = document.createElement("td");
td.appendChild(createResult(word, count, type, 1));
tr.appendChild(td);
td = document.createElement("td");
td.appendChild(createForm(word, count, type, 2));
tr.appendChild(td);
td = document.createElement("td");
td.appendChild(createResult(word, count, type, 2));
tr.appendChild(td);
return tr;
});
}
function buildRows(word, count) {
return [buildHeader(word)].concat(buildTypes(word, count));
}
function buildSeparator() {
var tr = document.createElement("tr");
var td = document.createElement("td");
td.colspan = 5;
td.innerHTML = '<hr />';
tr.appendChild(td);
return tr;
}
function buildTable(vocab) {
var decltable = document.getElementById("decltable");
var list = vocab.filter((e) =>
e.chapter >= start && e.chapter <= end &&
e.type == "noun" &&
(decl == 0 || e.declension == decl));
if (!list.length) {
decltable.innerHTML = "<tr><td>There aren't any nouns to quiz you on!</td></tr>";
return;
}
var count = (start == end) ? list.length : 10;
while (count && list.length) {
var rand = Math.floor(Math.random() * list.length);
var word = list.splice(rand, 1)[0];
var trs = buildRows(word, count);
trs.forEach((tr) => decltable.appendChild(tr));
count--;
if (count && list.length) {
decltable.appendChild(buildSeparator());
}
}
}
function createChapterOptions(id, selected) {
var selector = document.getElementById(id);
for (var i = 1; i <= 40; i++) {
var opt = document.createElement("option");
opt.value = i;
opt.text = i;
opt.selected = (i === selected);
selector.appendChild(opt);
}
}
function buildDeclPage() {
GetVocab(buildTable);
createChapterOptions("declstart", start);
createChapterOptions("declend", end ? end : 40);
}
addEventListener("load", buildDeclPage);