-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
395 lines (307 loc) · 13.8 KB
/
app.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
// GET ALL REQUIRED ELEMENTS FROM HTML
const getLocationElement = document.querySelector(".get-location");
const searchBarElement = document.querySelector(".search-bar");
const searchButtonElement = document.querySelector(".search-button");
const weatherAppContainerElement = document.querySelector(".weather-app-container");
const notificationElement = document.querySelector(".notification");
const temperatureValueElement = document.querySelector('.temperature-value');
const weatherIconElement = document.querySelector('.weather-icon');
const weatherDescriptionElement = document.querySelector('.weather-description');
const locationInfoElement = document.querySelector('.location-info');
const weatherFeelsLikeElement = document.querySelector(".weather-feels-like");
const humidityElement = document.querySelector(".humidity");
const precipitationElement = document.querySelector(".precipitation");
const windDirectionElement = document.querySelector(".wind-direction");
const windSpeedElement = document.querySelector(".wind-speed");
const dewPointElement = document.querySelector(".dew-point");
const visibilityElement = document.querySelector(".visibility");
const sunriseElement = document.querySelector(".sunrise");
const sunsetElement = document.querySelector(".sunset");
const chronologicalElements = document.querySelectorAll(".daily-data");
const switchUnitElement = document.querySelector(".switch-unit");
// VARIABLE TO STORE CUURENT TEMPERATURE UNIT TO BASE SWITCHING UNIT OFF
let unitInUse = "celcius";
// VARIABLE TO DECIDE WHETHER TO GET WEATHER OF USER LOCATION
let searchSuggestionIsClicked = false;
let searchBarIsFocused = false;
// APP CONSTANTS
const KELVIN = 273;
const key = "2f5cb98dbd931c10023726a1875dabf7";
// WEATHER INFO OBJECT WITH VALUES IN CELCIUS
const weather = {
temperature: {
unit: "celcius"
},
daily: {
0: [],
1: [],
2: [],
3: [],
4: [],
5: [],
6: [],
7: []
}
}
// WEATHER INFO OBJECT WITH VALUES IN FAHRENHEIT
const weatherInFahrenheit = JSON.parse(JSON.stringify(weather));
// EVENT LISTENERS TO GET WEATHER INFO
searchButtonElement.addEventListener("click", () => {
getWeatherInfoBySearchedLocation(searchBarElement.value);
});
searchBarElement.addEventListener("keydown", (event) => {
let key = event.keyCode;
if (key == 13) {
getWeatherInfoBySearchedLocation(searchBarElement.value);
}
});
getLocationElement.addEventListener("click", () => {
getLocation();
});
// FUNCTION TO GET GEOLOCATION
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getPosition, showError);
} else {
weatherAppContainerElement.style.gridTemplateRows = "auto 250px 95px";
notificationElement.innerHTML = `Browser doesn't support Geolocation`;
notificationElement.style.display = "block";
}
}
function getPosition(position) {
let lat = position.coords.latitude;
let lon = position.coords.longitude;
getWeatherInfo(lat, lon);
}
// DISPLAY THE NOTIFICATION ELEMENT AND SHOW THE ERROR
function showError(error) {
weatherAppContainerElement.style.gridTemplateRows = "auto 250px 95px";
notificationElement.innerHTML = `${error.message}`;
notificationElement.style.display = "block";
}
// FUNCTION TO CHANGE HTML ELEMENT VALUES AND DISPLAY WEATHER INFO
function displayWeather() {
temperatureValueElement.innerHTML = `<p><span class="temperature-number">${weather.temperature.value}</span><span class="initial-unit">°C</span></p>`
weatherIconElement.innerHTML = `<img src="icons/${weather.iconId}.png" alt="${weather.description}"></img>`
weatherDescriptionElement.innerHTML = ` <p>${weather.description}</p>`;
locationInfoElement.innerHTML = `<p>${weather.locationInfo}</p>`;
weatherFeelsLikeElement.innerHTML = ` <p>Feels like <span>${weather.temperature.feels_like}°</span></p>`;
humidityElement.innerHTML = `<p>Humidity <span>${weather.humidity}%</span></p>`;
precipitationElement.innerHTML = ` <p>Precipitation <span>${Math.round(weather.precipitation*100)}%</span></p>`;
windSpeedElement.innerHTML = `<p>Wind <span>${weather.wind_speed} kmph</span></p>`
dewPointElement.innerHTML = `Dew point <span>${weather.dew_point}°</span>`;
visibilityElement.innerHTML = `Visibility <span>${weather.visibility} km</span>`;
sunriseElement.innerHTML = `Sunrise <span>${weather.sunrise}</span></div>`;
sunsetElement.innerHTML = `<div class="sunset">Sunset <span>${weather.sunset}</span></div>`;
for (let i = 0; i <= 7; i++) {
chronologicalElements[i].children[0].innerHTML = `${weather.daily[i].name}`;
chronologicalElements[i].children[1].innerHTML = `<img src="icons/${weather.daily[i].icon}.png" alt="${weather.daily[i].description}">`;
chronologicalElements[i].children[2].innerHTML = `${weather.daily[i].max}° | ${weather.daily[i].min}°`;
}
// TO NOT SHOW THE ERROR IF WEATHER IS DISPLAYED SUCCESSFULLY
weatherAppContainerElement.style.gridTemplateRows = "250px 95px";
notificationElement.style.display = "none";
}
// GET WEATHER INFO FROM API
function getWeatherInfo(lat, lon) {
const api = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&appid=${key}`;
const callWeatherInfoApi = async() => {
const request = await fetch(api);
const data = await request.json();
return data;
};
callWeatherInfoApi().then((data) => {
weather.temperature.value = Math.floor(data.current.temp - KELVIN);
weather.description = data.current.weather[0].description;
weather.iconId = data.current.weather[0].icon;
weather.temperature.feels_like = Math.floor(data.current.feels_like - KELVIN);
weather.humidity = data.current.humidity;
weather.precipitation = data.hourly[0].pop;
const windDirection = data.current.wind_deg;
windDirectionElement.style.transform = `rotateZ(${windDirection}deg)`;
weather.wind_speed = data.current.wind_speed;
weather.dew_point = Math.floor(data.current.dew_point - KELVIN);
weather.visibility = data.current.visibility / 1000;
const sunriseJsTime = new Date(data.current.sunrise * 1000);
const sunriseHours = sunriseJsTime.getHours();
const sunriseMins = sunriseJsTime.getMinutes();
const sunsetJsTime = new Date(data.current.sunset * 1000);
const sunsetHours = sunsetJsTime.getHours() - 12;
const sunsetMins = sunsetJsTime.getMinutes();
const sunriseTime = `${sunriseHours}:${sunriseMins}AM`;
const sunsetTime = `${sunsetHours}:${sunsetMins}PM`;
weather.sunrise = sunriseTime;
weather.sunset = sunsetTime;
const dayNames = [];
for (let i = 0; i <= 7; i++) {
let day;
day = new Date(data.daily[i].dt * 1000);
dayNames.push(getDayName(day.getDay()));
weather.daily[i].name = dayNames[i];
weather.daily[i].icon = data.daily[i].weather[0].icon;
weather.daily[i].min = Math.floor(data.daily[i].temp.min - KELVIN);
weather.daily[i].max = Math.floor(data.daily[i].temp.max - KELVIN);
weather.daily[i].description = data.daily[i].weather[0].description;
weatherInFahrenheit.daily[i].min = Math.floor(weather.daily[i].min * 1.8 + 32);
weatherInFahrenheit.daily[i].max = Math.floor(weather.daily[i].max * 1.8 + 32);
}
weatherInFahrenheit.temperature.value = Math.floor(weather.temperature.value * 1.8 + 32);
weatherInFahrenheit.temperature.feels_like = Math.floor(weather.temperature.feels_like * 1.8 + 32);
weatherInFahrenheit.dew_point = Math.floor(weather.dew_point * 1.8 + 32);
getLocationInfo(lat, lon);
});
}
// GET LOCATION INFO FROM API
function getLocationInfo(lat, lon) {
const api = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${key}`;
const callLocationInfoApi = async() => {
const request = await fetch(api);
const data = await request.json();
return data;
};
callLocationInfoApi().then((data) => weather.locationInfo = `${data.name}, ${data.sys.country}`)
.then(() => {
displayWeather();
});
}
// HELPER FUNCTIONS TO BE USED
function displayLocationSearchSuggestions(address) {
if (address.hasOwnProperty("city") == true && address.hasOwnProperty("state") == false) {
return `<p>${address.city}, ${address.country}</p>`;
} else if (address.hasOwnProperty("city")) {
return `<p>${address.city}, ${address.country}</p>`;
} else if (address.hasOwnProperty("state")) {
return `<p>${address.state}, ${address.country}</p>`;
} else {
return `<p>${address.country}</p>`;
}
}
function removeNodes(nodeArray, numberOfNodesToRemove) {
if (nodeArray.length > 0) {
for (let i = 0; i < numberOfNodesToRemove; i++) { nodeArray[i].remove(); }
}
}
function makeLocationSearchSuggestionsSelectable(searchSuggestions) {
searchBarElement.style.borderBottomLeftRadius = "0px";
searchBarElement.style.borderBottomRightRadius = "0px";
for (let i = 0; i < searchSuggestions.length; i++) {
searchSuggestions[i].addEventListener("mousedown", () => {
searchSuggestionIsClicked = true;
searchBarElement.value = searchSuggestions[i].innerText;
getWeatherInfoBySearchedLocation(searchBarElement.value);
removeNodes(searchSuggestions, searchSuggestions.length);
});
}
}
// SHOW LOCATION SUGGESTIONS WHEN SEARCHING USING API
function addLocationSearchSuggestions() {
searchBarIsFocused = true;
let searchedLocation = searchBarElement.value;
if (searchedLocation == "") return;
const searchBarContainerElement = document.querySelector(".search-bar-container");
const apiKey = "ECzcVm1vzmQ07xEB_0IwcVe-AlUPYOl9QxIz1NVSTG8";
const api = `https://autocomplete.geocoder.ls.hereapi.com/6.2/suggest.json?apiKey=${apiKey}&query=${searchedLocation}&maxresults=4`;
const getLocationSuggestions = async() => {
const request = await fetch(api);
const data = await request.json();
return data;
};
getLocationSuggestions().then((data) => {
if (!searchBarIsFocused) return
let suggestionElements = document.querySelectorAll(".suggestion");
removeNodes(suggestionElements, suggestionElements.length);
for (let i = 0; i < data.suggestions.length; i++) {
const suggestion = document.createElement("div");
suggestion.className = "suggestion";
searchBarContainerElement.append(suggestion);
}
suggestionElements = document.querySelectorAll(".suggestion");
makeLocationSearchSuggestionsSelectable(suggestionElements);
for (let i = 0; i < suggestionElements.length; i++) {
suggestionElements[i].innerHTML = displayLocationSearchSuggestions(data.suggestions[i].address);
}
});
}
// EVENT LISTNERS FOR SHOWING LOCATION SUGGESTIONS WHEN SEARCHING
searchBarElement.addEventListener("input", addLocationSearchSuggestions);
searchBarElement.addEventListener("focus", addLocationSearchSuggestions);
// REMOVE LOCATION SUGGESTIONS WHEN FOCUS IS LOST FROM SEARCH BAR
function removeLocationSearchSuggestions() {
searchBarIsFocused = false;
let suggestionElements = document.querySelectorAll(".suggestion");
if (!suggestionElements) return
removeNodes(suggestionElements, suggestionElements.length);
if (window.innerWidth > 758) searchBarElement.style.borderRadius = "8px";
}
searchBarElement.addEventListener("blur", removeLocationSearchSuggestions);
// GET WEATHER INFO FROM THE API BY CITY NAME SEARCHED FOR
function getWeatherInfoBySearchedLocation(searchedLocation) {
const api = `https://api.openweathermap.org/data/2.5/weather?q=${searchedLocation}&appid=${key}`;
const getCoordinatesOfSearchedLocation = async() => {
const request = await fetch(api);
const data = await request.json();
return data;
}
getCoordinatesOfSearchedLocation()
.then((data) => {
if (data.hasOwnProperty("message")) {
weatherAppContainerElement.style.gridTemplateRows = "auto 250px 95px";
notificationElement.innerHTML = `${data.message}`;
notificationElement.style.display = "block";
return;
}
const lat = data.coord.lat;
const lon = data.coord.lon;
const coords = [lat, lon]
return coords;
})
.then((coords) => {
if (coords) getWeatherInfo(coords[0], coords[1]);
});
}
// FUNCTION TO FIND DAY NAME USING DAY NUMBER
function getDayName(dayNo) {
switch (dayNo) {
case 0:
return "SUN"
break;
case 1:
return "MON"
break;
case 2:
return "TUE"
break;
case 3:
return "WED"
break;
case 4:
return "THURS"
break;
case 5:
return "FRI"
break;
case 6:
return "SAT"
}
}
//FUNCTION TO CONVERT THE TEMPERATURE UNIT
function switchUnit() {
if (weather.temperature.value == undefined) return;
if (unitInUse === "celcius") {
unitInUse = "fahrenheit";
displayWeather(weatherInFahrenheit);
} else {
unitInUse = "celcius";
displayWeather(weather);
}
}
// EVENT LISTENERS FOR SWITCHING TEMPERATURE UNIT
switchUnitElement.addEventListener("click", () => { switchUnit(); });
const temperatureNumberElement = document.querySelector(".temperature-number");
if (screen.width <= 758) {
temperatureNumberElement.addEventListener("click", () => {
switchUnit();
});
}
// RUN GEOLOCATION FINDER WHEN APPLICATION IS RUN
if (!searchSuggestionIsClicked) getLocation();