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 | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 560 | updateInstance: stillToImplement, |
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 | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 646 | function updateDevice(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 647 | evTrace(data); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 648 | var device = data.payload, |
| 649 | id = device.id, |
| 650 | nodeData = network.lookup[id]; |
| 651 | if (nodeData) { |
| 652 | $.extend(nodeData, device); |
| 653 | updateDeviceState(nodeData); |
| 654 | } else { |
| 655 | logicError('updateDevice lookup fail. ID = "' + id + '"'); |
| 656 | } |
| 657 | } |
| 658 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 659 | function updateLink(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 660 | evTrace(data); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 661 | var link = data.payload, |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 662 | result = findLink(link, 'update'), |
| 663 | bad = result.badLogic; |
| 664 | if (bad) { |
| 665 | logicError(bad + ': ' + link.id); |
| 666 | return; |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 667 | } |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 668 | result.updateWith(link); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 671 | function updateHost(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 672 | evTrace(data); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 673 | var host = data.payload, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 674 | id = host.id, |
| 675 | hostData = network.lookup[id]; |
| 676 | if (hostData) { |
| 677 | $.extend(hostData, host); |
| 678 | updateHostState(hostData); |
| 679 | } else { |
| 680 | logicError('updateHost lookup fail. ID = "' + id + '"'); |
| 681 | } |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 684 | // TODO: fold removeX(...) methods into base method - remove dup code |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 685 | function removeLink(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 686 | evTrace(data); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 687 | var link = data.payload, |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 688 | result = findLink(link, 'remove'), |
| 689 | bad = result.badLogic; |
| 690 | if (bad) { |
| 691 | logicError(bad + ': ' + link.id); |
| 692 | return; |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 693 | } |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 694 | result.removeRawLink(); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 697 | function removeHost(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 698 | evTrace(data); |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 699 | var host = data.payload, |
| 700 | id = host.id, |
| 701 | hostData = network.lookup[id]; |
| 702 | if (hostData) { |
| 703 | removeHostElement(hostData); |
| 704 | } else { |
| 705 | logicError('removeHost lookup fail. ID = "' + id + '"'); |
| 706 | } |
| 707 | } |
| 708 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 709 | function showDetails(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 710 | evTrace(data); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 711 | populateDetails(data.payload); |
| 712 | detailPane.show(); |
| 713 | } |
| 714 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 715 | function showTraffic(data) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 716 | evTrace(data); |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 717 | var paths = data.payload.paths; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 718 | |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 719 | // Revert any links hilighted previously. |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 720 | link.classed('primary secondary animated optical', false); |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 721 | // Remove all previous labels. |
| 722 | removeLinkLabels(); |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 723 | |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 724 | // Now hilight all links in the paths payload, and attach |
| 725 | // labels to them, if they are defined. |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 726 | paths.forEach(function (p) { |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 727 | var n = p.links.length, |
| 728 | i, |
| 729 | ldata; |
| 730 | |
| 731 | for (i=0; i<n; i++) { |
| 732 | ldata = findLinkById(p.links[i]); |
| 733 | if (ldata) { |
| 734 | ldata.el.classed(p.class, true); |
| 735 | ldata.label = p.labels[i]; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 736 | } |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 737 | } |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 738 | }); |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 739 | updateLinks(); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 742 | // ............................... |
| 743 | |
| 744 | function stillToImplement(data) { |
| 745 | var p = data.payload; |
| 746 | note(data.event, p.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 747 | network.view.alert('Not yet implemented: "' + data.event + '"'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 748 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 749 | |
| 750 | function unknownEvent(data) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 751 | network.view.alert('Unknown event type: "' + data.event + '"'); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | function handleServerEvent(data) { |
| 755 | var fn = eventDispatch[data.event] || unknownEvent; |
| 756 | fn(data); |
| 757 | } |
| 758 | |
| 759 | // ============================== |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 760 | // Out-going messages... |
| 761 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 762 | function userFeedback(msg) { |
| 763 | // for now, use the alert pane as is. Maybe different alert style in |
| 764 | // the future (centered on view; dismiss button?) |
| 765 | network.view.alert(msg); |
| 766 | } |
| 767 | |
| 768 | function nSel() { |
| 769 | return selectOrder.length; |
| 770 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 771 | function getSel(idx) { |
| 772 | return selections[selectOrder[idx]]; |
| 773 | } |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 774 | function getSelId(idx) { |
| 775 | return getSel(idx).obj.id; |
| 776 | } |
| 777 | function allSelectionsClass(cls) { |
| 778 | for (var i=0, n=nSel(); i<n; i++) { |
| 779 | if (getSel(i).obj.class !== cls) { |
| 780 | return false; |
| 781 | } |
| 782 | } |
| 783 | return true; |
| 784 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 785 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 786 | // request details for the selected element |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 787 | // invoked from selection of a single node. |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 788 | function requestDetails() { |
| 789 | var data = getSel(0).obj, |
| 790 | payload = { |
| 791 | id: data.id, |
| 792 | class: data.class |
| 793 | }; |
| 794 | sendMessage('requestDetails', payload); |
| 795 | } |
| 796 | |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 797 | function addIntentAction() { |
| 798 | sendMessage('addHostIntent', { |
| 799 | one: getSelId(0), |
Thomas Vachuska | 82f2c62 | 2014-11-17 12:23:18 -0800 | [diff] [blame] | 800 | two: getSelId(1), |
| 801 | ids: [ getSelId(0), getSelId(1) ] |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 802 | }); |
| 803 | } |
| 804 | |
| 805 | function showTrafficAction() { |
| 806 | // if nothing is hovered over, and nothing selected, send cancel request |
| 807 | if (!hovered && nSel() === 0) { |
| 808 | sendMessage('cancelTraffic', {}); |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | // NOTE: hover is only populated if "show traffic on hover" is |
| 813 | // toggled on, and the item hovered is a host... |
| 814 | var hoverId = (trafficHover() && hovered && hovered.class === 'host') |
| 815 | ? hovered.id : ''; |
| 816 | sendMessage('requestTraffic', { |
| 817 | ids: selectOrder, |
| 818 | hover: hoverId |
| 819 | }); |
| 820 | } |
| 821 | |
| 822 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 823 | // ============================== |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 824 | // onos instance panel functions |
| 825 | |
| 826 | function updateInstances() { |
| 827 | var onoses = oiBox.el.selectAll('.onosInst') |
| 828 | .data(onosOrder, function (d) { return d.id; }); |
| 829 | |
| 830 | // operate on existing onoses if necessary |
| 831 | |
| 832 | var entering = onoses.enter() |
| 833 | .append('div') |
| 834 | .attr('class', 'onosInst') |
| 835 | .classed('online', function (d) { return d.online; }) |
Simon Hunt | 9c15eca | 2014-11-15 18:37:59 -0800 | [diff] [blame] | 836 | .on('click', clickInst); |
| 837 | |
| 838 | entering.each(function (d, i) { |
| 839 | var el = d3.select(this), |
| 840 | img; |
| 841 | |
| 842 | $('<img src="img/host.png">').appendTo(el); |
| 843 | img = el.select('img') |
| 844 | .attr({ |
| 845 | width: 40, |
| 846 | top: -10, |
| 847 | left: -10 |
| 848 | }) |
| 849 | .style({ |
| 850 | }); |
| 851 | |
| 852 | $('<div>').attr('class', 'onosTitle').text(d.id).appendTo(el); |
| 853 | |
| 854 | // is the UI attached to this instance? |
| 855 | // TODO: need uiAttached boolean in instance data |
| 856 | //if (d.uiAttached) { |
| 857 | if (i === 0) { |
| 858 | $('<img src="img/ui.png">').attr('class','ui').appendTo(el); |
| 859 | } |
| 860 | }); |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 861 | |
| 862 | // operate on existing + new onoses here |
| 863 | |
| 864 | // the departed... |
| 865 | var exiting = onoses.exit() |
| 866 | .transition() |
| 867 | .style('opacity', 0) |
| 868 | .remove(); |
| 869 | } |
| 870 | |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 871 | function clickInst(d) { |
| 872 | var el = d3.select(this), |
| 873 | aff = el.classed('affinity'); |
| 874 | if (!aff) { |
| 875 | setAffinity(el, d); |
| 876 | } else { |
| 877 | cancelAffinity(); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | function setAffinity(el, d) { |
| 882 | d3.selectAll('.onosInst') |
| 883 | .classed('mastership', true) |
| 884 | .classed('affinity', false); |
| 885 | el.classed('affinity', true); |
| 886 | |
| 887 | suppressLayers(true); |
| 888 | node.each(function (n) { |
| 889 | if (n.master === d.id) { |
| 890 | n.el.classed('suppressed', false); |
| 891 | } |
| 892 | }); |
| 893 | oiShowMaster = true; |
| 894 | } |
| 895 | |
| 896 | function cancelAffinity() { |
| 897 | d3.selectAll('.onosInst') |
| 898 | .classed('mastership affinity', false); |
| 899 | restoreLayerState(); |
| 900 | oiShowMaster = false; |
| 901 | } |
| 902 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 903 | // ============================== |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 904 | // force layout modification functions |
| 905 | |
| 906 | function translate(x, y) { |
| 907 | return 'translate(' + x + ',' + y + ')'; |
| 908 | } |
| 909 | |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 910 | function rotate(deg) { |
| 911 | return 'rotate(' + deg + ')'; |
| 912 | } |
| 913 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 914 | function missMsg(what, id) { |
| 915 | return '\n[' + what + '] "' + id + '" missing '; |
| 916 | } |
| 917 | |
| 918 | function linkEndPoints(srcId, dstId) { |
| 919 | var srcNode = network.lookup[srcId], |
| 920 | dstNode = network.lookup[dstId], |
| 921 | sMiss = !srcNode ? missMsg('src', srcId) : '', |
| 922 | dMiss = !dstNode ? missMsg('dst', dstId) : ''; |
| 923 | |
| 924 | if (sMiss || dMiss) { |
| 925 | logicError('Node(s) not on map for link:\n' + sMiss + dMiss); |
| 926 | return null; |
| 927 | } |
| 928 | return { |
| 929 | source: srcNode, |
| 930 | target: dstNode, |
| 931 | x1: srcNode.x, |
| 932 | y1: srcNode.y, |
| 933 | x2: dstNode.x, |
| 934 | y2: dstNode.y |
| 935 | }; |
| 936 | } |
| 937 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 938 | function createHostLink(host) { |
| 939 | var src = host.id, |
| 940 | dst = host.cp.device, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 941 | id = host.ingress, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 942 | lnk = linkEndPoints(src, dst); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 943 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 944 | if (!lnk) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 945 | return null; |
| 946 | } |
| 947 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 948 | // Synthesize link ... |
| 949 | $.extend(lnk, { |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 950 | key: id, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 951 | class: 'link', |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 952 | |
| 953 | type: function () { return 'hostLink'; }, |
| 954 | // TODO: ideally, we should see if our edge switch is online... |
| 955 | online: function () { return true; }, |
| 956 | linkWidth: function () { return 1; } |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 957 | }); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 958 | return lnk; |
| 959 | } |
| 960 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 961 | function createLink(link) { |
| 962 | var lnk = linkEndPoints(link.src, link.dst), |
| 963 | type = link.type; |
| 964 | |
| 965 | if (!lnk) { |
| 966 | return null; |
| 967 | } |
| 968 | |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 969 | $.extend(lnk, { |
| 970 | key: link.id, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 971 | class: 'link', |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 972 | fromSource: link, |
| 973 | |
| 974 | // functions to aggregate dual link state |
| 975 | type: function () { |
| 976 | var s = lnk.fromSource, |
| 977 | t = lnk.fromTarget; |
| 978 | return (s && s.type) || (t && t.type) || defaultLinkType; |
| 979 | }, |
| 980 | online: function () { |
| 981 | var s = lnk.fromSource, |
| 982 | t = lnk.fromTarget; |
| 983 | return (s && s.online) || (t && t.online); |
| 984 | }, |
| 985 | linkWidth: function () { |
| 986 | var s = lnk.fromSource, |
| 987 | t = lnk.fromTarget, |
| 988 | ws = (s && s.linkWidth) || 0, |
| 989 | wt = (t && t.linkWidth) || 0; |
| 990 | return Math.max(ws, wt); |
| 991 | } |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 992 | }); |
| 993 | return lnk; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 994 | } |
| 995 | |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 996 | function removeLinkLabels() { |
| 997 | network.links.forEach(function (d) { |
| 998 | d.label = ''; |
| 999 | }); |
| 1000 | } |
| 1001 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1002 | var widthRatio = 1.4, |
| 1003 | linkScale = d3.scale.linear() |
| 1004 | .domain([1, 12]) |
| 1005 | .range([widthRatio, 12 * widthRatio]) |
| 1006 | .clamp(true); |
| 1007 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1008 | function updateLinks() { |
| 1009 | link = linkG.selectAll('.link') |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 1010 | .data(network.links, function (d) { return d.key; }); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1011 | |
| 1012 | // operate on existing links, if necessary |
| 1013 | // link .foo() .bar() ... |
| 1014 | |
| 1015 | // operate on entering links: |
| 1016 | var entering = link.enter() |
| 1017 | .append('line') |
| 1018 | .attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1019 | x1: function (d) { return d.x1; }, |
| 1020 | y1: function (d) { return d.y1; }, |
| 1021 | x2: function (d) { return d.x2; }, |
| 1022 | y2: function (d) { return d.y2; }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1023 | stroke: config.topo.linkInColor, |
| 1024 | 'stroke-width': config.topo.linkInWidth |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1025 | }); |
| 1026 | |
| 1027 | // augment links |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1028 | entering.each(function (d) { |
| 1029 | var link = d3.select(this); |
| 1030 | // provide ref to element selection from backing data.... |
| 1031 | d.el = link; |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 1032 | restyleLinkElement(d); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1033 | }); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 1034 | |
| 1035 | // operate on both existing and new links, if necessary |
| 1036 | //link .foo() .bar() ... |
| 1037 | |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 1038 | // apply or remove labels |
| 1039 | var labelData = getLabelData(); |
| 1040 | applyLinkLabels(labelData); |
| 1041 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 1042 | // operate on exiting links: |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 1043 | link.exit() |
Simon Hunt | 13bf9c8 | 2014-11-18 07:26:44 -0800 | [diff] [blame] | 1044 | .attr('stroke-dasharray', '3, 3') |
| 1045 | .style('opacity', 0.5) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1046 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1047 | .duration(1500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1048 | .attr({ |
Simon Hunt | 13bf9c8 | 2014-11-18 07:26:44 -0800 | [diff] [blame] | 1049 | 'stroke-dasharray': '3, 12', |
| 1050 | stroke: config.topo.linkOutColor, |
| 1051 | 'stroke-width': config.topo.linkOutWidth |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1052 | }) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1053 | .style('opacity', 0.0) |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 1054 | .remove(); |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 1055 | |
| 1056 | // NOTE: invoke a single tick to force the labels to position |
| 1057 | // onto their links. |
| 1058 | tick(); |
| 1059 | } |
| 1060 | |
| 1061 | function getLabelData() { |
| 1062 | // create the backing data for showing labels.. |
| 1063 | var data = []; |
| 1064 | link.each(function (d) { |
| 1065 | if (d.label) { |
| 1066 | data.push({ |
| 1067 | id: 'lab-' + d.key, |
| 1068 | key: d.key, |
| 1069 | label: d.label, |
| 1070 | ldata: d |
| 1071 | }); |
| 1072 | } |
| 1073 | }); |
| 1074 | return data; |
| 1075 | } |
| 1076 | |
| 1077 | var linkLabelOffset = '0.3em'; |
| 1078 | |
| 1079 | function applyLinkLabels(data) { |
| 1080 | var entering; |
| 1081 | |
| 1082 | linkLabel = linkLabelG.selectAll('.linkLabel') |
| 1083 | .data(data, function (d) { return d.id; }); |
| 1084 | |
Thomas Vachuska | f75b7ab | 2014-11-19 12:15:55 -0800 | [diff] [blame^] | 1085 | linkLabel.text(function (d) { return d.label; }); |
| 1086 | |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 1087 | entering = linkLabel.enter().append('g') |
| 1088 | .classed('linkLabel', true) |
| 1089 | .attr('id', function (d) { return d.id; }); |
| 1090 | |
| 1091 | entering.each(function (d) { |
| 1092 | var el = d3.select(this), |
| 1093 | rect, |
| 1094 | text, |
| 1095 | parms = { |
| 1096 | x1: d.ldata.x1, |
| 1097 | y1: d.ldata.y1, |
| 1098 | x2: d.ldata.x2, |
| 1099 | y2: d.ldata.y2 |
| 1100 | }; |
| 1101 | |
| 1102 | d.el = el; |
| 1103 | rect = el.append('rect'); |
| 1104 | text = el.append('text').text(d.label); |
| 1105 | rect.attr(rectAroundText(el)); |
| 1106 | text.attr('dy', linkLabelOffset); |
| 1107 | |
| 1108 | el.attr('transform', transformLabel(parms)); |
| 1109 | }); |
| 1110 | |
| 1111 | // Remove any links that are no longer required. |
| 1112 | linkLabel.exit().remove(); |
| 1113 | } |
| 1114 | |
| 1115 | function rectAroundText(el) { |
| 1116 | var text = el.select('text'), |
| 1117 | box = text.node().getBBox(); |
| 1118 | |
| 1119 | // translate the bbox so that it is centered on [x,y] |
| 1120 | box.x = -box.width / 2; |
| 1121 | box.y = -box.height / 2; |
| 1122 | |
| 1123 | // add padding |
| 1124 | box.x -= 1; |
| 1125 | box.width += 2; |
| 1126 | return box; |
| 1127 | } |
| 1128 | |
| 1129 | function transformLabel(p) { |
| 1130 | var dx = p.x2 - p.x1, |
| 1131 | dy = p.y2 - p.y1, |
| 1132 | xMid = dx/2 + p.x1, |
| 1133 | yMid = dy/2 + p.y1; |
| 1134 | //length = Math.sqrt(dx*dx + dy*dy), |
| 1135 | //rads = Math.asin(dy/length), |
| 1136 | //degs = rads / (Math.PI*2) * 360; |
| 1137 | |
| 1138 | return translate(xMid, yMid); |
| 1139 | |
| 1140 | // TODO: consider making label parallel to line |
| 1141 | //return [ |
| 1142 | // translate(xMid, yMid), |
| 1143 | // rotate(degs), |
| 1144 | // translate(0, 8) |
| 1145 | //].join(''); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | function createDeviceNode(device) { |
| 1149 | // start with the object as is |
| 1150 | var node = device, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1151 | type = device.type, |
| 1152 | svgCls = type ? 'node device ' + type : 'node device'; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1153 | |
| 1154 | // Augment as needed... |
| 1155 | node.class = 'device'; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1156 | node.svgClass = device.online ? svgCls + ' online' : svgCls; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1157 | positionNode(node); |
| 1158 | |
| 1159 | // cache label array length |
| 1160 | network.deviceLabelCount = device.labels.length; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1161 | return node; |
| 1162 | } |
| 1163 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1164 | function createHostNode(host) { |
| 1165 | // start with the object as is |
| 1166 | var node = host; |
| 1167 | |
| 1168 | // Augment as needed... |
| 1169 | node.class = 'host'; |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1170 | if (!node.type) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1171 | node.type = 'endstation'; |
| 1172 | } |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1173 | node.svgClass = 'node host ' + node.type; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1174 | positionNode(node); |
| 1175 | |
| 1176 | // cache label array length |
| 1177 | network.hostLabelCount = host.labels.length; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1178 | return node; |
| 1179 | } |
| 1180 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1181 | function positionNode(node) { |
| 1182 | var meta = node.metaUi, |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 1183 | x = meta && meta.x, |
| 1184 | y = meta && meta.y, |
| 1185 | xy; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1186 | |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 1187 | // If we have [x,y] already, use that... |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1188 | if (x && y) { |
| 1189 | node.fixed = true; |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 1190 | node.x = x; |
| 1191 | node.y = y; |
| 1192 | return; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1193 | } |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 1194 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1195 | var location = node.location; |
| 1196 | if (location && location.type === 'latlng') { |
| 1197 | var coord = geoMapProjection([location.lng, location.lat]); |
| 1198 | node.fixed = true; |
| 1199 | node.x = coord[0]; |
| 1200 | node.y = coord[1]; |
| 1201 | return; |
| 1202 | } |
| 1203 | |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 1204 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 1205 | // (center of the view) causes them to explode outwards when |
| 1206 | // the force layout kicks in. So, we spread them out a bit |
| 1207 | // initially, to provide a more serene layout convergence. |
| 1208 | // Additionally, if the node is a host, we place it near |
| 1209 | // the device it is connected to. |
| 1210 | |
| 1211 | function spread(s) { |
| 1212 | return Math.floor((Math.random() * s) - s/2); |
| 1213 | } |
| 1214 | |
| 1215 | function randDim(dim) { |
| 1216 | return dim / 2 + spread(dim * 0.7071); |
| 1217 | } |
| 1218 | |
| 1219 | function rand() { |
| 1220 | return { |
| 1221 | x: randDim(network.view.width()), |
| 1222 | y: randDim(network.view.height()) |
| 1223 | }; |
| 1224 | } |
| 1225 | |
| 1226 | function near(node) { |
| 1227 | var min = 12, |
| 1228 | dx = spread(12), |
| 1229 | dy = spread(12); |
| 1230 | return { |
| 1231 | x: node.x + min + dx, |
| 1232 | y: node.y + min + dy |
| 1233 | }; |
| 1234 | } |
| 1235 | |
| 1236 | function getDevice(cp) { |
| 1237 | var d = network.lookup[cp.device]; |
| 1238 | return d || rand(); |
| 1239 | } |
| 1240 | |
| 1241 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
| 1242 | $.extend(node, xy); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1245 | function iconUrl(d) { |
| 1246 | return 'img/' + d.type + '.png'; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | // returns the newly computed bounding box of the rectangle |
| 1250 | function adjustRectToFitText(n) { |
| 1251 | var text = n.select('text'), |
| 1252 | box = text.node().getBBox(), |
| 1253 | lab = config.labels; |
| 1254 | |
| 1255 | text.attr('text-anchor', 'middle') |
| 1256 | .attr('y', '-0.8em') |
| 1257 | .attr('x', lab.imgPad/2); |
| 1258 | |
| 1259 | // translate the bbox so that it is centered on [x,y] |
| 1260 | box.x = -box.width / 2; |
| 1261 | box.y = -box.height / 2; |
| 1262 | |
| 1263 | // add padding |
| 1264 | box.x -= (lab.padLR + lab.imgPad/2); |
| 1265 | box.width += lab.padLR * 2 + lab.imgPad; |
| 1266 | box.y -= lab.padTB; |
| 1267 | box.height += lab.padTB * 2; |
| 1268 | |
| 1269 | return box; |
| 1270 | } |
| 1271 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1272 | function mkSvgClass(d) { |
| 1273 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 1274 | } |
| 1275 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1276 | function hostLabel(d) { |
| 1277 | var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0; |
| 1278 | return d.labels[idx]; |
| 1279 | } |
| 1280 | function deviceLabel(d) { |
| 1281 | var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0; |
| 1282 | return d.labels[idx]; |
| 1283 | } |
| 1284 | function niceLabel(label) { |
| 1285 | return (label && label.trim()) ? label : '.'; |
| 1286 | } |
| 1287 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1288 | function updateDeviceLabel(d) { |
| 1289 | var label = niceLabel(deviceLabel(d)), |
| 1290 | node = d.el, |
| 1291 | box; |
| 1292 | |
| 1293 | node.select('text') |
| 1294 | .text(label) |
| 1295 | .style('opacity', 0) |
| 1296 | .transition() |
| 1297 | .style('opacity', 1); |
| 1298 | |
| 1299 | box = adjustRectToFitText(node); |
| 1300 | |
| 1301 | node.select('rect') |
| 1302 | .transition() |
| 1303 | .attr(box); |
| 1304 | |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1305 | node.select('image') |
| 1306 | .transition() |
| 1307 | .attr('x', box.x + config.icons.xoff) |
| 1308 | .attr('y', box.y + config.icons.yoff); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | function updateHostLabel(d) { |
| 1312 | var label = hostLabel(d), |
| 1313 | host = d.el; |
| 1314 | |
| 1315 | host.select('text').text(label); |
| 1316 | } |
| 1317 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1318 | function updateDeviceState(nodeData) { |
| 1319 | nodeData.el.classed('online', nodeData.online); |
| 1320 | updateDeviceLabel(nodeData); |
| 1321 | // TODO: review what else might need to be updated |
| 1322 | } |
| 1323 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1324 | function updateLinkState(linkData) { |
| 1325 | updateLinkWidth(linkData); |
Thomas Vachuska | badb93f | 2014-11-15 23:51:17 -0800 | [diff] [blame] | 1326 | linkData.el.classed('inactive', !linkData.online); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1327 | // TODO: review what else might need to be updated |
| 1328 | // update label, if showing |
| 1329 | } |
| 1330 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1331 | function updateHostState(hostData) { |
| 1332 | updateHostLabel(hostData); |
| 1333 | // TODO: review what else might need to be updated |
| 1334 | } |
| 1335 | |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1336 | function nodeMouseOver(d) { |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1337 | hovered = d; |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1338 | if (trafficHover() && d.class === 'host') { |
| 1339 | showTrafficAction(); |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | function nodeMouseOut(d) { |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1344 | hovered = null; |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1345 | if (trafficHover() && d.class === 'host') { |
| 1346 | showTrafficAction(); |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1347 | } |
| 1348 | } |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1349 | |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1350 | function addHostIcon(node, radius, iconId) { |
| 1351 | var dim = radius * 1.5, |
| 1352 | xlate = -dim / 2; |
| 1353 | |
| 1354 | node.append('svg:image') |
| 1355 | .attr('transform', translate(xlate,xlate)) |
| 1356 | .attr('xlink:href', 'img/' + iconId + '.png') |
| 1357 | .attr('width', dim) |
| 1358 | .attr('height', dim); |
| 1359 | } |
| 1360 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1361 | function updateNodes() { |
| 1362 | node = nodeG.selectAll('.node') |
| 1363 | .data(network.nodes, function (d) { return d.id; }); |
| 1364 | |
| 1365 | // operate on existing nodes, if necessary |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1366 | // update host labels |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1367 | //node .foo() .bar() ... |
| 1368 | |
| 1369 | // operate on entering nodes: |
| 1370 | var entering = node.enter() |
| 1371 | .append('g') |
| 1372 | .attr({ |
| 1373 | id: function (d) { return safeId(d.id); }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1374 | class: mkSvgClass, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1375 | transform: function (d) { return translate(d.x, d.y); }, |
| 1376 | opacity: 0 |
| 1377 | }) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1378 | .call(network.drag) |
Simon Hunt | 6ac93f3 | 2014-11-13 12:17:27 -0800 | [diff] [blame] | 1379 | .on('mouseover', nodeMouseOver) |
| 1380 | .on('mouseout', nodeMouseOut) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1381 | .transition() |
| 1382 | .attr('opacity', 1); |
| 1383 | |
| 1384 | // augment device nodes... |
| 1385 | entering.filter('.device').each(function (d) { |
| 1386 | var node = d3.select(this), |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1387 | icon = iconUrl(d), |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1388 | label = niceLabel(deviceLabel(d)), |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1389 | box; |
| 1390 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1391 | // provide ref to element from backing data.... |
| 1392 | d.el = node; |
| 1393 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1394 | node.append('rect') |
| 1395 | .attr({ |
| 1396 | 'rx': 5, |
| 1397 | 'ry': 5 |
| 1398 | }); |
| 1399 | |
| 1400 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1401 | .text(label) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1402 | .attr('dy', '1.1em'); |
| 1403 | |
| 1404 | box = adjustRectToFitText(node); |
| 1405 | |
| 1406 | node.select('rect') |
| 1407 | .attr(box); |
| 1408 | |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1409 | if (icon) { |
| 1410 | var cfg = config.icons; |
| 1411 | node.append('rect') |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1412 | .attr({ |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1413 | x: box.x + config.icons.xoff, |
| 1414 | y: box.y + config.icons.yoff, |
| 1415 | width: cfg.w, |
| 1416 | height: cfg.h, |
| 1417 | rx: 4 |
| 1418 | }).style({ |
| 1419 | stroke: '#000', |
| 1420 | fill: '#ddd' |
| 1421 | }); |
| 1422 | node.append('svg:image') |
| 1423 | .attr({ |
| 1424 | x: box.x + config.icons.xoff + 2, |
| 1425 | y: box.y + config.icons.yoff + 2, |
| 1426 | width: cfg.w - 4, |
| 1427 | height: cfg.h - 4, |
| 1428 | 'xlink:href': icon |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1429 | }); |
| 1430 | } |
| 1431 | |
| 1432 | // debug function to show the modelled x,y coordinates of nodes... |
| 1433 | if (debug('showNodeXY')) { |
| 1434 | node.select('rect').attr('fill-opacity', 0.5); |
| 1435 | node.append('circle') |
| 1436 | .attr({ |
| 1437 | class: 'debug', |
| 1438 | cx: 0, |
| 1439 | cy: 0, |
| 1440 | r: '3px' |
| 1441 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1442 | } |
| 1443 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1444 | |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1445 | // TODO: better place for this configuration state |
| 1446 | var defaultHostRadius = 9, |
| 1447 | hostRadius = { |
| 1448 | bgpSpeaker: 14, |
| 1449 | router: 14, |
| 1450 | host: 14 |
| 1451 | }, |
| 1452 | hostIcon = { |
| 1453 | bgpSpeaker: 'bgpSpeaker', |
| 1454 | router: 'router', |
| 1455 | host: 'host' |
| 1456 | }; |
| 1457 | |
| 1458 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1459 | // augment host nodes... |
| 1460 | entering.filter('.host').each(function (d) { |
| 1461 | var node = d3.select(this), |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1462 | r = hostRadius[d.type] || defaultHostRadius, |
| 1463 | textDy = r + 10, |
| 1464 | icon = hostIcon[d.type]; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1465 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1466 | // provide ref to element from backing data.... |
| 1467 | d.el = node; |
| 1468 | |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1469 | node.append('circle') |
| 1470 | .attr('r', r); |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1471 | |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1472 | if (icon) { |
| 1473 | addHostIcon(node, r, icon); |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1474 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1475 | |
Paul Greyson | 29cd58f | 2014-11-18 13:14:57 -0800 | [diff] [blame] | 1476 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1477 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1478 | .text(hostLabel) |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1479 | .attr('dy', textDy) |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1480 | .attr('text-anchor', 'middle'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1481 | |
| 1482 | // debug function to show the modelled x,y coordinates of nodes... |
| 1483 | if (debug('showNodeXY')) { |
| 1484 | node.select('circle').attr('fill-opacity', 0.5); |
| 1485 | node.append('circle') |
| 1486 | .attr({ |
| 1487 | class: 'debug', |
| 1488 | cx: 0, |
| 1489 | cy: 0, |
| 1490 | r: '3px' |
| 1491 | }); |
| 1492 | } |
| 1493 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1494 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1495 | // operate on both existing and new nodes, if necessary |
| 1496 | //node .foo() .bar() ... |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1497 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1498 | // operate on exiting nodes: |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1499 | // Note that the node is removed after 2 seconds. |
| 1500 | // Sub element animations should be shorter than 2 seconds. |
| 1501 | var exiting = node.exit() |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 1502 | .transition() |
| 1503 | .duration(2000) |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1504 | .style('opacity', 0) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1505 | .remove(); |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1506 | |
| 1507 | // host node exits.... |
| 1508 | exiting.filter('.host').each(function (d) { |
| 1509 | var node = d3.select(this); |
| 1510 | |
| 1511 | node.select('text') |
| 1512 | .style('opacity', 0.5) |
| 1513 | .transition() |
| 1514 | .duration(1000) |
| 1515 | .style('opacity', 0); |
| 1516 | // note, leave <g>.remove to remove this element |
| 1517 | |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1518 | node.select('circle') |
| 1519 | .style('stroke-fill', '#555') |
| 1520 | .style('fill', '#888') |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 1521 | .style('opacity', 0.5) |
| 1522 | .transition() |
| 1523 | .duration(1500) |
| 1524 | .attr('r', 0); |
| 1525 | // note, leave <g>.remove to remove this element |
| 1526 | |
| 1527 | }); |
| 1528 | |
| 1529 | // TODO: device node exits |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1530 | } |
| 1531 | |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 1532 | function find(key, array) { |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1533 | for (var idx = 0, n = array.length; idx < n; idx++) { |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 1534 | if (array[idx].key === key) { |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1535 | return idx; |
| 1536 | } |
| 1537 | } |
| 1538 | return -1; |
| 1539 | } |
| 1540 | |
| 1541 | function removeLinkElement(linkData) { |
Simon Hunt | 8257f4c | 2014-11-16 19:34:54 -0800 | [diff] [blame] | 1542 | var idx = find(linkData.key, network.links), |
| 1543 | removed; |
| 1544 | if (idx >=0) { |
| 1545 | // remove from links array |
| 1546 | removed = network.links.splice(idx, 1); |
| 1547 | // remove from lookup cache |
| 1548 | delete network.lookup[removed[0].key]; |
| 1549 | updateLinks(); |
| 1550 | network.force.resume(); |
| 1551 | } |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 1552 | } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1553 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 1554 | function removeHostElement(hostData) { |
| 1555 | // first, remove associated hostLink... |
| 1556 | removeLinkElement(hostData.linkData); |
| 1557 | |
| 1558 | // remove from lookup cache |
| 1559 | delete network.lookup[hostData.id]; |
| 1560 | // remove from nodes array |
| 1561 | var idx = find(hostData.id, network.nodes); |
| 1562 | network.nodes.splice(idx, 1); |
| 1563 | // remove from SVG |
| 1564 | updateNodes(); |
| 1565 | network.force.resume(); |
| 1566 | } |
| 1567 | |
| 1568 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1569 | function tick() { |
| 1570 | node.attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1571 | transform: function (d) { return translate(d.x, d.y); } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1572 | }); |
| 1573 | |
| 1574 | link.attr({ |
| 1575 | x1: function (d) { return d.source.x; }, |
| 1576 | y1: function (d) { return d.source.y; }, |
| 1577 | x2: function (d) { return d.target.x; }, |
| 1578 | y2: function (d) { return d.target.y; } |
| 1579 | }); |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 1580 | |
| 1581 | linkLabel.each(function (d) { |
| 1582 | var el = d3.select(this); |
| 1583 | var lnk = findLinkById(d.key), |
| 1584 | parms = { |
| 1585 | x1: lnk.source.x, |
| 1586 | y1: lnk.source.y, |
| 1587 | x2: lnk.target.x, |
| 1588 | y2: lnk.target.y |
| 1589 | }; |
| 1590 | el.attr('transform', transformLabel(parms)); |
| 1591 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1592 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1593 | |
| 1594 | // ============================== |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1595 | // Web-Socket for live data |
| 1596 | |
| 1597 | function webSockUrl() { |
| 1598 | return document.location.toString() |
| 1599 | .replace(/\#.*/, '') |
| 1600 | .replace('http://', 'ws://') |
| 1601 | .replace('https://', 'wss://') |
| 1602 | .replace('index2.html', config.webSockUrl); |
| 1603 | } |
| 1604 | |
| 1605 | webSock = { |
| 1606 | ws : null, |
| 1607 | |
| 1608 | connect : function() { |
| 1609 | webSock.ws = new WebSocket(webSockUrl()); |
| 1610 | |
| 1611 | webSock.ws.onopen = function() { |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1612 | noWebSock(false); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1613 | }; |
| 1614 | |
| 1615 | webSock.ws.onmessage = function(m) { |
| 1616 | if (m.data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1617 | wsTraceRx(m.data); |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 1618 | handleServerEvent(JSON.parse(m.data)); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1619 | } |
| 1620 | }; |
| 1621 | |
| 1622 | webSock.ws.onclose = function(m) { |
| 1623 | webSock.ws = null; |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1624 | noWebSock(true); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1625 | }; |
| 1626 | }, |
| 1627 | |
| 1628 | send : function(text) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1629 | if (text != null) { |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1630 | webSock._send(text); |
| 1631 | } |
| 1632 | }, |
| 1633 | |
| 1634 | _send : function(message) { |
| 1635 | if (webSock.ws) { |
| 1636 | webSock.ws.send(message); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1637 | } else if (config.useLiveData) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1638 | network.view.alert('no web socket open\n\n' + message); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 1639 | } else { |
| 1640 | console.log('WS Send: ' + JSON.stringify(message)); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | }; |
| 1645 | |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1646 | function noWebSock(b) { |
| 1647 | mask.style('display',b ? 'block' : 'none'); |
| 1648 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1649 | |
| 1650 | function sendMessage(evType, payload) { |
| 1651 | var toSend = { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1652 | event: evType, |
| 1653 | sid: ++sid, |
| 1654 | payload: payload |
| 1655 | }, |
| 1656 | asText = JSON.stringify(toSend); |
| 1657 | wsTraceTx(asText); |
| 1658 | webSock.send(asText); |
Simon Hunt | c76ae89 | 2014-11-18 17:31:51 -0800 | [diff] [blame] | 1659 | |
| 1660 | // Temporary measure for debugging UI behavior ... |
| 1661 | if (!config.useLiveData) { |
| 1662 | handleTestSend(toSend); |
| 1663 | } |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | function wsTraceTx(msg) { |
| 1667 | wsTrace('tx', msg); |
| 1668 | } |
| 1669 | function wsTraceRx(msg) { |
| 1670 | wsTrace('rx', msg); |
| 1671 | } |
| 1672 | function wsTrace(rxtx, msg) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1673 | console.log('[' + rxtx + '] ' + msg); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
Simon Hunt | c76ae89 | 2014-11-18 17:31:51 -0800 | [diff] [blame] | 1676 | // NOTE: Temporary hardcoded example for showing detail pane |
| 1677 | // while we fine- |
| 1678 | // Probably should not merge this change... |
| 1679 | function handleTestSend(msg) { |
| 1680 | if (msg.event === 'requestDetails') { |
| 1681 | showDetails({ |
| 1682 | event: 'showDetails', |
| 1683 | sid: 1001, |
| 1684 | payload: { |
| 1685 | "id": "of:0000ffffffffff09", |
| 1686 | "type": "roadm", |
| 1687 | "propOrder": [ |
| 1688 | "Name", |
| 1689 | "Vendor", |
| 1690 | "H/W Version", |
| 1691 | "S/W Version", |
| 1692 | "-", |
| 1693 | "Latitude", |
| 1694 | "Longitude", |
| 1695 | "Ports" |
| 1696 | ], |
| 1697 | "props": { |
| 1698 | "Name": null, |
| 1699 | "Vendor": "Linc", |
| 1700 | "H/W Version": "OE", |
| 1701 | "S/W Version": "?", |
| 1702 | "-": "", |
| 1703 | "Latitude": "40.8", |
| 1704 | "Longitude": "73.1", |
| 1705 | "Ports": "2" |
| 1706 | } |
| 1707 | } |
| 1708 | }); |
| 1709 | } |
| 1710 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1711 | |
| 1712 | // ============================== |
| 1713 | // Selection stuff |
| 1714 | |
| 1715 | function selectObject(obj, el) { |
| 1716 | var n, |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1717 | srcEv = d3.event.sourceEvent, |
| 1718 | meta = srcEv.metaKey, |
| 1719 | shift = srcEv.shiftKey; |
| 1720 | |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1721 | if ((panZoom() && !meta) || (!panZoom() && meta)) { |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1722 | return; |
| 1723 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1724 | |
| 1725 | if (el) { |
| 1726 | n = d3.select(el); |
| 1727 | } else { |
| 1728 | node.each(function(d) { |
| 1729 | if (d == obj) { |
| 1730 | n = d3.select(el = this); |
| 1731 | } |
| 1732 | }); |
| 1733 | } |
| 1734 | if (!n) return; |
| 1735 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1736 | if (shift && n.classed('selected')) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1737 | deselectObject(obj.id); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1738 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1739 | return; |
| 1740 | } |
| 1741 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1742 | if (!shift) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1743 | deselectAll(); |
| 1744 | } |
| 1745 | |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1746 | selections[obj.id] = { obj: obj, el: el }; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1747 | selectOrder.push(obj.id); |
| 1748 | |
| 1749 | n.classed('selected', true); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1750 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | function deselectObject(id) { |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1754 | var obj = selections[id], |
| 1755 | idx; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1756 | if (obj) { |
| 1757 | d3.select(obj.el).classed('selected', false); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1758 | delete selections[id]; |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1759 | idx = $.inArray(id, selectOrder); |
| 1760 | if (idx >= 0) { |
| 1761 | selectOrder.splice(idx, 1); |
| 1762 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1763 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | function deselectAll() { |
| 1767 | // deselect all nodes in the network... |
| 1768 | node.classed('selected', false); |
| 1769 | selections = {}; |
| 1770 | selectOrder = []; |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1771 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1772 | } |
| 1773 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1774 | // update the state of the detail pane, based on current selections |
| 1775 | function updateDetailPane() { |
| 1776 | var nSel = selectOrder.length; |
| 1777 | if (!nSel) { |
| 1778 | detailPane.hide(); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1779 | showTrafficAction(); // sends cancelTraffic event |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1780 | } else if (nSel === 1) { |
| 1781 | singleSelect(); |
| 1782 | } else { |
| 1783 | multiSelect(); |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | function singleSelect() { |
| 1788 | requestDetails(); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1789 | // NOTE: detail pane will be shown from showDetails event callback |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | function multiSelect() { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1793 | populateMultiSelect(); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | function addSep(tbody) { |
| 1797 | var tr = tbody.append('tr'); |
| 1798 | $('<hr>').appendTo(tr.append('td').attr('colspan', 2)); |
| 1799 | } |
| 1800 | |
| 1801 | function addProp(tbody, label, value) { |
| 1802 | var tr = tbody.append('tr'); |
| 1803 | |
| 1804 | tr.append('td') |
| 1805 | .attr('class', 'label') |
| 1806 | .text(label + ' :'); |
| 1807 | |
| 1808 | tr.append('td') |
| 1809 | .attr('class', 'value') |
| 1810 | .text(value); |
| 1811 | } |
| 1812 | |
| 1813 | function populateMultiSelect() { |
| 1814 | detailPane.empty(); |
| 1815 | |
| 1816 | var title = detailPane.append("h2"), |
| 1817 | table = detailPane.append("table"), |
| 1818 | tbody = table.append("tbody"); |
| 1819 | |
| 1820 | title.text('Multi-Select...'); |
| 1821 | |
| 1822 | selectOrder.forEach(function (d, i) { |
| 1823 | addProp(tbody, i+1, d); |
| 1824 | }); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1825 | |
| 1826 | addMultiSelectActions(); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | function populateDetails(data) { |
| 1830 | detailPane.empty(); |
| 1831 | |
| 1832 | var title = detailPane.append("h2"), |
| 1833 | table = detailPane.append("table"), |
| 1834 | tbody = table.append("tbody"); |
| 1835 | |
| 1836 | $('<img src="img/' + data.type + '.png">').appendTo(title); |
| 1837 | $('<span>').attr('class', 'icon').text(data.id).appendTo(title); |
| 1838 | |
| 1839 | data.propOrder.forEach(function(p) { |
| 1840 | if (p === '-') { |
| 1841 | addSep(tbody); |
| 1842 | } else { |
| 1843 | addProp(tbody, p, data.props[p]); |
| 1844 | } |
| 1845 | }); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1846 | |
| 1847 | addSingleSelectActions(); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1848 | } |
| 1849 | |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1850 | function addSingleSelectActions() { |
| 1851 | detailPane.append('hr'); |
| 1852 | // always want to allow 'show traffic' |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1853 | addAction(detailPane, 'Show Traffic', showTrafficAction); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | function addMultiSelectActions() { |
| 1857 | detailPane.append('hr'); |
| 1858 | // always want to allow 'show traffic' |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1859 | addAction(detailPane, 'Show Traffic', showTrafficAction); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1860 | // if exactly two hosts are selected, also want 'add host intent' |
| 1861 | if (nSel() === 2 && allSelectionsClass('host')) { |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1862 | addAction(detailPane, 'Add Host Intent', addIntentAction); |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1863 | } |
| 1864 | } |
| 1865 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1866 | function addAction(panel, text, cb) { |
| 1867 | panel.append('div') |
Simon Hunt | d72bc70 | 2014-11-13 18:38:04 -0800 | [diff] [blame] | 1868 | .classed('actionBtn', true) |
| 1869 | .text(text) |
| 1870 | .on('click', cb); |
| 1871 | } |
| 1872 | |
| 1873 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1874 | function zoomPan(scale, translate) { |
| 1875 | zoomPanContainer.attr("transform", "translate(" + translate + ")scale(" + scale + ")"); |
| 1876 | // keep the map lines constant width while zooming |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 1877 | bgImg.style("stroke-width", 2.0 / scale + "px"); |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | function resetZoomPan() { |
| 1881 | zoomPan(1, [0,0]); |
| 1882 | zoom.scale(1).translate([0,0]); |
| 1883 | } |
| 1884 | |
| 1885 | function setupZoomPan() { |
| 1886 | function zoomed() { |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1887 | if (!panZoom() ^ !d3.event.sourceEvent.metaKey) { |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 1888 | zoomPan(d3.event.scale, d3.event.translate); |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | zoom = d3.behavior.zoom() |
| 1893 | .translate([0, 0]) |
| 1894 | .scale(1) |
| 1895 | .scaleExtent([1, 8]) |
| 1896 | .on("zoom", zoomed); |
| 1897 | |
| 1898 | svg.call(zoom); |
| 1899 | } |
| 1900 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1901 | // ============================== |
| 1902 | // Test harness code |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1903 | |
| 1904 | function prepareScenario(view, ctx, dbg) { |
| 1905 | var sc = scenario, |
| 1906 | urlSc = sc.evDir + ctx + sc.evScenario; |
| 1907 | |
| 1908 | if (!ctx) { |
| 1909 | view.alert("No scenario specified (null ctx)"); |
| 1910 | return; |
| 1911 | } |
| 1912 | |
| 1913 | sc.view = view; |
| 1914 | sc.ctx = ctx; |
| 1915 | sc.debug = dbg; |
| 1916 | sc.evNumber = 0; |
| 1917 | |
| 1918 | d3.json(urlSc, function(err, data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1919 | var p = data && data.params || {}, |
| 1920 | desc = data && data.description || null, |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 1921 | intro = data && data.title; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1922 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1923 | if (err) { |
| 1924 | view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err); |
| 1925 | } else { |
| 1926 | sc.params = p; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1927 | if (desc) { |
| 1928 | intro += '\n\n ' + desc.join('\n '); |
| 1929 | } |
| 1930 | view.alert(intro); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1931 | } |
| 1932 | }); |
| 1933 | |
| 1934 | } |
| 1935 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1936 | // ============================== |
| 1937 | // Toggle Buttons in masthead |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1938 | |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1939 | // TODO: toggle button (and other widgets in the masthead) should be provided |
| 1940 | // by the framework; not generated by the view. |
| 1941 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1942 | var showInstances, |
| 1943 | doPanZoom, |
| 1944 | showTrafficOnHover; |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1945 | |
| 1946 | function addButtonBar(view) { |
| 1947 | var bb = d3.select('#mast') |
| 1948 | .append('span').classed('right', true).attr('id', 'bb'); |
| 1949 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1950 | function mkTogBtn(text, cb) { |
| 1951 | return bb.append('span') |
| 1952 | .classed('btn', true) |
| 1953 | .text(text) |
| 1954 | .on('click', cb); |
| 1955 | } |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1956 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1957 | showInstances = mkTogBtn('Show Instances', toggleInst); |
Simon Hunt | e5b7175 | 2014-11-18 20:06:07 -0800 | [diff] [blame] | 1958 | //doPanZoom = mkTogBtn('Pan/Zoom', togglePanZoom); |
| 1959 | //showTrafficOnHover = mkTogBtn('Show traffic on hover', toggleTrafficHover); |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1960 | } |
| 1961 | |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1962 | function instShown() { |
| 1963 | return showInstances.classed('active'); |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 1964 | } |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1965 | function toggleInst() { |
| 1966 | showInstances.classed('active', !instShown()); |
| 1967 | if (instShown()) { |
| 1968 | oiBox.show(); |
| 1969 | } else { |
| 1970 | oiBox.hide(); |
| 1971 | } |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1972 | } |
| 1973 | |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 1974 | function panZoom() { |
Simon Hunt | e5b7175 | 2014-11-18 20:06:07 -0800 | [diff] [blame] | 1975 | return false; |
| 1976 | //return doPanZoom.classed('active'); |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1977 | } |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1978 | function togglePanZoom() { |
| 1979 | doPanZoom.classed('active', !panZoom()); |
| 1980 | } |
| 1981 | |
| 1982 | function trafficHover() { |
Thomas Vachuska | f75b7ab | 2014-11-19 12:15:55 -0800 | [diff] [blame^] | 1983 | return hoverEnabled; |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 1984 | } |
| 1985 | function toggleTrafficHover() { |
| 1986 | showTrafficOnHover.classed('active', !trafficHover()); |
| 1987 | } |
| 1988 | |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 1989 | function loadGlyphs(svg) { |
| 1990 | var defs = svg.append('defs'); |
| 1991 | gly.defBird(defs); |
| 1992 | gly.defBullhorn(defs); |
| 1993 | } |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 1994 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1995 | // ============================== |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1996 | // View life-cycle callbacks |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1997 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1998 | function preload(view, ctx, flags) { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1999 | var w = view.width(), |
| 2000 | h = view.height(), |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 2001 | fcfg = config.force, |
| 2002 | fpad = fcfg.pad, |
| 2003 | forceDim = [w - 2*fpad, h - 2*fpad]; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2004 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 2005 | // NOTE: view.$div is a D3 selection of the view's div |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2006 | var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize; |
| 2007 | svg = view.$div.append('svg').attr('viewBox', viewBox); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 2008 | setSize(svg, view); |
| 2009 | |
Simon Hunt | 7fa116d | 2014-11-17 14:16:55 -0800 | [diff] [blame] | 2010 | loadGlyphs(svg); |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 2011 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 2012 | zoomPanContainer = svg.append('g').attr('id', 'zoomPanContainer'); |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 2013 | setupZoomPan(); |
| 2014 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 2015 | // add blue glow filter to svg layer |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 2016 | d3u.appendGlow(zoomPanContainer); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 2017 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 2018 | // group for the topology |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 2019 | topoG = zoomPanContainer.append('g') |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2020 | .attr('id', 'topo-G') |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 2021 | .attr('transform', fcfg.translate()); |
| 2022 | |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 2023 | // subgroups for links, link labels, and nodes |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 2024 | linkG = topoG.append('g').attr('id', 'links'); |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 2025 | linkLabelG = topoG.append('g').attr('id', 'linkLabels'); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 2026 | nodeG = topoG.append('g').attr('id', 'nodes'); |
| 2027 | |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 2028 | // selection of links, linkLabels, and nodes |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 2029 | link = linkG.selectAll('.link'); |
Simon Hunt | e2575b6 | 2014-11-18 15:25:53 -0800 | [diff] [blame] | 2030 | linkLabel = linkLabelG.selectAll('.linkLabel'); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 2031 | node = nodeG.selectAll('.node'); |
| 2032 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 2033 | function chrg(d) { |
| 2034 | return fcfg.charge[d.class] || -12000; |
| 2035 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 2036 | function ldist(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 2037 | return fcfg.linkDistance[d.type] || 50; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 2038 | } |
| 2039 | function lstrg(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 2040 | // 0.0 - 1.0 |
| 2041 | return fcfg.linkStrength[d.type] || 1.0; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 2044 | function selectCb(d, self) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 2045 | selectObject(d, self); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | function atDragEnd(d, self) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 2049 | // once we've finished moving, pin the node in position |
| 2050 | d.fixed = true; |
| 2051 | d3.select(self).classed('fixed', true); |
| 2052 | if (config.useLiveData) { |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 2053 | sendUpdateMeta(d); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 2054 | } else { |
| 2055 | console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']'); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 2059 | function sendUpdateMeta(d) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 2060 | sendMessage('updateMeta', { |
| 2061 | id: d.id, |
| 2062 | 'class': d.class, |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 2063 | 'memento': { |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 2064 | x: d.x, |
| 2065 | y: d.y |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 2066 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 2067 | }); |
| 2068 | } |
| 2069 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 2070 | // set up the force layout |
| 2071 | network.force = d3.layout.force() |
| 2072 | .size(forceDim) |
| 2073 | .nodes(network.nodes) |
| 2074 | .links(network.links) |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 2075 | .gravity(0.4) |
| 2076 | .friction(0.7) |
| 2077 | .charge(chrg) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 2078 | .linkDistance(ldist) |
| 2079 | .linkStrength(lstrg) |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 2080 | .on('tick', tick); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2081 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 2082 | network.drag = d3u.createDragBehavior(network.force, |
Simon Hunt | deab432 | 2014-11-13 18:49:07 -0800 | [diff] [blame] | 2083 | selectCb, atDragEnd, panZoom); |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 2084 | |
| 2085 | // create mask layer for when we lose connection to server. |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 2086 | // TODO: this should be part of the framework |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 2087 | mask = view.$div.append('div').attr('id','topo-mask'); |
| 2088 | para(mask, 'Oops!'); |
| 2089 | para(mask, 'Web-socket connection to server closed...'); |
| 2090 | para(mask, 'Try refreshing the page.'); |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 2091 | |
| 2092 | mask.append('svg') |
| 2093 | .attr({ |
| 2094 | id: 'mask-bird', |
| 2095 | width: w, |
| 2096 | height: h |
| 2097 | }) |
| 2098 | .append('g') |
| 2099 | .attr('transform', birdTranslate(w, h)) |
| 2100 | .style('opacity', 0.3) |
| 2101 | .append('use') |
| 2102 | .attr({ |
| 2103 | 'xlink:href': '#bird', |
| 2104 | width: config.birdDim, |
| 2105 | height: config.birdDim, |
| 2106 | fill: '#111' |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 2107 | }) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 2108 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2109 | |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 2110 | function para(sel, text) { |
| 2111 | sel.append('p').text(text); |
| 2112 | } |
| 2113 | |
| 2114 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 2115 | function load(view, ctx, flags) { |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 2116 | // resize, in case the window was resized while we were not loaded |
| 2117 | resize(view, ctx, flags); |
| 2118 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 2119 | // cache the view token, so network topo functions can access it |
| 2120 | network.view = view; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 2121 | config.useLiveData = !flags.local; |
| 2122 | |
| 2123 | if (!config.useLiveData) { |
| 2124 | prepareScenario(view, ctx, flags.debug); |
| 2125 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 2126 | |
| 2127 | // set our radio buttons and key bindings |
Simon Hunt | 9462e8c | 2014-11-14 17:28:09 -0800 | [diff] [blame] | 2128 | layerBtnSet = view.setRadio(layerButtons); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 2129 | view.setKeys(keyDispatch); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2130 | |
Simon Hunt | f8e5b4e0 | 2014-11-13 11:17:57 -0800 | [diff] [blame] | 2131 | // patch in our "button bar" for now |
| 2132 | // TODO: implement a more official frameworky way of doing this.. |
| 2133 | addButtonBar(view); |
| 2134 | |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2135 | // Load map data asynchronously; complete startup after that.. |
| 2136 | loadGeoJsonData(); |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 2137 | |
| 2138 | // start the and timer |
| 2139 | var dashIdx = 0; |
| 2140 | antTimer = setInterval(function () { |
| 2141 | // TODO: figure out how to choose Src-->Dst and Dst-->Src, per link |
| 2142 | dashIdx = dashIdx === 0 ? 14 : dashIdx - 2; |
| 2143 | d3.selectAll('.animated').style('stroke-dashoffset', dashIdx); |
| 2144 | }, 35); |
| 2145 | } |
| 2146 | |
| 2147 | function unload(view, ctx, flags) { |
| 2148 | if (antTimer) { |
| 2149 | clearInterval(antTimer); |
| 2150 | antTimer = null; |
| 2151 | } |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | // TODO: move these to config/state portion of script |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2155 | var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2156 | geoJson; |
| 2157 | |
| 2158 | function loadGeoJsonData() { |
| 2159 | d3.json(geoJsonUrl, function (err, data) { |
| 2160 | if (err) { |
| 2161 | // fall back to USA map background |
| 2162 | loadStaticMap(); |
| 2163 | } else { |
| 2164 | geoJson = data; |
| 2165 | loadGeoMap(); |
| 2166 | } |
| 2167 | |
| 2168 | // finally, connect to the server... |
| 2169 | if (config.useLiveData) { |
| 2170 | webSock.connect(); |
| 2171 | } |
| 2172 | }); |
| 2173 | } |
| 2174 | |
| 2175 | function showBg() { |
| 2176 | return config.options.showBackground ? 'visible' : 'hidden'; |
| 2177 | } |
| 2178 | |
| 2179 | function loadStaticMap() { |
| 2180 | fnTrace('loadStaticMap', config.backgroundUrl); |
| 2181 | var w = network.view.width(), |
| 2182 | h = network.view.height(); |
| 2183 | |
| 2184 | // load the background image |
| 2185 | bgImg = svg.insert('svg:image', '#topo-G') |
| 2186 | .attr({ |
| 2187 | id: 'topo-bg', |
| 2188 | width: w, |
| 2189 | height: h, |
| 2190 | 'xlink:href': config.backgroundUrl |
| 2191 | }) |
| 2192 | .style({ |
| 2193 | visibility: showBg() |
| 2194 | }); |
| 2195 | } |
| 2196 | |
| 2197 | function loadGeoMap() { |
| 2198 | fnTrace('loadGeoMap', geoJsonUrl); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2199 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2200 | // extracts the topojson data into geocoordinate-based geometry |
| 2201 | var topoData = topojson.feature(geoJson, geoJson.objects.states); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2202 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2203 | // see: http://bl.ocks.org/mbostock/4707858 |
| 2204 | geoMapProjection = d3.geo.mercator(); |
| 2205 | var path = d3.geo.path().projection(geoMapProjection); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2206 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2207 | geoMapProjection |
| 2208 | .scale(1) |
| 2209 | .translate([0, 0]); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 2210 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2211 | // [[x1,y1],[x2,y2]] |
| 2212 | var b = path.bounds(topoData); |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 2213 | // size map to 95% of minimum dimension to fill space |
| 2214 | 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] | 2215 | 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] | 2216 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 2217 | geoMapProjection |
| 2218 | .scale(s) |
| 2219 | .translate(t); |
| 2220 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 2221 | bgImg = zoomPanContainer.insert("g", '#topo-G'); |
Thomas Vachuska | 8954329 | 2014-11-19 11:28:33 -0800 | [diff] [blame] | 2222 | bgImg.attr('id', 'map').selectAll('path') |
| 2223 | .data(topoData.features) |
| 2224 | .enter() |
| 2225 | .append('path') |
| 2226 | .attr('d', path); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2227 | } |
| 2228 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 2229 | function resize(view, ctx, flags) { |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 2230 | var w = view.width(), |
| 2231 | h = view.height(); |
| 2232 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 2233 | setSize(svg, view); |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 2234 | |
| 2235 | d3.select('#mask-bird').attr({ width: w, height: h}) |
| 2236 | .select('g').attr('transform', birdTranslate(w, h)); |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 2237 | } |
| 2238 | |
Simon Hunt | 12ce12e | 2014-11-15 21:13:19 -0800 | [diff] [blame] | 2239 | function birdTranslate(w, h) { |
| 2240 | var bdim = config.birdDim; |
| 2241 | return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')'; |
| 2242 | } |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 2243 | |
| 2244 | // ============================== |
| 2245 | // View registration |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2246 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 2247 | onos.ui.addView('topo', { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 2248 | preload: preload, |
| 2249 | load: load, |
Simon Hunt | a255a2c | 2014-11-13 22:29:35 -0800 | [diff] [blame] | 2250 | unload: unload, |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 2251 | resize: resize |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2252 | }); |
| 2253 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 2254 | detailPane = onos.ui.addFloatingPanel('topo-detail'); |
Simon Hunt | a5e8914 | 2014-11-14 07:00:33 -0800 | [diff] [blame] | 2255 | oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL'); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 2256 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 2257 | }(ONOS)); |