From 8ad216dedf017f3d6de047a25d08db3b98e16361 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 9 Mar 2016 15:33:52 +0100 Subject: test --- client/index.html | 2 +- client/js/controllers/image/image.js | 6 +++++- client/js/services/Image.js | 20 +++++++++++++++++++- client/partials/image/image.html | 7 +++++++ 4 files changed, 32 insertions(+), 3 deletions(-) (limited to 'client') diff --git a/client/index.html b/client/index.html index a06994c..7ff859d 100644 --- a/client/index.html +++ b/client/index.html @@ -66,7 +66,7 @@ - + diff --git a/client/js/controllers/image/image.js b/client/js/controllers/image/image.js index e298fcc..d499acb 100644 --- a/client/js/controllers/image/image.js +++ b/client/js/controllers/image/image.js @@ -3,7 +3,7 @@ * * @param {$scope} $scope The $scope service from angular */ -mainApp.controller('imageCtrl', ['$scope', 'Image', 'Loading', function ($scope, Image, Loading) +mainApp.controller('imageCtrl', ['$scope', 'Image', 'Loading',function ($scope, Image, Loading) { var callbackTest=function(){ $scope.images=Image.getData().images; @@ -17,5 +17,9 @@ mainApp.controller('imageCtrl', ['$scope', 'Image', 'Loading', function ($scope, else{ callbackTest(); } + + $scope.doUpload = function () { + Image.uploadImage($scope.myFile,function(){}); + }; }]); diff --git a/client/js/services/Image.js b/client/js/services/Image.js index 2e8c56f..decb5b2 100644 --- a/client/js/services/Image.js +++ b/client/js/services/Image.js @@ -44,6 +44,23 @@ mainApp.factory('Image',[ '$http', 'Identity', function($http, Identity){ }; + var uploadImage=function(fileToUpload, callback) { + + + var result=$http.post('../server/index.php', + $.param({"token" : Identity.getToken(), "task" : "image", 'action':'uploadImage', 'filename':fileToUpload, 'id':'6564'})); + + // Wait and handle the response + result.then(function (response){ + callback(parseUploadImageAnswer(response, false)); + },function(response){ + callback(parseUploadImageAnswer(response, true)); + }); + + + + } + var getData=function(response){ return data; @@ -52,7 +69,8 @@ mainApp.factory('Image',[ '$http', 'Identity', function($http, Identity){ // Return services objects return { getImages:getImages, - getData:getData + getData:getData, + uploadImage:uploadImage }; diff --git a/client/partials/image/image.html b/client/partials/image/image.html index 8af8af5..3f597fb 100644 --- a/client/partials/image/image.html +++ b/client/partials/image/image.html @@ -6,5 +6,12 @@
{{image.name}}
+
+ + +
+ + + -- cgit v1.2.3 From 004dc2acdd2dea2dff8af670f855c40b54f38f4b Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 16 Mar 2016 15:36:12 +0100 Subject: Test --- client/partials/image/image.html | 23 +++++++++++++++++++---- index.php | 3 +++ 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'client') diff --git a/client/partials/image/image.html b/client/partials/image/image.html index 8af8af5..620eda8 100644 --- a/client/partials/image/image.html +++ b/client/partials/image/image.html @@ -1,10 +1,25 @@
- Images disponibles + Image Manager
-
- {{image.name}} -
+ + + + + + + + + + + + + + + + + +
NameSizeAction
{{ image.name }}000actions
diff --git a/index.php b/index.php index e69de29..cf60860 100644 --- a/index.php +++ b/index.php @@ -0,0 +1,3 @@ + -- cgit v1.2.3 From f01cfa2ec71672e2b178c06f87037d8b793ddd67 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 16 Mar 2016 16:05:24 +0100 Subject: Make image manager --- client/partials/image/image.html | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'client') diff --git a/client/partials/image/image.html b/client/partials/image/image.html index 471ce3c..c90cbe0 100644 --- a/client/partials/image/image.html +++ b/client/partials/image/image.html @@ -4,6 +4,14 @@
+ + + +
+ + +
+

@@ -15,13 +23,16 @@ - - + + - - +
{{ image.name }}000actions{{ (image.size / 1048576).toFixed(2) }} MB
+ + + +
-- cgit v1.2.3 From 0ce2b2fa301dde1c5b9c50c826d3ed44b00a5c44 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 16 Mar 2016 18:01:03 +0100 Subject: Add overlay upload --- client/index.html | 2 +- client/partials/image/image.html | 8 ++++---- client/partials/image/upload.html | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 client/partials/image/upload.html (limited to 'client') diff --git a/client/index.html b/client/index.html index 7ff859d..08be038 100644 --- a/client/index.html +++ b/client/index.html @@ -23,7 +23,7 @@
- +
diff --git a/client/partials/image/image.html b/client/partials/image/image.html index c90cbe0..886a11d 100644 --- a/client/partials/image/image.html +++ b/client/partials/image/image.html @@ -1,14 +1,14 @@ -
+ + +
Image Manager
- -
- +

diff --git a/client/partials/image/upload.html b/client/partials/image/upload.html new file mode 100644 index 0000000..9049abf --- /dev/null +++ b/client/partials/image/upload.html @@ -0,0 +1,25 @@ +
- - + + - + - + - + - + - + - - + + + + - - diff --git a/client/js/controllers/login.js b/client/js/controllers/login.js index fed358d..93a373e 100644 --- a/client/js/controllers/login.js +++ b/client/js/controllers/login.js @@ -5,62 +5,61 @@ * @param {$sce} $sce The $sce angular service * @param {$http} $http The $http angular service * @param {Identity} The Identity service - + */ -mainApp.controller('loginCtrl', ['$scope','$sce','Identity', function ($scope,$sce, Identity) -{ - // Check for login and define default states - if(!Identity.isAlreadyLogin()){ - $('#loginModal').modal({backdrop: 'static', keyboard: false}); - } +mainApp.controller('loginCtrl', ['$scope', '$sce', 'Identity', function ($scope, $sce, Identity) + { + // Check for login and define default states + if (!Identity.isAlreadyLogin()) { + $('#loginModal').modal({backdrop: 'static', keyboard: false}); + } - // Manager logout event - $scope.$on('logoutEvent', function(){ - $('#loginModal').modal({backdrop: 'static', keyboard: false}); - }); + // Manager logout event + $scope.$on('logoutEvent', function () { + $('#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 - $('#loginButton').hide(); - $('#loadingLoginButton').show(); + // Hide loading button and message alert + $('#loadingLoginButton').hide(); $('#failedToLoginAlert').hide(); - // Get data from templates - var username=$("#loginFormUsername").val(); - var password=$("#loginFormPassword").val(); - var projectname=$("#loginFormProjectname").val(); - // Function to call to handle result - var responseCallback=function(response){ + // Defined function for login + $scope.loginAction = function () { - if(response.status!==0){ - // Set reason of fail - $scope.failReason=response.failReason; + // Begin login state for template + $('#loginButton').hide(); + $('#loadingLoginButton').show(); + $('#failedToLoginAlert').hide(); - // Display the error - $('#failedToLoginAlert').show(); - } - else { - // Else the user is online ! - $('#loginModal').modal('hide'); + // Get data from templates + var username = $("#loginFormUsername").val(); + var password = $("#loginFormPassword").val(); + var projectname = $("#loginFormProjectname").val(); + + // Function to call to handle result + var responseCallback = function (response) { + + if (response.status !== 0) { + // Set reason of fail + $scope.failReason = response.failReason; + + // Display the error + $('#failedToLoginAlert').show(); + } else { + // Else the user is online ! + $('#loginModal').modal('hide'); + } + + // Reset button state + $('#loginButton').show(); + $('#loadingLoginButton').hide(); } - // Reset button state - $('#loginButton').show(); - $('#loadingLoginButton').hide(); - } - - // Try to login - Identity.login(username, password, projectname, responseCallback); - }; - -}]); + // Try to login + Identity.login(username, password, projectname, responseCallback); + }; + + }]); -- cgit v1.2.3 From 8e700c2b7de7a2bc095ea8edf3204f160f8a8941 Mon Sep 17 00:00:00 2001 From: Loic GUEGAN Date: Sun, 17 Apr 2016 18:42:15 +0200 Subject: Add comments and correct some bugs --- client/js/app.js | 2 +- client/js/controllers/home/home.js | 104 +++++++++++++++++-------------------- client/js/controllers/login.js | 6 ++- client/js/controllers/status.js | 20 ++++--- client/js/services/Compute.js | 2 - client/partials/home/home.html | 11 ++-- 6 files changed, 68 insertions(+), 77 deletions(-) (limited to 'client') diff --git a/client/js/app.js b/client/js/app.js index e911050..4ef35b5 100644 --- a/client/js/app.js +++ b/client/js/app.js @@ -25,7 +25,7 @@ mainApp.config(['$routeProvider', function($routeProvider){ controller: 'imageCtrl' }) .otherwise({ - redirectTo: '/' + redirectTo: '/home' }); }]); diff --git a/client/js/controllers/home/home.js b/client/js/controllers/home/home.js index 10142c9..7880e12 100755 --- a/client/js/controllers/home/home.js +++ b/client/js/controllers/home/home.js @@ -3,58 +3,52 @@ * * @param {$scope} $scope The $scope service from angular */ -mainApp.controller('homeCtrl', [ '$scope', 'Compute', '$rootScope', 'Loading','Identity', 'Image', function ($scope, Compute, $rootScope, Loading, Identity, Image) -{ - - var callMeAfterPullData=function(data){ - console.log(data); - $scope.machines=Compute.getData().machines; - Loading.stop(); - } - - ; - if(Compute.getData().machines == null && Identity.isAlreadyLogin()){ - Loading.start(); - Compute.pullData(callMeAfterPullData); - } - else{ - if(Identity.isAlreadyLogin()){ - callMeAfterPullData(); - } - } - - - Image.getImages(function(){}); - - - - $scope.raiseShowMachineDetailsEvent=function(id){ - - var callback=function(){ - Loading.stop(); - var data=Compute.getData(); - $rootScope.$broadcast("showMachineDetailsEvent", data.machines[id], data.axioms); - - } - Loading.start(); - Compute.pullMachines(callback); - } - - - - - - if(Identity.isAlreadyLogin()){ - if(Compute.getData().machines == null){ - Loading.start(); - Compute.pullData(callMeAfterPullData); - } - else{ - if(Identity.isAlreadyLogin()){ - callMeAfterPullData(); - } - } - Image.getImages(function(){}); - } - -}]); +mainApp.controller('homeCtrl', ['$scope', 'Compute', '$rootScope', 'Loading', 'Identity', 'Image', function ($scope, Compute, $rootScope, Loading, Identity, Image) + { + + // Function to call after pull all data about machines + var callMeAfterPullData = function (data) { + $scope.machines = Compute.getData().machines; + Loading.stop(); + }; + + // Function to call to try to retrieve data and update the view + var tryToRetrieveData = function () { + // If no data retrieve about machine and user is logged + if (Compute.getData().machines == null && Identity.isAlreadyLogin()) { + Loading.start(); // Show loading gif + Compute.pullData(callMeAfterPullData); // Retrieve data and call the callback + } else { + // Else if user is logged and data is already retrieve + // simply display data + if (Identity.isAlreadyLogin()) { + callMeAfterPullData(); // Display data + } + } + }; + + + // On user login + $scope.$on('loginEvent', function () { + tryToRetrieveData(); + }); + + + + // Function to call from view to display the details of a machine + $scope.raiseShowMachineDetailsEvent = function (id) { + + // Stop loading gif and display overlay + var callback = function () { + Loading.stop(); + var data = Compute.getData(); + $rootScope.$broadcast("showMachineDetailsEvent", data.machines[id], data.axioms); + + }; + Loading.start(); // Show loading gif + Compute.pullMachines(callback); // Retrieve machine info and display overlay + }; + + // Try to retrieve data for the first time + tryToRetrieveData(); + }]); diff --git a/client/js/controllers/login.js b/client/js/controllers/login.js index 93a373e..4a0de42 100644 --- a/client/js/controllers/login.js +++ b/client/js/controllers/login.js @@ -7,7 +7,7 @@ * @param {Identity} The Identity service */ -mainApp.controller('loginCtrl', ['$scope', '$sce', 'Identity', function ($scope, $sce, Identity) +mainApp.controller('loginCtrl', ['$scope', '$sce', 'Identity', '$rootScope', function ($scope, $sce, Identity, $rootScope) { // Check for login and define default states if (!Identity.isAlreadyLogin()) { @@ -51,12 +51,14 @@ mainApp.controller('loginCtrl', ['$scope', '$sce', 'Identity', function ($scope, } else { // Else the user is online ! $('#loginModal').modal('hide'); + // Send login event + $rootScope.$broadcast("loginEvent"); } // Reset button state $('#loginButton').show(); $('#loadingLoginButton').hide(); - } + }; // Try to login Identity.login(username, password, projectname, responseCallback); diff --git a/client/js/controllers/status.js b/client/js/controllers/status.js index 15850f4..c3e634b 100644 --- a/client/js/controllers/status.js +++ b/client/js/controllers/status.js @@ -6,16 +6,14 @@ * @param {$scope} $scope The $scope service from angular * @param {Identity} The Identity service */ -mainApp.controller('statusCtrl', ['$scope','Identity', '$rootScope', function ($scope, Identity, $rootScope) -{ +mainApp.controller('statusCtrl', ['$scope', 'Identity', '$rootScope', function ($scope, Identity, $rootScope) + { + // Give profile to model + $scope.profile = Identity.getProfile(); - - // Give profile to model - $scope.profile=Identity.getProfile(); - - // Function to logout - $scope.logout=function(){ - Identity.logout(); - }; + // Function to logout + $scope.logout = function () { + Identity.logout(); + }; -}]); + }]); diff --git a/client/js/services/Compute.js b/client/js/services/Compute.js index 2bf28d8..032b402 100644 --- a/client/js/services/Compute.js +++ b/client/js/services/Compute.js @@ -101,8 +101,6 @@ mainApp.factory('Compute',[ '$http', 'Identity', function($http, Identity){ // Wait and handle the response result.then(function (response){ - alert(Identity.getToken()); - callback(parsePullImagesAnswer(response, false)); },function(response){ diff --git a/client/partials/home/home.html b/client/partials/home/home.html index f9f8878..11958d2 100644 --- a/client/partials/home/home.html +++ b/client/partials/home/home.html @@ -1,12 +1,11 @@ -
+
Home
- - Pour charger les machines, recharger la page (temporaire)
- Selectionner une machine: - - + + Selectionner une machine: + +
-- cgit v1.2.3 From fdfdd27a35ebc22222361ed273b6889fec4cf51e Mon Sep 17 00:00:00 2001 From: Loic GUEGAN Date: Sun, 17 Apr 2016 18:43:49 +0200 Subject: Add ';' --- client/js/services/Compute.js | 283 +++++++++++++++++++++--------------------- 1 file changed, 139 insertions(+), 144 deletions(-) (limited to 'client') diff --git a/client/js/services/Compute.js b/client/js/services/Compute.js index 032b402..5e95f22 100644 --- a/client/js/services/Compute.js +++ b/client/js/services/Compute.js @@ -1,145 +1,140 @@ -mainApp.factory('Compute',[ '$http', 'Identity', function($http, Identity){ - - - // Init data - var data={}; - data.machines=null; - data.axioms={} // Contain static data - data.axioms.ram=[128,512,1024,2048,4096]; - data.axioms.disk=[1,2,5,10,25,50,100,150,200] - data.axioms.images={}; // Retrieve after - - - /** - * Parse pullMachines answer - * @param {response} the server response - * @param {boolean} false if the request as been send true else - * @return {requestParserResult} the result of parsing - */ - var parsePullMachinesAnswer=function(response, failedToSendRequest){ - - // Defined return object - var requestParserResult={}; - requestParserResult.status=1; - requestParserResult.failReason=null; - - - if (typeof response.data.Servers !== 'undefined') { - // Set status code - requestParserResult.status=0; - data.machines=response.data.Servers; - - } - else if(failedToSendRequest){ - requestParserResult.failReason="Failed to send PullMachine request"; - } - else{ - requestParserResult.failReason="Error"; - } - return requestParserResult; - }; - - - /** - * Retrieve machine list - * @param {callback} function to call after request complete - */ - var pullMachines=function(callback){ - // Send listServers request - var result=$http.post('../server/index.php', - $.param({"token" : Identity.getToken(), "task" : "compute", "action":"listServers"})); - - // Wait and handle the response - result.then(function (response){ - callback(parsePullMachinesAnswer(response, false)); - },function(response){ - callback(parsePullMachinesAnswer(response, true)); - }); - }; - - - /** - * Parse pullImages answer - * @param {response} the server response - * @param {boolean} false if the request as been send true else - * @return {requestParserResult} the result of parsing - */ - var parsePullImagesAnswer=function(response, failedToSendRequest){ - - // Defined return object - var requestParserResult={}; - requestParserResult.status=1; - requestParserResult.failReason=null; - - - if (typeof response.data.Images !== 'undefined') { - // Set status code - requestParserResult.status=0; - data.axioms.images=response.data.Images; - } - else if(failedToSendRequest){ - requestParserResult.failReason="Failed to send PullImage request"; - } - else{ - requestParserResult.failReason="Error"; - } - return requestParserResult; - }; - - - - - /** - * Retrieve machine list - * @param {callback} function to call after request complete - */ - var pullImages=function(callback){ - // Send listServers request - var result=$http.post('../server/index.php', - $.param({"token" : Identity.getToken(), "task" : "compute", "action":"listImages"})); - - // Wait and handle the response - result.then(function (response){ - callback(parsePullImagesAnswer(response, false)); - },function(response){ - - callback(parsePullImagesAnswer(response, true)); - }); - }; - - - /** - * Retrieve all data - * @param {callback} function to call after request complete - */ - var pullData=function(callback){ - var nextFunction=function(response){ - if(response.status==0){ - pullMachines(callback); - } - else{ - callback(response); - } - } - pullImages(nextFunction); - } - - - /** - * Get Data - * @return {data} return the data object - */ - var getData=function(){ - return data; - } - - // Return services objects - return { - pullMachines: pullMachines, - pullData: pullData, - getData: getData - }; - - -}]); +mainApp.factory('Compute', ['$http', 'Identity', function ($http, Identity) { + + + // Init data + var data = {}; + data.machines = null; + data.axioms = {} // Contain static data + data.axioms.ram = [128, 512, 1024, 2048, 4096]; + data.axioms.disk = [1, 2, 5, 10, 25, 50, 100, 150, 200] + data.axioms.images = {}; // Retrieve after + + + /** + * Parse pullMachines answer + * @param {response} the server response + * @param {boolean} false if the request as been send true else + * @return {requestParserResult} the result of parsing + */ + var parsePullMachinesAnswer = function (response, failedToSendRequest) { + + // Defined return object + var requestParserResult = {}; + requestParserResult.status = 1; + requestParserResult.failReason = null; + + + if (typeof response.data.Servers !== 'undefined') { + // Set status code + requestParserResult.status = 0; + data.machines = response.data.Servers; + + } else if (failedToSendRequest) { + requestParserResult.failReason = "Failed to send PullMachine request"; + } else { + requestParserResult.failReason = "Error"; + } + return requestParserResult; + }; + + + /** + * Retrieve machine list + * @param {callback} function to call after request complete + */ + var pullMachines = function (callback) { + // Send listServers request + var result = $http.post('../server/index.php', + $.param({"token": Identity.getToken(), "task": "compute", "action": "listServers"})); + + // Wait and handle the response + result.then(function (response) { + callback(parsePullMachinesAnswer(response, false)); + }, function (response) { + callback(parsePullMachinesAnswer(response, true)); + }); + }; + + + /** + * Parse pullImages answer + * @param {response} the server response + * @param {boolean} false if the request as been send true else + * @return {requestParserResult} the result of parsing + */ + var parsePullImagesAnswer = function (response, failedToSendRequest) { + + // Defined return object + var requestParserResult = {}; + requestParserResult.status = 1; + requestParserResult.failReason = null; + + + if (typeof response.data.Images !== 'undefined') { + // Set status code + requestParserResult.status = 0; + data.axioms.images = response.data.Images; + } else if (failedToSendRequest) { + requestParserResult.failReason = "Failed to send PullImage request"; + } else { + requestParserResult.failReason = "Error"; + } + return requestParserResult; + }; + + + + + /** + * Retrieve machine list + * @param {callback} function to call after request complete + */ + var pullImages = function (callback) { + // Send listServers request + var result = $http.post('../server/index.php', + $.param({"token": Identity.getToken(), "task": "compute", "action": "listImages"})); + + // Wait and handle the response + result.then(function (response) { + callback(parsePullImagesAnswer(response, false)); + }, function (response) { + + callback(parsePullImagesAnswer(response, true)); + }); + }; + + + /** + * Retrieve all data + * @param {callback} function to call after request complete + */ + var pullData = function (callback) { + var nextFunction = function (response) { + if (response.status == 0) { + pullMachines(callback); + } else { + callback(response); + } + }; + pullImages(nextFunction); + }; + + + /** + * Get Data + * @return {data} return the data object + */ + var getData = function () { + return data; + }; + + // Return services objects + return { + pullMachines: pullMachines, + pullData: pullData, + getData: getData + }; + + + }]); -- cgit v1.2.3 From 968eda48cc8f3af2214e347e83937086c5e93ad4 Mon Sep 17 00:00:00 2001 From: Loic GUEGAN Date: Sun, 17 Apr 2016 18:44:29 +0200 Subject: Add ';' and correct indent --- client/js/services/Identity.js | 336 ++++++++++++++++++++--------------------- 1 file changed, 167 insertions(+), 169 deletions(-) (limited to 'client') diff --git a/client/js/services/Identity.js b/client/js/services/Identity.js index 73e5d86..17678ed 100644 --- a/client/js/services/Identity.js +++ b/client/js/services/Identity.js @@ -1,170 +1,168 @@ -mainApp.factory('Identity',[ '$http', '$cookies', '$rootScope', function($http, $cookies, $rootScope){ - - /* Create profile structure to store informations - * about current session - */ - var profile={}; - profile.username=null; - profile.projectname=null; - 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'); - var tokenPart_0InCookie=$cookies.getObject('token.part_0'); - var tokenPart_1InCookie=$cookies.getObject('token.part_1'); - - - // Check if cookie is defined - if(typeof profileInCookie !== 'undefined' - && typeof tokenPart_0InCookie !== 'undefined' - && typeof tokenPart_1InCookie !== 'undefined' - ){ - - //if(token!==null){ - // If yes, put it into variables - angular.extend(profile, profileInCookie); - token=tokenPart_0InCookie+tokenPart_1InCookie; - //} - - // Return I'm Login - return true; - } - - // Show the login overlay - $rootScope.$broadcast("logoutEvent"); - - // 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'); - token=null; - profile.username=null; - profile.projectname=null; - - // Reload Page - //location.reload(); - $rootScope.$broadcast("logoutEvent"); - - } - - - /** - * - * @param {string} response The response to parse - * @param {boolean} to check if the request is send or not - * @returns {requestParserResult} Formated data - */ - 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; - - // 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}); - - // Put token in var - token=response.data.token; - - } - else if(failedToSendRequest){ - requestParserResult.failReason="Failed to send request"; - } - else{ - requestParserResult.failReason="Please check your username, password and project name !"; - } - - - return requestParserResult; - }; - - - /** - * Function to connect to OpenStack - * - * @param {object} $http Angular $http service - * @param {string} username The user name - * @param {string} password The user password - * @param {string} projectname The user project name - * @param {function} function to call when data is avalaible - */ - var login=function(username, password,projectname,callback){ - - // Set profile information (early) - profile.username=username; - profile.projectname=projectname; - - var result=$http.post('../server/index.php', - $.param({"task" : "Authenticate", "user" : username, "password" : password, "project" : projectname})); - - // Wait and handle the response - result.then(function (response){ - callback(parseLoginAnswer(response, false)); - },function(response){ - 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, - getProfile: getProfile, - isAlreadyLogin: isAlreadyLogin, - logout:logout, - getToken:getToken - }; - - -}]); +mainApp.factory('Identity', ['$http', '$cookies', '$rootScope', function ($http, $cookies, $rootScope) { + + /* Create profile structure to store informations + * about current session + */ + var profile = {}; + profile.username = null; + profile.projectname = null; + 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'); + var tokenPart_0InCookie = $cookies.getObject('token.part_0'); + var tokenPart_1InCookie = $cookies.getObject('token.part_1'); + + + // Check if cookie is defined + if (typeof profileInCookie !== 'undefined' + && typeof tokenPart_0InCookie !== 'undefined' + && typeof tokenPart_1InCookie !== 'undefined' + ) { + + //if(token!==null){ + // If yes, put it into variables + angular.extend(profile, profileInCookie); + token = tokenPart_0InCookie + tokenPart_1InCookie; + //} + + // Return I'm Login + return true; + } + + // Show the login overlay + $rootScope.$broadcast("logoutEvent"); + + // 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'); + token = null; + profile.username = null; + profile.projectname = null; + + // Reload Page + //location.reload(); + $rootScope.$broadcast("logoutEvent"); + + }; + + + /** + * + * @param {string} response The response to parse + * @param {boolean} to check if the request is send or not + * @returns {requestParserResult} Formated data + */ + 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; + + // 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}); + + // Put token in var + token = response.data.token; + + } else if (failedToSendRequest) { + requestParserResult.failReason = "Failed to send request"; + } else { + requestParserResult.failReason = "Please check your username, password and project name !"; + } + + + return requestParserResult; + }; + + + /** + * Function to connect to OpenStack + * + * @param {object} $http Angular $http service + * @param {string} username The user name + * @param {string} password The user password + * @param {string} projectname The user project name + * @param {function} function to call when data is avalaible + */ + var login = function (username, password, projectname, callback) { + + // Set profile information (early) + profile.username = username; + profile.projectname = projectname; + + var result = $http.post('../server/index.php', + $.param({"task": "Authenticate", "user": username, "password": password, "project": projectname})); + + // Wait and handle the response + result.then(function (response) { + callback(parseLoginAnswer(response, false)); + }, function (response) { + 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, + getProfile: getProfile, + isAlreadyLogin: isAlreadyLogin, + logout: logout, + getToken: getToken + }; + + + }]); -- cgit v1.2.3 From bb1598713c0b6bbe39209c14681c759ab9907edc Mon Sep 17 00:00:00 2001 From: Loic GUEGAN Date: Sun, 17 Apr 2016 18:50:05 +0200 Subject: Clean code, add comment etc.. --- client/js/controllers/home/machineDetails.js | 55 +++---- client/js/controllers/image/image.js | 34 ++--- client/js/controllers/image/upload.js | 108 +++++++------- client/js/controllers/network/network.js | 1 + client/js/services/Image.js | 214 ++++++++++++++------------- client/js/services/Loading.js | 45 +++--- client/js/services/Network.js | 50 ++++--- 7 files changed, 263 insertions(+), 244 deletions(-) (limited to 'client') diff --git a/client/js/controllers/home/machineDetails.js b/client/js/controllers/home/machineDetails.js index 371310b..9c8c602 100644 --- a/client/js/controllers/home/machineDetails.js +++ b/client/js/controllers/home/machineDetails.js @@ -3,42 +3,43 @@ * * @param {$scope} $scope The $scope service from angular */ -mainApp.controller('machineDetailsCtrl', [ '$scope', 'Compute', '$rootScope', '$timeout', 'Identity', function ($scope, Compute, $rootScope, $timeout, Identity) -{ +mainApp.controller('machineDetailsCtrl', ['$scope', 'Compute', '$rootScope', '$timeout', 'Identity', function ($scope, Compute, $rootScope, $timeout, Identity) + { - // Init scope - $scope.machine={}; - $scope.machineIsStarting=false; // For loading icon + // Init scope + $scope.machine = {}; + $scope.machineIsStarting = false; // For loading icon + // When we need to show details of machine + $scope.$on('showMachineDetailsEvent', function (eventName, machine, axioms) { + $scope.machine = machine; + $scope.axioms = axioms; + $('#machineDetailsModal').modal({backdrop: false, keyboard: true}); + }); - $scope.$on('showMachineDetailsEvent', function(eventName ,machine, axioms){ - $scope.machine=machine; - $scope.axioms=axioms; - $('#machineDetailsModal').modal({backdrop: false, keyboard: true}); - }); + // Try to stop or start a machine + $scope.toggleMachineState = function () { + // Display gif + $scope.machineIsStarting = true; + // Fake timeout + $timeout(function () { + $scope.machineIsStarting = false; + }, 3000); + $timeout(function () { + $scope.machine.online = !$scope.machine.online; - $scope.toggleMachineState=function(){ - // Display gif - $scope.machineIsStarting=true; + }, 3000); - // Fake timeout - $timeout(function(){ - $scope.machineIsStarting=false; - }, 3000); - $timeout(function(){ - $scope.machine.online=!$scope.machine.online; - }, 3000); + }; - - }; + // Apply modifications + $scope.applyModifications = function () { + //Todo + }; - $scope.applyModifications=function(){ - //Todo - } - -}]); + }]); diff --git a/client/js/controllers/image/image.js b/client/js/controllers/image/image.js index d9a9c06..d0578d7 100644 --- a/client/js/controllers/image/image.js +++ b/client/js/controllers/image/image.js @@ -4,23 +4,21 @@ * @param {$scope} $scope The $scope service from angular */ mainApp.controller('imageCtrl', ['$scope', 'Image', 'Loading', 'Identity', function ($scope, Image, Loading, Identity) -{ + { - var callbackTest=function(){ - $scope.images=Image.getData().images; - Loading.stop(); - }; + // Update view + var callMeAfterGetImage = function () { + $scope.images = Image.getData().images; + Loading.stop(); + }; - - if(Identity.isAlreadyLogin()){ - - if(Image.getData().images==null){ - Loading.start(); - Image.getImages(callbackTest); - } - else{ - callbackTest(); - } - - } -}]); + // If user is login try to retrieve data + if (Identity.isAlreadyLogin()) { + if (Image.getData().images == null) { + Loading.start(); + Image.getImages(callMeAfterGetImage); + } else { + callMeAfterGetImage(); + } + } + }]); diff --git a/client/js/controllers/image/upload.js b/client/js/controllers/image/upload.js index d9587d0..6737269 100644 --- a/client/js/controllers/image/upload.js +++ b/client/js/controllers/image/upload.js @@ -3,58 +3,60 @@ * * @param {$scope} $scope The $scope service from angular */ -mainApp.controller('uploadImageCtrl', ['$scope', 'Image', 'Loading', 'Identity', 'upload', function ($scope, Image, Loading, Identity,upload) -{ +mainApp.controller('uploadImageCtrl', ['$scope', 'Image', 'Loading', 'Identity', 'upload', function ($scope, Image, Loading, Identity, upload) + { /*$scope.uploader = new FileUploader({ - "token" : Identity.getToken(), - "task" : "image", - 'action':'uploadImage', - 'id':'6564' - }); - $scope.uploader.url='../server/index.php' - $scope.uploader.alias='file_name' - $scope.uploader.formData={ - "token" : Identity.getToken(), - "task" : "image", - 'action':'uploadImage', - 'id':'6564' - } - */ + "token" : Identity.getToken(), + "task" : "image", + 'action':'uploadImage', + 'id':'6564' + }); + $scope.uploader.url='../server/index.php' + $scope.uploader.alias='file_name' + $scope.uploader.formData={ + "token" : Identity.getToken(), + "task" : "image", + 'action':'uploadImage', + 'id':'6564' + } + */ - $scope.doUpload = function () { - console.log($('#imageToUpload').prop('files')[0]); - Image.uploadImage($('#imageToUpload').prop('files')[0], function(){alert("done")}) - /*$("#drop-area-div").dmUploader({ - extraData: { - "token" : Identity.getToken(), - "task" : "image", - 'action':'uploadImage', - 'id':'6564'}, - url:"../server/index.php" - }); - */ - - /*upload({ - url: '../server/index.php', - method: 'POST', - data: { - "token" : Identity.getToken(), - "task" : "image", - 'action':'uploadImage', - 'id':'6564', - "file_name": $scope.myFile, // a jqLite type="file" element, upload() will extract all the files from the input and put them into the FormData object before sending. - } - }).then( - function (response) { - console.log(response.data); // will output whatever you choose to return from the server on a successful upload - }, - function (response) { - console.error(response); // Will return if status code is above 200 and lower than 300, same as $http - } - );*/ - - - }; - - $scope.token=Identity.getToken(); -}]); + $scope.doUpload = function () { + console.log($('#imageToUpload').prop('files')[0]); + Image.uploadImage($('#imageToUpload').prop('files')[0], function () { + alert("done") + }) + /*$("#drop-area-div").dmUploader({ + extraData: { + "token" : Identity.getToken(), + "task" : "image", + 'action':'uploadImage', + 'id':'6564'}, + url:"../server/index.php" + }); + */ + + /*upload({ + url: '../server/index.php', + method: 'POST', + data: { + "token" : Identity.getToken(), + "task" : "image", + 'action':'uploadImage', + 'id':'6564', + "file_name": $scope.myFile, // a jqLite type="file" element, upload() will extract all the files from the input and put them into the FormData object before sending. + } + }).then( + function (response) { + console.log(response.data); // will output whatever you choose to return from the server on a successful upload + }, + function (response) { + console.error(response); // Will return if status code is above 200 and lower than 300, same as $http + } + );*/ + + + }; + + $scope.token = Identity.getToken(); + }]); diff --git a/client/js/controllers/network/network.js b/client/js/controllers/network/network.js index 7264aec..f600f83 100644 --- a/client/js/controllers/network/network.js +++ b/client/js/controllers/network/network.js @@ -5,4 +5,5 @@ */ mainApp.controller('networkCtrl', function ($scope) { + }); \ No newline at end of file diff --git a/client/js/services/Image.js b/client/js/services/Image.js index 4947a1d..795f85e 100644 --- a/client/js/services/Image.js +++ b/client/js/services/Image.js @@ -1,102 +1,114 @@ -mainApp.factory('Image',[ '$http', 'Identity', function($http, Identity){ - - var data={}; - data.images=null; - - - var parseUploadImageAnswer=function(response, failedToSendRequest){ - - // Defined return object - var requestParserResult={}; - requestParserResult.status=1; - requestParserResult.failReason=null; - - - if (typeof response.data.Images !== 'undefined') { - // Set status code - requestParserResult.status=0; - data.images=response.data.Images; - - } - else if(failedToSendRequest){ - requestParserResult.failReason="Failed to send request"; - } - else{ - requestParserResult.failReason="Error"; - } - return requestParserResult; - }; - - - var getImages=function(callback){ - - var result=$http.post('../server/index.php', - $.param({"token" : Identity.getToken(), "task" : "image", 'action':'listImage'})); - - // Wait and handle the response - result.then(function (response){ - callback(parseUploadImageAnswer(response, false)); - },function(response){ - callback(parseUploadImageAnswer(response, true)); - }); - - - }; - - var uploadImage=function(fileToUpload, callback) { - var form_data = new FormData(); - form_data.append('file', fileToUpload); - console.log(fileToUpload); - form_data.append("task" , "image"); - form_data.append("token" , Identity.getToken()); - form_data.append('action',"uploadImage"); - form_data.append('id','6564'); - form_data.append('file_name', fileToUpload); - - $.ajax({ - url: "../server/index.php", // Url to which the request is send - type: "POST", // Type of request to be send, called as method - data: form_data, // Data sent to server, a set of key/value pairs (i.e. form fields and values) - file_name:fileToUpload, - token : Identity.getToken(), - task : "image", - action:'uploadImage', - id:'6564', - contentType: false, // The content type used when sending data to the server. - cache: false, // To unable request pages to be cached - processData:false, // To send DOMDocument or non processed data file it is set to false - success: function(data) // A function to be called if request succeeds - { - alert("success"); - } - }); - - //var result=$http.post('../server/index.php', - // $.param({"token" : Identity.getToken(), "task" : "image", 'action':'uploadImage', 'file_name':form_data, 'id':'6564'})); - - // Wait and handle the response - /* result.then(function (response){ - callback(parseUploadImageAnswer(response, false)); - },function(response){ - callback(parseUploadImageAnswer(response, true)); - });*/ - - - - } - - - var getData=function(response){ - return data; - }; - - // Return services objects - return { - getImages:getImages, - getData:getData, - uploadImage:uploadImage - }; - - -}]); +mainApp.factory('Image', ['$http', 'Identity', function ($http, Identity) { + + // Data object + var data = {}; + data.images = null; // Images + + /** + * Parse uploadImage anwser + * @param {type} response + * @param {type} failedToSendRequest + * @returns {Image_L2.parseUploadImageAnswer.requestParserResult} + */ + var parseUploadImageAnswer = function (response, failedToSendRequest) { + + // Defined return object + var requestParserResult = {}; + requestParserResult.status = 1; + requestParserResult.failReason = null; + + + if (typeof response.data.Images !== 'undefined') { + // Set status code + requestParserResult.status = 0; + data.images = response.data.Images; + + } else if (failedToSendRequest) { + requestParserResult.failReason = "Failed to send request"; + } else { + requestParserResult.failReason = "Error"; + } + return requestParserResult; + }; + + + /** + * Get images + * @param {type} callback + * @returns {undefined} + */ + var getImages = function (callback) { + + var result = $http.post('../server/index.php', + $.param({"token": Identity.getToken(), "task": "image", 'action': 'listImage'})); + + // Wait and handle the response + result.then(function (response) { + callback(parseUploadImageAnswer(response, false)); + }, function (response) { + callback(parseUploadImageAnswer(response, true)); + }); + + + }; + + /** + * Upload an image + * @param {type} fileToUpload + * @param {type} callback + * @returns {undefined} + */ + var uploadImage = function (fileToUpload, callback) { + var form_data = new FormData(); + form_data.append('file', fileToUpload); + console.log(fileToUpload); + form_data.append("task", "image"); + form_data.append("token", Identity.getToken()); + form_data.append('action', "uploadImage"); + form_data.append('id', '6564'); + form_data.append('file_name', fileToUpload); + + $.ajax({ + url: "../server/index.php", // Url to which the request is send + type: "POST", // Type of request to be send, called as method + data: form_data, // Data sent to server, a set of key/value pairs (i.e. form fields and values) + file_name: fileToUpload, + token: Identity.getToken(), + task: "image", + action: 'uploadImage', + id: '6564', + contentType: false, // The content type used when sending data to the server. + cache: false, // To unable request pages to be cached + processData: false, // To send DOMDocument or non processed data file it is set to false + success: function (data) // A function to be called if request succeeds + { + alert("success"); + } + }); + + //var result=$http.post('../server/index.php', + // $.param({"token" : Identity.getToken(), "task" : "image", 'action':'uploadImage', 'file_name':form_data, 'id':'6564'})); + + // Wait and handle the response + /* result.then(function (response){ + callback(parseUploadImageAnswer(response, false)); + },function(response){ + callback(parseUploadImageAnswer(response, true)); + });*/ + }; + + + var getData = function (response) { + return data; + }; + + // Return services objects + return { + getImages: getImages, + getData: getData, + uploadImage: uploadImage + }; + + + }]); diff --git a/client/js/services/Loading.js b/client/js/services/Loading.js index db06194..7ea2b42 100644 --- a/client/js/services/Loading.js +++ b/client/js/services/Loading.js @@ -1,23 +1,26 @@ +/** + * Loading service + * @param {type} param1 + * @param {type} param2 + */ +mainApp.factory('Loading', [function () { + /** + * Display Loading modal + */ + var start = function () { + $('#loadingModal').modal({backdrop: 'static', keyboard: false}); + }; -mainApp.factory('Loading',[ function(){ - /** - * Display Loading modal - */ - var start=function(){ - $('#loadingModal').modal({backdrop: 'static', keyboard: false}); - }; + /** + * Hide Loading modal + */ + var stop = function () { + $('#loadingModal').modal('hide'); + }; - /** - * Hide Loading modal - */ - var stop=function(){ - $('#loadingModal').modal('hide'); - } - - - // Service returns - return { - start:start, - stop:stop - }; -}]); + // Service returns + return { + start: start, + stop: stop + }; + }]); diff --git a/client/js/services/Network.js b/client/js/services/Network.js index bd2a24f..a0cd5a4 100644 --- a/client/js/services/Network.js +++ b/client/js/services/Network.js @@ -1,27 +1,29 @@ -mainApp.factory('Network',[ '$http', 'Identity', function($http, Identity){ - - var data={}; - data.networks=null; - -var ListId=function(fileToUpload, callback) { - - - var result=$http.post('../server/index.php', - $.param({"token" : Identity.getToken(), "task" : "network", 'action':'list_network_ids'})); - - // Wait and handle the response - result.then(function (response){ - callback(parseUploadImageAnswer(response, false)); - },function(response){ - callback(parseUploadImageAnswer(response, true)); - }); - - console.log(result) - - } - - -}]); +mainApp.factory('Network', ['$http', 'Identity', function ($http, Identity) { + + // Data object + var data = {}; + data.networks = null; // Networks + + /** + * ListId + * @param {type} fileToUpload + * @param {type} callback + * @returns {undefined} + */ + var ListId = function (fileToUpload, callback) { + var result = $http.post('../server/index.php', + $.param({"token": Identity.getToken(), "task": "network", 'action': 'list_network_ids'})); + + // Wait and handle the response + result.then(function (response) { + callback(parseUploadImageAnswer(response, false)); + }, function (response) { + callback(parseUploadImageAnswer(response, true)); + }); + }; + + + }]); -- cgit v1.2.3