Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
196 lines (165 loc) · 6.58 KB
/
Copy pathscript.js
File metadata and controls
196 lines (165 loc) · 6.58 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
// --- Configuration ---
let apiKey = localStorage.getItem('skycast_api_key');
const defaultCity = 'Johannesburg'; // Default location changed to JHB
// --- DOM Elements ---
const modal = document.getElementById('apiKeyModal');
const apiKeyInput = document.getElementById('userApiKey');
const saveKeyBtn = document.getElementById('saveApiKeyBtn');
const cityInput = document.getElementById('cityInput');
const searchBtn = document.getElementById('searchBtn');
const body = document.body;
// --- Map Initialization ---
// Initialize map centered on Johannesburg
let map = L.map('map').setView([-26.2041, 28.0473], 10);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
// --- Map Click Event (Reverse Geocoding) ---
map.on('click', function(e) {
const { lat, lng } = e.latlng;
getWeatherDataByCoords(lat, lng);
});
// --- Event Listeners ---
saveKeyBtn.addEventListener('click', () => {
const key = apiKeyInput.value.trim();
if (key) {
localStorage.setItem('skycast_api_key', key);
apiKey = key;
modal.classList.add('hidden');
getWeatherData(defaultCity);
} else {
alert('Please enter a valid key');
}
});
searchBtn.addEventListener('click', () => {
const city = cityInput.value;
if (city) getWeatherData(city);
});
cityInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
const city = cityInput.value;
if (city) getWeatherData(city);
}
});
// --- API Functions ---
function checkKey() {
if (!apiKey) {
modal.classList.remove('hidden');
} else {
modal.classList.add('hidden');
getWeatherData(defaultCity);
}
}
function clearKey() {
localStorage.removeItem('skycast_api_key');
location.reload();
}
// Fetch by City Name
async function getWeatherData(city) {
try {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;
await fetchData(url);
} catch (error) {
handleError(error);
}
}
// Fetch by Coordinates (Map Click)
async function getWeatherDataByCoords(lat, lon) {
try {
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=metric&appid=${apiKey}`;
await fetchData(url);
} catch (error) {
handleError(error);
}
}
// Common Fetch Logic
async function fetchData(weatherUrl) {
const weatherRes = await fetch(weatherUrl);
if (!weatherRes.ok) {
if (weatherRes.status === 401) throw new Error('Invalid API Key.');
if (weatherRes.status === 404) throw new Error('Location not found.');
throw new Error('Error fetching data.');
}
const weatherData = await weatherRes.json();
// Get Forecast using coordinates from the first result
const forecastRes = await fetch(`https://api.openweathermap.org/data/2.5/forecast?lat=${weatherData.coord.lat}&lon=${weatherData.coord.lon}&units=metric&appid=${apiKey}`);
const forecastData = await forecastRes.json();
updateUI(weatherData);
updateForecast(forecastData.list);
updateMap(weatherData.coord.lat, weatherData.coord.lon);
// Pass timestamp (dt) and timezone to calculate local time for night theme
updateTheme(weatherData.weather[0].main, weatherData.dt, weatherData.timezone);
}
function handleError(error) {
alert(error.message);
if (error.message.includes('Invalid API Key')) clearKey();
}
// --- UI Update Functions ---
function updateUI(data) {
document.getElementById('cityName').textContent = `${data.name}, ${data.sys.country}`;
cityInput.value = data.name; // Update search bar with found name
document.getElementById('dateDisplay').textContent = new Date().toLocaleDateString('en-US', { weekday: 'long', hour: 'numeric', minute: 'numeric' });
document.getElementById('temperature').textContent = `${Math.round(data.main.temp)}°`;
document.getElementById('weatherDescription').textContent = data.weather[0].description;
document.getElementById('weatherIcon').src = `https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png`;
document.getElementById('windSpeed').textContent = `${data.wind.speed} km/h`;
document.getElementById('humidity').textContent = `${data.main.humidity}%`;
document.getElementById('feelsLike').textContent = `${Math.round(data.main.feels_like)}°`;
document.getElementById('visibility').textContent = `${(data.visibility / 1000).toFixed(1)} km`;
document.getElementById('pressure').textContent = `${data.main.pressure} hPa`;
}
function updateForecast(list) {
const container = document.getElementById('forecastContainer');
container.innerHTML = '';
// Filter for noon readings
const dailyData = list.filter(item => item.dt_txt.includes("12:00:00"));
dailyData.slice(0, 5).forEach(day => {
const date = new Date(day.dt * 1000);
const dayName = date.toLocaleDateString('en-US', { weekday: 'short' });
const card = document.createElement('div');
card.className = 'forecast-item';
card.innerHTML = `
<p>${dayName}</p>
<img src="https://openweathermap.org/img/wn/${day.weather[0].icon}.png" alt="icon">
<h3>${Math.round(day.main.temp)}°</h3>
<p style="font-size: 0.8rem">${day.weather[0].main}</p>
`;
container.appendChild(card);
});
}
function updateMap(lat, lon) {
if(!map) return;
map.setView([lat, lon], 10);
map.eachLayer((layer) => {
if (layer instanceof L.Marker) {
map.removeLayer(layer);
}
});
L.marker([lat, lon]).addTo(map)
.bindPopup("Selected Location")
.openPopup();
}
function updateTheme(condition, timestamp, timezoneOffset) {
// Calculate local time of the searched city
const localTime = new Date((timestamp + timezoneOffset) * 1000);
const hours = localTime.getUTCHours();
const isNight = hours >= 19 || hours <= 6;
// Reset Classes
body.className = '';
if (isNight) {
body.classList.add('theme-night');
} else if (['Rain', 'Drizzle', 'Thunderstorm', 'Snow'].includes(condition)) {
body.classList.add('theme-rainy');
} else if (['Clouds', 'Mist', 'Fog', 'Haze'].includes(condition)) {
body.classList.add('theme-cloudy');
} else {
// Default (Clear/Sunny)
body.classList.remove('theme-night', 'theme-rainy', 'theme-cloudy');
}
// Fix Leaflet map sizing on resize (Mobile/Desktop switch)
window.addEventListener('resize', () => {
if(map) map.invalidateSize();
});
}
// Start App
checkKey();