2018-01-22 13:01:38 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
/* global angular */
|
|
|
|
|
/* global $ */
|
2019-01-17 13:26:47 +01:00
|
|
|
|
2021-05-29 12:11:17 -07:00
|
|
|
angular.module('Application').controller('MainController', ['$scope', '$route', '$timeout', '$location', '$interval', 'Client', function ($scope, $route, $timeout, $location, $interval, 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.client = Client;
|
2018-03-28 12:26:24 +02:00
|
|
|
$scope.subscription = {};
|
2021-05-29 12:11:17 -07:00
|
|
|
$scope.notificationCount = 0;
|
2018-01-22 13:01:38 -08:00
|
|
|
$scope.hideNavBarActions = $location.path() === '/logs';
|
|
|
|
|
|
2020-11-05 16:45:45 +01:00
|
|
|
$scope.reboot = {
|
|
|
|
|
busy: false,
|
|
|
|
|
|
|
|
|
|
show: function () {
|
|
|
|
|
$scope.reboot.busy = false;
|
|
|
|
|
$('#rebootModal').modal('show');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
submit: function () {
|
|
|
|
|
$scope.reboot.busy = true;
|
|
|
|
|
|
|
|
|
|
Client.reboot(function (error) {
|
|
|
|
|
if (error) return Client.error(error);
|
|
|
|
|
|
|
|
|
|
$('#rebootModal').modal('hide');
|
|
|
|
|
|
|
|
|
|
// trigger refetch to show offline banner
|
|
|
|
|
$timeout(function () { Client.getStatus(function () {}); }, 5000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-22 13:01:38 -08:00
|
|
|
$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();
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-21 12:34:40 +01:00
|
|
|
$scope.openSubscriptionSetup = function () {
|
|
|
|
|
Client.openSubscriptionSetup($scope.subscription);
|
2018-01-22 13:01:38 -08:00
|
|
|
};
|
|
|
|
|
|
2020-02-14 14:16:04 +01:00
|
|
|
// NOTE: this function is exported and called from the appstore.js
|
2019-05-04 21:19:46 -07:00
|
|
|
$scope.updateSubscriptionStatus = function () {
|
2020-02-24 17:29:20 +01:00
|
|
|
if (!Client.getUserInfo().isAtLeastAdmin) return;
|
2019-05-14 16:41:08 +02:00
|
|
|
|
2019-05-04 21:19:46 -07:00
|
|
|
Client.getSubscription(function (error, subscription) {
|
2019-05-06 11:07:19 +02:00
|
|
|
if (error && error.statusCode === 412) return; // ignore if not yet registered
|
2020-06-18 14:50:24 +02:00
|
|
|
if (error && error.statusCode === 402) return; // ignore if not yet registered
|
2020-02-21 12:34:40 +01:00
|
|
|
if (error) return console.error(error);
|
2018-03-28 14:32:21 +02:00
|
|
|
|
2019-05-04 21:19:46 -07:00
|
|
|
$scope.subscription = subscription;
|
2018-03-28 14:19:34 +02:00
|
|
|
});
|
2018-04-05 21:49:15 +02:00
|
|
|
};
|
2018-03-28 14:19:34 +02:00
|
|
|
|
2021-05-29 12:11:17 -07:00
|
|
|
function refreshNotifications() {
|
2021-07-07 19:07:43 +02:00
|
|
|
if (!Client.getUserInfo().isAtLeastAdmin) return;
|
|
|
|
|
|
2021-05-29 12:11:17 -07:00
|
|
|
Client.getNotifications({ acknowledged: false }, 1, 100, function (error, results) { // counter maxes out at 100
|
2020-02-14 14:16:04 +01:00
|
|
|
if (error) console.error(error);
|
2021-05-29 12:11:17 -07:00
|
|
|
else $scope.notificationCount = results.length;
|
2020-02-14 14:16:04 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 12:36:08 +01:00
|
|
|
// update state of acknowledged notification
|
2021-06-30 17:24:41 +02:00
|
|
|
$scope.notificationAcknowledged = function () {
|
|
|
|
|
if ($scope.notificationCount === 0) return; // already down to 0
|
|
|
|
|
$scope.notificationCount--;
|
2019-01-08 12:36:08 +01:00
|
|
|
};
|
|
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
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
|
2020-12-21 22:36:43 -08:00
|
|
|
window.location.href = '/restore.html' + window.location.search;
|
2019-09-05 22:22:42 +02:00
|
|
|
} else {
|
2020-12-21 22:36:43 -08:00
|
|
|
window.location.href = (status.adminFqdn ? '/setup.html' : '/setupdns.html') + window.location.search;
|
2019-09-05 22:22:42 +02:00
|
|
|
}
|
|
|
|
|
return;
|
2018-07-29 20:33:21 -07:00
|
|
|
}
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
// support local development with localhost check
|
2021-03-18 14:43:17 +01:00
|
|
|
if (window.location.hostname !== status.adminFqdn && window.location.hostname !== 'localhost' && !window.location.hostname.startsWith('192.')) {
|
2019-09-05 22:22:42 +02:00
|
|
|
// user is accessing by IP or by the old admin location (pre-migration)
|
2020-12-21 22:36:43 -08:00
|
|
|
window.location.href = '/setupdns.html' + window.location.search;
|
2019-09-05 22:22:42 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2019-09-05 22:22:42 +02: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);
|
|
|
|
|
}
|
2018-05-01 11:26:57 -07:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
console.log('Running dashboard version ', localStorage.version);
|
2018-05-01 11:26:57 -07:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
// 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
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
Client.refreshConfig(function (error) {
|
|
|
|
|
if (error) return Client.initError(error, init);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2020-11-18 00:28:10 +01:00
|
|
|
Client.refreshAvailableLanguages(function (error) {
|
2019-09-05 22:22:42 +02:00
|
|
|
if (error) return Client.initError(error, init);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2020-11-18 00:28:10 +01:00
|
|
|
Client.refreshInstalledApps(function (error) {
|
|
|
|
|
if (error) return Client.initError(error, init);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2020-11-18 00:28:10 +01:00
|
|
|
// now mark the Client to be ready
|
|
|
|
|
Client.setReady();
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2020-11-18 00:28:10 +01:00
|
|
|
$scope.config = Client.getConfig();
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2020-11-18 00:28:10 +01:00
|
|
|
$scope.initialized = true;
|
2020-07-10 10:43:08 -07:00
|
|
|
|
2020-11-18 00:28:10 +01:00
|
|
|
if (Client.getConfig().mandatory2FA && !Client.getUserInfo().twoFactorAuthenticationEnabled) {
|
|
|
|
|
$location.path('/profile').search({ setup2fa: true });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2021-05-29 12:11:17 -07:00
|
|
|
$interval(refreshNotifications, 60 * 1000);
|
|
|
|
|
refreshNotifications();
|
2020-11-18 00:28:10 +01:00
|
|
|
|
|
|
|
|
$scope.updateSubscriptionStatus();
|
|
|
|
|
});
|
2019-09-05 22:22:42 +02:00
|
|
|
});
|
2018-01-22 13:01:38 -08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-09-05 22:22:42 +02:00
|
|
|
}
|
2018-01-22 13:01:38 -08:00
|
|
|
|
|
|
|
|
Client.onConfig(function (config) {
|
|
|
|
|
if (config.cloudronName) {
|
|
|
|
|
document.title = config.cloudronName;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}]);
|