199d199f3SIftekharul Islamwindow.angular && (function(angular) { 299d199f3SIftekharul Islam 'use strict'; 399d199f3SIftekharul Islam 499d199f3SIftekharul Islam angular 599d199f3SIftekharul Islam .module('app.common.directives') 699d199f3SIftekharul Islam .directive('appHeader', ['APIUtils', function(APIUtils) { 799d199f3SIftekharul Islam return { 899d199f3SIftekharul Islam 'restrict': 'E', 9bbcf670aSEd Tanous 'template': require('./app-header.html'), 1099d199f3SIftekharul Islam 'scope': { 1199d199f3SIftekharul Islam 'path': '=' 1299d199f3SIftekharul Islam }, 13a72686f7SAndrew Geissler 'controller': ['$rootScope', '$scope', 'dataService', 'userModel', '$location', '$route', 14a72686f7SAndrew Geissler function($rootScope, $scope, dataService, userModel, $location, $route) { 1599d199f3SIftekharul Islam $scope.dataService = dataService; 1699d199f3SIftekharul Islam 17c22425f2SIftekharul Islam $scope.loadServerHealth = function() { 18c22425f2SIftekharul Islam APIUtils.getLogs().then(function(result) { 19c22425f2SIftekharul Islam dataService.updateServerHealth(result.data); 20c22425f2SIftekharul Islam }); 21*ba5e3f34SAndrew Geissler }; 22c22425f2SIftekharul Islam 2399d199f3SIftekharul Islam $scope.loadServerStatus = function() { 2499d199f3SIftekharul Islam if (!userModel.isLoggedIn()) { 2599d199f3SIftekharul Islam return; 2699d199f3SIftekharul Islam } 27a1d238f3SIftekharul Islam APIUtils.getHostState().then(function(status) { 2899d199f3SIftekharul Islam if (status == 'xyz.openbmc_project.State.Host.HostState.Off') { 2999d199f3SIftekharul Islam dataService.setPowerOffState(); 30*ba5e3f34SAndrew Geissler } 31*ba5e3f34SAndrew Geissler else if (status == 'xyz.openbmc_project.State.Host.HostState.Running') { 3299d199f3SIftekharul Islam dataService.setPowerOnState(); 33*ba5e3f34SAndrew Geissler } 34*ba5e3f34SAndrew Geissler else { 35d80c280bSCamVan Nguyen dataService.setErrorState(); 3699d199f3SIftekharul Islam } 37a1d238f3SIftekharul Islam }, function(error) { 38a1d238f3SIftekharul Islam dataService.activateErrorModal(); 3999d199f3SIftekharul Islam }); 40*ba5e3f34SAndrew Geissler }; 41ba556c31SIftekharul Islam 42ba556c31SIftekharul Islam $scope.loadNetworkInfo = function() { 43ba556c31SIftekharul Islam if (!userModel.isLoggedIn()) { 44ba556c31SIftekharul Islam return; 45ba556c31SIftekharul Islam } 46ba556c31SIftekharul Islam APIUtils.getNetworkInfo().then(function(data) { 47ba556c31SIftekharul Islam dataService.setNetworkInfo(data); 48ba556c31SIftekharul Islam }); 49*ba5e3f34SAndrew Geissler }; 50ba556c31SIftekharul Islam 51c22425f2SIftekharul Islam function loadData() { 5299d199f3SIftekharul Islam $scope.loadServerStatus(); 53ba556c31SIftekharul Islam $scope.loadNetworkInfo(); 54c22425f2SIftekharul Islam $scope.loadServerHealth(); 55c22425f2SIftekharul Islam } 56c22425f2SIftekharul Islam 57c22425f2SIftekharul Islam loadData(); 5899d199f3SIftekharul Islam 5999d199f3SIftekharul Islam $scope.logout = function() { 6099d199f3SIftekharul Islam userModel.logout(function(status, error) { 6199d199f3SIftekharul Islam if (status) { 6299d199f3SIftekharul Islam $location.path('/logout'); 63*ba5e3f34SAndrew Geissler } 64*ba5e3f34SAndrew Geissler else { 6599d199f3SIftekharul Islam console.log(error); 6699d199f3SIftekharul Islam } 6799d199f3SIftekharul Islam }); 68*ba5e3f34SAndrew Geissler }; 6999d199f3SIftekharul Islam 7099d199f3SIftekharul Islam $scope.refresh = function() { 71a72686f7SAndrew Geissler //reload current page controllers and header 72c22425f2SIftekharul Islam loadData(); 73a72686f7SAndrew Geissler $route.reload(); 74a5f222a1SMichael Davis //Add flash class to header timestamp on click of refresh 75a5f222a1SMichael Davis var myEl = angular.element(document.querySelector('.header__refresh')); 76a5f222a1SMichael Davis myEl.addClass('flash'); 77a5f222a1SMichael Davis setTimeout(function() { 78*ba5e3f34SAndrew Geissler myEl.removeClass('flash'); 79a5f222a1SMichael Davis }, 2000); 80a5f222a1SMichael Davis 81*ba5e3f34SAndrew Geissler }; 8299d199f3SIftekharul Islam 8399d199f3SIftekharul Islam var loginListener = $rootScope.$on('user-logged-in', function(event, arg) { 84c22425f2SIftekharul Islam loadData(); 8599d199f3SIftekharul Islam }); 8699d199f3SIftekharul Islam 8799d199f3SIftekharul Islam $scope.$on('$destroy', function() { 8899d199f3SIftekharul Islam loginListener(); 8999d199f3SIftekharul Islam }); 904250f302SMichael Davis 914250f302SMichael Davis $scope.multiRecent = function() { 924250f302SMichael Davis $scope.multi_server_recent = !$scope.multi_server_recent; 934250f302SMichael Davis }; 94*ba5e3f34SAndrew Geissler } 95*ba5e3f34SAndrew Geissler ] 9699d199f3SIftekharul Islam }; 9799d199f3SIftekharul Islam }]); 9899d199f3SIftekharul Islam})(window.angular); 99