update for creation of policy and destinations.

This commit is contained in:
kunw 2016-06-17 18:07:33 +08:00
parent 3b2b0d8849
commit 7ae5b39407
2 changed files with 40 additions and 27 deletions

View File

@ -23,16 +23,19 @@
vm.selectDestination = selectDestination; vm.selectDestination = selectDestination;
vm.projectId = getParameterByName('project_id', $location.absUrl()); vm.projectId = getParameterByName('project_id', $location.absUrl());
$scope.$on('$locationChangeSuccess', function() {
vm.projectId = getParameterByName('project_id', $location.absUrl());
});
vm.addNew = addNew; vm.addNew = addNew;
vm.edit = edit; vm.edit = edit;
vm.prepareDestination = prepareDestination; vm.prepareDestination = prepareDestination;
vm.createPolicy = createPolicy; vm.create = create;
vm.updatePolicy = updatePolicy; vm.update = update;
vm.pingDestination = pingDestination; vm.pingDestination = pingDestination;
$scope.$watch('vm.destinations', function(current) { $scope.$watch('vm.destinations', function(current) {
if(current) { if(current) {
console.log('destination:' + angular.toJson(current));
vm1.selection = current[0]; vm1.selection = current[0];
vm1.endpoint = vm1.selection.endpoint; vm1.endpoint = vm1.selection.endpoint;
vm1.username = vm1.selection.username; vm1.username = vm1.selection.username;
@ -48,9 +51,11 @@
vm.policyId = Number(parts[1]); vm.policyId = Number(parts[1]);
switch(parts[0]) { switch(parts[0]) {
case 'ADD_NEW': case 'ADD_NEW':
vm.addNew(); break; vm.addNew();
break;
case 'EDIT': case 'EDIT':
vm.edit(vm.policyId); break; vm.edit(vm.policyId);
break;
} }
} }
}); });
@ -81,13 +86,13 @@
.error(listReplicationPolicyFailed); .error(listReplicationPolicyFailed);
} }
function createPolicy(policy) { function create(policy) {
CreateReplicationPolicyService(policy) CreateReplicationPolicyService(policy)
.success(createReplicationPolicySuccess) .success(createReplicationPolicySuccess)
.error(createReplicationPolicyFailed); .error(createReplicationPolicyFailed);
} }
function updatePolicy(policy) { function update(policy) {
console.log('Update policy ID:' + vm.policyId); console.log('Update policy ID:' + vm.policyId);
UpdateReplicationPolicyService(vm.policyId, policy) UpdateReplicationPolicyService(vm.policyId, policy)
.success(updateReplicationPolicySuccess) .success(updateReplicationPolicySuccess)
@ -119,7 +124,7 @@
} }
function listDestinationSuccess(data, status) { function listDestinationSuccess(data, status) {
vm.destinations = data; vm.destinations = data || [];
} }
function listDestinationFailed(data, status) { function listDestinationFailed(data, status) {
console.log('Failed list destination:' + data); console.log('Failed list destination:' + data);
@ -136,12 +141,17 @@
} }
function createReplicationPolicySuccess(data, status) { function createReplicationPolicySuccess(data, status) {
console.log('Successful create replication policy.'); console.log('Successful create replication policy.');
vm.reload();
} }
function createReplicationPolicyFailed(data, status) { function createReplicationPolicyFailed(data, status) {
if(status === 409) {
alert('Policy name already exists.');
}
console.log('Failed create replication policy.'); console.log('Failed create replication policy.');
} }
function updateReplicationPolicySuccess(data, status) { function updateReplicationPolicySuccess(data, status) {
console.log('Successful update replication policy.'); console.log('Successful update replication policy.');
vm.reload();
} }
function updateReplicationPolicyFailed(data, status) { function updateReplicationPolicyFailed(data, status) {
console.log('Failed update replication policy.'); console.log('Failed update replication policy.');
@ -179,14 +189,15 @@
function link(scope, element, attr, ctrl) { function link(scope, element, attr, ctrl) {
element.find('#createPolicyModal').on('shown.bs.modal', function() { element.find('#createPolicyModal').on('show.bs.modal', function() {
ctrl.prepareDestination(); ctrl.prepareDestination();
scope.form.$setPristine(); scope.form.$setPristine();
scope.form.$setUntouched();
}); });
ctrl.save = save; ctrl.save = save;
function save(form) { function save(form) {
console.log(angular.toJson(form));
var postPayload = { var postPayload = {
'projectId': Number(ctrl.projectId), 'projectId': Number(ctrl.projectId),
'targetId': form.destination.selection.id, 'targetId': form.destination.selection.id,
@ -198,14 +209,14 @@
}; };
switch(ctrl.action) { switch(ctrl.action) {
case 'ADD_NEW': case 'ADD_NEW':
ctrl.createPolicy(postPayload); break; ctrl.create(postPayload);
break;
case 'EDIT': case 'EDIT':
ctrl.updatePolicy(postPayload); break; ctrl.update(postPayload);
break;
} }
element.find('#createPolicyModal').modal('hide'); element.find('#createPolicyModal').modal('hide');
ctrl.reload();
} }
} }
} }

View File

@ -20,7 +20,7 @@
vm.update = update; vm.update = update;
vm.pingDestination = pingDestination; vm.pingDestination = pingDestination;
$scope.$watch('vm.action + "," + vm.targetId', function(current) { $scope.$watch('vm.action+","+vm.targetId', function(current) {
if(current) { if(current) {
var parts = current.split(','); var parts = current.split(',');
vm.action = parts[0]; vm.action = parts[0];
@ -62,6 +62,9 @@
} }
function createDestinationFailed(data, status) { function createDestinationFailed(data, status) {
if(status === 409) {
alert('Destination already exists.');
}
console.log('Failed create destination:' + data); console.log('Failed create destination:' + data);
} }
@ -105,7 +108,6 @@
'username': vm0.username, 'username': vm0.username,
'password': vm0.password 'password': vm0.password
}; };
console.log('Ping target:' + angular.toJson(target));
PingDestinationService(target) PingDestinationService(target)
.success(pingDestinationSuccess) .success(pingDestinationSuccess)
.error(pingDestinationFailed); .error(pingDestinationFailed);
@ -136,24 +138,24 @@
function link(scope, element, attrs, ctrl) { function link(scope, element, attrs, ctrl) {
element.find('#createDestinationModal').on('hidden.bs.modal', function() { element.find('#createDestinationModal').on('show.bs.modal', function() {
scope.form.$setPristine(); scope.form.$setPristine();
scope.form.$setUntouched();
}); });
ctrl.save = save; ctrl.save = save;
function save(destination) { function save(destination) {
if(destination) { if(destination) {
console.log('destination:' + angular.toJson(destination));
switch(ctrl.action) { switch(ctrl.action) {
case 'ADD_NEW': case 'ADD_NEW':
ctrl.create(destination); ctrl.create(destination);
break; break;
case 'EDIT': case 'EDIT':
ctrl.update(destination); ctrl.update(destination);
break;
} }
element.find('#createDestinationModal').modal('hide'); element.find('#createDestinationModal').modal('hide');
ctrl.reload();
} }
} }
} }