-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
177 lines (133 loc) · 4.62 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
//check if previous file already existed in local storage
var usersch = JSON.parse(localStorage.getItem("userhx")) || [];
var city = 'adelaide';
var apikey ='5e799cbc4834793851ed4eb3fbe95228';
var key = "0959a518fd93cebc53ed34118e0e9471";
$(document).ready(function(){
var cday = moment().format('dddd, Do MMM YYYY');
$('#currentDay').text(cday);
getinfo(city);
forcast(city);
displayhx(usersch);
$('#searchbtn').on('click',function(){
event.preventDefault();
var newcity = $('#userentry').val();
if(newcity==""){
console.log('empty')
$('#mymodal').modal('show')
}else{
getinfo(newcity);
document.getElementById('start').innerHTML=""
forcast(newcity);
hx();
}
})
$('.hxbtn').on('click',function(){
var btnclass = $(this).html();
getinfo(btnclass);
document.getElementById('start').innerHTML=""
forcast(btnclass);
})
})
$('#refreshBtn').on('click',function() {
localStorage.clear('userhx');
location.reload();
})
//ajax get weather info via API
function getinfo(location){
var queryURL = 'https://api.openweathermap.org/data/2.5/weather?q='+location+'&units=metric&appid='+apikey
$.ajax({
url: queryURL,
method: "GET"
}).then(function(response) {
var info = response;
displayinfo(info)
var lon = info.coord.lon
var lat = info.coord.lat
uvi(lat,lon)
});
}
function uvi(lat, lon){
var queryURL = 'https://api.openweathermap.org/data/2.5/uvi?appid='+apikey+'&lat='+lat+'&lon='+lon;
$.ajax({
url: queryURL,
method: "GET"
}).then(function(response) {
var uvinfo = response;
displayuv(uvinfo);
});
}
function forcast(location){
var queryURL = 'https://api.openweathermap.org/data/2.5/forecast?q='+location+'&units=metric&appid='+apikey;
$.ajax({
url: queryURL,
method: "GET"
}).then(function(response) {
var foreinfo = response;
displayforecast(foreinfo);
})
}
function displayinfo(obj){
$('#currentCity').text(obj.name);
var x = Math.round(obj.main.temp)
$('#temp').text(x + ' °C');
var y = obj.weather[0].description;
var z = obj.weather[0].icon;
var iconurl = 'http://openweathermap.org/img/wn/'+z+'.png';
$('#desc').text(capitalisefst(y));//obj.weather[0].main+" "+
$('#wicon').attr('src',iconurl)
$('#humid').text(obj.main.humidity + ' %');
$('#wind').text(obj.wind.speed + ' m/s');
}
function displayuv(obj){
$('#uvi').text(obj.value);
var q = Math.round(obj.value);
var r = $('#uvi');
var s = $('#uvid');
if(q>=11){r.css('background-color','purple');s.text('Extreme')}
else if(q<=10 && q>=8){r.css('background-color','red');s.text('Very High')}
else if(q<=7 && q>=6){r.css('background-color','orange');s.text('High')}
else if(q<=5 && q>=3){r.css('background-color','yellow');s.text('Moderate')}
else if(q<=2 && q>=1){r.css('background-color','green');s.text('Low')}
}
function displayforecast(obj){
for(i=0;i<5;i++){
var frarr = (i*8+7);
var fdate = obj.list[frarr].dt;
var dateString = moment.unix(fdate).format('dddd, Do MMM YYYY');
var ftemp = Math.round(obj.list[frarr].main.temp);
var ficon = obj.list[frarr].weather[0].icon;
var ficonurl = 'http://openweathermap.org/img/wn/'+ficon+'.png';
var fhumid = obj.list[frarr].main.humidity;
var fblock = $('<div class="card fblock m-2">');
var finfo = $('<div class="text-white p-2">').text(dateString);
var fdicon = $('<img src='+ficonurl+' alt="weather icon">');
var fdtemp = $('<div class="text-white">').text("Temp: "+ftemp+" °C");
var fdhum = $('<div class="text-white">').text("Humidity: "+fhumid+" %");
$('#start').append(fblock);
fblock.append(finfo);
finfo.append(fdtemp);
fdtemp.append(fdhum);
fdhum.append(fdicon);
}
}
function hx(){
const usrhx = $('#userentry').val();
//usersch.push(usrhx);
usersch.splice(0,0,usrhx);
usersch.splice(5)
localStorage.setItem("userhx",JSON.stringify(usersch));
displayhx(usersch);
$('#userentry').val("")
}
function displayhx(usersch){
$('#history').html("");
for(j=0;j<usersch.length;j++){
var scrhx = $('<button type="button" class="hxbtn btn-block fblock text-white">').text(capitalisefst(usersch[j]));
$('#history').append(scrhx);
}
}
//Capitalise 1st letter
function capitalisefst(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}