2018-01-22 13:01:38 -08:00
'use strict' ;
angular . module ( 'Application' ) . controller ( 'MainController' , [ '$scope' , '$route' , '$timeout' , '$location' , 'Client' , 'AppStore' , function ( $scope , $route , $timeout , $location , Client , AppStore ) {
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 . appstoreConfig = { } ;
2018-03-28 12:26:24 +02:00
$scope . subscription = { } ;
2019-01-07 17:23:26 +01:00
$scope . notifications = [ ] ;
2018-03-28 12:26:24 +02:00
$scope . ready = false ;
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 . error = function ( error ) {
console . error ( error ) ;
window . location . href = '/error.html' ;
} ;
$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 . waitForPlanSelection = function ( ) {
if ( $scope . waitingForPlanSelection ) return ;
$scope . waitingForPlanSelection = true ;
function checkPlan ( ) {
if ( ! $scope . waitingForPlanSelection ) return ;
AppStore . getSubscription ( $scope . appstoreConfig , function ( error , result ) {
if ( error ) return console . error ( error ) ;
// check again to give more immediate feedback once a subscription was setup
2018-03-14 23:28:36 +01:00
if ( result . plan . id === 'free' ) {
2018-01-22 13:01:38 -08:00
$timeout ( checkPlan , 5000 ) ;
} else {
$scope . waitingForPlanSelection = false ;
$ ( '#setupSubscriptionModal' ) . modal ( 'hide' ) ;
if ( $scope . config . update && $scope . config . update . box ) $ ( '#updateModal' ) . modal ( 'show' ) ;
}
} ) ;
}
checkPlan ( ) ;
} ;
$scope . showSubscriptionModal = function ( ) {
$ ( '#setupSubscriptionModal' ) . modal ( 'show' ) ;
} ;
2019-01-08 14:08:29 +01:00
function refreshNotifications ( ) {
Client . getNotifications ( false , 1 , 20 , function ( error , results ) {
2019-01-08 14:33:47 +01:00
$timeout ( refreshNotifications , 10000 ) ;
if ( error ) return console . error ( error ) ;
2019-01-08 14:08:29 +01:00
var newNotifications = results . filter ( function ( a ) {
return ! $scope . notifications . find ( function ( b ) {
return a . id === b . id ;
} ) ;
} ) ;
newNotifications . forEach ( function ( n ) {
$scope . notifications . push ( n ) ;
2019-01-10 13:29:59 +01:00
Client . notify ( n . title , n . message , false , 'info' , '/#/notifications' ) ;
2019-01-08 14:08:29 +01:00
} ) ;
} ) ;
}
2018-01-22 13:01:38 -08:00
function runConfigurationChecks ( ) {
2018-03-30 15:12:14 +02:00
if ( $scope . config . update && $scope . config . update . box ) {
2019-01-07 17:30:01 +01:00
Client . notify ( 'Update Available' , 'Update now to version ' + $scope . config . update . box . version + '.' , true , 'success' , '/#/settings' ) ;
2018-01-22 13:01:38 -08:00
}
Client . getBackupConfig ( function ( error , backupConfig ) {
if ( error ) return console . error ( error ) ;
if ( backupConfig . provider === 'noop' ) {
2019-01-07 17:30:01 +01:00
Client . notify ( 'Backup Configuration' , 'Cloudron backups are disabled. Please ensure this server is backed up using alternate means.' , false , 'info' , '/#/backups' ) ;
2018-06-14 12:59:25 -07:00
} else if ( backupConfig . provider === 'filesystem' && ! backupConfig . externalDisk ) {
Client . notify ( 'Backup Configuration' ,
2018-07-24 22:40:27 -07:00
'Cloudron backups are currently on the same disk as the Cloudron server instance. This is dangerous and can lead to complete data loss if the disk fails.' ,
2019-01-07 17:30:01 +01:00
false /* persistent */ , 'info' , '/#/backups' ) ;
2018-01-22 13:01:38 -08:00
}
} ) ;
2018-11-26 08:48:58 +01:00
Client . isRebootRequired ( function ( error , isRequired ) {
if ( error ) return console . error ( error ) ;
if ( ! isRequired ) return ;
2019-01-07 17:30:01 +01:00
Client . notify ( 'Reboot Required' , 'To finish security updates, a reboot is necessary.' , true /* persistent */ , 'warning' , '/#/system' ) ;
2018-11-26 08:48:58 +01:00
} ) ;
2019-01-07 17:23:26 +01:00
2019-01-08 14:08:29 +01:00
refreshNotifications ( ) ;
2018-01-22 13:01:38 -08:00
}
2018-03-28 14:19:34 +02:00
$scope . fetchAppstoreProfileAndSubscription = function ( callback ) {
2018-04-23 20:54:54 +02:00
Client . getAppstoreConfig ( function ( error , appstoreConfig ) {
2018-03-28 14:32:21 +02:00
if ( error ) return callback ( error ) ;
2018-04-23 20:54:54 +02:00
if ( ! appstoreConfig . token ) return callback ( ) ;
2018-03-28 14:19:34 +02:00
2018-04-23 20:54:54 +02:00
AppStore . getProfile ( appstoreConfig . token , function ( error , result ) {
2018-03-28 14:32:21 +02:00
if ( error ) return callback ( error ) ;
2018-03-28 14:19:34 +02:00
2018-04-23 20:54:54 +02:00
// assign late to avoid UI flicketing on update
appstoreConfig . profile = result ;
2018-04-26 14:49:14 +02:00
$scope . appstoreConfig = appstoreConfig ;
2018-04-05 23:27:39 +02:00
AppStore . getSubscription ( $scope . appstoreConfig , function ( error , result ) {
if ( error ) return callback ( error ) ;
$scope . subscription = result ;
2018-03-28 14:32:21 +02:00
2018-04-05 23:27:39 +02:00
callback ( ) ;
} ) ;
2018-03-28 14:19:34 +02:00
} ) ;
} ) ;
2018-04-05 21:49:15 +02:00
} ;
2018-03-28 14:19:34 +02:00
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 ; } ) ;
} ;
2018-01-22 13:01:38 -08:00
Client . getStatus ( function ( error , status ) {
if ( error ) return $scope . error ( error ) ;
// 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 ) ;
2018-12-15 16:08:44 -08:00
if ( status . restore . active || status . restore . errorMessage ) { // show the error message in restore page
2018-07-29 20:33:21 -07:00
window . location . href = '/restore.html' ;
} else {
window . location . href = status . adminFqdn ? '/setup.html' : '/setupdns.html' ;
}
2018-01-22 13:01:38 -08:00
return ;
}
// 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 ;
}
$scope . status = status ;
2018-05-01 11:26:57 -07: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 ) ;
2018-05-01 11:29:33 -07:00
// get user profile as the first thing. this populates the "scope" and affects subsequent API calls
Client . refreshUserInfo ( function ( error ) {
2018-01-22 13:01:38 -08:00
if ( error ) return $scope . error ( error ) ;
2018-05-01 11:29:33 -07:00
Client . refreshConfig ( function ( error ) {
2018-01-22 13:01:38 -08:00
if ( error ) return $scope . error ( error ) ;
Client . refreshInstalledApps ( function ( error ) {
if ( error ) return $scope . error ( error ) ;
// now mark the Client to be ready
Client . setReady ( ) ;
$scope . config = Client . getConfig ( ) ;
$scope . initialized = true ;
2018-10-30 23:52:09 -07:00
if ( ! $scope . config . managed ) {
2018-01-22 13:01:38 -08:00
runConfigurationChecks ( ) ;
2018-04-05 23:27:39 +02:00
$scope . fetchAppstoreProfileAndSubscription ( function ( error ) {
if ( error ) console . error ( error ) ;
2018-03-28 14:32:21 +02:00
2018-04-05 23:27:39 +02:00
$scope . ready = true ;
2018-03-14 19:53:31 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
}
} ) ;
} ) ;
} ) ;
} ) ;
Client . onConfig ( function ( config ) {
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 ( ) ;
} ) ;
} ) ;
} ] ) ;