blob: d15697bd2ff5c2cffb52d5fc7cc28b211eeca444 [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 Geisslerd27bb132018-05-24 11:07:27 -07004 angular.module('app.common.directives').directive('appHeader', [
5 'APIUtils',
6 function(APIUtils) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07007 return {
8 'restrict': 'E',
9 'template': require('./app-header.html'),
Andrew Geisslerd27bb132018-05-24 11:07:27 -070010 'scope': {'path': '='},
11 'controller': [
12 '$rootScope', '$scope', 'dataService', 'userModel', '$location',
13 '$route',
14 function(
15 $rootScope, $scope, dataService, userModel, $location, $route) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070016 $scope.dataService = dataService;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050017
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070018 $scope.loadServerHealth = function() {
19 APIUtils.getLogs().then(function(result) {
20 dataService.updateServerHealth(result.data);
21 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -050022 };
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070023
24 $scope.loadServerStatus = function() {
25 if (!userModel.isLoggedIn()) {
26 return;
27 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -070028 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 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070044 };
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');
Andrew Geisslerd27bb132018-05-24 11:07:27 -070067 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070068 console.log(error);
69 }
70 });
71 };
72
73 $scope.refresh = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070074 // reload current page controllers and header
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070075 loadData();
76 $route.reload();
Andrew Geisslerd27bb132018-05-24 11:07:27 -070077 // Add flash class to header timestamp on click of refresh
78 var myEl =
79 angular.element(document.querySelector('.header__refresh'));
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070080 myEl.addClass('flash');
81 setTimeout(function() {
82 myEl.removeClass('flash');
83 }, 2000);
84
85 };
86
Andrew Geisslerd27bb132018-05-24 11:07:27 -070087 var loginListener =
88 $rootScope.$on('user-logged-in', function(event, arg) {
89 loadData();
90 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070091
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 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700102 }
103 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500104})(window.angular);