blob: 2a34381d9dbf8d28e6812c3c4c6677c648e18b63 [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 }
Iftekharul Islama1d238f2018-02-26 12:29:45 -060027 APIUtils.getHostState().then(function(status){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050028 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 }
Iftekharul Islama1d238f2018-02-26 12:29:45 -060035 }, function(error){
36 dataService.activateErrorModal();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050037 });
38 }
Iftekharul Islamba556c32017-08-11 08:37:12 -050039
40 $scope.loadNetworkInfo = function(){
41 if(!userModel.isLoggedIn()){
42 return;
43 }
44 APIUtils.getNetworkInfo().then(function(data){
45 dataService.setNetworkInfo(data);
46 });
47 }
48
Iftekharul Islamc22425f2017-09-06 10:04:14 -050049 function loadData(){
50 $scope.loadServerStatus();
51 $scope.loadNetworkInfo();
52 $scope.loadServerHealth();
53 }
54
55 loadData();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050056
57 $scope.logout = function(){
58 userModel.logout(function(status, error){
59 if(status){
60 $location.path('/logout');
61 }else{
62 console.log(error);
63 }
64 });
65 }
66
67 $scope.refresh = function(){
Andrew Geisslera72686f2018-04-11 11:44:50 -070068 //reload current page controllers and header
Iftekharul Islamc22425f2017-09-06 10:04:14 -050069 loadData();
Andrew Geisslera72686f2018-04-11 11:44:50 -070070 $route.reload();
Michael Davisa5f222a2017-08-04 15:02:38 -050071 //Add flash class to header timestamp on click of refresh
72 var myEl = angular.element( document.querySelector( '.header__refresh' ) );
73 myEl.addClass('flash');
74 setTimeout(function () {
75 myEl.removeClass("flash");
76 },2000);
77
Iftekharul Islam99d199f2017-03-24 15:28:25 -050078 }
79
80 var loginListener = $rootScope.$on('user-logged-in', function(event, arg){
Iftekharul Islamc22425f2017-09-06 10:04:14 -050081 loadData();
Iftekharul Islam99d199f2017-03-24 15:28:25 -050082 });
83
84 $scope.$on('$destroy', function(){
85 loginListener();
86 });
Michael Davis4250f302017-09-06 11:03:52 -050087
88 $scope.multiRecent = function(){
89 $scope.multi_server_recent = !$scope.multi_server_recent;
90 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050091 }]
92 };
93 }]);
94})(window.angular);