-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
453 lines (390 loc) · 11.3 KB
/
index.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FCC Weather Cast</title>
<script src="https://code.jquery.com/jquery-2.2.0.min.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="https://cdn.rawgit.com/erikflowers/weather-icons/master/css/weather-icons.min.css" type="text/css" media="all" />
<style type="text/css" media="all">
@import url(https://fonts.googleapis.com/css?family=Open+Sans:300,400,600);
html {
height: 100%;
overflow: hidden;
}
*,
*:before,
*:after {
box-sizing: border-box;
}
body {
background: no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
font-family: 'Open sans', sans-serif;
font-size: 16px;
padding: 2em;
}
#card {
left: 10%;
position: absolute;
top: 20%;
}
.card {
-webkit-box-shadow: 0 8px 8px rgba(0, 0, 0, 0.5);
-moz-box-shadow: 0 8px 8px rgba(0, 0, 0, 0.5);
box-shadow: 0 8px 8px rgba(0, 0, 0, 0.5);
border-radius: 3px;
color: #fff;
height: 430px;
padding: 4em;
position: relative;
text-align: center;
width: 300px;
}
.card .settings {
cursor: pointer;
display: block;
position: absolute;
top: -5px !important;
right: 15px !important;
width: 20px;
}
.card .settings .point {
display: block;
font-size: 30px;
height: 10px;
margin: auto;
}
.card.chill {
background-color: #5BC0DE;
}
.card.ideal {
background-color: #5CB85C;
}
.card.warm {
background-color: #F0AD4E;
}
.card .city {
margin-top: 25px;
}
.card .city,
.card .temp {
font-size: 2em;
}
.card .weather {
display: block;
font-size: 5em;
margin: 0.5em 0;
}
.card .temp {
font-style: normal;
}
.loading {
background: no-repeat center center rgba(255,255,255,0.50);
background-image: url("http://blog.teamtreehouse.com/wp-content/uploads/2015/05/InternetSlowdown_Day.gif");
background-image: url("https://themarketingoak.files.wordpress.com/2015/07/circle-loading-animation.gif");
height:100%;
width:100%;
position:fixed;
left:0;
top:0;
z-index:1 !important;
}
</style>
</head>
<body>
<div id="card">
<div class="front">
<div class="card">
<span class="settings">
<i class="point">.</i>
<i class="point">.</i>
<i class="point">.</i>
</span>
<h2 class="city"></h2>
<i class="weather wi"></i>
<em class="temp">
<span class="degree"></span>
<i id="metrics" class="wi wi-celsius"></i>
</em>
</div>
</div><!--/.front-->
<div class="back">
<div class="card">
<span class="settings">
<i class="point">.</i>
<i class="point">.</i>
<i class="point">.</i>
</span>
<form id="optForm">
<fieldset>
<legend>
<i class="wi wi-thermometer"></i>
</legend>
<i class="wi wi-celsius"></i> <input checked type="radio" name="tempMetrics" value="°C"/>
<i class="wi wi-fahrenheit"></i> <input type="radio" name="tempMetrics" value="°F"/>
</fieldset>
</form>
</div>
</div><!--/.back-->
</div>
<div class="loading"></div>
<script src="https://cdn.rawgit.com/nnattawat/flip/v1.0.19/dist/jquery.flip.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
(function($){
var WeatherApp = (function() {
var App = {
"apiKey": {
"flickr": "28602178605addc1a7730e3c90733b22",
"openWeather": "44db6a862fba0b067b1930da0d769e98"
},
"apiUrl": {
"flickr": "https://api.flickr.com/services/rest/?method=flickr.photos.search",
"openWeather": "http://api.openweathermap.org/data/2.5/weather?"
},
"units": "metric",
"$container": $('body'),
"$card": $('.card'),
"$city": $('.city'),
"$weather": $('.weather'),
"$temp": $('.temp .degree'),
"$loader": $('.loading'),
"icons": {
"01d": "wi-day-sunny",
"01n": "wi-night-clear",
"02d": "wi-day-cloudy",
"02n": "wi-night-alt-cloudy",
"03d": "wi-cloud",
"03n": "wi-cloud",
"04d": "wi-cloudy",
"04n": "wi-cloudy",
"09d": "wi-rain",
"09n": "wi-rain",
"10d": "wi-day-rain",
"10n": "wi-night-alt-rain",
"11n": "wi-day-thunderstorm",
"11d": "wi-day-thunderstorm",
"13n": "wi-snow",
"13d": "wi-snow",
"50d": "wi-fog",
"50n": "wi-fog"
}
};
/**
* function doAjaxCall
* Makes an ajax call
* @param {String}
* @param {function}
* @param {function} Optional
*/
function doAjaxCall(url, cb, promiseDoneCb) {
var promiseDoneCb = promiseDoneCb || function() {console.log('Ajax call terminate')};
$.get(url, cb,"jsonp").done(promiseDoneCb);
};
/**
* function getWeather
* Makes an ajax call to OpenWeatherAPI
* @param {JSON Object}
*/
function getWeather(locationData) {
// Extrapolate the latitude e longitude
var lat = locationData.loc.split(",")[0],
lon = locationData.loc.split(",")[1],
url = getOpenWeatherApiUrl(lat, lon);
doAjaxCall(url, collectWeatherData);
};
/**
* function collectWeatherData
* Collect data about weather user location, pass them to renderView()
* and get the background photo for the application container
* @param {JSON Object}
*/
function collectWeatherData(weather) {
var viewObj = {
"city" : weather.name,
"climateCondition" : weather.weather[0].main,
"iconID" : getIconClass(weather.weather[0].icon),
"temperature": Number(Math.round(weather.main.temp +'e1')+'e-1'),
"temperatureColor" : getTemperatureColor(weather.main.temp)
};
renderView(viewObj);
getBgPhoto(weather.weather[0].main);
};
/**
* function getBgPhoto
* Makes an ajax call to FlickrAPI and grab photos with the climate condition of user location
* @param {String}
*/
function getBgPhoto(climateCondition) {
var url = getFlickrApiUrl(climateCondition),
promiseDone = function() {
App.$loader.fadeOut();
};
doAjaxCall(url, collectPhotoData, promiseDone);
};
/**
* function collectPhotoData
* Build the photo url
* @param {JSON Object}
*/
function collectPhotoData (photos) {
var photos = photos.photos,
rand = getRandomArbitrary(0, photos.photo.length),
photoUrl = "https://farm" + photos.photo[rand].farm +
".staticflickr.com/" + photos.photo[rand].server + "/" +
photos.photo[rand].id + "_" + photos.photo[rand].secret + ".jpg";
App.$container.css('backgroundImage', "url(" + photoUrl + ")");
};
/**
* function renderView
* Render the view with custom attributes based on wheather user location
* @param {[Object]}
*/
function renderView(viewObj) {
App.$city.text(viewObj.city);
App.$card.addClass(viewObj.temperatureColor);
App.$weather.addClass(viewObj.iconID);
App.$temp.html(viewObj.temperature);
};
/**
* function getTemperatureColor
* Obtain a color based on temperature degree
* @param {int}
* @return {String}
*/
function getTemperatureColor(temp) {
var tempColor = "";
if(temp <= 12) {
tempColor = "chill";
} else if (temp <= 25) {
tempColor = "ideal";
} else {
tempColor = "warm";
}
return tempColor;
};
/**
* function getOpenWeatherApiUrl
* Obtain the complete API url with setted custom params
* @param {int}
* @param {int}
* @return {String}
*/
function getOpenWeatherApiUrl(lat, lon) {
return App.apiUrl.openWeather +
"lat=" + lat +
"&lon=" + lon +
"&units=" + App.units +
"&appid=" + App.apiKey.openWeather;
};
/**
* function getFlickrApiUrl
* Obtain the complete API url with setted custom params
* @param {String}
* @return {String}
*/
function getFlickrApiUrl(climateCondition) {
return App.apiUrl.flickr +
"&api_key=" + App.apiKey.flickr +
"&format=json" +
"&content_type=1" +
"&accuracy=1" +
"&sort=relevance" +
"&text=weather " + climateCondition.toLowerCase() +
"&jsoncallback=?";
};
/**
* function getIconClass
* Obtain the corrispondend icon class of weather icon
* @param {String}
* @return {String}
*/
function getIconClass(iconID) {
return App.icons[iconID];
};
/**
* function getRandomArbitrary
* Obtain a random number in a certain range
* @param {int}
* @param {int}
* @return {int}
*/
function getRandomArbitrary(min, max) {
return Math.floor(Math.random() * (max - min) + min);
};
return {
init: function() {
/*
* Grab the ip of the user
*
*/
doAjaxCall('http://ipinfo.io', getWeather);
}
};
})();
WeatherApp.init();
})(jQuery);
</script>
<script>
(function($) {
/** @type {jQuery Object} [input containing the switcher temperature] */
var tempMetrics = $('#optForm input[name=tempMetrics]'),
newClass;
// Subscribe to change event when user select the desired metric
tempMetrics.on('change', function() {
// Get the desired metric from input
var toMetric = $('input[name=tempMetrics]:checked', '#optForm').val(),
// Get the floated temperature
temp = parseFloat($('.degree').text()),
// Get new converted temperature
newTemp = getConvertedTemperature(toMetric, temp);
// Change the value of degree element
$('.degree').text(newTemp);
if (toMetric === '°F') {
newClass = "wi wi-fahrenheit";
} else {
newClass = "wi wi-celsius";
}
// Reset and set the new metric class
$('#metrics').removeClass().addClass(newClass);
// Back to main view
$('#card').flip('toggle');
});
/**
* [getConvertedTemperature Does a temperure conversion]
* @param {String} metric Celsius of Fahrenheit
* @param {int} temp Temperature value
* @return {int} Converted Temperature
*/
function getConvertedTemperature(metric, temp) {
var ret;
switch(metric) {
case "°F":
ret = Number(Math.round((temp * 1.8 + 32) + 'e1') + 'e-1');
break;
case "°C":
ret = Number(Math.round((temp - 32) / 1.8 + 'e1') + 'e-1');
break;
}
return ret;
}
})(jQuery);
</script>
<script>
// Start card flip functionality
(function($){
var $card = $('#card'),
$settings = $('.settings');
$card.flip({
axis: "x"
,trigger: "manual"
})
$settings.on('click', function(evt) {
$card.flip('toggle');
})
})(jQuery);
</script>
</body>
</html>