blob: 7de4cd17415516ef8412279993395bf282fbecf3 [file] [log] [blame]
Simon Huntef31fb22014-12-19 13:16:44 -08001/*
Simon Hunt8ead3a22015-01-06 11:00:15 -08002 * Copyright 2014,2015 Open Networking Laboratory
Simon Huntef31fb22014-12-19 13:16:44 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Topology View Module
Simon Huntef31fb22014-12-19 13:16:44 -080019 */
20
21(function () {
22 'use strict';
Simon Hunt6cc53692015-01-07 11:33:45 -080023
24 var moduleDependencies = [
Simon Hunt36a58c62015-04-08 11:00:07 -070025 'ngCookies',
Simon Hunt6cc53692015-01-07 11:33:45 -080026 'onosUtil',
Simon Hunt1e4a0012015-01-21 11:36:08 -080027 'onosSvg',
28 'onosRemote'
Simon Hunt6cc53692015-01-07 11:33:45 -080029 ];
30
Simon Hunta4281242016-02-26 20:18:45 -080031 // references to injected services
Thomas Vachuska26be4f32016-03-31 01:10:27 -070032 var $scope, $log, $cookies, $loc, fs, ks, zs, gs, ms, sus, flash, wss, ps, th,
33 tds, t3s, tes, tfs, tps, tis, tms, tss, tls, tts, tos, fltr, ttbs, tspr,
Simon Hunt8a119b02016-03-29 16:00:32 -070034 ttip, tov;
Simon Hunt6cc53692015-01-07 11:33:45 -080035
36 // DOM elements
Simon Hunt2052e5d2015-04-13 17:40:44 -070037 var ovtopo, svg, defs, zoomLayer, mapG, spriteG, forceG, noDevsLayer;
Simon Hunt6cc53692015-01-07 11:33:45 -080038
39 // Internal state
Simon Hunta34fcb52016-02-25 16:27:32 -080040 var zoomer, actionMap, themeListener;
Simon Hunt6cc53692015-01-07 11:33:45 -080041
Simon Huntcacce342015-01-07 16:13:05 -080042 // --- Short Cut Keys ------------------------------------------------
43
Simon Hunt8d22c4b2015-08-06 16:24:43 -070044 function setUpKeys(overlayKeys) {
Simon Hunt5724fb42015-02-05 16:59:40 -080045 // key bindings need to be made after the services have been injected
46 // thus, deferred to here...
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070047 actionMap = {
Simon Hunt36a58c62015-04-08 11:00:07 -070048 I: [toggleInstances, 'Toggle ONOS instances panel'],
Simon Huntee7a3ce2015-04-09 13:28:37 -070049 O: [toggleSummary, 'Toggle ONOS summary panel'],
Simon Hunt239e5882015-04-23 15:07:04 -070050 D: [toggleUseDetailsFlag, 'Disable / enable details panel'],
Simon Huntac4c6f72015-02-03 19:50:53 -080051
Simon Huntfcbde892015-04-16 12:05:28 -070052 H: [toggleHosts, 'Toggle host visibility'],
53 M: [toggleOffline, 'Toggle offline visibility'],
54 P: [togglePorts, 'Toggle Port Highlighting'],
Simon Hunt86b7c882015-04-02 23:06:08 -070055 dash: [tfs.showBadLinks, 'Show bad links'],
Thomas Vachuska26be4f32016-03-31 01:10:27 -070056 B: [toggleMap, 'Toggle background geo map'],
57 G: [openMapSelection, 'Select background geo map'],
Simon Hunt2052e5d2015-04-13 17:40:44 -070058 S: [toggleSprites, 'Toggle sprite layer'],
Simon Huntac4c6f72015-02-03 19:50:53 -080059
Simon Huntfd7106c2016-02-09 15:05:26 -080060 X: [tfs.resetAllLocations, 'Reset node locations'],
Simon Hunt96f88c62015-02-19 17:57:25 -080061 Z: [tos.toggleOblique, 'Toggle oblique view (Experimental)'],
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070062 N: [fltr.clickAction, 'Cycle node layers'],
Simon Hunt5724fb42015-02-05 16:59:40 -080063 L: [tfs.cycleDeviceLabels, 'Cycle device labels'],
Simon Hunt445e8152015-02-06 13:00:12 -080064 U: [tfs.unpin, 'Unpin node (hover mouse over)'],
Simon Hunt5724fb42015-02-05 16:59:40 -080065 R: [resetZoom, 'Reset pan / zoom'],
Simon Hunt90dcc3e2015-03-25 15:01:27 -070066 dot: [ttbs.toggleToolbar, 'Toggle Toolbar'],
Simon Huntac4c6f72015-02-03 19:50:53 -080067
Simon Hunt0e48c292015-02-19 16:11:37 -080068 E: [equalizeMasters, 'Equalize mastership roles'],
Simon Huntac4c6f72015-02-03 19:50:53 -080069
Simon Hunt5b024d72016-01-29 11:02:43 -080070 // topology overlay selections
71 F1: function () { ttbs.fnkey(0); },
72 F2: function () { ttbs.fnkey(1); },
73 F3: function () { ttbs.fnkey(2); },
74 F4: function () { ttbs.fnkey(3); },
75 F5: function () { ttbs.fnkey(4); },
76
Simon Hunt08f841d02015-02-10 14:39:20 -080077 esc: handleEscape,
Simon Huntac4c6f72015-02-03 19:50:53 -080078
Simon Hunt09060142015-03-18 20:23:32 -070079 _keyListener: ttbs.keyListener,
80
Simon Hunt5724fb42015-02-05 16:59:40 -080081 _helpFormat: [
Thomas Vachuska26be4f32016-03-31 01:10:27 -070082 ['I', 'O', 'D', 'H', 'M', 'P', 'dash', 'B', 'G', 'S' ],
Simon Hunt8d22c4b2015-08-06 16:24:43 -070083 ['X', 'Z', 'N', 'L', 'U', 'R', '-', 'E', '-', 'dot'],
84 [] // this column reserved for overlay actions
Simon Hunt5724fb42015-02-05 16:59:40 -080085 ]
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070086 };
87
Simon Hunt8d22c4b2015-08-06 16:24:43 -070088 if (fs.isO(overlayKeys)) {
89 mergeKeys(overlayKeys);
90 }
91
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070092 ks.keyBindings(actionMap);
Simon Huntac4c6f72015-02-03 19:50:53 -080093
Simon Hunt639dc662015-02-18 14:19:20 -080094 ks.gestureNotes([
Simon Hunt5724fb42015-02-05 16:59:40 -080095 ['click', 'Select the item and show details'],
96 ['shift-click', 'Toggle selection state'],
97 ['drag', 'Reposition (and pin) device / host'],
98 ['cmd-scroll', 'Zoom in / out'],
99 ['cmd-drag', 'Pan']
Simon Hunt639dc662015-02-18 14:19:20 -0800100 ]);
Simon Hunt5724fb42015-02-05 16:59:40 -0800101 }
Simon Hunt6cc53692015-01-07 11:33:45 -0800102
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700103 // when a topology overlay is activated, we need to bind their keystrokes
104 // and include them in the quick-help panel
105 function mergeKeys(extra) {
106 var _hf = actionMap._helpFormat[2];
Simon Hunt71892222015-09-29 13:39:40 -0700107
108 ks.checkNotGlobal(extra);
109
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700110 extra._keyOrder.forEach(function (k) {
111 var d = extra[k],
112 cb = d && d.cb,
113 tt = d && d.tt;
114 // NOTE: ignore keys that are already defined
115 if (d && !actionMap[k]) {
116 actionMap[k] = [cb, tt];
117 _hf.push(k);
118 }
119 });
120 }
121
Simon Hunt08f841d02015-02-10 14:39:20 -0800122 // --- Keystroke functions -------------------------------------------
Simon Huntac4c6f72015-02-03 19:50:53 -0800123
Simon Hunt36a58c62015-04-08 11:00:07 -0700124 function toggleInstances(x) {
Simon Huntee7a3ce2015-04-09 13:28:37 -0700125 updatePrefsState('insts', tis.toggle(x));
Simon Huntac4c6f72015-02-03 19:50:53 -0800126 tfs.updateDeviceColors();
Simon Hunt6cc53692015-01-07 11:33:45 -0800127 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800128
Simon Huntee7a3ce2015-04-09 13:28:37 -0700129 function toggleSummary(x) {
130 updatePrefsState('summary', tps.toggleSummary(x));
Simon Hunt18bf9822015-02-12 17:35:45 -0800131 }
132
Simon Hunt239e5882015-04-23 15:07:04 -0700133 function toggleUseDetailsFlag(x) {
134 updatePrefsState('detail', tps.toggleUseDetailsFlag(x));
Simon Huntee7a3ce2015-04-09 13:28:37 -0700135 }
136
Simon Huntfcbde892015-04-16 12:05:28 -0700137 function toggleHosts(x) {
138 updatePrefsState('hosts', tfs.toggleHosts(x));
139 }
140
141 function toggleOffline(x) {
142 updatePrefsState('offdev', tfs.toggleOffline(x));
143 }
144
145 function togglePorts(x) {
146 updatePrefsState('porthl', tfs.togglePorts(x));
147 }
148
149 function _togSvgLayer(x, G, tag, what) {
150 var on = (x === 'keyev') ? !sus.visible(G) : !!x,
Simon Huntee7a3ce2015-04-09 13:28:37 -0700151 verb = on ? 'Show' : 'Hide';
Simon Huntfcbde892015-04-16 12:05:28 -0700152 sus.visible(G, on);
153 updatePrefsState(tag, on);
154 flash.flash(verb + ' ' + what);
155 }
156
157 function toggleMap(x) {
158 _togSvgLayer(x, mapG, 'bg', 'background map');
Simon Huntee7a3ce2015-04-09 13:28:37 -0700159 }
Simon Hunt36a58c62015-04-08 11:00:07 -0700160
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700161 function openMapSelection() {
162 tms.openMapSelection();
163 }
164
Simon Hunt2052e5d2015-04-13 17:40:44 -0700165 function toggleSprites(x) {
Simon Huntfcbde892015-04-16 12:05:28 -0700166 _togSvgLayer(x, spriteG, 'spr', 'sprite layer');
Simon Hunt2052e5d2015-04-13 17:40:44 -0700167 }
168
Simon Hunt08f841d02015-02-10 14:39:20 -0800169 function resetZoom() {
170 zoomer.reset();
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700171 flash.flash('Pan and zoom reset');
Simon Hunt08f841d02015-02-10 14:39:20 -0800172 }
173
Simon Hunt0e48c292015-02-19 16:11:37 -0800174 function equalizeMasters() {
Simon Hunt237676b52015-03-10 19:04:26 -0700175 wss.sendEvent('equalizeMasters');
Simon Hunt0e48c292015-02-19 16:11:37 -0800176 flash.flash('Equalizing master roles');
177 }
178
Simon Hunt08f841d02015-02-10 14:39:20 -0800179 function handleEscape() {
Simon Hunta142dd22015-02-12 22:07:51 -0800180 if (tis.showMaster()) {
181 // if an instance is selected, cancel the affinity mapping
182 tis.cancelAffinity()
Simon Hunt08f841d02015-02-10 14:39:20 -0800183
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700184 } else if (tov.hooks.escape()) {
185 // else if the overlay consumed the ESC event...
186 // (work already done)
187
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700188 } else if (tss.deselectAll()) {
Simon Hunta0eb0a82015-02-11 12:30:06 -0800189 // else if we have node selections, deselect them all
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700190 // (work already done)
191
192 } else if (tls.deselectLink()) {
193 // else if we have a link selected, deselect it
194 // (work already done)
Simon Hunt08f841d02015-02-10 14:39:20 -0800195
Simon Hunta0eb0a82015-02-11 12:30:06 -0800196 } else if (tis.isVisible()) {
197 // else if the Instance Panel is visible, hide it
198 tis.hide();
199 tfs.updateDeviceColors();
Simon Hunt08f841d02015-02-10 14:39:20 -0800200
Simon Hunta0eb0a82015-02-11 12:30:06 -0800201 } else if (tps.summaryVisible()) {
202 // else if the Summary Panel is visible, hide it
203 tps.hideSummaryPanel();
Simon Hunta0eb0a82015-02-11 12:30:06 -0800204 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800205 }
Simon Huntcacce342015-01-07 16:13:05 -0800206
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700207 // --- Toolbar Functions ---------------------------------------------
208
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700209 function notValid(what) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700210 $log.warn('topo.js getActionEntry(): Not a valid ' + what);
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700211 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700212
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700213 function getActionEntry(key) {
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700214 var entry;
215
216 if (!key) {
217 notValid('key');
218 return null;
219 }
220
221 entry = actionMap[key];
222
223 if (!entry) {
224 notValid('actionMap entry');
225 return null;
226 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700227 return fs.isA(entry) || [entry, ''];
228 }
229
230 function setUpToolbar() {
231 ttbs.init({
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700232 getActionEntry: getActionEntry,
233 setUpKeys: setUpKeys
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700234 });
235 ttbs.createToolbar();
236 }
237
Simon Huntcacce342015-01-07 16:13:05 -0800238 // --- Glyphs, Icons, and the like -----------------------------------
239
Simon Hunt6cc53692015-01-07 11:33:45 -0800240 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -0800241 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -0800242 gs.loadDefs(defs);
Simon Hunt0ee28682015-02-12 20:48:11 -0800243 sus.loadGlowDefs(defs);
Simon Hunt6cc53692015-01-07 11:33:45 -0800244 }
245
246
Simon Huntcacce342015-01-07 16:13:05 -0800247 // --- Pan and Zoom --------------------------------------------------
248
249 // zoom enabled predicate. ev is a D3 source event.
250 function zoomEnabled(ev) {
Simon Huntd552ee92015-04-02 17:06:35 -0700251 return fs.isMobile() || (ev.metaKey || ev.altKey);
Simon Huntcacce342015-01-07 16:13:05 -0800252 }
253
254 function zoomCallback() {
Simon Huntbb5e0d82015-04-16 14:25:46 -0700255 var sc = zoomer.scale(),
256 tr = zoomer.translate();
257
258 ps.setPrefs('topo_zoom', {tx:tr[0], ty:tr[1], sc:sc});
Simon Huntcacce342015-01-07 16:13:05 -0800259
Simon Hunt0541fb82015-01-14 18:59:57 -0800260 // keep the map lines constant width while zooming
Simon Hunt737c89f2015-01-28 12:23:19 -0800261 mapG.style('stroke-width', (2.0 / sc) + 'px');
Simon Huntcacce342015-01-07 16:13:05 -0800262 }
263
264 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800265 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -0800266 zoomer = zs.createZoomer({
267 svg: svg,
268 zoomLayer: zoomLayer,
269 zoomEnabled: zoomEnabled,
270 zoomCallback: zoomCallback
271 });
272 }
273
274
Simon Hunt0541fb82015-01-14 18:59:57 -0800275 // callback invoked when the SVG view has been resized..
Simon Hunt3a6eec02015-02-09 21:16:43 -0800276 function svgResized(s) {
277 tfs.newDim([s.width, s.height]);
Simon Hunt0541fb82015-01-14 18:59:57 -0800278 }
279
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800280 // --- Background Map ------------------------------------------------
281
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800282 function setUpNoDevs() {
283 var g, box;
284 noDevsLayer = svg.append('g').attr({
285 id: 'topo-noDevsLayer',
286 transform: sus.translate(500,500)
287 });
288 // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html.
289 // We are translating this layer to have its origin at the center
290
291 g = noDevsLayer.append('g');
292 gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird');
293 g.append('text').text('No devices are connected')
294 .attr({ x: 120, y: 80});
295
296 box = g.node().getBBox();
297 box.x -= box.width/2;
298 box.y -= box.height/2;
299 g.attr('transform', sus.translate(box.x, box.y));
300
301 showNoDevs(true);
302 }
303
304 function showNoDevs(b) {
Simon Hunt18bf9822015-02-12 17:35:45 -0800305 sus.visible(noDevsLayer, b);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800306 }
307
Simon Hunt2362b072015-06-11 20:08:22 -0700308
309 var countryFilters = {
310 world: function (c) {
311 return c.properties.continent !== 'Antarctica';
312 },
313
314 // NOTE: for "usa" we are using our hand-crafted topojson file
315
316 s_america: function (c) {
317 return c.properties.continent === 'South America';
318 },
319
Simon Huntac099e82015-12-03 09:17:47 -0800320 ns_america: function (c) {
321 return c.properties.custom === 'US-cont' ||
322 c.properties.subregion === 'Central America' ||
Simon Huntd4712622015-12-02 11:19:50 -0800323 c.properties.continent === 'South America';
324 },
325
Simon Hunt2362b072015-06-11 20:08:22 -0700326 japan: function (c) {
327 return c.properties.geounit === 'Japan';
328 },
329
330 europe: function (c) {
331 return c.properties.continent === 'Europe';
332 },
333
334 italy: function (c) {
335 return c.properties.geounit === 'Italy';
336 },
337
338 uk: function (c) {
339 // technically, Ireland is not part of the United Kingdom,
340 // but the map looks weird without it showing.
341 return c.properties.adm0_a3 === 'GBR' ||
342 c.properties.adm0_a3 === 'IRL';
Simon Hunt1178d5b2015-09-02 17:02:37 -0700343 },
344
345 s_korea: function (c) {
346 return c.properties.adm0_a3 === 'KOR';
347 },
348
349 australia: function (c) {
350 return c.properties.adm0_a3 === 'AUS';
Simon Hunt2362b072015-06-11 20:08:22 -0700351 }
352 };
353
Simon Hunta34fcb52016-02-25 16:27:32 -0800354 var tintOn = 0,
355 shadeFlip = 0,
356 shadePalette = {
357 light: {
358 sea: 'aliceblue',
359 land: 'white',
360 outline: '#ddd'
361 },
362 dark: {
363 sea: '#001830',
364 land: '#232331',
365 outline: '#3a3a3a'
366 }
367 };
368
369 function shading() {
370 return tintOn ? {
371 palette: shadePalette[th.theme()],
372 flip: shadeFlip
373 } : '';
374 }
Simon Hunt2362b072015-06-11 20:08:22 -0700375
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700376 function setMap(map) {
377 ps.setPrefs('topo_mapid', map);
378 setUpMap($loc);
379 opacifyMap(true);
380 }
381
382 function currentMap() {
383 return ps.getPrefs(
384 'topo_mapid',
385 { mapid: 'usa', mapscale: 1, tint: 'off'},
386 $loc.search()
387 );
388 }
389
Simon Hunt2362b072015-06-11 20:08:22 -0700390 function setUpMap($loc) {
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700391 var prefs = currentMap(),
Simon Hunt8a119b02016-03-29 16:00:32 -0700392 mapId = prefs.mapid,
393 mapScale = prefs.mapscale,
394 tint = prefs.tint,
Simon Hunt2362b072015-06-11 20:08:22 -0700395 promise,
Simon Huntfacad992016-02-25 09:58:33 -0800396 cfilter;
Simon Hunt2362b072015-06-11 20:08:22 -0700397
Simon Hunta34fcb52016-02-25 16:27:32 -0800398 tintOn = tint === 'on' ? 1 : 0;
399
Simon Hunt18491362016-03-04 11:09:42 -0800400 $log.debug('setUpMap() mapId:', mapId, ', mapScale:', mapScale,
401 ', tint:', tint);
402
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700403 mapG = d3.select('#topo-map');
404 if (mapG.empty()) {
405 mapG = zoomLayer.append('g').attr('id', 'topo-map');
406 } else {
407 mapG.each(function(d,i) {
408 d3.selectAll(this.childNodes).remove();
409 });
410 }
Simon Hunt2362b072015-06-11 20:08:22 -0700411 if (mapId === 'usa') {
Simon Hunta34fcb52016-02-25 16:27:32 -0800412 shadeFlip = 0;
413 promise = ms.loadMapInto(mapG, '*continental_us', {
414 adjustScale: mapScale,
415 shading: shading()
416 });
Simon Hunte1bef6e2016-02-24 22:16:04 -0800417 } else if (mapId === 'bayarea') {
Simon Hunta34fcb52016-02-25 16:27:32 -0800418 shadeFlip = 1;
Simon Huntfacad992016-02-25 09:58:33 -0800419 promise = ms.loadMapInto(mapG, '*bayarea', {
420 objectTag: 'bayareaGEO',
421 adjustScale: mapScale,
Simon Hunta34fcb52016-02-25 16:27:32 -0800422 shading: shading()
Simon Huntfacad992016-02-25 09:58:33 -0800423 });
zylin3077df52016-03-04 10:40:17 +0800424 } else if (mapId === 'taiwan') {
425 shadeFlip = 0;
426 promise = ms.loadMapInto(mapG, '*taiwan', {
427 objectTag: 'taiwan',
428 adjustScale: mapScale,
429 shading: shading()
430 })
Simon Hunt2362b072015-06-11 20:08:22 -0700431 } else {
Simon Hunta34fcb52016-02-25 16:27:32 -0800432 shadeFlip = 0;
Simon Hunt2362b072015-06-11 20:08:22 -0700433 cfilter = countryFilters[mapId] || countryFilters.world;
Simon Hunta34fcb52016-02-25 16:27:32 -0800434 promise = ms.loadMapRegionInto(mapG, {
435 countryFilter: cfilter,
436 adjustScale: mapScale,
437 shading: shading()
438 });
Simon Hunt2362b072015-06-11 20:08:22 -0700439 }
Simon Hunt8a119b02016-03-29 16:00:32 -0700440 ps.setPrefs('topo_mapid', prefs);
Simon Hunt2362b072015-06-11 20:08:22 -0700441 return promise;
Simon Hunt0541fb82015-01-14 18:59:57 -0800442 }
443
Simon Hunta34fcb52016-02-25 16:27:32 -0800444 // set up theme listener to re-shade the map when required.
445 function mapShader(on) {
446 if (on) {
447 themeListener = th.addListener(function () {
448 ms.reshade(shading());
449 });
450 } else {
451 th.removeListener(themeListener);
452 themeListener = null;
453 }
454 }
455
Simon Huntc3c5b672015-02-20 11:32:13 -0800456 function opacifyMap(b) {
457 mapG.transition()
458 .duration(1000)
459 .attr('opacity', b ? 1 : 0);
460 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800461
Simon Huntf9fc0e72015-04-16 15:10:57 -0700462 function setUpSprites($loc, tspr) {
Simon Hunt8a119b02016-03-29 16:00:32 -0700463 var prefs = ps.getPrefs('topo_sprites', { sprites: '' }, $loc.search()),
464 sprId = prefs.sprites;
Simon Huntf9fc0e72015-04-16 15:10:57 -0700465
466 spriteG = zoomLayer.append ('g').attr('id', 'topo-sprites');
467 if (sprId) {
Simon Hunt8a119b02016-03-29 16:00:32 -0700468 ps.setPrefs('topo_sprites', prefs);
Simon Huntf9fc0e72015-04-16 15:10:57 -0700469 tspr.loadSprites(spriteG, defs, sprId);
470 }
471 }
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700472
Simon Huntc7ae7952015-04-08 18:59:27 -0700473 // --- User Preferemces ----------------------------------------------
Simon Hunt36a58c62015-04-08 11:00:07 -0700474
Simon Huntc7ae7952015-04-08 18:59:27 -0700475 var prefsState = {};
Simon Hunt36a58c62015-04-08 11:00:07 -0700476
Simon Huntc7ae7952015-04-08 18:59:27 -0700477 function updatePrefsState(what, b) {
478 prefsState[what] = b ? 1 : 0;
479 ps.setPrefs('topo_prefs', prefsState);
Simon Hunt36a58c62015-04-08 11:00:07 -0700480 }
481
Simon Hunt36a58c62015-04-08 11:00:07 -0700482
Simon Huntc7ae7952015-04-08 18:59:27 -0700483 function restoreConfigFromPrefs() {
484 // NOTE: toolbar will have set this for us..
Thomas Vachuska0af26912016-03-21 21:37:30 -0700485 prefsState = ps.asNumbers(ps.getPrefs('topo_prefs', ttbs.defaultPrefs));
Simon Hunt36a58c62015-04-08 11:00:07 -0700486
Simon Hunte1ce4292015-06-12 13:07:02 -0700487 $log.debug('TOPO- Prefs State:', prefsState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700488
Simon Huntf41f3092015-04-16 10:33:26 -0700489 flash.enable(false);
Simon Huntc7ae7952015-04-08 18:59:27 -0700490 toggleInstances(prefsState.insts);
Simon Huntee7a3ce2015-04-09 13:28:37 -0700491 toggleSummary(prefsState.summary);
Simon Hunt239e5882015-04-23 15:07:04 -0700492 toggleUseDetailsFlag(prefsState.detail);
Bri Prebilic Cole9b80ca02015-08-03 11:26:54 -0700493 toggleHosts(prefsState.hosts);
494 toggleOffline(prefsState.offdev);
495 togglePorts(prefsState.porthl);
496 toggleMap(prefsState.bg);
Simon Huntf9fc0e72015-04-16 15:10:57 -0700497 toggleSprites(prefsState.spr);
Thomas Vachuska0af26912016-03-21 21:37:30 -0700498 t3s.setDevLabIndex(prefsState.dlbls);
Simon Huntf41f3092015-04-16 10:33:26 -0700499 flash.enable(true);
Simon Hunt36a58c62015-04-08 11:00:07 -0700500 }
501
502
Simon Hunte1ce4292015-06-12 13:07:02 -0700503 // somewhat hackish, because summary update cannot happen until we
504 // have opened the websocket to the server; hence this extra function
505 // invoked after tes.start()
506 function restoreSummaryFromPrefs() {
Thomas Vachuska0af26912016-03-21 21:37:30 -0700507 prefsState = ps.asNumbers(ps.getPrefs('topo_prefs', ttbs.defaultPrefs));
Simon Hunte1ce4292015-06-12 13:07:02 -0700508 $log.debug('TOPO- Prefs SUMMARY State:', prefsState.summary);
509
510 flash.enable(false);
511 toggleSummary(prefsState.summary);
512 flash.enable(true);
513 }
514
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700515 function topoStartDone() {
516 var d = $scope.intentData;
517 if (d) {
518 tts.selectIntent(d);
519 }
520 }
Simon Hunte1ce4292015-06-12 13:07:02 -0700521
Simon Huntcacce342015-01-07 16:13:05 -0800522 // --- Controller Definition -----------------------------------------
523
Simon Hunt6cc53692015-01-07 11:33:45 -0800524 angular.module('ovTopo', moduleDependencies)
Simon Hunt237676b52015-03-10 19:04:26 -0700525 .controller('OvTopoCtrl', ['$scope', '$log', '$location', '$timeout',
Simon Hunt36a58c62015-04-08 11:00:07 -0700526 '$cookies', 'FnService', 'MastService', 'KeyService', 'ZoomService',
Simon Hunt0e48c292015-02-19 16:11:37 -0800527 'GlyphService', 'MapService', 'SvgUtilService', 'FlashService',
Simon Hunta34fcb52016-02-25 16:27:32 -0800528 'WebSocketService', 'PrefsService', 'ThemeService',
Thomas Vachuska0af26912016-03-21 21:37:30 -0700529 'TopoDialogService', 'TopoD3Service',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800530 'TopoEventService', 'TopoForceService', 'TopoPanelService',
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700531 'TopoInstService', 'TopoSelectService', 'TopoLinkService',
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700532 'TopoTrafficService', 'TopoObliqueService', 'TopoFilterService',
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700533 'TopoToolbarService', 'TopoMapService', 'TopoSpriteService',
534 'TooltipService', 'TopoOverlayService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800535
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700536 function (_$scope_, _$log_, _$loc_, $timeout, _$cookies_, _fs_, mast, _ks_,
Simon Hunta34fcb52016-02-25 16:27:32 -0800537 _zs_, _gs_, _ms_, _sus_, _flash_, _wss_, _ps_, _th_,
Thomas Vachuska0af26912016-03-21 21:37:30 -0700538 _tds_, _t3s_, _tes_,
Simon Hunta211f7f2015-11-09 12:48:23 -0800539 _tfs_, _tps_, _tis_, _tss_, _tls_, _tts_, _tos_, _fltr_,
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700540 _ttbs_, _tms_, _tspr_, _ttip_, _tov_) {
541 var params = _$loc_.search(),
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700542 projection,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800543 dim,
Simon Hunt1894d792015-02-04 17:09:20 -0800544 uplink = {
545 // provides function calls back into this space
546 showNoDevs: showNoDevs,
547 projection: function () { return projection; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800548 zoomLayer: function () { return zoomLayer; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800549 zoomer: function () { return zoomer; },
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700550 opacifyMap: opacifyMap,
551 topoStartDone: topoStartDone
Simon Huntac4c6f72015-02-03 19:50:53 -0800552 };
553
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700554 $scope = _$scope_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800555 $log = _$log_;
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700556 $loc = _$loc_;
Simon Hunt36a58c62015-04-08 11:00:07 -0700557 $cookies = _$cookies_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800558 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800559 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800560 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800561 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800562 ms = _ms_;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800563 sus = _sus_;
Simon Hunt0e48c292015-02-19 16:11:37 -0800564 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700565 wss = _wss_;
Simon Huntc7ae7952015-04-08 18:59:27 -0700566 ps = _ps_;
Simon Hunta34fcb52016-02-25 16:27:32 -0800567 th = _th_;
Simon Hunta211f7f2015-11-09 12:48:23 -0800568 tds = _tds_;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700569 t3s = _t3s_;
Simon Huntc252aa62015-02-10 16:45:39 -0800570 tes = _tes_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800571 tfs = _tfs_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800572 // TODO: consider funnelling actions through TopoForceService...
573 // rather than injecting references to these 'sub-modules',
574 // just so we can invoke functions on them.
Simon Huntc252aa62015-02-10 16:45:39 -0800575 tps = _tps_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800576 tis = _tis_;
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700577 tms = _tms_;
Simon Hunt6036b192015-02-11 11:20:26 -0800578 tss = _tss_;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700579 tls = _tls_;
Simon Huntf542d842015-02-11 16:20:33 -0800580 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800581 tos = _tos_;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700582 fltr = _fltr_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700583 ttbs = _ttbs_;
Simon Hunta4281242016-02-26 20:18:45 -0800584 tspr = _tspr_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700585 ttip = _ttip_;
Simon Hunt72e44bf2015-07-21 21:34:20 -0700586 tov = _tov_;
Simon Huntef31fb22014-12-19 13:16:44 -0800587
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700588 tms.start({
589 toggleMap: toggleMap,
590 currentMap: currentMap,
591 setMap: setMap
592 });
593
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700594 if (params.intentKey && params.intentAppId && params.intentAppName) {
595 $scope.intentData = {
596 key: params.intentKey,
597 appId: params.intentAppId,
598 appName: params.intentAppName
599 };
600 }
601
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700602 $scope.notifyResize = function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800603 svgResized(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800604 };
Simon Huntef31fb22014-12-19 13:16:44 -0800605
Simon Hunt54442fa2015-01-26 14:17:38 -0800606 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800607 $scope.$on('$destroy', function () {
608 $log.log('OvTopoCtrl is saying Buh-Bye!');
Thomas Vachuska329af532015-03-10 02:08:33 -0700609 tes.stop();
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700610 tms.stop();
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700611 ks.unbindKeys();
Simon Hunt626d2102015-01-29 11:54:50 -0800612 tps.destroyPanels();
Simon Hunta211f7f2015-11-09 12:48:23 -0800613 tds.closeDialog();
Simon Hunt4b668592015-01-29 17:33:53 -0800614 tis.destroyInst();
Simon Hunt3a6eec02015-02-09 21:16:43 -0800615 tfs.destroyForce();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700616 ttbs.destroyToolbar();
Simon Hunta34fcb52016-02-25 16:27:32 -0800617 mapShader(false);
Simon Hunt584122a2015-01-21 15:32:40 -0800618 });
619
Simon Huntcacce342015-01-07 16:13:05 -0800620 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800621 ovtopo = d3.select('#ov-topo');
622 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800623 // set the svg size to match that of the window, less the masthead
624 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt3a6eec02015-02-09 21:16:43 -0800625 dim = [svg.attr('width'), svg.attr('height')];
Simon Hunt426bd862015-01-14 16:48:41 -0800626
Simon Hunt6cc53692015-01-07 11:33:45 -0800627 setUpKeys();
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700628 setUpToolbar();
Simon Hunt6cc53692015-01-07 11:33:45 -0800629 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800630 setUpZoom();
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800631 setUpNoDevs();
Simon Hunt2362b072015-06-11 20:08:22 -0700632 setUpMap($loc).then(
Simon Hunt1894d792015-02-04 17:09:20 -0800633 function (proj) {
Thomas Vachuska0af26912016-03-21 21:37:30 -0700634 var z = ps.getPrefs('topo_zoom', { tx:0, ty:0, sc:1 });
Simon Huntbb5e0d82015-04-16 14:25:46 -0700635 zoomer.panZoom([z.tx, z.ty], z.sc);
636 $log.debug('** Zoom restored:', z);
637
Simon Hunt1894d792015-02-04 17:09:20 -0800638 projection = proj;
Simon Huntbb5e0d82015-04-16 14:25:46 -0700639 $log.debug('** We installed the projection:', proj);
Simon Huntf41f3092015-04-16 10:33:26 -0700640 flash.enable(false);
Simon Huntc7ae7952015-04-08 18:59:27 -0700641 toggleMap(prefsState.bg);
Simon Huntf41f3092015-04-16 10:33:26 -0700642 flash.enable(true);
Simon Hunta34fcb52016-02-25 16:27:32 -0800643 mapShader(true);
Simon Hunte1ce4292015-06-12 13:07:02 -0700644
645 // now we have the map projection, we are ready for
646 // the server to send us device/host data...
647 tes.start();
648 // need to do the following so we immediately get
649 // the summary panel data back from the server
650 restoreSummaryFromPrefs();
Simon Hunt1894d792015-02-04 17:09:20 -0800651 }
652 );
Simon Hunta4281242016-02-26 20:18:45 -0800653 tes.bindHandlers();
Simon Huntf9fc0e72015-04-16 15:10:57 -0700654 setUpSprites($loc, tspr);
Simon Huntfd1231a2015-01-26 22:14:51 -0800655
Simon Hunt1894d792015-02-04 17:09:20 -0800656 forceG = zoomLayer.append('g').attr('id', 'topo-force');
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800657 tfs.initForce(svg, forceG, uplink, dim);
Simon Hunta142dd22015-02-12 22:07:51 -0800658 tis.initInst({ showMastership: tfs.showMastership });
Simon Hunt237676b52015-03-10 19:04:26 -0700659 tps.initPanels();
Simon Hunt6cc53692015-01-07 11:33:45 -0800660
Simon Hunt36a58c62015-04-08 11:00:07 -0700661 // temporary solution for persisting user settings
Simon Huntc7ae7952015-04-08 18:59:27 -0700662 restoreConfigFromPrefs();
Simon Hunta5b53af2015-10-12 15:56:40 -0700663 ttbs.setDefaultOverlay();
Simon Hunt36a58c62015-04-08 11:00:07 -0700664
Simon Hunt72e44bf2015-07-21 21:34:20 -0700665 $log.debug('registered overlays...', tov.list());
Simon Hunt6cc53692015-01-07 11:33:45 -0800666 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800667 }]);
668}());