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