blob: 6a9493b16716fa85acdeced226004bf6920301b8 [file] [log] [blame]
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001window.angular && (function (angular) {
2 'use strict';
3
4 angular
5 .module('app.common.directives')
6 .directive('appHeader', ['APIUtils', function (APIUtils) {
7 return {
8 'restrict': 'E',
9 'templateUrl': 'common/directives/app-header.html',
10 'scope': {
11 'path': '='
12 },
13 'controller': ['$rootScope', '$scope','dataService', 'userModel', '$location', function($rootScope, $scope, dataService, userModel, $location){
14 $scope.dataService = dataService;
15
16 $scope.loadServerStatus = function(){
17 if(!userModel.isLoggedIn()){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050018 return;
19 }
20 APIUtils.getHostState(function(status){
21 if(status == 'xyz.openbmc_project.State.Host.HostState.Off'){
22 dataService.setPowerOffState();
23 }else if(status == 'xyz.openbmc_project.State.Host.HostState.Running'){
24 dataService.setPowerOnState();
25 }else{
26 dataService.setBootingState();
27 }
28 });
29 }
Iftekharul Islamba556c32017-08-11 08:37:12 -050030
31 $scope.loadNetworkInfo = function(){
32 if(!userModel.isLoggedIn()){
33 return;
34 }
35 APIUtils.getNetworkInfo().then(function(data){
36 dataService.setNetworkInfo(data);
37 });
38 }
39
Iftekharul Islam99d199f2017-03-24 15:28:25 -050040 $scope.loadServerStatus();
Iftekharul Islamba556c32017-08-11 08:37:12 -050041 $scope.loadNetworkInfo();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050042
43 $scope.logout = function(){
44 userModel.logout(function(status, error){
45 if(status){
46 $location.path('/logout');
47 }else{
48 console.log(error);
49 }
50 });
51 }
52
53 $scope.refresh = function(){
54 $scope.loadServerStatus();
Iftekharul Islamba556c32017-08-11 08:37:12 -050055 $scope.loadNetworkInfo();
Michael Davisa5f222a2017-08-04 15:02:38 -050056
57 //Add flash class to header timestamp on click of refresh
58 var myEl = angular.element( document.querySelector( '.header__refresh' ) );
59 myEl.addClass('flash');
60 setTimeout(function () {
61 myEl.removeClass("flash");
62 },2000);
63
Iftekharul Islam99d199f2017-03-24 15:28:25 -050064 }
65
66 var loginListener = $rootScope.$on('user-logged-in', function(event, arg){
67 $scope.loadServerStatus();
Iftekharul Islamba556c32017-08-11 08:37:12 -050068 $scope.loadNetworkInfo();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050069 });
70
71 $scope.$on('$destroy', function(){
72 loginListener();
73 });
74 }]
75 };
76 }]);
77})(window.angular);