This commit is contained in:
Yoggzo 2016-02-29 14:20:13 +01:00
parent 6349739a6c
commit cfce59d6dc
47 changed files with 500 additions and 52 deletions

View file

@ -0,0 +1,39 @@
/**
* The home controller
*
* @param {$scope} $scope The $scope service from angular
*/
mainApp.controller('machineDetailsCtrl', [ '$scope', 'Compute', '$rootScope', '$timeout', function ($scope, Compute, $rootScope, $timeout)
{
$scope.machine={};
$("#waitingForToggleMachine").hide();
$scope.$on('showMachineDetailsEvent', function(eventName ,machine){
$scope.machine=machine;
$('#machineDetailsModal').modal({backdrop: false, keyboard: true});
});
$scope.toggleMachineState=function(){
$("#waitingForToggleMachine").show();
// Fake timeout
$timeout(function(){
$("#waitingForToggleMachine").hide();
}, 3000);
$timeout(function(){
$scope.machine.online=!$scope.machine.online;
}, 3000);
};
$scope.applyModifications=function(){
//Todo
}
}]);

View file

@ -3,15 +3,22 @@
*
* @param {$scope} $scope The $scope service from angular
*/
mainApp.controller('homeCtrl', [ '$scope', 'Compute', function ($scope, Compute)
mainApp.controller('homeCtrl', [ '$scope', 'Compute', '$rootScope', function ($scope, Compute, $rootScope)
{
var updatePage=function(){
// TODO Update graph etc...
}
// Retrieve all Data
Compute.pullData(updatePage);
Compute.getMachines(function(adzda){});
$scope.raiseShowMachineDetailsEvent=function(){
var machine={name: "Machine 1", online:true};
$rootScope.$broadcast("showMachineDetailsEvent", machine);
}
}]);

View file

@ -13,14 +13,19 @@ mainApp.controller('loginCtrl', ['$scope','$sce','Identity', function ($scope,$s
if(!Identity.isAlreadyLogin()){
$('#loginModal').modal({backdrop: 'static', keyboard: false});
}
// Manager logout event
$scope.$on('logoutEvent', function(){
Identity.logout();
$('#loginModal').modal({backdrop: 'static', keyboard: false});
});
// Hide loading button and message alert
$('#loadingLoginButton').hide();
$('#failedToLoginAlert').hide();
// Defined function for login
$scope.loginAction=function(){
// Begin login state for template

View file

@ -10,13 +10,11 @@ mainApp.controller('statusCtrl', ['$scope','Identity', '$rootScope', function ($
{
// Give profile to model
$scope.profile=Identity.profile;
$scope.profile=Identity.getProfile();
// Function to logout
$scope.logout=function(){
Identity.logout();
$scope.raiseLogoutEvent=function(){
$rootScope.$broadcast('logoutEvent');
};
}]);

View file

@ -2,11 +2,12 @@
mainApp.factory('Compute',[ '$http', 'Identity', function($http, Identity){
// Create data
var data={};
data.machines={};
// Parser
var parseGetMachinesAnswer=function(response, failedToSendRequest){
@ -16,8 +17,24 @@ mainApp.factory('Compute',[ '$http', 'Identity', function($http, Identity){
// Get Machine
var getMachines=function(callback){
var params={
"token" : Identity.getToken(),
"task" : "compute",
"action":"getServer",
"serverId":"69d5bcc4-2fab-4634-b0d2-f455fee5b7bd"
};
var result=$http.post('../server/index.php',
$.param({"token" : Identity.profile.token, "task" : "Compute"}));
$.param(params));
// Wait and handle the response
result.then(function (response){
console.log(response.data.Servers);
callback(parseGetMachinesAnswer(response, false));
},function(response){
alert(response.status);
callback(parseGetMachinesAnswer(response, true));
});
};
@ -31,8 +48,8 @@ mainApp.factory('Compute',[ '$http', 'Identity', function($http, Identity){
// Return services objects
return {
getMachines: getMachines
pullData: pullData
getMachines: getMachines,
pullData: pullData,
data:data
};

View file

@ -7,37 +7,47 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){
var profile={};
profile.username=null;
profile.projectname=null;
profile.token=null;
/**
* Save profile in cookies
*/
var saveCookieForSession=function(){
$cookies.putObject('profile', 5);
};
var token=null;
/*
* @returns {boolean} Return true if a cookie is found (and load it in profile) false else
*/
var isAlreadyLogin=function(){
// Load cookies
var profileInCookie=$cookies.getObject('profile');
console.log(profileInCookie);
var tokenPart_0InCookie=$cookies.getObject('token.part_0');
var tokenPart_1InCookie=$cookies.getObject('token.part_1');
if(typeof profileInCookie !== 'undefined'){
// Check if cookie is defined
if(typeof profileInCookie !== 'undefined'
&& typeof tokenPart_0InCookie !== 'undefined'
&& typeof tokenPart_1InCookie !== 'undefined'
){
// If yes, put it into variables
angular.extend(profile, profileInCookie);
token=tokenPart_0InCookie+tokenPart_1InCookie;
// Return I'm Login
return true;
}
// Return I'm not Login
return false;
}
/*
* Destroy profile cookies
*/
var logout=function(){
$cookies.remove('profile');
$cookies.remove('token.part_0');
$cookies.remove('token.part_1');
}
@ -49,15 +59,28 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){
*/
var parseLoginAnswer=function(response, failedToSendRequest){
// Defined return object
var requestParserResult={};
requestParserResult.status=1;
requestParserResult.failReason=null;
if (typeof response.data.token !== 'undefined') {
// Set status code
requestParserResult.status=0;
profile.token=response.data.token;
saveCookieForSession();
// Find the middle of the token to split it
var middle=parseInt(response.data.token.length/2);
// Create expire date (cookie expire in 55 mins)
var expireDate=new Date();
expireDate.setMinutes(expireDate.getMinutes()+55);
// Save profile
$cookies.putObject('profile', profile, {'expires': expireDate});
// Save first part of token
$cookies.putObject('token.part_0', response.data.token.substring(0, middle), {'expires': expireDate});
// Save second part of token
$cookies.putObject('token.part_1', response.data.token.substring(middle, response.data.token.length), {'expires': expireDate});
}
else if(failedToSendRequest){
requestParserResult.failReason="Failed to send request";
@ -69,7 +92,6 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){
return requestParserResult;
};
/**
* Function to connect to OpenStack
@ -87,24 +109,39 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){
profile.projectname=projectname;
var result=$http.post('../server/index.php',
$.param({"task" : "Authenticate", "user" : username, "password" : password, "project" : projectname}));
$.param({"task" : "Authenticate", "user" : username, "password" : password, "project" : projectname}));
// Wait and handle the response
result.then(function (response){
callback(parseLoginAnswer(response), false);
callback(parseLoginAnswer(response, false));
},function(response){
callback(parseLoginAnswer(response), true)
callback(parseLoginAnswer(response, true));
});
};
/*
* Get the profile
*/
var getProfile=function(){
return profile;
}
/*
* Get the token
*/
var getToken=function(){
return token;
}
// Return services objects
return {
login: login,
profile: profile,
getProfile: getProfile,
isAlreadyLogin: isAlreadyLogin,
logout:logout
logout:logout,
getToken:getToken
};