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. |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 32 | var $log, $cookies, fs, ks, zs, gs, ms, sus, flash, wss, ps, |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame^] | 33 | tes, tfs, tps, tis, tss, tls, tts, tos, fltr, ttbs, ttip; |
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'], |
| 49 | D: [toggleDetails, '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'], |
| 70 | A: [tts.showAllTrafficAction, 'Monitor all traffic'], |
| 71 | F: [tts.showDeviceLinkFlowsAction, 'Show device link flows'], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 72 | |
Simon Hunt | 0e48c29 | 2015-02-19 16:11:37 -0800 | [diff] [blame] | 73 | E: [equalizeMasters, 'Equalize mastership roles'], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 74 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 75 | esc: handleEscape, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 76 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 77 | _keyListener: ttbs.keyListener, |
| 78 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 79 | _helpFormat: [ |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 80 | ['I', 'O', 'D', '-', 'H', 'M', 'P', 'dash', 'B' ], |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 81 | ['X', 'Z', 'N', 'L', 'U', 'R', '-', 'dot'], |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 82 | ['V', 'rightArrow', 'leftArrow', 'W', 'A', 'F', '-', 'E' ] |
| 83 | ] |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | ks.keyBindings(actionMap); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 87 | |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 88 | ks.gestureNotes([ |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 89 | ['click', 'Select the item and show details'], |
| 90 | ['shift-click', 'Toggle selection state'], |
| 91 | ['drag', 'Reposition (and pin) device / host'], |
| 92 | ['cmd-scroll', 'Zoom in / out'], |
| 93 | ['cmd-drag', 'Pan'] |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 94 | ]); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 95 | } |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 96 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 97 | // --- Keystroke functions ------------------------------------------- |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 98 | |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 99 | function toggleInstances(x) { |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 100 | updatePrefsState('insts', tis.toggle(x)); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 101 | tfs.updateDeviceColors(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 102 | } |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 103 | |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 104 | function toggleSummary(x) { |
| 105 | updatePrefsState('summary', tps.toggleSummary(x)); |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 108 | function toggleDetails(x) { |
| 109 | updatePrefsState('detail', tps.toggleDetails(x)); |
| 110 | } |
| 111 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 112 | function toggleHosts(x) { |
| 113 | updatePrefsState('hosts', tfs.toggleHosts(x)); |
| 114 | } |
| 115 | |
| 116 | function toggleOffline(x) { |
| 117 | updatePrefsState('offdev', tfs.toggleOffline(x)); |
| 118 | } |
| 119 | |
| 120 | function togglePorts(x) { |
| 121 | updatePrefsState('porthl', tfs.togglePorts(x)); |
| 122 | } |
| 123 | |
| 124 | function _togSvgLayer(x, G, tag, what) { |
| 125 | var on = (x === 'keyev') ? !sus.visible(G) : !!x, |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 126 | verb = on ? 'Show' : 'Hide'; |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 127 | sus.visible(G, on); |
| 128 | updatePrefsState(tag, on); |
| 129 | flash.flash(verb + ' ' + what); |
| 130 | } |
| 131 | |
| 132 | function toggleMap(x) { |
| 133 | _togSvgLayer(x, mapG, 'bg', 'background map'); |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 134 | } |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 135 | |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 136 | function toggleSprites(x) { |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 137 | _togSvgLayer(x, spriteG, 'spr', 'sprite layer'); |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 140 | function resetZoom() { |
| 141 | zoomer.reset(); |
Bri Prebilic Cole | 9cf1a8d | 2015-04-21 13:15:29 -0700 | [diff] [blame] | 142 | flash.flash('Pan and zoom reset'); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Simon Hunt | 0e48c29 | 2015-02-19 16:11:37 -0800 | [diff] [blame] | 145 | function equalizeMasters() { |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 146 | wss.sendEvent('equalizeMasters'); |
Simon Hunt | 0e48c29 | 2015-02-19 16:11:37 -0800 | [diff] [blame] | 147 | flash.flash('Equalizing master roles'); |
| 148 | } |
| 149 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 150 | function handleEscape() { |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 151 | if (tis.showMaster()) { |
| 152 | // if an instance is selected, cancel the affinity mapping |
| 153 | tis.cancelAffinity() |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 154 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 155 | } else if (tss.deselectAll()) { |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 156 | // else if we have node selections, deselect them all |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 157 | // (work already done) |
| 158 | |
| 159 | } else if (tls.deselectLink()) { |
| 160 | // else if we have a link selected, deselect it |
| 161 | // (work already done) |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 162 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 163 | } else if (tis.isVisible()) { |
| 164 | // else if the Instance Panel is visible, hide it |
| 165 | tis.hide(); |
| 166 | tfs.updateDeviceColors(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 167 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 168 | } else if (tps.summaryVisible()) { |
| 169 | // else if the Summary Panel is visible, hide it |
| 170 | tps.hideSummaryPanel(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 171 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 172 | } else { |
| 173 | // TODO: set hover mode to hoverModeNone |
Simon Hunt | 62c2f8f | 2015-02-19 14:19:10 -0800 | [diff] [blame] | 174 | // talk to Thomas about this: shouldn't it be done |
| 175 | // when we deselect the node (if tss.haveDetails()...) |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 176 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 177 | } |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 178 | |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 179 | // --- Toolbar Functions --------------------------------------------- |
| 180 | |
| 181 | function getActionEntry(key) { |
| 182 | var entry = actionMap[key]; |
| 183 | return fs.isA(entry) || [entry, '']; |
| 184 | } |
| 185 | |
| 186 | function setUpToolbar() { |
| 187 | ttbs.init({ |
| 188 | getActionEntry: getActionEntry |
| 189 | }); |
| 190 | ttbs.createToolbar(); |
| 191 | } |
| 192 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 193 | // --- Glyphs, Icons, and the like ----------------------------------- |
| 194 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 195 | function setUpDefs() { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 196 | defs = svg.append('defs'); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 197 | gs.loadDefs(defs); |
Simon Hunt | 0ee2868 | 2015-02-12 20:48:11 -0800 | [diff] [blame] | 198 | sus.loadGlowDefs(defs); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 202 | // --- Pan and Zoom -------------------------------------------------- |
| 203 | |
| 204 | // zoom enabled predicate. ev is a D3 source event. |
| 205 | function zoomEnabled(ev) { |
Simon Hunt | d552ee9 | 2015-04-02 17:06:35 -0700 | [diff] [blame] | 206 | return fs.isMobile() || (ev.metaKey || ev.altKey); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | function zoomCallback() { |
Simon Hunt | bb5e0d8 | 2015-04-16 14:25:46 -0700 | [diff] [blame] | 210 | var sc = zoomer.scale(), |
| 211 | tr = zoomer.translate(); |
| 212 | |
| 213 | ps.setPrefs('topo_zoom', {tx:tr[0], ty:tr[1], sc:sc}); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 214 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 215 | // keep the map lines constant width while zooming |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 216 | mapG.style('stroke-width', (2.0 / sc) + 'px'); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | function setUpZoom() { |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 220 | zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer'); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 221 | zoomer = zs.createZoomer({ |
| 222 | svg: svg, |
| 223 | zoomLayer: zoomLayer, |
| 224 | zoomEnabled: zoomEnabled, |
| 225 | zoomCallback: zoomCallback |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 230 | // callback invoked when the SVG view has been resized.. |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 231 | function svgResized(s) { |
| 232 | tfs.newDim([s.width, s.height]); |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 235 | // --- Background Map ------------------------------------------------ |
| 236 | |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 237 | function setUpNoDevs() { |
| 238 | var g, box; |
| 239 | noDevsLayer = svg.append('g').attr({ |
| 240 | id: 'topo-noDevsLayer', |
| 241 | transform: sus.translate(500,500) |
| 242 | }); |
| 243 | // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html. |
| 244 | // We are translating this layer to have its origin at the center |
| 245 | |
| 246 | g = noDevsLayer.append('g'); |
| 247 | gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird'); |
| 248 | g.append('text').text('No devices are connected') |
| 249 | .attr({ x: 120, y: 80}); |
| 250 | |
| 251 | box = g.node().getBBox(); |
| 252 | box.x -= box.width/2; |
| 253 | box.y -= box.height/2; |
| 254 | g.attr('transform', sus.translate(box.x, box.y)); |
| 255 | |
| 256 | showNoDevs(true); |
| 257 | } |
| 258 | |
| 259 | function showNoDevs(b) { |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 260 | sus.visible(noDevsLayer, b); |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 263 | function setUpMap() { |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 264 | mapG = zoomLayer.append('g').attr('id', 'topo-map'); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 265 | // returns a promise for the projection... |
| 266 | return ms.loadMapInto(mapG, '*continental_us'); |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 269 | function opacifyMap(b) { |
| 270 | mapG.transition() |
| 271 | .duration(1000) |
| 272 | .attr('opacity', b ? 1 : 0); |
| 273 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 274 | |
Simon Hunt | f9fc0e7 | 2015-04-16 15:10:57 -0700 | [diff] [blame] | 275 | function setUpSprites($loc, tspr) { |
| 276 | var s1 = $loc.search().sprites, |
| 277 | s2 = ps.getPrefs('topo_sprites'), |
| 278 | sprId = s1 || (s2 && s2.id); |
| 279 | |
| 280 | spriteG = zoomLayer.append ('g').attr('id', 'topo-sprites'); |
| 281 | if (sprId) { |
| 282 | ps.setPrefs('topo_sprites', {id:sprId}); |
| 283 | tspr.loadSprites(spriteG, defs, sprId); |
| 284 | } |
| 285 | } |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 286 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 287 | // --- User Preferemces ---------------------------------------------- |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 288 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 289 | var prefsState = {}; |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 290 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 291 | function updatePrefsState(what, b) { |
| 292 | prefsState[what] = b ? 1 : 0; |
| 293 | ps.setPrefs('topo_prefs', prefsState); |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 296 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 297 | function restoreConfigFromPrefs() { |
| 298 | // NOTE: toolbar will have set this for us.. |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 299 | prefsState = ps.asNumbers(ps.getPrefs('topo_prefs')); |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 300 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 301 | $log.debug('TOPO---- Prefs State:', prefsState); |
| 302 | |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 303 | flash.enable(false); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 304 | toggleInstances(prefsState.insts); |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 305 | toggleSummary(prefsState.summary); |
| 306 | toggleDetails(prefsState.detail); |
Simon Hunt | f9fc0e7 | 2015-04-16 15:10:57 -0700 | [diff] [blame] | 307 | toggleSprites(prefsState.spr); |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 308 | flash.enable(true); |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 312 | // --- Controller Definition ----------------------------------------- |
| 313 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 314 | angular.module('ovTopo', moduleDependencies) |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 315 | .controller('OvTopoCtrl', ['$scope', '$log', '$location', '$timeout', |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 316 | '$cookies', 'FnService', 'MastService', 'KeyService', 'ZoomService', |
Simon Hunt | 0e48c29 | 2015-02-19 16:11:37 -0800 | [diff] [blame] | 317 | 'GlyphService', 'MapService', 'SvgUtilService', 'FlashService', |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 318 | 'WebSocketService', 'PrefsService', |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 319 | 'TopoEventService', 'TopoForceService', 'TopoPanelService', |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 320 | 'TopoInstService', 'TopoSelectService', 'TopoLinkService', |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 321 | 'TopoTrafficService', 'TopoObliqueService', 'TopoFilterService', |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame^] | 322 | 'TopoToolbarService', 'TopoSpriteService', 'TooltipService', |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 323 | |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 324 | function ($scope, _$log_, $loc, $timeout, _$cookies_, _fs_, mast, _ks_, |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 325 | _zs_, _gs_, _ms_, _sus_, _flash_, _wss_, _ps_, _tes_, _tfs_, |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame^] | 326 | _tps_, _tis_, _tss_, _tls_, _tts_, _tos_, _fltr_, _ttbs_, tspr, |
| 327 | _ttip_) { |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 328 | var self = this, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 329 | projection, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 330 | dim, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 331 | uplink = { |
| 332 | // provides function calls back into this space |
| 333 | showNoDevs: showNoDevs, |
| 334 | projection: function () { return projection; }, |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 335 | zoomLayer: function () { return zoomLayer; }, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 336 | zoomer: function () { return zoomer; }, |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 337 | opacifyMap: opacifyMap |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 338 | }; |
| 339 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 340 | $log = _$log_; |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 341 | $cookies = _$cookies_; |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 342 | fs = _fs_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 343 | ks = _ks_; |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 344 | zs = _zs_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 345 | gs = _gs_; |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame] | 346 | ms = _ms_; |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 347 | sus = _sus_; |
Simon Hunt | 0e48c29 | 2015-02-19 16:11:37 -0800 | [diff] [blame] | 348 | flash = _flash_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 349 | wss = _wss_; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 350 | ps = _ps_; |
Simon Hunt | c252aa6 | 2015-02-10 16:45:39 -0800 | [diff] [blame] | 351 | tes = _tes_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 352 | tfs = _tfs_; |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 353 | // TODO: consider funnelling actions through TopoForceService... |
| 354 | // rather than injecting references to these 'sub-modules', |
| 355 | // just so we can invoke functions on them. |
Simon Hunt | c252aa6 | 2015-02-10 16:45:39 -0800 | [diff] [blame] | 356 | tps = _tps_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 357 | tis = _tis_; |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 358 | tss = _tss_; |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 359 | tls = _tls_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 360 | tts = _tts_; |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 361 | tos = _tos_; |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 362 | fltr = _fltr_; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 363 | ttbs = _ttbs_; |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame^] | 364 | ttip = _ttip_; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 365 | |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 366 | self.notifyResize = function () { |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 367 | svgResized(fs.windowSize(mast.mastHeight())); |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 368 | }; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 369 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 370 | // Cleanup on destroyed scope.. |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 371 | $scope.$on('$destroy', function () { |
| 372 | $log.log('OvTopoCtrl is saying Buh-Bye!'); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 373 | tes.stop(); |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 374 | tps.destroyPanels(); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 375 | tis.destroyInst(); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 376 | tfs.destroyForce(); |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 377 | ttbs.destroyToolbar(); |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame^] | 378 | ttip.resetTooltip(); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 379 | }); |
| 380 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 381 | // svg layer and initialization of components |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 382 | ovtopo = d3.select('#ov-topo'); |
| 383 | svg = ovtopo.select('svg'); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 384 | // set the svg size to match that of the window, less the masthead |
| 385 | svg.attr(fs.windowSize(mast.mastHeight())); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 386 | dim = [svg.attr('width'), svg.attr('height')]; |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 387 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 388 | setUpKeys(); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 389 | setUpToolbar(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 390 | setUpDefs(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 391 | setUpZoom(); |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 392 | setUpNoDevs(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 393 | setUpMap().then( |
| 394 | function (proj) { |
Simon Hunt | bb5e0d8 | 2015-04-16 14:25:46 -0700 | [diff] [blame] | 395 | var z = ps.getPrefs('topo_zoom') || {tx:0, ty:0, sc:1}; |
| 396 | zoomer.panZoom([z.tx, z.ty], z.sc); |
| 397 | $log.debug('** Zoom restored:', z); |
| 398 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 399 | projection = proj; |
Simon Hunt | bb5e0d8 | 2015-04-16 14:25:46 -0700 | [diff] [blame] | 400 | $log.debug('** We installed the projection:', proj); |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 401 | flash.enable(false); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 402 | toggleMap(prefsState.bg); |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 403 | flash.enable(true); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 404 | } |
| 405 | ); |
Simon Hunt | f9fc0e7 | 2015-04-16 15:10:57 -0700 | [diff] [blame] | 406 | setUpSprites($loc, tspr); |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 407 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 408 | forceG = zoomLayer.append('g').attr('id', 'topo-force'); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 409 | tfs.initForce(svg, forceG, uplink, dim); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 410 | tis.initInst({ showMastership: tfs.showMastership }); |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 411 | tps.initPanels(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 412 | tes.start(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 413 | |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 414 | // temporary solution for persisting user settings |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 415 | restoreConfigFromPrefs(); |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 416 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 417 | $log.log('OvTopoCtrl has been created'); |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 418 | }]); |
| 419 | }()); |