Rework the restore/clone dialogs

This commit is contained in:
Johannes Zellner
2019-07-12 17:18:21 +02:00
parent 2835d1bd87
commit 88bc30bbea
2 changed files with 177 additions and 162 deletions

View File

@@ -281,27 +281,89 @@ angular.module('Application').controller('AppsController', ['$scope', '$location
app: {}
};
$scope.appRestore = {
$scope.appClone = {
busy: false,
busyFetching: false,
error: {},
app: {},
backups: [ ],
selectedBackup: null,
backup: {},
// from clone
// form
location: '',
domain: null,
portBindings: {},
portBindingsInfo: {},
portBindingsEnabled: {},
action: 'restore',
show: function (app, backup) {
// hide restore modal if open
$('#appRestoreModal').modal('hide');
$scope.appClone.busy = false;
$scope.appClone.error = {};
$scope.appClone.app = app;
$scope.appClone.backup = backup;
$scope.appClone.location = '';
$scope.appClone.domain = $scope.domains.find(function (d) { return app.domain === d.domain; }); // pre-select the app's domain
$scope.appClone.portBindingsInfo = angular.extend({}, $scope.appClone.app.manifest.tcpPorts, $scope.appClone.app.manifest.udpPorts); // Portbinding map only for information
// set default ports
for (var env in $scope.appClone.portBindingsInfo) {
$scope.appClone.portBindings[env] = $scope.appClone.portBindingsInfo[env].defaultValue || 0;
$scope.appClone.portBindingsEnabled[env] = true;
}
$('#appCloneModal').modal('show');
},
submit: function () {
$scope.appClone.busy = true;
// only use enabled ports from portBindings
var finalPortBindings = {};
for (var env in $scope.appClone.portBindings) {
if ($scope.appClone.portBindingsEnabled[env]) {
finalPortBindings[env] = $scope.appClone.portBindings[env];
}
}
var data = {
location: $scope.appClone.location,
domain: $scope.appClone.domain.domain,
portBindings: finalPortBindings,
backupId: $scope.appClone.backup.id
};
Client.cloneApp($scope.appClone.app.id, data, function (error, clonedApp) {
$scope.appClone.busy = false;
if (error) {
if (error.statusCode === 409 && (error.message.indexOf('is reserved') !== -1 || error.message.indexOf('is already in use') !== -1)) {
$scope.appClone.error.port = error.message;
} else if (error.statusCode === 409) {
$scope.appClone.error.location = 'This location is already taken.';
$('#appCloneLocationInput').focus();
} else {
Client.error(error);
}
return;
}
$('#appCloneModal').modal('hide');
Client.refreshAppCache(clonedApp.id); // reflect the new app state immediately
});
}
}
$scope.appRestore = {
busy: false,
busyFetching: false,
error: {},
app: {},
backups: [],
copyBackupIdDone: false,
copyBackupId: function () {
var copyText = document.getElementById('appRestoreSelectedBackupId');
copyBackupId: function (backup) {
var copyText = document.getElementById('appRestoreBackupIdHelper');
copyText.value = backup.id;
copyText.select();
document.execCommand('copy');
@@ -311,10 +373,6 @@ angular.module('Application').controller('AppsController', ['$scope', '$location
$timeout(function () { $scope.appRestore.copyBackupIdDone = false; }, 2500);
},
selectBackup: function (backup) {
$scope.appRestore.selectedBackup = backup;
},
createBackup: function () {
Client.backupApp($scope.appRestore.app.id, function (error) {
if (error) Client.error(error);
@@ -327,7 +385,6 @@ angular.module('Application').controller('AppsController', ['$scope', '$location
if (error) return Client.error(error);
$scope.appRestore.backups = backups;
if (backups.length) $scope.appRestore.selectedBackup = backups[0]; // pre-select first backup
});
}
@@ -336,60 +393,12 @@ angular.module('Application').controller('AppsController', ['$scope', '$location
});
},
clone: function () {
$scope.appRestore.busy = true;
// only use enabled ports from portBindings
var finalPortBindings = {};
for (var env in $scope.appRestore.portBindings) {
if ($scope.appRestore.portBindingsEnabled[env]) {
finalPortBindings[env] = $scope.appRestore.portBindings[env];
}
}
var data = {
location: $scope.appRestore.location,
domain: $scope.appRestore.domain.domain,
portBindings: finalPortBindings,
backupId: $scope.appRestore.selectedBackup.id
};
Client.cloneApp($scope.appRestore.app.id, data, function (error, clonedApp) {
$scope.appRestore.busy = false;
if (error) {
if (error.statusCode === 409 && (error.message.indexOf('is reserved') !== -1 || error.message.indexOf('is already in use') !== -1)) {
$scope.appRestore.error.port = error.message;
} else if (error.statusCode === 409) {
$scope.appRestore.error.location = 'This name is already taken.';
$('#appRestoreLocationInput').focus();
} else {
Client.error(error);
}
return;
}
$('#appRestoreModal').modal('hide');
Client.refreshAppCache(clonedApp.id); // reflect the new app state immediately
});
},
show: function (app) {
$scope.reset();
$scope.appRestore.app = app;
$scope.appRestore.busyFetching = true;
$scope.appRestore.domain = $scope.domains.find(function (d) { return app.domain === d.domain; }); // pre-select the app's domain
$scope.appRestore.portBindingsInfo = angular.extend({}, $scope.appRestore.app.manifest.tcpPorts, $scope.appRestore.app.manifest.udpPorts); // Portbinding map only for information
// set default ports
for (var env in $scope.appRestore.portBindingsInfo) {
$scope.appRestore.portBindings[env] = $scope.appRestore.portBindingsInfo[env].defaultValue || 0;
$scope.appRestore.portBindingsEnabled[env] = true;
}
$scope.appRestore.action = 'restore';
$('#appRestoreModal').modal('show');
Client.getAppBackups(app.id, function (error, backups) {
@@ -397,7 +406,6 @@ angular.module('Application').controller('AppsController', ['$scope', '$location
Client.error(error);
} else {
$scope.appRestore.backups = backups;
if (backups.length) $scope.appRestore.selectedBackup = backups[0]; // pre-select first backup
$scope.appRestore.busyFetching = false;
}
});
@@ -405,10 +413,10 @@ angular.module('Application').controller('AppsController', ['$scope', '$location
return false; // prevent propagation and default
},
restore: function () {
restore: function (backup) {
$scope.appRestore.busy = true;
Client.restoreApp($scope.appRestore.app.id, $scope.appRestore.selectedBackup.id, function (error) {
Client.restoreApp($scope.appRestore.app.id, backup.id, function (error) {
if (error) {
Client.error(error);
} else {
@@ -512,7 +520,6 @@ angular.module('Application').controller('AppsController', ['$scope', '$location
// reset restore dialog
$scope.appRestore.error = {};
$scope.appRestore.app = {};
$scope.appRestore.selectedBackup = null;
$scope.appRestore.backups = [];
$scope.appRestore.location = '';
$scope.appRestore.domain = null;