harbor/static/resources/js/layout/account-setting/account-setting.controller.js

99 lines
2.7 KiB
JavaScript
Raw Normal View History

(function() {
'use strict';
angular
.module('harbor.layout.account.setting')
.controller('AccountSettingController', AccountSettingController);
2016-06-21 07:28:21 +02:00
AccountSettingController.$inject = ['ChangePasswordService', 'UpdateUserService', '$filter', 'trFilter', '$scope', '$window', 'currentUser'];
2016-06-21 07:28:21 +02:00
function AccountSettingController(ChangePasswordService, UpdateUserService, $filter, trFilter, $scope, $window, currentUser) {
2016-06-25 21:12:17 +02:00
var vm = this;
vm.isOpen = false;
2016-06-21 07:28:21 +02:00
2016-05-14 07:34:54 +02:00
vm.hasError = false;
vm.errorMessage = '';
vm.reset = reset;
vm.confirm = confirm;
2016-06-21 07:28:21 +02:00
vm.updateUser = updateUser;
vm.cancel = cancel;
2016-06-21 07:28:21 +02:00
$scope.user = currentUser.get();
if(!$scope.user) {
$window.location.href = '/';
return;
}
2016-06-21 07:28:21 +02:00
var userId = $scope.user.user_id;
//Error message dialog handler for account setting.
$scope.$on('modalTitle', function(e, val) {
vm.modalTitle = val;
});
$scope.$on('modalMessage', function(e, val) {
vm.modalMessage = val;
});
$scope.$on('raiseError', function(e, val) {
if(val) {
vm.action = function() {
$scope.$broadcast('showDialog', false);
};
vm.contentType = 'text/plain';
vm.confirmOnly = true;
$scope.$broadcast('showDialog', true);
}
});
2016-06-21 07:28:21 +02:00
2016-05-14 07:34:54 +02:00
function reset() {
2016-06-21 07:28:21 +02:00
$scope.form.$setUntouched();
$scope.form.$setPristine();
2016-05-14 07:34:54 +02:00
vm.hasError = false;
vm.errorMessage = '';
}
function confirm() {
$window.location.href = '/dashboard';
2016-06-21 07:28:21 +02:00
}
function updateUser(user) {
vm.confirmOnly = true;
vm.action = vm.confirm;
if(user && angular.isDefined(user.username) && angular.isDefined(user.realname)) {
2016-07-06 07:09:32 +02:00
UpdateUserService(userId, user)
.success(updateUserSuccess)
.error(updateUserFailed);
currentUser.set(user);
}
}
2016-07-06 07:09:32 +02:00
2016-06-21 07:28:21 +02:00
function updateUserSuccess(data, status) {
vm.modalTitle = $filter('tr')('change_profile', []);
vm.modalMessage = $filter('tr')('successful_changed_profile', []);
$scope.$broadcast('showDialog', true);
2016-06-21 07:28:21 +02:00
}
function updateUserFailed(data, status) {
$scope.$emit('modalTitle', $filter('tr')('error'));
var message;
if(status === 409) {
message = $filter('tr')('email_has_been_taken');
}else{
message = $filter('tr')('failed_to_update_user') + data;
}
$scope.$emit('modalMessage', message);
$scope.$emit('raiseError', true);
console.log('Failed to update user.');
2016-06-21 07:28:21 +02:00
}
2016-05-14 07:34:54 +02:00
function cancel(form) {
2016-06-21 07:28:21 +02:00
$window.location.href = '/dashboard';
}
}
})();