forked from duc-cute/weather-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
298 lines (273 loc) · 12.7 KB
/
app.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
'use strict'
const $=document.querySelector.bind(document);
const $$=document.querySelectorAll.bind(document);
const place=$('.city__position .city__name');
const city=$('.weather__title-heading span');
const temperatureTitleMax=$('.temperature__value-max ');
const temperatureTitleMin=$('.temperature__value-min ');
const weatherShortDes=$('.weather__title-short-des');
const pressure=$('.weather__title-more-des .pressure span');
const visibility=$('.weather__title-more-des .visibility span');
const humidity=$('.weather__title-more-des .humidity span');
const nameWeatherTitle=$('.weather__title-heading .name__weather-short');
const speedWind=$('.weather__title-wind .wind__speed-now');
const titleDes=$('.weather__status-body .weather__title-des');
const cityStatus=$('.city__status');
const cityTemp=$('.city__temperature-value');
const cloudsPercent=$('.clouds__range-sliderValue span');
const inputClouds=$('.input-percent #clouds__rate');
const percentBar=$('.percent-bar');
const sunset=$('.content__time-value .content__time-value-end');
const sunrise=$('.content__time-value .content__time-value-start');
const inputTimeSun=$('#time-sun');
const inputBg=$('.content__time-sun-bg');
const sun=$('.content__time-sun > i');
const predictionDay=$$('.weather__prediction-day');
const weatherNextCity=$('.weather__tomorrow-city')
const weatherNextTemp=$('.weather__tomorrow-temperature');
const weatherNextDes=$('.weather__tomorrow-des');
const searchInput=$('#search');
const app= {
months : [
{1: 'January'},
{2: 'February'},
{3: 'March'},
{4: 'April'},
{5: 'May'},
{6: 'June'},
{7: 'July'},
{8: 'August'},
{9: 'September'},
{10: 'October'},
{11: 'November'},
{12: 'December'}
],
render:function() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
let lon=position.coords.longitude
let lat=position.coords.latitude
let apiCurrent=`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=acf3af863acf99df0d6e052b4af19254`
fetch(apiCurrent)
.then(res => {
return res.json()
})
.then(data => {
this.weatherReport(data);
})
})
}
},
weatherReport : function(data) {
var urlWeatherReport=`https://api.openweathermap.org/data/2.5/forecast?q=${data.name}&appid=acf3af863acf99df0d6e052b4af19254`
fetch(urlWeatherReport)
.then(res => {
return res.json()
})
.then(dayForecastData => {
//Place
const cityName=data.name.trim().replace('Tinh ','');
place.innerText=`${cityName}, ${data.sys.country}`
city.innerText=`${cityName}`
//Temperature
temperatureTitleMax.innerHTML=`${Math.round(data.main.temp_max - 272.15)}<sup>o</sup>C`
temperatureTitleMin.innerHTML=`${Math.round(data.main.temp_min - 272.15)}<sup>o</sup>C`
weatherShortDes.innerText=`${data.weather[0].description}`
//More des
pressure.innerText=`${data.main.pressure} hPa`
visibility.innerText=`${Math.round(data.visibility / 1000)} km`
humidity.innerText=`${data.main.humidity} %`
nameWeatherTitle.innerText=`${data.weather[0].main}`
speedWind.innerText=`${Math.floor(data.wind.speed * 36) / 10}km`
titleDes.innerText=`${data.weather[0].description}`
cityStatus.innerText=`${data.weather[0].main}`
cityTemp.innerHTML=`${Math.round(data.main.temp - 272.15)}<sup>o</sup>C`
//input clouds
cloudsPercent.innerText=`${data.clouds.all}%`
cloudsPercent.style.left=`${data.clouds.all}%`
percentBar.style.width=`${data.clouds.all}%`
//Tomorrow weather
weatherNextCity.innerText=`${cityName}`
//Time Sun
function sunRise() {
let dateSunrise = new Date((data.sys.sunrise)*1000);
var hours=dateSunrise.getHours() < 10 ? '0'+dateSunrise.getHours() : dateSunrise.getHours();
var minutes=dateSunrise.getMinutes() <10 ? '0'+dateSunrise.getMinutes() : dateSunrise.getMinutes();
var hoursSunriseChange=hours > 12 ? `${hours-12}` : hours;
sunrise.innerText=`${hoursSunriseChange}:${minutes} am`
}
function sunSet() {
let dateSunset = new Date((data.sys.sunset)*1000);
var hours=dateSunset.getHours() < 10 ? '0'+dateSunset.getHours() : dateSunset.getHours();
var minutes=dateSunset.getMinutes() <10 ? '0'+dateSunset.getMinutes() : dateSunset.getMinutes();
var hoursSunsetChange=hours > 12 ? `${hours-12}` : hours;
sunset.innerText=`${hoursSunsetChange}:${minutes} pm`
}
sunRise()
sunSet()
//Input Sun
function inputSun() {
let dataSunrise=data.sys.sunrise
let dataSunset=data.sys.sunset
let dataCurrent=data.dt
let valueInput=Math.round((dataCurrent - dataSunrise)/(dataSunset-dataSunrise)*100)
if(valueInput >= 0) {
inputBg.style.width=`${valueInput}%`
let currentSun=11 + (dataCurrent - dataSunrise)/(dataSunset-dataSunrise)*72.675;
if(currentSun< 14) {
sun.style.left='14%'
}else if(currentSun > 84) {
sun.style.left='84%'
}else {
sun.style.left=`${currentSun}%`
}
let currentSlider=13.66 + (dataCurrent - dataSunrise)/(dataSunset-dataSunrise)*72.675;
if(currentSlider < 13.66) {
inputTimeSun.value='13.3'
}else if(currentSlider > 86) {
inputTimeSun.value='86.7'
}else {
inputTimeSun.value=`${currentSlider}`
}
}else {
dataCurrent=1;
console.log(valueInput)
}
}
inputSun()
this.dayForecast(dayForecastData);
//Chart
const list1=[]
const list2=[]
for(var i=5;i<=11;i+=2) {
var temp=Math.round(dayForecastData.list[i].main.temp - 272.15)
list1.push(temp)
console.log(dayForecastData.list[i].dt_txt);
}
for(var i=3;i<=11;i+=2) {
var temp=Math.round(dayForecastData.list[i].main.temp - 272.15)
list2.push(temp)
}
const labels = [
'Morning',
'Afternoon',
'Evening',
'Night',
]
const datas = {
labels: labels,
datasets: [{
label: 'My First dataset',
data: list1,
pointBackgroundColor:'#ff8d24',
pointHoverBackgroundColor:'red',
tension:0.5,
borderColor: '#ff8d24',
},
{
label: 'My Second dataset',
backgroundColor: '#ccc',
data: list2,
pointBackgroundColor:'transparent',
pointHoverBackgroundColor:'ccc',
tension:0.3,
borderColor: '#ccc',
borderWidth:1,
pointBorderWidth:0,
}
]
}
const config = {
type: 'line',
data: datas,
options: {
plugins: {
legend: {
display:false,
position:'right',
}
},
responsive: false,
scales: {
y: {
beginAtZero:true,
},
},
layout: {
// padding: {
// right: 80,
// bottom:80
// }
}
}
}
Chart.defaults.color='#333'
Chart.defaults.borderColor='#fff'
Chart.defaults.font.family='Arial'
Chart.defaults.font.size='18'
$('#myChart').remove();
$('.content__chart-myChart').innerHTML='<canvas id="myChart"></canvas>'
var ctx = document.getElementById("myChart").getContext("2d");
const myChartWeather = new Chart(ctx,config);
})
},
handleDayForecast:function(data,i,index) {
const icons={
Clouds:`<i class="fa-solid fa-clouds icon-clouds"></i>`,
Rain:`<i class="fa-solid fa-cloud-rain icon-rain"></i>`
}
const iconName =data.list[i].weather[0].main;
// console.log(icons[iconName])
let date=new Date(data.list[i].dt*1000);
const month=this.months[date.getMonth()][date.getMonth()+1];
const day=date.getDate();
const tempMax=`${Math.round(data.list[i].main.temp_max-272,15)}<sup>o</sup>C`
const tempMin=`${Math.round(data.list[i].main.temp_min-272,15)}<sup>o</sup>C`
var predictionDayArr=Array.from(predictionDay)
predictionDayArr[index].querySelector('.weather__prediction-date').innerText=`${month} ${day}`
predictionDayArr[index].querySelector('.weather__prediction-des span').innerText=data.list[i].weather[0].main
predictionDayArr[index].querySelector('.weather__prediction-icon').innerHTML=icons[iconName];
predictionDayArr[index].querySelector('.weather__prediction-temperature').innerHTML=`${tempMax} /${tempMin}`;
},
dayForecast:function(data) {
var index=0;
weatherNextDes.innerText=data.list[7].weather[0].description
weatherNextTemp.innerHTML=`${Math.round(data.list[0].main.temp - 272,15)}<sup>o</sup>C `
for(var i=7;i<16;i+=8) {
this.handleDayForecast(data,i,index);
index++;
}
},
renderBySearch:function() {
var placeInput=searchInput.value
if(placeInput) {
var urlSearch=`https://api.openweathermap.org/data/2.5/weather?q=${placeInput}&appid=acf3af863acf99df0d6e052b4af19254`
fetch(urlSearch)
.then(res => {
return res.json()
})
.then(data => {
this.weatherReport(data)
})
}
},
handleInput :function() {
//Input onblur
searchInput.onblur=function searchByCity() {
// this.renderBySearch()
app.renderBySearch()
searchInput.value=''
}
searchInput.addEventListener('keyup',e => {
if(e.keyCode === 13) {
app.renderBySearch()
searchInput.value='';
}
})
},
start:function() {
this.render();
this.handleInput()
},
}
app.start();