Files
cloudron-box/webadmin/src/js/main.js

204 lines
6.9 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('Application').controller('MainController', ['$scope', '$route', '$interval', 'Client', function ($scope, $route, $interval, Client) {
$scope.initialized = false;
$scope.user = Client.getUserInfo();
$scope.installedApps = Client.getInstalledApps();
$scope.config = {};
$scope.client = Client;
2016-05-06 14:41:20 +02:00
$scope.tutorialStep = -1;
$scope.tutorialSteps = [
2016-05-06 13:06:12 +02:00
{ title: 'intro', page: '#/apps' },
{ title: 'appstore', page: '#/appstore' },
2016-05-06 21:32:34 +02:00
{ title: 'users', page: '#/users' }
2016-05-06 13:06:12 +02:00
];
2016-05-06 14:38:17 +02:00
$scope.startTutorial = function () {
2016-05-06 14:41:20 +02:00
$scope.tutorialStep = 0;
if ($scope.tutorialSteps[$scope.tutorialStep]) window.location.href = $scope.tutorialSteps[$scope.tutorialStep].page;
2016-05-06 14:38:17 +02:00
};
$scope.endTutorial = function () {
2016-05-06 14:41:20 +02:00
$scope.tutorialStep = -1;
2016-05-06 14:38:17 +02:00
Client.setShowTutorial(false, function (error) {
if (error) console.error(error);
2016-05-06 21:32:34 +02:00
window.location.href = '#/apps';
2016-05-06 14:38:17 +02:00
});
};
2016-05-06 14:41:20 +02:00
$scope.nextTutorialStep = function () {
$scope.tutorialStep += 1;
2016-05-06 13:06:12 +02:00
2016-05-06 14:41:20 +02:00
if ($scope.tutorialSteps[$scope.tutorialStep]) window.location.href = $scope.tutorialSteps[$scope.tutorialStep].page;
2016-05-06 14:38:17 +02:00
2016-05-06 14:41:20 +02:00
if ($scope.tutorialStep >= $scope.tutorialSteps.length) $scope.endTutorial();
2016-05-06 13:06:12 +02:00
};
2016-05-06 14:41:20 +02:00
$scope.prevTutorialStep = function () {
$scope.tutorialStep -= 1;
2016-05-06 13:06:12 +02:00
2016-05-06 14:41:20 +02:00
if ($scope.tutorialSteps[$scope.tutorialStep]) window.location.href = $scope.tutorialSteps[$scope.tutorialStep].page;
2016-05-06 13:06:12 +02:00
};
$scope.update = {
busy: false,
error: {},
password: ''
};
$scope.upgradeRequest = {
busy: false
};
$scope.isActive = function (url) {
if (!$route.current) return false;
return $route.current.$$route.originalPath.indexOf(url) === 0;
};
$scope.logout = function (event) {
event.stopPropagation();
$scope.initialized = false;
Client.logout();
};
$scope.error = function (error) {
console.error(error);
window.location.href = '/error.html';
};
$scope.showUpdateModal = function (form) {
$scope.update.error.password = null;
$scope.update.password = '';
form.$setPristine();
form.$setUntouched();
$('#updateModal').modal('show');
};
$scope.doUpdate = function () {
$scope.update.error.password = null;
$scope.update.busy = true;
Client.update($scope.update.password, function (error) {
if (error) {
if (error.statusCode === 403) {
$scope.update.error.password = true;
$scope.update.password = '';
$scope.update_form.password.$setPristine();
$('#inputUpdatePassword').focus();
} else {
console.error('Unable to update.', error);
}
$scope.update.busy = false;
return;
}
window.location.href = '/update.html';
});
};
2015-12-29 11:00:32 +01:00
Client.getStatus(function (error, status) {
if (error) return $scope.error(error);
if (!status.activated) {
window.location.href = '/setup.html';
return;
}
Client.refreshConfig(function (error) {
if (error) return $scope.error(error);
// check version and force reload if needed
if (!localStorage.version) {
localStorage.version = Client.getConfig().version;
} else if (localStorage.version !== Client.getConfig().version) {
localStorage.version = Client.getConfig().version;
window.location.reload(true);
}
2016-05-06 14:23:21 +02:00
Client.refreshUserInfo(function (error) {
if (error) return $scope.error(error);
Client.refreshInstalledApps(function (error) {
if (error) return $scope.error(error);
// kick off installed apps and config polling
2016-04-02 22:47:40 +02:00
var refreshAppsTimer = $interval(Client.refreshInstalledApps.bind(Client), 5000);
var refreshConfigTimer = $interval(Client.refreshConfig.bind(Client), 5000);
var refreshUserInfoTimer = $interval(Client.refreshUserInfo.bind(Client), 5000);
$scope.$on('$destroy', function () {
$interval.cancel(refreshAppsTimer);
$interval.cancel(refreshConfigTimer);
$interval.cancel(refreshUserInfoTimer);
});
// now mark the Client to be ready
Client.setReady();
$scope.config = Client.getConfig();
$scope.initialized = true;
// check if we have aws credentials if selfhosting
if ($scope.config.isCustomDomain) {
Client.getDnsConfig(function (error, result) {
if (error) return console.error(error);
if (result.provider === 'route53' && (!result.accessKeyId || !result.secretAccessKey)) {
var actionScope = $scope.$new(true);
actionScope.action = '/#/certs';
2016-01-18 13:39:27 +01:00
Client.notify('Missing AWS credentials', 'Please provide AWS credentials, click here to add them.', true, 'error', actionScope);
}
});
}
2016-05-06 13:06:12 +02:00
// welcome screen
2016-05-06 14:38:17 +02:00
if ($scope.user.showTutorial && $scope.user.admin) $scope.startTutorial();
});
});
});
});
// wait till the view has loaded until showing a modal dialog
Client.onConfig(function (config) {
2016-06-13 16:51:28 +02:00
// if (!config.billing) {
// setTimeout(function () {
// $('.upgrade')[0].classList.remove('hide');
// $('.upgrade .trigger').hover(function () {
// $('.upgrade .content')[0].classList.add('active');
// $('.upgrade .trigger')[0].classList.add('active');
// });
// $('.upgrade').hover(function () {}, function () {
// $('.upgrade .content')[0].classList.remove('active');
// $('.upgrade .trigger')[0].classList.remove('active');
// });
// }, 2000);
// }
// check if we are actually updating
if (config.progress.update && config.progress.update.percent !== -1) {
window.location.href = '/update.html';
}
if (config.cloudronName) {
document.title = config.cloudronName;
}
});
// setup all the dialog focus handling
['updateModal'].forEach(function (id) {
$('#' + id).on('shown.bs.modal', function () {
$(this).find("[autofocus]:first").focus();
});
});
}]);