blob: cb4704e20512214d45549388aa4faaff66837d0e [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
Iftekharul Islamc22425f2017-09-06 10:04:14 -050016 $scope.loadServerHealth = function(){
17 APIUtils.getLogs().then(function(result){
18 dataService.updateServerHealth(result.data);
19 });
20 }
21
Iftekharul Islam99d199f2017-03-24 15:28:25 -050022 $scope.loadServerStatus = function(){
23 if(!userModel.isLoggedIn()){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050024 return;
25 }
26 APIUtils.getHostState(function(status){
27 if(status == 'xyz.openbmc_project.State.Host.HostState.Off'){
28 dataService.setPowerOffState();
29 }else if(status == 'xyz.openbmc_project.State.Host.HostState.Running'){
30 dataService.setPowerOnState();
31 }else{
32 dataService.setBootingState();
33 }
34 });
35 }
Iftekharul Islamba556c32017-08-11 08:37:12 -050036
37 $scope.loadNetworkInfo = function(){
38 if(!userModel.isLoggedIn()){
39 return;
40 }
41 APIUtils.getNetworkInfo().then(function(data){
42 dataService.setNetworkInfo(data);
43 });
44 }
45
Iftekharul Islamc22425f2017-09-06 10:04:14 -050046 function loadData(){
47 $scope.loadServerStatus();
48 $scope.loadNetworkInfo();
49 $scope.loadServerHealth();
50 }
51
52 loadData();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050053
54 $scope.logout = function(){
55 userModel.logout(function(status, error){
56 if(status){
57 $location.path('/logout');
58 }else{
59 console.log(error);
60 }
61 });
62 }
63
64 $scope.refresh = function(){
Iftekharul Islamc22425f2017-09-06 10:04:14 -050065 loadData();
Michael Davisa5f222a2017-08-04 15:02:38 -050066
67 //Add flash class to header timestamp on click of refresh
68 var myEl = angular.element( document.querySelector( '.header__refresh' ) );
69 myEl.addClass('flash');
70 setTimeout(function () {
71 myEl.removeClass("flash");
72 },2000);
73
Iftekharul Islam99d199f2017-03-24 15:28:25 -050074 }
75
76 var loginListener = $rootScope.$on('user-logged-in', function(event, arg){
Iftekharul Islamc22425f2017-09-06 10:04:14 -050077 loadData();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050078 });
79
80 $scope.$on('$destroy', function(){
81 loginListener();
82 });
83 }]
84 };
85 }]);
86})(window.angular);