-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
151 lines (142 loc) · 4.42 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
// Search on enter key event
App = {
search: function(e) {
if (e.keyCode == 13) {
var val = document.getElementById('search-field').value;
window.open('https://google.com/search?q=' + val);
document.getElementById('search-field').value = '';
document.getElementById('search-field').blur();
document.getElementById('search').style.display = 'none';
}
},
// Get current time and format
getTime: function() {
var date = new Date();
return date.toLocaleString('en-US', {
hour: 'numeric',
minute: 'numeric',
hour12: true
});
},
getWeatherIcon: function(iconCode) {
var dayNight = iconCode.slice(2, 3);
console.log(dayNight);
var code = iconCode.slice(0, 2);
if (dayNight == 'd') {
switch (code) {
case '01':
return 'icons/sunny.png';
case '02':
return 'icons/partly_cloudy.png';
case '03':
return 'icons/cloudy.png';
case '04':
return 'icons/cloudy_s_sunny.png';
case '09':
return 'icons/rain_s_cloudy.png';
case '10':
return 'icons/rain.png';
case '11':
return 'icons/thunderstorms.png';
case '13':
return 'icons/snow.png';
case '50':
return 'icons/mist.png';
default:
return null;
}
} else {
switch (code) {
case '01':
return 'icons/night.png';
case '02':
return 'icons/night_partly_cloudy.png';
case '03':
return 'icons/cloudy.png';
case '04':
return 'icons/cloudy_night.png';
case '09':
return 'icons/rain_night.png';
case '10':
return 'icons/rain.png';
case '11':
return 'icons/thunderstorms.png';
case '13':
return 'icons/night_snow.png';
case '50':
return 'icons/mist.png';
default:
return null;
}
}
},
getWeather: function() {
let xhr = new XMLHttpRequest();
/* OPEN WEATHER MAP */
xhr.open(
'GET',
'https://api.openweathermap.org/data/2.5/weather?id=2158177&appid=2d33137dd0ae28b599bdcedc827a9560&units=metric'
);
xhr.onload = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let json = JSON.parse(xhr.responseText);
var temp = json.main.feels_like.toFixed(0) + '°C';
var weatherDescription = json.weather[0].description;
var weatherIcon = App.getWeatherIcon(json.weather[0].icon);
if (weatherIcon) {
document.getElementById('weather').innerHTML =
weatherDescription + ', feels like ' + temp;
document.getElementById('weather-icon').src = weatherIcon;
} else {
document.getElementById('weather').innerHTML =
weatherDescription + ' ' + temp;
}
} else {
console.log('error msg: ' + xhr.status);
}
}
};
/* Aus BOM */
//xhr.open('GET', "https://api.weather.bom.gov.au/v1/locations/r3gx2f/observations");
//xhr.onload = () => {
// if (xhr.readyState === 4) {
// if (xhr.status === 200) {
// let json = JSON.parse(xhr.responseText);
// //var temp = json.main.temp.toFixed(0) + "°C";
// } else {
// console.log('error msg: ' + xhr.status);
// }
//}
xhr.send();
},
init: function() {
/* CLOCK */
document.getElementById('clock').innerHTML = App.getTime();
setInterval(() => {
document.getElementById('clock').innerHTML = App.getTime();
}, 30000);
/* Weather */
App.getWeather();
/* EVENT LISTENERS */
// search when th enter key is pressed
var searchField = document.getElementById('search-field');
searchField.addEventListener('keypress', event => {
return App.search(event);
});
// search field shorcut
document.addEventListener('keydown', event => {
if (event.keyCode == 32) {
// Spacebar code to open search
document.getElementById('search').style.display = 'flex';
document.getElementById('search-field').focus();
} else if (event.keyCode == 27) {
// Esc to close search
document.getElementById('search-field').value = '';
document.getElementById('search-field').blur();
document.getElementById('search').style.display = 'none';
}
});
}
};
window.onload = App.init();