-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
366 lines (318 loc) · 11.7 KB
/
Copy pathscript.js
File metadata and controls
366 lines (318 loc) · 11.7 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
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
// -------------------------------
// CONFIG
// -------------------------------
const BASE_URL = "https://api.open-meteo.com/v1/forecast";
const GEOCODING_URL = "https://geocoding-api.open-meteo.com/v1/search";
// Weather code to icon mapping (simplified)
const weatherIcons = {
0: "icon-sunny.webp", // Clear sky
1: "icon-partly-cloudy.webp", // Mainly clear
2: "icon-partly-cloudy.webp", // Partly cloudy
3: "icon-overcast.webp", // Overcast
45: "icon-fog.webp", // Fog
48: "icon-fog.webp", // Depositing rime fog
51: "icon-drizzle.webp", // Light drizzle
53: "icon-drizzle.webp", // Moderate drizzle
55: "icon-drizzle.webp", // Dense drizzle
56: "icon-drizzle.webp", // Light freezing drizzle
57: "icon-drizzle.webp", // Dense freezing drizzle
61: "icon-rain.webp", // Slight rain
63: "icon-rain.webp", // Moderate rain
65: "icon-rain.webp", // Heavy rain
66: "icon-rain.webp", // Light freezing rain
67: "icon-rain.webp", // Heavy freezing rain
71: "icon-snow.webp", // Slight snow fall
73: "icon-snow.webp", // Moderate snow fall
75: "icon-snow.webp", // Heavy snow fall
77: "icon-snow.webp", // Snow grains
80: "icon-rain.webp", // Slight rain showers
81: "icon-rain.webp", // Moderate rain showers
82: "icon-rain.webp", // Violent rain showers
85: "icon-snow.webp", // Slight snow showers
86: "icon-snow.webp", // Heavy snow showers
95: "icon-storm.webp", // Thunderstorm
96: "icon-storm.webp", // Thunderstorm with slight hail
99: "icon-storm.webp" // Thunderstorm with heavy hail
};
// HTML ELEMENTS
const searchInput = document.getElementById("searchInput");
const searchBtn = document.getElementById("searchBtn");
const tempEl = document.getElementById("temp");
const conditionEl = document.getElementById("condition");
const locationEl = document.getElementById("location");
const iconEl = document.getElementById("icon");
const feelsLikeEl = document.getElementById("feelsLike");
const humidityEl = document.getElementById("humidity");
const windEl = document.getElementById("wind");
const precipEl = document.getElementById("precip");
const forecastGrid = document.getElementById("forecast");
const hourlyGrid = document.getElementById("hourly");
const unitsSelect = document.getElementById("unitsSelect");
const geoBtn = document.getElementById("geoBtn");
const prevDayBtn = document.getElementById("prevDay");
const nextDayBtn = document.getElementById("nextDay");
const selectedDayEl = document.getElementById("selectedDay");
let units = "metric"; // metric or imperial
let currentLocation = null;
let forecastData = null;
let currentDayIndex = 0;
// -------------------------------
// GEOCODING
// -------------------------------
async function geocodeCity(city) {
const url = `${GEOCODING_URL}?name=${encodeURIComponent(city)}&count=1&language=en&format=json`;
const res = await fetch(url);
if (!res.ok) throw new Error("Geocoding failed");
const data = await res.json();
if (!data.results || data.results.length === 0) throw new Error("City not found");
return data.results[0];
}
// -------------------------------
// FETCH WEATHER
// -------------------------------
async function fetchWeather(lat, lon) {
const url = `${BASE_URL}?latitude=${lat}&longitude=${lon}¤t_weather=true&hourly=temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,weathercode,wind_speed_10m&daily=weathercode,temperature_2m_max,temperature_2m_min,sunrise,sunset&timezone=auto&forecast_days=7`;
const res = await fetch(url);
if (!res.ok) throw new Error("Weather data fetch failed");
return await res.json();
}
// -------------------------------
// WEATHER ICON MAPPING
// -------------------------------
function getWeatherIcon(code) {
return `./assets/images/${weatherIcons[code] || "icon-sunny.webp"}`;
}
function getWeatherDescription(code) {
const descriptions = {
0: "Clear sky",
1: "Mainly clear",
2: "Partly cloudy",
3: "Overcast",
45: "Fog",
48: "Depositing rime fog",
51: "Light drizzle",
53: "Moderate drizzle",
55: "Dense drizzle",
56: "Light freezing drizzle",
57: "Dense freezing drizzle",
61: "Slight rain",
63: "Moderate rain",
65: "Heavy rain",
66: "Light freezing rain",
67: "Heavy freezing rain",
71: "Slight snow fall",
73: "Moderate snow fall",
75: "Heavy snow fall",
77: "Snow grains",
80: "Slight rain showers",
81: "Moderate rain showers",
82: "Violent rain showers",
85: "Slight snow showers",
86: "Heavy snow showers",
95: "Thunderstorm",
96: "Thunderstorm with slight hail",
99: "Thunderstorm with heavy hail"
};
return descriptions[code] || "Unknown";
}
// -------------------------------
// UNIT CONVERSIONS
// -------------------------------
function convertTemp(temp) {
return units === "imperial" ? (temp * 9/5) + 32 : temp;
}
function convertWindSpeed(speed) {
return units === "imperial" ? speed * 0.621371 : speed;
}
function convertPrecipitation(precip) {
return units === "imperial" ? precip / 25.4 : precip;
}
function getTempUnit() {
return units === "imperial" ? "°F" : "°C";
}
function getWindUnit() {
return units === "imperial" ? "mph" : "km/h";
}
function getPrecipUnit() {
return units === "imperial" ? "in" : "mm";
}
// -------------------------------
// UPDATE CURRENT WEATHER
// -------------------------------
function updateCurrent(data) {
const current = data.current_weather;
const hourly = data.hourly;
// Find current hour index
const now = new Date();
const currentHour = now.getHours();
const currentIndex = hourly.time.findIndex(time => {
const hour = new Date(time).getHours();
return hour === currentHour;
});
locationEl.textContent = currentLocation ? `${currentLocation.name}, ${currentLocation.country}` : "Unknown Location";
conditionEl.textContent = getWeatherDescription(current.weathercode);
iconEl.src = getWeatherIcon(current.weathercode);
const temp = convertTemp(current.temperature);
tempEl.textContent = `${temp.toFixed(1)}${getTempUnit()}`;
if (currentIndex !== -1) {
const feelsLike = convertTemp(hourly.apparent_temperature[currentIndex]);
feelsLikeEl.textContent = `${feelsLike.toFixed(1)}${getTempUnit()}`;
humidityEl.textContent = `${hourly.relative_humidity_2m[currentIndex]}%`;
const windSpeed = convertWindSpeed(hourly.wind_speed_10m[currentIndex]);
windEl.textContent = `${windSpeed.toFixed(1)} ${getWindUnit()}`;
const precip = convertPrecipitation(hourly.precipitation[currentIndex]);
precipEl.textContent = `${precip.toFixed(2)} ${getPrecipUnit()}`;
}
}
// -------------------------------
// UPDATE 7-DAY FORECAST
// -------------------------------
function updateForecast(data) {
const daily = data.daily;
forecastGrid.innerHTML = "";
daily.time.forEach((date, index) => {
const div = document.createElement("div");
div.className = "forecast-card";
const weekday = new Date(date).toLocaleDateString("en-US", { weekday: "short" });
const maxTemp = convertTemp(daily.temperature_2m_max[index]);
const minTemp = convertTemp(daily.temperature_2m_min[index]);
div.innerHTML = `
<div class="card-day">${weekday}</div>
<img src="${getWeatherIcon(daily.weathercode[index])}" width="48" alt="weather icon" />
<div class="card-temp">
${maxTemp.toFixed(0)}${getTempUnit()} / ${minTemp.toFixed(0)}${getTempUnit()}
</div>
`;
div.addEventListener("click", () => {
currentDayIndex = index;
updateSelectedDay();
updateHourly(data, index);
});
forecastGrid.appendChild(div);
});
updateHourly(data, 0);
}
// -------------------------------
// UPDATE HOURLY FORECAST
// -------------------------------
function updateHourly(data, dayIndex) {
const hourly = data.hourly;
hourlyGrid.innerHTML = "";
// Get hours for the selected day
const startIndex = dayIndex * 24;
const endIndex = startIndex + 24;
for (let i = startIndex; i < endIndex; i++) {
if (i >= hourly.time.length) break;
const time = new Date(hourly.time[i]);
const hour = time.getHours();
const displayHour = hour === 0 ? "12AM" : hour < 12 ? `${hour}AM` : hour === 12 ? "12PM" : `${hour - 12}PM`;
const div = document.createElement("div");
div.className = "hourly-item";
const temp = convertTemp(hourly.temperature_2m[i]);
div.innerHTML = `
<div>${displayHour}</div>
<img src="${getWeatherIcon(hourly.weathercode[i])}" width="40" alt="weather icon" />
<div>${temp.toFixed(0)}${getTempUnit()}</div>
`;
hourlyGrid.appendChild(div);
}
}
// -------------------------------
// UPDATE SELECTED DAY DISPLAY
// -------------------------------
function updateSelectedDay() {
const days = ["Today", "Tomorrow", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7"];
selectedDayEl.textContent = days[currentDayIndex] || `Day ${currentDayIndex + 1}`;
}
// -------------------------------
// MAIN LOAD WEATHER FUNCTION
// -------------------------------
async function loadWeather(location) {
try {
let lat, lon, name, country;
if (typeof location === "string") {
// City name
const geoData = await geocodeCity(location);
lat = geoData.latitude;
lon = geoData.longitude;
name = geoData.name;
country = geoData.country;
} else {
// Coordinates
lat = location.lat;
lon = location.lon;
name = location.name || "Current Location";
country = location.country || "";
}
currentLocation = { name, country };
const data = await fetchWeather(lat, lon);
forecastData = data;
updateCurrent(data);
updateForecast(data);
updateSelectedDay();
} catch (err) {
console.error(err);
alert("Error loading weather data: " + err.message);
}
}
// -------------------------------
// EVENT LISTENERS
// -------------------------------
searchBtn.addEventListener("click", () => {
const city = searchInput.value.trim();
if (city) loadWeather(city);
});
searchInput.addEventListener("keyup", (e) => {
if (e.key === "Enter") searchBtn.click();
});
unitsSelect.addEventListener("change", () => {
units = unitsSelect.value;
if (forecastData) {
updateCurrent(forecastData);
updateForecast(forecastData);
updateHourly(forecastData, currentDayIndex);
}
});
geoBtn.addEventListener("click", () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(pos => {
loadWeather({
lat: pos.coords.latitude,
lon: pos.coords.longitude,
name: "Current Location"
});
}, () => alert("Location access denied"));
} else {
alert("Geolocation is not supported by this browser");
}
});
prevDayBtn.addEventListener("click", () => {
if (currentDayIndex > 0) {
currentDayIndex--;
updateSelectedDay();
if (forecastData) updateHourly(forecastData, currentDayIndex);
}
});
nextDayBtn.addEventListener("click", () => {
if (currentDayIndex < 6) {
currentDayIndex++;
updateSelectedDay();
if (forecastData) updateHourly(forecastData, currentDayIndex);
}
});
// LOAD DEFAULT (Current Location)
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(pos => {
loadWeather({
lat: pos.coords.latitude,
lon: pos.coords.longitude,
name: "Current Location"
});
}, () => {
// Fallback to New York if geolocation fails
loadWeather("New York");
});
} else {
// Fallback to New York if geolocation not supported
loadWeather("New York");
}