Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | /* |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 18 | ONOS network topology viewer - version 1.1 |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | |
| 23 | (function (onos) { |
| 24 | 'use strict'; |
| 25 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 26 | // shorter names for library APIs |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 27 | var d3u = onos.lib.d3util, |
| 28 | trace; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 29 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 30 | // configuration data |
| 31 | var config = { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 32 | useLiveData: true, |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 33 | fnTrace: true, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 34 | debugOn: false, |
| 35 | debug: { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 36 | showNodeXY: true, |
| 37 | showKeyHandler: false |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 38 | }, |
| 39 | options: { |
| 40 | layering: true, |
| 41 | collisionPrevention: true, |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 42 | showBackground: true |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 43 | }, |
| 44 | backgroundUrl: 'img/us-map.png', |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 45 | webSockUrl: 'ws/topology', |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 46 | data: { |
| 47 | live: { |
| 48 | jsonUrl: 'rs/topology/graph', |
| 49 | detailPrefix: 'rs/topology/graph/', |
| 50 | detailSuffix: '' |
| 51 | }, |
| 52 | fake: { |
| 53 | jsonUrl: 'json/network2.json', |
| 54 | detailPrefix: 'json/', |
| 55 | detailSuffix: '.json' |
| 56 | } |
| 57 | }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 58 | labels: { |
| 59 | imgPad: 16, |
| 60 | padLR: 4, |
| 61 | padTB: 3, |
| 62 | marginLR: 3, |
| 63 | marginTB: 2, |
| 64 | port: { |
| 65 | gap: 3, |
| 66 | width: 18, |
| 67 | height: 14 |
| 68 | } |
| 69 | }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 70 | topo: { |
| 71 | linkInColor: '#66f', |
| 72 | linkInWidth: 14 |
| 73 | }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 74 | icons: { |
| 75 | w: 28, |
| 76 | h: 28, |
| 77 | xoff: -12, |
| 78 | yoff: -8 |
| 79 | }, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 80 | iconUrl: { |
| 81 | device: 'img/device.png', |
| 82 | host: 'img/host.png', |
| 83 | pkt: 'img/pkt.png', |
| 84 | opt: 'img/opt.png' |
| 85 | }, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 86 | force: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 87 | note_for_links: 'link.type is used to differentiate', |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 88 | linkDistance: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 89 | direct: 100, |
| 90 | optical: 120, |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 91 | hostLink: 3 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 92 | }, |
| 93 | linkStrength: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 94 | direct: 1.0, |
| 95 | optical: 1.0, |
| 96 | hostLink: 1.0 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 97 | }, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 98 | note_for_nodes: 'node.class is used to differentiate', |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 99 | charge: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 100 | device: -8000, |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 101 | host: -5000 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 102 | }, |
| 103 | pad: 20, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 104 | translate: function() { |
| 105 | return 'translate(' + |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 106 | config.force.pad + ',' + |
| 107 | config.force.pad + ')'; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 108 | } |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 109 | }, |
| 110 | // see below in creation of viewBox on main svg |
| 111 | logicalSize: 1000 |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 114 | // radio buttons |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 115 | var layerButtons = [ |
| 116 | { text: 'All Layers', id: 'all', cb: showAllLayers }, |
| 117 | { text: 'Packet Only', id: 'pkt', cb: showPacketLayer }, |
| 118 | { text: 'Optical Only', id: 'opt', cb: showOpticalLayer } |
| 119 | ], |
| 120 | layerBtnSet, |
| 121 | layerBtnDispatch = { |
| 122 | all: showAllLayers, |
| 123 | pkt: showPacketLayer, |
| 124 | opt: showOpticalLayer |
| 125 | }; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 126 | |
| 127 | // key bindings |
| 128 | var keyDispatch = { |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 129 | M: testMe, // TODO: remove (testing only) |
| 130 | S: injectStartupEvents, // TODO: remove (testing only) |
| 131 | space: injectTestEvent, // TODO: remove (testing only) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 132 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 133 | B: toggleBg, |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 134 | L: cycleLabels, |
| 135 | P: togglePorts, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 136 | U: unpin, |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 137 | R: resetZoomPan, |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 138 | esc: handleEscape |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 139 | }; |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 140 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 141 | // state variables |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 142 | var network = { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 143 | view: null, // view token reference |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 144 | nodes: [], |
| 145 | links: [], |
| 146 | lookup: {} |
| 147 | }, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 148 | scenario = { |
| 149 | evDir: 'json/ev/', |
| 150 | evScenario: '/scenario.json', |
| 151 | evPrefix: '/ev_', |
| 152 | evOnos: '_onos.json', |
| 153 | evUi: '_ui.json', |
| 154 | ctx: null, |
| 155 | params: {}, |
| 156 | evNumber: 0, |
| 157 | view: null, |
| 158 | debug: false |
| 159 | }, |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 160 | webSock, |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 161 | sid = 0, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 162 | deviceLabelIndex = 0, |
| 163 | hostLabelIndex = 0, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 164 | selections = {}, |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 165 | selectOrder = [], |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 166 | hovered = null, |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 167 | detailPane, |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 168 | antTimer = null, |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 169 | onosInstances = {}, |
| 170 | onosOrder = [], |
| 171 | oiBox, |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 172 | oiShowMaster = false, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 173 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 174 | portLabelsOn = false; |
| 175 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 176 | // D3 selections |
| 177 | var svg, |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 178 | zoomPanContainer, |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 179 | bgImg, |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 180 | topoG, |
| 181 | nodeG, |
| 182 | linkG, |
| 183 | node, |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 184 | link, |
| 185 | mask; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 186 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 187 | // the projection for the map background |
| 188 | var geoMapProjection; |
| 189 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 190 | // the zoom function |
| 191 | var zoom; |
| 192 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 193 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 194 | // For Debugging / Development |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 195 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 196 | function note(label, msg) { |
| 197 | console.log('NOTE: ' + label + ': ' + msg); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 200 | function debug(what) { |
| 201 | return config.debugOn && config.debug[what]; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 204 | function fnTrace(msg, id) { |
| 205 | if (config.fnTrace) { |
| 206 | console.log('FN: ' + msg + ' [' + id + ']'); |
| 207 | } |
| 208 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 209 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 210 | function evTrace(data) { |
| 211 | fnTrace(data.event, data.payload.id); |
| 212 | } |
| 213 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 214 | // ============================== |
| 215 | // Key Callbacks |
| 216 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 217 | function testMe(view) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 218 | //view.alert('test'); |
| 219 | detailPane.show(); |
| 220 | setTimeout(detailPane.hide, 2000); |
| 221 | oiBox.show(); |
| 222 | setTimeout(oiBox.hide, 2000); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 225 | function abortIfLive() { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 226 | if (config.useLiveData) { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 227 | network.view.alert("Sorry, currently using live data.."); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 228 | return true; |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 229 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 230 | return false; |
| 231 | } |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 232 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 233 | function testDebug(msg) { |
| 234 | if (scenario.debug) { |
| 235 | scenario.view.alert(msg); |
| 236 | } |
| 237 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 238 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 239 | function injectTestEvent(view) { |
| 240 | if (abortIfLive()) { return; } |
| 241 | var sc = scenario, |
| 242 | evn = ++sc.evNumber, |
| 243 | pfx = sc.evDir + sc.ctx + sc.evPrefix + evn, |
| 244 | onosUrl = pfx + sc.evOnos, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 245 | uiUrl = pfx + sc.evUi, |
| 246 | stack = [ |
| 247 | { url: onosUrl, cb: handleServerEvent }, |
| 248 | { url: uiUrl, cb: handleUiEvent } |
| 249 | ]; |
| 250 | recurseFetchEvent(stack, evn); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 253 | function recurseFetchEvent(stack, evn) { |
| 254 | var v = scenario.view, |
| 255 | frame; |
| 256 | if (stack.length === 0) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 257 | v.alert('Oops!\n\nNo event #' + evn + ' found.'); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | frame = stack.shift(); |
| 261 | |
| 262 | d3.json(frame.url, function (err, data) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 263 | if (err) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 264 | if (err.status === 404) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 265 | // if we didn't find the data, try the next stack frame |
| 266 | recurseFetchEvent(stack, evn); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 267 | } else { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 268 | v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 269 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 270 | } else { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 271 | testDebug('loaded: ' + frame.url); |
| 272 | frame.cb(data); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 273 | } |
| 274 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 275 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 276 | } |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 277 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 278 | function handleUiEvent(data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 279 | scenario.view.alert('UI Tx: ' + data.event + '\n\n' + |
| 280 | JSON.stringify(data)); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | function injectStartupEvents(view) { |
| 284 | var last = scenario.params.lastAuto || 0; |
| 285 | if (abortIfLive()) { return; } |
| 286 | |
| 287 | while (scenario.evNumber < last) { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 288 | injectTestEvent(view); |
| 289 | } |
| 290 | } |
| 291 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 292 | function toggleBg() { |
| 293 | var vis = bgImg.style('visibility'); |
| 294 | bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden'); |
| 295 | } |
| 296 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 297 | function cycleLabels() { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 298 | deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1) |
| 299 | ? 0 : deviceLabelIndex + 1; |
Simon Hunt | 5f36d34 | 2014-11-08 21:33:14 -0800 | [diff] [blame] | 300 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 301 | network.nodes.forEach(function (d) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 302 | if (d.class === 'device') { |
| 303 | updateDeviceLabel(d); |
| 304 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 305 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | function togglePorts(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 309 | view.alert('togglePorts() callback') |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 312 | function unpin() { |
| 313 | if (hovered) { |
| 314 | hovered.fixed = false; |
| 315 | hovered.el.classed('fixed', false); |
| 316 | network.force.resume(); |
| 317 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 320 | function handleEscape(view) { |
| 321 | if (oiShowMaster) { |
| 322 | cancelAffinity(); |
| 323 | } else { |
| 324 | deselectAll(); |
| 325 | } |
| 326 | } |
| 327 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 328 | // ============================== |
| 329 | // Radio Button Callbacks |
| 330 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 331 | var layerLookup = { |
| 332 | host: { |
| 333 | endstation: 'pkt', // default, if host event does not define type |
| 334 | bgpSpeaker: 'pkt' |
| 335 | }, |
| 336 | device: { |
| 337 | switch: 'pkt', |
| 338 | roadm: 'opt' |
| 339 | }, |
| 340 | link: { |
| 341 | hostLink: 'pkt', |
| 342 | direct: 'pkt', |
| 343 | optical: 'opt' |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | function inLayer(d, layer) { |
| 348 | var look = layerLookup[d.class], |
| 349 | lyr = look && look[d.type]; |
| 350 | return lyr === layer; |
| 351 | } |
| 352 | |
| 353 | function unsuppressLayer(which) { |
| 354 | node.each(function (d) { |
| 355 | var node = d.el; |
| 356 | if (inLayer(d, which)) { |
| 357 | node.classed('suppressed', false); |
| 358 | } |
| 359 | }); |
| 360 | |
| 361 | link.each(function (d) { |
| 362 | var link = d.el; |
| 363 | if (inLayer(d, which)) { |
| 364 | link.classed('suppressed', false); |
| 365 | } |
| 366 | }); |
| 367 | } |
| 368 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 369 | function suppressLayers(b) { |
| 370 | node.classed('suppressed', b); |
| 371 | link.classed('suppressed', b); |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 372 | // d3.selectAll('svg .port').classed('inactive', false); |
| 373 | // d3.selectAll('svg .portText').classed('inactive', false); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 376 | function showAllLayers() { |
| 377 | suppressLayers(false); |
| 378 | } |
| 379 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 380 | function showPacketLayer() { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 381 | node.classed('suppressed', true); |
| 382 | link.classed('suppressed', true); |
| 383 | unsuppressLayer('pkt'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | function showOpticalLayer() { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 387 | node.classed('suppressed', true); |
| 388 | link.classed('suppressed', true); |
| 389 | unsuppressLayer('opt'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 392 | function restoreLayerState() { |
| 393 | layerBtnDispatch[layerBtnSet.selected()](); |
| 394 | } |
| 395 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 396 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 397 | // Private functions |
| 398 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 399 | function safeId(s) { |
| 400 | return s.replace(/[^a-z0-9]/gi, '-'); |
| 401 | } |
| 402 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 403 | // set the size of the given element to that of the view (reduced if padded) |
| 404 | function setSize(el, view, pad) { |
| 405 | var padding = pad ? pad * 2 : 0; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 406 | el.attr({ |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 407 | width: view.width() - padding, |
| 408 | height: view.height() - padding |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 409 | }); |
| 410 | } |
| 411 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 412 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 413 | // ============================== |
| 414 | // Event handlers for server-pushed events |
| 415 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 416 | function logicError(msg) { |
| 417 | // TODO, report logic error to server, via websock, so it can be logged |
| 418 | network.view.alert('Logic Error:\n\n' + msg); |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 419 | console.warn(msg); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 422 | var eventDispatch = { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 423 | addInstance: addInstance, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 424 | addDevice: addDevice, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 425 | addLink: addLink, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 426 | addHost: addHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 427 | |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 428 | updateInstance: stillToImplement, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 429 | updateDevice: updateDevice, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 430 | updateLink: updateLink, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 431 | updateHost: updateHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 432 | |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 433 | removeInstance: stillToImplement, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 434 | removeDevice: stillToImplement, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 435 | removeLink: removeLink, |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 436 | removeHost: removeHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 437 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 438 | showDetails: showDetails, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 439 | showPath: showPath, |
| 440 | showTraffic: showTraffic |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 441 | }; |
| 442 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 443 | function addInstance(data) { |
| 444 | evTrace(data); |
| 445 | var inst = data.payload, |
| 446 | id = inst.id; |
| 447 | if (onosInstances[id]) { |
| 448 | logicError('ONOS instance already added: ' + id); |
| 449 | return; |
| 450 | } |
| 451 | onosInstances[id] = inst; |
| 452 | onosOrder.push(inst); |
| 453 | updateInstances(); |
| 454 | } |
| 455 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 456 | function addDevice(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 457 | evTrace(data); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 458 | var device = data.payload, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 459 | nodeData = createDeviceNode(device); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 460 | network.nodes.push(nodeData); |
| 461 | network.lookup[nodeData.id] = nodeData; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 462 | updateNodes(); |
| 463 | network.force.start(); |
| 464 | } |
| 465 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 466 | function addLink(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 467 | evTrace(data); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 468 | var link = data.payload, |
| 469 | lnk = createLink(link); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 470 | if (lnk) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 471 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 472 | network.lookup[lnk.id] = lnk; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 473 | updateLinks(); |
| 474 | network.force.start(); |
| 475 | } |
| 476 | } |
| 477 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 478 | function addHost(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 479 | evTrace(data); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 480 | var host = data.payload, |
| 481 | node = createHostNode(host), |
| 482 | lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 483 | network.nodes.push(node); |
| 484 | network.lookup[host.id] = node; |
| 485 | updateNodes(); |
| 486 | |
| 487 | lnk = createHostLink(host); |
| 488 | if (lnk) { |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 489 | node.linkData = lnk; // cache ref on its host |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 490 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 491 | network.lookup[host.ingress] = lnk; |
| 492 | network.lookup[host.egress] = lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 493 | updateLinks(); |
| 494 | } |
| 495 | network.force.start(); |
| 496 | } |
| 497 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 498 | // TODO: fold updateX(...) methods into one base method; remove duplication |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 499 | function updateDevice(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 500 | evTrace(data); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 501 | var device = data.payload, |
| 502 | id = device.id, |
| 503 | nodeData = network.lookup[id]; |
| 504 | if (nodeData) { |
| 505 | $.extend(nodeData, device); |
| 506 | updateDeviceState(nodeData); |
| 507 | } else { |
| 508 | logicError('updateDevice lookup fail. ID = "' + id + '"'); |
| 509 | } |
| 510 | } |
| 511 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 512 | function updateLink(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 513 | evTrace(data); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 514 | var link = data.payload, |
| 515 | id = link.id, |
| 516 | linkData = network.lookup[id]; |
| 517 | if (linkData) { |
| 518 | $.extend(linkData, link); |
| 519 | updateLinkState(linkData); |
| 520 | } else { |
| 521 | logicError('updateLink lookup fail. ID = "' + id + '"'); |
| 522 | } |
| 523 | } |
| 524 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 525 | function updateHost(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 526 | evTrace(data); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 527 | var host = data.payload, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 528 | id = host.id, |
| 529 | hostData = network.lookup[id]; |
| 530 | if (hostData) { |
| 531 | $.extend(hostData, host); |
| 532 | updateHostState(hostData); |
| 533 | } else { |
| 534 | logicError('updateHost lookup fail. ID = "' + id + '"'); |
| 535 | } |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 538 | // TODO: fold removeX(...) methods into base method - remove dup code |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 539 | function removeLink(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 540 | evTrace(data); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 541 | var link = data.payload, |
| 542 | id = link.id, |
| 543 | linkData = network.lookup[id]; |
| 544 | if (linkData) { |
| 545 | removeLinkElement(linkData); |
| 546 | } else { |
| 547 | logicError('removeLink lookup fail. ID = "' + id + '"'); |
| 548 | } |
| 549 | } |
| 550 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 551 | function removeHost(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 552 | evTrace(data); |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 553 | var host = data.payload, |
| 554 | id = host.id, |
| 555 | hostData = network.lookup[id]; |
| 556 | if (hostData) { |
| 557 | removeHostElement(hostData); |
| 558 | } else { |
| 559 | logicError('removeHost lookup fail. ID = "' + id + '"'); |
| 560 | } |
| 561 | } |
| 562 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 563 | function showDetails(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 564 | evTrace(data); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 565 | populateDetails(data.payload); |
| 566 | detailPane.show(); |
| 567 | } |
| 568 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 569 | function showPath(data) { |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 570 | // TODO: review - making sure we are handling the payload correctly. |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 571 | evTrace(data); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 572 | var links = data.payload.links, |
| 573 | s = [ data.event + "\n" + links.length ]; |
| 574 | links.forEach(function (d, i) { |
| 575 | s.push(d); |
| 576 | }); |
| 577 | network.view.alert(s.join('\n')); |
| 578 | |
| 579 | links.forEach(function (d, i) { |
| 580 | var link = network.lookup[d]; |
| 581 | if (link) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 582 | link.el.classed('showPath', true); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 583 | } |
| 584 | }); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 587 | function showTraffic(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 588 | evTrace(data); |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 589 | var paths = data.payload.paths; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 590 | |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 591 | // Revert any links hilighted previously. |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 592 | link.classed('primary secondary animated optical', false); |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 593 | |
| 594 | // Now hilight all links in the paths payload. |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 595 | paths.forEach(function (p) { |
| 596 | var cls = p.class; |
| 597 | p.links.forEach(function (id) { |
| 598 | var lnk = network.lookup[id]; |
| 599 | if (lnk) { |
| 600 | lnk.el.classed(cls, true); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 601 | } |
| 602 | }); |
| 603 | }); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 606 | // ............................... |
| 607 | |
| 608 | function stillToImplement(data) { |
| 609 | var p = data.payload; |
| 610 | note(data.event, p.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 611 | network.view.alert('Not yet implemented: "' + data.event + '"'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 612 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 613 | |
| 614 | function unknownEvent(data) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 615 | network.view.alert('Unknown event type: "' + data.event + '"'); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | function handleServerEvent(data) { |
| 619 | var fn = eventDispatch[data.event] || unknownEvent; |
| 620 | fn(data); |
| 621 | } |
| 622 | |
| 623 | // ============================== |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 624 | // Out-going messages... |
| 625 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 626 | function userFeedback(msg) { |
| 627 | // for now, use the alert pane as is. Maybe different alert style in |
| 628 | // the future (centered on view; dismiss button?) |
| 629 | network.view.alert(msg); |
| 630 | } |
| 631 | |
| 632 | function nSel() { |
| 633 | return selectOrder.length; |
| 634 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 635 | function getSel(idx) { |
| 636 | return selections[selectOrder[idx]]; |
| 637 | } |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 638 | function getSelId(idx) { |
| 639 | return getSel(idx).obj.id; |
| 640 | } |
| 641 | function allSelectionsClass(cls) { |
| 642 | for (var i=0, n=nSel(); i<n; i++) { |
| 643 | if (getSel(i).obj.class !== cls) { |
| 644 | return false; |
| 645 | } |
| 646 | } |
| 647 | return true; |
| 648 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 649 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 650 | // request details for the selected element |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 651 | // invoked from selection of a single node. |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 652 | function requestDetails() { |
| 653 | var data = getSel(0).obj, |
| 654 | payload = { |
| 655 | id: data.id, |
| 656 | class: data.class |
| 657 | }; |
| 658 | sendMessage('requestDetails', payload); |
| 659 | } |
| 660 | |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 661 | function addIntentAction() { |
| 662 | sendMessage('addHostIntent', { |
| 663 | one: getSelId(0), |
| 664 | two: getSelId(1) |
| 665 | }); |
| 666 | } |
| 667 | |
| 668 | function showTrafficAction() { |
| 669 | // if nothing is hovered over, and nothing selected, send cancel request |
| 670 | if (!hovered && nSel() === 0) { |
| 671 | sendMessage('cancelTraffic', {}); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | // NOTE: hover is only populated if "show traffic on hover" is |
| 676 | // toggled on, and the item hovered is a host... |
| 677 | var hoverId = (trafficHover() && hovered && hovered.class === 'host') |
| 678 | ? hovered.id : ''; |
| 679 | sendMessage('requestTraffic', { |
| 680 | ids: selectOrder, |
| 681 | hover: hoverId |
| 682 | }); |
| 683 | } |
| 684 | |
| 685 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 686 | // ============================== |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 687 | // onos instance panel functions |
| 688 | |
| 689 | function updateInstances() { |
| 690 | var onoses = oiBox.el.selectAll('.onosInst') |
| 691 | .data(onosOrder, function (d) { return d.id; }); |
| 692 | |
| 693 | // operate on existing onoses if necessary |
| 694 | |
| 695 | var entering = onoses.enter() |
| 696 | .append('div') |
| 697 | .attr('class', 'onosInst') |
| 698 | .classed('online', function (d) { return d.online; }) |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 699 | .on('click', clickInst) |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 700 | .text(function (d) { return d.id; }); |
| 701 | |
| 702 | // operate on existing + new onoses here |
| 703 | |
| 704 | // the departed... |
| 705 | var exiting = onoses.exit() |
| 706 | .transition() |
| 707 | .style('opacity', 0) |
| 708 | .remove(); |
| 709 | } |
| 710 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 711 | function clickInst(d) { |
| 712 | var el = d3.select(this), |
| 713 | aff = el.classed('affinity'); |
| 714 | if (!aff) { |
| 715 | setAffinity(el, d); |
| 716 | } else { |
| 717 | cancelAffinity(); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | function setAffinity(el, d) { |
| 722 | d3.selectAll('.onosInst') |
| 723 | .classed('mastership', true) |
| 724 | .classed('affinity', false); |
| 725 | el.classed('affinity', true); |
| 726 | |
| 727 | suppressLayers(true); |
| 728 | node.each(function (n) { |
| 729 | if (n.master === d.id) { |
| 730 | n.el.classed('suppressed', false); |
| 731 | } |
| 732 | }); |
| 733 | oiShowMaster = true; |
| 734 | } |
| 735 | |
| 736 | function cancelAffinity() { |
| 737 | d3.selectAll('.onosInst') |
| 738 | .classed('mastership affinity', false); |
| 739 | restoreLayerState(); |
| 740 | oiShowMaster = false; |
| 741 | } |
| 742 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 743 | // ============================== |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 744 | // force layout modification functions |
| 745 | |
| 746 | function translate(x, y) { |
| 747 | return 'translate(' + x + ',' + y + ')'; |
| 748 | } |
| 749 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 750 | function missMsg(what, id) { |
| 751 | return '\n[' + what + '] "' + id + '" missing '; |
| 752 | } |
| 753 | |
| 754 | function linkEndPoints(srcId, dstId) { |
| 755 | var srcNode = network.lookup[srcId], |
| 756 | dstNode = network.lookup[dstId], |
| 757 | sMiss = !srcNode ? missMsg('src', srcId) : '', |
| 758 | dMiss = !dstNode ? missMsg('dst', dstId) : ''; |
| 759 | |
| 760 | if (sMiss || dMiss) { |
| 761 | logicError('Node(s) not on map for link:\n' + sMiss + dMiss); |
| 762 | return null; |
| 763 | } |
| 764 | return { |
| 765 | source: srcNode, |
| 766 | target: dstNode, |
| 767 | x1: srcNode.x, |
| 768 | y1: srcNode.y, |
| 769 | x2: dstNode.x, |
| 770 | y2: dstNode.y |
| 771 | }; |
| 772 | } |
| 773 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 774 | function createHostLink(host) { |
| 775 | var src = host.id, |
| 776 | dst = host.cp.device, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 777 | id = host.ingress, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 778 | lnk = linkEndPoints(src, dst); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 779 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 780 | if (!lnk) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 781 | return null; |
| 782 | } |
| 783 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 784 | // Synthesize link ... |
| 785 | $.extend(lnk, { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 786 | id: id, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 787 | class: 'link', |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 788 | type: 'hostLink', |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 789 | svgClass: 'link hostLink', |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 790 | linkWidth: 1 |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 791 | }); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 792 | return lnk; |
| 793 | } |
| 794 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 795 | function createLink(link) { |
| 796 | var lnk = linkEndPoints(link.src, link.dst), |
| 797 | type = link.type; |
| 798 | |
| 799 | if (!lnk) { |
| 800 | return null; |
| 801 | } |
| 802 | |
| 803 | // merge in remaining data |
| 804 | $.extend(lnk, link, { |
| 805 | class: 'link', |
| 806 | svgClass: type ? 'link ' + type : 'link' |
| 807 | }); |
| 808 | return lnk; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 811 | var widthRatio = 1.4, |
| 812 | linkScale = d3.scale.linear() |
| 813 | .domain([1, 12]) |
| 814 | .range([widthRatio, 12 * widthRatio]) |
| 815 | .clamp(true); |
| 816 | |
| 817 | function updateLinkWidth (d) { |
| 818 | // TODO: watch out for .showPath/.showTraffic classes |
| 819 | d.el.transition() |
| 820 | .duration(1000) |
| 821 | .attr('stroke-width', linkScale(d.linkWidth)); |
| 822 | } |
| 823 | |
| 824 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 825 | function updateLinks() { |
| 826 | link = linkG.selectAll('.link') |
| 827 | .data(network.links, function (d) { return d.id; }); |
| 828 | |
| 829 | // operate on existing links, if necessary |
| 830 | // link .foo() .bar() ... |
| 831 | |
| 832 | // operate on entering links: |
| 833 | var entering = link.enter() |
| 834 | .append('line') |
| 835 | .attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 836 | class: function (d) { return d.svgClass; }, |
| 837 | x1: function (d) { return d.x1; }, |
| 838 | y1: function (d) { return d.y1; }, |
| 839 | x2: function (d) { return d.x2; }, |
| 840 | y2: function (d) { return d.y2; }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 841 | stroke: config.topo.linkInColor, |
| 842 | 'stroke-width': config.topo.linkInWidth |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 843 | }) |
| 844 | .transition().duration(1000) |
| 845 | .attr({ |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 846 | 'stroke-width': function (d) { return linkScale(d.linkWidth); }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 847 | stroke: '#666' // TODO: remove explicit stroke, rather... |
| 848 | }); |
| 849 | |
| 850 | // augment links |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 851 | entering.each(function (d) { |
| 852 | var link = d3.select(this); |
| 853 | // provide ref to element selection from backing data.... |
| 854 | d.el = link; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 855 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 856 | // TODO: add src/dst port labels etc. |
| 857 | }); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 858 | |
| 859 | // operate on both existing and new links, if necessary |
| 860 | //link .foo() .bar() ... |
| 861 | |
| 862 | // operate on exiting links: |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 863 | link.exit() |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 864 | .attr({ |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 865 | 'stroke-dasharray': '3, 3' |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 866 | }) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 867 | .style('opacity', 0.4) |
| 868 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 869 | .duration(1500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 870 | .attr({ |
| 871 | 'stroke-dasharray': '3, 12' |
| 872 | }) |
| 873 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 874 | .duration(500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 875 | .style('opacity', 0.0) |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 876 | .remove(); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | function createDeviceNode(device) { |
| 880 | // start with the object as is |
| 881 | var node = device, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 882 | type = device.type, |
| 883 | svgCls = type ? 'node device ' + type : 'node device'; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 884 | |
| 885 | // Augment as needed... |
| 886 | node.class = 'device'; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 887 | node.svgClass = device.online ? svgCls + ' online' : svgCls; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 888 | positionNode(node); |
| 889 | |
| 890 | // cache label array length |
| 891 | network.deviceLabelCount = device.labels.length; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 892 | return node; |
| 893 | } |
| 894 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 895 | function createHostNode(host) { |
| 896 | // start with the object as is |
| 897 | var node = host; |
| 898 | |
| 899 | // Augment as needed... |
| 900 | node.class = 'host'; |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 901 | if (!node.type) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 902 | node.type = 'endstation'; |
| 903 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 904 | node.svgClass = 'node host'; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 905 | positionNode(node); |
| 906 | |
| 907 | // cache label array length |
| 908 | network.hostLabelCount = host.labels.length; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 909 | return node; |
| 910 | } |
| 911 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 912 | function positionNode(node) { |
| 913 | var meta = node.metaUi, |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 914 | x = meta && meta.x, |
| 915 | y = meta && meta.y, |
| 916 | xy; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 917 | |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 918 | // If we have [x,y] already, use that... |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 919 | if (x && y) { |
| 920 | node.fixed = true; |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 921 | node.x = x; |
| 922 | node.y = y; |
| 923 | return; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 924 | } |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 925 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 926 | var location = node.location; |
| 927 | if (location && location.type === 'latlng') { |
| 928 | var coord = geoMapProjection([location.lng, location.lat]); |
| 929 | node.fixed = true; |
| 930 | node.x = coord[0]; |
| 931 | node.y = coord[1]; |
| 932 | return; |
| 933 | } |
| 934 | |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 935 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 936 | // (center of the view) causes them to explode outwards when |
| 937 | // the force layout kicks in. So, we spread them out a bit |
| 938 | // initially, to provide a more serene layout convergence. |
| 939 | // Additionally, if the node is a host, we place it near |
| 940 | // the device it is connected to. |
| 941 | |
| 942 | function spread(s) { |
| 943 | return Math.floor((Math.random() * s) - s/2); |
| 944 | } |
| 945 | |
| 946 | function randDim(dim) { |
| 947 | return dim / 2 + spread(dim * 0.7071); |
| 948 | } |
| 949 | |
| 950 | function rand() { |
| 951 | return { |
| 952 | x: randDim(network.view.width()), |
| 953 | y: randDim(network.view.height()) |
| 954 | }; |
| 955 | } |
| 956 | |
| 957 | function near(node) { |
| 958 | var min = 12, |
| 959 | dx = spread(12), |
| 960 | dy = spread(12); |
| 961 | return { |
| 962 | x: node.x + min + dx, |
| 963 | y: node.y + min + dy |
| 964 | }; |
| 965 | } |
| 966 | |
| 967 | function getDevice(cp) { |
| 968 | var d = network.lookup[cp.device]; |
| 969 | return d || rand(); |
| 970 | } |
| 971 | |
| 972 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
| 973 | $.extend(node, xy); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 974 | } |
| 975 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 976 | function iconUrl(d) { |
| 977 | return 'img/' + d.type + '.png'; |
| 978 | } |
| 979 | |
| 980 | // returns the newly computed bounding box of the rectangle |
| 981 | function adjustRectToFitText(n) { |
| 982 | var text = n.select('text'), |
| 983 | box = text.node().getBBox(), |
| 984 | lab = config.labels; |
| 985 | |
| 986 | text.attr('text-anchor', 'middle') |
| 987 | .attr('y', '-0.8em') |
| 988 | .attr('x', lab.imgPad/2); |
| 989 | |
| 990 | // translate the bbox so that it is centered on [x,y] |
| 991 | box.x = -box.width / 2; |
| 992 | box.y = -box.height / 2; |
| 993 | |
| 994 | // add padding |
| 995 | box.x -= (lab.padLR + lab.imgPad/2); |
| 996 | box.width += lab.padLR * 2 + lab.imgPad; |
| 997 | box.y -= lab.padTB; |
| 998 | box.height += lab.padTB * 2; |
| 999 | |
| 1000 | return box; |
| 1001 | } |
| 1002 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1003 | function mkSvgClass(d) { |
| 1004 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 1005 | } |
| 1006 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1007 | function hostLabel(d) { |
| 1008 | var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0; |
| 1009 | return d.labels[idx]; |
| 1010 | } |
| 1011 | function deviceLabel(d) { |
| 1012 | var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0; |
| 1013 | return d.labels[idx]; |
| 1014 | } |
| 1015 | function niceLabel(label) { |
| 1016 | return (label && label.trim()) ? label : '.'; |
| 1017 | } |
| 1018 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1019 | function updateDeviceLabel(d) { |
| 1020 | var label = niceLabel(deviceLabel(d)), |
| 1021 | node = d.el, |
| 1022 | box; |
| 1023 | |
| 1024 | node.select('text') |
| 1025 | .text(label) |
| 1026 | .style('opacity', 0) |
| 1027 | .transition() |
| 1028 | .style('opacity', 1); |
| 1029 | |
| 1030 | box = adjustRectToFitText(node); |
| 1031 | |
| 1032 | node.select('rect') |
| 1033 | .transition() |
| 1034 | .attr(box); |
| 1035 | |
| 1036 | node.select('image') |
| 1037 | .transition() |
| 1038 | .attr('x', box.x + config.icons.xoff) |
| 1039 | .attr('y', box.y + config.icons.yoff); |
| 1040 | } |
| 1041 | |
| 1042 | function updateHostLabel(d) { |
| 1043 | var label = hostLabel(d), |
| 1044 | host = d.el; |
| 1045 | |
| 1046 | host.select('text').text(label); |
| 1047 | } |
| 1048 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1049 | function updateDeviceState(nodeData) { |
| 1050 | nodeData.el.classed('online', nodeData.online); |
| 1051 | updateDeviceLabel(nodeData); |
| 1052 | // TODO: review what else might need to be updated |
| 1053 | } |
| 1054 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1055 | function updateLinkState(linkData) { |
| 1056 | updateLinkWidth(linkData); |
| 1057 | // TODO: review what else might need to be updated |
| 1058 | // update label, if showing |
| 1059 | } |
| 1060 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1061 | function updateHostState(hostData) { |
| 1062 | updateHostLabel(hostData); |
| 1063 | // TODO: review what else might need to be updated |
| 1064 | } |
| 1065 | |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1066 | function nodeMouseOver(d) { |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1067 | hovered = d; |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1068 | if (trafficHover() && d.class === 'host') { |
| 1069 | showTrafficAction(); |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | function nodeMouseOut(d) { |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1074 | hovered = null; |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1075 | if (trafficHover() && d.class === 'host') { |
| 1076 | showTrafficAction(); |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1077 | } |
| 1078 | } |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1079 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1080 | function updateNodes() { |
| 1081 | node = nodeG.selectAll('.node') |
| 1082 | .data(network.nodes, function (d) { return d.id; }); |
| 1083 | |
| 1084 | // operate on existing nodes, if necessary |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1085 | // update host labels |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1086 | //node .foo() .bar() ... |
| 1087 | |
| 1088 | // operate on entering nodes: |
| 1089 | var entering = node.enter() |
| 1090 | .append('g') |
| 1091 | .attr({ |
| 1092 | id: function (d) { return safeId(d.id); }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1093 | class: mkSvgClass, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1094 | transform: function (d) { return translate(d.x, d.y); }, |
| 1095 | opacity: 0 |
| 1096 | }) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1097 | .call(network.drag) |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1098 | .on('mouseover', nodeMouseOver) |
| 1099 | .on('mouseout', nodeMouseOut) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1100 | .transition() |
| 1101 | .attr('opacity', 1); |
| 1102 | |
| 1103 | // augment device nodes... |
| 1104 | entering.filter('.device').each(function (d) { |
| 1105 | var node = d3.select(this), |
| 1106 | icon = iconUrl(d), |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1107 | label = niceLabel(deviceLabel(d)), |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1108 | box; |
| 1109 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1110 | // provide ref to element from backing data.... |
| 1111 | d.el = node; |
| 1112 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1113 | node.append('rect') |
| 1114 | .attr({ |
| 1115 | 'rx': 5, |
| 1116 | 'ry': 5 |
| 1117 | }); |
| 1118 | |
| 1119 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1120 | .text(label) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1121 | .attr('dy', '1.1em'); |
| 1122 | |
| 1123 | box = adjustRectToFitText(node); |
| 1124 | |
| 1125 | node.select('rect') |
| 1126 | .attr(box); |
| 1127 | |
| 1128 | if (icon) { |
| 1129 | var cfg = config.icons; |
| 1130 | node.append('svg:image') |
| 1131 | .attr({ |
| 1132 | x: box.x + config.icons.xoff, |
| 1133 | y: box.y + config.icons.yoff, |
| 1134 | width: cfg.w, |
| 1135 | height: cfg.h, |
| 1136 | 'xlink:href': icon |
| 1137 | }); |
| 1138 | } |
| 1139 | |
| 1140 | // debug function to show the modelled x,y coordinates of nodes... |
| 1141 | if (debug('showNodeXY')) { |
| 1142 | node.select('rect').attr('fill-opacity', 0.5); |
| 1143 | node.append('circle') |
| 1144 | .attr({ |
| 1145 | class: 'debug', |
| 1146 | cx: 0, |
| 1147 | cy: 0, |
| 1148 | r: '3px' |
| 1149 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1150 | } |
| 1151 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1152 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1153 | // augment host nodes... |
| 1154 | entering.filter('.host').each(function (d) { |
| 1155 | var node = d3.select(this), |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1156 | box; |
| 1157 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1158 | // provide ref to element from backing data.... |
| 1159 | d.el = node; |
| 1160 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1161 | node.append('circle') |
| 1162 | .attr('r', 8); // TODO: define host circle radius |
| 1163 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1164 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1165 | .text(hostLabel) |
| 1166 | .attr('dy', '1.3em') |
| 1167 | .attr('text-anchor', 'middle'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1168 | |
| 1169 | // debug function to show the modelled x,y coordinates of nodes... |
| 1170 | if (debug('showNodeXY')) { |
| 1171 | node.select('circle').attr('fill-opacity', 0.5); |
| 1172 | node.append('circle') |
| 1173 | .attr({ |
| 1174 | class: 'debug', |
| 1175 | cx: 0, |
| 1176 | cy: 0, |
| 1177 | r: '3px' |
| 1178 | }); |
| 1179 | } |
| 1180 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1181 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1182 | // operate on both existing and new nodes, if necessary |
| 1183 | //node .foo() .bar() ... |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1184 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1185 | // operate on exiting nodes: |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1186 | // Note that the node is removed after 2 seconds. |
| 1187 | // Sub element animations should be shorter than 2 seconds. |
| 1188 | var exiting = node.exit() |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 1189 | .transition() |
| 1190 | .duration(2000) |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1191 | .style('opacity', 0) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1192 | .remove(); |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1193 | |
| 1194 | // host node exits.... |
| 1195 | exiting.filter('.host').each(function (d) { |
| 1196 | var node = d3.select(this); |
| 1197 | |
| 1198 | node.select('text') |
| 1199 | .style('opacity', 0.5) |
| 1200 | .transition() |
| 1201 | .duration(1000) |
| 1202 | .style('opacity', 0); |
| 1203 | // note, leave <g>.remove to remove this element |
| 1204 | |
| 1205 | node.select('circle') |
| 1206 | .style('stroke-fill', '#555') |
| 1207 | .style('fill', '#888') |
| 1208 | .style('opacity', 0.5) |
| 1209 | .transition() |
| 1210 | .duration(1500) |
| 1211 | .attr('r', 0); |
| 1212 | // note, leave <g>.remove to remove this element |
| 1213 | |
| 1214 | }); |
| 1215 | |
| 1216 | // TODO: device node exits |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1219 | function find(id, array) { |
| 1220 | for (var idx = 0, n = array.length; idx < n; idx++) { |
| 1221 | if (array[idx].id === id) { |
| 1222 | return idx; |
| 1223 | } |
| 1224 | } |
| 1225 | return -1; |
| 1226 | } |
| 1227 | |
| 1228 | function removeLinkElement(linkData) { |
| 1229 | // remove from lookup cache |
| 1230 | delete network.lookup[linkData.id]; |
| 1231 | // remove from links array |
| 1232 | var idx = find(linkData.id, network.links); |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 1233 | network.links.splice(idx, 1); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1234 | // remove from SVG |
| 1235 | updateLinks(); |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 1236 | network.force.resume(); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1237 | } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1238 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 1239 | function removeHostElement(hostData) { |
| 1240 | // first, remove associated hostLink... |
| 1241 | removeLinkElement(hostData.linkData); |
| 1242 | |
| 1243 | // remove from lookup cache |
| 1244 | delete network.lookup[hostData.id]; |
| 1245 | // remove from nodes array |
| 1246 | var idx = find(hostData.id, network.nodes); |
| 1247 | network.nodes.splice(idx, 1); |
| 1248 | // remove from SVG |
| 1249 | updateNodes(); |
| 1250 | network.force.resume(); |
| 1251 | } |
| 1252 | |
| 1253 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1254 | function tick() { |
| 1255 | node.attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1256 | transform: function (d) { return translate(d.x, d.y); } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1257 | }); |
| 1258 | |
| 1259 | link.attr({ |
| 1260 | x1: function (d) { return d.source.x; }, |
| 1261 | y1: function (d) { return d.source.y; }, |
| 1262 | x2: function (d) { return d.target.x; }, |
| 1263 | y2: function (d) { return d.target.y; } |
| 1264 | }); |
| 1265 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1266 | |
| 1267 | // ============================== |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1268 | // Web-Socket for live data |
| 1269 | |
| 1270 | function webSockUrl() { |
| 1271 | return document.location.toString() |
| 1272 | .replace(/\#.*/, '') |
| 1273 | .replace('http://', 'ws://') |
| 1274 | .replace('https://', 'wss://') |
| 1275 | .replace('index2.html', config.webSockUrl); |
| 1276 | } |
| 1277 | |
| 1278 | webSock = { |
| 1279 | ws : null, |
| 1280 | |
| 1281 | connect : function() { |
| 1282 | webSock.ws = new WebSocket(webSockUrl()); |
| 1283 | |
| 1284 | webSock.ws.onopen = function() { |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1285 | noWebSock(false); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1286 | }; |
| 1287 | |
| 1288 | webSock.ws.onmessage = function(m) { |
| 1289 | if (m.data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1290 | wsTraceRx(m.data); |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 1291 | handleServerEvent(JSON.parse(m.data)); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1292 | } |
| 1293 | }; |
| 1294 | |
| 1295 | webSock.ws.onclose = function(m) { |
| 1296 | webSock.ws = null; |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1297 | noWebSock(true); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1298 | }; |
| 1299 | }, |
| 1300 | |
| 1301 | send : function(text) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1302 | if (text != null) { |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1303 | webSock._send(text); |
| 1304 | } |
| 1305 | }, |
| 1306 | |
| 1307 | _send : function(message) { |
| 1308 | if (webSock.ws) { |
| 1309 | webSock.ws.send(message); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1310 | } else if (config.useLiveData) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1311 | network.view.alert('no web socket open\n\n' + message); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1312 | } else { |
| 1313 | console.log('WS Send: ' + JSON.stringify(message)); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | }; |
| 1318 | |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1319 | function noWebSock(b) { |
| 1320 | mask.style('display',b ? 'block' : 'none'); |
| 1321 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1322 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1323 | // TODO: use cache of pending messages (key = sid) to reconcile responses |
| 1324 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1325 | function sendMessage(evType, payload) { |
| 1326 | var toSend = { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1327 | event: evType, |
| 1328 | sid: ++sid, |
| 1329 | payload: payload |
| 1330 | }, |
| 1331 | asText = JSON.stringify(toSend); |
| 1332 | wsTraceTx(asText); |
| 1333 | webSock.send(asText); |
| 1334 | } |
| 1335 | |
| 1336 | function wsTraceTx(msg) { |
| 1337 | wsTrace('tx', msg); |
| 1338 | } |
| 1339 | function wsTraceRx(msg) { |
| 1340 | wsTrace('rx', msg); |
| 1341 | } |
| 1342 | function wsTrace(rxtx, msg) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1343 | console.log('[' + rxtx + '] ' + msg); |
| 1344 | // TODO: integrate with trace view |
| 1345 | //if (trace) { |
| 1346 | // trace.output(rxtx, msg); |
| 1347 | //} |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | // ============================== |
| 1352 | // Selection stuff |
| 1353 | |
| 1354 | function selectObject(obj, el) { |
| 1355 | var n, |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1356 | srcEv = d3.event.sourceEvent, |
| 1357 | meta = srcEv.metaKey, |
| 1358 | shift = srcEv.shiftKey; |
| 1359 | |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1360 | if ((panZoom() && !meta) || (!panZoom() && meta)) { |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1361 | return; |
| 1362 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1363 | |
| 1364 | if (el) { |
| 1365 | n = d3.select(el); |
| 1366 | } else { |
| 1367 | node.each(function(d) { |
| 1368 | if (d == obj) { |
| 1369 | n = d3.select(el = this); |
| 1370 | } |
| 1371 | }); |
| 1372 | } |
| 1373 | if (!n) return; |
| 1374 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1375 | if (shift && n.classed('selected')) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1376 | deselectObject(obj.id); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1377 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1378 | return; |
| 1379 | } |
| 1380 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1381 | if (!shift) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1382 | deselectAll(); |
| 1383 | } |
| 1384 | |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1385 | selections[obj.id] = { obj: obj, el: el }; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1386 | selectOrder.push(obj.id); |
| 1387 | |
| 1388 | n.classed('selected', true); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1389 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | function deselectObject(id) { |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1393 | var obj = selections[id], |
| 1394 | idx; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1395 | if (obj) { |
| 1396 | d3.select(obj.el).classed('selected', false); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1397 | delete selections[id]; |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1398 | idx = $.inArray(id, selectOrder); |
| 1399 | if (idx >= 0) { |
| 1400 | selectOrder.splice(idx, 1); |
| 1401 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1402 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | function deselectAll() { |
| 1406 | // deselect all nodes in the network... |
| 1407 | node.classed('selected', false); |
| 1408 | selections = {}; |
| 1409 | selectOrder = []; |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1410 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1411 | } |
| 1412 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1413 | // update the state of the detail pane, based on current selections |
| 1414 | function updateDetailPane() { |
| 1415 | var nSel = selectOrder.length; |
| 1416 | if (!nSel) { |
| 1417 | detailPane.hide(); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1418 | showTrafficAction(); // sends cancelTraffic event |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1419 | } else if (nSel === 1) { |
| 1420 | singleSelect(); |
| 1421 | } else { |
| 1422 | multiSelect(); |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | function singleSelect() { |
| 1427 | requestDetails(); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1428 | // NOTE: detail pane will be shown from showDetails event callback |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | function multiSelect() { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1432 | populateMultiSelect(); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | function addSep(tbody) { |
| 1436 | var tr = tbody.append('tr'); |
| 1437 | $('<hr>').appendTo(tr.append('td').attr('colspan', 2)); |
| 1438 | } |
| 1439 | |
| 1440 | function addProp(tbody, label, value) { |
| 1441 | var tr = tbody.append('tr'); |
| 1442 | |
| 1443 | tr.append('td') |
| 1444 | .attr('class', 'label') |
| 1445 | .text(label + ' :'); |
| 1446 | |
| 1447 | tr.append('td') |
| 1448 | .attr('class', 'value') |
| 1449 | .text(value); |
| 1450 | } |
| 1451 | |
| 1452 | function populateMultiSelect() { |
| 1453 | detailPane.empty(); |
| 1454 | |
| 1455 | var title = detailPane.append("h2"), |
| 1456 | table = detailPane.append("table"), |
| 1457 | tbody = table.append("tbody"); |
| 1458 | |
| 1459 | title.text('Multi-Select...'); |
| 1460 | |
| 1461 | selectOrder.forEach(function (d, i) { |
| 1462 | addProp(tbody, i+1, d); |
| 1463 | }); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1464 | |
| 1465 | addMultiSelectActions(); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | function populateDetails(data) { |
| 1469 | detailPane.empty(); |
| 1470 | |
| 1471 | var title = detailPane.append("h2"), |
| 1472 | table = detailPane.append("table"), |
| 1473 | tbody = table.append("tbody"); |
| 1474 | |
| 1475 | $('<img src="img/' + data.type + '.png">').appendTo(title); |
| 1476 | $('<span>').attr('class', 'icon').text(data.id).appendTo(title); |
| 1477 | |
| 1478 | data.propOrder.forEach(function(p) { |
| 1479 | if (p === '-') { |
| 1480 | addSep(tbody); |
| 1481 | } else { |
| 1482 | addProp(tbody, p, data.props[p]); |
| 1483 | } |
| 1484 | }); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1485 | |
| 1486 | addSingleSelectActions(); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1487 | } |
| 1488 | |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1489 | function addSingleSelectActions() { |
| 1490 | detailPane.append('hr'); |
| 1491 | // always want to allow 'show traffic' |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1492 | addAction(detailPane, 'Show Traffic', showTrafficAction); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | function addMultiSelectActions() { |
| 1496 | detailPane.append('hr'); |
| 1497 | // always want to allow 'show traffic' |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1498 | addAction(detailPane, 'Show Traffic', showTrafficAction); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1499 | // if exactly two hosts are selected, also want 'add host intent' |
| 1500 | if (nSel() === 2 && allSelectionsClass('host')) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1501 | addAction(detailPane, 'Add Host Intent', addIntentAction); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1505 | function addAction(panel, text, cb) { |
| 1506 | panel.append('div') |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1507 | .classed('actionBtn', true) |
| 1508 | .text(text) |
| 1509 | .on('click', cb); |
| 1510 | } |
| 1511 | |
| 1512 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1513 | function zoomPan(scale, translate) { |
| 1514 | zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")"); |
| 1515 | // keep the map lines constant width while zooming |
| 1516 | bgImg.style("stroke-width", 2.0 / scale + "px"); |
| 1517 | } |
| 1518 | |
| 1519 | function resetZoomPan() { |
| 1520 | zoomPan(1, [0,0]); |
| 1521 | zoom.scale(1).translate([0,0]); |
| 1522 | } |
| 1523 | |
| 1524 | function setupZoomPan() { |
| 1525 | function zoomed() { |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1526 | if (!panZoom() ^ !d3.event.sourceEvent.metaKey) { |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1527 | zoomPan(d3.event.scale, d3.event.translate); |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | zoom = d3.behavior.zoom() |
| 1532 | .translate([0, 0]) |
| 1533 | .scale(1) |
| 1534 | .scaleExtent([1, 8]) |
| 1535 | .on("zoom", zoomed); |
| 1536 | |
| 1537 | svg.call(zoom); |
| 1538 | } |
| 1539 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1540 | // ============================== |
| 1541 | // Test harness code |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1542 | |
| 1543 | function prepareScenario(view, ctx, dbg) { |
| 1544 | var sc = scenario, |
| 1545 | urlSc = sc.evDir + ctx + sc.evScenario; |
| 1546 | |
| 1547 | if (!ctx) { |
| 1548 | view.alert("No scenario specified (null ctx)"); |
| 1549 | return; |
| 1550 | } |
| 1551 | |
| 1552 | sc.view = view; |
| 1553 | sc.ctx = ctx; |
| 1554 | sc.debug = dbg; |
| 1555 | sc.evNumber = 0; |
| 1556 | |
| 1557 | d3.json(urlSc, function(err, data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1558 | var p = data && data.params || {}, |
| 1559 | desc = data && data.description || null, |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 1560 | intro = data && data.title; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1561 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1562 | if (err) { |
| 1563 | view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err); |
| 1564 | } else { |
| 1565 | sc.params = p; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1566 | if (desc) { |
| 1567 | intro += '\n\n ' + desc.join('\n '); |
| 1568 | } |
| 1569 | view.alert(intro); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1570 | } |
| 1571 | }); |
| 1572 | |
| 1573 | } |
| 1574 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1575 | // ============================== |
| 1576 | // Toggle Buttons in masthead |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1577 | |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1578 | // TODO: toggle button (and other widgets in the masthead) should be provided |
| 1579 | // by the framework; not generated by the view. |
| 1580 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1581 | var showInstances, |
| 1582 | doPanZoom, |
| 1583 | showTrafficOnHover; |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1584 | |
| 1585 | function addButtonBar(view) { |
| 1586 | var bb = d3.select('#mast') |
| 1587 | .append('span').classed('right', true).attr('id', 'bb'); |
| 1588 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1589 | function mkTogBtn(text, cb) { |
| 1590 | return bb.append('span') |
| 1591 | .classed('btn', true) |
| 1592 | .text(text) |
| 1593 | .on('click', cb); |
| 1594 | } |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1595 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1596 | showInstances = mkTogBtn('Show Instances', toggleInst); |
| 1597 | doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom); |
| 1598 | showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover); |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1601 | function instShown() { |
| 1602 | return showInstances.classed('active'); |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1603 | } |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1604 | function toggleInst() { |
| 1605 | showInstances.classed('active', !instShown()); |
| 1606 | if (instShown()) { |
| 1607 | oiBox.show(); |
| 1608 | } else { |
| 1609 | oiBox.hide(); |
| 1610 | } |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1611 | } |
| 1612 | |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1613 | function panZoom() { |
| 1614 | return doPanZoom.classed('active'); |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1615 | } |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1616 | function togglePanZoom() { |
| 1617 | doPanZoom.classed('active', !panZoom()); |
| 1618 | } |
| 1619 | |
| 1620 | function trafficHover() { |
| 1621 | return showTrafficOnHover.classed('active'); |
| 1622 | } |
| 1623 | function toggleTrafficHover() { |
| 1624 | showTrafficOnHover.classed('active', !trafficHover()); |
| 1625 | } |
| 1626 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1627 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1628 | // ============================== |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1629 | // View life-cycle callbacks |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1630 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1631 | function preload(view, ctx, flags) { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1632 | var w = view.width(), |
| 1633 | h = view.height(), |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1634 | fcfg = config.force, |
| 1635 | fpad = fcfg.pad, |
| 1636 | forceDim = [w - 2*fpad, h - 2*fpad]; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1637 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1638 | // NOTE: view.$div is a D3 selection of the view's div |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1639 | var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize; |
| 1640 | svg = view.$div.append('svg').attr('viewBox', viewBox); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1641 | setSize(svg, view); |
| 1642 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1643 | zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer'); |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1644 | setupZoomPan(); |
| 1645 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1646 | // add blue glow filter to svg layer |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1647 | d3u.appendGlow(zoomPanContainer); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1648 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1649 | // group for the topology |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1650 | topoG = zoomPanContainer.append('g') |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1651 | .attr('id', 'topo-G') |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1652 | .attr('transform', fcfg.translate()); |
| 1653 | |
| 1654 | // subgroups for links and nodes |
| 1655 | linkG = topoG.append('g').attr('id', 'links'); |
| 1656 | nodeG = topoG.append('g').attr('id', 'nodes'); |
| 1657 | |
| 1658 | // selection of nodes and links |
| 1659 | link = linkG.selectAll('.link'); |
| 1660 | node = nodeG.selectAll('.node'); |
| 1661 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1662 | function chrg(d) { |
| 1663 | return fcfg.charge[d.class] || -12000; |
| 1664 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1665 | function ldist(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1666 | return fcfg.linkDistance[d.type] || 50; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1667 | } |
| 1668 | function lstrg(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1669 | // 0.0 - 1.0 |
| 1670 | return fcfg.linkStrength[d.type] || 1.0; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1673 | function selectCb(d, self) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1674 | selectObject(d, self); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | function atDragEnd(d, self) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1678 | // once we've finished moving, pin the node in position |
| 1679 | d.fixed = true; |
| 1680 | d3.select(self).classed('fixed', true); |
| 1681 | if (config.useLiveData) { |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1682 | sendUpdateMeta(d); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1683 | } else { |
| 1684 | console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']'); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1688 | function sendUpdateMeta(d) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1689 | sendMessage('updateMeta', { |
| 1690 | id: d.id, |
| 1691 | 'class': d.class, |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1692 | 'memento': { |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1693 | x: d.x, |
| 1694 | y: d.y |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1695 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1696 | }); |
| 1697 | } |
| 1698 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1699 | // set up the force layout |
| 1700 | network.force = d3.layout.force() |
| 1701 | .size(forceDim) |
| 1702 | .nodes(network.nodes) |
| 1703 | .links(network.links) |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1704 | .gravity(0.4) |
| 1705 | .friction(0.7) |
| 1706 | .charge(chrg) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1707 | .linkDistance(ldist) |
| 1708 | .linkStrength(lstrg) |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1709 | .on('tick', tick); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1710 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1711 | network.drag = d3u.createDragBehavior(network.force, |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1712 | selectCb, atDragEnd, panZoom); |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1713 | |
| 1714 | // create mask layer for when we lose connection to server. |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1715 | // TODO: this should be part of the framework |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1716 | mask = view.$div.append('div').attr('id','topo-mask'); |
| 1717 | para(mask, 'Oops!'); |
| 1718 | para(mask, 'Web-socket connection to server closed...'); |
| 1719 | para(mask, 'Try refreshing the page.'); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1720 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1721 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1722 | function para(sel, text) { |
| 1723 | sel.append('p').text(text); |
| 1724 | } |
| 1725 | |
| 1726 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1727 | function load(view, ctx, flags) { |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1728 | // resize, in case the window was resized while we were not loaded |
| 1729 | resize(view, ctx, flags); |
| 1730 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1731 | // cache the view token, so network topo functions can access it |
| 1732 | network.view = view; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1733 | config.useLiveData = !flags.local; |
| 1734 | |
| 1735 | if (!config.useLiveData) { |
| 1736 | prepareScenario(view, ctx, flags.debug); |
| 1737 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1738 | |
| 1739 | // set our radio buttons and key bindings |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 1740 | layerBtnSet = view.setRadio(layerButtons); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1741 | view.setKeys(keyDispatch); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1742 | |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1743 | // patch in our "button bar" for now |
| 1744 | // TODO: implement a more official frameworky way of doing this.. |
| 1745 | addButtonBar(view); |
| 1746 | |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1747 | // Load map data asynchronously; complete startup after that.. |
| 1748 | loadGeoJsonData(); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1749 | |
| 1750 | // start the and timer |
| 1751 | var dashIdx = 0; |
| 1752 | antTimer = setInterval(function () { |
| 1753 | // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link |
| 1754 | dashIdx = dashIdx === 0 ? 14 : dashIdx - 2; |
| 1755 | d3.selectAll('.animated').style('stroke-dashoffset', dashIdx); |
| 1756 | }, 35); |
| 1757 | } |
| 1758 | |
| 1759 | function unload(view, ctx, flags) { |
| 1760 | if (antTimer) { |
| 1761 | clearInterval(antTimer); |
| 1762 | antTimer = null; |
| 1763 | } |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | // TODO: move these to config/state portion of script |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1767 | var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1768 | geoJson; |
| 1769 | |
| 1770 | function loadGeoJsonData() { |
| 1771 | d3.json(geoJsonUrl, function (err, data) { |
| 1772 | if (err) { |
| 1773 | // fall back to USA map background |
| 1774 | loadStaticMap(); |
| 1775 | } else { |
| 1776 | geoJson = data; |
| 1777 | loadGeoMap(); |
| 1778 | } |
| 1779 | |
| 1780 | // finally, connect to the server... |
| 1781 | if (config.useLiveData) { |
| 1782 | webSock.connect(); |
| 1783 | } |
| 1784 | }); |
| 1785 | } |
| 1786 | |
| 1787 | function showBg() { |
| 1788 | return config.options.showBackground ? 'visible' : 'hidden'; |
| 1789 | } |
| 1790 | |
| 1791 | function loadStaticMap() { |
| 1792 | fnTrace('loadStaticMap', config.backgroundUrl); |
| 1793 | var w = network.view.width(), |
| 1794 | h = network.view.height(); |
| 1795 | |
| 1796 | // load the background image |
| 1797 | bgImg = svg.insert('svg:image', '#topo-G') |
| 1798 | .attr({ |
| 1799 | id: 'topo-bg', |
| 1800 | width: w, |
| 1801 | height: h, |
| 1802 | 'xlink:href': config.backgroundUrl |
| 1803 | }) |
| 1804 | .style({ |
| 1805 | visibility: showBg() |
| 1806 | }); |
| 1807 | } |
| 1808 | |
| 1809 | function loadGeoMap() { |
| 1810 | fnTrace('loadGeoMap', geoJsonUrl); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1811 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1812 | // extracts the topojson data into geocoordinate-based geometry |
| 1813 | var topoData = topojson.feature(geoJson, geoJson.objects.states); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1814 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1815 | // see: http://bl.ocks.org/mbostock/4707858 |
| 1816 | geoMapProjection = d3.geo.mercator(); |
| 1817 | var path = d3.geo.path().projection(geoMapProjection); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1818 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1819 | geoMapProjection |
| 1820 | .scale(1) |
| 1821 | .translate([0, 0]); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1822 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1823 | // [[x1,y1],[x2,y2]] |
| 1824 | var b = path.bounds(topoData); |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1825 | // size map to 95% of minimum dimension to fill space |
| 1826 | var s = .95 / Math.min((b[1][0] - b[0][0]) / config.logicalSize, (b[1][1] - b[0][1]) / config.logicalSize); |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1827 | var t = [(config.logicalSize - s * (b[1][0] + b[0][0])) / 2, (config.logicalSize - s * (b[1][1] + b[0][1])) / 2]; |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1828 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1829 | geoMapProjection |
| 1830 | .scale(s) |
| 1831 | .translate(t); |
| 1832 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1833 | bgImg = zoomPanContainer.insert("g", '#topo-G'); |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1834 | bgImg.attr('id', 'map').selectAll('path') |
| 1835 | .data(topoData.features) |
| 1836 | .enter() |
| 1837 | .append('path') |
| 1838 | .attr('d', path); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1839 | } |
| 1840 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1841 | function resize(view, ctx, flags) { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1842 | setSize(svg, view); |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | |
| 1846 | // ============================== |
| 1847 | // View registration |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1848 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 1849 | onos.ui.addView('topo', { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1850 | preload: preload, |
| 1851 | load: load, |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1852 | unload: unload, |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1853 | resize: resize |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1854 | }); |
| 1855 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1856 | detailPane = onos.ui.addFloatingPanel('topo-detail'); |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1857 | oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL'); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1858 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1859 | }(ONOS)); |