-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (144 loc) · 4.83 KB
/
index.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
// Live time and date
let now = new Date();
console.log(now.getDate());
let liveTime = document.querySelector(".liveTime");
let date = now.getDate();
let hours = now.getHours();
if (hours < 10) {
hours = `0${hours}`;
}
let minutes = now.getMinutes();
if (minutes < 10) {
minutes = `0${minutes}`;
}
let months = [
"Jan",
"Feb",
"March",
"April",
"May",
"June",
"July",
"Aug",
"Sept",
"Oct",
"Nov",
"Dec",
];
let month = months[now.getMonth()];
liveTime.innerHTML = `${month} ${date}, ${hours}:${minutes}`;
// format day
function formatDay(timestamp) {
let date = new Date(timestamp * 1000);
let day = date.getDay();
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
return days[day];
}
// Forecast
function displayForecast(response) {
console.log(response);
let forecast = response.data.daily;
let forecastElement = document.querySelector("#forecast");
let forecastHTML = `<div class="row">`;
forecast.forEach(function (forecastDay, index) {
if (index < 6) {
forecastHTML =
forecastHTML +
`
<div class="col-2">
<div class="weather-forecast-date">${formatDay(forecastDay.dt)}</div>
<img alt="" src="http://openweathermap.org/img/wn/${
forecastDay.weather[0].icon
}@2x.png" class="w-100" />
<div class="weather-forecast-temperatures"> <span class="weather-forecast-temperature-maximum"> ${Math.round(
forecastDay.temp.max
)}°</span> <span class="weather-forecast-temperature-minimum"> ${Math.round(
forecastDay.temp.min
)}°</span> </div>
</div>`;
}
});
forecastHTML = forecastHTML + `</div>`;
forecastElement.innerHTML = forecastHTML;
}
// Forecast coordinates
function getForecast(coordinates) {
console.log(coordinates);
let apiKey = "5201594abea9f3e38b70e65b11a80c24";
let apiUrl = `https://api.openweathermap.org/data/2.5/onecall?lat=${coordinates.lat}&lon=${coordinates.lon}&appid=${apiKey}&units=metric`;
axios.get(apiUrl).then(displayForecast);
}
// current weather search
function currentWeather(response) {
console.log(response);
let city = document.querySelector("#cityName");
let temperature = document.querySelector("#temp");
celsiusTemperature = response.data.main.temp;
city.innerHTML = response.data.name;
temperature.innerHTML = Math.round(response.data.main.temp);
document.querySelector("#description").innerHTML =
response.data.weather[0].main;
document.querySelector("#humidity").innerHTML = response.data.main.humidity;
document.querySelector("#wind").innerHTML = Math.round(
response.data.wind.speed
);
// weather icon
let iconElement = document.querySelector("#main-icon");
iconElement.setAttribute(
"src",
`http://openweathermap.org/img/wn/${response.data.weather[0].icon}@2x.png`
);
// api forecast
getForecast(response.data.coord);
}
// city
function search(city) {
let apiKey = "5201594abea9f3e38b70e65b11a80c24";
let apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
axios.get(apiUrl).then(currentWeather);
}
function submit(event) {
event.preventDefault();
let city = document.querySelector("#search-text-input").value;
search(city);
}
function searchLocation(position) {
let apiKey = "5201594abea9f3e38b70e65b11a80c24";
let lat = position.coords.latitude;
let lon = position.coords.longitude;
let apiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}&units=metric`;
axios.get(apiUrl).then(currentWeather);
}
function CurrentLocation(event) {
event.preventDefault();
navigator.geolocation.getCurrentPosition(searchLocation);
}
//Celsius and Farenheit conversion
function temperatureCelsius(event) {
event.preventDefault();
celsiusLink.classList.add("active");
farenheitLink.classList.remove("active");
let temperatureElement = document.querySelector("#temp");
temperatureElement.innerHTML = Math.round(celsiusTemperature);
}
let celsiusLink = document.querySelector("#celsius-link");
celsiusLink.addEventListener("click", temperatureCelsius);
function temperatureFarenheit(event) {
event.preventDefault();
let temperatureElement = document.querySelector("#temp");
celsiusLink.classList.remove("active");
farenheitLink.classList.add("active");
let temperature = temperatureElement.innerHTML;
temperature = Number(temperature);
temperatureElement.innerHTML = Math.round((celsiusTemperature * 9) / 5 + 32);
}
let farenheitLink = document.querySelector("#farenheit-link");
farenheitLink.addEventListener("click", temperatureFarenheit);
let celsiusTemperature = null;
let form = document.querySelector("#search-form");
form.addEventListener("submit", submit);
let current = document.querySelector("#exactLocation");
current.addEventListener("click", CurrentLocation);
// search city
search("Athens");
// display forecast