blob: b6c83ce8cc774eca8c546ae0d7725694c57f4d72 [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 Hunt8a119b02016-03-29 16:00:32 -070033 tds, t3s, tes, tfs, tps, tis, tss, tls, tts, tos, fltr, ttbs, tspr,
34 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'],
56 B: [toggleMap, 'Toggle background map'],
Simon Hunt2052e5d2015-04-13 17:40:44 -070057 S: [toggleSprites, 'Toggle sprite layer'],
Simon Huntac4c6f72015-02-03 19:50:53 -080058
Simon Huntfd7106c2016-02-09 15:05:26 -080059 X: [tfs.resetAllLocations, 'Reset node locations'],
Simon Hunt96f88c62015-02-19 17:57:25 -080060 Z: [tos.toggleOblique, 'Toggle oblique view (Experimental)'],
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070061 N: [fltr.clickAction, 'Cycle node layers'],
Simon Hunt5724fb42015-02-05 16:59:40 -080062 L: [tfs.cycleDeviceLabels, 'Cycle device labels'],
Simon Hunt445e8152015-02-06 13:00:12 -080063 U: [tfs.unpin, 'Unpin node (hover mouse over)'],
Simon Hunt5724fb42015-02-05 16:59:40 -080064 R: [resetZoom, 'Reset pan / zoom'],
Simon Hunt90dcc3e2015-03-25 15:01:27 -070065 dot: [ttbs.toggleToolbar, 'Toggle Toolbar'],
Simon Huntac4c6f72015-02-03 19:50:53 -080066
Simon Hunt0e48c292015-02-19 16:11:37 -080067 E: [equalizeMasters, 'Equalize mastership roles'],
Simon Huntac4c6f72015-02-03 19:50:53 -080068
Simon Hunt5b024d72016-01-29 11:02:43 -080069 // topology overlay selections
70 F1: function () { ttbs.fnkey(0); },
71 F2: function () { ttbs.fnkey(1); },
72 F3: function () { ttbs.fnkey(2); },
73 F4: function () { ttbs.fnkey(3); },
74 F5: function () { ttbs.fnkey(4); },
75
Simon Hunt08f841d02015-02-10 14:39:20 -080076 esc: handleEscape,
Simon Huntac4c6f72015-02-03 19:50:53 -080077
Simon Hunt09060142015-03-18 20:23:32 -070078 _keyListener: ttbs.keyListener,
79
Simon Hunt5724fb42015-02-05 16:59:40 -080080 _helpFormat: [
Simon Hunt8d22c4b2015-08-06 16:24:43 -070081 ['I', 'O', 'D', 'H', 'M', 'P', 'dash', 'B', 'S' ],
82 ['X', 'Z', 'N', 'L', 'U', 'R', '-', 'E', '-', 'dot'],
83 [] // this column reserved for overlay actions
Simon Hunt5724fb42015-02-05 16:59:40 -080084 ]
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070085 };
86
Simon Hunt8d22c4b2015-08-06 16:24:43 -070087 if (fs.isO(overlayKeys)) {
88 mergeKeys(overlayKeys);
89 }
90
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070091 ks.keyBindings(actionMap);
Simon Huntac4c6f72015-02-03 19:50:53 -080092
Simon Hunt639dc662015-02-18 14:19:20 -080093 ks.gestureNotes([
Simon Hunt5724fb42015-02-05 16:59:40 -080094 ['click', 'Select the item and show details'],
95 ['shift-click', 'Toggle selection state'],
96 ['drag', 'Reposition (and pin) device / host'],
97 ['cmd-scroll', 'Zoom in / out'],
98 ['cmd-drag', 'Pan']
Simon Hunt639dc662015-02-18 14:19:20 -080099 ]);
Simon Hunt5724fb42015-02-05 16:59:40 -0800100 }
Simon Hunt6cc53692015-01-07 11:33:45 -0800101
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700102 // when a topology overlay is activated, we need to bind their keystrokes
103 // and include them in the quick-help panel
104 function mergeKeys(extra) {
105 var _hf = actionMap._helpFormat[2];
Simon Hunt71892222015-09-29 13:39:40 -0700106
107 ks.checkNotGlobal(extra);
108
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700109 extra._keyOrder.forEach(function (k) {
110 var d = extra[k],
111 cb = d && d.cb,
112 tt = d && d.tt;
113 // NOTE: ignore keys that are already defined
114 if (d && !actionMap[k]) {
115 actionMap[k] = [cb, tt];
116 _hf.push(k);
117 }
118 });
119 }
120
Simon Hunt08f841d02015-02-10 14:39:20 -0800121 // --- Keystroke functions -------------------------------------------
Simon Huntac4c6f72015-02-03 19:50:53 -0800122
Simon Hunt36a58c62015-04-08 11:00:07 -0700123 function toggleInstances(x) {
Simon Huntee7a3ce2015-04-09 13:28:37 -0700124 updatePrefsState('insts', tis.toggle(x));
Simon Huntac4c6f72015-02-03 19:50:53 -0800125 tfs.updateDeviceColors();
Simon Hunt6cc53692015-01-07 11:33:45 -0800126 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800127
Simon Huntee7a3ce2015-04-09 13:28:37 -0700128 function toggleSummary(x) {
129 updatePrefsState('summary', tps.toggleSummary(x));
Simon Hunt18bf9822015-02-12 17:35:45 -0800130 }
131
Simon Hunt239e5882015-04-23 15:07:04 -0700132 function toggleUseDetailsFlag(x) {
133 updatePrefsState('detail', tps.toggleUseDetailsFlag(x));
Simon Huntee7a3ce2015-04-09 13:28:37 -0700134 }
135
Simon Huntfcbde892015-04-16 12:05:28 -0700136 function toggleHosts(x) {
137 updatePrefsState('hosts', tfs.toggleHosts(x));
138 }
139
140 function toggleOffline(x) {
141 updatePrefsState('offdev', tfs.toggleOffline(x));
142 }
143
144 function togglePorts(x) {
145 updatePrefsState('porthl', tfs.togglePorts(x));
146 }
147
148 function _togSvgLayer(x, G, tag, what) {
149 var on = (x === 'keyev') ? !sus.visible(G) : !!x,
Simon Huntee7a3ce2015-04-09 13:28:37 -0700150 verb = on ? 'Show' : 'Hide';
Simon Huntfcbde892015-04-16 12:05:28 -0700151 sus.visible(G, on);
152 updatePrefsState(tag, on);
153 flash.flash(verb + ' ' + what);
154 }
155
156 function toggleMap(x) {
157 _togSvgLayer(x, mapG, 'bg', 'background map');
Simon Huntee7a3ce2015-04-09 13:28:37 -0700158 }
Simon Hunt36a58c62015-04-08 11:00:07 -0700159
Simon Hunt2052e5d2015-04-13 17:40:44 -0700160 function toggleSprites(x) {
Simon Huntfcbde892015-04-16 12:05:28 -0700161 _togSvgLayer(x, spriteG, 'spr', 'sprite layer');
Simon Hunt2052e5d2015-04-13 17:40:44 -0700162 }
163
Simon Hunt08f841d02015-02-10 14:39:20 -0800164 function resetZoom() {
165 zoomer.reset();
Bri Prebilic Cole9cf1a8d2015-04-21 13:15:29 -0700166 flash.flash('Pan and zoom reset');
Simon Hunt08f841d02015-02-10 14:39:20 -0800167 }
168
Simon Hunt0e48c292015-02-19 16:11:37 -0800169 function equalizeMasters() {
Simon Hunt237676b52015-03-10 19:04:26 -0700170 wss.sendEvent('equalizeMasters');
Simon Hunt0e48c292015-02-19 16:11:37 -0800171 flash.flash('Equalizing master roles');
172 }
173
Simon Hunt08f841d02015-02-10 14:39:20 -0800174 function handleEscape() {
Simon Hunta142dd22015-02-12 22:07:51 -0800175 if (tis.showMaster()) {
176 // if an instance is selected, cancel the affinity mapping
177 tis.cancelAffinity()
Simon Hunt08f841d02015-02-10 14:39:20 -0800178
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700179 } else if (tov.hooks.escape()) {
180 // else if the overlay consumed the ESC event...
181 // (work already done)
182
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700183 } else if (tss.deselectAll()) {
Simon Hunta0eb0a82015-02-11 12:30:06 -0800184 // else if we have node selections, deselect them all
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700185 // (work already done)
186
187 } else if (tls.deselectLink()) {
188 // else if we have a link selected, deselect it
189 // (work already done)
Simon Hunt08f841d02015-02-10 14:39:20 -0800190
Simon Hunta0eb0a82015-02-11 12:30:06 -0800191 } else if (tis.isVisible()) {
192 // else if the Instance Panel is visible, hide it
193 tis.hide();
194 tfs.updateDeviceColors();
Simon Hunt08f841d02015-02-10 14:39:20 -0800195
Simon Hunta0eb0a82015-02-11 12:30:06 -0800196 } else if (tps.summaryVisible()) {
197 // else if the Summary Panel is visible, hide it
198 tps.hideSummaryPanel();
Simon Hunta0eb0a82015-02-11 12:30:06 -0800199 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800200 }
Simon Huntcacce342015-01-07 16:13:05 -0800201
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700202 // --- Toolbar Functions ---------------------------------------------
203
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700204 function notValid(what) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700205 $log.warn('topo.js getActionEntry(): Not a valid ' + what);
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700206 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700207
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700208 function getActionEntry(key) {
Bri Prebilic Cole2efc7152015-04-29 15:47:06 -0700209 var entry;
210
211 if (!key) {
212 notValid('key');
213 return null;
214 }
215
216 entry = actionMap[key];
217
218 if (!entry) {
219 notValid('actionMap entry');
220 return null;
221 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700222 return fs.isA(entry) || [entry, ''];
223 }
224
225 function setUpToolbar() {
226 ttbs.init({
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700227 getActionEntry: getActionEntry,
228 setUpKeys: setUpKeys
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700229 });
230 ttbs.createToolbar();
231 }
232
Simon Huntcacce342015-01-07 16:13:05 -0800233 // --- Glyphs, Icons, and the like -----------------------------------
234
Simon Hunt6cc53692015-01-07 11:33:45 -0800235 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -0800236 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -0800237 gs.loadDefs(defs);
Simon Hunt0ee28682015-02-12 20:48:11 -0800238 sus.loadGlowDefs(defs);
Simon Hunt6cc53692015-01-07 11:33:45 -0800239 }
240
241
Simon Huntcacce342015-01-07 16:13:05 -0800242 // --- Pan and Zoom --------------------------------------------------
243
244 // zoom enabled predicate. ev is a D3 source event.
245 function zoomEnabled(ev) {
Simon Huntd552ee92015-04-02 17:06:35 -0700246 return fs.isMobile() || (ev.metaKey || ev.altKey);
Simon Huntcacce342015-01-07 16:13:05 -0800247 }
248
249 function zoomCallback() {
Simon Huntbb5e0d82015-04-16 14:25:46 -0700250 var sc = zoomer.scale(),
251 tr = zoomer.translate();
252
253 ps.setPrefs('topo_zoom', {tx:tr[0], ty:tr[1], sc:sc});
Simon Huntcacce342015-01-07 16:13:05 -0800254
Simon Hunt0541fb82015-01-14 18:59:57 -0800255 // keep the map lines constant width while zooming
Simon Hunt737c89f2015-01-28 12:23:19 -0800256 mapG.style('stroke-width', (2.0 / sc) + 'px');
Simon Huntcacce342015-01-07 16:13:05 -0800257 }
258
259 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800260 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -0800261 zoomer = zs.createZoomer({
262 svg: svg,
263 zoomLayer: zoomLayer,
264 zoomEnabled: zoomEnabled,
265 zoomCallback: zoomCallback
266 });
267 }
268
269
Simon Hunt0541fb82015-01-14 18:59:57 -0800270 // callback invoked when the SVG view has been resized..
Simon Hunt3a6eec02015-02-09 21:16:43 -0800271 function svgResized(s) {
272 tfs.newDim([s.width, s.height]);
Simon Hunt0541fb82015-01-14 18:59:57 -0800273 }
274
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800275 // --- Background Map ------------------------------------------------
276
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800277 function setUpNoDevs() {
278 var g, box;
279 noDevsLayer = svg.append('g').attr({
280 id: 'topo-noDevsLayer',
281 transform: sus.translate(500,500)
282 });
283 // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html.
284 // We are translating this layer to have its origin at the center
285
286 g = noDevsLayer.append('g');
287 gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird');
288 g.append('text').text('No devices are connected')
289 .attr({ x: 120, y: 80});
290
291 box = g.node().getBBox();
292 box.x -= box.width/2;
293 box.y -= box.height/2;
294 g.attr('transform', sus.translate(box.x, box.y));
295
296 showNoDevs(true);
297 }
298
299 function showNoDevs(b) {
Simon Hunt18bf9822015-02-12 17:35:45 -0800300 sus.visible(noDevsLayer, b);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800301 }
302
Simon Hunt2362b072015-06-11 20:08:22 -0700303
304 var countryFilters = {
305 world: function (c) {
306 return c.properties.continent !== 'Antarctica';
307 },
308
309 // NOTE: for "usa" we are using our hand-crafted topojson file
310
311 s_america: function (c) {
312 return c.properties.continent === 'South America';
313 },
314
Simon Huntac099e82015-12-03 09:17:47 -0800315 ns_america: function (c) {
316 return c.properties.custom === 'US-cont' ||
317 c.properties.subregion === 'Central America' ||
Simon Huntd4712622015-12-02 11:19:50 -0800318 c.properties.continent === 'South America';
319 },
320
Simon Hunt2362b072015-06-11 20:08:22 -0700321 japan: function (c) {
322 return c.properties.geounit === 'Japan';
323 },
324
325 europe: function (c) {
326 return c.properties.continent === 'Europe';
327 },
328
329 italy: function (c) {
330 return c.properties.geounit === 'Italy';
331 },
332
333 uk: function (c) {
334 // technically, Ireland is not part of the United Kingdom,
335 // but the map looks weird without it showing.
336 return c.properties.adm0_a3 === 'GBR' ||
337 c.properties.adm0_a3 === 'IRL';
Simon Hunt1178d5b2015-09-02 17:02:37 -0700338 },
339
340 s_korea: function (c) {
341 return c.properties.adm0_a3 === 'KOR';
342 },
343
344 australia: function (c) {
345 return c.properties.adm0_a3 === 'AUS';
Simon Hunt2362b072015-06-11 20:08:22 -0700346 }
347 };
348
Simon Hunta34fcb52016-02-25 16:27:32 -0800349 var tintOn = 0,
350 shadeFlip = 0,
351 shadePalette = {
352 light: {
353 sea: 'aliceblue',
354 land: 'white',
355 outline: '#ddd'
356 },
357 dark: {
358 sea: '#001830',
359 land: '#232331',
360 outline: '#3a3a3a'
361 }
362 };
363
364 function shading() {
365 return tintOn ? {
366 palette: shadePalette[th.theme()],
367 flip: shadeFlip
368 } : '';
369 }
Simon Hunt2362b072015-06-11 20:08:22 -0700370
371 function setUpMap($loc) {
Simon Hunt8a119b02016-03-29 16:00:32 -0700372 var prefs = ps.getPrefs(
373 'topo_mapid',
374 { mapid: 'usa', mapscale: 1, tint: 'off'},
375 $loc.search()
376 ),
377 mapId = prefs.mapid,
378 mapScale = prefs.mapscale,
379 tint = prefs.tint,
Simon Hunt2362b072015-06-11 20:08:22 -0700380 promise,
Simon Huntfacad992016-02-25 09:58:33 -0800381 cfilter;
Simon Hunt2362b072015-06-11 20:08:22 -0700382
Simon Hunta34fcb52016-02-25 16:27:32 -0800383 tintOn = tint === 'on' ? 1 : 0;
384
Simon Hunt18491362016-03-04 11:09:42 -0800385 $log.debug('setUpMap() mapId:', mapId, ', mapScale:', mapScale,
386 ', tint:', tint);
387
Simon Hunt737c89f2015-01-28 12:23:19 -0800388 mapG = zoomLayer.append('g').attr('id', 'topo-map');
Simon Hunt2362b072015-06-11 20:08:22 -0700389 if (mapId === 'usa') {
Simon Hunta34fcb52016-02-25 16:27:32 -0800390 shadeFlip = 0;
391 promise = ms.loadMapInto(mapG, '*continental_us', {
392 adjustScale: mapScale,
393 shading: shading()
394 });
Simon Hunte1bef6e2016-02-24 22:16:04 -0800395 } else if (mapId === 'bayarea') {
Simon Hunta34fcb52016-02-25 16:27:32 -0800396 shadeFlip = 1;
Simon Huntfacad992016-02-25 09:58:33 -0800397 promise = ms.loadMapInto(mapG, '*bayarea', {
398 objectTag: 'bayareaGEO',
399 adjustScale: mapScale,
Simon Hunta34fcb52016-02-25 16:27:32 -0800400 shading: shading()
Simon Huntfacad992016-02-25 09:58:33 -0800401 });
zylin3077df52016-03-04 10:40:17 +0800402 } else if (mapId === 'taiwan') {
403 shadeFlip = 0;
404 promise = ms.loadMapInto(mapG, '*taiwan', {
405 objectTag: 'taiwan',
406 adjustScale: mapScale,
407 shading: shading()
408 })
Simon Hunt2362b072015-06-11 20:08:22 -0700409 } else {
Simon Hunta34fcb52016-02-25 16:27:32 -0800410 shadeFlip = 0;
Simon Hunt2362b072015-06-11 20:08:22 -0700411 cfilter = countryFilters[mapId] || countryFilters.world;
Simon Hunta34fcb52016-02-25 16:27:32 -0800412 promise = ms.loadMapRegionInto(mapG, {
413 countryFilter: cfilter,
414 adjustScale: mapScale,
415 shading: shading()
416 });
Simon Hunt2362b072015-06-11 20:08:22 -0700417 }
Simon Hunt8a119b02016-03-29 16:00:32 -0700418 ps.setPrefs('topo_mapid', prefs);
Simon Hunt2362b072015-06-11 20:08:22 -0700419 return promise;
Simon Hunt0541fb82015-01-14 18:59:57 -0800420 }
421
Simon Hunta34fcb52016-02-25 16:27:32 -0800422 // set up theme listener to re-shade the map when required.
423 function mapShader(on) {
424 if (on) {
425 themeListener = th.addListener(function () {
426 ms.reshade(shading());
427 });
428 } else {
429 th.removeListener(themeListener);
430 themeListener = null;
431 }
432 }
433
Simon Huntc3c5b672015-02-20 11:32:13 -0800434 function opacifyMap(b) {
435 mapG.transition()
436 .duration(1000)
437 .attr('opacity', b ? 1 : 0);
438 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800439
Simon Huntf9fc0e72015-04-16 15:10:57 -0700440 function setUpSprites($loc, tspr) {
Simon Hunt8a119b02016-03-29 16:00:32 -0700441 var prefs = ps.getPrefs('topo_sprites', { sprites: '' }, $loc.search()),
442 sprId = prefs.sprites;
Simon Huntf9fc0e72015-04-16 15:10:57 -0700443
444 spriteG = zoomLayer.append ('g').attr('id', 'topo-sprites');
445 if (sprId) {
Simon Hunt8a119b02016-03-29 16:00:32 -0700446 ps.setPrefs('topo_sprites', prefs);
Simon Huntf9fc0e72015-04-16 15:10:57 -0700447 tspr.loadSprites(spriteG, defs, sprId);
448 }
449 }
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700450
Simon Huntc7ae7952015-04-08 18:59:27 -0700451 // --- User Preferemces ----------------------------------------------
Simon Hunt36a58c62015-04-08 11:00:07 -0700452
Simon Huntc7ae7952015-04-08 18:59:27 -0700453 var prefsState = {};
Simon Hunt36a58c62015-04-08 11:00:07 -0700454
Simon Huntc7ae7952015-04-08 18:59:27 -0700455 function updatePrefsState(what, b) {
456 prefsState[what] = b ? 1 : 0;
457 ps.setPrefs('topo_prefs', prefsState);
Simon Hunt36a58c62015-04-08 11:00:07 -0700458 }
459
Simon Hunt36a58c62015-04-08 11:00:07 -0700460
Simon Huntc7ae7952015-04-08 18:59:27 -0700461 function restoreConfigFromPrefs() {
462 // NOTE: toolbar will have set this for us..
Thomas Vachuska0af26912016-03-21 21:37:30 -0700463 prefsState = ps.asNumbers(ps.getPrefs('topo_prefs', ttbs.defaultPrefs));
Simon Hunt36a58c62015-04-08 11:00:07 -0700464
Simon Hunte1ce4292015-06-12 13:07:02 -0700465 $log.debug('TOPO- Prefs State:', prefsState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700466
Simon Huntf41f3092015-04-16 10:33:26 -0700467 flash.enable(false);
Simon Huntc7ae7952015-04-08 18:59:27 -0700468 toggleInstances(prefsState.insts);
Simon Huntee7a3ce2015-04-09 13:28:37 -0700469 toggleSummary(prefsState.summary);
Simon Hunt239e5882015-04-23 15:07:04 -0700470 toggleUseDetailsFlag(prefsState.detail);
Bri Prebilic Cole9b80ca02015-08-03 11:26:54 -0700471 toggleHosts(prefsState.hosts);
472 toggleOffline(prefsState.offdev);
473 togglePorts(prefsState.porthl);
474 toggleMap(prefsState.bg);
Simon Huntf9fc0e72015-04-16 15:10:57 -0700475 toggleSprites(prefsState.spr);
Thomas Vachuska0af26912016-03-21 21:37:30 -0700476 t3s.setDevLabIndex(prefsState.dlbls);
Simon Huntf41f3092015-04-16 10:33:26 -0700477 flash.enable(true);
Simon Hunt36a58c62015-04-08 11:00:07 -0700478 }
479
480
Simon Hunte1ce4292015-06-12 13:07:02 -0700481 // somewhat hackish, because summary update cannot happen until we
482 // have opened the websocket to the server; hence this extra function
483 // invoked after tes.start()
484 function restoreSummaryFromPrefs() {
Thomas Vachuska0af26912016-03-21 21:37:30 -0700485 prefsState = ps.asNumbers(ps.getPrefs('topo_prefs', ttbs.defaultPrefs));
Simon Hunte1ce4292015-06-12 13:07:02 -0700486 $log.debug('TOPO- Prefs SUMMARY State:', prefsState.summary);
487
488 flash.enable(false);
489 toggleSummary(prefsState.summary);
490 flash.enable(true);
491 }
492
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700493 function topoStartDone() {
494 var d = $scope.intentData;
495 if (d) {
496 tts.selectIntent(d);
497 }
498 }
Simon Hunte1ce4292015-06-12 13:07:02 -0700499
Simon Huntcacce342015-01-07 16:13:05 -0800500 // --- Controller Definition -----------------------------------------
501
Simon Hunt6cc53692015-01-07 11:33:45 -0800502 angular.module('ovTopo', moduleDependencies)
Simon Hunt237676b52015-03-10 19:04:26 -0700503 .controller('OvTopoCtrl', ['$scope', '$log', '$location', '$timeout',
Simon Hunt36a58c62015-04-08 11:00:07 -0700504 '$cookies', 'FnService', 'MastService', 'KeyService', 'ZoomService',
Simon Hunt0e48c292015-02-19 16:11:37 -0800505 'GlyphService', 'MapService', 'SvgUtilService', 'FlashService',
Simon Hunta34fcb52016-02-25 16:27:32 -0800506 'WebSocketService', 'PrefsService', 'ThemeService',
Thomas Vachuska0af26912016-03-21 21:37:30 -0700507 'TopoDialogService', 'TopoD3Service',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800508 'TopoEventService', 'TopoForceService', 'TopoPanelService',
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700509 'TopoInstService', 'TopoSelectService', 'TopoLinkService',
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700510 'TopoTrafficService', 'TopoObliqueService', 'TopoFilterService',
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700511 'TopoToolbarService', 'TopoSpriteService', 'TooltipService',
Simon Hunt72e44bf2015-07-21 21:34:20 -0700512 'TopoOverlayService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800513
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700514 function (_$scope_, _$log_, $loc, $timeout, _$cookies_, _fs_, mast, _ks_,
Simon Hunta34fcb52016-02-25 16:27:32 -0800515 _zs_, _gs_, _ms_, _sus_, _flash_, _wss_, _ps_, _th_,
Thomas Vachuska0af26912016-03-21 21:37:30 -0700516 _tds_, _t3s_, _tes_,
Simon Hunta211f7f2015-11-09 12:48:23 -0800517 _tfs_, _tps_, _tis_, _tss_, _tls_, _tts_, _tos_, _fltr_,
Simon Hunta4281242016-02-26 20:18:45 -0800518 _ttbs_, _tspr_, _ttip_, _tov_) {
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700519 var params = $loc.search(),
520 projection,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800521 dim,
Simon Hunt1894d792015-02-04 17:09:20 -0800522 uplink = {
523 // provides function calls back into this space
524 showNoDevs: showNoDevs,
525 projection: function () { return projection; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800526 zoomLayer: function () { return zoomLayer; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800527 zoomer: function () { return zoomer; },
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700528 opacifyMap: opacifyMap,
529 topoStartDone: topoStartDone
Simon Huntac4c6f72015-02-03 19:50:53 -0800530 };
531
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700532 $scope = _$scope_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800533 $log = _$log_;
Simon Hunt36a58c62015-04-08 11:00:07 -0700534 $cookies = _$cookies_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800535 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800536 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800537 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800538 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800539 ms = _ms_;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800540 sus = _sus_;
Simon Hunt0e48c292015-02-19 16:11:37 -0800541 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700542 wss = _wss_;
Simon Huntc7ae7952015-04-08 18:59:27 -0700543 ps = _ps_;
Simon Hunta34fcb52016-02-25 16:27:32 -0800544 th = _th_;
Simon Hunta211f7f2015-11-09 12:48:23 -0800545 tds = _tds_;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700546 t3s = _t3s_;
Simon Huntc252aa62015-02-10 16:45:39 -0800547 tes = _tes_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800548 tfs = _tfs_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800549 // TODO: consider funnelling actions through TopoForceService...
550 // rather than injecting references to these 'sub-modules',
551 // just so we can invoke functions on them.
Simon Huntc252aa62015-02-10 16:45:39 -0800552 tps = _tps_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800553 tis = _tis_;
Simon Hunt6036b192015-02-11 11:20:26 -0800554 tss = _tss_;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700555 tls = _tls_;
Simon Huntf542d842015-02-11 16:20:33 -0800556 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800557 tos = _tos_;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700558 fltr = _fltr_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700559 ttbs = _ttbs_;
Simon Hunta4281242016-02-26 20:18:45 -0800560 tspr = _tspr_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700561 ttip = _ttip_;
Simon Hunt72e44bf2015-07-21 21:34:20 -0700562 tov = _tov_;
Simon Huntef31fb22014-12-19 13:16:44 -0800563
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700564 if (params.intentKey && params.intentAppId && params.intentAppName) {
565 $scope.intentData = {
566 key: params.intentKey,
567 appId: params.intentAppId,
568 appName: params.intentAppName
569 };
570 }
571
Bri Prebilic Cole068814d2015-05-14 16:06:38 -0700572 $scope.notifyResize = function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800573 svgResized(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800574 };
Simon Huntef31fb22014-12-19 13:16:44 -0800575
Simon Hunt54442fa2015-01-26 14:17:38 -0800576 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800577 $scope.$on('$destroy', function () {
578 $log.log('OvTopoCtrl is saying Buh-Bye!');
Thomas Vachuska329af532015-03-10 02:08:33 -0700579 tes.stop();
Bri Prebilic Cole9dcaea52015-07-21 14:39:48 -0700580 ks.unbindKeys();
Simon Hunt626d2102015-01-29 11:54:50 -0800581 tps.destroyPanels();
Simon Hunta211f7f2015-11-09 12:48:23 -0800582 tds.closeDialog();
Simon Hunt4b668592015-01-29 17:33:53 -0800583 tis.destroyInst();
Simon Hunt3a6eec02015-02-09 21:16:43 -0800584 tfs.destroyForce();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700585 ttbs.destroyToolbar();
Simon Hunta34fcb52016-02-25 16:27:32 -0800586 mapShader(false);
Simon Hunt584122a2015-01-21 15:32:40 -0800587 });
588
Simon Huntcacce342015-01-07 16:13:05 -0800589 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800590 ovtopo = d3.select('#ov-topo');
591 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800592 // set the svg size to match that of the window, less the masthead
593 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt3a6eec02015-02-09 21:16:43 -0800594 dim = [svg.attr('width'), svg.attr('height')];
Simon Hunt426bd862015-01-14 16:48:41 -0800595
Simon Hunt6cc53692015-01-07 11:33:45 -0800596 setUpKeys();
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700597 setUpToolbar();
Simon Hunt6cc53692015-01-07 11:33:45 -0800598 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800599 setUpZoom();
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800600 setUpNoDevs();
Simon Hunt2362b072015-06-11 20:08:22 -0700601 setUpMap($loc).then(
Simon Hunt1894d792015-02-04 17:09:20 -0800602 function (proj) {
Thomas Vachuska0af26912016-03-21 21:37:30 -0700603 var z = ps.getPrefs('topo_zoom', { tx:0, ty:0, sc:1 });
Simon Huntbb5e0d82015-04-16 14:25:46 -0700604 zoomer.panZoom([z.tx, z.ty], z.sc);
605 $log.debug('** Zoom restored:', z);
606
Simon Hunt1894d792015-02-04 17:09:20 -0800607 projection = proj;
Simon Huntbb5e0d82015-04-16 14:25:46 -0700608 $log.debug('** We installed the projection:', proj);
Simon Huntf41f3092015-04-16 10:33:26 -0700609 flash.enable(false);
Simon Huntc7ae7952015-04-08 18:59:27 -0700610 toggleMap(prefsState.bg);
Simon Huntf41f3092015-04-16 10:33:26 -0700611 flash.enable(true);
Simon Hunta34fcb52016-02-25 16:27:32 -0800612 mapShader(true);
Simon Hunte1ce4292015-06-12 13:07:02 -0700613
614 // now we have the map projection, we are ready for
615 // the server to send us device/host data...
616 tes.start();
617 // need to do the following so we immediately get
618 // the summary panel data back from the server
619 restoreSummaryFromPrefs();
Simon Hunt1894d792015-02-04 17:09:20 -0800620 }
621 );
Simon Hunta4281242016-02-26 20:18:45 -0800622 tes.bindHandlers();
Simon Huntf9fc0e72015-04-16 15:10:57 -0700623 setUpSprites($loc, tspr);
Simon Huntfd1231a2015-01-26 22:14:51 -0800624
Simon Hunt1894d792015-02-04 17:09:20 -0800625 forceG = zoomLayer.append('g').attr('id', 'topo-force');
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800626 tfs.initForce(svg, forceG, uplink, dim);
Simon Hunta142dd22015-02-12 22:07:51 -0800627 tis.initInst({ showMastership: tfs.showMastership });
Simon Hunt237676b52015-03-10 19:04:26 -0700628 tps.initPanels();
Simon Hunt6cc53692015-01-07 11:33:45 -0800629
Simon Hunt36a58c62015-04-08 11:00:07 -0700630 // temporary solution for persisting user settings
Simon Huntc7ae7952015-04-08 18:59:27 -0700631 restoreConfigFromPrefs();
Simon Hunta5b53af2015-10-12 15:56:40 -0700632 ttbs.setDefaultOverlay();
Simon Hunt36a58c62015-04-08 11:00:07 -0700633
Simon Hunt72e44bf2015-07-21 21:34:20 -0700634 $log.debug('registered overlays...', tov.list());
Simon Hunt6cc53692015-01-07 11:33:45 -0800635 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800636 }]);
637}());