-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (65 loc) · 2.18 KB
/
index.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
const searcher = document.querySelector("#searcher")
const results = document.querySelector("#results")
const toggle = document.querySelector("#lang-toggle")
const dropbtn = document.querySelector(".dropbtn")
const dropdown = document.querySelector("#myDropdown")
let language = "eng"
toggle.addEventListener('change',()=>{
searcher.value = ""
if(toggle.checked){
searcher.placeholder="Enter a Navajo word..."
language="nav"
} else {
searcher.placeholder="Enter an English word..."
language="eng"
}
})
searcher.addEventListener("keyup",(e)=>{
if(!searcher.value){
results.innerHTML="";
return
}
if(e.key === "Enter"){
let bodyObj = {
Word:searcher.value
}
axios.post(`/save`,bodyObj).then(({data:saves})=>{
dropdown.innerHTML = ""
for(let save of saves){
let link = document.createElement("a")
link.textContent = save
dropdown.appendChild(link)
}
})
} else {
let curr = searcher.value
axios.get(`/${language}/${curr}`).then(res=>{
if(curr===searcher.value){
results.innerHTML="";
for(obj of res.data){
let entry = document.createElement("li")
let definition = document.createElement("span")
let word = document.createElement("strong")
word.textContent = obj.Word;
definition.textContent=`: ${obj.Def}`
entry.appendChild(word)
entry.appendChild(definition)
results.appendChild(entry)
}
}
}).catch(err=>{console.log(err)})
}
})
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
dropbtn.addEventListener("click",()=>{
dropdown.classList.toggle("show")
})
// Close the dropdown if the user clicks outside of it
window.addEventListener("click",(event)=>{
if (!event.target.matches('.dropbtn')) {
if(dropdown.classList.contains('show')){
dropdown.classList.remove('show')
}
}
})