-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
475 lines (426 loc) · 17 KB
/
app.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
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
import {renderData} from "/processData.js";
//ERROR | TIP MESSAGES
const messages = document.getElementById("messages");
const warning = document.getElementById("warning");
const loader = document.getElementById("loader");
const searchTip = document.getElementById("search-tip");
const connectionTip = document.getElementById("connection-tip");
//INTRO VIEW RESOURCES
const introView = document.getElementById("intro-view");
const introMessage = document.getElementById("intro-message");
const logo = document.getElementById("logo");
const aboutWordo = document.getElementById("about-wordo");
const introExploreBtn = document.getElementById("intro-explore-btn");
const introBody = document.getElementById("intro-body");
const introViewExploreBtn = document.getElementById("intro-view-explore-btn"); //explore btn in same page as images
//MAIN VIEW RESOURCES
const mainView = document.getElementById("main-view");
const mainBody = document.getElementById("main-body");
const menu = document.getElementById("menu");
const menuIcon = document.getElementById("menu-icon");
const closeMenuIcon = document.getElementById("close-menu");
const recentSearchHeader = document.getElementById("recent-search-header");
const recentSearch = document.getElementById("recent-search");
const userInput = document.getElementById("user-input");
const searchBtn = document.getElementById("search-btn");
//BOOKMARK VIEW
const bookmarkView = document.getElementById("bookmark-view");
const wordsSaved = document.getElementById("words-saved-link");
const bookmarkExitBtn = document.getElementById("bookmark-exit-btn");
const savedWordsContainer = document.getElementById("saved-words-container");
let savedWordsMessage = document.getElementById("saved-words-message");
//WORD OF THE DAY
const wordOfTheDay = document.getElementById("word-of-the-day");
wordOfTheDay.addEventListener("click", () => { alert("This feature will be added soon") });
//---------------------------------------- FUNCTIONS ------------------------------------------
//show element
function show(element, value = "block") {
element.style.display = value;
}
//hide element
function hide(element) {
element.style.display = "none";
}
//-------------------- LOG MESSAGES ---------------------
function logMessage(element) {
hide(mainBody);
hide(searchTip);
hide(connectionTip);
hide(loader);
show(messages, "flex");
show(element, "flex");
}
function logWarning(message) {
warning.innerText = message;
warning.classList.toggle("slide-warning-up");
show(warning);
setTimeout(() => {
warning.classList.toggle("slide-warning-up");
warning.classList.toggle("slide-warning-down");
}, 3500);
}
//log message to user when no word has been added
function logNoSavedWords(){
if(savedWordsContainer.childElementCount < 1) {
savedWordsContainer.innerHTML = `
<div id="saved-words-message" class="saved-words-message">
<span class="saved-words-message__exclamation-icon"><i class="fa fa-exclamation-circle"></i></span>
<p class="saved-words-message__message-text">you haven't saved any word yet, <br> go ahead save some words you'd <br> like to remember</p>
</div>
`;
savedWordsMessage = document.getElementById("saved-words-message");
}
}
//----------------------- CREATE ELEMENTS ------------------
//create word container
export function createWordContainer(word, partOfSpeech, transcription, definition){
if(partOfSpeech === null) partOfSpeech = "unknown";
mainBody.innerHTML += `
<section id="${word}-${partOfSpeech}" class="word-container">
<div class="word-utils">
<p class="word-utils__part-of-speech">${partOfSpeech.toLowerCase()}</p>
<p class="word-utils__save">save</p>
</div>
<div class="word">
<h3 class="word__text">${word.toUpperCase()}</h3>
<p class="word__transcription">/ ${transcription} /</p>
<p class="word__definition">${definition}</p>
</div>
</section>
`;
//to make id unique for each word container, set id as word-partofspeech
let wordContainer = document.getElementById(`${word}-${partOfSpeech}`);
//capitalize the part of speech
wordContainer.firstElementChild.firstElementChild.style.textTransform = "capitalize";
wordContainer.style.backgroundColor = `var(--${partOfSpeech.toLowerCase()})`;
}
//create definitions
export function createDefinitions(word, partOfSpeech, definitions) {
mainBody.innerHTML += `
<section id="${word}-${partOfSpeech}-definitions" class="definitions-container">
<h4 class="definitions-container__header">Definitions</h4>
</section>
`;
let definitionsContainer = document.getElementById(`${word}-${partOfSpeech}-definitions`);
//note definitions is an array of sentences (stings)
for(let definition of definitions){
definitionsContainer.innerHTML += `
<p class="definitions-container__text">${definition}</p>
`;
}
}
//create examples
export function createExamples(word, partOfSpeech, examples){
mainBody.innerHTML += `
<section class="examples-overall-container">
<h4 class="examples-overall-container__header">Examples</h4>
<div id="${word}-${partOfSpeech}-examples" class="example-container">
</div>
</section>
`;
let examplesContainer = document.getElementById(`${word}-${partOfSpeech}-examples`);
for(let example of examples) {
examplesContainer.innerHTML += `
<section class="example">
<p class="example__text"> ${example} </p>
</section>
`;
}
}
//create syllables
export function createSyllables(word, partOfSpeech, syllable, count) {
if(partOfSpeech === null) partOfSpeech = "unknown";
mainBody.innerHTML += `
<section class="syllables-container">
<h4 class="syllables-container__header">Syllables</h4>
<div id="${word}-${partOfSpeech}-syllables" class="syllables">
<p class="syllables__word">${syllable}</p>
<p id="syllables-count" class="syllables__count">${count}</p>
</div>
</section>
`;
let syllablesBg = document.getElementById(`${word}-${partOfSpeech}-syllables`);
syllablesBg.style.backgroundColor = `var(--${partOfSpeech})`;
//change syllable count color to match syllable background color
let syllablesCount = document.getElementById("syllables-count");
syllablesCount.style.color = `var(--${partOfSpeech})`;
}
//create synonyms & antonyms
export function createSynonymAntonym(synonyms = "none", antonyms = "none"){
//antonyms and synonyms to from array to string
if(synonyms) synonyms = synonyms.join(", ");
if(synonyms) antonyms = antonyms.join(", ");
mainBody.innerHTML += `
<section class="synonym-antonym">
<h4 class="synonym-antonym__header">Synonyms</h4>
<div class="synonyms">
<p class="synonyms__text">${synonyms}</p>
</div>
<hr class="synonym-antonym__divider"/>
<h4 class="synonym-antonym__header">Antonyms</h4>
<div class="synonyms">
<p class="synonyms__text">${antonyms}</p>
</div>
</section>
`;
}
//create recent searches
function createRecentSearch(word) {
let searchContainer = document.createElement("section");
searchContainer.setAttribute("class", "recent-search");
searchContainer.innerHTML = `
<p class="recent-search__word">${word}</p>
<span class="recent-search__remove-icon"><i class="fa fa-times"></i></span>
`;
recentSearch.insertBefore(searchContainer, recentSearch.children[0]);
//recently searched word is clicked
let recentlySearchedWords = document.getElementsByClassName("recent-search__word");
for(let word of recentlySearchedWords) {
word.addEventListener("click", () => {
userInput.value = word.innerText;
userInput.focus();
})
}
//recently searched word is deleted
let removeRecentSearchWord = document.getElementsByClassName("recent-search__remove-icon");
for(let btn of removeRecentSearchWord) {
btn.addEventListener("click", () => {
recentSearch.removeChild(btn.parentElement);
if(recentSearch.childElementCount < 1) { //if there are no recent searches
hide(recentSearchHeader);
hide(recentSearch);
}
userInput.focus();
})
}
}
//------------------------- API REQUEST --------------------------
function getWordData(word){
fetch(`https://wordsapiv1.p.rapidapi.com/words/${word.toLowerCase()}`, {
"method": "GET",
"headers": {
"x-rapidapi-host": "wordsapiv1.p.rapidapi.com",
"x-rapidapi-key": process.env.API_KEY,
}
})
.then(response => response.json())
.then(data => {
if(data.success !== false && data.results){
createRecentSearch(word);
//prevent number of recently searched words from exceeding 10
if(recentSearch.childElementCount > 10) recentSearch.removeChild(recentSearch.lastElementChild);
hide(loader);
hide(messages);
mainBody.innerHTML = "";
userInput.value = "";
show(mainBody);
renderData(data);
}
else {
hide(messages);
hide(loader);
show(mainBody);
setTimeout(() => {
logWarning("word not found!");
if(mainBody.childElementCount < 1) logMessage(searchTip);
}, 300);
}
})
.catch(error => {
if(error.message === "Failed to fetch"){
logMessage(connectionTip);
}
else {
alert("error encountered searching that word! please search another");
hide(mainBody);
logMessage(searchTip);
}
});
}
//---------------------------------------- VIEWS ---------------------------------
//----------- INTRO VIEW ---------------
window.addEventListener("load", () => {
//explore button on first page is clicked
introExploreBtn.addEventListener("click", () =>{
introExploreBtn.style.display = "none";
logo.classList.add("fade-out-logo");
aboutWordo.classList.add("slide-out-text");
setTimeout(() => {
hide(introMessage);
show(introBody);
}, 1300);
});
})
//intro view is clicked and links to main view
introViewExploreBtn.addEventListener("click", () => {
hide(introView);
show(mainView);
logMessage(searchTip);
mainView.classList.add("fade-element-in");
});
//---------- MAIN VIEW ------------------
//menu icon is clicked
menuIcon.addEventListener("click", () => {
menu.classList.toggle("slide-menu-down");
show(menu, "flex");
});
//menu icon is clicked
closeMenuIcon.addEventListener("click", () => {
menu.classList.toggle("slide-menu-down");
menu.classList.toggle("slide-menu-up");
setTimeout(() => {
hide(menu);
menu.classList.toggle("slide-menu-up");
}, 100);
});
//hide recent search when user clicks on screen
mainBody.addEventListener("click", () => {
hide(recentSearchHeader);
hide(recentSearch);
})
//user inputs data
userInput.addEventListener("input", () => {
//prevent input from starting with space
if(userInput.value.startsWith(" ")) userInput.value = userInput.value.replace(" ", "");
if(userInput.value === "") {
hide(recentSearchHeader);
hide(recentSearch);
}
else {
//if there are recently searched words, display them
if(recentSearch.childElementCount > 0) {
recentSearch.classList.add("slide-recentSearch-up");
recentSearchHeader.classList.add("slide-recentSearch-up");
show(recentSearchHeader);
show(recentSearch, "flex");
}
}
})
//search button is clicked
searchBtn.addEventListener("click", () => {
if(userInput.value){
logMessage(loader);
getWordData(userInput.value);
hide(recentSearchHeader);
hide(recentSearch);
}
})
//enter key is pressed
document.addEventListener("keydown", (e) => {
if(e.key === "Enter"){
searchBtn.click();
userInput.blur(); //remove focus from the input
}
})
//change save button background color to white, to show it has been saved
function indicateSave(element, state = false) {
if(element.innerText === "save") {
//get background color of the main word container
let bgColor = element.parentElement.parentElement.style.backgroundColor;
element.innerText = "saved";
element.style.backgroundColor = "white";
element.style.color = bgColor;
}
else {
element.innerText = "save";
element.style.background = "none";
element.style.color = "white";
}
}
//save button is clicked
export function enableSave() {
let save = document.getElementsByClassName("word-utils__save");
for(let i = 0; i < save.length; i++) {
//save word from the API data in word container
let word = save[i].parentElement.nextElementSibling.firstElementChild.innerText;
save[i].addEventListener("click", () => {
//when a save btn is clicked, indicate save for all parts of speech of the word
for(let j = 0; j < save.length; j++) indicateSave(save[j]);
//if there is a word saved in savedWordsContainer, remove the message displayed when there are no saved words
if(savedWordsContainer.lastElementChild.id === "saved-words-message") {
savedWordsContainer.removeChild(savedWordsMessage);
}
renderSavedWords(word);
});
}
}
//---------- BOOKMARK VIEW ------------------
//words saved header is clicked and leads to words saved page
wordsSaved.addEventListener("click", () => {
mainView.classList.add("slide-element-out");
setTimeout(() => {
hide(mainView);
hide(menu);
menu.classList.remove("slide-menu-down");//so that menu doesn't loose its slide down effect
mainView.classList.remove("slide-element-out");
bookmarkView.classList.add("fade-in");
show(bookmarkView);
}, 250)
})
//bookmark view is exited
bookmarkExitBtn.addEventListener("click", () => {
bookmarkView.classList.add("fade-out");
bookmarkView.classList.remove("fade-in");
setTimeout(() => {
hide(bookmarkView);
show(mainView);
bookmarkView.classList.remove("fade-out");
}, 250)
})
//render saved words
let wordExists = []; //this array variable (woreExists) is used for making sure a word is saved before we make saved words unique
function renderSavedWords(word){
//add to saved words and make sure a word doesn't get saved multiple times
if(!wordExists.includes(word)){
savedWordsContainer.innerHTML += `
<div class="saved">
<p class="saved__word">${word.toLowerCase()}</p>
<span id="saved-remove-btn" class="saved__remove-icon"><i class="fa fa-times"></i></span>
</div>
`;
wordExists.push(word);
//save to local storage
storeUserSession(wordExists);
}
//saved word is deleted
const deleteSavedWordBtns = document.getElementsByClassName("saved__remove-icon");
for(let deleteBtn of deleteSavedWordBtns) {
deleteBtn.addEventListener("click", () => {
deleteBtn.parentElement.classList.add("fade-out");
setTimeout(() => {
savedWordsContainer.removeChild(deleteBtn.parentElement);
let text = deleteBtn.previousElementSibling.innerText.toUpperCase(); //we converted to uppercase cos all words in wordExists are in uppercase
wordExists.splice(wordExists.indexOf(text), 1);
//save to local storage
storeUserSession(wordExists);
logNoSavedWords();
}, 500);
})
}
//display saved word info when the saved word is clicked
let savedWord = document.getElementsByClassName("saved__word");
for(let each of savedWord){
each.addEventListener("click", () => {
hide(bookmarkView);
show(mainView);
logMessage(loader);
getWordData(each.innerText);
})
}
}
//save to local storage
function storeUserSession(data){
localStorage.setItem("savedWords", data);
}
//get data from local storage
if(localStorage.savedWords){
//render all saved words retrieved from the local storage
let listOfWords = localStorage.savedWords.split(",");
for(let list of listOfWords) {
renderSavedWords(list);
}
//if there are saved words, hide the bookmark message: "you haven't saved any word yet..."
if(savedWordsContainer.firstElementChild.id === "saved-words-message") {
savedWordsContainer.removeChild(savedWordsContainer.firstElementChild);
}
}