2016-06-07 10:49:38 +02:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('harbor.system.management')
|
|
|
|
.directive('createDestination', createDestination);
|
|
|
|
|
2016-06-15 11:46:23 +02:00
|
|
|
CreateDestinationController.$inject = ['$scope', 'ListDestinationService', 'CreateDestinationService', 'UpdateDestinationService', 'PingDestinationService'];
|
2016-06-07 10:49:38 +02:00
|
|
|
|
2016-06-15 11:46:23 +02:00
|
|
|
function CreateDestinationController($scope, ListDestinationService, CreateDestinationService, UpdateDestinationService, PingDestinationService) {
|
2016-06-07 10:49:38 +02:00
|
|
|
var vm = this;
|
|
|
|
|
2016-06-14 12:47:32 +02:00
|
|
|
$scope.destination = {};
|
|
|
|
|
|
|
|
var vm0 = $scope.destination;
|
|
|
|
vm.addNew = addNew;
|
|
|
|
vm.edit = edit;
|
|
|
|
vm.create = create;
|
|
|
|
vm.update = update;
|
2016-06-15 11:46:23 +02:00
|
|
|
vm.pingDestination = pingDestination;
|
2016-06-14 12:47:32 +02:00
|
|
|
|
2016-06-17 12:07:33 +02:00
|
|
|
$scope.$watch('vm.action+","+vm.targetId', function(current) {
|
2016-06-14 12:47:32 +02:00
|
|
|
if(current) {
|
|
|
|
var parts = current.split(',');
|
|
|
|
vm.action = parts[0];
|
|
|
|
vm.targetId = parts[1];
|
|
|
|
switch(vm.action) {
|
|
|
|
case 'ADD_NEW':
|
2016-06-15 11:46:23 +02:00
|
|
|
vm.modalTitle = 'Create destination';
|
2016-06-14 12:47:32 +02:00
|
|
|
vm.addNew();
|
|
|
|
break;
|
|
|
|
case 'EDIT':
|
2016-06-15 11:46:23 +02:00
|
|
|
vm.modalTitle = 'Edit destination';
|
2016-06-14 12:47:32 +02:00
|
|
|
vm.edit(vm.targetId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function addNew() {
|
|
|
|
vm0.name = '';
|
|
|
|
vm0.endpoint = '';
|
|
|
|
vm0.username = '';
|
|
|
|
vm0.password = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function edit(targetId) {
|
|
|
|
getDestination(targetId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function create(destination) {
|
|
|
|
CreateDestinationService(destination.name, destination.endpoint,
|
2016-06-07 10:49:38 +02:00
|
|
|
destination.username, destination.password)
|
|
|
|
.success(createDestinationSuccess)
|
|
|
|
.error(createDestinationFailed);
|
|
|
|
}
|
2016-06-14 12:47:32 +02:00
|
|
|
|
2016-06-07 10:49:38 +02:00
|
|
|
function createDestinationSuccess(data, status) {
|
2016-06-17 10:11:46 +02:00
|
|
|
console.log('Successful created destination.');
|
|
|
|
vm.reload();
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDestinationFailed(data, status) {
|
2016-06-17 12:07:33 +02:00
|
|
|
if(status === 409) {
|
|
|
|
alert('Destination already exists.');
|
|
|
|
}
|
2016-06-07 10:49:38 +02:00
|
|
|
console.log('Failed create destination:' + data);
|
|
|
|
}
|
|
|
|
|
2016-06-14 12:47:32 +02:00
|
|
|
function update(destination) {
|
|
|
|
UpdateDestinationService(vm.targetId, destination)
|
|
|
|
.success(updateDestinationSuccess)
|
|
|
|
.error(updateDestinationFailed);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateDestinationSuccess(data, status) {
|
|
|
|
console.log('Successful update destination.');
|
2016-06-17 10:11:46 +02:00
|
|
|
vm.reload();
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateDestinationFailed(data, status) {
|
|
|
|
console.log('Failed update destination.');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDestination(targetId) {
|
|
|
|
ListDestinationService(targetId)
|
|
|
|
.success(getDestinationSuccess)
|
|
|
|
.error(getDestinationFailed);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDestinationSuccess(data, status) {
|
|
|
|
var destination = data;
|
|
|
|
vm0.name = destination.name;
|
|
|
|
vm0.endpoint = destination.endpoint;
|
|
|
|
vm0.username = destination.username;
|
|
|
|
vm0.password = destination.password;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDestinationFailed(data, status) {
|
|
|
|
console.log('Failed get destination.');
|
|
|
|
}
|
2016-06-15 11:46:23 +02:00
|
|
|
|
|
|
|
function pingDestination() {
|
|
|
|
var target = {
|
|
|
|
'name': vm0.name,
|
|
|
|
'endpoint': vm0.endpoint,
|
|
|
|
'username': vm0.username,
|
|
|
|
'password': vm0.password
|
|
|
|
};
|
|
|
|
PingDestinationService(target)
|
|
|
|
.success(pingDestinationSuccess)
|
|
|
|
.error(pingDestinationFailed);
|
|
|
|
}
|
|
|
|
function pingDestinationSuccess(data, status) {
|
|
|
|
alert('Successful ping target.');
|
|
|
|
}
|
|
|
|
function pingDestinationFailed(data, status) {
|
|
|
|
alert('Failed ping target:' + data);
|
|
|
|
}
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDestination() {
|
|
|
|
var directive = {
|
|
|
|
'restrict': 'E',
|
2016-06-16 08:10:35 +02:00
|
|
|
'templateUrl': '/static/resources/js/components/system-management/create-destination.directive.html',
|
2016-06-14 12:47:32 +02:00
|
|
|
'scope': {
|
|
|
|
'action': '@',
|
|
|
|
'targetId': '@',
|
|
|
|
'reload': '&'
|
|
|
|
},
|
|
|
|
'link': link,
|
2016-06-07 10:49:38 +02:00
|
|
|
'controller': CreateDestinationController,
|
|
|
|
'controllerAs': 'vm',
|
|
|
|
'bindToController': true
|
|
|
|
};
|
|
|
|
return directive;
|
2016-06-14 12:47:32 +02:00
|
|
|
|
|
|
|
function link(scope, element, attrs, ctrl) {
|
|
|
|
|
2016-06-17 12:07:33 +02:00
|
|
|
element.find('#createDestinationModal').on('show.bs.modal', function() {
|
|
|
|
scope.form.$setPristine();
|
|
|
|
scope.form.$setUntouched();
|
2016-06-15 11:46:23 +02:00
|
|
|
});
|
|
|
|
|
2016-06-14 12:47:32 +02:00
|
|
|
ctrl.save = save;
|
|
|
|
|
|
|
|
function save(destination) {
|
2016-06-17 12:07:33 +02:00
|
|
|
if(destination) {
|
2016-06-14 12:47:32 +02:00
|
|
|
switch(ctrl.action) {
|
|
|
|
case 'ADD_NEW':
|
|
|
|
ctrl.create(destination);
|
|
|
|
break;
|
|
|
|
case 'EDIT':
|
|
|
|
ctrl.update(destination);
|
2016-06-17 12:07:33 +02:00
|
|
|
break;
|
2016-06-14 12:47:32 +02:00
|
|
|
}
|
|
|
|
element.find('#createDestinationModal').modal('hide');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 10:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
})();
|