-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
341 lines (319 loc) · 11.8 KB
/
script.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//--------------------Themes Toggle---------------------------
const themeButtonMobile = document.querySelector("#options > li:nth-child(1)");
const themeButton = document.getElementById("subthemes");
const mainnav = document.getElementById("mainnav");
const collapse = document.getElementById("collapse");
const mobilenav = document.getElementById("mobilenav");
let resultbox = document.getElementById("resultbox");
let currentTheme = "dark";
let lastsearch = "";
themeButton.addEventListener("click", () => {
if (currentTheme === "dark") {
document.body.style.backgroundImage = 'url("./images/BGlight.png")';
mainnav.style.backgroundColor = "#03657C";
resultbox.style.backgroundColor = "#03657Ca7";
collapse.style.backgroundColor = "#03657C";
mobilenav.style.backgroundColor = "#03657C";
currentTheme = "light";
} else if (currentTheme === "light") {
document.body.style.backgroundImage = 'url("./images/BGdark.png")';
mainnav.style.backgroundColor = "#462c4c";
resultbox.style.backgroundColor = "#462c4ca7";
mobilenav.style.backgroundColor = "#462c4c";
collapse.style.backgroundColor = "#462c4c";
currentTheme = "dark";
}
});
themeButtonMobile.addEventListener("click", () => {
if (currentTheme === "dark") {
document.body.style.backgroundImage = 'url("./images/BGlight.png")';
mainnav.style.backgroundColor = "#03657C";
resultbox.style.backgroundColor = "#03657Ca7";
collapse.style.backgroundColor = "#03657C";
mobilenav.style.backgroundColor = "#03657C";
currentTheme = "light";
} else if (currentTheme === "light") {
document.body.style.backgroundImage = 'url("./images/BGdark.png")';
mainnav.style.backgroundColor = "#462c4c";
resultbox.style.backgroundColor = "#462c4ca7";
mobilenav.style.backgroundColor = "#462c4c";
collapse.style.backgroundColor = "#462c4c";
currentTheme = "dark";
}
});
//------------------Mobile Hamburger-------------------
const hamburger = document.getElementById("hamburger");
hamburger.addEventListener("click", () => {
let top = collapse.style.top;
if (top === "3.2rem") {
collapse.style.top = "-100%";
} else {
collapse.style.top = "3.2rem";
}
});
//-------------------------API---------------------------
//-----------input city---------------
const listofsearchbars = document.getElementsByTagName("form");
const searchbar0 = document.querySelector("#subnav > form > input[type=text]");
const searchbar1 = document.querySelector(
"#landingsection > form > input[type=text]"
);
const carousel = document.querySelector("#carousel");
const landingsection = document.querySelector("#landingsection");
const navsearchbar = document.querySelector("#subnav > .searchbar");
async function fetchdatafrominputsearch1(e) {
e.preventDefault();
const city = searchbar1.value;
try {
const { result, forecastresult } = await getWeather(city);
addWeathertoPage(result, forecastresult);
landingsection.style.display = "none";
navsearchbar.style.visibility = "visible";
lastsearch = city;
} catch (error) {
if (language === "eng") {
searchbar1.value = "Invalid city, try again ;)";
} else {
searchbar1.value = "Città non valida, riprova ;)";
}
console.log("???????????????errorrrrrrrr" + error);
}
setTimeout(() => {
searchbar1.value = "";
}, 1000);
}
async function fetchdatafrominputsearch0(e) {
e.preventDefault();
const city = searchbar0.value;
try {
const { result, forecastresult } = await getWeather(city);
addWeathertoPage(result, forecastresult);
landingsection.style.display = "none";
navsearchbar.style.visibility = "visible";
lastsearch = city;
} catch (error) {
if (language === "eng") {
searchbar0.value = "Invalid city, try again ;)";
} else {
searchbar0.value = "Città non valida, riprova ;)";
}
console.log("???????????????errorrrrrrrr" + error);
}
setTimeout(() => {
searchbar0.value = "";
}, 1000);
}
listofsearchbars[1].addEventListener("submit", fetchdatafrominputsearch1);
listofsearchbars[0].addEventListener("submit", fetchdatafrominputsearch0);
const mobilelogo = document.getElementById("mobilelogo");
const mainlogo = document.getElementById("mainlogo");
const logos = [mainlogo, mobilelogo];
logos.forEach((elem) => {
elem.addEventListener("click", () => {
landingsection.style.display = "flex";
navsearchbar.style.visibility = "hidden";
collapse.style.top = "-100%";
});
});
//-----------fetch weather info------------
const apikey = "586123e42d6868e0ac3177be248417fa";
const url = (city) => {
if (language === "eng") {
return `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apikey}`;
} else {
return `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apikey}&lang=it`;
}
};
const forecast = (city) =>
`https://api.openweathermap.org/data/2.5/forecast?q=${city}&cnt=5&appid=${apikey}`;
const KtoC = (K) => Math.floor(K - 273.15);
async function getWeather(city) {
const response = await fetch(url(city));
const result = await response.json();
const forecastresponse = await fetch(forecast(city));
const forecastresult = await forecastresponse.json();
console.log(result);
console.log(forecastresult);
return { result, forecastresult };
}
//----------------add weather result to page----------------
const resultsection = document.getElementById("resultsection");
const citynamesection = document.querySelector("#resultsection > h1");
const addWeathertoPage = (responsedata, forecastresponsedata) => {
const cityname = responsedata.name;
const weathercondition = responsedata.weather[0].description;
const temp = KtoC(responsedata.main.temp);
const feelsLike = KtoC(responsedata.main.feels_like);
const humidity = responsedata.main.humidity;
const wind = responsedata.wind.speed; // Unit m/s
const visibility = responsedata.visibility / 1000;
citynamesection.innerHTML = cityname;
let newresultbox = document.createElement("div");
newresultbox.id = "resultbox";
if (currentTheme === "light") {
newresultbox.style.backgroundColor = "#03657Ca7";
}
newresultbox.innerHTML = `
<div id="temp">
<h2>${weathercondition}</h2>
<div>${temp}<span>°</span></div>
</div>
<div id="details">
<h2>DETAILS</h2>
<hr />
<table>
<tr>
<td>Feels like:</td>
<td>${feelsLike}<span>°</span></td>
</tr>
<tr>
<td>Humidity:</td>
<td>${humidity}%</td>
</tr>
<tr>
<td>Wind:</td>
<td>${wind}m/s</td>
</tr>
<tr>
<td>Visibility:</td>
<td>${visibility}km</td>
</tr>
</table>
</div>
<div class="col" id="morning">
<h3>Morning</h3>
<div class="visual">
<img src=${chooseicon(
forecastresponsedata.list[0].weather[0].id
)} alt="morning" />
</div>
</div>
<div class="col" id="afternoon">
<h3>Afternoon</h3>
<div class="visual">
<img src=${chooseicon(
forecastresponsedata.list[2].weather[0].id
)} alt="afernoon" />
</div>
</div>
<div class="col" id="night">
<h3>Night</h3>
<div class="visual">
<img src=${chooseicon(
forecastresponsedata.list[4].weather[0].id
)} alt="night" />
</div>
</div>
`;
resultbox.replaceWith(newresultbox);
resultbox = newresultbox;
if (language === "eng") {
changelanguage("eng");
} else {
changelanguage("ita");
}
};
//choose icon
const chooseicon = (weatherid) => {
if (200 <= weatherid && weatherid <= 211) {
return "./icons/Lightning.png";
} else if (212 <= weatherid && weatherid <= 232) {
return "./icons/Thunder.png";
} else if (300 <= weatherid && weatherid <= 321) {
return "./icons/Rainy.png";
} else if (500 <= weatherid && weatherid <= 531) {
return "./icons/Storm.png";
} else if (600 <= weatherid && weatherid <= 622) {
return "./icons/Snowy.png";
} else if (701 <= weatherid && weatherid <= 741) {
return "./icons/Foggy.png";
} else if (751 <= weatherid && weatherid <= 781) {
return "./icons/Windy.png";
} else if (800 == weatherid) {
return "./icons/Sun.png";
} else if (801 <= weatherid && weatherid <= 804) {
return "./icons/Cloudy.png";
}
return "./icons/Cloudy.png";
};
//-----------------------change language-------------------------------------
const langButtonMobile = document.querySelector("#options > li:nth-child(2)");
const langButton = document.getElementById("language");
const langarray = [langButton, langButtonMobile];
let language = "eng";
async function changelanguagefrombutton() {
if (language === "eng") {
changelanguage("ita");
} else {
changelanguage("eng");
}
if (landingsection.style.display === "none") {
const { result, forecastresult } = await getWeather(lastsearch);
addWeathertoPage(result, forecastresult);
}
}
langarray.forEach((toggle) => {
toggle.addEventListener("click", changelanguagefrombutton);
});
changelanguage = (langoption) => {
//textes that change with language
const textspan2 = document.getElementById("span2");
const texttheme = document.querySelector(
"#options > li:nth-child(1) > span:nth-child(2)"
);
const texteng = document.getElementById("eng");
const textita = document.getElementById("ita");
const textprompt = document.querySelector("#landingsection > h1");
const textdetails = document.querySelector("#details > h2");
const textfeelslike = document.querySelector(
"#details > table > tbody > tr:nth-child(1) > td:nth-child(1)"
);
const textwind = document.querySelector(
"#details > table > tbody > tr:nth-child(3) > td:nth-child(1)"
);
const texthumidity = document.querySelector(
"#details > table > tbody > tr:nth-child(2) > td:nth-child(1)"
);
const textvisibility = document.querySelector(
"#details > table > tbody > tr:nth-child(4) > td:nth-child(1)"
);
const textmorning = document.querySelector("#morning > h3");
const textafternoon = document.querySelector("#afternoon > h3");
const textnight = document.querySelector("#night > h3");
if (langoption == "ita") {
textspan2.innerHTML = "ITA";
texttheme.innerHTML = "tema";
texteng.style.fontWeight = "300";
textita.style.fontWeight = "600";
textprompt.innerHTML = "come stai oggi?";
textdetails.innerHTML = "DETTAGLIO";
textfeelslike.innerHTML = "sentito come";
textwind.innerHTML = "vento";
texthumidity.innerHTML = "umidità";
textvisibility.innerHTML = "visibilità";
textmorning.innerHTML = "mattina";
textafternoon.innerHTML = "pomeriggio";
textnight.innerHTML = "notte";
searchbar0.placeholder = "Cerca la tua città...";
searchbar1.placeholder = "Cerca la tua città...";
language = "ita";
} else {
// getWeather(lastsearch);
textspan2.innerHTML = "ENG";
texttheme.innerHTML = "Theme";
texteng.style.fontWeight = "600";
textita.style.fontWeight = "300";
textprompt.innerHTML = "how are you today?";
textdetails.innerHTML = "DETAILS";
textfeelslike.innerHTML = "feels like";
textwind.innerHTML = "wind";
texthumidity.innerHTML = "humidity";
textvisibility.innerHTML = "visibility";
textmorning.innerHTML = "morning";
textafternoon.innerHTML = "afternoon";
textnight.innerHTML = "night";
searchbar0.placeholder = "Search your city...";
searchbar1.placeholder = "Search your city...";
language = "eng";
}
};