2015-07-08 21:29:55 +00:00
|
|
|
/**
|
2015-07-08 21:40:17 +00:00
|
|
|
* ownCloud - Weather
|
2015-07-08 21:29:55 +00:00
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Loic Blot <loic.blot@unix-experience.fr>
|
|
|
|
* @copyright Loic Blot 2014-2015
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
var app = angular.module('Weather', []);
|
|
|
|
|
2015-07-09 19:35:17 +00:00
|
|
|
var g_error500 = 'Fatal Error: please check your owncloud.log and send a bug report here: https://github.com/nerzhul/weather/issues';
|
2015-07-08 21:29:55 +00:00
|
|
|
|
|
|
|
function undef (obj) {
|
|
|
|
return typeof obj === undefined || obj === undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function emptyStr (obj) {
|
|
|
|
return undef(obj) || obj == '';
|
|
|
|
}
|
|
|
|
|
|
|
|
app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compile', '$http',
|
|
|
|
function ($scope, $interval, $timeout, $compile, $http) {
|
2015-07-18 07:46:06 +00:00
|
|
|
$scope.owncloudAppImgPath = '';
|
2015-07-08 21:29:55 +00:00
|
|
|
$scope.userId = '';
|
|
|
|
$scope.cities = [];
|
2015-07-08 22:05:57 +00:00
|
|
|
$scope.showAddCity = false;
|
|
|
|
$scope.addCityError = '';
|
2015-07-08 21:29:55 +00:00
|
|
|
|
2015-07-09 19:35:17 +00:00
|
|
|
$scope.cityLoadError = '';
|
2015-07-11 15:01:30 +00:00
|
|
|
$scope.currentCity = null;
|
2015-07-18 08:01:38 +00:00
|
|
|
$scope.selectedCityId = 0;
|
2015-07-11 16:54:23 +00:00
|
|
|
$scope.domCity = null;
|
2015-07-18 07:46:06 +00:00
|
|
|
$scope.homeCity = '';
|
2015-07-09 19:35:17 +00:00
|
|
|
|
2015-07-11 15:07:40 +00:00
|
|
|
$scope.imageMapper = {
|
2015-07-11 16:16:41 +00:00
|
|
|
"Clear": "sun.png",
|
|
|
|
"Clouds": "clouds.png",
|
2015-07-11 16:45:36 +00:00
|
|
|
"Haze": "mist.png",
|
2015-07-11 16:16:41 +00:00
|
|
|
"Mist": "mist.png",
|
2015-07-11 15:43:34 +00:00
|
|
|
"Rain": "rain.jpg",
|
2015-07-11 16:16:41 +00:00
|
|
|
"Snow": "snow.png",
|
|
|
|
"Thunderstorm": "thunderstorm.png",
|
2015-07-11 15:07:40 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 16:54:23 +00:00
|
|
|
// Reload weather information each minute
|
|
|
|
$interval(function () {
|
|
|
|
if ($scope.currentCity != null) {
|
|
|
|
$scope.loadCity($scope.domCity);
|
|
|
|
}
|
|
|
|
}, 60000);
|
|
|
|
|
2015-07-08 21:29:55 +00:00
|
|
|
$timeout(function () {
|
2015-07-18 07:46:06 +00:00
|
|
|
var imgPath = OC.generateUrl('/apps/weather').replace('index.php/','');
|
|
|
|
$scope.owncloudAppImgPath = imgPath;
|
2015-07-08 21:29:55 +00:00
|
|
|
$scope.loadCities();
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.loadCities = function () {
|
2015-07-08 22:05:57 +00:00
|
|
|
$http.get(OC.generateUrl('/apps/weather/city/getall')).
|
2015-07-08 21:29:55 +00:00
|
|
|
success(function (data, status, headers, config) {
|
|
|
|
if (!undef(data['cities'])) {
|
|
|
|
$scope.cities = data['cities']
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!undef(data['userid'])) {
|
|
|
|
$scope.userId = data['userid'];
|
|
|
|
}
|
|
|
|
}).
|
|
|
|
fail(function (data, status, headers, config) {
|
|
|
|
$scope.fatalError();
|
|
|
|
});
|
|
|
|
};
|
2015-07-08 22:05:57 +00:00
|
|
|
|
2015-07-09 19:35:17 +00:00
|
|
|
$scope.loadCity = function(city) {
|
|
|
|
if (undef(city) || emptyStr(city.name)) {
|
|
|
|
alert(g_error500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-11 15:01:30 +00:00
|
|
|
$http.get(OC.generateUrl('/apps/weather/weather/get?name=' + city.name)).
|
2015-07-09 19:35:17 +00:00
|
|
|
success(function (data, status, headers, config) {
|
2015-07-11 15:01:30 +00:00
|
|
|
if (data != null) {
|
2015-07-11 16:54:23 +00:00
|
|
|
$scope.domCity = city;
|
2015-07-11 15:01:30 +00:00
|
|
|
$scope.currentCity = data;
|
2015-07-18 08:01:38 +00:00
|
|
|
$scope.selectedCityId = city.id;
|
2015-07-11 15:07:40 +00:00
|
|
|
$scope.currentCity.image = $scope.imageMapper[$scope.currentCity.weather[0].main];
|
2015-07-11 15:43:34 +00:00
|
|
|
$scope.currentCity.wind.desc = "";
|
|
|
|
if ($scope.currentCity.wind.deg > 0 && $scope.currentCity.wind.deg < 23 ||
|
|
|
|
$scope.currentCity.wind.deg > 333) {
|
|
|
|
$scope.currentCity.wind.desc = "North";
|
|
|
|
}
|
|
|
|
else if ($scope.currentCity.wind.deg > 22 && $scope.currentCity.wind.deg < 67) {
|
|
|
|
$scope.currentCity.wind.desc = "North-East";
|
|
|
|
}
|
|
|
|
else if ($scope.currentCity.wind.deg > 66 && $scope.currentCity.wind.deg < 113) {
|
|
|
|
$scope.currentCity.wind.desc = "East";
|
|
|
|
}
|
|
|
|
else if ($scope.currentCity.wind.deg > 112 && $scope.currentCity.wind.deg < 157) {
|
|
|
|
$scope.currentCity.wind.desc = "South-East";
|
|
|
|
}
|
|
|
|
else if ($scope.currentCity.wind.deg > 156 && $scope.currentCity.wind.deg < 201) {
|
|
|
|
$scope.currentCity.wind.desc = "South";
|
|
|
|
}
|
|
|
|
else if ($scope.currentCity.wind.deg > 200 && $scope.currentCity.wind.deg < 245) {
|
|
|
|
$scope.currentCity.wind.desc = "South-West";
|
|
|
|
}
|
|
|
|
else if ($scope.currentCity.wind.deg > 244 && $scope.currentCity.wind.deg < 289) {
|
|
|
|
$scope.currentCity.wind.desc = "West";
|
|
|
|
}
|
|
|
|
else if ($scope.currentCity.wind.deg > 288 && $scope.currentCity.wind.deg < 334) {
|
|
|
|
$scope.currentCity.wind.desc = "North-West";
|
|
|
|
}
|
2015-07-09 19:35:17 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$scope.cityLoadError = 'Failed to get city weather informations. Please contact your administrator';
|
|
|
|
}
|
|
|
|
}).
|
|
|
|
error(function (data, status, headers, config) {
|
|
|
|
if (status == 404) {
|
|
|
|
$scope.cityLoadError = "No city with this name found.";
|
|
|
|
}
|
|
|
|
else if (status == 500) {
|
|
|
|
$scope.cityLoadError = g_error500;
|
|
|
|
}
|
|
|
|
}).
|
|
|
|
fail(function (data, status, headers, config) {
|
|
|
|
$scope.cityLoadError = g_error500;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-08 22:05:57 +00:00
|
|
|
$scope.addCity = function(city) {
|
|
|
|
if (undef(city) || emptyStr(city.name)) {
|
|
|
|
$scope.addCityError = 'Empty city name !';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$http.post(OC.generateUrl('/apps/weather/city/add'), {'name': city.name}).
|
|
|
|
success(function (data, status, headers, config) {
|
|
|
|
if (data != null && !undef(data['id'])) {
|
2015-07-09 19:05:43 +00:00
|
|
|
$scope.cities.push({"name": city.name, "id": data['id']})
|
2015-07-08 22:05:57 +00:00
|
|
|
$scope.showAddCity = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$scope.addCityError = 'Failed to add city. Please contact your administrator';
|
|
|
|
}
|
|
|
|
}).
|
2015-07-09 19:35:17 +00:00
|
|
|
error(function (data, status, headers, config) {
|
|
|
|
if (status == 404) {
|
|
|
|
$scope.addCityError = "No city with this name found.";
|
|
|
|
}
|
|
|
|
else if (status == 409) {
|
|
|
|
$scope.addCityError = "This city is already registered for your account.";
|
|
|
|
}
|
|
|
|
}).
|
2015-07-08 22:05:57 +00:00
|
|
|
fail(function (data, status, headers, config) {
|
|
|
|
$scope.addCityError = g_error500;
|
|
|
|
});
|
|
|
|
};
|
2015-07-09 19:05:43 +00:00
|
|
|
|
|
|
|
$scope.deleteCity = function(city) {
|
|
|
|
if (undef(city)) {
|
|
|
|
alert(g_error500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$http.post(OC.generateUrl('/apps/weather/city/delete'), {'id': city.id}).
|
|
|
|
success(function (data, status, headers, config) {
|
|
|
|
if (data != null && !undef(data['deleted'])) {
|
|
|
|
for (var i = 0; i < $scope.cities.length; i++) {
|
|
|
|
if ($scope.cities[i].id === city.id) {
|
|
|
|
$scope.cities.splice(i, 1);
|
|
|
|
// If current city is the removed city, close it
|
|
|
|
if ($scope.selectedCityId === city.id) {
|
2015-07-18 08:01:38 +00:00
|
|
|
$scope.currentCity = null;
|
2015-07-09 19:05:43 +00:00
|
|
|
$scope.selectedCityId = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert('Failed to remove city. Please contact your administrator');
|
|
|
|
}
|
|
|
|
}).
|
|
|
|
fail(function (data, status, headers, config) {
|
|
|
|
alert(g_error500);
|
|
|
|
});
|
|
|
|
};
|
2015-07-08 21:40:17 +00:00
|
|
|
}
|
2015-07-08 21:29:55 +00:00
|
|
|
]);
|