-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.html
277 lines (236 loc) · 12.4 KB
/
weather.html
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<link type="text/css" rel="stylesheet" href="resources/css/weather.css">
<link type="text/css" rel="stylesheet" href="resources/css/weather-icons.css">
<link type="text/css" rel="stylesheet" href="resources/css/weather-icons-wind.css">
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans">
<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js" integrity="sha256-0YPKAwZP7Mp3ALMRVB2i8GXeEndvCq3eSl/WsAl1Ryk=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/typeit/4.4.0/typeit.js"></script>
</head>
<body>
<div class="container-fluid h-100">
<div class="row main-row align-items-center h-100">
<div class="col-sm-2 col-lg-4 col-1 main-cont-shrink"></div>
<div class="col-sm-8 col-lg-4 col-10 main-container">
<div class="row row-change1">
<div class="col-12 gif-container">
<img class="gif-cat img-responsive" src="resources/images/weather-cat.gif">
</div>
<div class="col-12 align-self-end align-middle text-container">
<p class="text"></p>
</div>
</div>
</div>
<div class="col-sm-2 col-lg-4 col-1 main-cont-shrink"></div>
</div>
</div>
</body>
<script>
//Updated in manageGeolocation with the json response from the weather api
var weatherResponse;
//function to manage the initial interface and geolocation
function manageGeolocation(callback){
//Check if the user has already been on the page, accepted the geolocation request or not
function checkAuthorizedGeolocation(){
getLocation();
if(typeof localStorage["authorizedGeolocation"] == "undefined"){
$(".text").typeIt({
speed: 30,
cursor: false,
// callback: function(){
// getLocation();
// }
})
.tiType("Meow! ")
.tiPause(1000)
.tiBreak()
.tiType("Please, share your location with us. Oh, and don't be afraid... ")
.tiPause(750)
.tiType("cats are very good at keeping secrets! :3")
}else if(localStorage["authorizedGeolocation"] == 0){
// getLocation();
}else if(localStorage["authorizedGeolocation"] == 1){
$(".text").typeIt({
speed: 30,
cursor: false,
// callback: function(){
// getLocation();
// }
})
.tiType("Meow! ")
.tiPause(1000)
.tiBreak()
.tiType("Welcome back. We have thousands of cats tracking you, the wait will be short!")
}
}
//Geolocation management
function getLocation() {
var options = {
enableHighAccuracy: true,
timeout : 10000,
maximumAge: 0
}
navigator.geolocation.getCurrentPosition(sendLocation, geoError, options);
}
//geolocation callback if successfull
function sendLocation(pos) {
var latitude = pos.coords.latitude;
var longitude = pos.coords.longitude;
localStorage["authorizedGeolocation"] = 1;
$.get("https://fcc-weather-api.glitch.me/api/current?lat=" + latitude + "&lon=" + longitude, function(data, status){
weatherResponse = data;
callback();
});
}
//geolocation error handling if request not successfull
function geoError(err) {
console.log(err);
if(err.code == 1) {
localStorage["authorizedGeolocation"] = 0;
$(".text").typeIt({
speed: 30,
cursor: false,
})
.tiDelete()
.tiType("Meouuw! :( ")
.tiBreak()
.tiType("We are a bit sad you haven't accepted to share your location.")
.tiBreak()
.tiType("But still, if you change your mind, follow the instructions in this link: ")
.tiType("<a href='https://support.google.com/chrome/answer/142065?hl=en'>Google instructions to share location</a>")
} else if (err == 0 || err == 2 || err == 3) {
$(".text").typeIt({
speed: 30,
cursor: false,
})
.tiDelete()
.tiType("There has been an error.")
.tiBreak()
.tiType("Please, refresh the page to accept the Geo Location request!")
}
}
checkAuthorizedGeolocation();
}
//function to manage the weather report page
function manageWeather(){
//fade-out effect from loading interface
$(".gif-container").animate({
opacity: 0
}, 1000, function(){
$(this).remove();
});
$(".text-container").animate({
opacity: 0
}, 1000, function(){
$(this).remove(resizeMainContainer());
});
//jQueryUI function to animate the main-container resize
function resizeMainContainer(){
$(".main-container").animate({
height: "80%"
}, function(){
//Change main-container to flex display, and then make the main-cont-shrink smaller and the main-container bigger for Laptop and pc
$(".main-container").css("display", "flex");
$(".main-cont-shrink").switchClass("col-lg-4", "col-lg-2");
$(".main-container").switchClass("col-lg-4", "col-lg-8", function(){
addElements();
});
});
}
//adding columns to row
function addElements(){
$(".row-change1").append("<div class='col-4 order-2 col-sm-4 order-sm-1 col-md-4 order-md-2 col-lg-4 order-lg-1 weaimage'></div>")
.append("<div class='col-12 order-1 col-sm-8 order-sm-2 col-md-12 order-md-1 col-lg-8 order-lg-2 weatherText'></div>")
//add weather-cat images to col-3
$(".weaimage").append("<img class='cat' src='resources/images/weather-catface.png'>")
//Time management to both increase the time each second and to assign time and day month and year to divs
var localTime = new Date();
$(".weatherText").append("<p class='date'>" + localTime.toUTCString().slice(0, 16) +"</p>");
$(".weatherText").append("<p class='time'>" + localTime.toLocaleTimeString() +"</p>");
var timeNeverStops = setInterval(function(){
localTime.setSeconds(localTime.getSeconds() + 1);
$(".time").text(localTime.toLocaleTimeString());
}, 1000);
//writing location for jSON for country and neighbourhood
$(".weatherText").append("<p class='location'>" + weatherResponse.name + ", " + weatherResponse.sys.country + "</p>");
//Set the icon for the weather, depending on the weatherResponse.weather[0].main and whether it's day or night
if(localTime.getTime() >= (weatherResponse.sys.sunrise * 1000) && localTime.getTime() <= (weatherResponse.sys.sunset * 1000)) {
switch(weatherResponse.weather[0].main) {
case 'Drizzle':
$(".weatherText").append("<i class=\"wi wi-day-sprinkle\"></i>");
break;
case 'Haze' :
$(".weatherText").append("<i class=\"wi wi-day-haze\"></i>");
break;
case 'Mist' :
$(".weatherText").append("<i class=\"wi wi-day-fog\"></i>");
break;
case 'Clouds':
$(".weatherText").append("<i class=\"wi wi-day-cloudy\"></i>");
break;
case 'Rain':
$(".weatherText").append("<i class=\"wi wi-day-rain\"></i>");
break;
case 'Snow':
$(".weatherText").append("<i class=\"wi wi-day-snow\"></i>");
break;
case 'Clear':
$(".weatherText").append("<i class=\"wi wi-day-sunny\"></i>");
break;
case 'Thunderstom':
$(".weatherText").append("<i class=\"wi wi-day-thunderstorm\"></i>");
break;
}
} else {
switch(weatherResponse.weather[0].main) {
case 'Drizzle':
$(".weatherText").append("<i class=\"wi wi-night-sprinkle\"></i>");
break;
case 'Haze' :
$(".weatherText").append("<i class=\"wi wi-night-haze\"></i>");
break;
case 'Mist' :
$(".weatherText").append("<i class=\"wi wi-night-fog\"></i>");
break;
case 'Clouds':
$(".weatherText").append("<i class=\"wi wi-night-cloudy\"></i>");
break;
case 'Rain':
$(".weatherText").append("<i class=\"wi wi-night-rain\"></i>");
break;
case 'Snow':
$(".weatherText").append("<i class=\"wi wi-night-snow\"></i>");
break;
case 'Clear':
$(".weatherText").append("<i class=\"wi wi-night-clear\"></i>");
break;
case 'Thunderstom':
$(".weatherText").append("<i class=\"wi wi-night-thunderstorm\"></i>");
break;
}
}
$(".weatherText").append("<p class='weather'>" + weatherResponse.weather[0].main + "</p>");
//Temperature and farenheit/celsius toggle management
var celsius = Math.round(weatherResponse.main.temp);
var farenheit = Math.round(celsius * (9/5) + 32);
$(".weatherText").append("<p class='temperature wea'>" + Math.round(celsius) + "<a class='temperatureToggle' href='#'> <span class='degrees'>°</span><span class='tempType'>C</span></a></p>");
$(".temperatureToggle").on("click", function(){
if($(".tempType").text() == "C") {
$(".temperature").contents()[0].nodeValue = farenheit;
$(".tempType").text("F");
} else {
$(".temperature").contents()[0].nodeValue = celsius;
$(".tempType").text("C");
}
});
}
}
//manageWeather();
manageGeolocation(manageWeather);
</script>
</html>