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

94 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-05-01 19:46:50 +02:00
(function() {
'use strict';
angular
.module('harbor.project')
.directive('addProject', addProject);
AddProjectController.$inject = ['AddProjectService', '$scope'];
function AddProjectController(AddProjectService, $scope) {
var vm = this;
$scope.p = {};
var vm0 = $scope.p;
vm0.projectName = '';
2016-05-01 19:46:50 +02:00
vm.isPublic = false;
vm.addProject = addProject;
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 addProject(p) {
if(p && angular.isDefined(p.projectName)) {
AddProjectService(p.projectName, vm.isPublic)
.success(addProjectSuccess)
.error(addProjectFailed);
2016-05-01 19:46:50 +02:00
}
}
function addProjectSuccess(data, status) {
$scope.$emit('addedSuccess', true);
vm.hasError = false;
vm.errorMessage = '';
2016-05-01 19:46:50 +02:00
}
function addProjectFailed(data, status) {
vm.hasError = true;
if(status === 400 && vm0.projectName!= '' && vm0.projectName.length < 4) {
vm.errorMessage = 'project_name_is_too_short';
}
if(status === 400 && vm0.projectName.length > 30) {
vm.errorMessage = 'project_name_is_too_long';
}
if(status === 409 && vm0.projectName != '') {
vm.errorMessage = 'project_already_exist';
}
2016-05-01 19:46:50 +02:00
console.log('Failed to add project:' + status);
}
2016-05-14 07:34:54 +02:00
function cancel(form){
if(form) {
form.$setPristine();
form.$setUntouched();
2016-05-14 07:34:54 +02:00
}
2016-05-01 19:46:50 +02:00
vm.isOpen = false;
vm0.projectName = '';
2016-05-04 12:42:05 +02:00
vm.isPublic = false;
vm.hasError = false; vm.close = close;
vm.errorMessage = '';
}
function reset() {
vm.hasError = false;
vm.errorMessage = '';
2016-05-01 19:46:50 +02:00
}
}
function addProject() {
var directive = {
'restrict': 'E',
'templateUrl': '/static/resources/js/components/project/add-project.directive.html',
2016-05-01 19:46:50 +02:00
'controller': AddProjectController,
'scope' : {
'isOpen': '='
},
2016-05-14 07:34:54 +02:00
'link': link,
2016-05-01 19:46:50 +02:00
'controllerAs': 'vm',
'bindToController': true
};
return directive;
2016-05-14 07:34:54 +02:00
function link(scope, element, attrs, ctrl) {
scope.form.$setPristine();
scope.form.$setUntouched();
2016-05-14 07:34:54 +02:00
}
2016-05-01 19:46:50 +02:00
}
})();