-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
214 lines (196 loc) · 9.97 KB
/
script.js
File metadata and controls
214 lines (196 loc) · 9.97 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
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
$(document).ready(function () {
//time(by list #) that call is made on the 5 day forecast api
//var instance = -1;
var i = 0;
var b = -1;
var lat = null;
var lon = null;
var newCity = null;
var searchedCity = null;
var cityPlacement = false;
/* sidenav opener */
function openNav() {
document.getElementById("searchSidenav").style.width = "400px";
document.getElementById("main").style.marginLeft = "400px";
}
/* sidenav closer */
function closeNav() {
document.getElementById("searchSidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
}
// (predetermined city) current weather response with uv index response nested inside
function predeterminedCurrentCall(location) {
$.ajax({
url: "https://api.openweathermap.org/data/2.5/weather?q=" + location + "&units=imperial&appid=715ee435d9e6cc809bc1cb6b62581405",
method: "GET"
}).then(function (response) {
$("#cityName h2").html(response.name);
$("#currentForecast .card-text:first").append("<span> Time: </span>" + response.dt + " UTC (Unix time) <img src='https://openweathermap.org/img/wn/" + response.weather[0].icon + "@2x.png'><br>");
$("#currentForecast .card-text:first").append("<span> Temperature: </span>" + response.main.temp + "℉<br>");
$("#currentForecast .card-text:first").append("<span> Temp feels like: </span>" + response.main.feels_like + "℉<br>");
$("#currentForecast .card-text:first").append("<span> Humidity: </span>" + response.main.humidity + "%<br>");
$("#currentForecast .card-text:first").append("<span> Weather Condition: </span>" + response.weather[0].main + ", " + response.weather[0].description + "<br>");
$("#currentForecast .card-text:first").append("<span> Windspead & Direction: </span>" + response.wind.speed + " mph" + " at " + response.wind.deg + "°<br>");
//using the current weather response to get lat and lon, inorder to get UV index response
var queryURL2 = "https://api.openweathermap.org/data/2.5/uvi?lat=" + response.coord.lat + "&lon=" + response.coord.lon + "&appid=715ee435d9e6cc809bc1cb6b62581405";
$.ajax({
url: queryURL2,
method: "GET"
}).then(function (response) {
$("#currentForecast .card-text:first").append("<span> UV Index: </span> <span class='uvIndex'>" + response.value + "</span> <br>");
if (response.value < 3) {
$(".uvIndex").css("background-color", "green");
}
if (response.value >= 3 && response.value < 6) {
$(".uvIndex").css("background-color", "yellow");
}
if (response.value >= 6) {
$(".uvIndex").css("background-color", "red");
}
//closes ajax.then call
});
//closes parent ajax.then call
});
//closes predeterminedCurrentCall function
};
// (predetermined city)5 day forecast
function predetermined5DayCall(location) {
$.ajax({
url: "https://api.openweathermap.org/data/2.5/forecast?q=" + location + "&units=imperial&appid=715ee435d9e6cc809bc1cb6b62581405",
method: "GET"
}).then(function (response) {
//get placement (#day+i)id to match list i
for (b = -1, i = 0; i < response.list.length; i++) {
if (response.list[i].dt_txt.indexOf("15:00:00") !== -1) {
b++;
$("#day" + b + " .card-text").append("<span>Time:</span> " + response.list[i].dt_txt + " <img src='https://openweathermap.org/img/wn/" + response.list[i].weather[0].icon + "@2x.png'> <br>");
$("#day" + b + " .card-text").append("<span>Temp:</span> " + response.list[i].main.temp + "℉ <br>");
$("#day" + b + " .card-text").append("<span>Humidity:</span> " + response.list[i].main.humidity + "% <br>");
$("#day" + b + " .card-text").append("<span>Weather Condition:</span> " + response.list[i].weather[0].main + " <br>");
};
};
});
};
// (searched city) current weather response with uv index response nested inside
function variableCityCurrentCall(location) {
var queryURL = "https://api.openweathermap.org/data/2.5/weather?q=" + location + "&units=imperial&appid=715ee435d9e6cc809bc1cb6b62581405";
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
$("#cityName h2").html(response.name);
$("#currentForecast .card-text:first").append("<span> Time: </span>" + response.dt + " UTC (Unix time) <img src='https://openweathermap.org/img/wn/" + response.weather[0].icon + "@2x.png'><br>");
$("#currentForecast .card-text:first").append("<span> Temperature: </span>" + response.main.temp + "℉<br>");
$("#currentForecast .card-text:first").append("<span> Temp feels like: </span>" + response.main.feels_like + "℉<br>");
$("#currentForecast .card-text:first").append("<span> Humidity: </span>" + response.main.humidity + "%<br>");
$("#currentForecast .card-text:first").append("<span> Weather Condition: </span>" + response.weather[0].main + ", " + response.weather[0].description + "<br>");
$("#currentForecast .card-text:first").append("<span> Windspead & Direction: </span>" + response.wind.speed + " mph" + " at " + response.wind.deg + "°<br>");
//using the current weather response to get lat and lon, inorder to get UV index response
var queryURL2 = "https://api.openweathermap.org/data/2.5/uvi?lat=" + response.coord.lat + "&lon=" + response.coord.lon + "&appid=715ee435d9e6cc809bc1cb6b62581405";
$.ajax({
url: queryURL2,
method: "GET"
}).then(function (response) {
$("#currentForecast .card-text:first").append("<span> UV Index: </span> <div class='uvIndex'>" + response.value + "</div> <br>");
});
//closes parent ajax call
});
//closes variableCityCurrentCall()
}
// (searched city) 5day weather response
function variableCity5DayCall(location) {
var queryURL = "https://api.openweathermap.org/data/2.5/forecast?q=" + location + "&units=imperial&appid=715ee435d9e6cc809bc1cb6b62581405";
//var instance = -1;
var i = 0;
$.ajax({
url: queryURL,
method: "GET"
}).then(function (response) {
for (b = -1, i = 0; i < response.list.length; i++) {
if (response.list[i].dt_txt.indexOf("15:00:00") !== -1) {
b++;
$("#day" + b + " .card-text").append("<span>Time:</span> " + response.list[i].dt_txt + " <img src='https://openweathermap.org/img/wn/" + response.list[i].weather[0].icon + "@2x.png'> <br>");
$("#day" + b + " .card-text").append("<span>Temp:</span> " + response.list[i].main.temp + "℉ <br>");
$("#day" + b + " .card-text").append("<span>Humidity:</span> " + response.list[i].main.humidity + "% <br>");
$("#day" + b + " .card-text").append("<span>Weather Condition:</span> " + response.list[i].weather[0].main + " <br>");
};
};
});
//closes variableCity5DayCall()
}
//statement for users first time visiting site and undefined citie in localstorage
//future development should be to try to refactor this part of the code. This section of code makes 4 ajax calls instead of 2 in some cases.
predeterminedCurrentCall("hartford,connecticut");
predetermined5DayCall("hartford,connecticut");
//for users who return to the site
if (localStorage.length > 0 && cityPlacement == false) {
predeterminedCurrentCall(localStorage[localStorage.length]);
predetermined5DayCall(localStorage[localStorage.length]);
}
// }else{
// predeterminedCurrentCall("hartford,connecticut");
// predetermined5DayCall("hartford,connecticut");
//
// }
//event listener to have a new api call using previously searched city
$("body").on("click", ".historyBTN", function () {
searchedCity = $(this).html();
$("#cityName h2").empty();
//returns css properties to pre-ajax_error settings
$("#cityName").css({ "width": "20%", "background-color": "grey", "color": "black" });
$("#currentForecast .card-text:first").empty();
for (i = 0; i < 5; i++) {
$("#day" + i + " .card-text").empty();
};
variableCityCurrentCall(searchedCity);
//displays ajax error as "location undefined"
$(document).ajaxError(function () {
$("#cityName h2").html("LOCATION UNDEFINED");
$("#cityName").css({ "width": "80%", "background-color": "black", "color": "red" });
cityPlacement = true;
});
variableCity5DayCall(searchedCity);
});
//event listener to open sidenav
$("#searchBTN").on("click", function () {
openNav();
//creating list of searched cities
for (i = 1; i <= localStorage.length; i++) {
$("#searchedCities").prepend("<button class='glow-on-hover historyBTN'>" + localStorage[i] + "</button><br>");
};
});
//event listener when city is entered
$("form").submit(function (event) {
event.preventDefault();
$("#cityName h2").empty();
//returns css properties to pre-ajax_error settings
$("#cityName").css({ "width": "20%", "background-color": "grey", "color": "black" });
$("#currentForecast .card-text:first").empty();
//clean code with all the i and b variables
for (i = 0; i < 5; i++) {
$("#day" + i + " .card-text").empty();
};
newCity = $("input").val().trim();
localStorage.setItem(localStorage.length + 1, newCity);
$("#searchedCities").prepend("<button class='glow-on-hover historyBTN'>" + localStorage[localStorage.length] + "</button><br>");
$("input").val("");
variableCityCurrentCall(newCity);
//displays ajax error as "location undefined"
$(document).ajaxError(function () {
$("#cityName h2").html("LOCATION UNDEFINED");
$("#cityName").css({ "width": "80%", "background-color": "black", "color": "red" });
cityPlacement = true;
});
variableCity5DayCall(newCity);
});
$("#clearHistory").on("click", function () {
localStorage.clear();
$("#searchedCities").empty();
//or location.reload();
})
//event listener to close sidenav
$(".closebtn").on("click", function () {
closeNav()
$("#searchedCities").empty();
});
});