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, |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 91 | hostLink: 5 |
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, |
| 101 | host: -300 |
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 | } |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 109 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 112 | // radio buttons |
| 113 | var btnSet = [ |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 114 | { text: 'All Layers', cb: showAllLayers }, |
| 115 | { text: 'Packet Only', cb: showPacketLayer }, |
| 116 | { text: 'Optical Only', cb: showOpticalLayer } |
| 117 | ]; |
| 118 | |
| 119 | // key bindings |
| 120 | var keyDispatch = { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 121 | M: testMe, // TODO: remove (testing only) |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 122 | S: injectStartupEvents, // TODO: remove (testing only) |
| 123 | space: injectTestEvent, // TODO: remove (testing only) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 124 | |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 125 | B: toggleBg, // TODO: do we really need this? |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 126 | L: cycleLabels, |
| 127 | P: togglePorts, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 128 | U: unpin, |
| 129 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 130 | W: requestTraffic, // bag of selections |
| 131 | Z: requestPath, // host-to-host intent (and monitor) |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 132 | X: cancelMonitor |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 133 | }; |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 134 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 135 | // state variables |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 136 | var network = { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 137 | view: null, // view token reference |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 138 | nodes: [], |
| 139 | links: [], |
| 140 | lookup: {} |
| 141 | }, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 142 | scenario = { |
| 143 | evDir: 'json/ev/', |
| 144 | evScenario: '/scenario.json', |
| 145 | evPrefix: '/ev_', |
| 146 | evOnos: '_onos.json', |
| 147 | evUi: '_ui.json', |
| 148 | ctx: null, |
| 149 | params: {}, |
| 150 | evNumber: 0, |
| 151 | view: null, |
| 152 | debug: false |
| 153 | }, |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 154 | webSock, |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 155 | sid = 0, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 156 | deviceLabelIndex = 0, |
| 157 | hostLabelIndex = 0, |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 158 | detailPane, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 159 | selectOrder = [], |
| 160 | selections = {}, |
| 161 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 162 | highlighted = null, |
| 163 | hovered = null, |
| 164 | viewMode = 'showAll', |
| 165 | portLabelsOn = false; |
| 166 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 167 | // D3 selections |
| 168 | var svg, |
| 169 | bgImg, |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 170 | topoG, |
| 171 | nodeG, |
| 172 | linkG, |
| 173 | node, |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 174 | link, |
| 175 | mask; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 176 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 177 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 178 | // For Debugging / Development |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 179 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 180 | function note(label, msg) { |
| 181 | console.log('NOTE: ' + label + ': ' + msg); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 184 | function debug(what) { |
| 185 | return config.debugOn && config.debug[what]; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 188 | function fnTrace(msg, id) { |
| 189 | if (config.fnTrace) { |
| 190 | console.log('FN: ' + msg + ' [' + id + ']'); |
| 191 | } |
| 192 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 193 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 194 | // ============================== |
| 195 | // Key Callbacks |
| 196 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 197 | function testMe(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 198 | view.alert('test'); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 201 | function abortIfLive() { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 202 | if (config.useLiveData) { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 203 | network.view.alert("Sorry, currently using live data.."); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 204 | return true; |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 205 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 206 | return false; |
| 207 | } |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 208 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 209 | function testDebug(msg) { |
| 210 | if (scenario.debug) { |
| 211 | scenario.view.alert(msg); |
| 212 | } |
| 213 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 214 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 215 | function injectTestEvent(view) { |
| 216 | if (abortIfLive()) { return; } |
| 217 | var sc = scenario, |
| 218 | evn = ++sc.evNumber, |
| 219 | pfx = sc.evDir + sc.ctx + sc.evPrefix + evn, |
| 220 | onosUrl = pfx + sc.evOnos, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 221 | uiUrl = pfx + sc.evUi, |
| 222 | stack = [ |
| 223 | { url: onosUrl, cb: handleServerEvent }, |
| 224 | { url: uiUrl, cb: handleUiEvent } |
| 225 | ]; |
| 226 | recurseFetchEvent(stack, evn); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 229 | function recurseFetchEvent(stack, evn) { |
| 230 | var v = scenario.view, |
| 231 | frame; |
| 232 | if (stack.length === 0) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 233 | v.alert('Oops!\n\nNo event #' + evn + ' found.'); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | frame = stack.shift(); |
| 237 | |
| 238 | d3.json(frame.url, function (err, data) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 239 | if (err) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 240 | if (err.status === 404) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 241 | // if we didn't find the data, try the next stack frame |
| 242 | recurseFetchEvent(stack, evn); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 243 | } else { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 244 | v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 245 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 246 | } else { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 247 | testDebug('loaded: ' + frame.url); |
| 248 | frame.cb(data); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 249 | } |
| 250 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 251 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 252 | } |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 253 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 254 | function handleUiEvent(data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 255 | scenario.view.alert('UI Tx: ' + data.event + '\n\n' + |
| 256 | JSON.stringify(data)); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | function injectStartupEvents(view) { |
| 260 | var last = scenario.params.lastAuto || 0; |
| 261 | if (abortIfLive()) { return; } |
| 262 | |
| 263 | while (scenario.evNumber < last) { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 264 | injectTestEvent(view); |
| 265 | } |
| 266 | } |
| 267 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 268 | function toggleBg() { |
| 269 | var vis = bgImg.style('visibility'); |
| 270 | bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden'); |
| 271 | } |
| 272 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 273 | function cycleLabels() { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 274 | deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1) |
| 275 | ? 0 : deviceLabelIndex + 1; |
Simon Hunt | 5f36d34 | 2014-11-08 21:33:14 -0800 | [diff] [blame] | 276 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 277 | network.nodes.forEach(function (d) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 278 | if (d.class === 'device') { |
| 279 | updateDeviceLabel(d); |
| 280 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 281 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | function togglePorts(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 285 | view.alert('togglePorts() callback') |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | function unpin(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 289 | view.alert('unpin() callback') |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // ============================== |
| 293 | // Radio Button Callbacks |
| 294 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 295 | function showAllLayers() { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 296 | // network.node.classed('inactive', false); |
| 297 | // network.link.classed('inactive', false); |
| 298 | // d3.selectAll('svg .port').classed('inactive', false); |
| 299 | // d3.selectAll('svg .portText').classed('inactive', false); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 300 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 301 | network.view.alert('showAllLayers() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | function showPacketLayer() { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 305 | showAllLayers(); |
| 306 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 307 | network.view.alert('showPacketLayer() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | function showOpticalLayer() { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 311 | showAllLayers(); |
| 312 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 313 | network.view.alert('showOpticalLayer() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 316 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 317 | // Private functions |
| 318 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 319 | function safeId(s) { |
| 320 | return s.replace(/[^a-z0-9]/gi, '-'); |
| 321 | } |
| 322 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 323 | // set the size of the given element to that of the view (reduced if padded) |
| 324 | function setSize(el, view, pad) { |
| 325 | var padding = pad ? pad * 2 : 0; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 326 | el.attr({ |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 327 | width: view.width() - padding, |
| 328 | height: view.height() - padding |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 329 | }); |
| 330 | } |
| 331 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 332 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 333 | // ============================== |
| 334 | // Event handlers for server-pushed events |
| 335 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 336 | function logicError(msg) { |
| 337 | // TODO, report logic error to server, via websock, so it can be logged |
| 338 | network.view.alert('Logic Error:\n\n' + msg); |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 339 | console.warn(msg); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 342 | var eventDispatch = { |
| 343 | addDevice: addDevice, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 344 | addLink: addLink, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 345 | addHost: addHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 346 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 347 | updateDevice: updateDevice, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 348 | updateLink: updateLink, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 349 | updateHost: updateHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 350 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 351 | removeDevice: stillToImplement, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 352 | removeLink: removeLink, |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 353 | removeHost: removeHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 354 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 355 | showDetails: showDetails, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 356 | showPath: showPath, |
| 357 | showTraffic: showTraffic |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 358 | }; |
| 359 | |
| 360 | function addDevice(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 361 | fnTrace('addDevice', data.payload.id); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 362 | var device = data.payload, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 363 | nodeData = createDeviceNode(device); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 364 | network.nodes.push(nodeData); |
| 365 | network.lookup[nodeData.id] = nodeData; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 366 | updateNodes(); |
| 367 | network.force.start(); |
| 368 | } |
| 369 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 370 | function addLink(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 371 | fnTrace('addLink', data.payload.id); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 372 | var link = data.payload, |
| 373 | lnk = createLink(link); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 374 | if (lnk) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 375 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 376 | network.lookup[lnk.id] = lnk; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 377 | updateLinks(); |
| 378 | network.force.start(); |
| 379 | } |
| 380 | } |
| 381 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 382 | function addHost(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 383 | fnTrace('addHost', data.payload.id); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 384 | var host = data.payload, |
| 385 | node = createHostNode(host), |
| 386 | lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 387 | network.nodes.push(node); |
| 388 | network.lookup[host.id] = node; |
| 389 | updateNodes(); |
| 390 | |
| 391 | lnk = createHostLink(host); |
| 392 | if (lnk) { |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 393 | node.linkData = lnk; // cache ref on its host |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 394 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 395 | network.lookup[host.ingress] = lnk; |
| 396 | network.lookup[host.egress] = lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 397 | updateLinks(); |
| 398 | } |
| 399 | network.force.start(); |
| 400 | } |
| 401 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 402 | // TODO: fold updateX(...) methods into one base method; remove duplication |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 403 | function updateDevice(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 404 | fnTrace('updateDevice', data.payload.id); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 405 | var device = data.payload, |
| 406 | id = device.id, |
| 407 | nodeData = network.lookup[id]; |
| 408 | if (nodeData) { |
| 409 | $.extend(nodeData, device); |
| 410 | updateDeviceState(nodeData); |
| 411 | } else { |
| 412 | logicError('updateDevice lookup fail. ID = "' + id + '"'); |
| 413 | } |
| 414 | } |
| 415 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 416 | function updateLink(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 417 | fnTrace('updateLink', data.payload.id); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 418 | var link = data.payload, |
| 419 | id = link.id, |
| 420 | linkData = network.lookup[id]; |
| 421 | if (linkData) { |
| 422 | $.extend(linkData, link); |
| 423 | updateLinkState(linkData); |
| 424 | } else { |
| 425 | logicError('updateLink lookup fail. ID = "' + id + '"'); |
| 426 | } |
| 427 | } |
| 428 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 429 | function updateHost(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 430 | fnTrace('updateHost', data.payload.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 431 | var host = data.payload, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 432 | id = host.id, |
| 433 | hostData = network.lookup[id]; |
| 434 | if (hostData) { |
| 435 | $.extend(hostData, host); |
| 436 | updateHostState(hostData); |
| 437 | } else { |
| 438 | logicError('updateHost lookup fail. ID = "' + id + '"'); |
| 439 | } |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 442 | // TODO: fold removeX(...) methods into base method - remove dup code |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 443 | function removeLink(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 444 | fnTrace('removeLink', data.payload.id); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 445 | var link = data.payload, |
| 446 | id = link.id, |
| 447 | linkData = network.lookup[id]; |
| 448 | if (linkData) { |
| 449 | removeLinkElement(linkData); |
| 450 | } else { |
| 451 | logicError('removeLink lookup fail. ID = "' + id + '"'); |
| 452 | } |
| 453 | } |
| 454 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 455 | function removeHost(data) { |
| 456 | fnTrace('removeHost', data.payload.id); |
| 457 | var host = data.payload, |
| 458 | id = host.id, |
| 459 | hostData = network.lookup[id]; |
| 460 | if (hostData) { |
| 461 | removeHostElement(hostData); |
| 462 | } else { |
| 463 | logicError('removeHost lookup fail. ID = "' + id + '"'); |
| 464 | } |
| 465 | } |
| 466 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 467 | function showDetails(data) { |
| 468 | fnTrace('showDetails', data.payload.id); |
| 469 | populateDetails(data.payload); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 470 | // TODO: Add single-select actions ... |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 471 | detailPane.show(); |
| 472 | } |
| 473 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 474 | function showPath(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 475 | fnTrace('showPath', data.payload.id); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 476 | var links = data.payload.links, |
| 477 | s = [ data.event + "\n" + links.length ]; |
| 478 | links.forEach(function (d, i) { |
| 479 | s.push(d); |
| 480 | }); |
| 481 | network.view.alert(s.join('\n')); |
| 482 | |
| 483 | links.forEach(function (d, i) { |
| 484 | var link = network.lookup[d]; |
| 485 | if (link) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 486 | link.el.classed('showPath', true); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 487 | } |
| 488 | }); |
| 489 | |
| 490 | // TODO: add selection-highlite lines to links |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 493 | function showTraffic(data) { |
| 494 | network.view.alert("showTraffic() -- TODO") |
| 495 | } |
| 496 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 497 | // ............................... |
| 498 | |
| 499 | function stillToImplement(data) { |
| 500 | var p = data.payload; |
| 501 | note(data.event, p.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 502 | network.view.alert('Not yet implemented: "' + data.event + '"'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 503 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 504 | |
| 505 | function unknownEvent(data) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 506 | network.view.alert('Unknown event type: "' + data.event + '"'); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | function handleServerEvent(data) { |
| 510 | var fn = eventDispatch[data.event] || unknownEvent; |
| 511 | fn(data); |
| 512 | } |
| 513 | |
| 514 | // ============================== |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 515 | // Out-going messages... |
| 516 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 517 | function userFeedback(msg) { |
| 518 | // for now, use the alert pane as is. Maybe different alert style in |
| 519 | // the future (centered on view; dismiss button?) |
| 520 | network.view.alert(msg); |
| 521 | } |
| 522 | |
| 523 | function nSel() { |
| 524 | return selectOrder.length; |
| 525 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 526 | function getSel(idx) { |
| 527 | return selections[selectOrder[idx]]; |
| 528 | } |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 529 | function getSelId(idx) { |
| 530 | return getSel(idx).obj.id; |
| 531 | } |
| 532 | function allSelectionsClass(cls) { |
| 533 | for (var i=0, n=nSel(); i<n; i++) { |
| 534 | if (getSel(i).obj.class !== cls) { |
| 535 | return false; |
| 536 | } |
| 537 | } |
| 538 | return true; |
| 539 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 540 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 541 | function requestTraffic() { |
| 542 | if (nSel() > 0) { |
| 543 | sendMessage('requestTraffic', { |
| 544 | ids: selectOrder |
| 545 | }); |
| 546 | } else { |
| 547 | userFeedback('Request-Traffic requires one or\n' + |
| 548 | 'more items to be selected.'); |
| 549 | } |
| 550 | } |
| 551 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 552 | function requestPath() { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 553 | if (nSel() === 2 && allSelectionsClass('host')) { |
| 554 | sendMessage('requestPath', { |
| 555 | one: getSelId(0), |
| 556 | two: getSelId(1) |
| 557 | }); |
| 558 | } else { |
| 559 | userFeedback('Request-Path requires two\n' + |
| 560 | 'hosts to be selected.'); |
| 561 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 564 | function cancelMonitor() { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 565 | // FIXME: from where do we get the intent id(s) to send to the server? |
| 566 | sendMessage('cancelMonitor', { |
| 567 | ids: ["need_the_intent_id"] |
| 568 | }); |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 571 | // request details for the selected element |
| 572 | function requestDetails() { |
| 573 | var data = getSel(0).obj, |
| 574 | payload = { |
| 575 | id: data.id, |
| 576 | class: data.class |
| 577 | }; |
| 578 | sendMessage('requestDetails', payload); |
| 579 | } |
| 580 | |
| 581 | // ============================== |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 582 | // force layout modification functions |
| 583 | |
| 584 | function translate(x, y) { |
| 585 | return 'translate(' + x + ',' + y + ')'; |
| 586 | } |
| 587 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 588 | function missMsg(what, id) { |
| 589 | return '\n[' + what + '] "' + id + '" missing '; |
| 590 | } |
| 591 | |
| 592 | function linkEndPoints(srcId, dstId) { |
| 593 | var srcNode = network.lookup[srcId], |
| 594 | dstNode = network.lookup[dstId], |
| 595 | sMiss = !srcNode ? missMsg('src', srcId) : '', |
| 596 | dMiss = !dstNode ? missMsg('dst', dstId) : ''; |
| 597 | |
| 598 | if (sMiss || dMiss) { |
| 599 | logicError('Node(s) not on map for link:\n' + sMiss + dMiss); |
| 600 | return null; |
| 601 | } |
| 602 | return { |
| 603 | source: srcNode, |
| 604 | target: dstNode, |
| 605 | x1: srcNode.x, |
| 606 | y1: srcNode.y, |
| 607 | x2: dstNode.x, |
| 608 | y2: dstNode.y |
| 609 | }; |
| 610 | } |
| 611 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 612 | function createHostLink(host) { |
| 613 | var src = host.id, |
| 614 | dst = host.cp.device, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 615 | id = host.ingress, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 616 | lnk = linkEndPoints(src, dst); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 617 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 618 | if (!lnk) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 619 | return null; |
| 620 | } |
| 621 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 622 | // Synthesize link ... |
| 623 | $.extend(lnk, { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 624 | id: id, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 625 | class: 'link', |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 626 | type: 'hostLink', |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 627 | svgClass: 'link hostLink', |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 628 | linkWidth: 1 |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 629 | }); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 630 | return lnk; |
| 631 | } |
| 632 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 633 | function createLink(link) { |
| 634 | var lnk = linkEndPoints(link.src, link.dst), |
| 635 | type = link.type; |
| 636 | |
| 637 | if (!lnk) { |
| 638 | return null; |
| 639 | } |
| 640 | |
| 641 | // merge in remaining data |
| 642 | $.extend(lnk, link, { |
| 643 | class: 'link', |
| 644 | svgClass: type ? 'link ' + type : 'link' |
| 645 | }); |
| 646 | return lnk; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 649 | var widthRatio = 1.4, |
| 650 | linkScale = d3.scale.linear() |
| 651 | .domain([1, 12]) |
| 652 | .range([widthRatio, 12 * widthRatio]) |
| 653 | .clamp(true); |
| 654 | |
| 655 | function updateLinkWidth (d) { |
| 656 | // TODO: watch out for .showPath/.showTraffic classes |
| 657 | d.el.transition() |
| 658 | .duration(1000) |
| 659 | .attr('stroke-width', linkScale(d.linkWidth)); |
| 660 | } |
| 661 | |
| 662 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 663 | function updateLinks() { |
| 664 | link = linkG.selectAll('.link') |
| 665 | .data(network.links, function (d) { return d.id; }); |
| 666 | |
| 667 | // operate on existing links, if necessary |
| 668 | // link .foo() .bar() ... |
| 669 | |
| 670 | // operate on entering links: |
| 671 | var entering = link.enter() |
| 672 | .append('line') |
| 673 | .attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 674 | class: function (d) { return d.svgClass; }, |
| 675 | x1: function (d) { return d.x1; }, |
| 676 | y1: function (d) { return d.y1; }, |
| 677 | x2: function (d) { return d.x2; }, |
| 678 | y2: function (d) { return d.y2; }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 679 | stroke: config.topo.linkInColor, |
| 680 | 'stroke-width': config.topo.linkInWidth |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 681 | }) |
| 682 | .transition().duration(1000) |
| 683 | .attr({ |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 684 | 'stroke-width': function (d) { return linkScale(d.linkWidth); }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 685 | stroke: '#666' // TODO: remove explicit stroke, rather... |
| 686 | }); |
| 687 | |
| 688 | // augment links |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 689 | entering.each(function (d) { |
| 690 | var link = d3.select(this); |
| 691 | // provide ref to element selection from backing data.... |
| 692 | d.el = link; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 693 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 694 | // TODO: add src/dst port labels etc. |
| 695 | }); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 696 | |
| 697 | // operate on both existing and new links, if necessary |
| 698 | //link .foo() .bar() ... |
| 699 | |
| 700 | // operate on exiting links: |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 701 | link.exit() |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 702 | .attr({ |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 703 | 'stroke-dasharray': '3, 3' |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 704 | }) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 705 | .style('opacity', 0.4) |
| 706 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 707 | .duration(1500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 708 | .attr({ |
| 709 | 'stroke-dasharray': '3, 12' |
| 710 | }) |
| 711 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 712 | .duration(500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 713 | .style('opacity', 0.0) |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 714 | .remove(); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | function createDeviceNode(device) { |
| 718 | // start with the object as is |
| 719 | var node = device, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 720 | type = device.type, |
| 721 | svgCls = type ? 'node device ' + type : 'node device'; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 722 | |
| 723 | // Augment as needed... |
| 724 | node.class = 'device'; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 725 | node.svgClass = device.online ? svgCls + ' online' : svgCls; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 726 | positionNode(node); |
| 727 | |
| 728 | // cache label array length |
| 729 | network.deviceLabelCount = device.labels.length; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 730 | return node; |
| 731 | } |
| 732 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 733 | function createHostNode(host) { |
| 734 | // start with the object as is |
| 735 | var node = host; |
| 736 | |
| 737 | // Augment as needed... |
| 738 | node.class = 'host'; |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 739 | if (!node.type) { |
| 740 | // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ? |
| 741 | node.type = 'endstation'; |
| 742 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 743 | node.svgClass = 'node host'; |
| 744 | // TODO: consider placing near its switch, if [x,y] not defined |
| 745 | positionNode(node); |
| 746 | |
| 747 | // cache label array length |
| 748 | network.hostLabelCount = host.labels.length; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 749 | return node; |
| 750 | } |
| 751 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 752 | function positionNode(node) { |
| 753 | var meta = node.metaUi, |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 754 | x = meta && meta.x, |
| 755 | y = meta && meta.y, |
| 756 | xy; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 757 | |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 758 | // If we have [x,y] already, use that... |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 759 | if (x && y) { |
| 760 | node.fixed = true; |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 761 | node.x = x; |
| 762 | node.y = y; |
| 763 | return; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 764 | } |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 765 | |
| 766 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 767 | // (center of the view) causes them to explode outwards when |
| 768 | // the force layout kicks in. So, we spread them out a bit |
| 769 | // initially, to provide a more serene layout convergence. |
| 770 | // Additionally, if the node is a host, we place it near |
| 771 | // the device it is connected to. |
| 772 | |
| 773 | function spread(s) { |
| 774 | return Math.floor((Math.random() * s) - s/2); |
| 775 | } |
| 776 | |
| 777 | function randDim(dim) { |
| 778 | return dim / 2 + spread(dim * 0.7071); |
| 779 | } |
| 780 | |
| 781 | function rand() { |
| 782 | return { |
| 783 | x: randDim(network.view.width()), |
| 784 | y: randDim(network.view.height()) |
| 785 | }; |
| 786 | } |
| 787 | |
| 788 | function near(node) { |
| 789 | var min = 12, |
| 790 | dx = spread(12), |
| 791 | dy = spread(12); |
| 792 | return { |
| 793 | x: node.x + min + dx, |
| 794 | y: node.y + min + dy |
| 795 | }; |
| 796 | } |
| 797 | |
| 798 | function getDevice(cp) { |
| 799 | var d = network.lookup[cp.device]; |
| 800 | return d || rand(); |
| 801 | } |
| 802 | |
| 803 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
| 804 | $.extend(node, xy); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 805 | } |
| 806 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 807 | function iconUrl(d) { |
| 808 | return 'img/' + d.type + '.png'; |
| 809 | } |
| 810 | |
| 811 | // returns the newly computed bounding box of the rectangle |
| 812 | function adjustRectToFitText(n) { |
| 813 | var text = n.select('text'), |
| 814 | box = text.node().getBBox(), |
| 815 | lab = config.labels; |
| 816 | |
| 817 | text.attr('text-anchor', 'middle') |
| 818 | .attr('y', '-0.8em') |
| 819 | .attr('x', lab.imgPad/2); |
| 820 | |
| 821 | // translate the bbox so that it is centered on [x,y] |
| 822 | box.x = -box.width / 2; |
| 823 | box.y = -box.height / 2; |
| 824 | |
| 825 | // add padding |
| 826 | box.x -= (lab.padLR + lab.imgPad/2); |
| 827 | box.width += lab.padLR * 2 + lab.imgPad; |
| 828 | box.y -= lab.padTB; |
| 829 | box.height += lab.padTB * 2; |
| 830 | |
| 831 | return box; |
| 832 | } |
| 833 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 834 | function mkSvgClass(d) { |
| 835 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 836 | } |
| 837 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 838 | function hostLabel(d) { |
| 839 | var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0; |
| 840 | return d.labels[idx]; |
| 841 | } |
| 842 | function deviceLabel(d) { |
| 843 | var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0; |
| 844 | return d.labels[idx]; |
| 845 | } |
| 846 | function niceLabel(label) { |
| 847 | return (label && label.trim()) ? label : '.'; |
| 848 | } |
| 849 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 850 | function updateDeviceLabel(d) { |
| 851 | var label = niceLabel(deviceLabel(d)), |
| 852 | node = d.el, |
| 853 | box; |
| 854 | |
| 855 | node.select('text') |
| 856 | .text(label) |
| 857 | .style('opacity', 0) |
| 858 | .transition() |
| 859 | .style('opacity', 1); |
| 860 | |
| 861 | box = adjustRectToFitText(node); |
| 862 | |
| 863 | node.select('rect') |
| 864 | .transition() |
| 865 | .attr(box); |
| 866 | |
| 867 | node.select('image') |
| 868 | .transition() |
| 869 | .attr('x', box.x + config.icons.xoff) |
| 870 | .attr('y', box.y + config.icons.yoff); |
| 871 | } |
| 872 | |
| 873 | function updateHostLabel(d) { |
| 874 | var label = hostLabel(d), |
| 875 | host = d.el; |
| 876 | |
| 877 | host.select('text').text(label); |
| 878 | } |
| 879 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 880 | function updateDeviceState(nodeData) { |
| 881 | nodeData.el.classed('online', nodeData.online); |
| 882 | updateDeviceLabel(nodeData); |
| 883 | // TODO: review what else might need to be updated |
| 884 | } |
| 885 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 886 | function updateLinkState(linkData) { |
| 887 | updateLinkWidth(linkData); |
| 888 | // TODO: review what else might need to be updated |
| 889 | // update label, if showing |
| 890 | } |
| 891 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 892 | function updateHostState(hostData) { |
| 893 | updateHostLabel(hostData); |
| 894 | // TODO: review what else might need to be updated |
| 895 | } |
| 896 | |
| 897 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 898 | function updateNodes() { |
| 899 | node = nodeG.selectAll('.node') |
| 900 | .data(network.nodes, function (d) { return d.id; }); |
| 901 | |
| 902 | // operate on existing nodes, if necessary |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 903 | // update host labels |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 904 | //node .foo() .bar() ... |
| 905 | |
| 906 | // operate on entering nodes: |
| 907 | var entering = node.enter() |
| 908 | .append('g') |
| 909 | .attr({ |
| 910 | id: function (d) { return safeId(d.id); }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 911 | class: mkSvgClass, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 912 | transform: function (d) { return translate(d.x, d.y); }, |
| 913 | opacity: 0 |
| 914 | }) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 915 | .call(network.drag) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 916 | //.on('mouseover', function (d) {}) |
| 917 | //.on('mouseover', function (d) {}) |
| 918 | .transition() |
| 919 | .attr('opacity', 1); |
| 920 | |
| 921 | // augment device nodes... |
| 922 | entering.filter('.device').each(function (d) { |
| 923 | var node = d3.select(this), |
| 924 | icon = iconUrl(d), |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 925 | label = niceLabel(deviceLabel(d)), |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 926 | box; |
| 927 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 928 | // provide ref to element from backing data.... |
| 929 | d.el = node; |
| 930 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 931 | node.append('rect') |
| 932 | .attr({ |
| 933 | 'rx': 5, |
| 934 | 'ry': 5 |
| 935 | }); |
| 936 | |
| 937 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 938 | .text(label) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 939 | .attr('dy', '1.1em'); |
| 940 | |
| 941 | box = adjustRectToFitText(node); |
| 942 | |
| 943 | node.select('rect') |
| 944 | .attr(box); |
| 945 | |
| 946 | if (icon) { |
| 947 | var cfg = config.icons; |
| 948 | node.append('svg:image') |
| 949 | .attr({ |
| 950 | x: box.x + config.icons.xoff, |
| 951 | y: box.y + config.icons.yoff, |
| 952 | width: cfg.w, |
| 953 | height: cfg.h, |
| 954 | 'xlink:href': icon |
| 955 | }); |
| 956 | } |
| 957 | |
| 958 | // debug function to show the modelled x,y coordinates of nodes... |
| 959 | if (debug('showNodeXY')) { |
| 960 | node.select('rect').attr('fill-opacity', 0.5); |
| 961 | node.append('circle') |
| 962 | .attr({ |
| 963 | class: 'debug', |
| 964 | cx: 0, |
| 965 | cy: 0, |
| 966 | r: '3px' |
| 967 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 968 | } |
| 969 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 970 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 971 | // augment host nodes... |
| 972 | entering.filter('.host').each(function (d) { |
| 973 | var node = d3.select(this), |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 974 | box; |
| 975 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 976 | // provide ref to element from backing data.... |
| 977 | d.el = node; |
| 978 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 979 | node.append('circle') |
| 980 | .attr('r', 8); // TODO: define host circle radius |
| 981 | |
| 982 | // TODO: are we attaching labels to hosts? |
| 983 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 984 | .text(hostLabel) |
| 985 | .attr('dy', '1.3em') |
| 986 | .attr('text-anchor', 'middle'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 987 | |
| 988 | // debug function to show the modelled x,y coordinates of nodes... |
| 989 | if (debug('showNodeXY')) { |
| 990 | node.select('circle').attr('fill-opacity', 0.5); |
| 991 | node.append('circle') |
| 992 | .attr({ |
| 993 | class: 'debug', |
| 994 | cx: 0, |
| 995 | cy: 0, |
| 996 | r: '3px' |
| 997 | }); |
| 998 | } |
| 999 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1000 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1001 | // operate on both existing and new nodes, if necessary |
| 1002 | //node .foo() .bar() ... |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1003 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1004 | // operate on exiting nodes: |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1005 | // Note that the node is removed after 2 seconds. |
| 1006 | // Sub element animations should be shorter than 2 seconds. |
| 1007 | var exiting = node.exit() |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 1008 | .transition() |
| 1009 | .duration(2000) |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1010 | .style('opacity', 0) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1011 | .remove(); |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1012 | |
| 1013 | // host node exits.... |
| 1014 | exiting.filter('.host').each(function (d) { |
| 1015 | var node = d3.select(this); |
| 1016 | |
| 1017 | node.select('text') |
| 1018 | .style('opacity', 0.5) |
| 1019 | .transition() |
| 1020 | .duration(1000) |
| 1021 | .style('opacity', 0); |
| 1022 | // note, leave <g>.remove to remove this element |
| 1023 | |
| 1024 | node.select('circle') |
| 1025 | .style('stroke-fill', '#555') |
| 1026 | .style('fill', '#888') |
| 1027 | .style('opacity', 0.5) |
| 1028 | .transition() |
| 1029 | .duration(1500) |
| 1030 | .attr('r', 0); |
| 1031 | // note, leave <g>.remove to remove this element |
| 1032 | |
| 1033 | }); |
| 1034 | |
| 1035 | // TODO: device node exits |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1038 | function find(id, array) { |
| 1039 | for (var idx = 0, n = array.length; idx < n; idx++) { |
| 1040 | if (array[idx].id === id) { |
| 1041 | return idx; |
| 1042 | } |
| 1043 | } |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | function removeLinkElement(linkData) { |
| 1048 | // remove from lookup cache |
| 1049 | delete network.lookup[linkData.id]; |
| 1050 | // remove from links array |
| 1051 | var idx = find(linkData.id, network.links); |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 1052 | network.links.splice(idx, 1); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1053 | // remove from SVG |
| 1054 | updateLinks(); |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 1055 | network.force.resume(); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1056 | } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1057 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 1058 | function removeHostElement(hostData) { |
| 1059 | // first, remove associated hostLink... |
| 1060 | removeLinkElement(hostData.linkData); |
| 1061 | |
| 1062 | // remove from lookup cache |
| 1063 | delete network.lookup[hostData.id]; |
| 1064 | // remove from nodes array |
| 1065 | var idx = find(hostData.id, network.nodes); |
| 1066 | network.nodes.splice(idx, 1); |
| 1067 | // remove from SVG |
| 1068 | updateNodes(); |
| 1069 | network.force.resume(); |
| 1070 | } |
| 1071 | |
| 1072 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1073 | function tick() { |
| 1074 | node.attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1075 | transform: function (d) { return translate(d.x, d.y); } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1076 | }); |
| 1077 | |
| 1078 | link.attr({ |
| 1079 | x1: function (d) { return d.source.x; }, |
| 1080 | y1: function (d) { return d.source.y; }, |
| 1081 | x2: function (d) { return d.target.x; }, |
| 1082 | y2: function (d) { return d.target.y; } |
| 1083 | }); |
| 1084 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1085 | |
| 1086 | // ============================== |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1087 | // Web-Socket for live data |
| 1088 | |
| 1089 | function webSockUrl() { |
| 1090 | return document.location.toString() |
| 1091 | .replace(/\#.*/, '') |
| 1092 | .replace('http://', 'ws://') |
| 1093 | .replace('https://', 'wss://') |
| 1094 | .replace('index2.html', config.webSockUrl); |
| 1095 | } |
| 1096 | |
| 1097 | webSock = { |
| 1098 | ws : null, |
| 1099 | |
| 1100 | connect : function() { |
| 1101 | webSock.ws = new WebSocket(webSockUrl()); |
| 1102 | |
| 1103 | webSock.ws.onopen = function() { |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1104 | noWebSock(false); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1105 | }; |
| 1106 | |
| 1107 | webSock.ws.onmessage = function(m) { |
| 1108 | if (m.data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1109 | wsTraceRx(m.data); |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 1110 | handleServerEvent(JSON.parse(m.data)); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1111 | } |
| 1112 | }; |
| 1113 | |
| 1114 | webSock.ws.onclose = function(m) { |
| 1115 | webSock.ws = null; |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1116 | noWebSock(true); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1117 | }; |
| 1118 | }, |
| 1119 | |
| 1120 | send : function(text) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1121 | if (text != null) { |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1122 | webSock._send(text); |
| 1123 | } |
| 1124 | }, |
| 1125 | |
| 1126 | _send : function(message) { |
| 1127 | if (webSock.ws) { |
| 1128 | webSock.ws.send(message); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1129 | } else { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1130 | network.view.alert('no web socket open\n\n' + message); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | }; |
| 1135 | |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1136 | function noWebSock(b) { |
| 1137 | mask.style('display',b ? 'block' : 'none'); |
| 1138 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1139 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1140 | // TODO: use cache of pending messages (key = sid) to reconcile responses |
| 1141 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1142 | function sendMessage(evType, payload) { |
| 1143 | var toSend = { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1144 | event: evType, |
| 1145 | sid: ++sid, |
| 1146 | payload: payload |
| 1147 | }, |
| 1148 | asText = JSON.stringify(toSend); |
| 1149 | wsTraceTx(asText); |
| 1150 | webSock.send(asText); |
| 1151 | } |
| 1152 | |
| 1153 | function wsTraceTx(msg) { |
| 1154 | wsTrace('tx', msg); |
| 1155 | } |
| 1156 | function wsTraceRx(msg) { |
| 1157 | wsTrace('rx', msg); |
| 1158 | } |
| 1159 | function wsTrace(rxtx, msg) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1160 | console.log('[' + rxtx + '] ' + msg); |
| 1161 | // TODO: integrate with trace view |
| 1162 | //if (trace) { |
| 1163 | // trace.output(rxtx, msg); |
| 1164 | //} |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | // ============================== |
| 1169 | // Selection stuff |
| 1170 | |
| 1171 | function selectObject(obj, el) { |
| 1172 | var n, |
| 1173 | meta = d3.event.sourceEvent.metaKey; |
| 1174 | |
| 1175 | if (el) { |
| 1176 | n = d3.select(el); |
| 1177 | } else { |
| 1178 | node.each(function(d) { |
| 1179 | if (d == obj) { |
| 1180 | n = d3.select(el = this); |
| 1181 | } |
| 1182 | }); |
| 1183 | } |
| 1184 | if (!n) return; |
| 1185 | |
| 1186 | if (meta && n.classed('selected')) { |
| 1187 | deselectObject(obj.id); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1188 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | if (!meta) { |
| 1193 | deselectAll(); |
| 1194 | } |
| 1195 | |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1196 | selections[obj.id] = { obj: obj, el: el }; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1197 | selectOrder.push(obj.id); |
| 1198 | |
| 1199 | n.classed('selected', true); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1200 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | function deselectObject(id) { |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1204 | var obj = selections[id], |
| 1205 | idx; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1206 | if (obj) { |
| 1207 | d3.select(obj.el).classed('selected', false); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1208 | delete selections[id]; |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1209 | idx = $.inArray(id, selectOrder); |
| 1210 | if (idx >= 0) { |
| 1211 | selectOrder.splice(idx, 1); |
| 1212 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1213 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | function deselectAll() { |
| 1217 | // deselect all nodes in the network... |
| 1218 | node.classed('selected', false); |
| 1219 | selections = {}; |
| 1220 | selectOrder = []; |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1221 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1224 | // FIXME: this click handler does not get unloaded when the view does |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1225 | $('#view').on('click', function(e) { |
| 1226 | if (!$(e.target).closest('.node').length) { |
| 1227 | if (!e.metaKey) { |
| 1228 | deselectAll(); |
| 1229 | } |
| 1230 | } |
| 1231 | }); |
| 1232 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1233 | // update the state of the detail pane, based on current selections |
| 1234 | function updateDetailPane() { |
| 1235 | var nSel = selectOrder.length; |
| 1236 | if (!nSel) { |
| 1237 | detailPane.hide(); |
| 1238 | } else if (nSel === 1) { |
| 1239 | singleSelect(); |
| 1240 | } else { |
| 1241 | multiSelect(); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | function singleSelect() { |
| 1246 | requestDetails(); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1247 | // NOTE: detail pane will be shown from showDetails event callback |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | function multiSelect() { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1251 | populateMultiSelect(); |
| 1252 | // TODO: Add multi-select actions ... |
| 1253 | } |
| 1254 | |
| 1255 | function addSep(tbody) { |
| 1256 | var tr = tbody.append('tr'); |
| 1257 | $('<hr>').appendTo(tr.append('td').attr('colspan', 2)); |
| 1258 | } |
| 1259 | |
| 1260 | function addProp(tbody, label, value) { |
| 1261 | var tr = tbody.append('tr'); |
| 1262 | |
| 1263 | tr.append('td') |
| 1264 | .attr('class', 'label') |
| 1265 | .text(label + ' :'); |
| 1266 | |
| 1267 | tr.append('td') |
| 1268 | .attr('class', 'value') |
| 1269 | .text(value); |
| 1270 | } |
| 1271 | |
| 1272 | function populateMultiSelect() { |
| 1273 | detailPane.empty(); |
| 1274 | |
| 1275 | var title = detailPane.append("h2"), |
| 1276 | table = detailPane.append("table"), |
| 1277 | tbody = table.append("tbody"); |
| 1278 | |
| 1279 | title.text('Multi-Select...'); |
| 1280 | |
| 1281 | selectOrder.forEach(function (d, i) { |
| 1282 | addProp(tbody, i+1, d); |
| 1283 | }); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | function populateDetails(data) { |
| 1287 | detailPane.empty(); |
| 1288 | |
| 1289 | var title = detailPane.append("h2"), |
| 1290 | table = detailPane.append("table"), |
| 1291 | tbody = table.append("tbody"); |
| 1292 | |
| 1293 | $('<img src="img/' + data.type + '.png">').appendTo(title); |
| 1294 | $('<span>').attr('class', 'icon').text(data.id).appendTo(title); |
| 1295 | |
| 1296 | data.propOrder.forEach(function(p) { |
| 1297 | if (p === '-') { |
| 1298 | addSep(tbody); |
| 1299 | } else { |
| 1300 | addProp(tbody, p, data.props[p]); |
| 1301 | } |
| 1302 | }); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | // ============================== |
| 1306 | // Test harness code |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1307 | |
| 1308 | function prepareScenario(view, ctx, dbg) { |
| 1309 | var sc = scenario, |
| 1310 | urlSc = sc.evDir + ctx + sc.evScenario; |
| 1311 | |
| 1312 | if (!ctx) { |
| 1313 | view.alert("No scenario specified (null ctx)"); |
| 1314 | return; |
| 1315 | } |
| 1316 | |
| 1317 | sc.view = view; |
| 1318 | sc.ctx = ctx; |
| 1319 | sc.debug = dbg; |
| 1320 | sc.evNumber = 0; |
| 1321 | |
| 1322 | d3.json(urlSc, function(err, data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1323 | var p = data && data.params || {}, |
| 1324 | desc = data && data.description || null, |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 1325 | intro = data && data.title; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1326 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1327 | if (err) { |
| 1328 | view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err); |
| 1329 | } else { |
| 1330 | sc.params = p; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1331 | if (desc) { |
| 1332 | intro += '\n\n ' + desc.join('\n '); |
| 1333 | } |
| 1334 | view.alert(intro); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1335 | } |
| 1336 | }); |
| 1337 | |
| 1338 | } |
| 1339 | |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1340 | |
| 1341 | function para(sel, text) { |
| 1342 | sel.append('p').text(text); |
| 1343 | } |
| 1344 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1345 | // ============================== |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1346 | // View life-cycle callbacks |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1347 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1348 | function preload(view, ctx, flags) { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1349 | var w = view.width(), |
| 1350 | h = view.height(), |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1351 | fcfg = config.force, |
| 1352 | fpad = fcfg.pad, |
| 1353 | forceDim = [w - 2*fpad, h - 2*fpad]; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1354 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1355 | // TODO: set trace api |
| 1356 | //trace = onos.exported.webSockTrace; |
| 1357 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1358 | // NOTE: view.$div is a D3 selection of the view's div |
| 1359 | svg = view.$div.append('svg'); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1360 | setSize(svg, view); |
| 1361 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1362 | // add blue glow filter to svg layer |
| 1363 | d3u.appendGlow(svg); |
| 1364 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1365 | // group for the topology |
| 1366 | topoG = svg.append('g') |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1367 | .attr('id', 'topo-G') |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1368 | .attr('transform', fcfg.translate()); |
| 1369 | |
| 1370 | // subgroups for links and nodes |
| 1371 | linkG = topoG.append('g').attr('id', 'links'); |
| 1372 | nodeG = topoG.append('g').attr('id', 'nodes'); |
| 1373 | |
| 1374 | // selection of nodes and links |
| 1375 | link = linkG.selectAll('.link'); |
| 1376 | node = nodeG.selectAll('.node'); |
| 1377 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1378 | function chrg(d) { |
| 1379 | return fcfg.charge[d.class] || -12000; |
| 1380 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1381 | function ldist(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1382 | return fcfg.linkDistance[d.type] || 50; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1383 | } |
| 1384 | function lstrg(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1385 | // 0.0 - 1.0 |
| 1386 | return fcfg.linkStrength[d.type] || 1.0; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1387 | } |
| 1388 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1389 | function selectCb(d, self) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1390 | selectObject(d, self); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | function atDragEnd(d, self) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1394 | // once we've finished moving, pin the node in position |
| 1395 | d.fixed = true; |
| 1396 | d3.select(self).classed('fixed', true); |
| 1397 | if (config.useLiveData) { |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1398 | sendUpdateMeta(d); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1402 | function sendUpdateMeta(d) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1403 | sendMessage('updateMeta', { |
| 1404 | id: d.id, |
| 1405 | 'class': d.class, |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1406 | 'memento': { |
| 1407 | x: Math.floor(d.x), |
| 1408 | y: Math.floor(d.y) |
| 1409 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1410 | }); |
| 1411 | } |
| 1412 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1413 | // set up the force layout |
| 1414 | network.force = d3.layout.force() |
| 1415 | .size(forceDim) |
| 1416 | .nodes(network.nodes) |
| 1417 | .links(network.links) |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1418 | .gravity(0.4) |
| 1419 | .friction(0.7) |
| 1420 | .charge(chrg) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1421 | .linkDistance(ldist) |
| 1422 | .linkStrength(lstrg) |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1423 | .on('tick', tick); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1424 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1425 | network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd); |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1426 | |
| 1427 | // create mask layer for when we lose connection to server. |
| 1428 | mask = view.$div.append('div').attr('id','topo-mask'); |
| 1429 | para(mask, 'Oops!'); |
| 1430 | para(mask, 'Web-socket connection to server closed...'); |
| 1431 | para(mask, 'Try refreshing the page.'); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1432 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1433 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1434 | function load(view, ctx, flags) { |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1435 | // resize, in case the window was resized while we were not loaded |
| 1436 | resize(view, ctx, flags); |
| 1437 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1438 | // cache the view token, so network topo functions can access it |
| 1439 | network.view = view; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1440 | config.useLiveData = !flags.local; |
| 1441 | |
| 1442 | if (!config.useLiveData) { |
| 1443 | prepareScenario(view, ctx, flags.debug); |
| 1444 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1445 | |
| 1446 | // set our radio buttons and key bindings |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1447 | view.setRadio(btnSet); |
| 1448 | view.setKeys(keyDispatch); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1449 | |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1450 | // Load map data asynchronously; complete startup after that.. |
| 1451 | loadGeoJsonData(); |
| 1452 | } |
| 1453 | |
| 1454 | // TODO: move these to config/state portion of script |
| 1455 | var geoJsonUrl = 'geoUsa.json', // TODO: Paul |
| 1456 | geoJson; |
| 1457 | |
| 1458 | function loadGeoJsonData() { |
| 1459 | d3.json(geoJsonUrl, function (err, data) { |
| 1460 | if (err) { |
| 1461 | // fall back to USA map background |
| 1462 | loadStaticMap(); |
| 1463 | } else { |
| 1464 | geoJson = data; |
| 1465 | loadGeoMap(); |
| 1466 | } |
| 1467 | |
| 1468 | // finally, connect to the server... |
| 1469 | if (config.useLiveData) { |
| 1470 | webSock.connect(); |
| 1471 | } |
| 1472 | }); |
| 1473 | } |
| 1474 | |
| 1475 | function showBg() { |
| 1476 | return config.options.showBackground ? 'visible' : 'hidden'; |
| 1477 | } |
| 1478 | |
| 1479 | function loadStaticMap() { |
| 1480 | fnTrace('loadStaticMap', config.backgroundUrl); |
| 1481 | var w = network.view.width(), |
| 1482 | h = network.view.height(); |
| 1483 | |
| 1484 | // load the background image |
| 1485 | bgImg = svg.insert('svg:image', '#topo-G') |
| 1486 | .attr({ |
| 1487 | id: 'topo-bg', |
| 1488 | width: w, |
| 1489 | height: h, |
| 1490 | 'xlink:href': config.backgroundUrl |
| 1491 | }) |
| 1492 | .style({ |
| 1493 | visibility: showBg() |
| 1494 | }); |
| 1495 | } |
| 1496 | |
| 1497 | function loadGeoMap() { |
| 1498 | fnTrace('loadGeoMap', geoJsonUrl); |
| 1499 | var w = network.view.width(), |
| 1500 | h = network.view.height(); |
| 1501 | |
| 1502 | // TODO: load map layer from GeoJSON stored in 'geoJson' var... |
| 1503 | // bgImg = svg.insert('<svg-element-type>', '#topo-G') ... |
| 1504 | |
| 1505 | // TODO: Paul |
| 1506 | } |
| 1507 | |
| 1508 | function resizeBg(view) { |
| 1509 | if (geoJson) { |
| 1510 | // TODO : resize GeoJSON map |
| 1511 | |
| 1512 | // TODO: Paul |
| 1513 | |
| 1514 | } else if (bgImg) { |
| 1515 | setSize(bgImg, view); |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 1516 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1517 | } |
| 1518 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1519 | function resize(view, ctx, flags) { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1520 | setSize(svg, view); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1521 | resizeBg(view); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1522 | |
| 1523 | // TODO: hook to recompute layout, perhaps? work with zoom/pan code |
| 1524 | // adjust force layout size |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | |
| 1528 | // ============================== |
| 1529 | // View registration |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1530 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 1531 | onos.ui.addView('topo', { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1532 | preload: preload, |
| 1533 | load: load, |
| 1534 | resize: resize |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1535 | }); |
| 1536 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1537 | detailPane = onos.ui.addFloatingPanel('topo-detail'); |
| 1538 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1539 | }(ONOS)); |