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.dataService = dataService;
14              $scope.showSubMenu = false;
15              $scope.change = function(firstLevel) {
16                if (firstLevel != $scope.firstLevel) {
17                  $scope.firstLevel = firstLevel;
18                  $scope.showSubMenu = true;
19                } else {
20                  $scope.showSubMenu = !$scope.showSubMenu;
21                }
22              };
23              $scope.closeSubnav = function() {
24                $scope.showSubMenu = false;
25              };
26              $scope.$watch('path', function() {
27                var urlRoot = $location.path().split('/')[1];
28                if (urlRoot != '') {
29                  $scope.firstLevel = urlRoot;
30                } else {
31                  $scope.firstLevel = 'overview';
32                }
33                $scope.showSubMenu = false;
34              });
35              $scope.$watch('showNavigation', function() {
36                var paddingTop = 0;
37                var urlRoot = $location.path().split('/')[1];
38                if (urlRoot != '') {
39                  $scope.firstLevel = urlRoot;
40                } else {
41                  $scope.firstLevel = 'overview';
42                }
43
44                if ($scope.showNavigation) {
45                  paddingTop =
46                      document.getElementById('header__wrapper').offsetHeight;
47                }
48                dataService.bodyStyle = {'padding-top': paddingTop + 'px'};
49                $scope.navStyle = {'top': paddingTop + 'px'};
50              });
51            }
52          ],
53          link: function(scope, element, attributes) {
54            var rawNavElement = angular.element(element)[0];
55            angular.element(window.document).bind('click', function(event) {
56              if (rawNavElement.contains(event.target)) return;
57
58              if (scope.showSubMenu) {
59                scope.$apply(function() {
60                  scope.showSubMenu = false;
61                });
62              }
63            });
64          }
65        };
66      });
67})(window.angular);
68