2017-01-31 23:26:29 +00:00
|
|
|
angular.module('portainer.services')
|
2017-02-01 09:13:48 +00:00
|
|
|
.factory('StateManager', ['$q', 'Config', 'Info', 'InfoHelper', 'Version', 'LocalStorage', function StateManagerFactory($q, Config, Info, InfoHelper, Version, LocalStorage) {
|
2017-01-31 23:26:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var state = {
|
|
|
|
loading: true,
|
|
|
|
application: {},
|
|
|
|
endpoint: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2017-02-01 09:13:48 +00:00
|
|
|
initialize: function() {
|
2017-01-31 23:26:29 +00:00
|
|
|
var endpointState = LocalStorage.getEndpointState();
|
|
|
|
if (endpointState) {
|
|
|
|
state.endpoint = endpointState;
|
|
|
|
}
|
2017-02-01 09:13:48 +00:00
|
|
|
|
|
|
|
var deferred = $q.defer();
|
|
|
|
var applicationState = LocalStorage.getApplicationState();
|
|
|
|
if (applicationState) {
|
|
|
|
state.application = applicationState;
|
|
|
|
state.loading = false;
|
|
|
|
deferred.resolve(state);
|
|
|
|
} else {
|
|
|
|
Config.$promise.then(function success(data) {
|
|
|
|
state.application.authentication = data.authentication;
|
|
|
|
state.application.logo = data.logo;
|
|
|
|
LocalStorage.storeApplicationState(state.application);
|
|
|
|
state.loading = false;
|
|
|
|
deferred.resolve(state);
|
|
|
|
}, function error(err) {
|
|
|
|
state.loading = false;
|
|
|
|
deferred.reject({msg: 'Unable to retrieve server configuration', err: err});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return deferred.promise;
|
2017-01-31 23:26:29 +00:00
|
|
|
},
|
|
|
|
clean: function() {
|
|
|
|
state.endpoint = {};
|
|
|
|
},
|
|
|
|
updateEndpointState: function(loading) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
if (loading) {
|
|
|
|
state.loading = true;
|
|
|
|
}
|
|
|
|
$q.all([Info.get({}).$promise, Version.get({}).$promise])
|
|
|
|
.then(function success(data) {
|
|
|
|
var endpointMode = InfoHelper.determineEndpointMode(data[0]);
|
|
|
|
var endpointAPIVersion = parseFloat(data[1].ApiVersion);
|
|
|
|
state.endpoint.mode = endpointMode;
|
|
|
|
state.endpoint.apiVersion = endpointAPIVersion;
|
|
|
|
LocalStorage.storeEndpointState(state.endpoint);
|
|
|
|
state.loading = false;
|
|
|
|
deferred.resolve();
|
|
|
|
}, function error(err) {
|
|
|
|
state.loading = false;
|
|
|
|
deferred.reject({msg: 'Unable to connect to the Docker endpoint', err: err});
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
getState: function() {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}]);
|