Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 1 | /* |
Simon Hunt | 8ead3a2 | 2015-01-06 11:00:15 -0800 | [diff] [blame] | 2 | * Copyright 2014,2015 Open Networking Laboratory |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 3 | * |
| 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 Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 23 | |
| 24 | var moduleDependencies = [ |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 25 | 'ngCookies', |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 26 | 'onosUtil', |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 27 | 'onosSvg', |
| 28 | 'onosRemote' |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 29 | ]; |
| 30 | |
| 31 | // references to injected services etc. |
Bri Prebilic Cole | 068814d | 2015-05-14 16:06:38 -0700 | [diff] [blame] | 32 | var $scope, $log, $cookies, fs, ks, zs, gs, ms, sus, flash, wss, ps, |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 33 | tes, tfs, tps, tis, tss, tls, tts, tos, fltr, ttbs, ttip, tov; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 34 | |
| 35 | // DOM elements |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 36 | var ovtopo, svg, defs, zoomLayer, mapG, spriteG, forceG, noDevsLayer; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 37 | |
| 38 | // Internal state |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 39 | var zoomer, actionMap; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 40 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 41 | // --- Short Cut Keys ------------------------------------------------ |
| 42 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 43 | function setUpKeys() { |
| 44 | // key bindings need to be made after the services have been injected |
| 45 | // thus, deferred to here... |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 46 | actionMap = { |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 47 | I: [toggleInstances, 'Toggle ONOS instances panel'], |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 48 | O: [toggleSummary, 'Toggle ONOS summary panel'], |
Simon Hunt | 239e588 | 2015-04-23 15:07:04 -0700 | [diff] [blame] | 49 | D: [toggleUseDetailsFlag, 'Disable / enable details panel'], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 50 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 51 | H: [toggleHosts, 'Toggle host visibility'], |
| 52 | M: [toggleOffline, 'Toggle offline visibility'], |
| 53 | P: [togglePorts, 'Toggle Port Highlighting'], |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 54 | dash: [tfs.showBadLinks, 'Show bad links'], |
| 55 | B: [toggleMap, 'Toggle background map'], |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 56 | S: [toggleSprites, 'Toggle sprite layer'], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 57 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 58 | //X: [toggleNodeLock, 'Lock / unlock node positions'], |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 59 | Z: [tos.toggleOblique, 'Toggle oblique view (Experimental)'], |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 60 | N: [fltr.clickAction, 'Cycle node layers'], |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 61 | L: [tfs.cycleDeviceLabels, 'Cycle device labels'], |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 62 | U: [tfs.unpin, 'Unpin node (hover mouse over)'], |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 63 | R: [resetZoom, 'Reset pan / zoom'], |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 64 | dot: [ttbs.toggleToolbar, 'Toggle Toolbar'], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 65 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 66 | V: [tts.showRelatedIntentsAction, 'Show all related intents'], |
| 67 | rightArrow: [tts.showNextIntentAction, 'Show next related intent'], |
| 68 | leftArrow: [tts.showPrevIntentAction, 'Show previous related intent'], |
| 69 | W: [tts.showSelectedIntentTrafficAction, 'Monitor traffic of selected intent'], |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 70 | A: [tts.showAllFlowTrafficAction, 'Monitor all traffic using flow stats'], |
| 71 | Q: [tts.showAllPortTrafficAction, 'Monitor all traffic using port stats'], |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 72 | F: [tts.showDeviceLinkFlowsAction, 'Show device link flows'], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 73 | |
Simon Hunt | 0e48c29 | 2015-02-19 16:11:37 -0800 | [diff] [blame] | 74 | E: [equalizeMasters, 'Equalize mastership roles'], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 75 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 76 | esc: handleEscape, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 77 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 78 | _keyListener: ttbs.keyListener, |
| 79 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 80 | _helpFormat: [ |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 81 | ['I', 'O', 'D', '-', 'H', 'M', 'P', 'dash', 'B' ], |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 82 | ['X', 'Z', 'N', 'L', 'U', 'R', '-', 'dot'], |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 83 | ['V', 'rightArrow', 'leftArrow', 'W', 'A', 'F', '-', 'E' ] |
| 84 | ] |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | ks.keyBindings(actionMap); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 88 | |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 89 | ks.gestureNotes([ |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 90 | ['click', 'Select the item and show details'], |
| 91 | ['shift-click', 'Toggle selection state'], |
| 92 | ['drag', 'Reposition (and pin) device / host'], |
| 93 | ['cmd-scroll', 'Zoom in / out'], |
| 94 | ['cmd-drag', 'Pan'] |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 95 | ]); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 96 | } |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 97 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 98 | // --- Keystroke functions ------------------------------------------- |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 99 | |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 100 | function toggleInstances(x) { |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 101 | updatePrefsState('insts', tis.toggle(x)); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 102 | tfs.updateDeviceColors(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 103 | } |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 104 | |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 105 | function toggleSummary(x) { |
| 106 | updatePrefsState('summary', tps.toggleSummary(x)); |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Simon Hunt | 239e588 | 2015-04-23 15:07:04 -0700 | [diff] [blame] | 109 | function toggleUseDetailsFlag(x) { |
| 110 | updatePrefsState('detail', tps.toggleUseDetailsFlag(x)); |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 113 | function toggleHosts(x) { |
| 114 | updatePrefsState('hosts', tfs.toggleHosts(x)); |
| 115 | } |
| 116 | |
| 117 | function toggleOffline(x) { |
| 118 | updatePrefsState('offdev', tfs.toggleOffline(x)); |
| 119 | } |
| 120 | |
| 121 | function togglePorts(x) { |
| 122 | updatePrefsState('porthl', tfs.togglePorts(x)); |
| 123 | } |
| 124 | |
| 125 | function _togSvgLayer(x, G, tag, what) { |
| 126 | var on = (x === 'keyev') ? !sus.visible(G) : !!x, |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 127 | verb = on ? 'Show' : 'Hide'; |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 128 | sus.visible(G, on); |
| 129 | updatePrefsState(tag, on); |
| 130 | flash.flash(verb + ' ' + what); |
| 131 | } |
| 132 | |
| 133 | function toggleMap(x) { |
| 134 | _togSvgLayer(x, mapG, 'bg', 'background map'); |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 135 | } |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 136 | |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 137 | function toggleSprites(x) { |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 138 | _togSvgLayer(x, spriteG, 'spr', 'sprite layer'); |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 141 | function resetZoom() { |
| 142 | zoomer.reset(); |
Bri Prebilic Cole | 9cf1a8d | 2015-04-21 13:15:29 -0700 | [diff] [blame] | 143 | flash.flash('Pan and zoom reset'); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Simon Hunt | 0e48c29 | 2015-02-19 16:11:37 -0800 | [diff] [blame] | 146 | function equalizeMasters() { |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 147 | wss.sendEvent('equalizeMasters'); |
Simon Hunt | 0e48c29 | 2015-02-19 16:11:37 -0800 | [diff] [blame] | 148 | flash.flash('Equalizing master roles'); |
| 149 | } |
| 150 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 151 | function handleEscape() { |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 152 | if (tis.showMaster()) { |
| 153 | // if an instance is selected, cancel the affinity mapping |
| 154 | tis.cancelAffinity() |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 155 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 156 | } else if (tss.deselectAll()) { |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 157 | // else if we have node selections, deselect them all |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 158 | // (work already done) |
| 159 | |
| 160 | } else if (tls.deselectLink()) { |
| 161 | // else if we have a link selected, deselect it |
| 162 | // (work already done) |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 163 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 164 | } else if (tis.isVisible()) { |
| 165 | // else if the Instance Panel is visible, hide it |
| 166 | tis.hide(); |
| 167 | tfs.updateDeviceColors(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 168 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 169 | } else if (tps.summaryVisible()) { |
| 170 | // else if the Summary Panel is visible, hide it |
| 171 | tps.hideSummaryPanel(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 172 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 173 | } else { |
| 174 | // TODO: set hover mode to hoverModeNone |
Simon Hunt | 62c2f8f | 2015-02-19 14:19:10 -0800 | [diff] [blame] | 175 | // talk to Thomas about this: shouldn't it be done |
| 176 | // when we deselect the node (if tss.haveDetails()...) |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 177 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 178 | } |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 179 | |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 180 | // --- Toolbar Functions --------------------------------------------- |
| 181 | |
Bri Prebilic Cole | 2efc715 | 2015-04-29 15:47:06 -0700 | [diff] [blame] | 182 | function notValid(what) { |
| 183 | $log.warn('Topo.js getActionEntry(): Not a valid ' + what); |
| 184 | } |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 185 | function getActionEntry(key) { |
Bri Prebilic Cole | 2efc715 | 2015-04-29 15:47:06 -0700 | [diff] [blame] | 186 | var entry; |
| 187 | |
| 188 | if (!key) { |
| 189 | notValid('key'); |
| 190 | return null; |
| 191 | } |
| 192 | |
| 193 | entry = actionMap[key]; |
| 194 | |
| 195 | if (!entry) { |
| 196 | notValid('actionMap entry'); |
| 197 | return null; |
| 198 | } |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 199 | return fs.isA(entry) || [entry, '']; |
| 200 | } |
| 201 | |
| 202 | function setUpToolbar() { |
| 203 | ttbs.init({ |
| 204 | getActionEntry: getActionEntry |
| 205 | }); |
| 206 | ttbs.createToolbar(); |
| 207 | } |
| 208 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 209 | // --- Glyphs, Icons, and the like ----------------------------------- |
| 210 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 211 | function setUpDefs() { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 212 | defs = svg.append('defs'); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 213 | gs.loadDefs(defs); |
Simon Hunt | 0ee2868 | 2015-02-12 20:48:11 -0800 | [diff] [blame] | 214 | sus.loadGlowDefs(defs); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 218 | // --- Pan and Zoom -------------------------------------------------- |
| 219 | |
| 220 | // zoom enabled predicate. ev is a D3 source event. |
| 221 | function zoomEnabled(ev) { |
Simon Hunt | d552ee9 | 2015-04-02 17:06:35 -0700 | [diff] [blame] | 222 | return fs.isMobile() || (ev.metaKey || ev.altKey); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | function zoomCallback() { |
Simon Hunt | bb5e0d8 | 2015-04-16 14:25:46 -0700 | [diff] [blame] | 226 | var sc = zoomer.scale(), |
| 227 | tr = zoomer.translate(); |
| 228 | |
| 229 | ps.setPrefs('topo_zoom', {tx:tr[0], ty:tr[1], sc:sc}); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 230 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 231 | // keep the map lines constant width while zooming |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 232 | mapG.style('stroke-width', (2.0 / sc) + 'px'); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | function setUpZoom() { |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 236 | zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer'); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 237 | zoomer = zs.createZoomer({ |
| 238 | svg: svg, |
| 239 | zoomLayer: zoomLayer, |
| 240 | zoomEnabled: zoomEnabled, |
| 241 | zoomCallback: zoomCallback |
| 242 | }); |
| 243 | } |
| 244 | |
| 245 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 246 | // callback invoked when the SVG view has been resized.. |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 247 | function svgResized(s) { |
| 248 | tfs.newDim([s.width, s.height]); |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 251 | // --- Background Map ------------------------------------------------ |
| 252 | |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 253 | function setUpNoDevs() { |
| 254 | var g, box; |
| 255 | noDevsLayer = svg.append('g').attr({ |
| 256 | id: 'topo-noDevsLayer', |
| 257 | transform: sus.translate(500,500) |
| 258 | }); |
| 259 | // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html. |
| 260 | // We are translating this layer to have its origin at the center |
| 261 | |
| 262 | g = noDevsLayer.append('g'); |
| 263 | gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird'); |
| 264 | g.append('text').text('No devices are connected') |
| 265 | .attr({ x: 120, y: 80}); |
| 266 | |
| 267 | box = g.node().getBBox(); |
| 268 | box.x -= box.width/2; |
| 269 | box.y -= box.height/2; |
| 270 | g.attr('transform', sus.translate(box.x, box.y)); |
| 271 | |
| 272 | showNoDevs(true); |
| 273 | } |
| 274 | |
| 275 | function showNoDevs(b) { |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 276 | sus.visible(noDevsLayer, b); |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 279 | |
| 280 | var countryFilters = { |
| 281 | world: function (c) { |
| 282 | return c.properties.continent !== 'Antarctica'; |
| 283 | }, |
| 284 | |
| 285 | // NOTE: for "usa" we are using our hand-crafted topojson file |
| 286 | |
| 287 | s_america: function (c) { |
| 288 | return c.properties.continent === 'South America'; |
| 289 | }, |
| 290 | |
| 291 | japan: function (c) { |
| 292 | return c.properties.geounit === 'Japan'; |
| 293 | }, |
| 294 | |
| 295 | europe: function (c) { |
| 296 | return c.properties.continent === 'Europe'; |
| 297 | }, |
| 298 | |
| 299 | italy: function (c) { |
| 300 | return c.properties.geounit === 'Italy'; |
| 301 | }, |
| 302 | |
| 303 | uk: function (c) { |
| 304 | // technically, Ireland is not part of the United Kingdom, |
| 305 | // but the map looks weird without it showing. |
| 306 | return c.properties.adm0_a3 === 'GBR' || |
| 307 | c.properties.adm0_a3 === 'IRL'; |
| 308 | } |
| 309 | }; |
| 310 | |
| 311 | |
| 312 | function setUpMap($loc) { |
| 313 | var s1 = $loc.search().mapid, |
| 314 | s2 = ps.getPrefs('topo_mapid'), |
Simon Hunt | f65c378 | 2015-06-12 13:33:48 -0700 | [diff] [blame] | 315 | mapId = s1 || (s2 && s2.id) || 'usa', |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 316 | promise, |
| 317 | cfilter, |
| 318 | opts; |
| 319 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 320 | mapG = zoomLayer.append('g').attr('id', 'topo-map'); |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 321 | if (mapId === 'usa') { |
| 322 | promise = ms.loadMapInto(mapG, '*continental_us'); |
| 323 | } else { |
| 324 | ps.setPrefs('topo_mapid', {id:mapId}); |
| 325 | cfilter = countryFilters[mapId] || countryFilters.world; |
| 326 | opts = { countryFilter: cfilter }; |
| 327 | promise = ms.loadMapRegionInto(mapG, opts); |
| 328 | } |
| 329 | return promise; |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 332 | function opacifyMap(b) { |
| 333 | mapG.transition() |
| 334 | .duration(1000) |
| 335 | .attr('opacity', b ? 1 : 0); |
| 336 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 337 | |
Simon Hunt | f9fc0e7 | 2015-04-16 15:10:57 -0700 | [diff] [blame] | 338 | function setUpSprites($loc, tspr) { |
| 339 | var s1 = $loc.search().sprites, |
| 340 | s2 = ps.getPrefs('topo_sprites'), |
| 341 | sprId = s1 || (s2 && s2.id); |
| 342 | |
| 343 | spriteG = zoomLayer.append ('g').attr('id', 'topo-sprites'); |
| 344 | if (sprId) { |
| 345 | ps.setPrefs('topo_sprites', {id:sprId}); |
| 346 | tspr.loadSprites(spriteG, defs, sprId); |
| 347 | } |
| 348 | } |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 349 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 350 | // --- User Preferemces ---------------------------------------------- |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 351 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 352 | var prefsState = {}; |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 353 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 354 | function updatePrefsState(what, b) { |
| 355 | prefsState[what] = b ? 1 : 0; |
| 356 | ps.setPrefs('topo_prefs', prefsState); |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 359 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 360 | function restoreConfigFromPrefs() { |
| 361 | // NOTE: toolbar will have set this for us.. |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 362 | prefsState = ps.asNumbers(ps.getPrefs('topo_prefs')); |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 363 | |
Simon Hunt | e1ce429 | 2015-06-12 13:07:02 -0700 | [diff] [blame] | 364 | $log.debug('TOPO- Prefs State:', prefsState); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 365 | |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 366 | flash.enable(false); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 367 | toggleInstances(prefsState.insts); |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 368 | toggleSummary(prefsState.summary); |
Simon Hunt | 239e588 | 2015-04-23 15:07:04 -0700 | [diff] [blame] | 369 | toggleUseDetailsFlag(prefsState.detail); |
Simon Hunt | f9fc0e7 | 2015-04-16 15:10:57 -0700 | [diff] [blame] | 370 | toggleSprites(prefsState.spr); |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 371 | flash.enable(true); |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
Simon Hunt | e1ce429 | 2015-06-12 13:07:02 -0700 | [diff] [blame] | 375 | // somewhat hackish, because summary update cannot happen until we |
| 376 | // have opened the websocket to the server; hence this extra function |
| 377 | // invoked after tes.start() |
| 378 | function restoreSummaryFromPrefs() { |
| 379 | prefsState = ps.asNumbers(ps.getPrefs('topo_prefs')); |
| 380 | $log.debug('TOPO- Prefs SUMMARY State:', prefsState.summary); |
| 381 | |
| 382 | flash.enable(false); |
| 383 | toggleSummary(prefsState.summary); |
| 384 | flash.enable(true); |
| 385 | } |
| 386 | |
| 387 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 388 | // --- Controller Definition ----------------------------------------- |
| 389 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 390 | angular.module('ovTopo', moduleDependencies) |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 391 | .controller('OvTopoCtrl', ['$scope', '$log', '$location', '$timeout', |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 392 | '$cookies', 'FnService', 'MastService', 'KeyService', 'ZoomService', |
Simon Hunt | 0e48c29 | 2015-02-19 16:11:37 -0800 | [diff] [blame] | 393 | 'GlyphService', 'MapService', 'SvgUtilService', 'FlashService', |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 394 | 'WebSocketService', 'PrefsService', |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 395 | 'TopoEventService', 'TopoForceService', 'TopoPanelService', |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 396 | 'TopoInstService', 'TopoSelectService', 'TopoLinkService', |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 397 | 'TopoTrafficService', 'TopoObliqueService', 'TopoFilterService', |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 398 | 'TopoToolbarService', 'TopoSpriteService', 'TooltipService', |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 399 | 'TopoOverlayService', |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 400 | |
Bri Prebilic Cole | 068814d | 2015-05-14 16:06:38 -0700 | [diff] [blame] | 401 | function (_$scope_, _$log_, $loc, $timeout, _$cookies_, _fs_, mast, _ks_, |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 402 | _zs_, _gs_, _ms_, _sus_, _flash_, _wss_, _ps_, _tes_, _tfs_, |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 403 | _tps_, _tis_, _tss_, _tls_, _tts_, _tos_, _fltr_, _ttbs_, tspr, |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 404 | _ttip_, _tov_) { |
Bri Prebilic Cole | 068814d | 2015-05-14 16:06:38 -0700 | [diff] [blame] | 405 | var projection, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 406 | dim, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 407 | uplink = { |
| 408 | // provides function calls back into this space |
| 409 | showNoDevs: showNoDevs, |
| 410 | projection: function () { return projection; }, |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 411 | zoomLayer: function () { return zoomLayer; }, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 412 | zoomer: function () { return zoomer; }, |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 413 | opacifyMap: opacifyMap |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 414 | }; |
| 415 | |
Bri Prebilic Cole | 068814d | 2015-05-14 16:06:38 -0700 | [diff] [blame] | 416 | $scope = _$scope_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 417 | $log = _$log_; |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 418 | $cookies = _$cookies_; |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 419 | fs = _fs_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 420 | ks = _ks_; |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 421 | zs = _zs_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 422 | gs = _gs_; |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame] | 423 | ms = _ms_; |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 424 | sus = _sus_; |
Simon Hunt | 0e48c29 | 2015-02-19 16:11:37 -0800 | [diff] [blame] | 425 | flash = _flash_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 426 | wss = _wss_; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 427 | ps = _ps_; |
Simon Hunt | c252aa6 | 2015-02-10 16:45:39 -0800 | [diff] [blame] | 428 | tes = _tes_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 429 | tfs = _tfs_; |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 430 | // TODO: consider funnelling actions through TopoForceService... |
| 431 | // rather than injecting references to these 'sub-modules', |
| 432 | // just so we can invoke functions on them. |
Simon Hunt | c252aa6 | 2015-02-10 16:45:39 -0800 | [diff] [blame] | 433 | tps = _tps_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 434 | tis = _tis_; |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 435 | tss = _tss_; |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 436 | tls = _tls_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 437 | tts = _tts_; |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 438 | tos = _tos_; |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 439 | fltr = _fltr_; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 440 | ttbs = _ttbs_; |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 441 | ttip = _ttip_; |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 442 | tov = _tov_; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 443 | |
Bri Prebilic Cole | 068814d | 2015-05-14 16:06:38 -0700 | [diff] [blame] | 444 | $scope.notifyResize = function () { |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 445 | svgResized(fs.windowSize(mast.mastHeight())); |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 446 | }; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 447 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 448 | // Cleanup on destroyed scope.. |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 449 | $scope.$on('$destroy', function () { |
| 450 | $log.log('OvTopoCtrl is saying Buh-Bye!'); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 451 | tes.stop(); |
Bri Prebilic Cole | 9dcaea5 | 2015-07-21 14:39:48 -0700 | [diff] [blame] | 452 | ks.unbindKeys(); |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 453 | tps.destroyPanels(); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 454 | tis.destroyInst(); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 455 | tfs.destroyForce(); |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 456 | ttbs.destroyToolbar(); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 457 | }); |
| 458 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 459 | // svg layer and initialization of components |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 460 | ovtopo = d3.select('#ov-topo'); |
| 461 | svg = ovtopo.select('svg'); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 462 | // set the svg size to match that of the window, less the masthead |
| 463 | svg.attr(fs.windowSize(mast.mastHeight())); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 464 | dim = [svg.attr('width'), svg.attr('height')]; |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 465 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 466 | setUpKeys(); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 467 | setUpToolbar(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 468 | setUpDefs(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 469 | setUpZoom(); |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 470 | setUpNoDevs(); |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 471 | setUpMap($loc).then( |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 472 | function (proj) { |
Simon Hunt | bb5e0d8 | 2015-04-16 14:25:46 -0700 | [diff] [blame] | 473 | var z = ps.getPrefs('topo_zoom') || {tx:0, ty:0, sc:1}; |
| 474 | zoomer.panZoom([z.tx, z.ty], z.sc); |
| 475 | $log.debug('** Zoom restored:', z); |
| 476 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 477 | projection = proj; |
Simon Hunt | bb5e0d8 | 2015-04-16 14:25:46 -0700 | [diff] [blame] | 478 | $log.debug('** We installed the projection:', proj); |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 479 | flash.enable(false); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 480 | toggleMap(prefsState.bg); |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 481 | flash.enable(true); |
Simon Hunt | e1ce429 | 2015-06-12 13:07:02 -0700 | [diff] [blame] | 482 | |
| 483 | // now we have the map projection, we are ready for |
| 484 | // the server to send us device/host data... |
| 485 | tes.start(); |
| 486 | // need to do the following so we immediately get |
| 487 | // the summary panel data back from the server |
| 488 | restoreSummaryFromPrefs(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 489 | } |
| 490 | ); |
Simon Hunt | f9fc0e7 | 2015-04-16 15:10:57 -0700 | [diff] [blame] | 491 | setUpSprites($loc, tspr); |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 492 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 493 | forceG = zoomLayer.append('g').attr('id', 'topo-force'); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 494 | tfs.initForce(svg, forceG, uplink, dim); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 495 | tis.initInst({ showMastership: tfs.showMastership }); |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 496 | tps.initPanels(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 497 | |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 498 | // temporary solution for persisting user settings |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 499 | restoreConfigFromPrefs(); |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 500 | |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 501 | $log.debug('registered overlays...', tov.list()); |
| 502 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 503 | $log.log('OvTopoCtrl has been created'); |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 504 | }]); |
| 505 | }()); |