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