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

173 lines
5.9 KiB
JavaScript
Raw Normal View History

2018-01-22 13:01:38 -08:00
'use strict';
/* global angular */
/* global $ */
2019-01-17 13:26:47 +01:00
2019-05-04 21:57:42 -07:00
angular.module('Application').controller('MainController', ['$scope', '$route', '$timeout', '$location', 'Client', function ($scope, $route, $timeout, $location, Client) {
2018-06-25 18:04:16 -07:00
$scope.initialized = false; // used to animate the UI
2018-01-22 13:01:38 -08:00
$scope.user = Client.getUserInfo();
$scope.installedApps = Client.getInstalledApps();
$scope.config = {};
$scope.status = {};
$scope.client = Client;
$scope.subscription = {};
$scope.notifications = [];
2018-01-22 13:01:38 -08:00
$scope.hideNavBarActions = $location.path() === '/logs';
$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.waitingForPlanSelection = false;
$('#setupSubscriptionModal').on('hide.bs.modal', function () {
$scope.waitingForPlanSelection = false;
// check for updates to stay in sync
Client.checkForUpdates(function (error) {
if (error) return console.error(error);
Client.refreshConfig();
});
});
$scope.showSubscriptionModal = function () {
$('#setupSubscriptionModal').modal('show');
2018-01-22 13:01:38 -08:00
function checkPlan() {
if (!$scope.waitingForPlanSelection) return;
Client.getSubscription(function (error, subscription) {
2018-01-22 13:01:38 -08:00
if (error) return console.error(error);
// check again to give more immediate feedback once a subscription was setup
if (subscription.plan.id === 'free' || subscription.plan.id === 'expired') {
2018-01-22 13:01:38 -08:00
$timeout(checkPlan, 5000);
} else {
$scope.waitingForPlanSelection = false;
$('#setupSubscriptionModal').modal('hide');
$scope.subscription = subscription;
2018-01-22 13:01:38 -08:00
}
});
}
$scope.waitingForPlanSelection = true;
2018-01-22 13:01:38 -08:00
checkPlan();
2018-01-22 13:01:38 -08:00
};
2019-01-08 14:08:29 +01:00
function refreshNotifications() {
Client.getNotifications(false, 1, 20, function (error, results) {
if (error) console.error(error);
else $scope.notifications = results;
2019-01-08 14:08:29 +01:00
$timeout(refreshNotifications, 60 * 1000);
2019-01-08 14:08:29 +01:00
});
}
// NOTE: this function is exported and called from the settings.js
$scope.updateSubscriptionStatus = function () {
if (!Client.getUserInfo().admin) return;
Client.getSubscription(function (error, subscription) {
if (error && error.statusCode === 412) return; // ignore if not yet registered
if (error) console.error(error);
2018-03-28 14:32:21 +02:00
$scope.subscription = subscription;
});
};
2019-01-08 12:36:08 +01:00
// update state of acknowledged notification
$scope.notificationAcknowledged = function (notificationId) {
// remove notification from list
$scope.notifications = $scope.notifications.filter(function (n) { return n.id !== notificationId; });
};
function init() {
Client.getStatus(function (error, status) {
if (error) return Client.initError(error, init);
// WARNING if anything about the routing is changed here test these use-cases:
//
// 1. Caas
// 3. selfhosted restore
// 4. local development with gulp develop
if (!status.activated) {
console.log('Not activated yet, redirecting', status);
if (status.restore.active || status.restore.errorMessage) { // show the error message in restore page
window.location.href = '/restore.html';
} else {
window.location.href = status.adminFqdn ? '/setup.html' : '/setupdns.html';
}
return;
}
2018-01-22 13:01:38 -08:00
// support local development with localhost check
if (window.location.hostname !== status.adminFqdn && window.location.hostname !== 'localhost') {
// user is accessing by IP or by the old admin location (pre-migration)
window.location.href = '/setupdns.html';
return;
}
2018-01-22 13:01:38 -08:00
$scope.status = status;
2018-01-22 13:01:38 -08:00
// check version and force reload if needed
if (!localStorage.version) {
localStorage.version = status.version;
} else if (localStorage.version !== status.version) {
localStorage.version = status.version;
window.location.reload(true);
}
console.log('Running dashboard version ', localStorage.version);
// get user profile as the first thing. this populates the "scope" and affects subsequent API calls
Client.refreshUserInfo(function (error) {
if (error) return Client.initError(error, init);
2018-01-22 13:01:38 -08:00
Client.refreshConfig(function (error) {
if (error) return Client.initError(error, init);
2018-01-22 13:01:38 -08:00
Client.refreshInstalledApps(function (error) {
if (error) return Client.initError(error, init);
2018-01-22 13:01:38 -08:00
// now mark the Client to be ready
Client.setReady();
2018-01-22 13:01:38 -08:00
$scope.config = Client.getConfig();
2018-01-22 13:01:38 -08:00
$scope.initialized = true;
2018-01-22 13:01:38 -08:00
refreshNotifications();
2018-01-22 13:01:38 -08:00
$scope.updateSubscriptionStatus();
});
2018-01-22 13:01:38 -08:00
});
});
});
}
2018-01-22 13:01:38 -08:00
Client.onConfig(function (config) {
if (config.cloudronName) {
document.title = config.cloudronName;
}
});
init();
2018-01-22 13:01:38 -08:00
// setup all the dialog focus handling
['updateModal'].forEach(function (id) {
$('#' + id).on('shown.bs.modal', function () {
2019-05-05 09:05:06 -07:00
$(this).find('[autofocus]:first').focus();
2018-01-22 13:01:38 -08:00
});
});
}]);