-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
308 lines (280 loc) · 10.4 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
$("document").ready(function () {
/* ************************* Global Variables ************************* */
var city;
const apiKey = "aaf6c28ed5b2ad844bd61b20d43ffe8c";
let queryURL;
let citiesSearchedObject = {};
let citiesSearchedObjectsArray = [];
let ajaxFlag = 0;
/* ************************* Function Declarations ************************* */
/* --------------- Global --------------- */
function init() {
loadSearchedCities();
}
function runAjax(url, thenFunction) {
$.ajax({
url: url,
method: "GET",
}).then(function (response) {
if (thenFunction) {
thenFunction(response);
} else {
return response;
}
});
}
/* --------------- Search Cities --------------- */
function loadSearchedCities() {
// Empty the searched cities list
$(".listSearchedCities").empty();
// Load searched cities from local storage
if (localStorage.getItem("searchedCitiesObjects")) {
citiesSearchedObjectsArray = localStorage.getItem(
"searchedCitiesObjects"
);
citiesSearchedObjectsArray = JSON.parse(citiesSearchedObjectsArray);
// Display searched cities list
if (citiesSearchedObjectsArray) {
citiesSearchedObjectsArray.forEach(function (object) {
var newSearchedCity = $("<a>");
newSearchedCity.attr("href", "#");
newSearchedCity.attr("searchedCity", object.city);
newSearchedCity.attr(
"class",
"list-group-item list-group-item-action list-group-item-light listItemSearchedCity"
);
newSearchedCity.text(object.city);
$(".listSearchedCities").append(newSearchedCity);
});
displayCityInCurrentWeather(
citiesSearchedObjectsArray[0].city,
citiesSearchedObjectsArray[0].data
);
}
} else {
citiesSearchedObjectsArray = [];
}
$(".listSearchedCities").hide();
$(".listSearchedCities").fadeIn(1000);
}
function displayCityInSearchedCities(res) {
// Empty the city search input
$("#cityInput").val("");
// Add city to the searched cities list array and data
citiesSearchedObject = {
city: city,
data: res,
};
citiesSearchedObjectsArray.unshift(citiesSearchedObject);
// Save searched cities array and data to local storage
var citiesSearchedObjectsArrayString = JSON.stringify(
citiesSearchedObjectsArray
);
localStorage.setItem(
"searchedCitiesObjects",
citiesSearchedObjectsArrayString
);
// Append new city element to the searched list
var newSearchedCity = $("<a>");
newSearchedCity.attr("href", "#");
newSearchedCity.attr("searchedCity", city);
newSearchedCity.attr(
"class",
"list-group-item list-group-item-action list-group-item-light listItemSearchedCity"
);
newSearchedCity.text(city);
$(".listSearchedCities").prepend(newSearchedCity);
$(".listSearchedCities").hide();
$(".listSearchedCities").fadeIn(1000);
displayCityInCurrentWeather(
citiesSearchedObjectsArray[0].city,
citiesSearchedObjectsArray[0].data
);
}
/* --------------- City Current --------------- */
function displayCityInCurrentWeather(city, cityData) {
// Variable declarations
date = new Date(cityData.current.dt * 1000);
var utc_date = date.toUTCString();
date = moment.utc(utc_date);
date = date.format("MMMM Do YYYY");
var icon = `https://openweathermap.org/img/wn/${cityData.current.weather[0].icon}@2x.png`;
temp = (cityData.current.temp - 273.15).toFixed(1);
var uv = cityData.current.uvi;
// Display in the DOM
$(".currentData .city").text(city);
$(".currentData .date").text(date);
$(".currentData .icon").attr("src", icon);
$(".currentData .icon").show();
$(".currentData .weather").text(
"Weather: " + cityData.current.weather[0].description
);
$(".currentData .temp").text("Temp: " + temp + "ºC");
$(".currentData .humid").text(
"Humidity: " + cityData.current.humidity + "%"
);
$(".currentData .wind").text(
"Wind speed: " + cityData.current.wind_speed + " mts/s"
);
// UV Index traffic light
if (uv >= 0 && uv <= 2) {
$(".currentData .uv").text("UV Index: ", uv);
$(".currentData .flag").text(" Low ");
$(".currentData .flag").css("background", "#d4edda");
$(".currentData .flag").css("color", "gray");
} else if (uv >= 3 && uv <= 5) {
$(".currentData .uv").text("UV Index: " + uv);
$(".currentData .flag").text(" Moderate ");
$(".currentData .flag").css("background", "#fff3cd");
$(".currentData .flag").css("color", "gray");
} else if (uv >= 6 && uv <= 7) {
$(".currentData .uv").text("UV Index: " + uv);
$(".currentData .flag").text(" High ");
$(".currentData .flag").css("background", "#fff3cd");
$(".currentData .flag").css("color", "gray");
} else if (uv >= 8 && uv <= 10) {
$(".currentData .uv").text("UV Index: " + uv);
$(".currentData .flag").text(" Very High ");
$(".currentData .flag").css("background", "#f8d7da");
$(".currentData .flag").css("color", "gray");
} else if (uv >= 11) {
$(".currentData .uv").text("UV Index: " + uv);
$(".currentData .flag").text(" Extreme ");
$(".currentData .flag").css("background", "#721c24");
$(".currentData .flag").css("color", "white");
}
$(".currentData").hide();
$(".currentData").fadeIn(1000);
windyMap(city, cityData);
displayForcastDay(cityData);
}
/* --------------- City Forcast --------------- */
function displayForcastDay(cityData) {
cityData = cityData.daily;
cityData.forEach(function (dayData, index) {
if (index <= 4) {
// Handling The Data
date = new Date(dayData.dt * 1000);
var utc_date = date.toUTCString();
date = moment.utc(utc_date);
date = date.format("MMMM Do YYYY");
var icon = `https://openweathermap.org/img/wn/${dayData.weather[0].icon}@2x.png`;
var weather = dayData.weather[0].description;
var temp = (dayData.temp.day - 273.15).toFixed(1) + "ºC";
var humid = dayData.humidity + "%";
var windSpeed = dayData.wind_speed + " mts/s";
var uv = dayData.uvi;
// Display in the DOM
$(".forecast .date").eq(index).text(date);
$(".forecast .icon").eq(index).attr("src", icon);
$(".forecast .icon").eq(index).show();
$(".forecast .weather").eq(index).text(weather);
$(".forecast .temp")
.eq(index)
.text("T: " + temp);
$(".forecast .humid")
.eq(index)
.text("H: " + humid);
$(".forecast .wind")
.eq(index)
.text("WS: " + windSpeed);
// UV Index traffic light
if (uv >= 0 && uv <= 2) {
$(".forecast .uv").eq(index).text("UV: ", uv);
$(".forecast .flag").eq(index).text(" Low ");
$(".forecast .flag").eq(index).css("background", "#d4edda");
$(".forecast .flag").eq(index).css("color", "gray");
} else if (uv >= 3 && uv <= 5) {
$(".forecast .uv")
.eq(index)
.text("UV: " + uv);
$(".forecast .flag").eq(index).text(" Moderate ");
$(".forecast .flag").eq(index).css("background", "#fff3cd");
$(".forecast .flag").eq(index).css("color", "gray");
} else if (uv >= 6 && uv <= 7) {
$(".forecast .uv")
.eq(index)
.text("UV: " + uv);
$(".forecast .flag").eq(index).text(" High ");
$(".forecast .flag").eq(index).css("background", "#fff3cd");
$(".forecast .flag").eq(index).css("color", "gray");
} else if (uv >= 8 && uv <= 10) {
$(".forecast .uv")
.eq(index)
.text("UV: " + uv);
$(".forecast .flag").eq(index).text(" Very High ");
$(".forecast .flag").eq(index).css("background", "#f8d7da");
$(".forecast .flag").eq(index).css("color", "gray");
} else if (uv >= 11) {
$(".forecast .uv")
.eq(index)
.text("UV: " + uv);
$(".forecast .flag").eq(index).text(" Extreme ");
$(".forecast .flag").eq(index).css("background", "#721c24");
$(".forecast .flag").eq(index).css("color", "white");
}
}
});
$(".forecast").hide();
$(".forecast").fadeIn(1000);
}
function getCurrentAndForcastData(res) {
if (ajaxFlag === 0) {
ajaxFlag = 1;
// Current and forecasts weather data
queryURL = `https://api.openweathermap.org/data/2.5/onecall?lat=${res.coord.lat}&lon=${res.coord.lon}&exclude=hourly&appid=${apiKey}`;
// 2nd Ajax request - to get the full city data
runAjax(queryURL, getCurrentAndForcastData);
} else {
displayCityInSearchedCities(res);
}
}
/* --------------- Windy Map API --------------- */
let options = {};
function windyMap(city, cityData) {
// Empty the Windy area
$("#windy").empty();
$("#windy").removeAttr("class");
options = {
// Required: API key
key: "GfrEeWltIBvyt5Y5Lz4DIxw2Q0cM9hDm",
// Put additional console output
verbose: true,
// Optional: Initial state of the map
city: city,
lat: cityData.lat,
lon: cityData.lon,
zoom: 11,
};
windyInit(options, windyCallBack);
}
function windyCallBack(windyAPI) {
// windyAPI is ready, and contain 'map', 'store',
// 'picker' and other usefull stuff
const { map } = windyAPI;
// .map is instance of Leaflet map
L.popup()
.setLatLng([options.lat, options.lon])
.setContent(options.city)
.openOn(map);
}
/* ************************* Event Listeners ************************* */
init();
// Click on the 'Seach Icon' button
$("#searchBtn").on("click", function (event) {
event.preventDefault();
city = $("#cityInput").val();
ajaxFlag = 0;
// 1st Ajax request - to get latitude and longitude
queryURL = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;
runAjax(queryURL, getCurrentAndForcastData);
});
// Click on any of the already searched cities names
$(".listSearchedCities").on("click", function (event) {
citiesSearchedObjectsArray.forEach(function (object) {
if (object.city === $(event.target).attr("searchedcity")) {
displayCityInCurrentWeather(object.city, object.data);
}
});
});
});