Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 1 | window.angular && (function(angular) { |
| 2 | 'use strict'; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 3 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 4 | 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 Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 16 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 17 | $scope.loadServerHealth = function() { |
| 18 | APIUtils.getLogs().then(function(result) { |
| 19 | dataService.updateServerHealth(result.data); |
| 20 | }); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 21 | }; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 22 | |
| 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 Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 98 | })(window.angular); |