Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
227 lines (183 loc) · 8.27 KB
/
Copy pathapp.js
File metadata and controls
227 lines (183 loc) · 8.27 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
document.body.style.background = "lightgrey"
let counter = 0;
let hoverTimer;
const overlay = document.createElement("div")
overlay.style.position = "fixed"
overlay.style.top = "0"
overlay.style.left = "0"
overlay.style.width = "100vw"
overlay.style.height = "100vh"
overlay.style.backgroundColor = "rgba(0,0,0,0.5)" // elsötétítés
overlay.style.display = "none"
overlay.style.justifyContent = "center"
overlay.style.alignItems = "center"
overlay.style.zIndex = "9999"
const asd = document.createElement("div")
asd.style.backgroundColor = "lightgray"
asd.style.padding="23px"
asd.style.width="8fr"
overlay.appendChild(asd)
document.body.appendChild(overlay)
const contentContainer = document.createElement("div")
contentContainer.style.marginLeft = "auto"
contentContainer.style.marginRight = "auto"
contentContainer.style.width = "30vw"
const text = document.createElement("p")
text.textContent = "Use the input field to search"
const loadingP=document.createElement("p")
loadingP.textContent = "Loading..."
loadingP.style.display = "none"
const inputFld=document.createElement("input")
inputFld.setAttribute("type", "text")
inputFld.setAttribute("id", "inputFld")
const searchBtn=document.createElement("button")
searchBtn.setAttribute("id","btn")
searchBtn.textContent ="Search"
searchBtn.addEventListener("click", ()=>{
let inputFieldContent = document.getElementById('inputFld').value
document.getElementById("loadingP").style.display = "block"
countrySearch(inputFieldContent)
})
document.body.appendChild(contentContainer)
contentContainer.appendChild(text)
contentContainer.appendChild(loadingP)
contentContainer.appendChild(inputFld)
contentContainer.appendChild(searchBtn)
document.querySelector('#inputFld').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
let inputFieldContent = document.getElementById('inputFld').value
loadingP.style.display = "block"
countrySearch(inputFieldContent)
}
});
async function countrySearch(countryName) {
try {
const resolve = await fetch(`https://restcountries.com/v3.1/name/${countryName}?fields=name,capital,population,region,flags,area,timezones,continents`)
const data = await resolve.json()
console.log(data)
setTimeout(() => {
loadingP.style.display = "none"
const line = document.createElement("hr")
const rmBtn = document.createElement("button")
const cardContainer = document.createElement("div")
rmBtn.addEventListener("click", () => {
cardContainer.remove()
line.remove()
rmBtn.remove()
})
cardContainer.setAttribute("id", "cardContainer")
document.body.appendChild(cardContainer)
document.body.appendChild(rmBtn)
document.body.appendChild(line)
cardContainer.style.display="flex"
cardContainer.style.flexWrap="wrap"
cardContainer.style.marginBottom="15px"
line.style.width = "80%"
line.style.height= "2px";
line.style.background = "darkgrey"
rmBtn.style.width = "auto"
rmBtn.style.height = "auto"
rmBtn.textContent = "Delete group"
data.forEach(adat => {
const card = document.createElement("div")
counter++
card.setAttribute("class", "card")
card.addEventListener("click", () => {
card.remove()
clearTimeout(hoverTimer)
counter--
if(counter===0){
cardContainer.remove()
line.remove()
rmBtn.remove()
}
})
cardContainer.appendChild(card)
card.style.margin="10px"
card.style.padding="23px"
card.style.background="grey"
card.style.width="8fr"
card.style.color="white"
card.style.border="3px solid lightgray"
card.style.textAlign="justify"
const name = document.createElement("p")
name.style.fontSize = "20pt"
name.textContent = adat.name.common
card.appendChild(name)
const flagSrc = document.createElement("img")
flagSrc.style.width = "auto"
flagSrc.style.height = "50px"
flagSrc.src = adat.flags.png
card.appendChild(flagSrc)
const capitalName = document.createElement("p")
capitalName.textContent = "Capital: "+ adat.capital
card.appendChild(capitalName)
const popName = document.createElement("p")
popName.textContent = "Pop: "+ adat.population
card.appendChild(popName)
const regionName = document.createElement("p")
regionName.textContent = "Region: "+ adat.region
card.appendChild(regionName)
card.addEventListener("mouseenter", (event) => {
const el = event.target
el.style.boxShadow = "5px 5px 5px 5px grey"
el.style.padding = "26px"
hoverTimer = setTimeout(() => {
console.log("5 másodperc hover után!")
overlay.style.display = "flex"
const name = document.createElement("p")
name.style.fontSize = "20pt"
name.textContent = adat.name.common
asd.appendChild(name)
const flagSrc = document.createElement("img")
flagSrc.style.width = "auto"
flagSrc.style.height = "50px"
flagSrc.src = adat.flags.png
asd.appendChild(flagSrc)
const capitalName = document.createElement("p")
capitalName.textContent = "Capital: "+ adat.capital
asd.appendChild(capitalName)
const popName = document.createElement("p")
popName.textContent = "Pop: "+ adat.population
asd.appendChild(popName)
const regionName = document.createElement("p")
regionName.textContent = "Region: "+ adat.region
asd.appendChild(regionName)
const areaName = document.createElement("p")
areaName.textContent = "Area: "+ adat.area
asd.appendChild(areaName)
const timezonesName = document.createElement("p")
timezonesName.textContent = "Timezones: "+ adat.timezones
asd.appendChild(timezonesName)
const continentsName = document.createElement("p")
continentsName.textContent = "Continents: "+ adat.continents
asd.appendChild(continentsName)
overlay.addEventListener("click", (e) => {
if (e.target === overlay) {
overlay.style.display = "none"
asd.removeChild(name)
asd.removeChild(flagSrc)
asd.removeChild(capitalName)
asd.removeChild(popName)
asd.removeChild(regionName)
asd.removeChild(areaName)
asd.removeChild(timezonesName)
asd.removeChild(continentsName)
}
})
}, 2000)
})
card.addEventListener("mouseleave", (event) => {
const el = event.target
el.style.boxShadow = "none"
el.style.padding = "23px"
clearTimeout(hoverTimer)
})
});
}, 3000);
} catch (error) {
console.log(error)
alert("A keresett ország nem található")
document.getElementById("inputFld").value = ""
}
}