1window.angular && (function(angular) {
2  'use strict';
3
4  angular.module('app.common.directives').directive('appHeader', [
5    'APIUtils',
6    function(APIUtils) {
7      return {
8        'restrict': 'E',
9        'template': require('./app-header.html'),
10        'scope': {'path': '='},
11        'controller': [
12          '$rootScope', '$scope', 'dataService', 'userModel', '$location',
13          '$route',
14          function(
15              $rootScope, $scope, dataService, userModel, $location, $route) {
16            $scope.dataService = dataService;
17
18            $scope.loadServerHealth = function() {
19              APIUtils.getLogs().then(function(result) {
20                dataService.updateServerHealth(result.data);
21              });
22            };
23
24            $scope.loadServerStatus = function() {
25              if (!userModel.isLoggedIn()) {
26                return;
27              }
28              APIUtils.getHostState().then(
29                  function(status) {
30                    if (status ==
31                        'xyz.openbmc_project.State.Host.HostState.Off') {
32                      dataService.setPowerOffState();
33                    } else if (
34                        status ==
35                        'xyz.openbmc_project.State.Host.HostState.Running') {
36                      dataService.setPowerOnState();
37                    } else {
38                      dataService.setErrorState();
39                    }
40                  },
41                  function(error) {
42                    dataService.activateErrorModal();
43                  });
44            };
45
46            $scope.loadNetworkInfo = function() {
47              if (!userModel.isLoggedIn()) {
48                return;
49              }
50              APIUtils.getNetworkInfo().then(function(data) {
51                dataService.setNetworkInfo(data);
52              });
53            };
54
55            function loadData() {
56              $scope.loadServerStatus();
57              $scope.loadNetworkInfo();
58              $scope.loadServerHealth();
59            }
60
61            loadData();
62
63            $scope.logout = function() {
64              userModel.logout(function(status, error) {
65                if (status) {
66                  $location.path('/logout');
67                } else {
68                  console.log(error);
69                }
70              });
71            };
72
73            $scope.refresh = function() {
74              // reload current page controllers and header
75              loadData();
76              $route.reload();
77              // Add flash class to header timestamp on click of refresh
78              var myEl =
79                  angular.element(document.querySelector('.header__refresh'));
80              myEl.addClass('flash');
81              setTimeout(function() {
82                myEl.removeClass('flash');
83              }, 2000);
84
85            };
86
87            var loginListener =
88                $rootScope.$on('user-logged-in', function(event, arg) {
89                  loadData();
90                });
91
92            $scope.$on('$destroy', function() {
93              loginListener();
94            });
95
96            $scope.multiRecent = function() {
97              $scope.multi_server_recent = !$scope.multi_server_recent;
98            };
99          }
100        ]
101      };
102    }
103  ]);
104})(window.angular);
105