1window.angular && (function(angular) {
2  'use strict';
3
4  angular.module('app.common.directives')
5      .directive('appNavigation', function() {
6        return {
7          'restrict': 'E',
8          'template': require('./app-navigation.html'),
9          'scope': {'path': '=', 'showNavigation': '='},
10          'controller': [
11            '$scope', '$location', 'dataService',
12            function($scope, $location, dataService) {
13              $scope.showHealthMenu = false;
14              $scope.showControlMenu = false;
15              $scope.showConfigMenu = false;
16              $scope.showAccessMenu = false;
17              $scope.dataService = dataService;
18
19              $scope.change = function(firstLevel) {
20                switch (firstLevel) {
21                  case 'server-health':
22                    $scope.showHealthMenu = !$scope.showHealthMenu;
23                    break;
24                  case 'server-control':
25                    $scope.showControlMenu = !$scope.showControlMenu;
26                    break;
27                  case 'configuration':
28                    $scope.showConfigMenu = !$scope.showConfigMenu;
29                    break;
30                  case 'access-control':
31                    $scope.showAccessMenu = !$scope.showAccessMenu;
32                    break;
33                  case 'overview':
34                    $location.url('/overview/server');
35                    break;
36                };
37              };
38              $scope.$watch('path', function() {
39                var urlRoot = $location.path().split('/')[1];
40                if (urlRoot != '') {
41                  $scope.firstLevel = urlRoot;
42                } else {
43                  $scope.firstLevel = 'overview';
44                }
45                $scope.showSubMenu = true;
46              });
47              $scope.$watch('showNavigation', function() {
48                var urlRoot = $location.path().split('/')[1];
49                if (urlRoot != '') {
50                  $scope.firstLevel = urlRoot;
51                } else {
52                  $scope.firstLevel = 'overview';
53                }
54              });
55            }
56          ],
57          link: function(scope, element, attributes) {
58            var rawNavElement = angular.element(element)[0];
59            angular.element(window.document).bind('click', function(event) {
60              if (rawNavElement.contains(event.target)) return;
61
62              if (scope.showSubMenu) {
63                scope.$apply(function() {
64                  scope.showSubMenu = true;
65                });
66              }
67            });
68          }
69        };
70      });
71})(window.angular);