-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
234 lines (200 loc) · 7.24 KB
/
script.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
// Countries API.
const countries_el = document.getElementById('countries');
const darkmode_btn = document.getElementById('dark-mode');
const search_el = document.getElementById('search');
const filter_btn = document.getElementById('filter');
const filter_region = filter_btn.querySelectorAll('li');
const modal = document.getElementById('modal');
const close_btn = document.getElementById('close');
get_countries();
// sort an array by name.common key.
function sort_by_key(array)
{
return array.sort(function(a, b) {
//console.log(a.name.common);
var x = a.name.common;
var y = b.name.common;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
async function get_countries()
{
const res = await fetch('https://restcountries.com/v3.1/all');
const countries = await res.json();
//console.log(countries); // unsorted, as they come from the API.
//countries_sorted = sort_by_key(countries);
sort_by_key(countries);
//console.log(countries_sorted);
display_countries(countries);
}
function display_countries(countries)
{
// clear the element before displaying the countries.
countries_el.innerHTML = '';
countries.forEach(country => {
const country_el = document.createElement('div');
country_el.classList.add('card');
country_el.innerHTML = `
<div class="card-header">
<img src="${country.flags.svg}" alt="Peru">
</div>
<div class="card-body">
<h2 class="country-name">
${country.name.common}
</h2>
<p class="country-codes" style="display: none">
<strong>Codes: </strong>${country.cca2}, ${country.cca3}
</p>
<p class="country-capital">
<strong>Capital: </strong>${country.capital}
</p>
<p class="country-region">
<strong>Region: </strong>${country.region}
</p>
<p class="country-population">
<strong>Population: </strong>${country.population.toLocaleString()}
</p>
</div>
`;
//console.log(typeof country);
country_el.addEventListener('click', () => {
modal.style.display = 'flex';
show_country_details(country);
});
countries_el.appendChild(country_el);
});
}
function show_country_details(country)
{
//console.log(typeof country);
const modal_body = modal.querySelector('.modal-body');
const modal_img = modal.querySelector('img');
modal_img.src = country.flags.svg;
{
// extract all JSON keys from under country.currencies
currencies = Object.keys(country.currencies);
//console.log("currencies: " + currencies);
var currencies_list = '';
currencies.forEach(key => {
//console.log("key: " + key); // e.g.: AFN.
// country -> currencies -> AFN -> name (which has the value of "Afagan afghani")
//console.log("name: " + country.currencies[key].name);
currencies_list = currencies_list + country.currencies[key].name + ', ';
});
currencies_list = currencies_list.slice(0, -2); // remove the last comma.
//console.log("currencies_list: " + currencies_list);
}
{
languages = Object.keys(country.languages);
//console.log("languages: " + languages);
var languages_list = '';
languages.forEach(key => {
//console.log("key: " + key);
//console.log("name: " + country.languages[key]);
languages_list = languages_list + country.languages[key] + ', ';
});
languages_list = languages_list.slice(0, -2); // remove the last comma.
//console.log("languages_list: " + languages_list);
}
modal_body.innerHTML = `
<h2 class=country-name">${country.name.common}</h2>
<p>
<strong>CCA2:</strong>
${country.cca2}
<p>
<strong>Region:</strong>
${country.region}
</p>
<p>
<strong>Area:</strong>
${country.area}
</p>
<p>
<strong>Capital:</strong>
${country.capital}
</p>
<p>
<strong>Population:</strong>
${country.population.toLocaleString()}
</p>
<p>
<strong>latlng:</strong>
${country.latlng}
</p>
<p>
<strong>Time Zones:</strong>
${country.timezones}
</p>
<p>
<strong>Currencies:</strong>
${currencies_list}
</p>
<p>
<strong>Languages:</strong>
${languages_list}
</p>
<p>
<strong>Borders:</strong>
${country.borders}
</p>
`;
}
// enable dark mode by default.
document.body.classList.toggle('dark');
// toggle the dark class.
darkmode_btn.addEventListener('click', () => {
document.body.classList.toggle('dark');
});
// toggle the filter class.
filter_btn.addEventListener('click', () => {
filter_btn.classList.toggle('open');
});
// close the modal.
close_btn.addEventListener('click', () => {
modal.style.display = 'none';
});
// search by country name.
// apply a style based on the search value from the search input.
search_el.addEventListener('input', (e) => {
const search_term = e.target.value;
//console.log(search_term);
// the search applies to multiple classes.
const query_list = document.querySelectorAll('.country-name');
var results_count = 0;
// use the HTML that we already have in the DOM.
// and only apply a style on it, hiding or showing it.
query_list.forEach((i) => {
//console.log("innerText: " + i.innerText);
if (i.innerText.toLowerCase().includes(search_term.toLowerCase())) {
// .card -> .card-body -> .country-name.
i.parentElement.parentElement.style.display = 'block';
results_count++;
} else {
// do not show it.
i.parentElement.parentElement.style.display = 'none';
}
});
if (results_count == 0) {
console.log("No results!");
//alert("No results!");
}
});
// add a filter on the li inside the .dropdown.
filter_region.forEach(filter => {
filter.addEventListener('click', (e) => {
filter_value = filter.innerHTML;
//console.log(filter_value);
const query_list = document.querySelectorAll('.country-region');
// use the HTML that we already have in the DOM.
// and only apply a style on it, hiding or showing it.
query_list.forEach((i) => {
console.log("innerText: " + i.innerText);
if (i.innerText.includes(filter_value) || filter_value === 'All') {
i.parentElement.parentElement.style.display = 'block';
} else {
// do not show it.
i.parentElement.parentElement.style.display = 'none';
}
});
});
});