Spaces:
Sleeping
Sleeping
Update static/js/script.js
Browse files- static/js/script.js +200 -95
static/js/script.js
CHANGED
@@ -1,96 +1,201 @@
|
|
1 |
-
document.getElementById('search-button').addEventListener('click', function() {
|
2 |
-
let city = document.getElementById('city-input').value;
|
3 |
-
fetchWeatherData(city);
|
4 |
-
|
5 |
-
|
6 |
-
document.getElementById('
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
}
|
17 |
-
|
18 |
-
function
|
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 |
});
|
|
|
1 |
+
document.getElementById('search-button').addEventListener('click', function() {
|
2 |
+
let city = document.getElementById('city-input').value;
|
3 |
+
fetchWeatherData(city);
|
4 |
+
addToCityHistory(city);
|
5 |
+
});
|
6 |
+
document.getElementById('clear-history-button').addEventListener('click', function() {
|
7 |
+
clearCityHistory();
|
8 |
+
});
|
9 |
+
let cityHistory = [];
|
10 |
+
|
11 |
+
|
12 |
+
function clearCityHistory() {
|
13 |
+
cityHistory = [];
|
14 |
+
displayCityHistory();
|
15 |
+
toggleClearHistoryButton();
|
16 |
+
}
|
17 |
+
|
18 |
+
function toggleClearHistoryButton() {
|
19 |
+
let clearHistoryButton = document.getElementById('clear-history-button');
|
20 |
+
if (cityHistory.length > 0) {
|
21 |
+
clearHistoryButton.style.display = 'block';
|
22 |
+
} else {
|
23 |
+
clearHistoryButton.style.display = 'none';
|
24 |
+
}
|
25 |
+
}
|
26 |
+
|
27 |
+
function addToCityHistory(city) {
|
28 |
+
if (!cityHistory.includes(city)) {
|
29 |
+
cityHistory.push(city);
|
30 |
+
}
|
31 |
+
displayCityHistory();
|
32 |
+
toggleClearHistoryButton();
|
33 |
+
}
|
34 |
+
|
35 |
+
function displayCityHistory() {
|
36 |
+
let historyContainer = document.getElementById('city-history');
|
37 |
+
historyContainer.innerHTML = '<strong>City History:</strong> ';
|
38 |
+
|
39 |
+
cityHistory.forEach((city, index) => {
|
40 |
+
let link = document.createElement('span');
|
41 |
+
link.classList.add('city-history-item');
|
42 |
+
link.textContent = city;
|
43 |
+
link.addEventListener('click', function() {
|
44 |
+
// console.log('City clicked:', city);
|
45 |
+
fetchWeatherData(city);
|
46 |
+
});
|
47 |
+
|
48 |
+
if (index < cityHistory.length - 1) {
|
49 |
+
historyContainer.appendChild(link);
|
50 |
+
historyContainer.innerHTML += ', ';
|
51 |
+
} else {
|
52 |
+
historyContainer.appendChild(link);
|
53 |
+
}
|
54 |
+
});
|
55 |
+
}
|
56 |
+
|
57 |
+
|
58 |
+
document.getElementById('location-button').addEventListener('click', function() {
|
59 |
+
if (navigator.geolocation) {
|
60 |
+
navigator.geolocation.getCurrentPosition(position => {
|
61 |
+
let lat = position.coords.latitude;
|
62 |
+
let lon = position.coords.longitude;
|
63 |
+
fetchWeatherDataByLocation(lat, lon);
|
64 |
+
addToCityHistory("Current Location");
|
65 |
+
});
|
66 |
+
} else {
|
67 |
+
alert('Geolocation is not supported by this browser.');
|
68 |
+
}
|
69 |
+
});
|
70 |
+
|
71 |
+
function fetchWeatherData(city) {
|
72 |
+
fetch(`/weather?city=${city}`)
|
73 |
+
.then(response => response.json())
|
74 |
+
.then(data => {
|
75 |
+
if (data.error) {
|
76 |
+
alert(data.error);
|
77 |
+
} else {
|
78 |
+
displayWeatherData(data);
|
79 |
+
}
|
80 |
+
})
|
81 |
+
.catch(error => console.error('Error:', error));
|
82 |
+
}
|
83 |
+
|
84 |
+
function fetchWeatherDataByLocation(lat, lon) {
|
85 |
+
fetch(`/weatherlocation?lat=${lat}&lon=${lon}`)
|
86 |
+
.then(response => response.json())
|
87 |
+
.then(data => {
|
88 |
+
if (data.error) {
|
89 |
+
alert(data.error);
|
90 |
+
} else {
|
91 |
+
displayWeatherData(data);
|
92 |
+
}
|
93 |
+
})
|
94 |
+
.catch(error => console.error('Error:', error));
|
95 |
+
}
|
96 |
+
|
97 |
+
function loadMoreForecast() {
|
98 |
+
fetch(`/load_more_forecast`)
|
99 |
+
.then(response => response.json())
|
100 |
+
.then(data => {
|
101 |
+
if (data.error) {
|
102 |
+
alert(data.error);
|
103 |
+
} else {
|
104 |
+
renderForecast(data, true); // Append new forecast data
|
105 |
+
}
|
106 |
+
|
107 |
+
})
|
108 |
+
.catch(error => console.error('Error loading more forecast:', error));
|
109 |
+
}
|
110 |
+
|
111 |
+
function displayWeatherData(data) {
|
112 |
+
let currentWeather = document.getElementById('current-weather');
|
113 |
+
currentWeather.innerHTML = `
|
114 |
+
<div class="weather-details">
|
115 |
+
<h2>${data.location.name} (${data.current.last_updated})</h2>
|
116 |
+
<p>Temperature: ${data.current.temp_c}°C</p>
|
117 |
+
<p>Wind: ${data.current.wind_kph} KPH</p>
|
118 |
+
<p>Humidity: ${data.current.humidity}%</p>
|
119 |
+
</div>
|
120 |
+
<div class="weather-icon">
|
121 |
+
<img src="${data.current.condition.icon}" alt="${data.current.condition.text}">
|
122 |
+
<p>${data.current.condition.text}</p>
|
123 |
+
</div>
|
124 |
+
`;
|
125 |
+
|
126 |
+
let forecast = document.getElementById('forecast');
|
127 |
+
forecast.innerHTML = '';
|
128 |
+
|
129 |
+
let lastFourDays = data.forecast.forecastday.slice(1, 5);
|
130 |
+
lastFourDays.forEach(day => {
|
131 |
+
forecast.innerHTML += `
|
132 |
+
<div class="forecast-day">
|
133 |
+
<h3>${day.date}</h3>
|
134 |
+
<img src="${day.day.condition.icon}" alt="${day.day.condition.text}">
|
135 |
+
<p>Temp: ${day.day.avgtemp_c}°C</p>
|
136 |
+
<p>Wind: ${day.day.maxwind_kph} KPH</p>
|
137 |
+
<p>Humidity: ${day.day.avghumidity}%</p>
|
138 |
+
</div>
|
139 |
+
`;
|
140 |
+
});
|
141 |
+
|
142 |
+
if (forecast.innerHTML != '') {
|
143 |
+
document.getElementById('load-more-btn').style.display = 'block';
|
144 |
+
}
|
145 |
+
}
|
146 |
+
|
147 |
+
|
148 |
+
function renderForecast(data, append = false) {
|
149 |
+
const forecastContainer = document.getElementById('forecast');
|
150 |
+
if (!append) {
|
151 |
+
forecastContainer.innerHTML = '';
|
152 |
+
}
|
153 |
+
let forecast = data.forecast.forecastday.slice(-4);
|
154 |
+
|
155 |
+
let rows = Math.ceil(forecast.length / 4);
|
156 |
+
for (let i = 0; i < rows; i++) {
|
157 |
+
const row = document.createElement('div');
|
158 |
+
row.className = 'forecast-row';
|
159 |
+
row.style.display = 'flex';
|
160 |
+
row.style.justifyContent = 'space-between';
|
161 |
+
for (let j = 0; j < 4; j++) {
|
162 |
+
let dayIndex = i * 4 + j;
|
163 |
+
if (dayIndex < forecast.length) {
|
164 |
+
const day = forecast[dayIndex];
|
165 |
+
const forecastElement = document.createElement('div');
|
166 |
+
forecastElement.className = 'forecast-day';
|
167 |
+
forecastElement.innerHTML = `
|
168 |
+
<h3>${day.date}</h3>
|
169 |
+
<img src="${day.day.condition.icon}" alt="${day.day.condition.text}">
|
170 |
+
<p>Temp: ${day.day.avgtemp_c}°C</p>
|
171 |
+
<p>Wind: ${day.day.maxwind_kph} KPH</p>
|
172 |
+
<p>Humidity: ${day.day.avghumidity}%</p>
|
173 |
+
`;
|
174 |
+
row.appendChild(forecastElement);
|
175 |
+
}
|
176 |
+
}
|
177 |
+
forecastContainer.appendChild(row);
|
178 |
+
}
|
179 |
+
}
|
180 |
+
|
181 |
+
|
182 |
+
document.getElementById('subscribe-form').addEventListener('submit', function(event) {
|
183 |
+
event.preventDefault();
|
184 |
+
let email = document.getElementById('email-input').value;
|
185 |
+
fetch('/subscribe', {
|
186 |
+
method: 'POST',
|
187 |
+
headers: {
|
188 |
+
'Content-Type': 'application/json',
|
189 |
+
},
|
190 |
+
body: JSON.stringify({ email: email })
|
191 |
+
})
|
192 |
+
.then(response => response.json())
|
193 |
+
.then(data => {
|
194 |
+
if (data.error) {
|
195 |
+
alert(data.error);
|
196 |
+
} else {
|
197 |
+
alert(data.message);
|
198 |
+
}
|
199 |
+
})
|
200 |
+
.catch(error => console.error('Error:', error));
|
201 |
});
|