-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
335 lines (275 loc) · 11 KB
/
Copy pathscript.js
File metadata and controls
335 lines (275 loc) · 11 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
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
let allCountries = [];
let searchResults = null;
let selectedCountry = null;
let regionFilter = "ALL";
let popFilter = "none";
let sortKey = "population";
let showFavorites = false;
let favorites = new Set();
let searchTimer = null;
// Load any saved favorites from the browser
try {
const saved = localStorage.getItem("gv-favorites");
if (saved) favorites = new Set(JSON.parse(saved));
} catch (e) {}
function saveFavorites() {
localStorage.setItem("gv-favorites", JSON.stringify([...favorites]));
}
function formatNum(n) {
if (typeof n !== "number") return "0";
if (n >= 1e9) return (n / 1e9).toFixed(1) + "B";
if (n >= 1e6) return (n / 1e6).toFixed(1) + "M";
return n.toLocaleString();
}
function parseCountry(c) {
const currencies = c.currencies
? Object.entries(c.currencies).map(([code, info]) => `${info.name} (${code})`).join(", ")
: "N/A";
const languages = c.languages
? Object.values(c.languages).join(", ")
: "N/A";
return {
name: c.name?.common || "Unknown",
official: c.name?.official || "",
flag: c.flags?.png || c.flags?.svg || "",
region: c.region || "Unknown",
subregion: c.subregion || "N/A",
capital: c.capital?.[0] || "N/A",
area: c.area || 0,
population: c.population || 0,
currencies,
languages,
cca2: c.cca2 || "",
};
}
// API
const API_BASE = "https://restcountries.com/v3.1";
const API_FIELDS = "name,flags,region,subregion,capital,area,population,currencies,languages,cca2";
function showError(msg) {
document.getElementById("loading").innerHTML = `
<div class="gv-no-results">
${msg}<br><br>
<button onclick="fetchAllCountries()" style="
margin-top:12px; padding:8px 20px; border-radius:20px;
border:1px solid #00E5FF; background:none; color:#00E5FF;
font-family:'JetBrains Mono', monospace; font-size:12px;
letter-spacing:1px; cursor:pointer; text-transform:uppercase">
↻ RETRY
</button>
</div>`;
}
async function fetchAllCountries() {
if (window.location.protocol === "file:") {
showError(
"Cannot fetch data when opened as a local file.<br>" +
"Please serve this file via a local server:<br><br>" +
"<code style='font-size:11px; color:#00E5FF'>npx serve .</code>" +
" or " +
"<code style='font-size:11px; color:#00E5FF'>python -m http.server</code>"
);
return;
}
const loadingEl = document.getElementById("loading");
loadingEl.innerHTML = '<div class="gv-spinner"></div>Loading countries...';
loadingEl.style.display = "flex";
try {
const response = await fetch(`${API_BASE}/all?fields=${API_FIELDS}`);
if (!response.ok) throw new Error(`HTTP ${response.status} ${response.statusText}`);
const data = await response.json();
if (!Array.isArray(data) || data.length === 0) throw new Error("Empty or invalid response from API");
allCountries = data.map(parseCountry);
loadingEl.style.display = "none";
updateUI();
} catch (err) {
console.error("GeoVault: failed to load countries —", err);
const reason = err.message === "Failed to fetch"
? "<strong style='color:#FF6B6B'>Network Request Blocked.</strong><br>This is typically caused by an ad-blocker flagging the API. Try disabling your tracker-blocker for this page."
: err.message;
showError(`Failed to load countries.<br><br><small style='color:#7E96AF; line-height:1.5'>${reason}</small>`);
}
}
async function searchCountries(query) {
if (!query.trim()) {
searchResults = null;
updateUI();
return;
}
try {
const response = await fetch(`${API_BASE}/name/${encodeURIComponent(query.trim())}?fields=${API_FIELDS}`);
const data = response.ok ? await response.json() : [];
searchResults = Array.isArray(data) ? data.map(parseCountry) : [];
} catch {
searchResults = [];
}
updateUI();
}
// FILTERING & SORTING
function getFilteredCountries() {
const base = searchResults ?? allCountries;
return base
.filter(c => {
if (showFavorites && !favorites.has(c.name)) return false;
if (regionFilter !== "ALL" && c.region !== regionFilter) return false;
if (popFilter === "1m" && c.population < 1_000_000) return false;
if (popFilter === "100m" && c.population < 100_000_000) return false;
return true;
})
.sort((a, b) => {
if (sortKey === "name") return a.name.localeCompare(b.name, "en", { sensitivity: "base" });
if (sortKey === "population") return b.population - a.population;
return b.area - a.area;
});
}
// RENDER
function createCardHTML(country, index) {
const isSelected = selectedCountry?.name === country.name;
const isFav = favorites.has(country.name);
const fallbackFlag = `https://flagcdn.com/w320/${country.cca2.toLowerCase()}.png`;
const lazyAttr = index > 12 ? 'loading="lazy"' : "";
return `
<div class="gv-card${isSelected ? " selected" : ""}" data-name="${country.name}">
<div class="gv-card-header">
<div class="gv-card-title">
<img class="gv-card-flag" src="${country.flag}" alt="${country.name}" ${lazyAttr}
onerror="this.onerror=null; this.src='${fallbackFlag}'">
<span class="gv-card-name">${country.name}</span>
</div>
<span class="gv-card-badge">${country.region}</span>
</div>
<div class="gv-card-stats">
<div>
<div class="gv-stat-label">POPULATION</div>
<div class="gv-stat-value${isSelected ? " cyan" : ""}">${formatNum(country.population)}</div>
</div>
<div>
<div class="gv-stat-label">AREA</div>
<div class="gv-stat-value">${formatNum(country.area)} km²</div>
</div>
<div>
<div class="gv-stat-label">REGION</div>
<div class="gv-stat-value">${country.region}</div>
</div>
</div>
<div class="gv-card-footer">
<span class="gv-card-langs">${country.languages}</span>
<button class="gv-fav-btn${isFav ? " favorited" : ""}" data-fav-name="${country.name}">
${isFav ? "♥" : "♡"}
</button>
</div>
</div>`;
}
function updateGrid() {
if (allCountries.length === 0) return;
const countries = getFilteredCountries();
const grid = document.getElementById("country-grid");
grid.innerHTML = countries.length
? countries.map(createCardHTML).join("")
: '<div class="gv-no-results">No countries found</div>';
}
function updatePanel() {
const panel = document.getElementById("detail-panel");
if (!selectedCountry) {
panel.style.display = "none";
return;
}
const c = selectedCountry;
panel.style.display = "";
const flagEl = document.getElementById("panel-flag");
flagEl.onerror = () => { flagEl.onerror = null; flagEl.src = `https://flagcdn.com/w320/${c.cca2.toLowerCase()}.png`; };
flagEl.src = c.flag;
flagEl.alt = c.name;
const set = (id, text) => { document.getElementById(id).textContent = text; };
set("panel-name", c.name);
set("panel-capital", c.capital);
set("panel-subregion", `Subregion · ${c.subregion}`);
set("panel-pop", c.population.toLocaleString());
set("panel-area", `${c.area.toLocaleString()} km²`);
set("panel-region", c.region);
set("panel-langs", c.languages);
set("panel-sub2", c.subregion);
set("panel-currencies", c.currencies);
set("panel-cap2", c.capital);
}
function updateStatusBar() {
const count = allCountries.length;
const totalPop = allCountries.reduce((sum, c) => sum + c.population, 0);
const totalArea = allCountries.reduce((sum, c) => sum + c.area, 0);
document.getElementById("nav-count").textContent = `${count} COUNTRIES · REST COUNTRIES API`;
document.getElementById("status-left").textContent = `${count} COUNTRIES · LIVE`;
document.getElementById("status-right").textContent = `COUNTRIES ${count} · WORLD POP ${formatNum(totalPop)} · LAND AREA ${formatNum(totalArea)} KM²`;
}
function updateUI() {
updateGrid();
updatePanel();
updateStatusBar();
}
// EVENT LISTENERS
// close the detail panel
document.getElementById("panel-close").addEventListener("click", () => {
selectedCountry = null;
updateUI();
});
// Search box → wait 400ms after the user stops typing, then search
document.getElementById("search-input").addEventListener("input", e => {
clearTimeout(searchTimer);
searchTimer = setTimeout(() => searchCountries(e.target.value), 400);
});
// Filter (region / population / favorites)
document.getElementById("filters").addEventListener("click", e => {
const btn = e.target.closest(".gv-pill");
if (!btn) return;
if (btn.dataset.region) {
regionFilter = btn.dataset.region;
showFavorites = false;
document.querySelectorAll("[data-region]").forEach(el => el.classList.toggle("active", el.dataset.region === regionFilter));
document.querySelector("[data-fav]").classList.remove("fav-active");
} else if (btn.dataset.pop) {
popFilter = popFilter === btn.dataset.pop ? "none" : btn.dataset.pop;
document.querySelectorAll("[data-pop]").forEach(el => el.classList.toggle("active", el.dataset.pop === popFilter));
} else if (btn.dataset.fav) {
showFavorites = !showFavorites;
btn.classList.toggle("fav-active", showFavorites);
document.querySelectorAll("[data-region]").forEach(el => el.classList.toggle("active", !showFavorites && el.dataset.region === regionFilter));
}
updateUI();
});
// Sort buttons (Name / Population / Area)
document.querySelector(".gv-sort-row").addEventListener("click", e => {
const btn = e.target.closest(".gv-sort-btn");
if (!btn) return;
sortKey = btn.dataset.sort;
document.querySelectorAll(".gv-sort-btn").forEach(el => el.classList.toggle("active", el.dataset.sort === sortKey));
updateUI();
});
// Clicks inside the country grid (card or favorite button)
document.getElementById("country-grid").addEventListener("click", e => {
// Favorite button
const favBtn = e.target.closest(".gv-fav-btn");
if (favBtn) {
e.stopPropagation();
const name = favBtn.dataset.favName;
favorites.has(name) ? favorites.delete(name) : favorites.add(name);
saveFavorites();
updateUI();
return;
}
// Country card → open / close
const card = e.target.closest(".gv-card");
if (card) {
const name = card.dataset.name;
const country = getFilteredCountries().find(c => c.name === name);
selectedCountry = selectedCountry?.name === name ? null : country;
updateUI();
}
});
// Light / Dark theme toggle
document.getElementById("theme-toggle").addEventListener("click", () => {
const app = document.getElementById("app");
const isDark = app.classList.contains("dark");
app.classList.toggle("dark", !isDark);
app.classList.toggle("light", isDark);
document.getElementById("icon-sun").style.display = isDark ? "none" : "";
document.getElementById("icon-moon").style.display = isDark ? "" : "none";
document.getElementById("theme-label").textContent = isDark ? "DARK" : "LIGHT";
});
fetchAllCountries();