Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 1 | window.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', |
| 9 | 'templateUrl': 'common/directives/app-header.html', |
| 10 | 'scope': { |
| 11 | 'path': '=' |
| 12 | }, |
| 13 | 'controller': ['$rootScope', '$scope','dataService', 'userModel', '$location', function($rootScope, $scope, dataService, userModel, $location){ |
| 14 | $scope.dataService = dataService; |
| 15 | |
Iftekharul Islam | c22425f | 2017-09-06 10:04:14 -0500 | [diff] [blame] | 16 | $scope.loadServerHealth = function(){ |
| 17 | APIUtils.getLogs().then(function(result){ |
| 18 | dataService.updateServerHealth(result.data); |
| 19 | }); |
| 20 | } |
| 21 | |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 22 | $scope.loadServerStatus = function(){ |
| 23 | if(!userModel.isLoggedIn()){ |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 24 | return; |
| 25 | } |
| 26 | APIUtils.getHostState(function(status){ |
| 27 | if(status == 'xyz.openbmc_project.State.Host.HostState.Off'){ |
| 28 | dataService.setPowerOffState(); |
| 29 | }else if(status == 'xyz.openbmc_project.State.Host.HostState.Running'){ |
| 30 | dataService.setPowerOnState(); |
| 31 | }else{ |
| 32 | dataService.setBootingState(); |
| 33 | } |
| 34 | }); |
| 35 | } |
Iftekharul Islam | ba556c3 | 2017-08-11 08:37:12 -0500 | [diff] [blame] | 36 | |
| 37 | $scope.loadNetworkInfo = function(){ |
| 38 | if(!userModel.isLoggedIn()){ |
| 39 | return; |
| 40 | } |
| 41 | APIUtils.getNetworkInfo().then(function(data){ |
| 42 | dataService.setNetworkInfo(data); |
| 43 | }); |
| 44 | } |
| 45 | |
Iftekharul Islam | c22425f | 2017-09-06 10:04:14 -0500 | [diff] [blame] | 46 | function loadData(){ |
| 47 | $scope.loadServerStatus(); |
| 48 | $scope.loadNetworkInfo(); |
| 49 | $scope.loadServerHealth(); |
| 50 | } |
| 51 | |
| 52 | loadData(); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 53 | |
| 54 | $scope.logout = function(){ |
| 55 | userModel.logout(function(status, error){ |
| 56 | if(status){ |
| 57 | $location.path('/logout'); |
| 58 | }else{ |
| 59 | console.log(error); |
| 60 | } |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | $scope.refresh = function(){ |
Iftekharul Islam | c22425f | 2017-09-06 10:04:14 -0500 | [diff] [blame] | 65 | loadData(); |
Michael Davis | a5f222a | 2017-08-04 15:02:38 -0500 | [diff] [blame] | 66 | |
| 67 | //Add flash class to header timestamp on click of refresh |
| 68 | var myEl = angular.element( document.querySelector( '.header__refresh' ) ); |
| 69 | myEl.addClass('flash'); |
| 70 | setTimeout(function () { |
| 71 | myEl.removeClass("flash"); |
| 72 | },2000); |
| 73 | |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | var loginListener = $rootScope.$on('user-logged-in', function(event, arg){ |
Iftekharul Islam | c22425f | 2017-09-06 10:04:14 -0500 | [diff] [blame] | 77 | loadData(); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 78 | }); |
| 79 | |
| 80 | $scope.$on('$destroy', function(){ |
| 81 | loginListener(); |
| 82 | }); |
| 83 | }] |
| 84 | }; |
| 85 | }]); |
| 86 | })(window.angular); |