-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
492 lines (441 loc) · 17.9 KB
/
script.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
var searchedLocations = JSON.parse(localStorage.getItem("searchedLocations")) || [];
/* Render search history */
const renderSearchHistory = () => {
for (let i = 0; i < searchedLocations.length; i++) {
$(`#search-dropdown-history`).append(`
<button class="dropdown-item btn" type="button" data-location="${searchedLocations[i]}">${searchedLocations[i]}</button>
`);
}
}
if (searchedLocations.length > 0) {
renderSearchHistory();
}
/* Store searched locations to local storage */
var storeLocations = () => {
localStorage.setItem("searchedLocations", JSON.stringify(searchedLocations));
}
/*######## HTML interactivity ########*/
/* Displays the weather panes */
var displayWeatherPane = (timeline) => {
$(`#${timeline}`).append(`
<h2 class="city-name" class="w-100">Location</h2>
<p id="${timeline}-date">Date</p>
<div id="${timeline}-temp-container w-100" class="row">
<div class="col-7 d-flex p-0">
<div class="w-100 pl-3">
<p id="${timeline}-min-max" class="align-self-center m-0">
<span id="${timeline}-min" class="fahr-display">min: 0</span>
<img src="assets/icons/dot.svg">
<span id="${timeline}-max" class="fahr-display">max: 0</span>
</p>
<div class="w-100">
<h3 id="${timeline}-temp" class="align-self-center m-0 fahr-display"
style="font-size:4rem;">0</h3>
</div>
<p id="${timeline}-feels" class="fahr-display" class="align-self-center m-0">Feels like 0
</p>
</div>
</div>
<div id="${timeline}-weather-container" class="col-5">
<div>
<img src="" id="${timeline}-icon" class="img-fluid">
<p id="${timeline}-main" class="m-0"></p>
<p id="${timeline}-description" class="m-0" style="font-size: x-small"></p>
</div>
</div>
<div id="${timeline}-info-container" class="pl-3 w-100">
<div class="row w-100">
<div class="col-7">
<div>Humidity:</div>
<div>Wind:</div>
<div>UV index:</div>
</div>
<div class="col-5">
<div id="${timeline}-humidity">0%</div>
<div id="${timeline}-wind-speed">0 mph N</div>
<div id="${timeline}-uv-index">0</div>
</div>
</div>
</div>
</div>
`)
}
/* create card elements for bootstrap accordion */
var displayCards = () => {
for (let i = 0; i < 5; i++) {
$(".accordion").append(`
<div class="card">
<div class="card-header" id="headingOne">
<h2 class="mb-0">
<div class="d-flex">
<button class="btn btn-block text-left pl-0" type="button"
data-toggle="collapse" data-target="#collapse-${i + 1}" aria-expanded="true"
aria-controls="collapse-${i + 1}"><span id="daily-date-${i + 1}">Date</span>
<p id="daily-description-${i + 1}" class="m-0 text-secondary" style="font-size:0.9rem;">Description</p>
</button>
<img id="daily-icon-${i + 1}" src=""></img>
<div class="ml-2 mb-2 mt-3 text-right">
<p id="daily-day-${i + 1}-temp" class="m-0 degree-display" style="font-size: 0.9rem;">100</p>
<p id="daily-night-${i + 1}-temp" class="m-0 degree-display text-secondary" style="font-size: 0.9rem;">90</p>
</div>
</div>
</h2>
</div>
<div id="collapse-${i + 1}" class="collapse" aria-labelledby="headine-${i + 1}" data-parent="#accordionExample">
<div class="card-body">
<div class="row w-100">
<div class="col-6">
<div class="text-secondary">Humidity:</div>
<div class="text-secondary">Wind:</div>
<div class="text-secondary">UV index:</div>
</div>
<div class="col-6">
<div id="daily-humidity-${i + 1}">0%</div>
<div id="daily-wind-${i + 1}">0 mph N</div>
<div id="daily-uvi-${i + 1}">0</div>
</div>
</div>
</div>
</div>
</div>
`)
}
}
/* Display weather panes for current day and tomorrow*/
displayWeatherPane("current");
displayWeatherPane("tomorrow");
/* display cards */
displayCards();
/* Toggle side bar display */
$('#sidebarCollapse').on('click', function () {
$('#sidebar').toggleClass('active');
$('.overlay').addClass('active');
});
$('#dismiss').on('click', function () {
$('#sidebar').addClass('active');
$('.overlay').removeClass('active');
});
/*######## Open Weather API ########*/
/* Current weather data */
const OW_API_KEY = "de36e36780976652d3e84a5502633fce";
const NUMBER_OF_DAYS = 5;
const searchCity = () => {
const input = $("#dropdownHistory").val().trim();
if (input.length === 0) {
return;
} else {
/* Ensures that city name will have same letter case */
const lowerCityName = input.toLowerCase();
cityName = titleCase(lowerCityName);
getCurrentWeather(cityName);
}
}
/* Search button to save search history*/
$("#search-button").on("click", (event) => {
event.preventDefault();
searchCity();
});
/* When user presses enter */
$("#search-form").on("submit", (event) => {
event.preventDefault();
searchCity();
})
/* Current weather data */
var getCurrentWeather = (cityName) => {
// Set Google Search url
$("#web-results").attr("href", `https://www.google.com/search?q=${cityName}+weather`);
const CURRENT_QUERY_URL = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${OW_API_KEY}`;
$.ajax({
url: CURRENT_QUERY_URL,
method: "GET"
})
.then(function (response) {
/* Checks if this city is already in the searchedLocations array */
if(!searchedLocations.includes(cityName)) {
/* If city does not exist, display in dropdown and store in local storage. */
$(`#search-dropdown-history`).append(`
<button class="dropdown-item btn" type="button" data-location="${cityName}">${cityName}</button>
`);
searchedLocations.push(cityName);
storeLocations();
}
/* Display location data */
var countryCode = response.sys.country;
$(".city-name").html(cityName + ', ' + countryCode);
/* Display Current Date */
var d = new Date(response.dt * 1000);
var currentDate = formatDate(d, "current");
$("#current-date").html(currentDate);
/* Display weather icon and description */
displayIcon(response, "current");
/* Get Weather data*/
const tempKelvin = parseFloat(response.main.temp);
const tempKelvinMax = parseFloat(response.main.temp_max);
const tempKelvinMin = parseFloat(response.main.temp_min);
const tempKelvinFeels = parseFloat(response.main.feels_like);
const humidity = response.main.humidity;
const windSpeed = response.wind.speed;
const windDirection = getWindDirection(parseInt(response.wind.deg));
displayWeather(tempKelvin, tempKelvinMax, tempKelvinMin, tempKelvinFeels, humidity, windSpeed, windDirection,"current");
// Get UV Index
getUVIndexData(response);
// Display Tomorrow's weather
getTomorrowWeather(response);
});
}
getCurrentWeather("Los Angeles"); // Default display
var getTomorrowWeather = (response) => {
const lon = response.coord.lon;
const lat = response.coord.lat;
const UV_QUERY_URL = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&appid=${OW_API_KEY}`;
$.ajax({
url: UV_QUERY_URL,
method: "GET"
}).then(function (response) {
// console.log(response);
const tomorrowsData = response.daily[1];
/* Display Date */
var d = new Date(tomorrowsData.dt * 1000);
var tomorrowDate = formatDate(d, "tomorrow");
$("#tomorrow-date").html(tomorrowDate);
/* Display tomorrow's weather icon and description */
displayIcon(tomorrowsData, "tomorrow");
/* Display tomorrow's temperatures */
const tempKelvin = parseFloat(tomorrowsData.temp.day);
const tempKelvinMax = parseFloat(tomorrowsData.temp.max);
const tempKelvinMin = parseFloat(tomorrowsData.temp.min);
const tempKelvinFeels = parseFloat(tomorrowsData.feels_like.day);
const humidity = tomorrowsData.humidity;
const windSpeed = tomorrowsData.wind_speed;
const windDirection = getWindDirection(parseInt(tomorrowsData.wind_deg));
displayWeather(tempKelvin, tempKelvinMax, tempKelvinMin, tempKelvinFeels, humidity, windSpeed, windDirection, "tomorrow");
/* Display UVI */
const uvIndex = parseInt(tomorrowsData.uvi);
displayUVI(uvIndex, "tomorrow");
/* Get data for the 5-day forecast */
getCardData(response);
})
}
/* Push data to cards*/
const getCardData = (response) => {
for (let i = 0; i < 5; i++) {
/* Get next Days */
dailyData = response.daily;
// Get date
const dt = dailyData[i + 1].dt;
const d = new Date(dt * 1000);
$(`#daily-date-${i + 1}`).html(formatDate(d, "tomorrow"))
// Get main, icon, description
const iconCode = dailyData[i].weather[0].icon;
$(`#daily-icon-${i + 1}`).attr("src", "https://openweathermap.org/img/wn/" + iconCode + ".png");
descriptionCapitalized = capitalize(dailyData[i].weather[0].description)
$(`#daily-description-${i + 1}`).html(descriptionCapitalized);
// Get day and night temps
const dayTempConversion = parseInt(kToF(parseFloat(dailyData[i].temp.day)));
const nightTempConversion = parseInt(kToF(parseFloat(dailyData[i].temp.night)));
$(`#daily-day-${i + 1}-temp`).html(dayTempConversion).attr("data-temp", dailyData[i].temp.day);
$(`#daily-night-${i + 1}-temp`).html(nightTempConversion).attr("data-temp", dailyData[i].temp.night);
// Get humidity, wind, wind direction, and uvi
$(`#daily-humidity-${i + 1}`).html(`${dailyData[i].humidity}%`);
const windDeg = dailyData[i].wind_deg;
$(`#daily-wind-${i + 1}`).html(`${dailyData[i + 1].wind_speed} mph ${getWindDirection(windDeg)}`)
var uvi = parseInt(dailyData[i].uvi);
$(`#daily-uvi-${i + 1}`).html(`${uvi} ${getUVIAlert(uvi)}`);
}
}
const getUVIndexData = (responseCurrent) => {
/* UV days data */
const lon = responseCurrent.coord.lon;
const lat = responseCurrent.coord.lat;
const UV_QUERY_URL = `https://api.openweathermap.org/data/2.5/uvi?appid=${OW_API_KEY}&lat=${lat}&lon=${lon}`;
$.ajax({
url: UV_QUERY_URL,
method: "GET"
}).then(function (response) {
const uvIndex = parseInt(response.value);
displayUVI(uvIndex, "current");
})
}
// TODO: make this return just alert
const displayUVI = (uvi, timeline) => {
/* Determine uv risk and display results*/
if (uvi < 3) {
$(`#${timeline}-uv-index`).html(`${uvi} ${getUVIAlert(uvi)}`);
} else if (uvi <= 5) {
$(`#${timeline}-uv-index`).html(`${uvi} ${getUVIAlert(uvi)}`);
} else if (uvi <= 7) {
$(`#${timeline}-uv-index`).html(`${uvi} ${getUVIAlert(uvi)}`);
} else if (uvi <= 10) {
$(`#${timeline}-uv-index`).html(`${uvi} ${getUVIAlert(uvi)}`);
} else if (uvi >= 11) {
$(`#${timeline}-uv-index`).html(`${uvi} ${getUVIAlert(uvi)}`);
}
}
const getUVIAlert = (uvi) => {
/* Determine uv risk and display results*/
if (uvi < 3) {
return `<span class="badge badge-success">Low</span>`
} else if (uvi <= 5) {
return `<span class="badge badge-warning">Moderate</span>`
} else if (uvi <= 7) {
return `<span class="badge badge-danger">High</span>`
} else if (uvi <= 10) {
return `<span class="badge badge-danger">Very High</span>`
} else if (uvi >= 11) {
return `<span class="badge badge-danger">Extreme</span>`
}
}
const displayIcon = (data, timeline) => {
/* Display weather icon and description */
const iconCode = data.weather[0].icon;
const main = data.weather[0].main;
const description = data.weather[0].description;
const iconURL = "https://openweathermap.org/img/wn/" + iconCode + ".png";
$(`#${timeline}-icon`).attr("src", iconURL);
$(`#${timeline}-main`).html(`<span class="font-weight-bold">${main}</span>`);
$(`#${timeline}-description`).html(capitalize(description));
}
const displayWeather = (tempKelvin, tempKelvinMax, tempKelvinMin, tempKelvinFeels, humidity, windSpeed, windDirection, timeline) => {
// Convert from kelvin to fahr
const tempFahr = kToF(tempKelvin);
const tempFahrMax = kToF(tempKelvinMax);
const tempFahrMin = kToF(tempKelvinMin);
const tempFahrFeels = kToF(tempKelvinFeels)
// Display temperatures and add data-temp attributes
$(`#${timeline}-temp`).html(tempFahr.toFixed(1)).attr("data-temp", tempKelvin);
$(`#${timeline}-min`).html(`min: ${tempFahrMin.toFixed(1)}`).attr("data-temp", tempKelvinMin);
$(`#${timeline}-max`).html(`max: ${tempFahrMax.toFixed(1)}`).attr("data-temp", tempKelvinMax);
$(`#${timeline}-feels`).html(`Feels like ${tempFahrFeels.toFixed(1)}`).attr("data-temp", tempKelvinFeels);
$(`#${timeline}-humidity`).html(`${humidity}%`);
$(`#${timeline}-wind-speed`).html(`${windSpeed} mph ${windDirection}`);
}
/* On click function to display */
$(document).on("click", ".dropdown-item", function(event) {
console.log($(this).data("location"));
const clickedLocation = $(this).data("location");
getCurrentWeather(clickedLocation);
})
// Test
// $("div[id*='-temp']").html("hello")
/*######## Additional function ########*/
// Format Date function
const formatDate = (d, timeline) => {
'use strict';
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
const days = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
];
const date = d.getDate();
const monthName = months[d.getMonth()];
const getHour = d.getHours();
const dayName = days[d.getDay()]
var hour;
var period;
if (getHour > 12) {
hour = getHour - 12;
period = "PM"
} else {
if (getHour === 0) {
hour = 12;
} else {
hour = getHour;
}
period = "AM"
}
var minutes = d.getMinutes();
if (minutes.toString().length == 1) {
minutes = "0" + minutes;
}
var formatted;
if (timeline === "current") {
formatted = `${monthName} ${date}, ${hour}:${minutes} ${period}`;
} else if (timeline === "tomorrow") {
formatted = `${dayName}, ${monthName} ${date}`;
}
// return formatted;
return formatted;
$(`#${timeline}-date`).html(formatted);
}
/* Convert kelvin to fahr
Formula: (0K − 273.15) × 9/5 + 32
*/
const kToF = (k) => {
return (k - 273.15) * 9 / 5 + 32;
}
/* Convert kelvin to celsius
Formula: 0K − 273.15 = -273.1°C
*/
const kToC = (k) => {
return k - 273.15;
}
/* Determine wind degree */
const getWindDirection = (degree) => {
// Round degree to the nearest tenth
var decimalDegree = degree / 10;
var roundDegree = Math.round(decimalDegree) * 10;
const directions = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW",
"W", "WNW", "NW", "NNW"];
if ((roundDegree >= 0 && roundDegree <= 10) || (roundDegree >= 350 && roundDegree <= 360)) {
return directions[0];
} else if (roundDegree >= 20 && roundDegree <= 30) {
return directions[1];
} else if (roundDegree >= 40 && roundDegree <= 50) {
return directions[2];
} else if (roundDegree >= 60 && roundDegree <= 70) {
return directions[3];
} else if (roundDegree >= 80 && roundDegree <= 100) {
return directions[4];
} else if (roundDegree >= 110 && roundDegree <= 120) {
return directions[5];
} else if (roundDegree >= 130 && roundDegree <= 140) {
return directions[6];
} else if (roundDegree >= 150 && roundDegree <= 160) {
return directions[7];
} else if (roundDegree >= 170 && roundDegree <= 190) {
return directions[8];
} else if (roundDegree >= 200 && roundDegree <= 210) {
return directions[9];
} else if (roundDegree >= 220 && roundDegree <= 230) {
return directions[10];
} else if (roundDegree >= 240 && roundDegree <= 250) {
return directions[11];
} else if (roundDegree >= 260 && roundDegree <= 280) {
return directions[12];
} else if (roundDegree >= 290 && roundDegree <= 300) {
return directions[13];
} else if (roundDegree >= 310 && roundDegree <= 320) {
return directions[14];
} else if (roundDegree >= 330 && roundDegree <= 340) {
return directions[15];
}
}
/* Capitalize string */
const capitalize = (s) => {
if (typeof s !== 'string') return ''
return s.charAt(0).toUpperCase() + s.slice(1)
}
/* Title Case function */
function titleCase(str) {
return str.toLowerCase().split(' ').map(function(word) {
return (word.charAt(0).toUpperCase() + word.slice(1));
}).join(' ');
}