1angular.module("mapApp", ["ui.bootstrap", "ui.utils", "nagiosDecorations",
2		"nagiosTime"])
3
4	.constant("nagiosProcessName", "Nagios Process")
5
6	// Layout options
7	.constant("layouts", {
8		UserSupplied: {
9			index: 0,
10			textAlignment: "below",
11			padding: {
12				top: 25,
13				right: 25,
14				bottom: 25,
15				left: 25,
16			},
17			textPadding: {
18				above: 4,
19				right: 4,
20				left: 4,
21				below: 11
22			},
23			dimensionType: "fixed"
24		},
25		DepthLayers: {
26			index: 1,
27			topPadding: 100,
28			bottomPadding: 100,
29			dyText: 4
30		},
31		CollapsedTree: {
32			index: 2,
33			topPadding: 100,
34			bottomPadding: 100,
35			dyText: 4
36		},
37		BalancedTree: {
38			index: 3,
39			topPadding: 100,
40			bottomPadding: 100,
41			dyText: 4
42		},
43		Circular: {
44			index: 4
45		},
46		CircularMarkup: {
47			index: 5,
48			radialExponent: 1.2,
49			padding: 20,
50			textPadding: 4
51		},
52		CircularBalloon: {
53			index: 6,
54			outsidePadding: 60,
55			rotation: -90,
56			textPadding: 4,
57			dyText: ".31em"
58		},
59		BalancedTreeVertical: {
60			index: 7,
61			leftPadding: 100,
62			rightPadding: 100,
63			dxText: 4,
64			dyText: 3
65		},
66		CollapsedTreeVertical: {
67			index: 8,
68			leftPadding: 100,
69			rightPadding: 100,
70			dxText: 4,
71			dyText: 3
72		},
73		DepthLayersVertical: {
74			index: 9,
75			leftPadding: 100,
76			rightPadding: 100,
77			dxText: 4,
78			dyText: 3
79		},
80		Force: {
81			index: 10,
82			outsidePadding: 60,
83			textPadding: 4
84		}
85	})
86
87	.config(function($locationProvider) {
88		$locationProvider.html5Mode({
89			enabled: true,
90			requireBase: false
91		});
92	})
93
94	.controller("mapCtrl", function($scope, $location, $modal, $http,
95			nagiosTimeService, nagiosProcessName, layouts, $window) {
96		$scope.search = $location.search();
97
98		// URL parameters
99		$scope.params = {
100			cgiurl: '@cgiurl@/',
101			layout: map_layout,
102			dimensions: $scope.search.dimensions ?
103					$scope.search.dimensions : "",
104			ulx: $scope.search.ulx ? parseInt($scope.search.ulx) : 0,
105			uly: $scope.search.uly ? parseInt($scope.search.uly) : 0,
106			lrx: $scope.search.lrx ? parseInt($scope.search.lrx) : 0,
107			lry: $scope.search.lry ? parseInt($scope.search.lry) : 0,
108			root: $scope.search.root ? $scope.search.root :
109					nagiosProcessName,
110			maxzoom: $scope.search.maxzoom ?
111					parseInt($scope.search.maxzoom) : 10,
112			nolinks: $scope.search.nolinks ? true : false,
113			notext: $scope.search.notext ? true : false,
114			nopopups: $scope.search.nopopups ? true : false,
115			nomenu: $scope.search.nomenu ? true : false,
116			noresize: $scope.search.noresize ? true : false,
117			noicons: $scope.search.noicons ? true : false,
118			iconurl: $scope.search.iconurl ? $scope.search.iconurl :
119					$location.absUrl().replace(/map\.php.*$/, "images/logos/"),
120		};
121
122		var rightPadding = 1;
123		var bottomPadding = 4;
124
125		$scope.svgWidth = $window.innerWidth - rightPadding;
126		$scope.svgHeight = $window.innerHeight - bottomPadding;
127
128		// Application state variables
129		$scope.formDisplayed = false;
130		$scope.reload = 0;
131
132		// Decoration-related variables
133		$scope.lastUpdate = "none";
134
135		// Determine whether we believe we have sufficient information to
136		// build the map. If we don't have a valid URL for the JSON CGIs
137		// we won't know that until we try to fetch the list of hosts, so
138		// we can't know that now.
139		$scope.canBuildMap = function() {
140			document.body.className = "";
141			if ($scope.params.layout == layouts.UserSupplied.index) {
142				switch ($scope.params.dimensions) {
143				case "fixed":
144				case "auto":
145					document.body.className = "hasBgImage";
146					return true;
147					break;
148				case "user":
149					if ($scope.params.ulx >= $scope.params.lrx ||
150							$scope.params.uly >= $scope.params.lry) {
151						return false;
152					}
153					else {
154						document.body.className = "hasBgImage";
155						return true;
156					}
157					break;
158				default:
159					return false;
160					break;
161				}
162			}
163			else {
164				return true;
165			}
166		};
167
168		angular.element($window).bind("resize", function() {
169			$scope.svgWidth = $window.innerWidth - rightPadding;
170			$scope.svgHeight = $window.innerHeight - bottomPadding;
171			$scope.$apply("svgWidth");
172			$scope.$apply("svgHeight");
173		});
174
175		$scope.displayForm = function(size) {
176			$scope.formDisplayed = true;
177			var modalInstance = $modal.open({
178				templateUrl: 'map-form.html',
179				controller: 'mapFormCtrl',
180				size: size,
181				resolve: {
182					params: function () {
183						return $scope.params;
184					}
185				}
186			});
187
188			modalInstance.result.then(function(params) {
189				$scope.formDisplayed = false;
190				$scope.params = params;
191				$scope.reload++;
192			},
193			function(reason) {
194				$scope.formDisplayed = false;
195			});
196		}
197
198		// Style the menu button
199		$scope.menuButtonStyle = function() {
200			return {
201				left: ($scope.svgWidth - 30) + "px",
202				top: "5px"
203			};
204		};
205
206		$scope.infoBoxTitle = function() {
207			if ($scope.params.root == nagiosProcessName) {
208				return "Network Map for All Hosts";
209			}
210			else {
211				return "Network Map for Host " + $scope.params.root;
212			}
213		};
214
215		if (!$scope.canBuildMap()) {
216			$scope.displayForm();
217		}
218
219	})
220