Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
382 lines (291 loc) · 12.2 KB
/
Copy pathscript.js
File metadata and controls
382 lines (291 loc) · 12.2 KB
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
const previousButton = document.getElementById("previous-button");
const nextButton = document.getElementById("next-button");
const list = document.querySelector(".dropdown-list");
var dropdownLength = 0;
//accesses the local storage and retrieves all the words saved by the user and adds them to the dropdown
function loadSaved() {
var values = [];
var keys = Object.keys(localStorage);
var i = keys.length;
for(let j = 0; j<i; ++j ) {
values.push( localStorage.getItem(keys[j]) );
}
while(i--) {
//if localStorage.getItem(word) = "true", then add to the saved list
if(values[i] === "true") {
if(dropdownLength == 0)
list.innerHTML = "";
console.log(values[i] + " " + keys[i]);
const listItem = document.createElement("div");
listItem.classList.add("list-item");
listItem.classList.add("hover-enable");
listItem.innerHTML = keys[i];
listItem.id = keys[i];
list.appendChild(listItem);
updateEventListeners();
++dropdownLength;
}
}
//if no saved items, then add a 'You have not saved anything' option
if(dropdownLength == 0) {
const listItem = document.createElement("div");
listItem.classList.add("list-item");
listItem.innerHTML = "You have not saved anything yet";
list.appendChild(listItem);
}
}
//load all saved words
loadSaved();
//display dropdown list when user clicks on the dropdown menu
const description = document.getElementById("description-element");
const dropdown = document.getElementById("dropdown-menu");
dropdown.addEventListener("click", () => {
if(list.style.opacity == 1) {
list.style.visibility = "hidden";
list.style.opacity = 0;
}
else {
//console.log("yo");
document.querySelector('.buttons').style.visibility = 'hidden';
list.style.visibility = "visible";
list.style.opacity = 1;
content.classList.remove("active");
description.innerHTML = ""
}
})
const content = document.querySelector(".content");
const example = document.querySelector(".example")
let audio;
//defining these values for easy traversing through the various definitions of the word in the JSON
var resultIndex = 0;
var meaningsIndex = 0;
var definitionsIndex = 0;
var resultLength = 0;
var meaningsLength = 0;
var definitionsLength = 0;
//var page = 1;
var apiJSON;
var searchItem;
//fetches the meaning and example of the searched word
function displaySearchResults(result, word) {
apiJSON = result;
searchItem = word;
resultLength = result.length;
meaningsLength = result[resultIndex].meanings.length;
definitionsLength = result[resultIndex].meanings[meaningsIndex].definitions.length;
if(result.title) {
description.innerHTML = `Can't find the meaning of <span>"${word}"</span>. Please check for any possible spelling errors.`;
}
else {
content.classList.add("active");
//if multiple meanings exist, display buttons for navigation
if(resultLength>1 || meaningsLength>1 || definitionsLength>1) {
document.querySelector('.buttons').style.visibility = 'visible';
}
let definition = result[resultIndex].meanings[meaningsIndex].definitions[definitionsIndex];
phonetics = `/${result[resultIndex].phonetics[0].text}/`;
document.querySelector(".word p").innerText = result[resultIndex].word;
document.querySelector(".word span").innerText = phonetics;
document.querySelector(".definition span").innerText = definition.definition;
document.querySelector(".part-of-speech p").innerText = result[resultIndex].meanings[meaningsIndex].partOfSpeech
if(localStorage.getItem(result[resultIndex].word) === "true") {
saveButton.classList.remove("saved");
saveButton.classList.remove("bi-bookmark-check-fill");
saveButton.classList.remove("bi-bookmark-check");
saveButton.classList.add("bi-bookmark-check-fill");
}
else {
saveButton.classList.remove("saved");
saveButton.classList.remove("bi-bookmark-check-fill");
saveButton.classList.remove("bi-bookmark-check");
saveButton.classList.add("bi-bookmark-check");
}
//console.log(definition.example);
//console.log(definition.example != undefined);
if(definition.example != undefined) {
document.querySelector(".example").style.display = "block";
document.querySelector(".example").style.borderBottom = "1px solid #CC7722";
document.querySelector(".example span").innerText = definition.example;
}
else {
document.querySelector(".example").style.display = "none";
document.querySelector(".example").style.border = "0px";
}
audio = new Audio("https:" + result[resultIndex].phonetics[0].audio);
}
}
const searchbar = document.getElementById("searchbar-element");
const saveButton = document.getElementById("save-icon");
//displays the results on dropdown click
//adds onclick event listener to all items of the dropdown list
function updateEventListeners() {
const listElements = document.querySelectorAll(".list-item");
listElements.forEach((element) => {
element.addEventListener("click", () => {
const placeholder = document.querySelector(".placeholder");
placeholder.innerHTML = element.innerHTML;
element.id = element.innerHTML;
const dropdownList = document.querySelector(".dropdown-list");
dropdownList.style.visibility = "hidden";
displayResults(element.innerHTML);
})
})
}
//displays results of the word when clicked on saved item
function displayResults(word) {
//page = 1;
nextButton.classList.add("enabled");
previousButton.classList.remove("enabled");
resultIndex = 0;
meaningsIndex = 0;
definitionsIndex = 0;
resultLength = 0;
meaningsLength = 0;
definitionsLength = 0;
//console.log(word);
if(localStorage.getItem(word) === "true") {
saveButton.classList.add("saved");
saveButton.classList.add("bi-bookmark-check-fill");
saveButton.classList.remove("bi-bookmark-check");
}
searchItem = word;
fetchApi(word);
searchbar.value = word;
}
//fetches the Dictionary API
function fetchApi(word) {
if(!(/^[a-zA-Z]+$/.test(word))) {
description.style.color = "#9A9A9A";
description.innerHTML = "Please enter only a single word containing letters of the English Alphabet.";
return;
}
content.classList.remove("active");
description.style.color = "#9A9A9A";
//console.log('yo');
description.innerHTML = "Just a minute, hold tight ...";
let url = `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`;
fetch(url).then(response => response.json()).then(result => displaySearchResults(result, word)).catch(() =>{
description.innerHTML = `Could not load your request. This could have occured either due to a spelling mistake or a network error at the back end.`;
});
}
//on enter, searches the word entered by the user
searchbar.addEventListener("keyup", e => {
//page = 1;
nextButton.classList.add("enabled");
previousButton.classList.remove("enabled");
let word = e.target.value.replace(/\s+/g, '');
if(e.key == "Enter" && word) {
resultIndex = 0;
meaningsIndex = 0;
definitionsIndex = 0;
resultLength = 0;
meaningsLength = 0;
definitionsLength = 0;
document.querySelector('.buttons').style.visibility = 'hidden';
content.classList.remove("active");
if(localStorage.getItem(word) === "true") {
saveButton.classList.add("saved");
saveButton.classList.add("bi-bookmark-check-fill");
saveButton.classList.remove("bi-bookmark-check");
}
fetchApi(word);
}
});
//plays the audio of the pronunciation of the word
const volume = document.getElementById("audio-icon");
volume.addEventListener("click", () => {
volume.classList.remove("bi-volume-up");
volume.classList.add("bi-volume-up-fill");
audio.play();
setTimeout(() => {
volume.classList.add("bi-volume-up");
volume.classList.remove("bi-volume-up-fill");
}, audio.duration*1000);
});
//saves the given word to the local storage
saveButton.addEventListener("click", () => {
const searchItem = document.getElementById("search-item").innerText;
if(localStorage.getItem(searchItem) === "true") {
saveButton.classList.remove("saved");
saveButton.classList.remove("bi-bookmark-check-fill");
saveButton.classList.add("bi-bookmark-check");
localStorage.setItem(searchItem, "false");
var targetOption = document.getElementById(searchItem);
targetOption.remove();
const placeholder = document.querySelector(".placeholder");
placeholder.innerHTML = "Scroll your saved ..."
--dropdownLength;
if(dropdownLength == 0) {
const listItem = document.createElement("div");
listItem.classList.add("list-item");
listItem.innerHTML = "You have not saved anything yet";
list.appendChild(listItem);
}
}
else {
if(dropdownLength == 0) {
list.innerHTML = "";
}
saveButton.classList.add("saved");
saveButton.classList.add("bi-bookmark-check-fill");
saveButton.classList.remove("bi-bookmark-check");
localStorage.setItem(searchItem, "true");
const listItem = document.createElement("div");
listItem.classList.add("list-item");
listItem.classList.add("hover-enable");
listItem.innerHTML = searchItem;
listItem.id = searchItem;
list.appendChild(listItem);
++dropdownLength;
updateEventListeners();
}
})
//navigate through the different meanings of the single word
previousButton.addEventListener("click", () => {
if(previousButton.classList.contains("enabled")) {
//--page;
if(definitionsIndex > 0) {
--definitionsIndex;
}
else if(meaningsIndex > 0) {
definitionsIndex = apiJSON[resultIndex].meanings[--meaningsIndex].definitions.length - 1;
}
else {
meaningsIndex = apiJSON[--resultIndex].meanings.length - 1;
definitionsIndex = apiJSON[resultIndex].meanings[meaningsIndex].definitions.length - 1;
}
if(!nextButton.classList.contains("enabled")) {
nextButton.classList.add("enabled");
}
document.querySelector('.buttons').style.visibility = 'hidden';
displaySearchResults(apiJSON, searchItem);
if(resultIndex == 0 && meaningsIndex == 0 && definitionsIndex == 0) {
previousButton.classList.remove("enabled");
}
}
})
nextButton.addEventListener("click", () => {
if(nextButton.classList.contains("enabled")) {
//++page;
if(definitionsIndex < definitionsLength-1) {
++definitionsIndex;
}
else if(meaningsIndex < meaningsLength-1) {
definitionsIndex = 0;
++meaningsIndex;
}
else {
definitionsIndex = 0;
meaningsIndex = 0;
++resultIndex;
}
if(!previousButton.classList.contains("enabled")) {
previousButton.classList.add("enabled");
}
document.querySelector('.buttons').style.visibility = 'hidden';
displaySearchResults(apiJSON, searchItem);
if(resultIndex == resultLength-1 && meaningsIndex == meaningsLength-1 && definitionsIndex == definitionsLength-1) {
nextButton.classList.remove("enabled");
}
}
})