blob: 9b532f60ec8360abc36599dd46cbf3022334d032 [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
Simon Hunta34fcb52016-02-25 16:27:32 -080032 var $scope, $log, $cookies, fs, ks, zs, gs, ms, sus, flash, wss, ps, th,
Simon Hunta4281242016-02-26 20:18:45 -080033 tds, tes, tfs, tps, tis, tss, tls, tts, tos, fltr, ttbs, tspr, ttip, tov;
Simon Hunt6cc53692015-01-07 11:33:45 -080034
35 // DOM elements
Simon Hunt2052e5d2015-04-13 17:40:44 -070036 var ovtopo, svg, defs, zoomLayer, mapG, spriteG, forceG, noDevsLayer;
Simon Hunt6cc53692015-01-07 11:33:45 -080037
38 // Internal state
Simon Hunta34fcb52016-02-25 16:27:32 -080039 var zoomer, actionMap, themeListener;
Simon Hunt6cc53692015-01-07 11:33:45 -080040
Simon Huntcacce342015-01-07 16:13:05 -080041 // --- Short Cut Keys ------------------------------------------------
42
Simon Hunt8d22c4b2015-08-06 16:24:43 -070043 function setUpKeys(overlayKeys) {
Simon Hunt5724fb42015-02-05 16:59:40 -080044 // key bindings need to be made after the services have been injected
45 // thus, deferred to here...
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070046 actionMap = {
Simon Hunt36a58c62015-04-08 11:00:07 -070047 I: [toggleInstances, 'Toggle ONOS instances panel'],
Simon Huntee7a3ce2015-04-09 13:28:37 -070048 O: [toggleSummary, 'Toggle ONOS summary panel'],
Simon Hunt239e5882015-04-23 15:07:04 -070049 D: [toggleUseDetailsFlag, 'Disable / enable details panel'],
Simon Huntac4c6f72015-02-03 19:50:53 -080050
Simon Huntfcbde892015-04-16 12:05:28 -070051 H: [toggleHosts, 'Toggle host visibility'],
52 M: [toggleOffline, 'Toggle offline visibility'],
53 P: [togglePorts, 'Toggle Port Highlighting'],
Simon Hunt86b7c882015-04-02 23:06:08 -070054 dash: [tfs.showBadLinks, 'Show bad links'],
55 B: [toggleMap, 'Toggle background map'],
Simon Hunt2052e5d2015-04-13 17:40:44 -070056 S: [toggleSprites, 'Toggle sprite layer'],
Simon Huntac4c6f72015-02-03 19:50:53 -080057
Simon Huntfd7106c2016-02-09 15:05:26 -080058 X: [tfs.resetAllLocations, 'Reset node locations'],
Simon Hunt96f88c62015-02-19 17:57:25 -080059 Z: [tos.toggleOblique, 'Toggle oblique view (Experimental)'],
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070060 N: [fltr.clickAction, 'Cycle node layers'],
Simon Hunt5724fb42015-02-05 16:59:40 -080061 L: [tfs.cycleDeviceLabels, 'Cycle device labels'],
Simon Hunt445e8152015-02-06 13:00:12 -080062 U: [tfs.unpin, 'Unpin node (hover mouse over)'],
Simon Hunt5724fb42015-02-05 16:59:40 -080063 R: [resetZoom, 'Reset pan / zoom'],
Simon Hunt90dcc3e2015-03-25 15:01:27 -070064 dot: [ttbs.toggleToolbar, 'Toggle Toolbar'],
Simon Huntac4c6f72015-02-03 19:50:53 -080065
Simon Hunt0e48c292015-02-19 16:11:37 -080066 E: [equalizeMasters, 'Equalize mastership roles'],
Simon Huntac4c6f72015-02-03 19:50:53 -080067
Simon Hunt5b024d72016-01-29 11:02:43 -080068 // topology overlay selections
69 F1: function () { ttbs.fnkey(0); },
70 F2: function () { ttbs.fnkey(1); },
71 F3: function () { ttbs.fnkey(2); },
72 F4: function () { ttbs.fnkey(3); },
73 F5: function () { ttbs.fnkey(4); },
74
Simon Hunt08f841d02015-02-10 14:39:20 -080075 esc: handleEscape,
Simon Huntac4c6f72015-02-03 19:50:53 -080076
Simon Hunt09060142015-03-18 20:23:32 -070077 _keyListener: ttbs.keyListener,
78
Simon Hunt5724fb42015-02-05 16:59:40 -080079 _helpFormat: [
Simon Hunt8d22c4b2015-08-06 16:24:43 -070080 ['I', 'O', 'D', 'H', 'M', 'P', 'dash', 'B', 'S' ],
81 ['X', 'Z', 'N', 'L', 'U', 'R', '-', 'E', '-', 'dot'],
82 [] // this column reserved for overlay actions
Simon Hunt5724fb42015-02-05 16:59:40 -080083 ]
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070084 };
85
Simon Hunt8d22c4b2015-08-06 16:24:43 -070086 if (fs.isO(overlayKeys)) {
87 mergeKeys(overlayKeys);
88 }
89
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070090 ks.keyBindings(actionMap);
Simon Huntac4c6f72015-02-03 19:50:53 -080091
Simon Hunt639dc662015-02-18 14:19:20 -080092 ks.gestureNotes([
Simon Hunt5724fb42015-02-05 16:59:40 -080093 ['click', 'Select the item and show details'],
94 ['shift-click', 'Toggle selection state'],
95 ['drag', 'Reposition (and pin) device / host'],
96 ['cmd-scroll', 'Zoom in / out'],
97 ['cmd-drag', 'Pan']
Simon Hunt639dc662015-02-18 14:19:20 -080098 ]);
Simon Hunt5724fb42015-02-05 16:59:40 -080099 }
Simon Hunt6cc53692015-01-07 11:33:45 -0800100
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700101 // when a topology overlay is activated, we need to bind their keystrokes
102 // and include them in the quick-help panel
103 function mergeKeys(extra) {
104 var _hf = actionMap._helpFormat[2];
Simon Hunt71892222015-09-29 13:39:40 -0700105
106 ks.checkNotGlobal(extra);
107
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700108 extra._keyOrder.forEach(function (k) {
109 var d = extra[k],
110 cb = d && d.cb,
111 tt = d && d.tt;
112 // NOTE: ignore keys that are already defined
113 if (d && !actionMap[k]) {
114 actionMap[k] = [cb, tt];
115 _hf.push(k);
116 }
117 });
118 }
119
Simon Hunt08f841d02015-02-10 14:39:20 -0800120 // --- Keystroke functions -------------------------------------------
Simon Huntac4c6f72015-02-03 19:50:53 -0800121
Simon Hunt36a58c62015-04-08 11:00:07 -0700122 function toggleInstances(x) {
Simon Huntee7a3ce2015-04-09 13:28:37 -0700123 updatePrefsState('insts', tis.toggle(x));
Simon Huntac4c6f72015-02-03 19:50:53 -0800124 tfs.updateDeviceColors();
Simon Hunt6cc53692015-01-07 11:33:45 -0800125 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800126
Simon Huntee7a3ce2015-04-09 13:28:37 -0700127 function toggleSummary(x) {
128 updatePrefsState('summary', tps.toggleSummary(x));
Simon Hunt18bf9822015-02-12 17:35:45 -0800129 }
130
Simon Hunt239e5882015-04-23 15:07:04 -0700131 function toggleUseDetailsFlag(x) {
132 updatePrefsState('detail', tps.toggleUseDetailsFlag(x));
Simon Huntee7a3ce2015-04-09 13:28:37 -0700133 }
134
Simon Huntfcbde892015-04-16 12:05:28 -0700135 function toggleHosts(x) {
136 updatePrefsState('hosts', tfs.toggleHosts(x));
137 }
138
139 function toggleOffline(x) {
140 updatePrefsState('offdev', tfs.toggleOffline(x));
141 }
142
143 function togglePorts(x) {
144 updatePrefsState('porthl', tfs.togglePorts(x));
145 }
146
147 function _togSvgLayer(x, G, tag, what) {
148 var on = (x === 'keyev') ? !sus.visible(G) : !!x,
Simon Huntee7a3ce2015-04-09 13:28:37 -0700149 verb = on ? 'Show' : 'Hide';
Simon Huntfcbde892015-04-16 12:05:28 -0700150 sus.visible(G, on);
151 updatePrefsState(tag, on);
152 flash.flash(verb + ' ' + what);
153 }
154
155 function toggleMap(x) {
156 _togSvgLayer(x, mapG, 'bg', 'background map');
Simon Huntee7a3ce2015-04-09 13:28:37 -0700157 }
Simon Hunt36a58c62015-04-08 11:00:07 -0700158
Simon Hunt2052e5d2015-04-13 17:40:44 -0700159 function toggleSprites(x) {
Simon Huntfcbde892015-04-16 12:05:28 -0700160 _togSvgLayer(x, spriteG, 'spr', 'sprite layer');
Simon Hunt2052e5d2015-04-13 17:40:44 -0700161 }
162
Simon Hunt08f841d02015-02-10 14:39:20 -0800163 function resetZoom() {
164 zoomer.reset();
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700165 flash.flash('Pan and zoom reset');
Simon Hunt08f841d02015-02-10 14:39:20 -0800166 }
167
Simon Hunt0e48c292015-02-19 16:11:37 -0800168 function equalizeMasters() {
Simon Hunt237676b52015-03-10 19:04:26 -0700169 wss.sendEvent('equalizeMasters');
Simon Hunt0e48c292015-02-19 16:11:37 -0800170 flash.flash('Equalizing master roles');
171 }
172
Simon Hunt08f841d02015-02-10 14:39:20 -0800173 function handleEscape() {
Simon Hunta142dd22015-02-12 22:07:51 -0800174 if (tis.showMaster()) {
175 // if an instance is selected, cancel the affinity mapping
176 tis.cancelAffinity()
Simon Hunt08f841d02015-02-10 14:39:20 -0800177
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700178 } else if (tov.hooks.escape()) {
179 // else if the overlay consumed the ESC event...
180 // (work already done)
181
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700182 } else if (tss.deselectAll()) {
Simon Hunta0eb0a82015-02-11 12:30:06 -0800183 // else if we have node selections, deselect them all
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700184 // (work already done)
185
186 } else if (tls.deselectLink()) {
187 // else if we have a link selected, deselect it
188 // (work already done)
Simon Hunt08f841d02015-02-10 14:39:20 -0800189
Simon Hunta0eb0a82015-02-11 12:30:06 -0800190 } else if (tis.isVisible()) {
191 // else if the Instance Panel is visible, hide it
192 tis.hide();
193 tfs.updateDeviceColors();
Simon Hunt08f841d02015-02-10 14:39:20 -0800194
Simon Hunta0eb0a82015-02-11 12:30:06 -0800195 } else if (tps.summaryVisible()) {
196 // else if the Summary Panel is visible, hide it
197 tps.hideSummaryPanel();
Simon Hunta0eb0a82015-02-11 12:30:06 -0800198 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800199 }
Simon Huntcacce342015-01-07 16:13:05 -0800200
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700201 // --- Toolbar Functions ---------------------------------------------
202
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700203 function notValid(what) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700204 $log.warn('topo.js getActionEntry(): Not a valid ' + what);
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700205 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700206
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700207 function getActionEntry(key) {
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700208 var entry;
209
210 if (!key) {
211 notValid('key');
212 return null;
213 }
214
215 entry = actionMap[key];
216
217 if (!entry) {
218 notValid('actionMap entry');
219 return null;
220 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700221 return fs.isA(entry) || [entry, ''];
222 }
223
224 function setUpToolbar() {
225 ttbs.init({
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700226 getActionEntry: getActionEntry,
227 setUpKeys: setUpKeys
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700228 });
229 ttbs.createToolbar();
230 }
231
Simon Huntcacce342015-01-07 16:13:05 -0800232 // --- Glyphs, Icons, and the like -----------------------------------
233
Simon Hunt6cc53692015-01-07 11:33:45 -0800234 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -0800235 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -0800236 gs.loadDefs(defs);
Simon Hunt0ee28682015-02-12 20:48:11 -0800237 sus.loadGlowDefs(defs);
Simon Hunt6cc53692015-01-07 11:33:45 -0800238 }
239
240
Simon Huntcacce342015-01-07 16:13:05 -0800241 // --- Pan and Zoom --------------------------------------------------
242
243 // zoom enabled predicate. ev is a D3 source event.
244 function zoomEnabled(ev) {
Simon Huntd552ee92015-04-02 17:06:35 -0700245 return fs.isMobile() || (ev.metaKey || ev.altKey);
Simon Huntcacce342015-01-07 16:13:05 -0800246 }
247
248 function zoomCallback() {
Simon Huntbb5e0d82015-04-16 14:25:46 -0700249 var sc = zoomer.scale(),
250 tr = zoomer.translate();
251
252 ps.setPrefs('topo_zoom', {tx:tr[0], ty:tr[1], sc:sc});
Simon Huntcacce342015-01-07 16:13:05 -0800253
Simon Hunt0541fb82015-01-14 18:59:57 -0800254 // keep the map lines constant width while zooming
Simon Hunt737c89f2015-01-28 12:23:19 -0800255 mapG.style('stroke-width', (2.0 / sc) + 'px');
Simon Huntcacce342015-01-07 16:13:05 -0800256 }
257
258 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800259 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -0800260 zoomer = zs.createZoomer({
261 svg: svg,
262 zoomLayer: zoomLayer,
263 zoomEnabled: zoomEnabled,
264 zoomCallback: zoomCallback
265 });
266 }
267
268
Simon Hunt0541fb82015-01-14 18:59:57 -0800269 // callback invoked when the SVG view has been resized..
Simon Hunt3a6eec02015-02-09 21:16:43 -0800270 function svgResized(s) {
271 tfs.newDim([s.width, s.height]);
Simon Hunt0541fb82015-01-14 18:59:57 -0800272 }
273
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800274 // --- Background Map ------------------------------------------------
275
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800276 function setUpNoDevs() {
277 var g, box;
278 noDevsLayer = svg.append('g').attr({
279 id: 'topo-noDevsLayer',
280 transform: sus.translate(500,500)
281 });
282 // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html.
283 // We are translating this layer to have its origin at the center
284
285 g = noDevsLayer.append('g');
286 gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird');
287 g.append('text').text('No devices are connected')
288 .attr({ x: 120, y: 80});
289
290 box = g.node().getBBox();
291 box.x -= box.width/2;
292 box.y -= box.height/2;
293 g.attr('transform', sus.translate(box.x, box.y));
294
295 showNoDevs(true);
296 }
297
298 function showNoDevs(b) {
Simon Hunt18bf9822015-02-12 17:35:45 -0800299 sus.visible(noDevsLayer, b);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800300 }
301
Simon Hunt2362b072015-06-11 20:08:22 -0700302
303 var countryFilters = {
304 world: function (c) {
305 return c.properties.continent !== 'Antarctica';
306 },
307
308 // NOTE: for "usa" we are using our hand-crafted topojson file
309
310 s_america: function (c) {
311 return c.properties.continent === 'South America';
312 },
313
Simon Huntac099e82015-12-03 09:17:47 -0800314 ns_america: function (c) {
315 return c.properties.custom === 'US-cont' ||
316 c.properties.subregion === 'Central America' ||
Simon Huntd4712622015-12-02 11:19:50 -0800317 c.properties.continent === 'South America';
318 },
319
Simon Hunt2362b072015-06-11 20:08:22 -0700320 japan: function (c) {
321 return c.properties.geounit === 'Japan';
322 },
323
324 europe: function (c) {
325 return c.properties.continent === 'Europe';
326 },
327
328 italy: function (c) {
329 return c.properties.geounit === 'Italy';
330 },
331
332 uk: function (c) {
333 // technically, Ireland is not part of the United Kingdom,
334 // but the map looks weird without it showing.
335 return c.properties.adm0_a3 === 'GBR' ||
336 c.properties.adm0_a3 === 'IRL';
Simon Hunt1178d5b2015-09-02 17:02:37 -0700337 },
338
339 s_korea: function (c) {
340 return c.properties.adm0_a3 === 'KOR';
341 },
342
343 australia: function (c) {
344 return c.properties.adm0_a3 === 'AUS';
Simon Hunt2362b072015-06-11 20:08:22 -0700345 }
346 };
347
Simon Hunta34fcb52016-02-25 16:27:32 -0800348 var tintOn = 0,
349 shadeFlip = 0,
350 shadePalette = {
351 light: {
352 sea: 'aliceblue',
353 land: 'white',
354 outline: '#ddd'
355 },
356 dark: {
357 sea: '#001830',
358 land: '#232331',
359 outline: '#3a3a3a'
360 }
361 };
362
363 function shading() {
364 return tintOn ? {
365 palette: shadePalette[th.theme()],
366 flip: shadeFlip
367 } : '';
368 }
Simon Hunt2362b072015-06-11 20:08:22 -0700369
370 function setUpMap($loc) {
Simon Huntfacad992016-02-25 09:58:33 -0800371 var qp = $loc.search(),
372 pr = ps.getPrefs('topo_mapid'),
373 mi1 = qp.mapid,
374 mi2 = pr && pr.id,
375 mapId = mi1 || mi2 || 'usa',
376 ms1 = qp.mapscale,
377 ms2 = pr && pr.scale,
378 mapScale = ms1 || ms2 || 1,
Simon Hunta34fcb52016-02-25 16:27:32 -0800379 t1 = qp.tint,
380 t2 = pr && pr.tint,
381 tint = t1 || t2 || 'off',
Simon Hunt2362b072015-06-11 20:08:22 -0700382 promise,
Simon Huntfacad992016-02-25 09:58:33 -0800383 cfilter;
Simon Hunt2362b072015-06-11 20:08:22 -0700384
Simon Hunta34fcb52016-02-25 16:27:32 -0800385 tintOn = tint === 'on' ? 1 : 0;
386
Simon Hunt18491362016-03-04 11:09:42 -0800387 $log.debug('setUpMap() mapId:', mapId, ', mapScale:', mapScale,
388 ', tint:', tint);
389
Simon Hunt737c89f2015-01-28 12:23:19 -0800390 mapG = zoomLayer.append('g').attr('id', 'topo-map');
Simon Hunt2362b072015-06-11 20:08:22 -0700391 if (mapId === 'usa') {
Simon Hunta34fcb52016-02-25 16:27:32 -0800392 shadeFlip = 0;
393 promise = ms.loadMapInto(mapG, '*continental_us', {
394 adjustScale: mapScale,
395 shading: shading()
396 });
Simon Hunte1bef6e2016-02-24 22:16:04 -0800397 } else if (mapId === 'bayarea') {
Simon Hunta34fcb52016-02-25 16:27:32 -0800398 shadeFlip = 1;
Simon Huntfacad992016-02-25 09:58:33 -0800399 promise = ms.loadMapInto(mapG, '*bayarea', {
400 objectTag: 'bayareaGEO',
401 adjustScale: mapScale,
Simon Hunta34fcb52016-02-25 16:27:32 -0800402 shading: shading()
Simon Huntfacad992016-02-25 09:58:33 -0800403 });
zylin3077df52016-03-04 10:40:17 +0800404 } else if (mapId === 'taiwan') {
405 shadeFlip = 0;
406 promise = ms.loadMapInto(mapG, '*taiwan', {
407 objectTag: 'taiwan',
408 adjustScale: mapScale,
409 shading: shading()
410 })
Simon Hunt2362b072015-06-11 20:08:22 -0700411 } else {
Simon Hunta34fcb52016-02-25 16:27:32 -0800412 shadeFlip = 0;
Simon Hunt2362b072015-06-11 20:08:22 -0700413 cfilter = countryFilters[mapId] || countryFilters.world;
Simon Hunta34fcb52016-02-25 16:27:32 -0800414 promise = ms.loadMapRegionInto(mapG, {
415 countryFilter: cfilter,
416 adjustScale: mapScale,
417 shading: shading()
418 });
Simon Hunt2362b072015-06-11 20:08:22 -0700419 }
Simon Hunta34fcb52016-02-25 16:27:32 -0800420 ps.setPrefs('topo_mapid', { id: mapId, scale: mapScale, tint: tint });
Simon Hunt2362b072015-06-11 20:08:22 -0700421 return promise;
Simon Hunt0541fb82015-01-14 18:59:57 -0800422 }
423
Simon Hunta34fcb52016-02-25 16:27:32 -0800424 // set up theme listener to re-shade the map when required.
425 function mapShader(on) {
426 if (on) {
427 themeListener = th.addListener(function () {
428 ms.reshade(shading());
429 });
430 } else {
431 th.removeListener(themeListener);
432 themeListener = null;
433 }
434 }
435
Simon Huntc3c5b672015-02-20 11:32:13 -0800436 function opacifyMap(b) {
437 mapG.transition()
438 .duration(1000)
439 .attr('opacity', b ? 1 : 0);
440 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800441
Simon Huntf9fc0e72015-04-16 15:10:57 -0700442 function setUpSprites($loc, tspr) {
443 var s1 = $loc.search().sprites,
444 s2 = ps.getPrefs('topo_sprites'),
445 sprId = s1 || (s2 && s2.id);
446
447 spriteG = zoomLayer.append ('g').attr('id', 'topo-sprites');
448 if (sprId) {
449 ps.setPrefs('topo_sprites', {id:sprId});
450 tspr.loadSprites(spriteG, defs, sprId);
451 }
452 }
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700453
Simon Huntc7ae7952015-04-08 18:59:27 -0700454 // --- User Preferemces ----------------------------------------------
Simon Hunt36a58c62015-04-08 11:00:07 -0700455
Simon Huntc7ae7952015-04-08 18:59:27 -0700456 var prefsState = {};
Simon Hunt36a58c62015-04-08 11:00:07 -0700457
Simon Huntc7ae7952015-04-08 18:59:27 -0700458 function updatePrefsState(what, b) {
459 prefsState[what] = b ? 1 : 0;
460 ps.setPrefs('topo_prefs', prefsState);
Simon Hunt36a58c62015-04-08 11:00:07 -0700461 }
462
Simon Hunt36a58c62015-04-08 11:00:07 -0700463
Simon Huntc7ae7952015-04-08 18:59:27 -0700464 function restoreConfigFromPrefs() {
465 // NOTE: toolbar will have set this for us..
Simon Huntfcbde892015-04-16 12:05:28 -0700466 prefsState = ps.asNumbers(ps.getPrefs('topo_prefs'));
Simon Hunt36a58c62015-04-08 11:00:07 -0700467
Simon Hunte1ce4292015-06-12 13:07:02 -0700468 $log.debug('TOPO- Prefs State:', prefsState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700469
Simon Huntf41f3092015-04-16 10:33:26 -0700470 flash.enable(false);
Simon Huntc7ae7952015-04-08 18:59:27 -0700471 toggleInstances(prefsState.insts);
Simon Huntee7a3ce2015-04-09 13:28:37 -0700472 toggleSummary(prefsState.summary);
Simon Hunt239e5882015-04-23 15:07:04 -0700473 toggleUseDetailsFlag(prefsState.detail);
Bri Prebilic Cole9b80ca02015-08-03 11:26:54 -0700474 toggleHosts(prefsState.hosts);
475 toggleOffline(prefsState.offdev);
476 togglePorts(prefsState.porthl);
477 toggleMap(prefsState.bg);
Simon Huntf9fc0e72015-04-16 15:10:57 -0700478 toggleSprites(prefsState.spr);
Simon Huntf41f3092015-04-16 10:33:26 -0700479 flash.enable(true);
Simon Hunt36a58c62015-04-08 11:00:07 -0700480 }
481
482
Simon Hunte1ce4292015-06-12 13:07:02 -0700483 // somewhat hackish, because summary update cannot happen until we
484 // have opened the websocket to the server; hence this extra function
485 // invoked after tes.start()
486 function restoreSummaryFromPrefs() {
487 prefsState = ps.asNumbers(ps.getPrefs('topo_prefs'));
488 $log.debug('TOPO- Prefs SUMMARY State:', prefsState.summary);
489
490 flash.enable(false);
491 toggleSummary(prefsState.summary);
492 flash.enable(true);
493 }
494
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700495 function topoStartDone() {
496 var d = $scope.intentData;
497 if (d) {
498 tts.selectIntent(d);
499 }
500 }
Simon Hunte1ce4292015-06-12 13:07:02 -0700501
Simon Huntcacce342015-01-07 16:13:05 -0800502 // --- Controller Definition -----------------------------------------
503
Simon Hunt6cc53692015-01-07 11:33:45 -0800504 angular.module('ovTopo', moduleDependencies)
Simon Hunt237676b52015-03-10 19:04:26 -0700505 .controller('OvTopoCtrl', ['$scope', '$log', '$location', '$timeout',
Simon Hunt36a58c62015-04-08 11:00:07 -0700506 '$cookies', 'FnService', 'MastService', 'KeyService', 'ZoomService',
Simon Hunt0e48c292015-02-19 16:11:37 -0800507 'GlyphService', 'MapService', 'SvgUtilService', 'FlashService',
Simon Hunta34fcb52016-02-25 16:27:32 -0800508 'WebSocketService', 'PrefsService', 'ThemeService',
509 'TopoDialogService',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800510 'TopoEventService', 'TopoForceService', 'TopoPanelService',
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700511 'TopoInstService', 'TopoSelectService', 'TopoLinkService',
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700512 'TopoTrafficService', 'TopoObliqueService', 'TopoFilterService',
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700513 'TopoToolbarService', 'TopoSpriteService', 'TooltipService',
Simon Hunt72e44bf2015-07-21 21:34:20 -0700514 'TopoOverlayService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800515
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700516 function (_$scope_, _$log_, $loc, $timeout, _$cookies_, _fs_, mast, _ks_,
Simon Hunta34fcb52016-02-25 16:27:32 -0800517 _zs_, _gs_, _ms_, _sus_, _flash_, _wss_, _ps_, _th_,
518 _tds_, _tes_,
Simon Hunta211f7f2015-11-09 12:48:23 -0800519 _tfs_, _tps_, _tis_, _tss_, _tls_, _tts_, _tos_, _fltr_,
Simon Hunta4281242016-02-26 20:18:45 -0800520 _ttbs_, _tspr_, _ttip_, _tov_) {
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700521 var params = $loc.search(),
522 projection,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800523 dim,
Simon Hunt1894d792015-02-04 17:09:20 -0800524 uplink = {
525 // provides function calls back into this space
526 showNoDevs: showNoDevs,
527 projection: function () { return projection; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800528 zoomLayer: function () { return zoomLayer; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800529 zoomer: function () { return zoomer; },
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700530 opacifyMap: opacifyMap,
531 topoStartDone: topoStartDone
Simon Huntac4c6f72015-02-03 19:50:53 -0800532 };
533
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700534 $scope = _$scope_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800535 $log = _$log_;
Simon Hunt36a58c62015-04-08 11:00:07 -0700536 $cookies = _$cookies_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800537 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800538 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800539 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800540 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800541 ms = _ms_;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800542 sus = _sus_;
Simon Hunt0e48c292015-02-19 16:11:37 -0800543 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700544 wss = _wss_;
Simon Huntc7ae7952015-04-08 18:59:27 -0700545 ps = _ps_;
Simon Hunta34fcb52016-02-25 16:27:32 -0800546 th = _th_;
Simon Hunta211f7f2015-11-09 12:48:23 -0800547 tds = _tds_;
Simon Huntc252aa62015-02-10 16:45:39 -0800548 tes = _tes_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800549 tfs = _tfs_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800550 // TODO: consider funnelling actions through TopoForceService...
551 // rather than injecting references to these 'sub-modules',
552 // just so we can invoke functions on them.
Simon Huntc252aa62015-02-10 16:45:39 -0800553 tps = _tps_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800554 tis = _tis_;
Simon Hunt6036b192015-02-11 11:20:26 -0800555 tss = _tss_;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700556 tls = _tls_;
Simon Huntf542d842015-02-11 16:20:33 -0800557 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800558 tos = _tos_;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700559 fltr = _fltr_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700560 ttbs = _ttbs_;
Simon Hunta4281242016-02-26 20:18:45 -0800561 tspr = _tspr_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700562 ttip = _ttip_;
Simon Hunt72e44bf2015-07-21 21:34:20 -0700563 tov = _tov_;
Simon Huntef31fb22014-12-19 13:16:44 -0800564
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700565 if (params.intentKey && params.intentAppId && params.intentAppName) {
566 $scope.intentData = {
567 key: params.intentKey,
568 appId: params.intentAppId,
569 appName: params.intentAppName
570 };
571 }
572
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700573 $scope.notifyResize = function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800574 svgResized(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800575 };
Simon Huntef31fb22014-12-19 13:16:44 -0800576
Simon Hunt54442fa2015-01-26 14:17:38 -0800577 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800578 $scope.$on('$destroy', function () {
579 $log.log('OvTopoCtrl is saying Buh-Bye!');
Thomas Vachuska329af532015-03-10 02:08:33 -0700580 tes.stop();
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700581 ks.unbindKeys();
Simon Hunt626d2102015-01-29 11:54:50 -0800582 tps.destroyPanels();
Simon Hunta211f7f2015-11-09 12:48:23 -0800583 tds.closeDialog();
Simon Hunt4b668592015-01-29 17:33:53 -0800584 tis.destroyInst();
Simon Hunt3a6eec02015-02-09 21:16:43 -0800585 tfs.destroyForce();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700586 ttbs.destroyToolbar();
Simon Hunta34fcb52016-02-25 16:27:32 -0800587 mapShader(false);
Simon Hunt584122a2015-01-21 15:32:40 -0800588 });
589
Simon Huntcacce342015-01-07 16:13:05 -0800590 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800591 ovtopo = d3.select('#ov-topo');
592 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800593 // set the svg size to match that of the window, less the masthead
594 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt3a6eec02015-02-09 21:16:43 -0800595 dim = [svg.attr('width'), svg.attr('height')];
Simon Hunt426bd862015-01-14 16:48:41 -0800596
Simon Hunt6cc53692015-01-07 11:33:45 -0800597 setUpKeys();
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700598 setUpToolbar();
Simon Hunt6cc53692015-01-07 11:33:45 -0800599 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800600 setUpZoom();
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800601 setUpNoDevs();
Simon Hunt2362b072015-06-11 20:08:22 -0700602 setUpMap($loc).then(
Simon Hunt1894d792015-02-04 17:09:20 -0800603 function (proj) {
Simon Huntbb5e0d82015-04-16 14:25:46 -0700604 var z = ps.getPrefs('topo_zoom') || {tx:0, ty:0, sc:1};
605 zoomer.panZoom([z.tx, z.ty], z.sc);
606 $log.debug('** Zoom restored:', z);
607
Simon Hunt1894d792015-02-04 17:09:20 -0800608 projection = proj;
Simon Huntbb5e0d82015-04-16 14:25:46 -0700609 $log.debug('** We installed the projection:', proj);
Simon Huntf41f3092015-04-16 10:33:26 -0700610 flash.enable(false);
Simon Huntc7ae7952015-04-08 18:59:27 -0700611 toggleMap(prefsState.bg);
Simon Huntf41f3092015-04-16 10:33:26 -0700612 flash.enable(true);
Simon Hunta34fcb52016-02-25 16:27:32 -0800613 mapShader(true);
Simon Hunte1ce4292015-06-12 13:07:02 -0700614
615 // now we have the map projection, we are ready for
616 // the server to send us device/host data...
617 tes.start();
618 // need to do the following so we immediately get
619 // the summary panel data back from the server
620 restoreSummaryFromPrefs();
Simon Hunt1894d792015-02-04 17:09:20 -0800621 }
622 );
Simon Hunta4281242016-02-26 20:18:45 -0800623 tes.bindHandlers();
Simon Huntf9fc0e72015-04-16 15:10:57 -0700624 setUpSprites($loc, tspr);
Simon Huntfd1231a2015-01-26 22:14:51 -0800625
Simon Hunt1894d792015-02-04 17:09:20 -0800626 forceG = zoomLayer.append('g').attr('id', 'topo-force');
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800627 tfs.initForce(svg, forceG, uplink, dim);
Simon Hunta142dd22015-02-12 22:07:51 -0800628 tis.initInst({ showMastership: tfs.showMastership });
Simon Hunt237676b52015-03-10 19:04:26 -0700629 tps.initPanels();
Simon Hunt6cc53692015-01-07 11:33:45 -0800630
Simon Hunt36a58c62015-04-08 11:00:07 -0700631 // temporary solution for persisting user settings
Simon Huntc7ae7952015-04-08 18:59:27 -0700632 restoreConfigFromPrefs();
Simon Hunta5b53af2015-10-12 15:56:40 -0700633 ttbs.setDefaultOverlay();
Simon Hunt36a58c62015-04-08 11:00:07 -0700634
Simon Hunt72e44bf2015-07-21 21:34:20 -0700635 $log.debug('registered overlays...', tov.list());
Simon Hunt6cc53692015-01-07 11:33:45 -0800636 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800637 }]);
638}());