-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
60 lines (52 loc) · 2.21 KB
/
ui.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
// UI Class
class UI{
constructor(){
this.location = document.getElementById('w-location');
this.desc = document.getElementById('w-desc');
this.string = document.getElementById('w-string');
this.details = document.getElementById('w-details');
this.icon = document.getElementById('w-icon');
this.humidity = document.getElementById('w-humidity');
this.feelsLike = document.getElementById('w-feelsLike');
this.dewpoint = document.getElementById('w-dewpoint');
this.wind = document.getElementById('w-wind');
}
// Show Results
paint(weather, unit){
this.location.textContent = `${weather.name}, ${weather.sys.country}`;
this.desc.textContent = `${weather.weather[0].description}`;
if(unit === 'metric'){
this.string.textContent = `${Math.round(weather.main.temp)} ` + String.fromCharCode(8451);
}else{
this.string.textContent = `${Math.round(weather.main.temp)} ` + String.fromCharCode(8457);
}
this.icon.setAttribute('src', `https://openweathermap.org/img/wn/${weather.weather[0].icon}@2x.png`);
this.humidity.textContent = `Relative Humidity: ${weather.main.humidity}%`;
this.feelsLike.textContent = `Pressure: ${weather.main.pressure} hpa`;
this.dewpoint.textContent = `Visibility: ${weather.visibility} m`;
this.wind.textContent = `Wind Speed/Degree: ${weather.wind.speed} m/s, (${weather.wind.deg})`;
}
// Show Alert
showAlert(msg, className) {
// Clear the last alerts
this.clearAlert();
// Create alert message
const div = document.createElement('div');
div.className = className;
div.appendChild(document.createTextNode(msg));
const container = document.querySelector('.container');
const search = document.querySelector('.row');
container.insertBefore(div, search)
// remove the message after 3 sec
setTimeout(() => {
this.clearAlert();
}, 3000);
}
// Clear Alert Message
clearAlert() {
const currentAlert = document.querySelector('.alert');
if (currentAlert) {
currentAlert.remove();
}
}
}