-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
165 lines (144 loc) · 4.67 KB
/
script.js
File metadata and controls
165 lines (144 loc) · 4.67 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
//DOM & declairs=================================================================
var searchCityInput = $("#search-city-input");
var searchBtn = $("#search-btn");
var cityValue;
var currentCity = $("#current-city");
var currentDate = $("#current-date");
var cityList = [];
//get JASON from local storage
jsonCall();
function jsonCall() {
var storage = JSON.parse(localStorage.getItem("cities"));
if (storage !== null) {
cityList = storage;
}
renderButtons();
}
//Start here======================================================================
searchBtn.on("click", function () {
// console.log("searchBtn clicked");
cityValue = searchCityInput.val();
cityList.push(cityValue);
localStorage.setItem("cities", JSON.stringify(cityList));
currentCondition();
renderButtons();
});
$("#clearBtn").on("click", function () {
localStorage.removeItem("cities");
cityList = [];
$("#search-history").empty();
});
function currentCondition() {
var APIkey = "444a2add20a5be5b1aa0fd99ae23639f";
var queryURL =
"https://api.openweathermap.org/data/2.5/weather?q=" +
cityValue +
"&appid=" +
APIkey +
"&units=imperial"; //metric,imperial
$.ajax({
url: queryURL,
method: "GET",
}).then(function (response) {
// console.log(response);
currentCity.text(response.name + ", " + response.sys.country);
currentDate.text(moment().format("LLL"));
//get weather icon
var iconNum = response.weather[0].icon;
var iconURL = "https://openweathermap.org/img/wn/" + iconNum + "@2x.png";
$("#iconToday").attr("src", iconURL);
$("#iconToday").attr("alt", "weather-icon");
$("#temperature").text(response.main.temp.toFixed(1) + "°F");
$("#humidity").text(response.main.humidity + "%");
$("#windSpeed").text(response.wind.speed.toFixed(1) + "mph");
//UV index
var uvlat = response.coord.lat;
var uvlon = response.coord.lon;
var uvURL =
"https://api.openweathermap.org/data/2.5/uvi?appid=" +
APIkey +
"&lat=" +
uvlat +
"&lon=" +
uvlon;
$.ajax({
url: uvURL,
method: "GET",
}).then(function (res) {
var uvShisu = res.value.toFixed(0);
$("#uvIndex").text(uvShisu);
console.log(uvShisu);
//uv index color
if (uvShisu < 3) {
$("#uv-color").removeClass().addClass("list-group-item uvL");
} else if (3 <= uvShisu && uvShisu <= 5) {
$("#uv-color").removeClass().addClass("list-group-item uvM");
} else if (6 <= uvShisu && uvShisu <= 7) {
$("#uv-color").removeClass().addClass("list-group-item uvH");
} else if (8 <= uvShisu && uvShisu <= 10) {
$("#uv-color").removeClass().addClass("list-group-item uvVH");
} else if (11 <= uvShisu) {
$("#uv-color").removeClass().addClass("list-group-item uvEX");
}
});
});
fiveDayForecast();
}
function fiveDayForecast() {
console.log("This is 5day forecast");
$("#fivedayforecast").empty(); //testing ok
var APIkey = "444a2add20a5be5b1aa0fd99ae23639f";
var queryURL =
"https://api.openweathermap.org/data/2.5/forecast?q=" +
cityValue +
"&appid=" +
APIkey +
"&units=imperial";
$.ajax({
url: queryURL,
method: "GET",
}).then(function (response) {
var startIndex = Math.floor(new Date().getHours() / 3);
// console.log(response);
for (let i = startIndex; i < response.list.length; i += 8) {
var newCard = $(
'<div id="forecast" class="card-body mx-2 my-2 bg-info text-white">'
);
var dt =
response.list[i].dt_txt.split(" ")[0].split("-")[1] +
"-" +
response.list[i].dt_txt.split(" ")[0].split("-")[2];
var newDate = $("<h5>").text(dt);
var newTemp = $("<p>").text(
"Tempature: " + response.list[i].main.temp + "°F"
);
var newHumid = $("<p>").text(
"Humidity: " + response.list[i].main.humidity + "%"
);
//get weather icon
var iconNum = response.list[i].weather[0].icon;
var iconURL = "https://openweathermap.org/img/wn/" + iconNum + "@2x.png";
var newIcon = $('<img id="newIcon">').attr("src", iconURL);
$("#iconToday").attr("alt", "weather-icon");
newCard.append(newDate, newIcon, newTemp, newHumid);
$("#fivedayforecast").append(newCard);
}
});
}
function renderButtons() {
$("#search-history").empty();
for (var i = 0; i < cityList.length; i++) {
var a = $('<button type="button" class="clbtn btn btn-light ">');
a.text(cityList[i]);
$("#search-history").prepend(a);
}
listClick();
}
function listClick() {
$(".clbtn").on("click", function (event) {
event.preventDefault();
cityValue = $(this).text();
currentCondition();
renderButtons();
});
}