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