-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
137 lines (118 loc) · 4.26 KB
/
Copy pathmain.js
File metadata and controls
137 lines (118 loc) · 4.26 KB
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
import { elementsDOM } from "./elements.js";
import { getWeather } from "./fetch.js";
let dataLocation = [];
let locationList = [];
function handleWeatherData(cityName, bool) {
return getWeather(cityName, bool).then(data => {
dataLocation = data;
return dataLocation;
});
}
function renderWeatherData(cityName) {
handleWeatherData(cityName, true).then(data => {
elementsDOM.currentLocation.textContent = data.name;
const iconUrl = `https://openweathermap.org/img/wn/${data.weather[0].icon}@4x.png`;
elementsDOM.iconWeather.src = iconUrl;
elementsDOM.degreeValue.textContent = `${Math.round(data.main.temp)}°`;
elementsDOM.btnLike.removeAttribute("hidden");
elementsDOM.extraBlock.innerHTML = "";
const feelLike = document.createElement("p");
feelLike.textContent = `Ощущается как: ${Math.round(
data.main.feels_like
)}°`;
elementsDOM.extraBlock.appendChild(feelLike);
const sunrise = document.createElement("p");
sunrise.textContent = `Рассвет: ${convertTime(data.sys.sunrise)}`;
elementsDOM.extraBlock.appendChild(sunrise);
const sunset = document.createElement("p");
sunset.textContent = `Закат: ${convertTime(data.sys.sunset)}`;
elementsDOM.extraBlock.appendChild(sunset);
const line = document.createElement("hr");
line.classList.add("line");
elementsDOM.extraBlock.appendChild(line);
});
renderWeatherCard(cityName);
}
function renderWeatherCard(cityName) {
handleWeatherData(cityName, false).then(data => {
elementsDOM.forecastCard.innerHTML = "";
data.list.map(element => {
const timeZone = document.createElement("p");
timeZone.textContent = convertTime(element.dt);
elementsDOM.forecastCard.appendChild(timeZone);
const tempZone = document.createElement("p");
tempZone.textContent = `Температура: ${Math.floor(element.main.temp)}°`;
elementsDOM.forecastCard.appendChild(tempZone);
const tempZoneFeelLike = document.createElement("p");
tempZoneFeelLike.textContent = `Ощущается как: ${Math.floor(
element.main.feels_like
)}°`;
elementsDOM.forecastCard.appendChild(tempZoneFeelLike);
const icon = document.createElement("img");
icon.classList.add("iconForecast");
const iconForecastUrl = `https://openweathermap.org/img/wn/${element.weather.icon}@4x.png`;
icon.src = iconForecastUrl;
elementsDOM.iconBlock.appendChild(icon);
const line = document.createElement("hr");
line.classList.add("line");
elementsDOM.forecastCard.appendChild(line);
});
});
}
function convertTime(apiTime) {
const timesTampMil = apiTime * 1000;
const date = new Date(timesTampMil);
const hours = ("0" + date.getHours()).slice(-2);
const minutes = ("0" + date.getMinutes()).slice(-2);
return `${hours}:${minutes}`;
}
function addElement(cityName) {
const listItem = document.createElement("li");
const itemName = document.createElement("p");
itemName.textContent = cityName;
const span = document.createElement("span");
span.classList.add("close");
span.innerHTML = "⨯";
listItem.appendChild(itemName);
listItem.appendChild(span);
itemName.addEventListener("click", event => {
if (event.target === itemName) {
const name = event.target.childNodes[0].textContent.trim();
renderWeatherData(name);
}
});
span.addEventListener("click", event => {
const target = event.target;
if (target) {
listItem.remove();
const favoriteCityName = target.previousSibling.textContent;
for (const key in locationList) {
if (Object.prototype.hasOwnProperty.call(locationList, key)) {
const element = locationList[key].name || locationList[key].city.name;
if (element === favoriteCityName) {
locationList.splice(key, 1);
break;
}
}
}
target.remove();
}
});
elementsDOM.favoriteCountry.insertAdjacentElement("beforeend", listItem);
}
function renderFavList() {
elementsDOM.favoriteCountry.innerHTML = "";
locationList.forEach(cityName => {
addElement(cityName.name || cityName.city.name);
});
}
elementsDOM.btnLike.addEventListener("click", cityName => {
cityName = elementsDOM.currentLocation.textContent;
locationList.push(dataLocation);
renderFavList();
});
elementsDOM.form.addEventListener("submit", event => {
event.preventDefault();
renderWeatherData(elementsDOM.input.value.trim());
elementsDOM.form.reset();
});