-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
541 lines (536 loc) · 17.8 KB
/
index.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import key from './config.js';
window.getWeather = async function () {
//where temperature will be sent
const weatherDisplay = document.querySelector("#tempDisplay");
//hide button for hourly display
document.getElementById('hourlyButtons').style.display="none";
let city = " ";
//get city, if no city set to Barcelona
if (document.querySelector('#city').value == ""){
city = "Barcelona"
} else {
city = document.querySelector('#city').value.trim();
}
const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${key}`
//get data
let response = await fetch(weatherUrl);
let data = await response.json();
//get temp from response
let temp = data.main.temp;
//convert temperature to farinheight if its the value of the button
temp = tempConvert(temp);
//push temperature
weatherDisplay.innerHTML = `<p> ${temp}°</p>`
//push city data to display
document.querySelector('#currentCity').innerHTML = city;
//load 7 day weather
getForecast(data.coord.lat,data.coord.lon);
};
getWeather();
//convert temp between cel and far
const tempConvert = (temp) => {
//convert temperature to farinheight if its the value of the button
let choice = document.querySelector('#celFar')
if(choice.value == "f") {
temp = Math.round(1.8 * (temp-273) + 32);
//Show them to click to diplay the other
choice.innerHTML = "Display Celsius"
//return temp
return temp
} else {
temp = Math.round(temp-273.5);
//Show them to click to diplay the other
choice.innerHTML = "Display Fahrenheit"
//return temp
return temp
}
};
//get days of the week
const getDays = (dayIndex) => {
//days of week
const days = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
]
//get that place on the index
let dayWeek = days[dayIndex];
return dayWeek
};
//get date for main date and time display
function getDate (){
//Months
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
const date = new Date();
//get day of week as number
const dayIndex = date.getDay();
//get that place on the index
const dayWeek = getDays(dayIndex);
//get the month as a number
const monthIndex = date.getMonth();
//get that spot on the index
const month = months[monthIndex];
//day
const day = date.getDate();
//year
const year = date.getFullYear();
//send data to html
const dateText = document.querySelector("#date");
dateText.innerHTML = `${dayWeek}, ${month} ${day}, ${year}`
};
getDate();
//get time for main date and time display
function getTime () {
const time = new Date();
//get hour
let hour = time.getHours();
//get minutes
let minutes = time.getMinutes()
//make sure it displays as 2 digits
if (minutes < 10) {
minutes = '0' + minutes;
} else {
minutes = minutes + '';
};
//add am and pm
let timeOfDay = " ";
if (hour < 12) {
timeOfDay = "AM";
hour = hour%12;
if(hour==0) {
hour = 12;
}
} else {
timeOfDay= "PM";
hour = hour%12;
if(hour==0) {
hour = 12;
}
};
//send data to div
const timeText = document.querySelector('#time');
timeText.innerHTML = `${hour}:${minutes} ${timeOfDay}`;
};
getTime();
//keep refreshing the time every 1000 miliseconds
setInterval(getTime,1000);
//keep track of celcius vs farenheight choice
window.celFar = function () {
let button = document.querySelector('#celFar')
const option = button.value;
//they want to see c
if(option == "f") {
//change value to c
button.value="c";
} else {
//set value to c
button.value="f";
}
getWeather();
};
//get weather for 7day forecast
const getForecast = async(lat,lon) => {
const url = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&appid=${key}`;
//hide button for hourly display
document.getElementById('hourlyButtons').style.display="none";
//change style of hourly button
document.getElementById('hourlyButton').style.backgroundColor= "";
//change style of daily button
document.getElementById('dailyButton').style.backgroundColor= "rgba(255, 255, 255, 0.3)";
try {
const res = await fetch(url);
const data = await res.json();
//get array of daily temps
const daily = data.daily
//get day divs where the data will be appended
const display = document.querySelectorAll('#day')
//loop through array of daily data, we only want 7 days, so minus 2
for(let i=0;i<daily.length-1;i++) {
//Loop through day divs append the new p element to the day div
for(let j=0;i<display.length;j++){
//create new p elements, max and min, and add the temp to the p elements inner text
const pMax = document.createElement('p');
pMax.id= "pMax";
const pMin = document.createElement('p');
pMin.id= "pMin";
//get the max and min temps that we will add to the p element, and convert from kelvin
const max = tempConvert(daily[i].temp.max)
const min = tempConvert(daily[i].temp.min)
if(document.getElementById('celFar').value== "f"){
pMax.innerText = `${max} °F `
pMin.innerText = `${min} °F `
} else {
pMax.innerText = `${max} °C `
pMin.innerText = `${min} °C `
}
//get the timestamp provided by the api
const timeStamp = daily[i].dt;
//create a new date object using timestap
const date = new Date(timeStamp*1000);
//get day as a number and pass it through getDays function
const day = getDays(date.getDay())
//create p element to append day of week
const dayWeek = document.createElement('p');
dayWeek.id = "dayWeek";
//add the day to the inner text
dayWeek.innerText = day
//reset display
display[j].innerText="";
//reset images on first iteration
if(i==0){
const images = document.querySelectorAll('#weatherIcons');
Array.from(images).forEach((image)=> {
image.parentNode.removeChild(image);
})
};
//get the weather icon code depending on the day
const iconCode = daily[i].weather[0].icon;
//create img element
const img = document.createElement('img');
img.id = "weatherIcons";
//add src
img.src=`http://openweathermap.org/img/wn/${iconCode}@2x.png`
//append dayWeek
display[j].appendChild(dayWeek);
//append max and min children
display[j].appendChild(pMax);
display[j].appendChild(pMin);
//append image
display[j].appendChild(img);
i++
}
}
} catch(err) {
console.log(err);
}
};
//get hourly forecase
window.hourlyForecast = async function () {
//show buttons for hourly display
document.getElementById('hourlyButtons').style.display="";
let city = " "
//get current city
if (document.querySelector('#city').value == ""){
city = "Barcelona"
} else {
city = document.querySelector('#city').value.trim();
}
//make api call to get lat and lon
const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${key}`
const response = await fetch(weatherUrl)
const data = await response.json();
//get lat long to make new api request for hourly data.
const lat = data.coord.lat;
const lon = data.coord.lon;
//make api call to get hourly data
const urlHourly = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=daily,minutely&appid=${key}`
const res = await fetch(urlHourly);
const dataHourly = await res.json();
const hourly = [...dataHourly.hourly]
//splie out the hours past 24 hours
hourly.splice(24);
//put the 12 clock hours to this array
const hours = [ ]
const timesOfDay = [ ];
const iconCodes = [ ]
//turn on display of the 24 divs
//change the timestamp to reflect hours
hourly.forEach((hour)=> {
//get the icon then push to array
let iconCode = hour.weather[0].icon;
iconCodes.push(iconCode);
hour = new Date(hour.dt*1000).getHours()
let timeOfDay=" "
//convert to 12-hour clock and add am/pm
if (hour < 12) {
timeOfDay = "AM";
hour = hour%12;
if(hour==0) {
hour = 12;
}
} else {
timeOfDay= "PM";
hour = hour%12;
if(hour==0) {
hour = 12;
}
};
//push hour to hours array
hours.push(hour);
//push times of day
timesOfDay.push(timeOfDay);
});
//hide weekly display
document.getElementById('daily').style.display = "none";
//show hourly display
document.getElementById('hourly').style.display = "";
//clear divs from the previous time
let oldDivs = document.querySelectorAll("#hourlyWeather");
oldDivs = [...oldDivs];
if(oldDivs){
oldDivs.forEach((div)=> {
div.parentNode.removeChild(div);
})
};
//get tabs
const tab1 = document.getElementById('tab1');
const tab2 = document.getElementById('tab2');
const tab3 = document.getElementById('tab3');
for(let i=0; i<hours.length;i++){
let div = document.createElement('div');
div.id = "hourlyWeather";
//add hour and time of day to div
let time = document.createElement('p');
time.innerText = `${hours[i]} ${timesOfDay[i]}`;
time.id="hourlyTime";
//append time to new div
div.appendChild(time);
//add temp from hourly array
let temp = hourly[i].temp
temp = tempConvert(temp);
//create p element for temp
let tempDisplay = document.createElement('p');
tempDisplay.id="hourlyTemps";
//inner text far vs cel
if(document.getElementById('celFar').value== "f"){
tempDisplay.innerText = `${temp} °F `
} else {
tempDisplay.innerText = `${temp} °C`
}
//append temp to div
div.appendChild(tempDisplay);
//append icons to div
//create img element
let img = document.createElement('img');
img.id = "hourlyIcons";
//get icon code from array
let iconCode = iconCodes[i];
//add src
img.src=`http://openweathermap.org/img/wn/${iconCode}@2x.png`;
div.appendChild(img);
if(i<8){
//append previously created div to tab
tab1.appendChild(div);
}
//if i<16 add to tab 2
else if (i<16){
//append previously created div to tab
tab2.appendChild(div);
}
//else add to tab 3
else {
//append previously created div to tab
tab3.appendChild(div);
}
};
//hide tab2 and tab3
document.getElementById('tab2').style.display = "none";
document.getElementById('tab3').style.display = "none";
//show the first tab
document.getElementById('tab1').style.display = ""
//Change style of first tab button
document.getElementById('firstTab').style.backgroundColor="rgba(255, 255, 255, 0.3)";
//change style of hourly button
document.getElementById('hourlyButton').style.backgroundColor= "rgba(255, 255, 255, 0.3)";
//change font-coor
document.getElementById('hourlyButton').style.color= "rgb(59, 58, 58);";
document.getElementById('dailyButton').style.color= "";
//change style of daily button
document.getElementById('dailyButton').style.backgroundColor= "";
}
/* async function hourlyForecast () {
//show buttons for hourly display
document.getElementById('hourlyButtons').style.display="";
let city = " "
//get current city
if (document.querySelector('#city').value == ""){
city = "Barcelona"
} else {
city = document.querySelector('#city').value.trim();
}
//make api call to get lat and lon
const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${key}`
const response = await fetch(weatherUrl)
const data = await response.json();
//get lat long to make new api request for hourly data.
const lat = data.coord.lat;
const lon = data.coord.lon;
//make api call to get hourly data
const urlHourly = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=daily,minutely&appid=${key}`
const res = await fetch(urlHourly);
const dataHourly = await res.json();
const hourly = [...dataHourly.hourly]
//splie out the hours past 24 hours
hourly.splice(24);
//put the 12 clock hours to this array
const hours = [ ]
const timesOfDay = [ ];
const iconCodes = [ ]
//turn on display of the 24 divs
//change the timestamp to reflect hours
hourly.forEach((hour)=> {
//get the icon then push to array
let iconCode = hour.weather[0].icon;
iconCodes.push(iconCode);
hour = new Date(hour.dt*1000).getHours()
//convert to 12-hour clock and add am/pm
if (hour < 12) {
timeOfDay = "AM";
hour = hour%12;
if(hour==0) {
hour = 12;
}
} else {
timeOfDay= "PM";
hour = hour%12;
if(hour==0) {
hour = 12;
}
};
//push hour to hours array
hours.push(hour);
//push times of day
timesOfDay.push(timeOfDay);
});
//hide weekly display
document.getElementById('daily').style.display = "none";
//show hourly display
document.getElementById('hourly').style.display = "";
//clear divs from the previous time
let oldDivs = document.querySelectorAll("#hourlyWeather");
oldDivs = [...oldDivs];
if(oldDivs){
oldDivs.forEach((div)=> {
div.parentNode.removeChild(div);
})
};
//get tabs
const tab1 = document.getElementById('tab1');
const tab2 = document.getElementById('tab2');
const tab3 = document.getElementById('tab3');
for(let i=0; i<hours.length;i++){
let div = document.createElement('div');
div.id = "hourlyWeather";
//add hour and time of day to div
let time = document.createElement('p');
time.innerText = `${hours[i]} ${timesOfDay[i]}`;
time.id="hourlyTime";
//append time to new div
div.appendChild(time);
//add temp from hourly array
let temp = hourly[i].temp
temp = tempConvert(temp);
//create p element for temp
let tempDisplay = document.createElement('p');
tempDisplay.id="hourlyTemps";
//inner text far vs cel
if(document.getElementById('celFar').value== "f"){
tempDisplay.innerText = `${temp} °F `
} else {
tempDisplay.innerText = `${temp} °C`
}
//append temp to div
div.appendChild(tempDisplay);
//append icons to div
//create img element
let img = document.createElement('img');
img.id = "hourlyIcons";
//get icon code from array
let iconCode = iconCodes[i];
//add src
img.src=`http://openweathermap.org/img/wn/${iconCode}@2x.png`;
div.appendChild(img);
if(i<8){
//append previously created div to tab
tab1.appendChild(div);
}
//if i<16 add to tab 2
else if (i<16){
//append previously created div to tab
tab2.appendChild(div);
}
//else add to tab 3
else {
//append previously created div to tab
tab3.appendChild(div);
}
};
//hide tab2 and tab3
document.getElementById('tab2').style.display = "none";
document.getElementById('tab3').style.display = "none";
//show the first tab
document.getElementById('tab1').style.display = ""
//Change style of first tab button
document.getElementById('firstTab').style.backgroundColor="rgba(255, 255, 255, 0.3)";
//change style of hourly button
document.getElementById('hourlyButton').style.backgroundColor= "rgba(255, 255, 255, 0.3)";
//change font-coor
document.getElementById('hourlyButton').style.color= "rgb(59, 58, 58);";
document.getElementById('dailyButton').style.color= "";
//change style of daily button
document.getElementById('dailyButton').style.backgroundColor= "";
}; */
window.dailyForecast = function () {
document.getElementById('hourly').style.display="none";
document.getElementById('daily').style.display=""
//hide button for hourly display
document.getElementById('hourlyButtons').style.display="none";
//change style of hourly button
document.getElementById('hourlyButton').style.backgroundColor= "";
//change style of daily button
document.getElementById('dailyButton').style.backgroundColor= "rgba(255, 255, 255, 0.3)";
//change font-coor
document.getElementById('dailyButton').style.color= "rgb(59, 58, 58);";
document.getElementById('hourlyButton').style.color= "";
};
//hourly tabs
window.openTab = function (value) {
if(value == "tab1") {
//hide other tabs
document.getElementById('tab2').style.display = "none";
document.getElementById('tab3').style.display = "none";
//show selected tap
document.getElementById('tab1').style.display = "";
//change style of button
document.getElementById('firstTab').style.backgroundColor="rgba(255, 255, 255, 0.3)";
document.getElementById('secondTab').style.backgroundColor="";
document.getElementById('thirdTab').style.backgroundColor="";
} else if (value == "tab2") {
//hide other tabs
document.getElementById('tab1').style.display = "none";
document.getElementById('tab3').style.display = "none";
//show selected tab
document.getElementById('tab2').style.display = "";
//change style of button
document.getElementById('secondTab').style.backgroundColor="rgba(255, 255, 255, 0.3)";
document.getElementById('firstTab').style.backgroundColor="";
document.getElementById('thirdTab').style.backgroundColor="";
} else {
//hide other tabs
document.getElementById('tab1').style.display = "none";
document.getElementById('tab2').style.display = "none";
//show selected tab
document.getElementById('tab3').style.display = "";
//change style of button
document.getElementById('thirdTab').style.backgroundColor="rgba(255, 255, 255, 0.3)";
document.getElementById('firstTab').style.backgroundColor="";
document.getElementById('secondTab').style.backgroundColor="";
}
};