-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
218 lines (166 loc) · 7.22 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
fetchCountries = async () => {
const response = await fetch('https://restcountries.com/v2/all');
const myJson = await response.json(); //extract JSON from the http response
console.log(myJson);
localStorage['jsonData'] = JSON.stringify(myJson);
debugger
for (var i = 0; i < myJson.length; i++) {
const div = document.createElement('div');
div.classList.add('country-item');
const img = document.createElement('img');
img.classList.add('flag');
img.setAttribute('src', myJson[i].flag);
img.setAttribute('alt', "flag-img");
const h3 = document.createElement('h3');
h3.textContent = myJson[i].name;
var li_list = ["Population", "Region", "Capital"];
var li_values = [myJson[i].population, myJson[i].region, myJson[i].capital];
const ul = document.createElement('ul');
for (var j = 0; j < li_list.length; j++) {
const li = document.createElement('li');
li.textContent = li_list[j] + ": " + li_values[j];
ul.appendChild(li);
}
div.appendChild(img);
div.appendChild(h3);
div.appendChild(ul);
div.addEventListener('click', function (e) {
displayCountryDetails(e, myJson);
})
var c_box = document.querySelector('#countries-box');
c_box.appendChild(div);
// if (i === 10) {
// return;
// }
}
console.log(c_box);
}
fetchCountries();
debugger
const search = document.forms['input-form'].querySelector('input');
search.addEventListener('keyup', function (e) {
const searchTerm = e.target.value.toLowerCase();
const countries = document.getElementsByTagName('h3');
document.getElementById('regions').selectedIndex = 0;
Array.from(countries).forEach(function (country) {
if (country.innerText.toLowerCase().indexOf(searchTerm) != -1) {
country.parentElement.style.display = "block";
} else {
country.parentElement.style.display = "none";
}
})
})
function selectRegions() {
const list = document.getElementById('regions');
debugger
document.getElementById('search').value = "";
const selected_region = list.options[list.selectedIndex].text.toLowerCase();
const regions = document.getElementsByTagName('li');
for (var i = 1; i < regions.length; i += 3) {
if (regions[i].innerText.toLowerCase().indexOf(selected_region) != -1) {
regions[i].parentElement.parentElement.style.display = "block";
} else {
regions[i].parentElement.parentElement.style.display = "none";
}
}
console.log(list);
}
function toggleTheme() {
// alert('clicked');
debugger
var currentTheme = document.documentElement.getAttribute('data-theme');
var text = document.getElementById('dark-mode-text');
if (currentTheme === 'light') {
targetTheme = 'dark';
text.innerText = "Light Mode";
document.getElementsByTagName('ion-icon')[0].setAttribute('name', 'sunny-outline');
} else if (currentTheme === 'dark') {
targetTheme = 'light';
text.innerText = "Dark Mode";
document.getElementsByTagName('ion-icon')[0].setAttribute('name', 'moon-outline');
}
document.documentElement.setAttribute('data-theme', targetTheme);
}
function displayCountryDetails(e, myJson) {
// console.log(myJson);
var main = document.getElementsByClassName('main');
var section = document.getElementsByClassName('details-section');
debugger
if (e.target.tagName === "IMG" || e.target.tagName === "H3" || e.target.tagName === "UL") {
var parent = e.target.parentElement;
var children = parent.children;
// console.log("JSON"+ myJson);
main[0].style.display = "none";
section[0].style.display = "block";
setData(myJson, children, section, false);
} else if (e.target.tagName === "LI") {
var parent = e.target.parentElement.parentElement;
var children = parent.children;
main[0].style.display = "none";
section[0].style.display = "block";
setData(myJson, children, section, false);
}
}
function setData(myJson, children, section, recursion) {
debugger
var textgrid = document.getElementsByClassName('text-grid');
textgrid[0].style.display = "grid";
for (var i = 0; i < myJson.length; i++) {
if (myJson[i].name.toLowerCase() === (recursion ? children.toLowerCase() : children[1].innerText.toLowerCase())) {
section[0].querySelectorAll('h3')[0].innerText = myJson[i].name;
section[0].querySelectorAll('img')[0].src = myJson[i].flag;
section[0].querySelectorAll('img')[0].alt = "flag-image";
section[0].querySelectorAll('.native-name')[0].innerText = myJson[i].nativeName;
section[0].querySelectorAll('.region')[0].innerText = myJson[i].region;
if (myJson[i] && myJson[i].capital) {
section[0].querySelectorAll('.capital')[0].innerText = myJson[i].capital;
} else {
section[0].querySelectorAll('.capital')[0].innerText = "";
}
if (myJson[i] && myJson[i].currencies) {
section[0].querySelectorAll('.currencies')[0].innerText = myJson[i].currencies[0].name;
} else {
section[0].querySelectorAll('.currencies')[0].innerText = "";
}
section[0].querySelectorAll('.population')[0].innerText = myJson[i].population;
section[0].querySelectorAll('.sub-region')[0].innerText = myJson[i].subregion;
section[0].querySelectorAll('.tld')[0].innerText = myJson[i].topLevelDomain[0];
section[0].querySelectorAll('.languages')[0].innerText = myJson[i].languages[0].name;
var nearbyCountryCodes = myJson[i].borders;
var nearbyCountries = [];
debugger
if (myJson[i] && myJson[i].borders) {
for (var j = 0; j < myJson.length; j++) {
for (k = 0; k < nearbyCountryCodes.length; k++) {
if (myJson[j].alpha3Code === nearbyCountryCodes[k]) {
nearbyCountries.push(myJson[j].name);
}
}
}
} else {
nearbyCountries.push("I do not have any borders!");
}
const ul = document.getElementById('neighboring-countries')
ul.innerHTML = '';
// if(nearbyCountryCodes.length === 0) {
// return;
// }
for (var j = 0; j < nearbyCountries.length; j++) {
var li = document.createElement('li');
li.textContent = nearbyCountries[j];
li.addEventListener('click', function (e) {
// debugger
setData(myJson, e.target.innerText, section, true);
})
ul.appendChild(li);
}
console.log(nearbyCountries);
}
}
}
function back() {
const main = document.getElementsByClassName('main');
const section = document.getElementsByClassName('details-section');
section[0].style.display = "none";
main[0].style.display = "block";
}