-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
121 lines (102 loc) · 3.5 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
const rotateMin = date => {
const minuteHand = document.getElementById('minute');
let secVal = date.getSeconds(),
degFromSeconds = .1 * secVal,
minuteVal = date.getMinutes();
minuteHand.style.transform = `rotate(${(minuteVal*6)+degFromSeconds}deg)`;
rotateHour(date, minuteVal);
}
const rotateHour = (date, minuteVal) => {
const hourHand = document.getElementById('hour');
let degFromMin = .5 * minuteVal,
hourVal = date.getHours();
hourHand.style.transform = `rotate(${(hourVal*30)+degFromMin}deg)`;
}
const weekDay = date => {
const weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
dateIndex = date.getDay();
return weekDays[dateIndex];
}
const monthName = date => {
const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
monthIndex = date.getMonth();
return monthNames[monthIndex];
}
const displayDate = date => {
const dateOutput = document.getElementById("date-text"),
dayNumber = date.getDate();
dateOutput.innerHTML = `${weekDay(date)},<br/>${monthName(date)} ${dayNumber}`;
}
const getUserLocation = async () => {
let connection = await fetch('http://ip-api.com/json/');
if (connection.ok){
let response = await connection.json(),
location = `${response.city}, ${response.country}`
return location;
}else {
return false;
}
}
const getWeatherInfo = () => {
return getUserLocation().then(async location => {
if (!location){
return false;
}
document.getElementById("location").innerHTML = location;
let connection = await fetch(`https://api.weatherapi.com/v1/current.json?key=38a5051e7ce6493f86c50549200212&q=${location}`, {cache: "no-cache"});
if(connection.ok){
let response = await connection.json(),
info = response.current;
return info;
}else {
return false;
}
});
}
const printTempInfo = scale => {
getWeatherInfo().then(info => {
if (!info){
return false;
}
const temp = document.getElementById("weather-temp");
switch (scale){
default:
case "c":
temp.innerHTML = `${info.temp_c}°c`;
temp.title = "Celsius";
break;
case "f":
temp.innerHTML = `${info.temp_f}°f`;
temp.title = "Fahrenheit";
}
})
}
const printWeatherInfo = () => {
getWeatherInfo().then(info =>{
if (!info){
return false;
}
const icon = document.getElementById("weather-icon"),
temp = document.getElementById("weather-temp"),
parent = document.getElementById("weather");
icon.src = info.condition.icon;
icon.alt = info.condition.text;
icon.title = info.condition.text;
printTempInfo('c');
icon.addEventListener('load', () => parent.style.display = "flex");
temp.addEventListener('click', switchTempScale);
})
}
const switchTempScale = () => {
const temp = document.getElementById("weather-temp");
temp.title[0] == "C" ? printTempInfo('f') : printTempInfo('c');
}
window.addEventListener("load", () => {
setInterval(() => {
let dd = new Date();
rotateMin(dd);
}, 100);
let dd = new Date();
displayDate(dd);
printWeatherInfo();
})