-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherWidget.js
221 lines (186 loc) · 6.17 KB
/
weatherWidget.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
const config = {
targetId: "weatherWidgetDiv",
lat: 46.6357,
lng: 14.311817,
apiKey: "96fe2517f425092a3ad95905aa85bd71",
URL: "https://api.openweathermap.org/data/2.5/weather",
refreshTime: 300000,
locations: [
{
name: "Klagenfurt",
lat: 46.6357,
lng: 14.311817,
},
{
name: "London",
lat: 51.5074,
lng: 0.1278,
},
{
name: "New York",
lat: 40.7128,
lng: -74.006,
},
{
name: "Tokyo",
lat: 35.6895,
lng: 139.6917,
},
],
};
const weatherWidget = {
execute: (config) => {
const loadWidget = () => {
weatherWidget.getWeatherData(config).then((data) => {
weatherWidget.createLayout();
weatherWidget.createWeatherDescription(data);
weatherWidget.createData(data);
weatherWidget.createLocationButton();
});
};
loadWidget();
setInterval(loadWidget, config.refreshTime);
},
getWeatherData: async function (config) {
try {
const apiUrl = `${config.URL}?lat=${config.lat}&lon=${config.lng}&appid=${config.apiKey}`;
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error("error");
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
createLayout: () => {
const target = document.getElementById(config.targetId);
target.innerHTML = "";
const main = document.createElement("div");
main.id = "weatherWidgetMain";
main.classList.add("uk-flex");
const iconDiv = document.createElement("div");
iconDiv.id = "iconDiv";
iconDiv.classList.add("uk-flex", "uk-flex-column", "uk-width-2-3");
const dataDiv = document.createElement("div");
dataDiv.id = "dataDiv";
dataDiv.classList.add("uk-grid", "uk-grid-medium", "uk-flex-column");
main.append(iconDiv, dataDiv);
target.append(main);
},
createWeatherDescription: (data) => {
const iconDiv = document.getElementById("iconDiv");
const icon = document.createElement("img");
icon.style.width = "200px";
const iconId = data.weather[0].icon;
icon.src = `https://openweathermap.org/img/w/${iconId}.png`;
const para = document.createElement("p");
para.classList.add("uk-text-left", "uk-padding-small", "uk-margin-remove");
para.innerText = data.weather[0].description;
iconDiv.append(para, icon);
},
createData: (data) => {
const dataDiv = document.getElementById("dataDiv");
const locationDate = weatherWidget.locationAndDate(data);
const weatherData = weatherWidget.weatherData(data);
dataDiv.append(locationDate, weatherData);
},
locationAndDate: (data) => {
const locationDate = document.createElement("div");
locationDate.id = "locationDate";
locationDate.classList.add(
"uk-grid",
"uk-flex-column",
"uk-margin-small-top"
);
//StadtName und Region
const cityName = document.createElement("p");
cityName.classList.add("uk-text-left");
cityName.innerText = data.name;
const regionPara = document.createElement("p");
regionPara.classList.add("uk-text-left");
const regionIcon = document.createElement("span");
regionIcon.setAttribute("uk-icon", "location");
regionIcon.setAttribute("ratio", "0.8");
regionPara.append(regionIcon);
const regionID = data.sys.country;
let regionNames = new Intl.DisplayNames(["en"], { type: "region" });
regionPara.append(document.createTextNode(` ${regionNames.of(regionID)}`));
//wochentag
const today = new Date();
const dayNames = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const day = document.createElement("p");
day.classList.add("uk-text-left");
const dayIcon = document.createElement("span");
dayIcon.setAttribute("uk-icon", "calendar");
dayIcon.setAttribute("ratio", "0.8");
day.append(dayIcon);
day.append(document.createTextNode("" + dayNames[today.getDay()]));
locationDate.append(cityName, regionPara, day);
return locationDate;
},
weatherData: (data) => {
const weatherData = document.createElement("div");
weatherData.id = "weatherData";
weatherData.classList.add(
"uk-grid",
"uk-flex-column",
"uk-margin-small-top"
);
//Temperatur
const tempCelsius = data.main.temp - 273.15;
const tempPara = document.createElement("p");
const tempIcon = document.createElement("i");
tempIcon.classList.add("wi", "wi-thermometer");
tempPara.append(tempIcon);
tempPara.appendChild(
document.createTextNode(" Temperature: " + tempCelsius.toFixed(1) + "°C")
);
//Luftfeuchtigkeit
const humidity = data.main.humidity;
const humidityPara = document.createElement("p");
const humidityIcon = document.createElement("i");
humidityIcon.classList.add("wi", "wi-humidity");
humidityPara.append(humidityIcon);
humidityPara.appendChild(
document.createTextNode(" Humidity: " + humidity + "%")
);
//Windgeschwindigkeit
const windSpeed = data.wind.speed;
const windSpeedPara = document.createElement("p");
const windIcon = document.createElement("i");
windIcon.classList.add("wi", "wi-wind");
windSpeedPara.append(windIcon);
windSpeedPara.appendChild(
document.createTextNode(" Wind: " + windSpeed + " m/s")
);
weatherData.append(tempPara, humidityPara, windSpeedPara);
return weatherData;
},
createLocationButton: () => {
const target = document.getElementById(config.targetId);
const locationButton = document.createElement("button");
locationButton.innerText = "Change Location";
locationButton.classList.add("uk-button", "uk-button-primary");
locationButton.addEventListener("click", () => {
const cleanLocationArr = config.locations.filter(
(location) => location.lat !== config.lat && location.lng !== config.lng
);
const randomLocation =
cleanLocationArr[Math.floor(Math.random() * cleanLocationArr.length)];
config.lat = randomLocation.lat;
config.lng = randomLocation.lng;
weatherWidget.execute(config);
});
target.append(locationButton);
},
};