-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
607 lines (527 loc) · 25.8 KB
/
scripts.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
function loadCSSBasedOnUserAgent() {
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.includes('android') || userAgent.includes('iphone') || userAgent.includes('ipad')) {
document.getElementById('CSS-source').href = 'mobile.css'; // Load mobile.css for mobile devices
} else {
document.getElementById('CSS-source').href = 'styles.css'; // Load default.css for other devices
}
}
loadCSSBasedOnUserAgent();
const apiKey = '49b0abfbcd72adcfc70fc7f555198b19';
const mapboxToken = 'pk.eyJ1IjoiczI3Mjg4IiwiYSI6ImNscnA4OW14cDAyZXAyam96Z25rZHdpd3QifQ.8r4lC0ndDr_xmmGzsWKjaQ';
// const apiKey = process.env.REACT_APP_OPENWEATHERMAPAPI;
// const mapboxToken = process.env.REACT_APP_MAPBOXTOKEN;
localStorage.setItem('popupClosed', false);
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
const modeSlider = document.getElementById('modeSlider');
const modeText = document.getElementById('modeText');
const MODE_KEY = 'websiteMode';
function updateMode(mode) {
switch (mode) {
case 'Light':
document.body.classList.remove('dark-mode');
document.body.classList.add('light-mode');
document.body.classList.remove('default-mode');
break;
case 'Default':
document.body.classList.remove('dark-mode', 'light-mode');
document.body.classList.add('default-mode');
break;
case 'Dark':
document.body.classList.remove('light-mode');
document.body.classList.add('dark-mode');
document.body.classList.remove('default-mode');
break;
default:
break;
}
modeText.textContent = mode;
localStorage.setItem(MODE_KEY, mode);
}
if (localStorage.getItem(MODE_KEY)) {
const savedMode = localStorage.getItem(MODE_KEY);
modeSlider.value = savedMode === 'Light' ? '1' : savedMode === 'Dark' ? '3' : '2';
updateMode(savedMode);
} else {
updateMode('Default'); // Set default mode if no saved mode is found
}
// Function to fetch location suggestions from MapBox API
async function getLocationSuggestions(query) {
const response = await fetch(`https://api.mapbox.com/geocoding/v5/mapbox.places/${query}.json?access_token=${mapboxToken}`);
const data = await response.json();
return data.features.map(feature => {
return {
place_name: feature.place_name,
coordinates: feature.center
};
});
}
// Function to display location suggestions
function displayLocationSuggestions(suggestions) {
const suggestionsList = document.getElementById('suggestions-list');
suggestionsList.style.display = 'block';
suggestionsList.innerHTML = '';
suggestions.forEach(suggestion => {
const listItem = document.createElement('li');
listItem.textContent = suggestion.place_name;
listItem.onclick = () => selectLocation(suggestion.coordinates, suggestion.place_name);
suggestionsList.appendChild(listItem);
});
}
// Function to handle the selection of a location from suggestions
async function selectLocation(coordinates, place_name) {
[longitude, latitude] = coordinates;
try {
const weatherData = await getWeatherData(latitude, longitude);
const forecastHourlyData = await getForecastHourlyData(latitude, longitude);
const forecastDailyData = await getForecastDailyData(latitude, longitude);
const mapContainer = document.getElementById('map-container');
mapContainer.style.display = 'grid';
const mapBoxTemperature = document.getElementById('map-box-temperature');
mapBoxTemperature.style.display = 'block';
const mapBoxPrecipitation = document.getElementById('map-box-precipitation');
mapBoxPrecipitation.style.display = 'block';
const mapBoxWind = document.getElementById('map-box-wind');
mapBoxWind.style.display = 'block';
displayCurrentWeather(weatherData, place_name);
displayForecastHourlyData(forecastHourlyData, weatherData);
displayForecastDailyData(forecastDailyData, weatherData);
displayMapData();
// Update the input field value
const locationInput = document.getElementById('location-input-field');
locationInput.value = `${place_name}`;
// Clear suggestions list
const suggestionsList = document.getElementById('suggestions-list');
suggestionsList.innerHTML = '';
// Hide the suggestions list and move cursor out of the input field
suggestionsList.style.display = 'none';
locationInput.blur();
} catch (error) {
console.error('Error fetching weather data:', error);
}
}
// Function to handle user input for location autocomplete
async function handleInput() {
const locationInput = document.getElementById('location-input-field').value;
if (locationInput.length > 0) {
const suggestions = await getLocationSuggestions(locationInput);
displayLocationSuggestions(suggestions);
} else {
const suggestionsList = document.getElementById('suggestions-list');
suggestionsList.innerHTML = '';
}
}
// Functions to fetch weather data from OpenWeatherMap API
async function getWeatherData(latitude, longitude) {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=metric`);
const data = await response.json();
return data;
}
async function getForecastHourlyData(latitude, longitude) {
const response = await fetch(`https://pro.openweathermap.org/data/2.5/forecast/hourly?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=metric&cnt=24`);
const data = await response.json();
return data.list;
}
async function getForecastDailyData(latitude, longitude) {
const response = await fetch(`https://api.openweathermap.org/data/2.5/forecast/daily?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=metric&cnt=7`);
const data = await response.json();
return data.list;
}
// Functions to fetch map data from OpenWeatherMap API add OpenStreetMap tiles and bind them to the map
async function getTA2MapData(latitude, longitude) {
const mapContainer = document.getElementById('map-box-temperature');
if (mapContainer && mapContainer._leaflet_id) {
mapContainer._leaflet_id = null;
}
mapContainer.innerHTML = '';
const TA2Map = L.map('map-box-temperature', {dragging: false, scrollWheelZoom: false, touchZoom: false, doubleClickZoom: false}).setView([latitude, longitude], 10);
const mapLayer = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
})
const weatherLayer = L.tileLayer('http://maps.openweathermap.org/maps/2.0/weather/{op}/{z}/{x}/{y}?appid={apiKey}&opacity=0.6&fill_bound=true', {
apiKey: apiKey,
maxZoom: 18,
attribution: 'Weather © <a href="https://openweathermap.org">OpenWeatherMap</a>',
op: 'TA2',
})
// Add the layers to TA2Map
mapLayer.addTo(TA2Map);
weatherLayer.addTo(TA2Map);
}
async function getPR0MapData(latitude, longitude) {
const mapContainer = document.getElementById('map-box-precipitation');
if (mapContainer && mapContainer._leaflet_id) {
mapContainer._leaflet_id = null;
}
mapContainer.innerHTML = '';
const PR0Map = L.map('map-box-precipitation', {dragging: false, scrollWheelZoom: false, touchZoom: false, doubleClickZoom: false}).setView([latitude, longitude], 7);
const mapLayer = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
})
const weatherLayer = L.tileLayer('http://maps.openweathermap.org/maps/2.0/weather/{op}/{z}/{x}/{y}?appid={apiKey}&opacity=0.6&fill_bound=true', {
apiKey: apiKey,
maxZoom: 18,
attribution: 'Weather © <a href="https://openweathermap.org">OpenWeatherMap</a>',
op: 'PR0',
})
// Add the layers to PR0Map
mapLayer.addTo(PR0Map);
weatherLayer.addTo(PR0Map);
}
async function getWNDMapData(latitude, longitude) {
const mapContainer = document.getElementById('map-box-wind');
if (mapContainer && mapContainer._leaflet_id) {
mapContainer._leaflet_id = null;
}
mapContainer.innerHTML = '';
const WNDMap = L.map('map-box-wind', {dragging: false, scrollWheelZoom: false, touchZoom: false, doubleClickZoom: false}).setView([latitude, longitude], 10);
const mapLayer = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
})
const weatherLayer = L.tileLayer('http://maps.openweathermap.org/maps/2.0/weather/{op}/{z}/{x}/{y}?appid={apiKey}&opacity=0.6&fill_bound=true', {
apiKey: apiKey,
maxZoom: 18,
attribution: 'Weather © <a href="https://openweathermap.org">OpenWeatherMap</a>',
op: 'WND',
})
// Add the layers to WNDMap
mapLayer.addTo(WNDMap);
weatherLayer.addTo(WNDMap);
}
// Function to display current weather and secrets
function displayCurrentWeather(weather, place_name) {
const currentWeatherTitle = document.getElementById('current-weather-title');
currentWeatherTitle.innerHTML = `<h2>Current Weather in ${place_name}</h2>`;
const sunrise = new Date(weather.sys.sunrise * 1000 + weather.timezone * 1000 - 3600 * 1000).toLocaleTimeString();
const sunset = new Date(weather.sys.sunset * 1000 + weather.timezone * 1000 - 3600 * 1000).toLocaleTimeString();
const currentWeatherElement = document.getElementById('current-weather');
currentWeatherElement.innerHTML = `
<p class="current-weather-description">Weather: ${capitalizeFirstLetter(weather.weather[0].description)}</p>
<p class="current-weather-temperature">Temperature: ${Math.round(weather.main.temp)}°C</p>
<p class="current-weather-feels-like">Feels Like: ${Math.round(weather.main.feels_like)}°C</p>
<p class="current-weather-humidity">Humidity: ${Math.round(weather.main.humidity)}%</p>
<p class="current-weather-pressure">Pressure: ${Math.round(weather.main.pressure)}hPa</p>
<p class="current-weather-visibility">Visibility: ${Math.round(weather.visibility / 1000 * 10) / 10}km</p>
<p class="current-weather-wind">Wind Speed: ${Math.round(weather.wind.speed * 10) / 10}m/s</p>
<p class="current-weather-sunrise">Sunrise: ${sunrise}</p>
<p class="current-weather-"sunset>Sunset: ${sunset}</p>
`;
const currentWeatherIcon = document.getElementById('current-weather-icon');
const weatherIconID = weather.weather[0].icon;
if (weatherIconID === '01d') {
currentWeatherIcon.innerHTML = `<img src="images/icons/01d.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#f5e7b9');
} else if (weatherIconID === '01n') {
currentWeatherIcon.innerHTML = `<img src="images/icons/01n.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#7791c2');
} else if (weatherIconID === '02d') {
currentWeatherIcon.innerHTML = `<img src="images/icons/02d.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#a7d6df');
} else if (weatherIconID === '02n') {
currentWeatherIcon.innerHTML = `<img src="images/icons/02n.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#90a5cc');
} else if (weatherIconID === '03d') {
currentWeatherIcon.innerHTML = `<img src="images/icons/03d.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#cccccc');
} else if (weatherIconID === '03n') {
currentWeatherIcon.innerHTML = `<img src="images/icons/03n.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#cccccc');
} else if (weatherIconID === '04d') {
currentWeatherIcon.innerHTML = `<img src="images/icons/04d.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#a3a2a2');
} else if (weatherIconID === '04n') {
currentWeatherIcon.innerHTML = `<img src="images/icons/04n.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#a3a2a2');
} else if (weatherIconID === '09d') {
currentWeatherIcon.innerHTML = `<img src="images/icons/09d.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#9bc7d4');
} else if (weatherIconID === '09n') {
currentWeatherIcon.innerHTML = `<img src="images/icons/09n.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#9bc7d4');
} else if (weatherIconID === '10d') {
currentWeatherIcon.innerHTML = `<img src="images/icons/10d.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#72afc2');
} else if (weatherIconID === '10n') {
currentWeatherIcon.innerHTML = `<img src="images/icons/10n.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#72afc2');
} else if (weatherIconID === '11d') {
currentWeatherIcon.innerHTML = `<img src="images/icons/11d.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#787b9b');
} else if (weatherIconID === '11n') {
currentWeatherIcon.innerHTML = `<img src="images/icons/11n.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#787b9b');
} else if (weatherIconID === '13d') {
currentWeatherIcon.innerHTML = `<img src="images/icons/13d.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#c0fdff');
} else if (weatherIconID === '13n') {
currentWeatherIcon.innerHTML = `<img src="images/icons/13n.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#c0fdff');
} else if (weatherIconID === '50d') {
currentWeatherIcon.innerHTML = `<img src="images/icons/50d.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#f4ffff');
} else if (weatherIconID === '50n') {
currentWeatherIcon.innerHTML = `<img src="images/icons/50n.png" alt="Current Weather Icon">`;
document.documentElement.style.setProperty('--default-background-color-body', '#f4ffff');
} else {
currentWeatherIcon.innerHTML = `<img src="images/001-meteorology.png" alt="Current Weather Icon">`;
}
// Memes
const memeBoxElement = document.getElementById('meme-box');
if (Math.round(weather.main.temp) <= -10) {
memeBoxElement.innerHTML = `<img src="images/memes/Jack-Nicholson-The-Shining-Snow.jpg" alt="Meme">`;
} else if (Math.round(weather.main.temp) >= 20 && weather.main.temp <= 25) {
memeBoxElement.innerHTML = `<img src="images/memes/0_ZjYSm_q36J4KChdn.jpg" alt="Meme">`;
} else if (Math.round(weather.main.temp) > 25 && weather.main.temp <= 30) {
memeBoxElement.innerHTML = `<img src="images/memes/5d018c085cf9819634dee6572fb5dd79.jpg" alt="Meme">`;
} else if (Math.round(weather.main.temp) > 30) {
memeBoxElement.innerHTML = `<img src="images/memes/Heat_wave.jpg" alt="Meme">`;
} else if (Math.round(weather.main.temp) >= -9 && weather.main.temp < 19) {
if (weather.weather[0].main === 'Rain' || weather.weather[0].main === 'Drizzle' || weather.weather[0].main === 'Thunderstorm') {
memeBoxElement.innerHTML = `<img src="images/memes/8d37f35ff717b6691ab1acf90dce6c83.jpg" alt="Meme">`;
} else if (weather.weather[0].main === 'Snow') {
memeBoxElement.innerHTML = `<img src="images/memes/Snowing.jpg" alt="Meme">`;
} else if (weather.weather[0].main === 'Clouds') {
memeBoxElement.innerHTML = `<img src="images/memes/Clouds.jpg" alt="Meme">`;
} else if (weatherIconID === '50d') {
memeBoxElement.innerHTML = `<img src="images/memes/Mist.jpg" alt="Meme">`;
} else { // Clear weather
memeBoxElement.innerHTML = `<img src="images/memes/bbd.gif" alt="Meme">`;
}
} else {
memeBoxElement.innerHTML = `<img src="images/memes/bbd.gif" alt="Meme">`;
}
// Display the popup if it hasn't been closed
if (localStorage.getItem('popupClosed') === 'true') {
} else {
setTimeout(displayPopupOnDesktop, 5000);
}
// Display the current weather container
const currentWeatherContainer = document.getElementById('current-weather-container');
currentWeatherContainer.style.display = 'grid';
}
// Function to display forecast hourly data
function displayForecastHourlyData(forecastHourly, weather) {
const forecastHourlyDataElement = document.getElementById('forecast-hourly-box');
forecastHourlyDataElement.innerHTML = '';
const weatherData = weather;
forecastHourly.forEach(hour => {
const date = new Date(hour.dt * 1000 + weatherData.timezone * 1000); // Convert Unix timestamp to JavaScript date object
const time = date.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' });
const weather = hour.weather[0];
const temperature = Math.round(hour.main.temp);
const feelsLike = Math.round(hour.main.feels_like);
const humidity = Math.round(hour.main.humidity);
const description = capitalizeFirstLetter(weather.description);
const iconUrl = `images/icons/${weather.icon}.png`;
const forecastItem = `
<div class="forecast-hourly-item">
<div class="forecast-hourly-time"><p>${time}</p></div>
<div class="forecast-hourly-icon"><img src="${iconUrl}" alt="${description}"></div>
<div class="forecast-hourly-temperature"><p>${temperature}/${feelsLike}°C</p></div>
<div class="forecast-hourly-humidity"><p>${humidity}%</p></div>
<div class="forecast-hourly-description"><p>${description}</p></div>
</div>
`;
forecastHourlyDataElement.innerHTML += forecastItem;
});
// Display the forecast title and table
const forecastHourlyContainer = document.getElementById('forecast-hourly-container');
forecastHourlyContainer.style.display = 'grid';
}
// Function to display forecast daily data
function displayForecastDailyData(forecastDaily, weather) {
const forecastDailyDataElement = document.getElementById('forecast-daily-box');
forecastDailyDataElement.innerHTML = '';
forecastDaily.forEach(day => {
const date = new Date(day.dt * 1000);
const dayOfWeek = date.toLocaleDateString('en-US', { weekday: 'long' });
const weather = day.weather[0];
const temperatureDay = Math.round(day.temp.day);
const temperatureNight = Math.round(day.temp.night);
const humidity = Math.round(day.humidity);
const pressure = Math.round(day.pressure);
const speed = Math.round(day.speed * 10) / 10;
const description = capitalizeFirstLetter(weather.description);
const iconUrl = `images/icons/${weather.icon}.png`;
const forecastItem = `
<div class="forecast-daily-item">
<div class="forecast-daily-day"><p>${dayOfWeek}</p></div>
<div class="forecast-daily-temperature-day"><p>Day: ${temperatureDay}°C</p></div>
<div class="forecast-daily-temperature-night"><p>Night: ${temperatureNight}°C</p></div>
<div class="forecast-daily-humidity"><p>Humidity: ${humidity}%</p></div>
<div class="forecast-daily-pressure"><p>P: ${pressure}hPa</p></div>
<div class="forecast-daily-wind"><p>Wind: ${speed}m/s</p></div>
<div class="forecast-daily-icon"><img src="${iconUrl}" alt="${description}"></div>
<div class="forecast-daily-description"><p>${description}</p></div>
</div>
`;
forecastDailyDataElement.innerHTML += forecastItem;
});
// Display the forecast title and table
const forecastDailyContainer = document.getElementById('forecast-daily-container');
forecastDailyContainer.style.display = 'grid';
}
// Function to display map data
function displayMapData() {
const mapDataElement = document.getElementById('map-container');
mapDataElement.style.display = 'grid';
const mapTitle = document.getElementById('map-title');
mapTitle.innerHTML = `<h2>Temperature Map</h2>`;
const mapBoxTemperature = document.getElementById('map-box-temperature');
mapBoxTemperature.style.display = 'block';
const mapBoxPrecipitation = document.getElementById('map-box-precipitation');
mapBoxPrecipitation.style.display = 'none';
const mapBoxWind = document.getElementById('map-box-wind');
mapBoxWind.style.display = 'none';
getTA2MapData(latitude, longitude);
}
// Function to handle search when button is clicked or Enter is pressed
function searchWeatherOnClick() {
const locationInput = document.getElementById('location-input-field').value;
if (locationInput.length > 0) {
// Fetch suggestions from MapBox API
getLocationSuggestions(locationInput)
.then(suggestions => {
if (suggestions.length > 0) {
const coordinates = suggestions[0].coordinates;
const place_name = suggestions[0].place_name;
selectLocation(coordinates, place_name);
}
})
.catch(error => {
console.error('Error fetching location suggestions:', error);
});
}
else {
alert('Please enter a location to search for weather data.');
}
}
// Function to clear the input field
function clearInputField() {
const locationInput = document.getElementById('location-input-field');
locationInput.value = '';
}
// Function to display the popup on desktop
function displayPopupOnDesktop() {
localStorage.setItem('popupClosed', false);
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
if (!isMobile) {
const popup = document.getElementById('popup');
popup.style.display = 'block';
localStorage.setItem('popupClosed', true);
}
}
// Function to close the popup
function closePopup() {
const popup = document.getElementById('popup');
popup.style.display = 'none';
}
// Function to display the meme container
const konamiCode = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'b', 'a'];
let konamiCodeIndex = 0;
function checkKonamiCode(event) {
const key = event.key;
const requiredKey = konamiCode[konamiCodeIndex];
if (key === requiredKey) {
konamiCodeIndex++;
if (konamiCodeIndex === konamiCode.length) {
// Display the meme container
const memeContainer = document.getElementById('meme-container');
memeContainer.style.display = 'grid';
konamiCodeIndex = 0;
}
} else {
konamiCodeIndex = 0;
}
}
// Attach an event listener to the input field for real-time suggestions, search button for search, and location input for clearing the input field
const inputField = document.getElementById('location-input-field');
inputField.addEventListener('input', handleInput);
const searchButton = document.getElementById('search-button');
searchButton.addEventListener('click', searchWeatherOnClick);
const locationInput = document.getElementById('location-input-field');
locationInput.addEventListener('click', clearInputField);
// Reload the page when the weather app icon is clicked
const weatherAppIcon = document.getElementById('weather-app-icon');
weatherAppIcon.addEventListener('click', () => {
location.reload();
});
// Function to display the map data
const mapTemperature = document.getElementById('map-temperature');
mapTemperature.addEventListener('click', () => {
const mapTitle = document.getElementById('map-title');
mapTitle.innerHTML = `<h2>Temperature Map</h2>`;
const mapBoxTemperature = document.getElementById('map-box-temperature');
mapBoxTemperature.style.display = 'block';
const mapBoxPrecipitation = document.getElementById('map-box-precipitation');
mapBoxPrecipitation.style.display = 'none';
const mapBoxWind = document.getElementById('map-box-wind');
mapBoxWind.style.display = 'none';
getTA2MapData(latitude, longitude);
window.scrollTo(0, document.body.scrollHeight);
});
const mapPrecipitation = document.getElementById('map-precipitation');
mapPrecipitation.addEventListener('click', () => {
const mapTitle = document.getElementById('map-title');
mapTitle.innerHTML = `<h2>Precipitation Map</h2>`;
const mapBoxTemperature = document.getElementById('map-box-temperature');
mapBoxTemperature.style.display = 'none';
const mapBoxPrecipitation = document.getElementById('map-box-precipitation');
mapBoxPrecipitation.style.display = 'block';
const mapBoxWind = document.getElementById('map-box-wind');
mapBoxWind.style.display = 'none';
getPR0MapData(latitude, longitude);
window.scrollTo(0, document.body.scrollHeight);
});
const mapWind = document.getElementById('map-wind');
mapWind.addEventListener('click', () => {
const mapTitle = document.getElementById('map-title');
mapTitle.innerHTML = `<h2>Wind Map</h2>`;
const mapBoxTemperature = document.getElementById('map-box-temperature');
mapBoxTemperature.style.display = 'none';
const mapBoxPrecipitation = document.getElementById('map-box-precipitation');
mapBoxPrecipitation.style.display = 'none';
const mapBoxWind = document.getElementById('map-box-wind');
mapBoxWind.style.display = 'block';
getWNDMapData(latitude, longitude);
window.scrollTo(0, document.body.scrollHeight);
});
// Handle Enter key press in input field
inputField.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
searchWeatherOnClick();
const searchButton = document.getElementById('search-button');
searchButton.addEventListener('click', searchWeatherOnClick);
}
});
// Event listener for key presses
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape') {
konamiCodeIndex = 0;
} else {
checkKonamiCode(event);
}
});
// Event listener for mode slider change
modeSlider.addEventListener('input', () => {
let mode = 'Default';
switch (parseInt(modeSlider.value)) {
case 1:
mode = 'Light';
break;
case 2:
mode = 'Default';
break;
case 3:
mode = 'Dark';
break;
default:
break;
}
updateMode(mode);
});