harbor/static/resources/js/components/project-member/add-project-member.directive.js

102 lines
2.4 KiB
JavaScript
Raw Normal View History

(function() {
'use strict';
angular
.module('harbor.project.member')
.directive('addProjectMember', addProjectMember);
AddProjectMemberController.$inject = ['$scope', 'roles', 'AddProjectMemberService'];
function AddProjectMemberController($scope, roles, AddProjectMemberService) {
var vm = this;
$scope.pm = {};
var pm = $scope.pm;
vm.roles = roles();
vm.optRole = 1;
vm.save = save;
vm.cancel = cancel;
vm.reset = reset;
2016-05-14 07:34:54 +02:00
vm.hasError = false;
vm.errorMessage = '';
2016-05-14 07:34:54 +02:00
function save(pm) {
if(pm && angular.isDefined(pm.username)) {
AddProjectMemberService(vm.projectId, vm.optRole, pm.username)
2016-05-14 07:34:54 +02:00
.success(addProjectMemberComplete)
.error(addProjectMemberFailed);
2016-05-14 07:34:54 +02:00
}
}
2016-05-14 07:34:54 +02:00
function cancel(form) {
form.$setPristine();
form.$setUntouched();
vm.isOpen = false;
pm.username = '';
2016-05-04 12:42:05 +02:00
vm.optRole = 1;
vm.hasError = false;
vm.errorMessage = '';
}
2016-04-27 12:29:55 +02:00
function addProjectMemberComplete(data, status, header) {
console.log('addProjectMemberComplete: status:' + status + ', data:' + data);
vm.reload();
vm.isOpen = false;
2016-04-27 12:29:55 +02:00
}
function addProjectMemberFailed(data, status, headers) {
2016-06-28 17:42:05 +02:00
if(status === 403) {
vm.hasError = true;
vm.errorMessage = 'failed_to_add_member';
}
if(status === 409 && pm.username != '') {
2016-05-14 07:34:54 +02:00
vm.hasError = true;
vm.errorMessage = 'username_already_exist';
}
2016-05-23 12:29:17 +02:00
if(status === 404) {
2016-05-14 07:34:54 +02:00
vm.hasError = true;
vm.errorMessage = 'username_does_not_exist';
}
2016-04-27 12:29:55 +02:00
console.log('addProjectMemberFailed: status:' + status + ', data:' + data);
}
function reset() {
vm.hasError = false;
vm.errorMessage = '';
}
}
function addProjectMember() {
var directive = {
'restrict': 'E',
'templateUrl': '/static/resources/js/components/project-member/add-project-member.directive.html',
'scope': {
'projectId': '@',
2016-04-28 19:36:39 +02:00
'isOpen': '=',
'reload': '&'
},
'link': link,
'controller': AddProjectMemberController,
'controllerAs': 'vm',
'bindToController': true
2016-05-23 12:29:17 +02:00
};
return directive;
function link(scope, element, attrs, ctrl) {
scope.form.$setPristine();
scope.form.$setUntouched();
}
}
})();