blob: 1ba55ba34acf480a82228fc77ead8601fb12a2fe [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',
Ed Tanousbbcf6702017-10-06 13:53:06 -07009 'template': require('./app-header.html'),
Iftekharul Islam99d199f2017-03-24 15:28:25 -050010 'scope': {
11 'path': '='
12 },
Andrew Geisslera72686f2018-04-11 11:44:50 -070013 'controller': ['$rootScope', '$scope','dataService', 'userModel', '$location', '$route',
14 function($rootScope, $scope, dataService, userModel, $location, $route){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050015 $scope.dataService = dataService;
16
Iftekharul Islamc22425f2017-09-06 10:04:14 -050017 $scope.loadServerHealth = function(){
18 APIUtils.getLogs().then(function(result){
19 dataService.updateServerHealth(result.data);
20 });
21 }
22
Iftekharul Islam99d199f2017-03-24 15:28:25 -050023 $scope.loadServerStatus = function(){
24 if(!userModel.isLoggedIn()){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050025 return;
26 }
27 APIUtils.getHostState(function(status){
28 if(status == 'xyz.openbmc_project.State.Host.HostState.Off'){
29 dataService.setPowerOffState();
30 }else if(status == 'xyz.openbmc_project.State.Host.HostState.Running'){
31 dataService.setPowerOnState();
32 }else{
33 dataService.setBootingState();
34 }
35 });
36 }
Iftekharul Islamba556c32017-08-11 08:37:12 -050037
38 $scope.loadNetworkInfo = function(){
39 if(!userModel.isLoggedIn()){
40 return;
41 }
42 APIUtils.getNetworkInfo().then(function(data){
43 dataService.setNetworkInfo(data);
44 });
45 }
46
Iftekharul Islamc22425f2017-09-06 10:04:14 -050047 function loadData(){
48 $scope.loadServerStatus();
49 $scope.loadNetworkInfo();
50 $scope.loadServerHealth();
51 }
52
53 loadData();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050054
55 $scope.logout = function(){
56 userModel.logout(function(status, error){
57 if(status){
58 $location.path('/logout');
59 }else{
60 console.log(error);
61 }
62 });
63 }
64
65 $scope.refresh = function(){
Andrew Geisslera72686f2018-04-11 11:44:50 -070066 //reload current page controllers and header
Iftekharul Islamc22425f2017-09-06 10:04:14 -050067 loadData();
Andrew Geisslera72686f2018-04-11 11:44:50 -070068 $route.reload();
Michael Davisa5f222a2017-08-04 15:02:38 -050069 //Add flash class to header timestamp on click of refresh
70 var myEl = angular.element( document.querySelector( '.header__refresh' ) );
71 myEl.addClass('flash');
72 setTimeout(function () {
73 myEl.removeClass("flash");
74 },2000);
75
Iftekharul Islam99d199f2017-03-24 15:28:25 -050076 }
77
78 var loginListener = $rootScope.$on('user-logged-in', function(event, arg){
Iftekharul Islamc22425f2017-09-06 10:04:14 -050079 loadData();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050080 });
81
82 $scope.$on('$destroy', function(){
83 loginListener();
84 });
Michael Davis4250f302017-09-06 11:03:52 -050085
86 $scope.multiRecent = function(){
87 $scope.multi_server_recent = !$scope.multi_server_recent;
88 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050089 }]
90 };
91 }]);
92})(window.angular);