blob: f39d61b71fc297ef7eb33e54cccb079ac0bd799d [file] [log] [blame]
Simon Huntef31fb22014-12-19 13:16:44 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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',
Simon Hunt5c3ed732017-07-20 19:03:28 +000028 'onosRemote'
Simon Hunt6cc53692015-01-07 11:33:45 -080029 ];
30
Simon Hunta4281242016-02-26 20:18:45 -080031 // references to injected services
Simon Hunt5c3ed732017-07-20 19:03:28 +000032 var $scope, $log, $loc, $timeout, $cookies,
Simon Hunt7faabd52016-08-18 16:16:19 -070033 fs, ks, zs, gs, ms, sus, flash, wss, ps, th, tds, t3s, tes, tfs, tps,
Simon Hunt5c3ed732017-07-20 19:03:28 +000034 tis, tms, tss, tls, tts, tos, fltr, ttbs, tspr, 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 Hunt5989ddf2017-08-02 20:38:12 -070040 var zoomer,
41 actionMap,
42 topoLion = function (x) { return '#' + x + '#'}; // func replaced later
Simon Hunt6cc53692015-01-07 11:33:45 -080043
Simon Huntcacce342015-01-07 16:13:05 -080044 // --- Short Cut Keys ------------------------------------------------
45
Simon Hunt8d22c4b2015-08-06 16:24:43 -070046 function setUpKeys(overlayKeys) {
Simon Hunt5724fb42015-02-05 16:59:40 -080047 // key bindings need to be made after the services have been injected
48 // thus, deferred to here...
Simon Hunt5989ddf2017-08-02 20:38:12 -070049
50 // we need functions that can be invoked after LION bundle loaded
51 function togInst() {return topoLion('tbtt_tog_instances')}
52 function togSumm() {return topoLion('tbtt_tog_summary')}
53 function togUseDet() {return topoLion('tbtt_tog_use_detail')}
54
55 function togHost() {return topoLion('tbtt_tog_host')}
56 function togOff() {return topoLion('tbtt_tog_offline')}
57 function togPortHi() {return topoLion('tbtt_tog_porthi')}
58
59 function showBad() {return topoLion('tbtt_bad_links')}
60 function togMap() {return topoLion('tbtt_tog_map')}
61 function selMap() {return topoLion('tbtt_sel_map')}
62 function togSpr() {return topoLion('tbtt_tog_sprite')}
63
64 function rstLoc() {return topoLion('tbtt_reset_loc')}
65 function togOb() {return topoLion('tbtt_tog_oblique')}
66 function cycLayer() {return topoLion('tbtt_cyc_layers')}
67 function cycDev() {return topoLion('tbtt_cyc_dev_labs')}
68 function cycHost() {return topoLion('tbtt_cyc_host_labs')}
69
70 function unpin() {return topoLion('tbtt_unpin_node')}
71 function rzoom() {return topoLion('tbtt_reset_zoom')}
72 function togtb() {return topoLion('tbtt_tog_toolbar')}
73 function eqmaster() {return topoLion('tbtt_eq_master')}
74
75 function uiClick() {return topoLion('click')}
76 function uiShClick() {return topoLion('shift_click')}
77 function uiDrag() {return topoLion('drag')}
78 function uiCmdScr() {return topoLion('cmd_scroll')}
79 function uiCmdDrag() {return topoLion('cmd_drag')}
80
81 function uiClickTxt() {return topoLion('qh_gest_click')}
82 function uiShClickTxt() {return topoLion('qh_gest_shift_click')}
83 function uiDragTxt() {return topoLion('qh_gest_drag')}
84 function uiCmdScrTxt() {return topoLion('qh_gest_cmd_scroll')}
85 function uiCmdDragTxt() {return topoLion('qh_gest_cmd_drag')}
86
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070087 actionMap = {
Simon Hunt5989ddf2017-08-02 20:38:12 -070088 I: [toggleInstances, togInst],
89 O: [toggleSummary, togSumm],
90 D: [toggleUseDetailsFlag, togUseDet],
Simon Huntac4c6f72015-02-03 19:50:53 -080091
Simon Hunt5989ddf2017-08-02 20:38:12 -070092 H: [toggleHosts, togHost],
93 M: [toggleOffline, togOff],
94 P: [togglePorts, togPortHi],
Simon Huntac4c6f72015-02-03 19:50:53 -080095
Simon Hunt5989ddf2017-08-02 20:38:12 -070096 dash: [tfs.showBadLinks, showBad],
97 B: [toggleMap, togMap],
98 G: [openMapSelection, selMap],
99 S: [toggleSprites, togSpr],
Simon Huntac4c6f72015-02-03 19:50:53 -0800100
Simon Hunt5989ddf2017-08-02 20:38:12 -0700101 X: [tfs.resetAllLocations, rstLoc],
102 Z: [tos.toggleOblique, togOb],
103 N: [fltr.clickAction, cycLayer],
104 L: [tfs.cycleDeviceLabels, cycDev],
105 'shift-L': [tfs.cycleHostLabels, cycHost],
106
107 U: [tfs.unpin, unpin],
108 R: [resetZoom, rzoom],
109 dot: [ttbs.toggleToolbar, togtb],
110 E: [equalizeMasters, eqmaster],
Simon Huntac4c6f72015-02-03 19:50:53 -0800111
Simon Hunt5c3ed732017-07-20 19:03:28 +0000112 //-- instance color palette debug
Simon Hunta9761342016-06-10 18:02:53 -0700113 // 9: function () { sus.cat7().testCard(svg); },
114
Simon Hunt5b024d72016-01-29 11:02:43 -0800115 // topology overlay selections
116 F1: function () { ttbs.fnkey(0); },
117 F2: function () { ttbs.fnkey(1); },
118 F3: function () { ttbs.fnkey(2); },
119 F4: function () { ttbs.fnkey(3); },
120 F5: function () { ttbs.fnkey(4); },
121
Simon Hunt08f841d02015-02-10 14:39:20 -0800122 esc: handleEscape,
Simon Huntac4c6f72015-02-03 19:50:53 -0800123
Simon Hunt09060142015-03-18 20:23:32 -0700124 _keyListener: ttbs.keyListener,
125
Simon Hunt5724fb42015-02-05 16:59:40 -0800126 _helpFormat: [
Simon Hunt5c3ed732017-07-20 19:03:28 +0000127 ['I', 'O', 'D', 'H', 'M', 'P', 'dash', 'B', 'G', 'S' ],
Simon Hunt10618f62017-06-15 19:30:52 -0700128 ['X', 'Z', 'N', 'L', 'shift-L', 'U', 'R', '-', 'E', '-', 'dot'],
Simon Hunt5c3ed732017-07-20 19:03:28 +0000129 [] // this column reserved for overlay actions
130 ]
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700131 };
132
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700133 if (fs.isO(overlayKeys)) {
134 mergeKeys(overlayKeys);
135 }
136
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700137 ks.keyBindings(actionMap);
Simon Huntac4c6f72015-02-03 19:50:53 -0800138
Simon Hunt639dc662015-02-18 14:19:20 -0800139 ks.gestureNotes([
Simon Hunt5989ddf2017-08-02 20:38:12 -0700140 [uiClick, uiClickTxt],
141 [uiShClick, uiShClickTxt],
142 [uiDrag, uiDragTxt],
143 [uiCmdScr, uiCmdScrTxt],
144 [uiCmdDrag, uiCmdDragTxt]
Simon Hunt639dc662015-02-18 14:19:20 -0800145 ]);
Simon Hunt5724fb42015-02-05 16:59:40 -0800146 }
Simon Hunt6cc53692015-01-07 11:33:45 -0800147
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700148 // when a topology overlay is activated, we need to bind their keystrokes
149 // and include them in the quick-help panel
150 function mergeKeys(extra) {
151 var _hf = actionMap._helpFormat[2];
Simon Hunt71892222015-09-29 13:39:40 -0700152
153 ks.checkNotGlobal(extra);
154
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700155 extra._keyOrder.forEach(function (k) {
156 var d = extra[k],
157 cb = d && d.cb,
158 tt = d && d.tt;
159 // NOTE: ignore keys that are already defined
160 if (d && !actionMap[k]) {
161 actionMap[k] = [cb, tt];
162 _hf.push(k);
163 }
164 });
165 }
166
Simon Hunt08f841d02015-02-10 14:39:20 -0800167 // --- Keystroke functions -------------------------------------------
Simon Huntac4c6f72015-02-03 19:50:53 -0800168
Simon Hunt36a58c62015-04-08 11:00:07 -0700169 function toggleInstances(x) {
Simon Huntee7a3ce2015-04-09 13:28:37 -0700170 updatePrefsState('insts', tis.toggle(x));
Simon Huntac4c6f72015-02-03 19:50:53 -0800171 tfs.updateDeviceColors();
Simon Hunt6cc53692015-01-07 11:33:45 -0800172 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800173
Simon Huntee7a3ce2015-04-09 13:28:37 -0700174 function toggleSummary(x) {
175 updatePrefsState('summary', tps.toggleSummary(x));
Simon Hunt18bf9822015-02-12 17:35:45 -0800176 }
177
Simon Hunt239e5882015-04-23 15:07:04 -0700178 function toggleUseDetailsFlag(x) {
179 updatePrefsState('detail', tps.toggleUseDetailsFlag(x));
Simon Huntee7a3ce2015-04-09 13:28:37 -0700180 }
181
Simon Huntfcbde892015-04-16 12:05:28 -0700182 function toggleHosts(x) {
183 updatePrefsState('hosts', tfs.toggleHosts(x));
184 }
185
186 function toggleOffline(x) {
187 updatePrefsState('offdev', tfs.toggleOffline(x));
188 }
189
190 function togglePorts(x) {
191 updatePrefsState('porthl', tfs.togglePorts(x));
192 }
193
194 function _togSvgLayer(x, G, tag, what) {
195 var on = (x === 'keyev') ? !sus.visible(G) : !!x,
Simon Huntee7a3ce2015-04-09 13:28:37 -0700196 verb = on ? 'Show' : 'Hide';
Simon Huntfcbde892015-04-16 12:05:28 -0700197 sus.visible(G, on);
198 updatePrefsState(tag, on);
199 flash.flash(verb + ' ' + what);
200 }
201
202 function toggleMap(x) {
203 _togSvgLayer(x, mapG, 'bg', 'background map');
Simon Huntee7a3ce2015-04-09 13:28:37 -0700204 }
Simon Hunt36a58c62015-04-08 11:00:07 -0700205
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700206 function openMapSelection() {
207 tms.openMapSelection();
208 }
209
Simon Hunt2052e5d2015-04-13 17:40:44 -0700210 function toggleSprites(x) {
Simon Huntfcbde892015-04-16 12:05:28 -0700211 _togSvgLayer(x, spriteG, 'spr', 'sprite layer');
Simon Hunt2052e5d2015-04-13 17:40:44 -0700212 }
213
Simon Hunt08f841d02015-02-10 14:39:20 -0800214 function resetZoom() {
215 zoomer.reset();
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700216 flash.flash('Pan and zoom reset');
Simon Hunt08f841d02015-02-10 14:39:20 -0800217 }
218
Simon Hunt0e48c292015-02-19 16:11:37 -0800219 function equalizeMasters() {
Simon Hunt237676b52015-03-10 19:04:26 -0700220 wss.sendEvent('equalizeMasters');
Simon Hunt0e48c292015-02-19 16:11:37 -0800221 flash.flash('Equalizing master roles');
222 }
223
Simon Hunt08f841d02015-02-10 14:39:20 -0800224 function handleEscape() {
Simon Hunta142dd22015-02-12 22:07:51 -0800225 if (tis.showMaster()) {
226 // if an instance is selected, cancel the affinity mapping
Simon Hunt5c3ed732017-07-20 19:03:28 +0000227 tis.cancelAffinity()
Simon Hunt08f841d02015-02-10 14:39:20 -0800228
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700229 } else if (tov.hooks.escape()) {
230 // else if the overlay consumed the ESC event...
231 // (work already done)
232
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700233 } else if (tss.deselectAll()) {
Simon Hunta0eb0a82015-02-11 12:30:06 -0800234 // else if we have node selections, deselect them all
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700235 // (work already done)
236
Prince Pereira46c82d42016-09-19 13:30:50 +0530237 } else if (tls.deselectAllLinks()) {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700238 // else if we have a link selected, deselect it
239 // (work already done)
Simon Hunt08f841d02015-02-10 14:39:20 -0800240
Simon Hunta0eb0a82015-02-11 12:30:06 -0800241 } else if (tis.isVisible()) {
242 // else if the Instance Panel is visible, hide it
243 tis.hide();
244 tfs.updateDeviceColors();
Simon Hunt08f841d02015-02-10 14:39:20 -0800245
Simon Hunta0eb0a82015-02-11 12:30:06 -0800246 } else if (tps.summaryVisible()) {
247 // else if the Summary Panel is visible, hide it
Simon Hunt356da762016-05-27 11:54:02 -0700248 tps.hideSummary();
Simon Hunta0eb0a82015-02-11 12:30:06 -0800249 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800250 }
Simon Huntcacce342015-01-07 16:13:05 -0800251
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700252 // --- Toolbar Functions ---------------------------------------------
253
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700254 function notValid(what) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700255 $log.warn('topo.js getActionEntry(): Not a valid ' + what);
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700256 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700257
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700258 function getActionEntry(key) {
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700259 var entry;
260
261 if (!key) {
262 notValid('key');
263 return null;
264 }
265
266 entry = actionMap[key];
267
268 if (!entry) {
269 notValid('actionMap entry');
270 return null;
271 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700272 return fs.isA(entry) || [entry, ''];
273 }
274
275 function setUpToolbar() {
276 ttbs.init({
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700277 getActionEntry: getActionEntry,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000278 setUpKeys: setUpKeys
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700279 });
280 ttbs.createToolbar();
281 }
282
Simon Huntcacce342015-01-07 16:13:05 -0800283 // --- Glyphs, Icons, and the like -----------------------------------
284
Simon Hunt6cc53692015-01-07 11:33:45 -0800285 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -0800286 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -0800287 gs.loadDefs(defs);
Simon Hunt0ee28682015-02-12 20:48:11 -0800288 sus.loadGlowDefs(defs);
Simon Hunt6cc53692015-01-07 11:33:45 -0800289 }
290
291
Simon Huntcacce342015-01-07 16:13:05 -0800292 // --- Pan and Zoom --------------------------------------------------
293
294 // zoom enabled predicate. ev is a D3 source event.
295 function zoomEnabled(ev) {
Simon Huntd552ee92015-04-02 17:06:35 -0700296 return fs.isMobile() || (ev.metaKey || ev.altKey);
Simon Huntcacce342015-01-07 16:13:05 -0800297 }
298
299 function zoomCallback() {
Simon Huntbb5e0d82015-04-16 14:25:46 -0700300 var sc = zoomer.scale(),
301 tr = zoomer.translate();
302
Simon Hunt5c3ed732017-07-20 19:03:28 +0000303 ps.setPrefs('topo_zoom', {tx:tr[0], ty:tr[1], sc:sc});
Simon Huntcacce342015-01-07 16:13:05 -0800304
Simon Hunt0541fb82015-01-14 18:59:57 -0800305 // keep the map lines constant width while zooming
Simon Hunt737c89f2015-01-28 12:23:19 -0800306 mapG.style('stroke-width', (2.0 / sc) + 'px');
Steven Burrowsf17f0ab2017-04-11 11:03:58 -0700307
308 tfs.setNodeScale(sc);
Simon Huntcacce342015-01-07 16:13:05 -0800309 }
310
311 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800312 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -0800313 zoomer = zs.createZoomer({
314 svg: svg,
315 zoomLayer: zoomLayer,
316 zoomEnabled: zoomEnabled,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000317 zoomCallback: zoomCallback
Simon Huntcacce342015-01-07 16:13:05 -0800318 });
319 }
320
321
Simon Hunt0541fb82015-01-14 18:59:57 -0800322 // callback invoked when the SVG view has been resized..
Simon Hunt3a6eec02015-02-09 21:16:43 -0800323 function svgResized(s) {
324 tfs.newDim([s.width, s.height]);
Simon Hunt0541fb82015-01-14 18:59:57 -0800325 }
326
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800327 // --- Background Map ------------------------------------------------
328
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800329 function setUpNoDevs() {
330 var g, box;
331 noDevsLayer = svg.append('g').attr({
332 id: 'topo-noDevsLayer',
Simon Hunt5c3ed732017-07-20 19:03:28 +0000333 transform: sus.translate(500,500)
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800334 });
335 // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html.
336 // We are translating this layer to have its origin at the center
337
338 g = noDevsLayer.append('g');
339 gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird');
340 g.append('text').text('No devices are connected')
Simon Hunt5c3ed732017-07-20 19:03:28 +0000341 .attr({ x: 120, y: 80});
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800342
343 box = g.node().getBBox();
344 box.x -= box.width/2;
345 box.y -= box.height/2;
346 g.attr('transform', sus.translate(box.x, box.y));
347
348 showNoDevs(true);
349 }
350
351 function showNoDevs(b) {
Simon Hunt18bf9822015-02-12 17:35:45 -0800352 sus.visible(noDevsLayer, b);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800353 }
354
Simon Hunt2362b072015-06-11 20:08:22 -0700355
356 var countryFilters = {
Simon Hunt2362b072015-06-11 20:08:22 -0700357 s_america: function (c) {
358 return c.properties.continent === 'South America';
359 },
360
Simon Huntac099e82015-12-03 09:17:47 -0800361 ns_america: function (c) {
362 return c.properties.custom === 'US-cont' ||
363 c.properties.subregion === 'Central America' ||
Simon Huntd4712622015-12-02 11:19:50 -0800364 c.properties.continent === 'South America';
365 },
366
Simon Hunt2362b072015-06-11 20:08:22 -0700367 japan: function (c) {
368 return c.properties.geounit === 'Japan';
369 },
370
371 europe: function (c) {
372 return c.properties.continent === 'Europe';
373 },
374
375 italy: function (c) {
376 return c.properties.geounit === 'Italy';
377 },
378
379 uk: function (c) {
380 // technically, Ireland is not part of the United Kingdom,
381 // but the map looks weird without it showing.
382 return c.properties.adm0_a3 === 'GBR' ||
383 c.properties.adm0_a3 === 'IRL';
Simon Hunt1178d5b2015-09-02 17:02:37 -0700384 },
385
386 s_korea: function (c) {
387 return c.properties.adm0_a3 === 'KOR';
388 },
389
390 australia: function (c) {
391 return c.properties.adm0_a3 === 'AUS';
Simon Hunt5c3ed732017-07-20 19:03:28 +0000392 }
Simon Hunt2362b072015-06-11 20:08:22 -0700393 };
394
Simon Hunta34fcb52016-02-25 16:27:32 -0800395 var tintOn = 0,
396 shadeFlip = 0,
397 shadePalette = {
398 light: {
399 sea: 'aliceblue',
400 land: 'white',
Simon Hunt5c3ed732017-07-20 19:03:28 +0000401 outline: '#ddd'
Simon Hunta34fcb52016-02-25 16:27:32 -0800402 },
403 dark: {
404 sea: '#001830',
405 land: '#232331',
Simon Hunt5c3ed732017-07-20 19:03:28 +0000406 outline: '#3a3a3a'
407 }
Simon Hunta34fcb52016-02-25 16:27:32 -0800408 };
409
410 function shading() {
411 return tintOn ? {
412 palette: shadePalette[th.theme()],
Simon Hunt5c3ed732017-07-20 19:03:28 +0000413 flip: shadeFlip
Simon Hunta34fcb52016-02-25 16:27:32 -0800414 } : '';
415 }
Simon Hunt2362b072015-06-11 20:08:22 -0700416
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700417 function setMap(map) {
418 ps.setPrefs('topo_mapid', map);
Simon Hunt90053ed2016-04-06 12:49:02 -0700419 setUpMap();
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700420 opacifyMap(true);
421 }
422
423 function currentMap() {
424 return ps.getPrefs(
425 'topo_mapid',
Steven Burrows3a9a6442016-05-05 15:31:16 +0100426 {
427 mapid: 'usa',
428 mapscale: 1,
429 mapfilepath: '*continental_us',
Simon Hunt5c3ed732017-07-20 19:03:28 +0000430 tint: 'off'
Steven Burrows3a9a6442016-05-05 15:31:16 +0100431 },
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700432 $loc.search()
433 );
434 }
435
Simon Hunt90053ed2016-04-06 12:49:02 -0700436 function setUpMap() {
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700437 var prefs = currentMap(),
Simon Hunt8a119b02016-03-29 16:00:32 -0700438 mapId = prefs.mapid,
Steven Burrows3a9a6442016-05-05 15:31:16 +0100439 mapFilePath = prefs.mapfilepath,
Simon Hunt8a119b02016-03-29 16:00:32 -0700440 mapScale = prefs.mapscale,
441 tint = prefs.tint,
Simon Hunt2362b072015-06-11 20:08:22 -0700442 promise,
Simon Huntfacad992016-02-25 09:58:33 -0800443 cfilter;
Simon Hunt2362b072015-06-11 20:08:22 -0700444
Simon Hunta34fcb52016-02-25 16:27:32 -0800445 tintOn = tint === 'on' ? 1 : 0;
446
Simon Hunt18491362016-03-04 11:09:42 -0800447 $log.debug('setUpMap() mapId:', mapId, ', mapScale:', mapScale,
448 ', tint:', tint);
449
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700450 mapG = d3.select('#topo-map');
451 if (mapG.empty()) {
452 mapG = zoomLayer.append('g').attr('id', 'topo-map');
453 } else {
Simon Hunt5c3ed732017-07-20 19:03:28 +0000454 mapG.each(function(d,i) {
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700455 d3.selectAll(this.childNodes).remove();
456 });
457 }
Steven Burrows3a9a6442016-05-05 15:31:16 +0100458
459 if (mapFilePath === '*countries') {
460
Simon Hunt90053ed2016-04-06 12:49:02 -0700461 cfilter = countryFilters[mapId] || countryFilters.uk;
Steven Burrows3a9a6442016-05-05 15:31:16 +0100462
Simon Hunta34fcb52016-02-25 16:27:32 -0800463 promise = ms.loadMapRegionInto(mapG, {
464 countryFilter: cfilter,
465 adjustScale: mapScale,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000466 shading: shading()
Simon Hunta34fcb52016-02-25 16:27:32 -0800467 });
Steven Burrows3a9a6442016-05-05 15:31:16 +0100468 } else {
469
470 promise = ms.loadMapInto(mapG, mapFilePath, mapId, {
471 adjustScale: mapScale,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000472 shading: shading()
Steven Burrows3a9a6442016-05-05 15:31:16 +0100473 });
Simon Hunt2362b072015-06-11 20:08:22 -0700474 }
Steven Burrows3a9a6442016-05-05 15:31:16 +0100475
Simon Hunt8a119b02016-03-29 16:00:32 -0700476 ps.setPrefs('topo_mapid', prefs);
Simon Hunt2362b072015-06-11 20:08:22 -0700477 return promise;
Simon Hunt0541fb82015-01-14 18:59:57 -0800478 }
479
Simon Huntf51bf462016-06-29 16:22:57 -0700480 function mapReshader() {
Simon Hunt5c3ed732017-07-20 19:03:28 +0000481 $log.debug('... Re-shading map ...')
Simon Huntf51bf462016-06-29 16:22:57 -0700482 ms.reshade(shading());
483 }
484
Simon Hunta34fcb52016-02-25 16:27:32 -0800485 // set up theme listener to re-shade the map when required.
486 function mapShader(on) {
487 if (on) {
Simon Huntf51bf462016-06-29 16:22:57 -0700488 th.addListener(mapReshader);
Simon Hunta34fcb52016-02-25 16:27:32 -0800489 } else {
Simon Huntf51bf462016-06-29 16:22:57 -0700490 th.removeListener(mapReshader);
Simon Hunta34fcb52016-02-25 16:27:32 -0800491 }
492 }
493
Simon Huntc3c5b672015-02-20 11:32:13 -0800494 function opacifyMap(b) {
495 mapG.transition()
496 .duration(1000)
497 .attr('opacity', b ? 1 : 0);
498 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800499
Simon Hunt90053ed2016-04-06 12:49:02 -0700500 function setUpSprites() {
Simon Hunt8a119b02016-03-29 16:00:32 -0700501 var prefs = ps.getPrefs('topo_sprites', { sprites: '' }, $loc.search()),
502 sprId = prefs.sprites;
Simon Huntf9fc0e72015-04-16 15:10:57 -0700503
Simon Hunt5c3ed732017-07-20 19:03:28 +0000504 spriteG = zoomLayer.append ('g').attr('id', 'topo-sprites');
Simon Huntf9fc0e72015-04-16 15:10:57 -0700505 if (sprId) {
Simon Hunt8a119b02016-03-29 16:00:32 -0700506 ps.setPrefs('topo_sprites', prefs);
Simon Huntf9fc0e72015-04-16 15:10:57 -0700507 tspr.loadSprites(spriteG, defs, sprId);
508 }
509 }
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700510
Simon Huntc7ae7952015-04-08 18:59:27 -0700511 // --- User Preferemces ----------------------------------------------
Simon Hunt36a58c62015-04-08 11:00:07 -0700512
Simon Huntc7ae7952015-04-08 18:59:27 -0700513 var prefsState = {};
Simon Hunt36a58c62015-04-08 11:00:07 -0700514
Simon Huntc7ae7952015-04-08 18:59:27 -0700515 function updatePrefsState(what, b) {
516 prefsState[what] = b ? 1 : 0;
517 ps.setPrefs('topo_prefs', prefsState);
Simon Hunt36a58c62015-04-08 11:00:07 -0700518 }
519
Simon Hunt36a58c62015-04-08 11:00:07 -0700520
Simon Huntc7ae7952015-04-08 18:59:27 -0700521 function restoreConfigFromPrefs() {
522 // NOTE: toolbar will have set this for us..
Simon Huntfc5c5842017-02-01 23:32:18 -0800523 prefsState = ps.asNumbers(
524 ps.getPrefs('topo_prefs', ttbs.defaultPrefs), ['ovid'], true
525 );
Simon Hunt36a58c62015-04-08 11:00:07 -0700526
Simon Hunte1ce4292015-06-12 13:07:02 -0700527 $log.debug('TOPO- Prefs State:', prefsState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700528
Simon Huntf41f3092015-04-16 10:33:26 -0700529 flash.enable(false);
Simon Huntc7ae7952015-04-08 18:59:27 -0700530 toggleInstances(prefsState.insts);
Simon Huntee7a3ce2015-04-09 13:28:37 -0700531 toggleSummary(prefsState.summary);
Simon Hunt239e5882015-04-23 15:07:04 -0700532 toggleUseDetailsFlag(prefsState.detail);
Bri Prebilic Cole9b80ca02015-08-03 11:26:54 -0700533 toggleHosts(prefsState.hosts);
534 toggleOffline(prefsState.offdev);
535 togglePorts(prefsState.porthl);
536 toggleMap(prefsState.bg);
Simon Huntf9fc0e72015-04-16 15:10:57 -0700537 toggleSprites(prefsState.spr);
Thomas Vachuska0af26912016-03-21 21:37:30 -0700538 t3s.setDevLabIndex(prefsState.dlbls);
Simon Hunt10618f62017-06-15 19:30:52 -0700539 t3s.setHostLabIndex(prefsState.hlbls);
Simon Huntf41f3092015-04-16 10:33:26 -0700540 flash.enable(true);
Simon Hunt36a58c62015-04-08 11:00:07 -0700541 }
542
543
Simon Hunte1ce4292015-06-12 13:07:02 -0700544 // somewhat hackish, because summary update cannot happen until we
545 // have opened the websocket to the server; hence this extra function
546 // invoked after tes.start()
547 function restoreSummaryFromPrefs() {
Simon Huntfc5c5842017-02-01 23:32:18 -0800548 prefsState = ps.asNumbers(
549 ps.getPrefs('topo_prefs', ttbs.defaultPrefs), ['ovid'], true
550 );
Simon Hunte1ce4292015-06-12 13:07:02 -0700551 $log.debug('TOPO- Prefs SUMMARY State:', prefsState.summary);
552
553 flash.enable(false);
554 toggleSummary(prefsState.summary);
555 flash.enable(true);
556 }
557
Simon Hunt8419efd2017-01-12 12:36:28 -0800558 // initial set of topo events received, now do post-processing
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700559 function topoStartDone() {
Simon Hunt8419efd2017-01-12 12:36:28 -0800560 // give a small delay before attempting to reselect node(s) and
561 // highlight elements, since they have to be re-added to the DOM first...
Simon Hunt7faabd52016-08-18 16:16:19 -0700562 $timeout(function () {
Simon Hunt8419efd2017-01-12 12:36:28 -0800563 $log.debug('^^ topo.topoStartDone() ^^');
564
565 // reselect the previous selection...
Simon Hunt7faabd52016-08-18 16:16:19 -0700566 tss.reselect();
Simon Hunt8419efd2017-01-12 12:36:28 -0800567
568 // if an intent should be shown, invoke the appropriate callback
569 if ($scope.intentData) {
Simon Hunt8419efd2017-01-12 12:36:28 -0800570 tov.hooks.showIntent($scope.intentData);
Simon Hunt7faabd52016-08-18 16:16:19 -0700571 }
Simon Hunt8419efd2017-01-12 12:36:28 -0800572
Simon Hunt7faabd52016-08-18 16:16:19 -0700573 }, 200);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700574 }
Simon Hunte1ce4292015-06-12 13:07:02 -0700575
Simon Huntcacce342015-01-07 16:13:05 -0800576 // --- Controller Definition -----------------------------------------
577
Simon Hunt6cc53692015-01-07 11:33:45 -0800578 angular.module('ovTopo', moduleDependencies)
Simon Hunt5989ddf2017-08-02 20:38:12 -0700579 .controller('OvTopoCtrl',
580 ['$scope', '$log', '$location', '$timeout', '$cookies',
581 'FnService', 'MastService', 'KeyService', 'ZoomService',
Simon Hunt0e48c292015-02-19 16:11:37 -0800582 'GlyphService', 'MapService', 'SvgUtilService', 'FlashService',
Simon Hunta34fcb52016-02-25 16:27:32 -0800583 'WebSocketService', 'PrefsService', 'ThemeService',
Simon Hunt5989ddf2017-08-02 20:38:12 -0700584 'TopoDialogService', 'TopoD3Service', 'TopoEventService',
585 'TopoForceService', 'TopoPanelService', 'TopoInstService',
586 'TopoSelectService', 'TopoLinkService', 'TopoTrafficService',
587 'TopoObliqueService', 'TopoFilterService', 'TopoToolbarService',
588 'TopoMapService', 'TopoSpriteService', 'TooltipService',
589 'TopoOverlayService', 'LionService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800590
Simon Hunt5989ddf2017-08-02 20:38:12 -0700591 function (_$scope_, _$log_, _$loc_, _$timeout_, _$cookies_,
592 _fs_, mast, _ks_, _zs_,
593 _gs_, _ms_, _sus_, _flash_,
594 _wss_, _ps_, _th_,
Thomas Vachuska0af26912016-03-21 21:37:30 -0700595 _tds_, _t3s_, _tes_,
Simon Hunt5989ddf2017-08-02 20:38:12 -0700596 _tfs_, _tps_, _tis_,
597 _tss_, _tls_, _tts_,
598 _tos_, _fltr_, _ttbs_,
599 _tms_, _tspr_, _ttip_,
600 _tov_, lion) {
Simon Huntfc5c5842017-02-01 23:32:18 -0800601
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700602 var params = _$loc_.search(),
Simon Huntfc5c5842017-02-01 23:32:18 -0800603 selOverlay = params.overlayId,
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700604 projection,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800605 dim,
Simon Hunt1894d792015-02-04 17:09:20 -0800606 uplink = {
607 // provides function calls back into this space
608 showNoDevs: showNoDevs,
609 projection: function () { return projection; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800610 zoomLayer: function () { return zoomLayer; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800611 zoomer: function () { return zoomer; },
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700612 opacifyMap: opacifyMap,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000613 topoStartDone: topoStartDone
Simon Huntac4c6f72015-02-03 19:50:53 -0800614 };
615
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700616 $scope = _$scope_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800617 $log = _$log_;
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700618 $loc = _$loc_;
Simon Hunt7faabd52016-08-18 16:16:19 -0700619 $timeout = _$timeout_;
Simon Hunt5c3ed732017-07-20 19:03:28 +0000620 $cookies = _$cookies_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800621 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800622 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800623 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800624 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800625 ms = _ms_;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800626 sus = _sus_;
Simon Hunt0e48c292015-02-19 16:11:37 -0800627 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700628 wss = _wss_;
Simon Huntc7ae7952015-04-08 18:59:27 -0700629 ps = _ps_;
Simon Hunta34fcb52016-02-25 16:27:32 -0800630 th = _th_;
Simon Hunta211f7f2015-11-09 12:48:23 -0800631 tds = _tds_;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700632 t3s = _t3s_;
Simon Huntc252aa62015-02-10 16:45:39 -0800633 tes = _tes_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800634 tfs = _tfs_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800635 // TODO: consider funnelling actions through TopoForceService...
636 // rather than injecting references to these 'sub-modules',
637 // just so we can invoke functions on them.
Simon Huntc252aa62015-02-10 16:45:39 -0800638 tps = _tps_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800639 tis = _tis_;
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700640 tms = _tms_;
Simon Hunt5c3ed732017-07-20 19:03:28 +0000641 tss = _tss_;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700642 tls = _tls_;
Simon Hunt5c3ed732017-07-20 19:03:28 +0000643 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800644 tos = _tos_;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700645 fltr = _fltr_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700646 ttbs = _ttbs_;
Simon Hunta4281242016-02-26 20:18:45 -0800647 tspr = _tspr_;
Simon Hunt5c3ed732017-07-20 19:03:28 +0000648 ttip = _ttip_;
Simon Hunt72e44bf2015-07-21 21:34:20 -0700649 tov = _tov_;
Simon Huntef31fb22014-12-19 13:16:44 -0800650
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700651 tms.start({
652 toggleMap: toggleMap,
653 currentMap: currentMap,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000654 setMap: setMap
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700655 });
656
Simon Hunt441c9ae2017-02-03 18:22:31 -0800657 // pull intent data from the query string...
Viswanath KSP0f297702016-08-13 18:02:43 +0530658 if (params.key && params.appId && params.appName) {
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700659 $scope.intentData = {
Viswanath KSP0f297702016-08-13 18:02:43 +0530660 key: params.key,
661 appId: params.appId,
Simon Hunt441c9ae2017-02-03 18:22:31 -0800662 appName: params.appName,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000663 intentType: params.intentType
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700664 };
665 }
666
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700667 $scope.notifyResize = function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800668 svgResized(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800669 };
Simon Huntef31fb22014-12-19 13:16:44 -0800670
Simon Hunt54442fa2015-01-26 14:17:38 -0800671 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800672 $scope.$on('$destroy', function () {
673 $log.log('OvTopoCtrl is saying Buh-Bye!');
Thomas Vachuska329af532015-03-10 02:08:33 -0700674 tes.stop();
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700675 tms.stop();
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700676 ks.unbindKeys();
Simon Hunt626d2102015-01-29 11:54:50 -0800677 tps.destroyPanels();
Simon Hunta211f7f2015-11-09 12:48:23 -0800678 tds.closeDialog();
Simon Hunt4b668592015-01-29 17:33:53 -0800679 tis.destroyInst();
Simon Hunt3a6eec02015-02-09 21:16:43 -0800680 tfs.destroyForce();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700681 ttbs.destroyToolbar();
Simon Hunta34fcb52016-02-25 16:27:32 -0800682 mapShader(false);
Simon Hunt584122a2015-01-21 15:32:40 -0800683 });
684
Simon Huntcacce342015-01-07 16:13:05 -0800685 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800686 ovtopo = d3.select('#ov-topo');
687 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800688 // set the svg size to match that of the window, less the masthead
689 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt3a6eec02015-02-09 21:16:43 -0800690 dim = [svg.attr('width'), svg.attr('height')];
Simon Hunt426bd862015-01-14 16:48:41 -0800691
Simon Hunt6cc53692015-01-07 11:33:45 -0800692 setUpKeys();
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700693 setUpToolbar();
Simon Hunt6cc53692015-01-07 11:33:45 -0800694 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800695 setUpZoom();
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800696 setUpNoDevs();
Simon Hunt90053ed2016-04-06 12:49:02 -0700697 setUpMap().then(
Simon Hunt1894d792015-02-04 17:09:20 -0800698 function (proj) {
Simon Hunt5c3ed732017-07-20 19:03:28 +0000699 var z = ps.getPrefs('topo_zoom', { tx:0, ty:0, sc:1 });
Simon Huntbb5e0d82015-04-16 14:25:46 -0700700 zoomer.panZoom([z.tx, z.ty], z.sc);
701 $log.debug('** Zoom restored:', z);
702
Simon Hunt1894d792015-02-04 17:09:20 -0800703 projection = proj;
Simon Huntbb5e0d82015-04-16 14:25:46 -0700704 $log.debug('** We installed the projection:', proj);
Simon Huntf41f3092015-04-16 10:33:26 -0700705 flash.enable(false);
Simon Huntc7ae7952015-04-08 18:59:27 -0700706 toggleMap(prefsState.bg);
Simon Huntf41f3092015-04-16 10:33:26 -0700707 flash.enable(true);
Simon Hunta34fcb52016-02-25 16:27:32 -0800708 mapShader(true);
Simon Hunte1ce4292015-06-12 13:07:02 -0700709
Simon Hunt5989ddf2017-08-02 20:38:12 -0700710 // piggyback off the deferred map loading to load the
711 // localization bundle after the uber bundle has arrived...
712 $scope.lion = lion.bundle('core.view.Topo');
713 topoLion = $scope.lion;
714 $log.debug('Loaded Topo LION Bundle:', topoLion);
715
Simon Hunte1ce4292015-06-12 13:07:02 -0700716 // now we have the map projection, we are ready for
717 // the server to send us device/host data...
718 tes.start();
719 // need to do the following so we immediately get
720 // the summary panel data back from the server
721 restoreSummaryFromPrefs();
Simon Hunt1894d792015-02-04 17:09:20 -0800722 }
723 );
Simon Hunta4281242016-02-26 20:18:45 -0800724 tes.bindHandlers();
Simon Hunt90053ed2016-04-06 12:49:02 -0700725 setUpSprites();
Simon Huntfd1231a2015-01-26 22:14:51 -0800726
Simon Hunt1894d792015-02-04 17:09:20 -0800727 forceG = zoomLayer.append('g').attr('id', 'topo-force');
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800728 tfs.initForce(svg, forceG, uplink, dim);
Simon Hunta142dd22015-02-12 22:07:51 -0800729 tis.initInst({ showMastership: tfs.showMastership });
Simon Hunt237676b52015-03-10 19:04:26 -0700730 tps.initPanels();
Simon Hunt6cc53692015-01-07 11:33:45 -0800731
Simon Huntc7ae7952015-04-08 18:59:27 -0700732 restoreConfigFromPrefs();
Simon Huntfc5c5842017-02-01 23:32:18 -0800733
734 ttbs.selectOverlay(selOverlay || prefsState.ovid);
Simon Hunt36a58c62015-04-08 11:00:07 -0700735
Simon Hunt72e44bf2015-07-21 21:34:20 -0700736 $log.debug('registered overlays...', tov.list());
Simon Hunt6cc53692015-01-07 11:33:45 -0800737 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800738 }]);
739}());