summaryrefslogtreecommitdiffstats
path: root/app/common/directives/app-header.js
blob: 69a9c7b31181b5df17f6b73ae1435feaae92563f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
window.angular && (function (angular) {
    'use strict';

    angular
        .module('app.common.directives')
        .directive('appHeader', ['APIUtils', function (APIUtils) {
            return {
                'restrict': 'E',
                'template': require('./app-header.html'),
                'scope': {
                   'path': '='
                },
                'controller': ['$rootScope', '$scope','dataService', 'userModel', '$location', function($rootScope, $scope, dataService, userModel, $location){
                    $scope.dataService = dataService;

                    $scope.loadServerHealth = function(){
                        APIUtils.getLogs().then(function(result){
                            dataService.updateServerHealth(result.data);
                        });
                    }

                    $scope.loadServerStatus = function(){
                        if(!userModel.isLoggedIn()){
                            return;
                        }
                        APIUtils.getHostState(function(status){
                            if(status == 'xyz.openbmc_project.State.Host.HostState.Off'){
                                dataService.setPowerOffState();
                            }else if(status == 'xyz.openbmc_project.State.Host.HostState.Running'){
                                dataService.setPowerOnState();
                            }else{
                                dataService.setBootingState();
                            }
                        });
                    }

                    $scope.loadNetworkInfo = function(){
                        if(!userModel.isLoggedIn()){
                            return;
                        }
                        APIUtils.getNetworkInfo().then(function(data){
                            dataService.setNetworkInfo(data);
                        });
                    }

                    function loadData(){
                       $scope.loadServerStatus();
                       $scope.loadNetworkInfo();
                       $scope.loadServerHealth();
                    }

                    loadData();

                    $scope.logout = function(){
                        userModel.logout(function(status, error){
                            if(status){
                               $location.path('/logout');
                            }else{
                                console.log(error);
                            }
                        });
                    }

                    $scope.refresh = function(){
                        loadData();

                        //Add flash class to header timestamp on click of refresh
                        var myEl = angular.element( document.querySelector( '.header__refresh' ) );
                        myEl.addClass('flash');
                        setTimeout(function () {
                            myEl.removeClass("flash");
                        },2000);

                    }

                    var loginListener = $rootScope.$on('user-logged-in', function(event, arg){
                        loadData();
                    });

                    $scope.$on('$destroy', function(){
                        loginListener();
                    });

                    $scope.multiRecent = function(){
                        $scope.multi_server_recent = !$scope.multi_server_recent;
                    };
                }]
            };
        }]);
})(window.angular);
OpenPOWER on IntegriCloud