-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
343 lines (285 loc) · 11.5 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tiripode - Parse</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<style>
/* TODO move to styles.css */
.form-control-dark {
border-color: var(--bs-gray);
}
.form-control-dark:focus {
border-color: #fff;
box-shadow: 0 0 0 .25rem rgba(255, 255, 255, .25);
}
.text-small {
font-size: 85%;
}
.dropdown-toggle:not(:focus) {
outline: 0;
}
</style>
<script>
// TODO move theming to theme.js
// Set theme to the user's preferred color scheme
function updateTheme() {
const colorMode = window.matchMedia("(prefers-color-scheme: dark)").matches ?
"dark" :
"light";
document.querySelector("html").setAttribute("data-bs-theme", colorMode);
}
async function fetchParse(word) {
// console.log("hi in fetch parse")
const parseResponse = await fetch("https://tech189.dev/tiripode/api.php?mode=parse&word=".concat(word));
const parsings = await parseResponse.json();
return parsings;
}
async function fetchDict(idList) {
// console.log("in fetch dict")
const responseTemplate = "https://tech189.dev/tiripode/api.php?mode=lookup&word=";
// make a list of promises for each entry to look up
const promises = idList.map(async i => {
const fetchData = await fetch(responseTemplate.concat(i));
return fetchData.json();
})
// debug
// console.log(promises)
// console.log(Promise.all(promises))
// fetch all the entries at once, wait for all to finish before moving on
const responses = await Promise.all(promises);
return responses;
}
function parse() {
let resultsArea = document.getElementById("resultsArea");
let loadingSpinner = document.getElementById("loadingSpinner");
const possibleFormsTitle = document.getElementById("possibleFormsTitle");
const possibleForms = document.getElementById("possibleForms");
const errorBox = document.getElementById("errorBox");
const readyText = document.getElementById("readyText");
const notesBox = document.getElementById("notesBox");
// clear div for new forms
possibleFormsTitle.textContent = "";
possibleForms.replaceChildren();
// show results area and spinner while results are being fetched
resultsArea.style.display = "";
loadingSpinner.style.display = "";
loadingSpinner.classList.add("d-flex");
errorBox.style.display = "none";
readyText.style.display = "none";
notesBox.style.display = "none";
let word = document.getElementById("wordBox").value
if (word === "") {
word = "please help am not good with computer"
// errorBox.style.display = "";
// errorBox.textContent = "Please why did you put that in the box smh";
}
word = word.trim().replace(/\s+/g, " ").replaceAll(" ", "-").toLowerCase();
let dict_ids = [];
try {
fetchParse(word).then(
(parsings) => {
if ("error" in parsings && errorBox.value !== "Error!") {
errorBox.style.display = "";
errorBox.textContent = parsings["error"];
}
// get a list of dict_ids - the words the results could be from
for (let i = 0; i < parsings.length; i++) {
if (!dict_ids.includes(parsings[i][0])) {
dict_ids.push(parsings[i][0])
}
}
fetchDict(dict_ids).then(
(entries) => {
for (let i = 0; i < entries.length; i++) {
// console.log("loop time :)")
// sort and join by new lines
let stems = entries[i]["stem"].split(",").sort().join("<br>");
// add counter if more than 1 result
let cardHeader = entries[i]["word"] + " (" + entries[i]["category"] + ")";
if (entries.length > 1) {
cardHeader = "Word " + (i + 1) + ': ' + cardHeader;
}
const templateCard = '<div class="card"> <div id="wordHeader" class="card-header"> ' + cardHeader + ' </div> <div class="card-body"> <h5 class="card-title">Definition</h5> <p id="dictionaryEntry" class="card-text">' + entries[i]["definition"] + '</p> <h5 class="card-title">Possible stems</h5> <p>' + stems + '</p> <h5 class="card-title">Possible forms</h5> <table class="table"> <thead> <tr> <th scope="col">Declension</th> <th scope="col">Case</th> <th scope="col">Gender</th> <th scope="col">Number</th> <th scope="col">Ending</th> </tr> </thead> <tbody id="resultTableBody' + i + '"> </tbody> </table> </div> </div> <br />';
possibleForms.insertAdjacentHTML('beforeend', templateCard);
// work out how many rows needed for one declension rowspan
let declensionLengths = {};
parsings.forEach(element => {
if (element[1] in declensionLengths) {
declensionLengths[element[1]] += 1;
}
else {
declensionLengths[element[1]] = 1;
}
});
console.log(declensionLengths)
// put the rows of forms into each entry table, counter keeps track of how many added so far
let formCounter = 0;
let previousEntry = 0;
for (let j = 0; j < parsings.length; j++) {
// element is current form
const element = parsings[j];
// new entry_id so new entry, move to next table, reset counter
if (entries[i]["entry_id"] !== previousEntry) {
formCounter = 0;
}
// debug
// console.log(formCounter)
// console.log("element0")
// console.log(element[0])
// console.log(typeof(element[0]))
// console.log("parsings i")
// console.log(entries[i]["entry_id"])
// console.log(typeof(entries[i]["entry_id"]))
// element[0] contains id - so if we're looking at the same entry as current form belongs to
if (element[0] === entries[i]["entry_id"]) {
console.log(element)
const resultTableBody = document.getElementById("resultTableBody" + i);
// span declension across rows, remove redundant "declension" text
let rowspanElement = "";
if (formCounter === 0) {
rowspanElement = '<td rowspan="' + declensionLengths[element[1]] + '">' + element[1].split(" ")[0] + '</td>';
}
// mark unlikely genders
let unlikelyGender = "";
if (element[5] === true) {
unlikelyGender = "†"
}
const templateRow = '<tr>' + rowspanElement + '<td>' + element[2] + '</td><td>' + element[3] + unlikelyGender + '</td><td>' + element[4] + '</td><td>/-' + element[6] + '/</td></tr>';
console.log(templateRow);
resultTableBody.insertAdjacentHTML('beforeend', templateRow);
previousEntry = entries[i]["entry_id"];
formCounter += 1;
}
}
// // hide spinner after results fetched
// loadingSpinner.style.display = "none";
// loadingSpinner.classList.remove("d-flex");
}
if ("error" in parsings) {
notesBox.style.display = "none";
}
else {
notesBox.style.display = "";
possibleFormsTitle.textContent = word + " could be from:";
}
}
)
.catch(error => {
console.log("hello");
console.log(error);
resultsArea.style.display = "none";
})
.finally(() => {
// console.log("helllllooooo")
// hide spinner after results fetched
loadingSpinner.style.display = "none";
loadingSpinner.classList.remove("d-flex");
})
}
);
}
catch {
// hide spinner if error found
loadingSpinner.style.display = "none";
loadingSpinner.classList.remove("d-flex");
}
}
// Set theme on load
updateTheme()
// Update theme when the preferred scheme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme)
fetch("https://tech189.dev/tiripode/api.php?mode=size")
.then(response => response.json())
.then(data => {
document.getElementById("readyText").innerText = "Ready to look up from a database of " + data["inflection_count"] + " inflections";
});
// stop pressing enter refreshing the page, bind instead to parse button
document.addEventListener("DOMContentLoaded", e => {
const form = document.getElementById("wordForm");
form.addEventListener("submit", (event) => {
event.preventDefault();
parse();
});
});
</script>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" class="d-none">
</svg>
<div class="container">
<header class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom">
<a href="./"
class="d-flex align-items-center mb-3 mb-md-0 me-md-auto link-body-emphasis text-decoration-none">
<svg class="bi me-2" width="40" height="32">
<use xlink:href="#bootstrap"></use>
</svg>
<span class="fs-4">Tiripode</span>
</a>
<ul class="nav nav-pills">
<li class="nav-item"><a href="./" class="nav-link active" aria-current="page">Parse</a></li>
<li class="nav-item"><a href="./dictionary.html" class="nav-link">Dictionary</a></li>
<li class="nav-item"><a href="./tables.html" class="nav-link">Tables</a></li>
<li class="nav-item"><a href="./gloss.html" class="nav-link">Gloss</a></li>
<li class="nav-item"><a href="./about.html" class="nav-link">About</a></li>
</ul>
</header>
</div>
<br />
<div class="justify-content-center mb-5 row">
<div class="col-lg-9">
<h4><span style="font-family: serif;">Χαῖρε!</span> 👋 Please enter a Mycenaean Greek word* to get it parsed:</h4>
<p>*only some 1st/2nd declension nouns and adjectives parsable. <a href="./about.html">More info...</a></p>
</div>
</div>
<div class="justify-content-center mb-5 row">
<div class="col-lg-6">
<form class="" id="wordForm">
<div class="input-group"><input placeholder="e.g. wo-ko or 𐀺𐀒" spellcheck="false" type="text"
class="form-control form-control-lg" value="" id="wordBox"><button
class="btn btn-primary d-inline-flex align-items-center" type="button" onclick="parse()">
Parse!
</button>
</div>
<br />
<!-- <div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="flexCheckChecked" checked>
<label class="form-check-label" for="flexCheckChecked">
Unconfirmed parsing
</label>
</div> -->
</form>
<br />
<p id="readyText">Checking API...</p>
</div>
</div>
<div id="resultsArea" class="justify-content-center mb-5 row" style="display: none;">
<div class="col-lg-9">
<h4 id="possibleFormsTitle">Possible forms:</h4>
<br />
<div id="loadingSpinner" class="d-flex justify-content-center">
<div class="spinner-border m-5" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
<div id="errorBox" class="alert alert-danger" role="alert" style="display: none;">
Error!
</div>
<div id="possibleForms">
</div>
<div id="notesBox">
<h6>Notes</h6>
<p>† These forms are possible but most words in this declension are not this gender.</p>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-Rx+T1VzGupg4BHQYs2gCW9It+akI2MM/mndMCy36UVfodzcJcF0GGLxZIzObiEfa"
crossorigin="anonymous"></script>
</body>
</html>