Skip to content

Commit

Permalink
fix(servers): fix an initialization issue on clean install
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Jul 16, 2015
1 parent a0dac9f commit fa47035
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 150 deletions.
7 changes: 7 additions & 0 deletions src/scripts/opennms/controllers/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,19 @@
//console.log('not default server: ' + $scope.servers[i].name);
}
}
if (angular.isUndefined($scope.defaultServer) && len > 0) {
Settings.setDefaultServerName($scope.servers[0].name);
}
};

var init = function() {
Servers.all().then(function(servers) {
$scope.servers = servers;
initDefaultServer();
if ($scope.launchAdd) {
$scope.launchAdd = false;
$scope.addServer();
}
});
Settings.get().then(function(settings) {
$scope.settings = settings;
Expand Down
16 changes: 9 additions & 7 deletions src/scripts/opennms/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,17 @@
}
};
updateTheme();

util.onInfoUpdated(updateTheme);

Servers.getDefault().then(function(server) {
console.log('main: getDefault: ' + angular.toJson(server));
if (!server) {
Modals.settings(true);
}
}, function(err) {
console.log('main: error: ' + angular.toJson(err));
});

var init = function() {
console.log('Ionic is ready.');
IAP.init().then(Ads.init);
Expand All @@ -129,11 +137,5 @@
}
});
*/

Servers.getDefault().then(function(server) {
if (!server) {
Modals.settings();
}
});
});
}());
3 changes: 2 additions & 1 deletion src/scripts/opennms/services/Modals.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,9 @@
modal.scope.show();
});
},
settings: function() {
settings: function(launchAdd) {
$scope.settingsModal.promise.then(function(modal) {
modal.scope.launchAdd = !!launchAdd;
modal.scope.show();
});
},
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/opennms/services/Resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@

var _getFavoritesPrefix = function() {
return Servers.getDefault().then(function(server) {
if (server.name && server.username) {
if (server && angular.isDefined(server.name) && angular.isDefined(server.username)) {
return ['favorite', server.name, server.username].join(':');
} else {
return $q.reject('Server unconfigured.');
Expand Down
35 changes: 24 additions & 11 deletions src/scripts/opennms/services/Servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
return StorageService.save(fsPrefix + '/' + encodeURIComponent(server.name) + '.json', server).then(function() {
checkServersUpdated();
return server;
}, function(err) {
console.log('Servers.saveServer: WARNING: StorageService.save failed: ' + angular.toJson(err));
return undefined;
});
};

Expand All @@ -81,21 +84,21 @@
});
console.log('Servers.init: saving default server: ' + angular.toJson(server, true));
return saveServer(server).then(function() {
ready.resolve(true);
return server;
});
} else {
return $q.reject('No servers configured.');
}
});
} else {
ready.resolve(true);
return names;
}
}).then(function() {
ready.resolve(true);
return ready.promise;
}, function(err) {
console.log('Servers.init: failed initialization: ' + angular.toJson(err));
ready.resolve(true);
ready.resolve(false);
return ready.promise;
});
};
Expand All @@ -104,7 +107,13 @@
return ready.promise.then(function() {
return StorageService.load(fsPrefix + '/' + encodeURIComponent(serverName) + '.json').then(function(data) {
return new Server(data);
}, function(err) {
console.log('Servers.getServer: failed to get ' + serverName + ' from the filesystem: ' + angular.toJson(err));
return $q.reject(err);
});
}, function(err) {
console.log('Servers.getServer: failed to get ' + serverName + ': ' + angular.toJson(err));
return $q.reject(err);
});
};

Expand All @@ -123,6 +132,9 @@
}
return servers;
});
}, function(err) {
console.log('Servers.getServers: failed: ' + angular.toJson(err));
return [];
});
};

Expand All @@ -136,19 +148,20 @@
return ready.promise.then(function() {
return Settings.getDefaultServerName().then(function(serverName) {
//console.log('Servers.getDefaultServer: ' + serverName);
return getServer(serverName);
if (serverName) {
return getServer(serverName);
} else {
return undefined;
}
});
}, function(err) {
console.log('Servers.getDefaultServer: failed: ' + angular.toJson(err));
return undefined;
});
};

var setDefaultServer = function(server) {
var serverName;
if (angular.isString(server)) {
serverName = server;
} else if (server && server.name) {
serverName = server.name;
}

var serverName = server.name? server.name:server;
if (serverName) {
return Settings.setDefaultServerName(serverName).then(function() {
return checkServersUpdated();
Expand Down
Loading

0 comments on commit fa47035

Please sign in to comment.