-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
240 lines (225 loc) · 6.65 KB
/
main.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
var myDataToday;
var myDataForecast;
var cityName = document.getElementById("city-name");
var cityDate = document.getElementById("date");
var dataBox = document.getElementById("data");
var mainIcon = document.getElementById("main-icon");
var windArrow = document.getElementById("wind-icon");
var input = document.getElementById("city-input");
var loading = document.getElementById("loading");
var submit = document.getElementById("check");
var temp = document.getElementById("temp");
var pressure = document.getElementById("pressure");
var humidity = document.getElementById("humidity");
var wind = document.getElementById("wind");
var forecastDay1 = document.getElementById("day1-day");
var forecastDay2 = document.getElementById("day2-day");
var forecastDay3 = document.getElementById("day3-day");
var forecastDay4 = document.getElementById("day4-day");
var forecastDay5 = document.getElementById("day5-day");
var forecastDays = [
forecastDay1,
forecastDay2,
forecastDay3,
forecastDay4,
forecastDay5
];
var forecastIcon1 = document.getElementById("day1-icon");
var forecastIcon2 = document.getElementById("day2-icon");
var forecastIcon3 = document.getElementById("day3-icon");
var forecastIcon4 = document.getElementById("day4-icon");
var forecastIcon5 = document.getElementById("day5-icon");
var forecastIcons = [
forecastIcon1,
forecastIcon2,
forecastIcon3,
forecastIcon4,
forecastIcon5
];
var forecastTemp1 = document.getElementById("day1-temp");
var forecastTemp2 = document.getElementById("day2-temp");
var forecastTemp3 = document.getElementById("day3-temp");
var forecastTemp4 = document.getElementById("day4-temp");
var forecastTemp5 = document.getElementById("day5-temp");
var forecastTemps = [
forecastTemp1,
forecastTemp2,
forecastTemp3,
forecastTemp4,
forecastTemp5
];
var forecastTempNight1 = document.getElementById("day1-temp-night");
var forecastTempNight2 = document.getElementById("day2-temp-night");
var forecastTempNight3 = document.getElementById("day3-temp-night");
var forecastTempNight4 = document.getElementById("day4-temp-night");
var forecastTempNight5 = document.getElementById("day5-temp-night");
var forecastTempsNight = [
forecastTempNight1,
forecastTempNight2,
forecastTempNight3,
forecastTempNight4,
forecastTempNight5
];
var days = ["Pon.", "Wt.", "Śr.", "Czw.", "Pią.", "Sob.", "Niedz."];
var tempUnit = "C";
var windUnit = "m/s";
var metricBtn = document.getElementById("metric-btn");
var imperialBtn = document.getElementById("imperial-btn");
var myRequest = new XMLHttpRequest();
var httpToday = "https://api.openweathermap.org/data/2.5/weather?q=";
var city;
var appid = "&appid=81631cc1843c3ced0966f73c8b9fcdf7";
var units = "&units=metric";
var urlToday;
var urlForecast;
var city2;
var httpForecast = "https://api.openweathermap.org/data/2.5/forecast/daily?q=";
submit.addEventListener("click", function() {
showData();
city = document.getElementById("city-input").value = "City..";
});
input.addEventListener("click", function() {
input.value = "";
});
imperialBtn.addEventListener("click", function() {
units = "&units=imperial";
windUnit = "miles/h";
tempUnit = "F";
changeUnits();
imperialBtn.style.color = "#000";
metricBtn.style.color = "#C6C6C6";
});
metricBtn.addEventListener("click", function() {
units = "&units=metric";
windUnit = "m/s";
tempUnit = "C";
changeUnits();
imperialBtn.style.color = "#C6C6C6";
metricBtn.style.color = "#000";
});
function showData() {
city = document.getElementById("city-input").value;
if (city == "city.." || city == "") {
alert("Enter City");
} else {
city2 = city;
urlToday = httpToday + city + appid + units;
urlForecast = httpForecast + city + appid + units;
loading.classList.toggle("loading-display");
doRequest();
}
}
function changeUnits() {
urlToday = httpToday + city2 + appid + units;
urlForecast = httpForecast + city2 + appid + units;
loading.classList.toggle("loading-display");
doRequest();
}
function doRequest() {
myRequest.open("GET", urlToday);
myRequest.onload = function() {
myDataToday = JSON.parse(myRequest.responseText);
doRequestForecast();
};
myRequest.send();
}
function doRequestForecast() {
myRequest.open("GET", urlForecast);
myRequest.onload = function() {
myDataForecast = JSON.parse(myRequest.responseText);
printData();
dataBoxDisplay();
loading.classList.toggle("loading-display");
};
myRequest.send();
}
function printData() {
var x = Math.pow(10, 1);
var temperature = Math.round(x * myDataToday.main.temp) / x;
var windDeg = myDataToday.wind.deg;
windArrow.style.transform = "rotate(" + windDeg + "deg)";
temp.innerHTML = temperature + "°" + tempUnit;
pressure.innerHTML = myDataToday.main.pressure + " hPa";
humidity.innerHTML = myDataToday.main.humidity + "%";
wind.innerHTML = myDataToday.wind.speed + " " + windUnit;
cityName.innerHTML = myDataToday.name + ", " + myDataToday.sys.country;
var iconId = myDataToday.weather[0].icon;
mainIcon.style.background = "url('img/" + iconId + ".png')";
mainIcon.style.backgroundSize = "cover";
for (var i = 0; i < forecastDays.length; i++) {
forecastIcons[i].style.background =
"url('./img/forecast/" +
myDataForecast.list[i].weather[0].icon +
".png')";
forecastIcons[i].style.backgroundSize = "cover";
forecastTemps[i].innerHTML = myDataForecast.list[i + 1].temp.day;
forecastTempsNight[i].innerHTML = myDataForecast.list[i + 1].temp.night;
}
}
function dataBoxDisplay() {
dataBox.classList = "data-display";
}
setInterval(function() {
var date = new Date();
updateTime(date);
}, 1000);
function updateTime(date) {
var day = date.getDay();
var dayCount = day - 1;
switch (day) {
case 0:
day = "niedziela";
break;
case 1:
day = "poniedziałek";
break;
case 2:
day = "wtorek";
break;
case 3:
day = "środa";
break;
case 4:
day = "czwartek";
break;
case 5:
day = "piątek";
break;
case 6:
day = "sobota";
break;
}
var hours = date.getHours();
var minutes = date.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;
} else if (hours < 10) {
hours = "0" + hours;
}
var time = day + ", " + hours + ":" + minutes;
cityDate.innerHTML = time;
for (var i = 0; i < 5; i++) {
dayCount++;
if (dayCount > 6) {
dayCount = 0;
}
forecastDays[i].innerHTML = days[dayCount];
}
}
let test = {
warzywa: 1,
owoce: 2,
mięso: 4,
ser: 1
};
console.log(
Object.keys(test).map(key => {
return [...Array(test[key])]
.map(i => {
return key;
})
.reduce((arr, el) => {
return arr.concat(el);
}, []);
})
);