-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexController.js
84 lines (74 loc) · 2.81 KB
/
indexController.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
(function () {
var app = angular.module('app');
app.controller('indexController', function ($scope, ipService, weatherService) {
// initialize fields
$scope.errorMessage = null;
$scope.location = null;
$scope.weather = null;
$scope.currentDegree = null;
$scope.currentLocation = null;
$scope.isFahrenheit = false;
var init = function () {
ipService.getIp().then(function (response) {
$scope.errorMessage = null;
var location = response.data;
$scope.currentLocation = location.city+" in "+location.country;
getWeather(location.city, location.country);
}, function (err) {
console.log(err.statusText);
$scope.errorMessage = 'Cannot Get Ip Information';
});
};
var getWeather = function (city, country) {
weatherService.getWeather(city, country).then(function (response) {
$scope.weather = response.data;
$scope.currentDegree = convertDegree($scope.isFahrenheit, $scope.weather.main.temp);
changeWeatherStatus($scope.weather.weather[0].main);
}, function (err) {
console.log(err.statusText);
$scope.errorMessage = 'Cannot Get Weather Data';
});
};
var convertDegree = function (fToC, value) {
if (fToC === false) { // Celcius to Fahrenheit
value = (value - 32) * 5 / 9;
} else { // Reverse
value = (value * 9) / 5 + 32;
}
return parseInt('' + value);
};
$scope.toggleDegree = function () {
$scope.isFahrenheit = !$scope.isFahrenheit;
$scope.currentDegree = convertDegree($scope.isFahrenheit, $scope.currentDegree);
};
function changeIcon(icon) {
$("div." + icon).removeClass("hide");
};
function changeWeatherStatus(status) {
switch (status = status.toLowerCase()) {
case 'dizzle':
changeIcon('sun-shower');
break;
case 'clouds':
changeIcon('cloudy');
break;
case 'rain':
changeIcon('rainy');
break;
case 'snow':
changeIcon('flurries');
break;
case 'clear':
changeIcon('sunny');
break;
case 'thunderstom':
changeIcon('thunder-storm');
break;
default:
$('div.clouds').removeClass('hide');
break;
}
};
init();
});
}());