forked from boradesanket13/Blaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
175 lines (162 loc) · 5.02 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
function disapper() {
document.querySelector(".weather").style.display = "none";
}
function functionAlert(msg, myYes) {
disapper();
var confirmBox = $("#confirm");
confirmBox.find(".message").text(msg);
confirmBox
.find(".yes")
.unbind()
.click(function () {
confirmBox.hide();
});
confirmBox.find(".yes").click(myYes);
confirmBox.show();
}
let temp1save,
temp2save = 0,
showTime;
let weather = {
apiKey: "e1b292964b3c86ad361260f80c9496e0",
fetchWeather: function (city, lat = null, lon = null) {
if (lat && lon) {
fetch(
"https://api.openweathermap.org/data/2.5/weather?lat=" +
lat +
"&lon=" +
lon +
"&units=metric&appid=" +
this.apiKey
)
.then((response) => {
if (!response.ok) {
functionAlert();
}
return response.json();
})
.then((data) => this.displayWeather(data));
} else if (city) {
fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
city +
"&units=metric&appid=" +
this.apiKey
)
.then((response) => {
if (!response.ok) {
functionAlert();
}
return response.json();
})
.then((data) => this.displayWeather(data));
}
},
displayWeather: function (data) {
const { name, timezone } = data;
const { icon, description } = data.weather[0];
const { temp, humidity } = data.main;
const temp2 = temp * 1.8 + 32;
const { speed } = data.wind;
document.querySelector(".city").innerText = "Weather in " + name;
document.querySelector(".icon").src =
"https://openweathermap.org/img/wn/" + icon + ".png";
document.querySelector(".description").innerText = description;
!document.querySelector("#checkbox").checked
? (document.querySelector(".temp").innerText = temp.toFixed(2) + "℃")
: (document.querySelector(".temp").innerText = temp2.toFixed(2) + "℉");
temp1save = temp.toFixed(2);
temp2save = temp2.toFixed(2);
speed1 = speed.toFixed(2);
speed2 = (speed * 0.62137).toFixed(2);
const timeOptions = {
hours: "2-digit",
minutes: "2-digit",
};
showTime = setInterval(() => {
let time = new Date(
new Date().getTime() +
timezone * 1000 +
new Date().getTimezoneOffset() * 60000
);
document.querySelector("#time").innerText =
time.toLocaleTimeString(undefined, timeOptions) +
" " +
time.toLocaleDateString();
}, 1000);
document.querySelector(".humidity").innerText =
"Humidity: " + humidity + "%";
document.querySelector(".wind").innerText =
"Wind speed: " + speed + " km/h";
document.querySelector(".weather").classList.remove("loading");
document.body.style.backgroundImage =
"url('https://source.unsplash.com/1600x900/?" + name + "')";
},
search: function () {
clearInterval(showTime);
this.fetchWeather(document.querySelector(".search-bar").value);
document.getElementById("search").focus();
document.getElementById("search").value = "";
},
};
document.querySelector(".search button").addEventListener("click", function () {
weather.search();
});
document
.querySelector(".search-bar")
.addEventListener("keyup", function (event) {
if (event.key == "Enter") {
weather.search();
}
});
// ask for location permission
// if granted: show user's location weather data
// if blocked: show default location weather data
navigator.geolocation.getCurrentPosition(
(position) => {
const { latitude, longitude } = position.coords;
weather.fetchWeather(null, latitude, longitude);
},
() => weather.fetchWeather("Nashik")
);
function onTempChange() {
!document.querySelector("#checkbox").checked
? ((document.querySelector(".temp").innerText = temp1save + "℃"),
(document.querySelector(".wind").innerText =
"Wind speed: " + speed1 + " km/h"))
: ((document.querySelector(".temp").innerText = temp2save + "℉"),
(document.querySelector(".wind").innerText =
"Wind speed: " + speed2 + " mph"));
}
function reloadPage() {
location.reload();
}
let darkMode = localStorage.getItem("darkMode");
const darkModeToggle = document.querySelector(".toggle-btn");
//check if Dark mode is enabled
// if its enabled turn it off
// else turn it on
const enableDarkMode = () => {
document.body.classList.add("darkmode");
localStorage.setItem("darkMode", "enabled");
};
const disableDarkMode = () => {
document.body.classList.remove("darkmode");
localStorage.setItem("darkMode", null);
};
darkModeToggle.addEventListener("click", () => {
darkMode = localStorage.getItem("darkMode");
if (darkMode != "enabled") {
enableDarkMode();
console.log(darkMode);
} else {
disableDarkMode();
console.log(darkMode);
}
});
function myFunction() {
var element = document.getElementById("box");
element.classList.toggle("light-mode");
var e2 = document.getElementById("search");
e2.classList.toggle("change");
}