Files
cloudron-box/src/views/settings.js

294 lines
9.2 KiB
JavaScript
Raw Normal View History

2018-01-22 13:01:38 -08:00
'use strict';
/* global angular:false */
/* global $:false */
2019-05-04 21:57:42 -07:00
angular.module('Application').controller('SettingsController', ['$scope', '$location', '$rootScope', '$timeout', 'Client', function ($scope, $location, $rootScope, $timeout, Client) {
2020-02-24 12:56:13 +01:00
Client.onReady(function () { if (!Client.getUserInfo().isAtLeastAdmin) $location.path('/'); });
2018-01-22 13:01:38 -08:00
$scope.client = Client;
$scope.user = Client.getUserInfo();
$scope.config = Client.getConfig();
$scope.installedApps = Client.getInstalledApps();
2018-01-22 13:01:38 -08:00
$scope.subscription = null;
$scope.subscriptionBusy = true;
2018-01-22 13:01:38 -08:00
$scope.openSubscriptionSetup = function () {
Client.openSubscriptionSetup($scope.subscription || {});
};
2018-03-09 00:32:34 -08:00
$scope.prettyProviderName = function (provider) {
switch (provider) {
case 'caas': return 'Managed Cloudron';
default: return provider;
2018-03-09 00:32:34 -08:00
}
};
$scope.update = {
error: {}, // this is for the dialog
busy: false,
2020-03-18 21:31:51 -07:00
checking: false,
percent: 0,
2018-12-10 16:50:40 +01:00
message: 'Downloading',
errorMessage: '', // this shows inline
2018-12-09 10:45:01 -08:00
taskId: '',
2019-05-12 13:08:40 -07:00
skipBackup: false,
2020-03-18 21:31:51 -07:00
checkNow: function () {
$scope.update.checking = true;
Client.checkForUpdates(function (error) {
if (error) Client.error(error);
$scope.update.checking = false;
});
},
show: function () {
$scope.update.error.generic = null;
$scope.update.busy = false;
$('#updateModal').modal('show');
},
stopUpdate: function () {
2019-01-24 15:55:21 -08:00
Client.stopTask($scope.update.taskId, function (error) {
if (error) {
if (error.statusCode === 409) {
$scope.update.errorMessage = 'No update is currently in progress';
} else {
console.error(error);
$scope.update.errorMessage = error.message;
}
$scope.update.busy = false;
return;
}
});
},
2018-12-08 20:21:11 -08:00
checkStatus: function () {
2018-12-09 10:45:01 -08:00
Client.getLatestTaskByType('update', function (error, task) {
2018-12-08 20:21:11 -08:00
if (error) return console.error(error);
2019-01-06 14:52:36 -08:00
if (!task) return;
2018-12-08 20:21:11 -08:00
2018-12-09 10:45:01 -08:00
$scope.update.taskId = task.id;
$scope.update.updateStatus();
2018-12-08 20:21:11 -08:00
});
},
2018-12-09 10:45:01 -08:00
reloadIfNeeded: function () {
Client.getStatus(function (error, status) {
if (error) return $scope.error(error);
if (window.localStorage.version !== status.version) window.location.reload(true);
});
},
updateStatus: function () {
Client.getTask($scope.update.taskId, function (error, data) {
if (error) return window.setTimeout($scope.update.updateStatus, 5000);
if (!data.active) {
$scope.update.busy = false;
$scope.update.message = '';
$scope.update.percent = 100; // indicates that 'result' is valid
$scope.update.errorMessage = data.success ? '' : data.error.message;
2018-12-09 10:45:01 -08:00
if (!data.errorMessage) $scope.update.reloadIfNeeded(); // assume success
return;
}
$scope.update.busy = true;
$scope.update.percent = data.percent;
$scope.update.message = data.message;
2018-12-09 10:45:01 -08:00
window.setTimeout($scope.update.updateStatus, 500);
});
},
startUpdate: function () {
$scope.update.error.generic = null;
$scope.update.busy = true;
$scope.update.percent = 0;
$scope.update.message = '';
$scope.update.errorMessage = '';
2019-05-12 13:08:40 -07:00
Client.update({ skipBackup: $scope.update.skipBackup }, function (error, taskId) {
if (error) {
2018-12-04 14:12:35 -08:00
$scope.update.error.generic = error.message;
$scope.update.busy = false;
return;
}
$('#updateModal').modal('hide');
2018-12-09 10:45:01 -08:00
$scope.update.taskId = taskId;
$scope.update.updateStatus();
});
}
};
2020-01-07 21:41:45 +01:00
$scope.timeZone = {
busy: false,
success: false,
error: '',
timeZone: '',
currentTimeZone: '',
availableTimeZones: window.timezones,
2020-01-07 21:41:45 +01:00
submit: function () {
if ($scope.timeZone.timeZone === $scope.timeZone.currentTimeZone) return;
$scope.timeZone.error = '';
$scope.timeZone.busy = true;
$scope.timeZone.success = false;
Client.setTimeZone($scope.timeZone.timeZone.id, function (error) {
2020-01-07 21:41:45 +01:00
if (error) $scope.timeZone.error = error.message;
else $scope.timeZone.currentTimeZone = $scope.timeZone.timeZone;
$scope.timeZone.busy = false;
$scope.timeZone.success = true;
});
}
2020-03-18 21:31:51 -07:00
};
2020-01-07 21:41:45 +01:00
2018-01-22 13:01:38 -08:00
$scope.autoUpdate = {
busy: false,
pattern: '',
currentPattern: '',
submit: function () {
if ($scope.autoUpdate.pattern === $scope.autoUpdate.currentPattern) return;
$scope.autoUpdate.busy = true;
Client.setAppAutoupdatePattern($scope.autoUpdate.pattern, function (error) {
2020-03-18 21:31:51 -07:00
if (error) Client.error(error);
2018-01-22 13:01:38 -08:00
else $scope.autoUpdate.currentPattern = $scope.autoUpdate.pattern;
2020-03-18 21:31:51 -07:00
$timeout(function () {
$scope.autoUpdate.busy = false;
}, 3000);
2018-01-22 13:01:38 -08:00
});
}
};
2020-01-07 21:41:45 +01:00
function getTimeZone() {
Client.getTimeZone(function (error, timeZone) {
if (error) return console.error(error);
$scope.timeZone.currentTimeZone = window.timezones.find(function (t) { return t.id === timeZone; });
$scope.timeZone.timeZone = $scope.timeZone.currentTimeZone;
2020-01-07 21:41:45 +01:00
});
}
2018-01-22 13:01:38 -08:00
function getAutoupdatePattern() {
Client.getAppAutoupdatePattern(function (error, result) {
2018-01-22 13:01:38 -08:00
if (error) return console.error(error);
$scope.autoUpdate.currentPattern = result.pattern;
$scope.autoUpdate.pattern = result.pattern;
});
}
2019-10-22 22:31:38 -07:00
function getRegistryConfig() {
Client.getRegistryConfig(function (error, result) {
if (error) return console.error(error);
2019-11-07 23:04:02 -08:00
$scope.registryConfig.currentConfig.serverAddress = result.serverAddress;
$scope.registryConfig.currentConfig.username = result.username || '';
$scope.registryConfig.currentConfig.email = result.email || '';
$scope.registryConfig.currentConfig.password = result.password;
2019-10-22 22:31:38 -07:00
});
}
2018-01-22 13:01:38 -08:00
function getSubscription() {
$scope.subscriptionBusy = true;
Client.getSubscription(function (error, result) {
$scope.subscriptionBusy = false;
if (error && error.statusCode === 412) return; // not yet registered
if (error) return console.error(error);
$scope.subscription = result;
});
}
2019-10-22 22:31:38 -07:00
$scope.registryConfig = {
busy: false,
error: null,
serverAddress: '',
username: '',
password: '',
2019-10-23 06:11:27 -07:00
email: '',
2019-11-07 23:04:02 -08:00
currentConfig: {},
2019-10-22 22:31:38 -07:00
reset: function () {
2020-04-03 09:56:38 -07:00
$scope.registryConfig.busy = false;
2019-10-22 22:31:38 -07:00
$scope.registryConfig.error = null;
2019-11-07 23:04:02 -08:00
$scope.registryConfig.serverAddress = $scope.registryConfig.currentConfig.serverAddress;
$scope.registryConfig.username = $scope.registryConfig.currentConfig.username;
$scope.registryConfig.email = $scope.registryConfig.currentConfig.email;
$scope.registryConfig.password = $scope.registryConfig.currentConfig.password;
2019-10-22 22:31:38 -07:00
$scope.registryConfigForm.$setUntouched();
$scope.registryConfigForm.$setPristine();
},
show: function () {
$scope.registryConfig.reset();
$('#registryConfigModal').modal('show');
},
submit: function () {
$scope.registryConfig.busy = true;
var data = {
serverAddress: $scope.registryConfig.serverAddress,
2019-10-23 06:11:27 -07:00
username: $scope.registryConfig.username || '',
password: $scope.registryConfig.password,
email: $scope.registryConfig.email || '',
2019-10-22 22:31:38 -07:00
};
Client.setRegistryConfig(data, function (error) {
$scope.registryConfig.busy = false;
if (error) {
2019-10-23 06:11:27 -07:00
$scope.registryConfig.error = error.message;
2019-10-22 22:31:38 -07:00
return;
}
$('#registryConfigModal').modal('hide');
getRegistryConfig();
});
}
};
2018-01-22 13:01:38 -08:00
Client.onReady(function () {
getAutoupdatePattern();
2019-10-22 22:31:38 -07:00
getRegistryConfig();
2020-01-07 21:41:45 +01:00
getTimeZone();
2018-01-22 13:01:38 -08:00
2018-12-08 20:21:11 -08:00
$scope.update.checkStatus();
2018-12-04 14:05:55 -08:00
2020-03-06 11:42:27 -08:00
if ($scope.user.isAtLeastOwner) getSubscription();
2018-01-22 13:01:38 -08:00
});
// setup all the dialog focus handling
2020-03-18 21:31:51 -07:00
['planChangeModal', 'appstoreLoginModal'].forEach(function (id) {
2018-01-22 13:01:38 -08:00
$('#' + id).on('shown.bs.modal', function () {
$(this).find("[autofocus]:first").focus();
});
});
$('.modal-backdrop').remove();
}]);