-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
271 lines (227 loc) · 7.47 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
//Date
const now = new Date()
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
]
const day = days[now.getDay()]
const hours = now.getHours()
const minutes = now.getMinutes()
const today = document.querySelector("#today-date")
if (minutes < 10) {
today.innerHTML = `${day}, ${hours}:0${minutes}`
} else if (hours < 10) {
today.innerHTML = `${day}, 0${hours}:${minutes}`
} else {
today.innerHTML = `${day}, ${hours}:${minutes}`
}
if (hours > 18 || hours < 6) {
document.body.style.background = `linear-gradient(300deg, rgb(142, 174, 253) 0%, rgb(13, 5, 37) 91%)`
} else {
document.body.style.background = `linear-gradient(300deg, rgb(241, 243, 248) 0%, rgb(142, 174, 253) 91%)`
}
//All the displaying functions
function showCity(response) {
document.querySelector("#city").innerHTML = response.data.name
document.querySelector("title").innerHTML = `Weather forecast | ${response.data.name}`
}
function showTemperature(response) {
document.querySelector("#temp-heading").innerHTML = `<img
src="http://openweathermap.org/img/wn/${
response.data.weather[0].icon
}@2x.png"
alt=""
width="60"/>${Math.round(response.data.main.temp)}°`
document.querySelector("#feels-like").innerHTML = `${
Math.round(response.data.main.feels_like * 10) / 10
}°C`
}
function showSky(response) {
document.querySelector("#sky-description").innerHTML =
response.data.weather[0].description
}
function showMaxTemp(response) {
document.querySelector("#max-temp").innerHTML = `${
Math.round(response.data.main.temp_max * 10) / 10
} °C`
}
function showMinTemp(response) {
document.querySelector("#min-temp").innerHTML = `${
Math.round(response.data.main.temp_min * 10) / 10
} °C`
}
function showHumidity(response) {
document.querySelector(
"#humidity"
).innerHTML = `${response.data.main.humidity}`
}
/*const showWindSpeed = (response) => {
document.querySelector("#wind-speed").innerHTML = `${
Math.round(response.data.wind.speed * 10) / 10
}`
}
*/
function showWindSpeed(response) {
document.querySelector("#wind-speed").innerHTML = `${
Math.round(response.data.wind.speed * 10) / 10
}`
}
//Function formating to hours
function formatHours(x) {
const date = new Date(x * 1000)
const hours = date.getHours()
return hours
}
//Function displaying first 5 hours of forecast
function displayFirstForecast(response) {
const forecast = response.data.hourly
const firstForecast = document.querySelector("#first-forecast")
let forecastHTML = `<div class="row px-3 py-0">`
forecast.forEach(function (forecastHour, index) {
const temp = `${Math.round(forecastHour.temp)}`
const firstForecastTemp = document.querySelector(".firstForecastTemp")
if (index > 0 && index < 6) {
forecastHTML =
forecastHTML +
`<div class="col p-2">
<div>${formatHours(forecastHour.dt)}:00</div>
<div><img src="http://openweathermap.org/img/wn/${forecastHour.weather[0].icon}@2x.png" class="ff-img"/></div>
<div>${temp}°</div>
</div>`
}
})
forecastHTML = forecastHTML + `</div>`
firstForecast.innerHTML = forecastHTML
}
//Function displaying hourly forecast
function displayHourlyForecast(response) {
const forecast = response.data.hourly
const hourlyForecast = document.querySelector("#hourly-forecast")
let forecastHTML = `<div class="table-responsive shadow"><table class="table"><tbody><tr>`
forecast.forEach(function (forecastHour, index) {
if (index > 5 && index < 25) {
forecastHTML =
forecastHTML + `<td>${formatHours(forecastHour.dt)}:00</td>`
}
})
forecastHTML = forecastHTML + `</tr><tr>`
forecast.forEach(function (forecastHour, index) {
if (index > 5 && index < 25) {
forecastHTML =
forecastHTML +
`<td>
<img src="http://openweathermap.org/img/wn/${forecastHour.weather[0].icon}@2x.png" class="hf-img"/>
</td>`
}
})
forecastHTML = forecastHTML + `</tr><tr>`
forecast.forEach(function (forecastHour, index) {
if (index > 5 && index < 25) {
forecastHTML = forecastHTML + `<td>${Math.round(forecastHour.temp)}°</td>`
}
})
forecastHTML = forecastHTML + `</tr></tbody></table></div>`
hourlyForecast.innerHTML = forecastHTML
}
//Function formating date for the foreast
function formatDay(x) {
const date = new Date(x * 1000)
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
]
const day = days[date.getDay()]
return day
}
//Function displaying daily forecast
function displayDailyForecast(response) {
const forecast = response.data.daily
const dailyForecast = document.querySelector("#daily-forecast")
let forecastHTML = `<table class="table table-sm shadow">`
forecast.forEach(function (forecastDay, index) {
if (index > 0 && index < 8) {
forecastHTML =
forecastHTML + ` <tbody>
<tr>
<th scope="row">${formatDay(forecastDay.dt)}</th>
<td><img src="http://openweathermap.org/img/wn/${forecastDay.weather[0].icon}@2x.png" class="df-img"/></td>
<td class="min-temp-daily">${Math.round(forecastDay.temp.min)}°</td>
<td>${Math.round(forecastDay.temp.max)}°</td>
</tr>
</tbody>
`
}
})
forecastHTML = forecastHTML + `</table>`
dailyForecast.innerHTML = forecastHTML
}
function displayForecasts(response) {
displayFirstForecast(response)
displayHourlyForecast(response)
displayDailyForecast(response)
console.log(response.data)
}
function getForecast(coordinates) {
const apiKey = "e2e761297b5d8c34616696904be5d3a8"
const apiUrl = `https://api.openweathermap.org/data/2.5/onecall?lat=${coordinates.lat}&lon=${coordinates.lon}&exclude=minutely&appid=${apiKey}&units=metric`
axios.get(apiUrl).then(displayForecasts)
}
//Function running all the displaying functions
function displayInfo(response) {
showCity(response)
showTemperature(response)
showSky(response)
showMaxTemp(response)
showMinTemp(response)
showHumidity(response)
showWindSpeed(response)
getForecast(response.data.coord)
}
//Function searching info about the city
function searchCity(city) {
const apiKey = "e2e761297b5d8c34616696904be5d3a8"
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`
if (city) {
axios.get(`${apiUrl}`).then(displayInfo)
} else {
alert("Please type a city")
}
}
//Function that's receiving city
function handleSubmit(event) {
event.preventDefault()
const city = document.querySelector("#search-city-input").value
searchCity(city)
}
//Function displaying info about the current city
function showPosition(position) {
const lat = position.coords.latitude
const lon = position.coords.longitude
const apiKey = "e2e761297b5d8c34616696904be5d3a8"
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}&units=metric`
axios.get(`${apiUrl}`).then(displayInfo)
}
//Function getting the current location, after clicking the location button
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition)
} else {
null
}
}
const currentLocationBtn = document.querySelector("#current-location")
currentLocationBtn.addEventListener("click", getLocation)
const form = document.querySelector("#search-city")
form.addEventListener("submit", handleSubmit)
//default
searchCity("Halden")