-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
445 lines (354 loc) · 17.8 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//Imports
import { clearPreviousResults, clearPrevDetailedResults } from './clearResults.js';
import { convertDateFormat, getDateAndTime, getTime } from './dateFunctions.js';
import { displayWeatherData } from './displayWeatherData.js';
import { threeCapitals } from './threeCaps.js';
import { getWeatherImage, getWeatherDescription } from './weatherCodes.js';
//Global variables
const lookUpCity = document.querySelector('.lookup-city');
// const logo = document.querySelector('.logo');
const placeSearch = document.querySelector('.search-bar');
const main = document.querySelector('main');
const header = document.querySelector('header');
let inputField = document.querySelector('input');
// Select all toggle
document.getElementById('select-all').addEventListener('change', function() {
const allCheckboxes = document.querySelectorAll('.filters input[type="checkbox"]');
const selectAllChecked = this.checked;
allCheckboxes.forEach(checkbox => {
checkbox.checked = selectAllChecked;
});
});
//Collapsible window for advanced search filter table
document.addEventListener('DOMContentLoaded', function() {
const collapsibleTitle = document.querySelector('.collapsible-title');
const collapsibleContent = document.querySelector('.collapsible-content');
collapsibleContent.style.display = 'none';
collapsibleTitle.addEventListener('click', function() {
if (collapsibleContent.style.display === 'none' || collapsibleContent.style.display === '') {
collapsibleContent.style.display = 'flex';
collapsibleTitle.textContent = 'Advanced Search ⬇️';
} else {
collapsibleContent.style.display = 'none';
collapsibleTitle.textContent = 'Advanced Search ➡️';
}
});
});
// Function to fetch weather data and create the panel
function fetchWeatherData(capital) {
const { timezone, latitude, longitude, country, name } = capital;
fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=temperature_2m,relative_humidity_2m,weather_code&timezone=${timezone}`)
.then(response => response.json())
.then(weatherData => {
const capitalsPanel = document.querySelector('.capitals');
const singlePanel = document.createElement('div');
singlePanel.className = 'single-panel';
const capitalName = document.createElement('h1');
capitalName.className = 'city-name';
capitalName.innerText = name;
const capitalCountry = document.createElement('h2');
capitalCountry.className = 'country-name';
capitalCountry.innerText = country;
const dateTime = document.createElement('p');
dateTime.className = 'date-time';
dateTime.innerHTML = getDateAndTime(weatherData.current.time);
const weatherImage = document.createElement('img');
weatherImage.src = getWeatherImage(weatherData.current.weather_code);
weatherImage.alt = getWeatherDescription(weatherData.current.weather_code);
weatherImage.className = 'weather-image-card';
const temperature = document.createElement('p');
temperature.className = 'temperature';
temperature.innerHTML = `${Math.floor(weatherData.current.temperature_2m)}°C`;
const weatherCode = document.createElement('p');
weatherCode.className = 'weather-code';
weatherCode.innerHTML = getWeatherDescription(weatherData.current.weather_code);
const humidityPanel = document.createElement('div');
humidityPanel.className = 'humidity-show';
const humidityIcon = document.createElement('img');
humidityIcon.src = './assets/humidity.png';
humidityIcon.alt = 'humidity and percentage symbols';
humidityIcon.className = 'humidity-symbol';
const humidity = document.createElement('p');
humidity.className = 'humidity';
humidity.innerHTML = `${weatherData.current.relative_humidity_2m}%`;
humidityPanel.appendChild(humidityIcon);
humidityPanel.appendChild(humidity);
capitalsPanel.appendChild(singlePanel);
singlePanel.appendChild(capitalName);
singlePanel.appendChild(capitalCountry);
singlePanel.appendChild(dateTime);
singlePanel.appendChild(weatherImage);
singlePanel.appendChild(temperature);
singlePanel.appendChild(weatherCode);
singlePanel.appendChild(humidityPanel);
});
}
//Show default 3 capitals on pageload
threeCapitals.forEach(fetchWeatherData);
lookUpCity.addEventListener('submit', (event) => {
event.preventDefault();
handleEvent();
const logo = document.querySelector('#logo');
logo.style.height = '12rem';
});
function handleEvent() {
const inputValue = inputField.value.trim();
if (!inputValue) return;
const defaultCapitals = document.querySelector(".capitals");
defaultCapitals.style.display = 'none';
clearPreviousResults();
clearPrevDetailedResults();
const collapsibleContent = document.querySelector('.collapsible-content');
collapsibleContent.style.display = 'none';
const apiGeoEndcode = `https://geocoding-api.open-meteo.com/v1/search?name=${inputValue}&count=1&language=en&format=json`;
// console.log(apiGeoEndcode);
fetch(apiGeoEndcode)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// console.log(data);
const cityName = document.createElement('h1');
cityName.className = 'city-name';
if (data.results && data.results.length > 0) {
let longitude = data.results[0].longitude;
let latitude = data.results[0].latitude;
let timezone = data.results[0].timezone;
let country = data.results[0].country;
let city = data.results[0].name;
let admin1 = data.results[0].admin1;
// console.log('Longitude:', longitude, 'Latitude:', latitude);
cityName.innerText = `${city}`;
const adminCountry = document.createElement('h2');
adminCountry.className = 'admin-country';
adminCountry.innerText = `${admin1}, ${country}`;
const apiWeatherEncode = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=temperature_2m,relative_humidity_2m,weather_code&daily=weather_code,temperature_2m_max,temperature_2m_min,sunrise,sunset&timezone=${timezone}`;
console.log(apiWeatherEncode);
fetch(apiWeatherEncode)
.then(response => response.json())
.then(weatherData => {
console.log(weatherData);
const dateTime = document.createElement('p');
dateTime.className = 'date-time';
dateTime.innerHTML = getDateAndTime(weatherData.current.time);
const sunsetSunriseLeft = document.createElement('div');
sunsetSunriseLeft.className = 'sunset-sunrise';
const sunIconLeft = document.createElement('img');
sunIconLeft.src = './assets/sun-morning.png';
sunIconLeft.alt = 'sun';
sunIconLeft.className = 'sun';
sunsetSunriseLeft.appendChild(sunIconLeft);
const sunriseLeft = document.createElement('p');
sunriseLeft.className = 'time';
sunriseLeft.innerText = getTime(weatherData.daily.sunrise[0]);
sunsetSunriseLeft.appendChild(sunriseLeft);
const moonIconLeft = document.createElement('img');
moonIconLeft.src = './assets/moon-evening.png';
moonIconLeft.alt = 'moon';
moonIconLeft.className = 'moon';
sunsetSunriseLeft.appendChild(moonIconLeft);
const sunsetLeft = document.createElement('p');
sunsetLeft.className = 'time';
sunsetLeft.innerText = getTime(weatherData.daily.sunset[0]);
sunsetSunriseLeft.appendChild(sunsetLeft);
const temperature = document.createElement('p');
temperature.className = 'temperature';
temperature.innerHTML = `${Math.floor(weatherData.current.temperature_2m)}°C`;
const weatherCode = document.createElement('p');
weatherCode.className = 'weather-code';
weatherCode.innerHTML = getWeatherDescription(weatherData.current.weather_code);
const humidityPanel = document.createElement('div');
humidityPanel.className = 'humidity-show';
const humidityIcon = document.createElement('img');
humidityIcon.src = './assets/humidity.png';
humidityIcon.alt = 'humidity and percentage symbols';
humidityIcon.className = 'humidity-symbol';
const humidity = document.createElement('p');
humidity.className = 'humidity';
humidity.innerHTML = `${weatherData.current.relative_humidity_2m}%`;
humidityPanel.appendChild(humidityIcon);
humidityPanel.appendChild(humidity);
const weatherCodeImage = document.createElement('img');
weatherCodeImage.src = getWeatherImage(weatherData.current.weather_code);
weatherCodeImage.alt = getWeatherDescription(weatherData.current.weather_code);
header.style.alignSelf = 'baseline';
const weatherBoard = document.createElement('div');
weatherBoard.className = 'weather-board';
main.appendChild(weatherBoard);
const leftPanel = document.createElement('div');
leftPanel.className = 'left-panel';
weatherBoard.appendChild(leftPanel);
leftPanel.appendChild(cityName);
leftPanel.appendChild(adminCountry);
leftPanel.appendChild(dateTime);
leftPanel.appendChild(sunsetSunriseLeft);
leftPanel.appendChild(weatherCodeImage);
leftPanel.appendChild(temperature);
leftPanel.appendChild(weatherCode);
leftPanel.appendChild(humidityPanel);
const rightPanel = document.createElement('div');
rightPanel.className = 'right-panel';
weatherBoard.appendChild(rightPanel);
const timeArray = weatherData.daily.time;
const slicedArray = timeArray.slice(1);
console.log('Original array:', timeArray);
console.log('Array after slice(1):', slicedArray);
slicedArray.forEach((date, index) => {
const nextDay = document.createElement('div');
nextDay.className = 'next-day';
rightPanel.appendChild(nextDay);
const dayMonth = document.createElement('p');
dayMonth.className = 'day-month';
dayMonth.innerText = convertDateFormat(timeArray[index + 1]);
nextDay.appendChild(dayMonth);
const sunsetSunriseRight = document.createElement('div');
sunsetSunriseRight.className = 'sunset-sunrise';
const sunIconRight = document.createElement('img');
sunIconRight.src = './assets/sun-morning.png';
sunIconRight.alt = 'sun';
sunIconRight.className = 'sun';
sunsetSunriseRight.appendChild(sunIconRight);
const sunriseRight = document.createElement('p');
sunriseRight.className = 'time';
sunriseRight.innerText = getTime(weatherData.daily.sunrise[index + 1]);
sunsetSunriseRight.appendChild(sunriseRight);
const moonIconRight = document.createElement('img');
moonIconRight.src = './assets/moon-evening.png';
moonIconRight.alt = 'moon';
moonIconRight.className = 'moon';
sunsetSunriseRight.appendChild(moonIconRight);
const sunsetRight = document.createElement('p');
sunsetRight.className = 'time';
sunsetRight.innerText = getTime(weatherData.daily.sunset[index + 1]);
sunsetSunriseRight.appendChild(sunsetRight);
nextDay.appendChild(sunsetSunriseRight);
const weatherCodeRight = document.createElement('p');
weatherCodeRight.className = 'weather-code';
weatherCodeRight.innerHTML = getWeatherDescription(weatherData.daily.weather_code[index +1]);
nextDay.appendChild(weatherCodeRight);
const nextDayImage = document.createElement('img');
nextDayImage.src = getWeatherImage(weatherData.current.weather_code);
nextDayImage.alt = getWeatherDescription(weatherData.current.weather_code);
nextDay.appendChild(nextDayImage);
const maxMinTemp = document.createElement('p');
maxMinTemp.className = 'max-min-temp';
maxMinTemp.innerText = `${Math.floor(weatherData.daily.temperature_2m_max[index + 1])}°C / ${Math.floor(weatherData.daily.temperature_2m_min[index + 1])}°C`;
nextDay.appendChild(maxMinTemp);
console.log(`Processing date: ${date} (original index: ${index + 1})`);
});
})
.catch(error => console.error('Error fetching weather data:', error));
} else {
cityName.innerText = "Name not recognised";
cityName.style.fontSize = "2rem";
main.removeChild(placeSearch);
header.appendChild(placeSearch);
const weatherBoard = document.createElement('div');
weatherBoard.className = 'weather-board';
main.appendChild(weatherBoard);
const leftPanel = document.createElement('div');
leftPanel.className = 'left-panel';
weatherBoard.appendChild(leftPanel);
leftPanel.appendChild(cityName);
const rightPanel = document.createElement('div');
rightPanel.className = 'right-panel';
weatherBoard.appendChild(rightPanel);
}
})
.catch(error => console.error('Error fetching geocoding data:', error));
inputField.value = '';
inputField.focus();
}
// Advanced Search function
document.addEventListener('DOMContentLoaded', () => {
const filterForm = document.querySelector("#filter-list");
filterForm.addEventListener('submit', (event) => {
event.preventDefault();
handleDetailedSearch();
const logo = document.querySelector('#logo');
logo.style.height = '12rem';
});
});
async function handleDetailedSearch() {
const detailedInput = document.querySelector("#detailed-city-name");
const inputValue = detailedInput.value.trim();
if (!inputValue) return;
const defaultCapitals = document.querySelector(".capitals");
defaultCapitals.style.display = 'none';
clearPreviousResults();
clearPrevDetailedResults();
const collapsibleContent = document.querySelector('.collapsible-content');
collapsibleContent.style.display = 'none';
// API call to get city coordinates
const apiGeoEndcode = `https://geocoding-api.open-meteo.com/v1/search?name=${inputValue}&count=1&language=en&format=json`;
try {
const geoResponse = await fetch(apiGeoEndcode);
if (!geoResponse.ok) {
throw new Error('Network response was not ok');
}
const geoData = await geoResponse.json();
if (geoData.results && geoData.results.length > 0) {
let longitude = geoData.results[0].longitude;
let latitude = geoData.results[0].latitude;
let country = geoData.results[0].country;
let city = geoData.results[0].name;
let admin1 = geoData.results[0].admin1;
// Display city information
const weatherContainer = document.createElement('div');
weatherContainer.className = 'detailed-results';
weatherContainer.innerHTML = '';
document.querySelector('main').appendChild(weatherContainer);
const cityName = document.createElement('h1');
cityName.className = 'city-name';
cityName.innerText = `${city}`;
const adminCountry = document.createElement('h2');
adminCountry.className = 'admin-country';
adminCountry.innerText = `${admin1}, ${country}`;
weatherContainer.appendChild(cityName);
weatherContainer.appendChild(adminCountry);
// Get the checked filters from the checkboxes
const dailyCheckBoxes = document.querySelectorAll(".daily-filters .filters input:checked");
const currentCheckBoxes = document.querySelectorAll(".current-filters .filters input:checked");
const getCheckedFilters = (checkboxes) => {
return Array.from(checkboxes)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
};
let dailyFilters = getCheckedFilters(dailyCheckBoxes);
let currentFilters = getCheckedFilters(currentCheckBoxes);
console.log('Daily Filters:', dailyFilters);
console.log('Current Filters:', currentFilters);
// Build the API URL
let apiWeatherEncode = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}`;
if (dailyFilters.length > 0) {
apiWeatherEncode += `&daily=${dailyFilters.join(',')}`;
}
if (currentFilters.length > 0) {
apiWeatherEncode += `¤t=${currentFilters.join(',')}`;
}
apiWeatherEncode += `&timezone=auto`;
console.log("Final API URL:", apiWeatherEncode);
// Make the second API call to get weather data
try {
const weatherResponse = await fetch(apiWeatherEncode);
if (!weatherResponse.ok) {
throw new Error('Weather API response was not ok');
}
const weatherData = await weatherResponse.json();
console.log('Weather data:', weatherData);
const dateTime = document.createElement('p');
dateTime.className = 'date-time';
dateTime.innerHTML = getDateAndTime(weatherData.current.time);
weatherContainer.appendChild(dateTime);
displayWeatherData(dailyFilters, currentFilters, weatherData, weatherContainer);
} catch (error) {
console.error('Error fetching weather data:', error);
}
}
} catch (error) {
console.error('Error fetching city data:', error);
}
}