Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 18 | ONOS network topology viewer - PoC version 1.0 |
| 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | |
| 23 | (function (onos) { |
| 24 | 'use strict'; |
| 25 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 26 | // reference to the framework api |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 27 | var api = onos.api; |
| 28 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 29 | // configuration data |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 30 | var config = { |
Simon Hunt | b4d9d4c | 2014-10-30 11:27:23 -0700 | [diff] [blame] | 31 | useLiveData: true, |
Simon Hunt | 7317137 | 2014-10-30 09:25:36 -0700 | [diff] [blame] | 32 | debugOn: false, |
| 33 | debug: { |
| 34 | showNodeXY: false, |
| 35 | showKeyHandler: true |
| 36 | }, |
| 37 | options: { |
| 38 | layering: true, |
Simon Hunt | ee66e61 | 2014-10-30 14:56:31 -0700 | [diff] [blame] | 39 | collisionPrevention: true, |
| 40 | loadBackground: true |
Simon Hunt | 7317137 | 2014-10-30 09:25:36 -0700 | [diff] [blame] | 41 | }, |
Simon Hunt | ee66e61 | 2014-10-30 14:56:31 -0700 | [diff] [blame] | 42 | backgroundUrl: 'img/us-map.png', |
Simon Hunt | b4d9d4c | 2014-10-30 11:27:23 -0700 | [diff] [blame] | 43 | data: { |
| 44 | live: { |
| 45 | jsonUrl: 'rs/topology/graph', |
| 46 | detailPrefix: 'rs/topology/graph/', |
| 47 | detailSuffix: '' |
| 48 | }, |
| 49 | fake: { |
| 50 | jsonUrl: 'json/network2.json', |
| 51 | detailPrefix: 'json/', |
| 52 | detailSuffix: '.json' |
| 53 | } |
| 54 | }, |
Simon Hunt | 7317137 | 2014-10-30 09:25:36 -0700 | [diff] [blame] | 55 | iconUrl: { |
| 56 | device: 'img/device.png', |
| 57 | host: 'img/host.png', |
| 58 | pkt: 'img/pkt.png', |
| 59 | opt: 'img/opt.png' |
| 60 | }, |
| 61 | mastHeight: 36, |
| 62 | force: { |
| 63 | note: 'node.class or link.class is used to differentiate', |
| 64 | linkDistance: { |
| 65 | infra: 200, |
| 66 | host: 40 |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 67 | }, |
Simon Hunt | 7317137 | 2014-10-30 09:25:36 -0700 | [diff] [blame] | 68 | linkStrength: { |
| 69 | infra: 1.0, |
| 70 | host: 1.0 |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 71 | }, |
Simon Hunt | 7317137 | 2014-10-30 09:25:36 -0700 | [diff] [blame] | 72 | charge: { |
| 73 | device: -800, |
| 74 | host: -1000 |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 75 | }, |
Simon Hunt | 7317137 | 2014-10-30 09:25:36 -0700 | [diff] [blame] | 76 | ticksWithoutCollisions: 50, |
| 77 | marginLR: 20, |
| 78 | marginTB: 20, |
| 79 | translate: function() { |
| 80 | return 'translate(' + |
| 81 | config.force.marginLR + ',' + |
| 82 | config.force.marginTB + ')'; |
| 83 | } |
| 84 | }, |
| 85 | labels: { |
| 86 | imgPad: 16, |
| 87 | padLR: 8, |
| 88 | padTB: 6, |
| 89 | marginLR: 3, |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 90 | marginTB: 2, |
| 91 | port: { |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 92 | gap: 3, |
| 93 | width: 18, |
| 94 | height: 14 |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 95 | } |
Simon Hunt | 7317137 | 2014-10-30 09:25:36 -0700 | [diff] [blame] | 96 | }, |
| 97 | icons: { |
| 98 | w: 32, |
| 99 | h: 32, |
| 100 | xoff: -12, |
| 101 | yoff: -8 |
| 102 | }, |
| 103 | constraints: { |
| 104 | ypos: { |
| 105 | host: 0.05, |
| 106 | switch: 0.3, |
| 107 | roadm: 0.7 |
| 108 | } |
| 109 | }, |
| 110 | hostLinkWidth: 1.0, |
| 111 | hostRadius: 7, |
| 112 | mouseOutTimerDelayMs: 120 |
| 113 | }; |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 114 | |
| 115 | // state variables |
| 116 | var view = {}, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 117 | network = {}, |
| 118 | selected = {}, |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 119 | highlighted = null, |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 120 | hovered = null, |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 121 | viewMode = 'showAll', |
| 122 | portLabelsOn = false; |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 123 | |
| 124 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 125 | function debug(what) { |
| 126 | return config.debugOn && config.debug[what]; |
| 127 | } |
| 128 | |
Simon Hunt | b4d9d4c | 2014-10-30 11:27:23 -0700 | [diff] [blame] | 129 | function urlData() { |
| 130 | return config.data[config.useLiveData ? 'live' : 'fake']; |
| 131 | } |
| 132 | |
| 133 | function networkJsonUrl() { |
| 134 | return urlData().jsonUrl; |
| 135 | } |
| 136 | |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 137 | function safeId(id) { |
| 138 | return id.replace(/[^a-z0-9]/gi, '_'); |
| 139 | } |
| 140 | |
Simon Hunt | b4d9d4c | 2014-10-30 11:27:23 -0700 | [diff] [blame] | 141 | function detailJsonUrl(id) { |
| 142 | var u = urlData(), |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 143 | encId = config.useLiveData ? encodeURIComponent(id) : safeId(id); |
Simon Hunt | b4d9d4c | 2014-10-30 11:27:23 -0700 | [diff] [blame] | 144 | return u.detailPrefix + encId + u.detailSuffix; |
| 145 | } |
| 146 | |
| 147 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 148 | // load the topology view of the network |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 149 | function loadNetworkView() { |
| 150 | // Hey, here I am, calling something on the ONOS api: |
| 151 | api.printTime(); |
| 152 | |
| 153 | resize(); |
| 154 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 155 | // go get our network data from the server... |
Simon Hunt | b4d9d4c | 2014-10-30 11:27:23 -0700 | [diff] [blame] | 156 | var url = networkJsonUrl(); |
| 157 | d3.json(url , function (err, data) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 158 | if (err) { |
| 159 | alert('Oops! Error reading JSON...\n\n' + |
Simon Hunt | b4d9d4c | 2014-10-30 11:27:23 -0700 | [diff] [blame] | 160 | 'URL: ' + url + '\n\n' + |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 161 | 'Error: ' + err.message); |
| 162 | return; |
| 163 | } |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 164 | // console.log("here is the JSON data..."); |
| 165 | // console.log(data); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 166 | |
| 167 | network.data = data; |
| 168 | drawNetwork(); |
| 169 | }); |
| 170 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 171 | // while we wait for the data, set up the handlers... |
| 172 | setUpClickHandler(); |
| 173 | setUpRadioButtonHandler(); |
| 174 | setUpKeyHandler(); |
| 175 | $(window).on('resize', resize); |
| 176 | } |
| 177 | |
| 178 | function setUpClickHandler() { |
| 179 | // click handler for "selectable" objects |
| 180 | $(document).on('click', '.select-object', function () { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 181 | // when any object of class "select-object" is clicked... |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 182 | var obj = network.lookup[$(this).data('id')]; |
| 183 | if (obj) { |
| 184 | selectObject(obj); |
| 185 | } |
| 186 | // stop propagation of event (I think) ... |
| 187 | return false; |
| 188 | }); |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 189 | } |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 190 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 191 | function setUpRadioButtonHandler() { |
| 192 | d3.selectAll('#displayModes .radio').on('click', function () { |
| 193 | var id = d3.select(this).attr('id'); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 194 | if (id !== viewMode) { |
| 195 | radioButton('displayModes', id); |
| 196 | viewMode = id; |
Simon Hunt | f967d51 | 2014-10-28 20:34:29 -0700 | [diff] [blame] | 197 | doRadioAction(id); |
| 198 | } |
| 199 | }); |
| 200 | } |
| 201 | |
| 202 | function doRadioAction(id) { |
| 203 | showAllLayers(); |
| 204 | if (id === 'showPkt') { |
| 205 | showPacketLayer(); |
| 206 | } else if (id === 'showOpt') { |
| 207 | showOpticalLayer(); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | function showAllLayers() { |
| 212 | network.node.classed('inactive', false); |
| 213 | network.link.classed('inactive', false); |
Simon Hunt | ac52575 | 2014-10-31 08:38:21 -0700 | [diff] [blame] | 214 | d3.selectAll('svg .port').classed('inactive', false) |
| 215 | d3.selectAll('svg .portText').classed('inactive', false) |
Simon Hunt | f967d51 | 2014-10-28 20:34:29 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | function showPacketLayer() { |
| 219 | network.node.each(function(d) { |
| 220 | // deactivate nodes that are not hosts or switches |
| 221 | if (d.class === 'device' && d.type !== 'switch') { |
| 222 | d3.select(this).classed('inactive', true); |
| 223 | } |
| 224 | }); |
| 225 | |
| 226 | network.link.each(function(lnk) { |
| 227 | // deactivate infrastructure links that have opt's as endpoints |
| 228 | if (lnk.source.type === 'roadm' || lnk.target.type === 'roadm') { |
| 229 | d3.select(this).classed('inactive', true); |
| 230 | } |
| 231 | }); |
Simon Hunt | ac52575 | 2014-10-31 08:38:21 -0700 | [diff] [blame] | 232 | |
| 233 | // deactivate non-packet ports |
| 234 | d3.selectAll('svg .optPort').classed('inactive', true) |
Simon Hunt | f967d51 | 2014-10-28 20:34:29 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | function showOpticalLayer() { |
| 238 | network.node.each(function(d) { |
| 239 | // deactivate nodes that are not optical devices |
| 240 | if (d.type !== 'roadm') { |
| 241 | d3.select(this).classed('inactive', true); |
| 242 | } |
| 243 | }); |
| 244 | |
| 245 | network.link.each(function(lnk) { |
| 246 | // deactivate infrastructure links that have opt's as endpoints |
| 247 | if (lnk.source.type !== 'roadm' || lnk.target.type !== 'roadm') { |
| 248 | d3.select(this).classed('inactive', true); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 249 | } |
| 250 | }); |
Simon Hunt | ac52575 | 2014-10-31 08:38:21 -0700 | [diff] [blame] | 251 | |
| 252 | // deactivate non-packet ports |
| 253 | d3.selectAll('svg .pktPort').classed('inactive', true) |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 256 | function setUpKeyHandler() { |
| 257 | d3.select('body') |
| 258 | .on('keydown', function () { |
| 259 | processKeyEvent(); |
| 260 | if (debug('showKeyHandler')) { |
| 261 | network.svg.append('text') |
| 262 | .attr('x', 5) |
| 263 | .attr('y', 15) |
| 264 | .style('font-size', '20pt') |
| 265 | .text('keyCode: ' + d3.event.keyCode + |
| 266 | ' applied to : ' + contextLabel()) |
| 267 | .transition().duration(2000) |
| 268 | .style('font-size', '2pt') |
| 269 | .style('fill-opacity', 0.01) |
| 270 | .remove(); |
| 271 | } |
| 272 | }); |
| 273 | } |
| 274 | |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 275 | function contextLabel() { |
| 276 | return hovered === null ? "(nothing)" : hovered.id; |
| 277 | } |
| 278 | |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 279 | function radioButton(group, id) { |
| 280 | d3.selectAll("#" + group + " .radio").classed("active", false); |
| 281 | d3.select("#" + group + " #" + id).classed("active", true); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 284 | function processKeyEvent() { |
| 285 | var code = d3.event.keyCode; |
| 286 | switch (code) { |
Simon Hunt | ee66e61 | 2014-10-30 14:56:31 -0700 | [diff] [blame] | 287 | case 66: // B |
| 288 | toggleBackground(); |
| 289 | break; |
Thomas Vachuska | 1de6601 | 2014-10-30 03:03:30 -0700 | [diff] [blame] | 290 | case 71: // G |
| 291 | cycleLayout(); |
| 292 | break; |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 293 | case 76: // L |
| 294 | cycleLabels(); |
| 295 | break; |
| 296 | case 80: // P |
| 297 | togglePorts(); |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 298 | break; |
| 299 | case 85: // U |
| 300 | unpin(); |
| 301 | break; |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | } |
| 305 | |
Simon Hunt | ee66e61 | 2014-10-30 14:56:31 -0700 | [diff] [blame] | 306 | function toggleBackground() { |
| 307 | var bg = d3.select('#bg'), |
| 308 | vis = bg.style('visibility'), |
| 309 | newvis = (vis === 'hidden') ? 'visible' : 'hidden'; |
| 310 | bg.style('visibility', newvis); |
| 311 | } |
| 312 | |
Thomas Vachuska | 1de6601 | 2014-10-30 03:03:30 -0700 | [diff] [blame] | 313 | function cycleLayout() { |
| 314 | config.options.layering = !config.options.layering; |
| 315 | network.force.resume(); |
| 316 | } |
| 317 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 318 | function cycleLabels() { |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 319 | console.log('Cycle Labels - context = ' + contextLabel()); |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | function togglePorts() { |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 323 | portLabelsOn = !portLabelsOn; |
| 324 | var portVis = portLabelsOn ? 'visible' : 'hidden'; |
| 325 | d3.selectAll('.port').style('visibility', portVis); |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 326 | d3.selectAll('.portText').style('visibility', portVis); |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | function unpin() { |
| 330 | if (hovered) { |
| 331 | hovered.fixed = false; |
Simon Hunt | f967d51 | 2014-10-28 20:34:29 -0700 | [diff] [blame] | 332 | findNodeFromData(hovered).classed('fixed', false); |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 333 | network.force.resume(); |
| 334 | } |
| 335 | console.log('Unpin - context = ' + contextLabel()); |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 338 | |
| 339 | // ======================================================== |
| 340 | |
| 341 | function drawNetwork() { |
| 342 | $('#view').empty(); |
| 343 | |
| 344 | prepareNodesAndLinks(); |
| 345 | createLayout(); |
| 346 | console.log("\n\nHere is the augmented network object..."); |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 347 | console.log(network); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | function prepareNodesAndLinks() { |
| 351 | network.lookup = {}; |
| 352 | network.nodes = []; |
| 353 | network.links = []; |
| 354 | |
| 355 | var nw = network.forceWidth, |
| 356 | nh = network.forceHeight; |
| 357 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 358 | function yPosConstraintForNode(n) { |
| 359 | return config.constraints.ypos[n.type || 'host']; |
| 360 | } |
| 361 | |
| 362 | // Note that both 'devices' and 'hosts' get mapped into the nodes array |
| 363 | |
| 364 | // first, the devices... |
| 365 | network.data.devices.forEach(function(n) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 366 | var ypc = yPosConstraintForNode(n), |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 367 | ix = Math.random() * 0.6 * nw + 0.2 * nw, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 368 | iy = ypc * nh, |
| 369 | node = { |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 370 | id: n.id, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 371 | labels: n.labels, |
| 372 | class: 'device', |
| 373 | icon: 'device', |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 374 | type: n.type, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 375 | x: ix, |
| 376 | y: iy, |
| 377 | constraint: { |
| 378 | weight: 0.7, |
| 379 | y: iy |
| 380 | } |
| 381 | }; |
| 382 | network.lookup[n.id] = node; |
| 383 | network.nodes.push(node); |
| 384 | }); |
| 385 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 386 | // then, the hosts... |
| 387 | network.data.hosts.forEach(function(n) { |
| 388 | var ypc = yPosConstraintForNode(n), |
| 389 | ix = Math.random() * 0.6 * nw + 0.2 * nw, |
| 390 | iy = ypc * nh, |
| 391 | node = { |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 392 | id: n.id, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 393 | labels: n.labels, |
| 394 | class: 'host', |
| 395 | icon: 'host', |
| 396 | type: n.type, |
| 397 | x: ix, |
| 398 | y: iy, |
| 399 | constraint: { |
| 400 | weight: 0.7, |
| 401 | y: iy |
| 402 | } |
| 403 | }; |
| 404 | network.lookup[n.id] = node; |
| 405 | network.nodes.push(node); |
| 406 | }); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 407 | |
| 408 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 409 | // now, process the explicit links... |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 410 | network.data.links.forEach(function(lnk) { |
| 411 | var src = network.lookup[lnk.src], |
| 412 | dst = network.lookup[lnk.dst], |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 413 | id = src.id + "-" + dst.id; |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 414 | |
| 415 | var link = { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 416 | class: 'infra', |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 417 | id: id, |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 418 | type: lnk.type, |
| 419 | width: lnk.linkWidth, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 420 | source: src, |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 421 | srcPort: lnk.srcPort, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 422 | target: dst, |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 423 | tgtPort: lnk.dstPort, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 424 | strength: config.force.linkStrength.infra |
| 425 | }; |
| 426 | network.links.push(link); |
| 427 | }); |
| 428 | |
| 429 | // finally, infer host links... |
| 430 | network.data.hosts.forEach(function(n) { |
| 431 | var src = network.lookup[n.id], |
| 432 | dst = network.lookup[n.cp.device], |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 433 | id = src.id + "-" + dst.id; |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 434 | |
| 435 | var link = { |
| 436 | class: 'host', |
| 437 | id: id, |
| 438 | type: 'hostLink', |
| 439 | width: config.hostLinkWidth, |
| 440 | source: src, |
| 441 | target: dst, |
| 442 | strength: config.force.linkStrength.host |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 443 | }; |
| 444 | network.links.push(link); |
| 445 | }); |
| 446 | } |
| 447 | |
| 448 | function createLayout() { |
| 449 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 450 | var cfg = config.force; |
| 451 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 452 | network.force = d3.layout.force() |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 453 | .size([network.forceWidth, network.forceHeight]) |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 454 | .nodes(network.nodes) |
| 455 | .links(network.links) |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 456 | .linkStrength(function(d) { return cfg.linkStrength[d.class]; }) |
| 457 | .linkDistance(function(d) { return cfg.linkDistance[d.class]; }) |
| 458 | .charge(function(d) { return cfg.charge[d.class]; }) |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 459 | .on('tick', tick); |
| 460 | |
| 461 | network.svg = d3.select('#view').append('svg') |
| 462 | .attr('width', view.width) |
| 463 | .attr('height', view.height) |
| 464 | .append('g') |
Simon Hunt | ae968a6 | 2014-10-22 14:54:41 -0700 | [diff] [blame] | 465 | .attr('transform', config.force.translate()); |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 466 | // .attr('id', 'zoomable') |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 467 | // .call(d3.behavior.zoom().on("zoom", zoomRedraw)); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 468 | |
Thomas Vachuska | 8cd66a5 | 2014-10-30 11:53:07 -0700 | [diff] [blame] | 469 | network.svg.append('svg:image') |
| 470 | .attr({ |
| 471 | id: 'bg', |
| 472 | width: view.width, |
| 473 | height: view.height, |
Simon Hunt | ee66e61 | 2014-10-30 14:56:31 -0700 | [diff] [blame] | 474 | 'xlink:href': config.backgroundUrl |
| 475 | }) |
| 476 | .style('visibility', |
| 477 | config.options.loadBackground ? 'visible' : 'hidden'); |
Thomas Vachuska | 8cd66a5 | 2014-10-30 11:53:07 -0700 | [diff] [blame] | 478 | |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 479 | // function zoomRedraw() { |
| 480 | // d3.select("#zoomable").attr("transform", |
| 481 | // "translate(" + d3.event.translate + ")" |
| 482 | // + " scale(" + d3.event.scale + ")"); |
| 483 | // } |
| 484 | |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 485 | // TODO: move glow/blur stuff to util script |
| 486 | var glow = network.svg.append('filter') |
| 487 | .attr('x', '-50%') |
| 488 | .attr('y', '-50%') |
| 489 | .attr('width', '200%') |
| 490 | .attr('height', '200%') |
| 491 | .attr('id', 'blue-glow'); |
| 492 | |
| 493 | glow.append('feColorMatrix') |
| 494 | .attr('type', 'matrix') |
| 495 | .attr('values', '0 0 0 0 0 ' + |
| 496 | '0 0 0 0 0 ' + |
| 497 | '0 0 0 0 .7 ' + |
| 498 | '0 0 0 1 0 '); |
| 499 | |
| 500 | glow.append('feGaussianBlur') |
| 501 | .attr('stdDeviation', 3) |
| 502 | .attr('result', 'coloredBlur'); |
| 503 | |
| 504 | glow.append('feMerge').selectAll('feMergeNode') |
| 505 | .data(['coloredBlur', 'SourceGraphic']) |
| 506 | .enter().append('feMergeNode') |
| 507 | .attr('in', String); |
| 508 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 509 | // TODO: legend (and auto adjust on scroll) |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 510 | // $('#view').on('scroll', function() { |
| 511 | // |
| 512 | // }); |
| 513 | |
| 514 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 515 | // TODO: move drag behavior into separate method. |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 516 | // == define node drag behavior... |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 517 | network.draggedThreshold = d3.scale.linear() |
| 518 | .domain([0, 0.1]) |
| 519 | .range([5, 20]) |
| 520 | .clamp(true); |
| 521 | |
| 522 | function dragged(d) { |
| 523 | var threshold = network.draggedThreshold(network.force.alpha()), |
| 524 | dx = d.oldX - d.px, |
| 525 | dy = d.oldY - d.py; |
| 526 | if (Math.abs(dx) >= threshold || Math.abs(dy) >= threshold) { |
| 527 | d.dragged = true; |
| 528 | } |
| 529 | return d.dragged; |
| 530 | } |
| 531 | |
| 532 | network.drag = d3.behavior.drag() |
| 533 | .origin(function(d) { return d; }) |
| 534 | .on('dragstart', function(d) { |
| 535 | d.oldX = d.x; |
| 536 | d.oldY = d.y; |
| 537 | d.dragged = false; |
| 538 | d.fixed |= 2; |
| 539 | }) |
| 540 | .on('drag', function(d) { |
| 541 | d.px = d3.event.x; |
| 542 | d.py = d3.event.y; |
| 543 | if (dragged(d)) { |
| 544 | if (!network.force.alpha()) { |
| 545 | network.force.alpha(.025); |
| 546 | } |
| 547 | } |
| 548 | }) |
| 549 | .on('dragend', function(d) { |
| 550 | if (!dragged(d)) { |
| 551 | selectObject(d, this); |
| 552 | } |
| 553 | d.fixed &= ~6; |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 554 | |
| 555 | // once we've finished moving, pin the node in position, |
Simon Hunt | f967d51 | 2014-10-28 20:34:29 -0700 | [diff] [blame] | 556 | // if it is a device (not a host) |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 557 | if (d.class === 'device') { |
| 558 | d.fixed = true; |
Simon Hunt | f967d51 | 2014-10-28 20:34:29 -0700 | [diff] [blame] | 559 | d3.select(this).classed('fixed', true) |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 560 | } |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 561 | }); |
| 562 | |
| 563 | $('#view').on('click', function(e) { |
| 564 | if (!$(e.target).closest('.node').length) { |
| 565 | deselectObject(); |
| 566 | } |
| 567 | }); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 568 | |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 569 | // ............................................................... |
| 570 | |
| 571 | // add links to the display |
| 572 | network.link = network.svg.append('g').attr('id', 'links') |
| 573 | .selectAll('.link') |
| 574 | .data(network.force.links(), function(d) {return d.id}) |
| 575 | .enter().append('line') |
| 576 | .attr('class', function(d) {return 'link ' + d.class}); |
| 577 | |
| 578 | network.linkSrcPort = network.svg.append('g') |
| 579 | .attr({ |
| 580 | id: 'srcPorts', |
| 581 | class: 'portLayer' |
| 582 | }); |
| 583 | network.linkTgtPort = network.svg.append('g') |
| 584 | .attr({ |
| 585 | id: 'tgtPorts', |
| 586 | class: 'portLayer' |
| 587 | }); |
| 588 | |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 589 | var portVis = portLabelsOn ? 'visible' : 'hidden', |
| 590 | pw = config.labels.port.width, |
| 591 | ph = config.labels.port.height; |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 592 | |
Simon Hunt | ac52575 | 2014-10-31 08:38:21 -0700 | [diff] [blame] | 593 | network.link.filter('.infra').each(function(d) { |
| 594 | var srcType = d.source.type === 'roadm' ? 'optPort' : 'pktPort', |
| 595 | tgtType = d.target.type === 'roadm' ? 'optPort' : 'pktPort'; |
| 596 | |
| 597 | if (d.source.type) |
| 598 | |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 599 | network.linkSrcPort.append('rect').attr({ |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 600 | id: 'srcPort-' + safeId(d.id), |
Simon Hunt | ac52575 | 2014-10-31 08:38:21 -0700 | [diff] [blame] | 601 | class: 'port ' + srcType, |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 602 | width: pw, |
| 603 | height: ph, |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 604 | rx: 4, |
| 605 | ry: 4 |
| 606 | }).style('visibility', portVis); |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 607 | |
| 608 | network.linkTgtPort.append('rect').attr({ |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 609 | id: 'tgtPort-' + safeId(d.id), |
Simon Hunt | ac52575 | 2014-10-31 08:38:21 -0700 | [diff] [blame] | 610 | class: 'port ' + tgtType, |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 611 | width: pw, |
| 612 | height: ph, |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 613 | rx: 4, |
| 614 | ry: 4 |
| 615 | }).style('visibility', portVis); |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 616 | |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 617 | network.linkSrcPort.append('text').attr({ |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 618 | id: 'srcText-' + safeId(d.id), |
Simon Hunt | ac52575 | 2014-10-31 08:38:21 -0700 | [diff] [blame] | 619 | class: 'portText ' + srcType |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 620 | }).text(d.srcPort) |
| 621 | .style('visibility', portVis); |
| 622 | |
| 623 | network.linkTgtPort.append('text').attr({ |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 624 | id: 'tgtText-' + safeId(d.id), |
Simon Hunt | ac52575 | 2014-10-31 08:38:21 -0700 | [diff] [blame] | 625 | class: 'portText ' + tgtType |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 626 | }).text(d.tgtPort) |
| 627 | .style('visibility', portVis); |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 628 | }); |
| 629 | |
| 630 | // ............................................................... |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 631 | |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 632 | // add nodes to the display |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 633 | network.node = network.svg.selectAll('.node') |
| 634 | .data(network.force.nodes(), function(d) {return d.id}) |
| 635 | .enter().append('g') |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 636 | .attr('class', function(d) { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 637 | var cls = 'node ' + d.class; |
| 638 | if (d.type) { |
| 639 | cls += ' ' + d.type; |
| 640 | } |
| 641 | return cls; |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 642 | }) |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 643 | .attr('transform', function(d) { |
| 644 | return translate(d.x, d.y); |
| 645 | }) |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 646 | .call(network.drag) |
| 647 | .on('mouseover', function(d) { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 648 | // TODO: show tooltip |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 649 | if (network.mouseoutTimeout) { |
| 650 | clearTimeout(network.mouseoutTimeout); |
| 651 | network.mouseoutTimeout = null; |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 652 | } |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 653 | hoverObject(d); |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 654 | }) |
| 655 | .on('mouseout', function(d) { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 656 | // TODO: hide tooltip |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 657 | if (network.mouseoutTimeout) { |
| 658 | clearTimeout(network.mouseoutTimeout); |
| 659 | network.mouseoutTimeout = null; |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 660 | } |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 661 | network.mouseoutTimeout = setTimeout(function() { |
| 662 | hoverObject(null); |
| 663 | }, config.mouseOutTimerDelayMs); |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 664 | }); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 665 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 666 | |
| 667 | // deal with device nodes first |
| 668 | network.nodeRect = network.node.filter('.device') |
| 669 | .append('rect') |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 670 | .attr({ |
| 671 | rx: 5, |
| 672 | ry: 5, |
| 673 | width: 100, |
| 674 | height: 12 |
| 675 | }); |
| 676 | // note that width/height are adjusted to fit the label text |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 677 | // then padded, and space made for the icon. |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 678 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 679 | network.node.filter('.device').each(function(d) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 680 | var node = d3.select(this), |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 681 | icon = iconUrl(d); |
| 682 | |
| 683 | node.append('text') |
| 684 | // TODO: add label cycle behavior |
| 685 | .text(d.id) |
| 686 | .attr('dy', '1.1em'); |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 687 | |
| 688 | if (icon) { |
| 689 | var cfg = config.icons; |
| 690 | node.append('svg:image') |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 691 | .attr({ |
| 692 | width: cfg.w, |
| 693 | height: cfg.h, |
| 694 | 'xlink:href': icon |
| 695 | }); |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 696 | // note, icon relative positioning (x,y) is done after we have |
| 697 | // adjusted the bounds of the rectangle... |
| 698 | } |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 699 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 700 | // debug function to show the modelled x,y coordinates of nodes... |
| 701 | if (debug('showNodeXY')) { |
| 702 | node.select('rect').attr('fill-opacity', 0.5); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 703 | node.append('circle') |
| 704 | .attr({ |
| 705 | class: 'debug', |
| 706 | cx: 0, |
| 707 | cy: 0, |
| 708 | r: '3px' |
| 709 | }); |
| 710 | } |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 711 | }); |
| 712 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 713 | // now process host nodes |
| 714 | network.nodeCircle = network.node.filter('.host') |
| 715 | .append('circle') |
| 716 | .attr({ |
| 717 | r: config.hostRadius |
| 718 | }); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 719 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 720 | network.node.filter('.host').each(function(d) { |
| 721 | var node = d3.select(this), |
| 722 | icon = iconUrl(d); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 723 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 724 | // debug function to show the modelled x,y coordinates of nodes... |
| 725 | if (debug('showNodeXY')) { |
| 726 | node.select('circle').attr('fill-opacity', 0.5); |
| 727 | node.append('circle') |
| 728 | .attr({ |
| 729 | class: 'debug', |
| 730 | cx: 0, |
| 731 | cy: 0, |
| 732 | r: '3px' |
| 733 | }); |
| 734 | } |
| 735 | }); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 736 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 737 | // this function is scheduled to happen soon after the given thread ends |
| 738 | setTimeout(function() { |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 739 | var lab = config.labels, |
| 740 | portGap = lab.port.gap, |
| 741 | midW = portGap + lab.port.width/ 2, |
| 742 | midH = portGap + lab.port.height / 2; |
| 743 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 744 | // post process the device nodes, to pad their size to fit the |
| 745 | // label text and attach the icon to the right location. |
| 746 | network.node.filter('.device').each(function(d) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 747 | // for every node, recompute size, padding, etc. so text fits |
| 748 | var node = d3.select(this), |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 749 | text = node.select('text'), |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 750 | box = adjustRectToFitText(node); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 751 | |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 752 | // now make the computed adjustment |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 753 | node.select('rect') |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 754 | .attr(box); |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 755 | |
| 756 | node.select('image') |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 757 | .attr('x', box.x + config.icons.xoff) |
| 758 | .attr('y', box.y + config.icons.yoff); |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 759 | |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 760 | var bounds = boundsFromBox(box), |
| 761 | portBounds = { |
| 762 | x1: bounds.x1 - midW, |
| 763 | x2: bounds.x2 + midW, |
| 764 | y1: bounds.y1 - midH, |
| 765 | y2: bounds.y2 + midH |
| 766 | }; |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 767 | |
| 768 | // todo: clean up extent and edge work.. |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 769 | d.extent = { |
| 770 | left: bounds.x1 - lab.marginLR, |
| 771 | right: bounds.x2 + lab.marginLR, |
| 772 | top: bounds.y1 - lab.marginTB, |
| 773 | bottom: bounds.y2 + lab.marginTB |
| 774 | }; |
| 775 | |
| 776 | d.edge = { |
| 777 | left : new geo.LineSegment(bounds.x1, bounds.y1, bounds.x1, bounds.y2), |
| 778 | right : new geo.LineSegment(bounds.x2, bounds.y1, bounds.x2, bounds.y2), |
| 779 | top : new geo.LineSegment(bounds.x1, bounds.y1, bounds.x2, bounds.y1), |
| 780 | bottom : new geo.LineSegment(bounds.x1, bounds.y2, bounds.x2, bounds.y2) |
| 781 | }; |
| 782 | |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 783 | d.portEdge = { |
| 784 | left : new geo.LineSegment( |
| 785 | portBounds.x1, portBounds.y1, portBounds.x1, portBounds.y2 |
| 786 | ), |
| 787 | right : new geo.LineSegment( |
| 788 | portBounds.x2, portBounds.y1, portBounds.x2, portBounds.y2 |
| 789 | ), |
| 790 | top : new geo.LineSegment( |
| 791 | portBounds.x1, portBounds.y1, portBounds.x2, portBounds.y1 |
| 792 | ), |
| 793 | bottom : new geo.LineSegment( |
| 794 | portBounds.x1, portBounds.y2, portBounds.x2, portBounds.y2 |
| 795 | ) |
| 796 | }; |
| 797 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 798 | }); |
| 799 | |
| 800 | network.numTicks = 0; |
| 801 | network.preventCollisions = false; |
| 802 | network.force.start(); |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 803 | for (var i = 0; i < config.force.ticksWithoutCollisions; i++) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 804 | network.force.tick(); |
| 805 | } |
| 806 | network.preventCollisions = true; |
| 807 | $('#view').css('visibility', 'visible'); |
| 808 | }); |
| 809 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 810 | |
| 811 | // returns the newly computed bounding box of the rectangle |
| 812 | function adjustRectToFitText(n) { |
| 813 | var text = n.select('text'), |
| 814 | box = text.node().getBBox(), |
| 815 | lab = config.labels; |
| 816 | |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 817 | // not sure why n.data() returns an array of 1 element... |
| 818 | var data = n.data()[0]; |
| 819 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 820 | text.attr('text-anchor', 'middle') |
| 821 | .attr('y', '-0.8em') |
| 822 | .attr('x', lab.imgPad/2) |
| 823 | ; |
| 824 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 825 | // translate the bbox so that it is centered on [x,y] |
| 826 | box.x = -box.width / 2; |
| 827 | box.y = -box.height / 2; |
| 828 | |
| 829 | // add padding |
| 830 | box.x -= (lab.padLR + lab.imgPad/2); |
| 831 | box.width += lab.padLR * 2 + lab.imgPad; |
| 832 | box.y -= lab.padTB; |
| 833 | box.height += lab.padTB * 2; |
| 834 | |
| 835 | return box; |
| 836 | } |
| 837 | |
| 838 | function boundsFromBox(box) { |
| 839 | return { |
| 840 | x1: box.x, |
| 841 | y1: box.y, |
| 842 | x2: box.x + box.width, |
| 843 | y2: box.y + box.height |
| 844 | }; |
| 845 | } |
| 846 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 849 | function iconUrl(d) { |
Thomas Vachuska | 1de6601 | 2014-10-30 03:03:30 -0700 | [diff] [blame] | 850 | return 'img/' + d.type + '.png'; |
| 851 | // return config.iconUrl[d.icon]; |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 854 | function translate(x, y) { |
| 855 | return 'translate(' + x + ',' + y + ')'; |
| 856 | } |
| 857 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 858 | // prevents collisions amongst device nodes |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 859 | function preventCollisions() { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 860 | var quadtree = d3.geom.quadtree(network.nodes), |
| 861 | hrad = config.hostRadius; |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 862 | |
| 863 | network.nodes.forEach(function(n) { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 864 | var nx1, nx2, ny1, ny2; |
| 865 | |
| 866 | if (n.class === 'device') { |
| 867 | nx1 = n.x + n.extent.left; |
| 868 | nx2 = n.x + n.extent.right; |
| 869 | ny1 = n.y + n.extent.top; |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 870 | ny2 = n.y + n.extent.bottom; |
| 871 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 872 | } else { |
| 873 | nx1 = n.x - hrad; |
| 874 | nx2 = n.x + hrad; |
| 875 | ny1 = n.y - hrad; |
| 876 | ny2 = n.y + hrad; |
| 877 | } |
| 878 | |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 879 | quadtree.visit(function(quad, x1, y1, x2, y2) { |
| 880 | if (quad.point && quad.point !== n) { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 881 | // check if the rectangles/circles intersect |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 882 | var p = quad.point, |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 883 | px1, px2, py1, py2, ix; |
| 884 | |
| 885 | if (p.class === 'device') { |
| 886 | px1 = p.x + p.extent.left; |
| 887 | px2 = p.x + p.extent.right; |
| 888 | py1 = p.y + p.extent.top; |
| 889 | py2 = p.y + p.extent.bottom; |
| 890 | |
| 891 | } else { |
| 892 | px1 = p.x - hrad; |
| 893 | px2 = p.x + hrad; |
| 894 | py1 = p.y - hrad; |
| 895 | py2 = p.y + hrad; |
| 896 | } |
| 897 | |
| 898 | ix = (px1 <= nx2 && nx1 <= px2 && py1 <= ny2 && ny1 <= py2); |
| 899 | |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 900 | if (ix) { |
| 901 | var xa1 = nx2 - px1, // shift n left , p right |
| 902 | xa2 = px2 - nx1, // shift n right, p left |
| 903 | ya1 = ny2 - py1, // shift n up , p down |
| 904 | ya2 = py2 - ny1, // shift n down , p up |
| 905 | adj = Math.min(xa1, xa2, ya1, ya2); |
| 906 | |
| 907 | if (adj == xa1) { |
| 908 | n.x -= adj / 2; |
| 909 | p.x += adj / 2; |
| 910 | } else if (adj == xa2) { |
| 911 | n.x += adj / 2; |
| 912 | p.x -= adj / 2; |
| 913 | } else if (adj == ya1) { |
| 914 | n.y -= adj / 2; |
| 915 | p.y += adj / 2; |
| 916 | } else if (adj == ya2) { |
| 917 | n.y += adj / 2; |
| 918 | p.y -= adj / 2; |
| 919 | } |
| 920 | } |
| 921 | return ix; |
| 922 | } |
| 923 | }); |
| 924 | |
| 925 | }); |
| 926 | } |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 927 | |
| 928 | function tick(e) { |
| 929 | network.numTicks++; |
| 930 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 931 | if (config.options.layering) { |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 932 | // adjust the y-coord of each node, based on y-pos constraints |
| 933 | network.nodes.forEach(function (n) { |
| 934 | var z = e.alpha * n.constraint.weight; |
| 935 | if (!isNaN(n.constraint.y)) { |
| 936 | n.y = (n.constraint.y * z + n.y * (1 - z)); |
| 937 | } |
| 938 | }); |
| 939 | } |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 940 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 941 | if (config.options.collisionPrevention && network.preventCollisions) { |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 942 | preventCollisions(); |
| 943 | } |
| 944 | |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 945 | var portHalfW = config.labels.port.width / 2, |
| 946 | portHalfH = config.labels.port.height / 2; |
| 947 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 948 | // clip visualization of links at bounds of nodes... |
| 949 | network.link.each(function(d) { |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 950 | var xs = d.source.x, |
| 951 | ys = d.source.y, |
| 952 | xt = d.target.x, |
| 953 | yt = d.target.y, |
| 954 | line = new geo.LineSegment(xs, ys, xt, yt), |
| 955 | e, ix, |
| 956 | exs, eys, ext, eyt, |
| 957 | pxs, pys, pxt, pyt; |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 958 | |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 959 | if (d.class === 'host') { |
| 960 | // no adjustment for source end of link, since hosts are dots |
| 961 | exs = xs; |
| 962 | eys = ys; |
| 963 | |
| 964 | } else { |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 965 | for (e in d.source.edge) { |
| 966 | ix = line.intersect(d.source.edge[e].offset(xs, ys)); |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 967 | if (ix.in1 && ix.in2) { |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 968 | exs = ix.x; |
| 969 | eys = ix.y; |
| 970 | |
| 971 | // also pick off the port label intersection |
| 972 | ix = line.intersect(d.source.portEdge[e].offset(xs, ys)); |
| 973 | pxs = ix.x; |
| 974 | pys = ix.y; |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 975 | break; |
| 976 | } |
| 977 | } |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 978 | } |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 979 | |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 980 | for (e in d.target.edge) { |
| 981 | ix = line.intersect(d.target.edge[e].offset(xt, yt)); |
| 982 | if (ix.in1 && ix.in2) { |
| 983 | ext = ix.x; |
| 984 | eyt = ix.y; |
| 985 | |
| 986 | // also pick off the port label intersection |
| 987 | ix = line.intersect(d.target.portEdge[e].offset(xt, yt)); |
| 988 | pxt = ix.x; |
| 989 | pyt = ix.y; |
| 990 | break; |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 991 | } |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 992 | } |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 993 | |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 994 | // adjust the endpoints of the link's line to match rectangles |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 995 | var sid = safeId(d.id); |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 996 | d3.select(this) |
| 997 | .attr('x1', exs) |
| 998 | .attr('y1', eys) |
| 999 | .attr('x2', ext) |
| 1000 | .attr('y2', eyt); |
| 1001 | |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 1002 | d3.select('#srcPort-' + sid) |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 1003 | .attr('x', pxs - portHalfW) |
| 1004 | .attr('y', pys - portHalfH); |
| 1005 | |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 1006 | d3.select('#tgtPort-' + sid) |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 1007 | .attr('x', pxt - portHalfW) |
| 1008 | .attr('y', pyt - portHalfH); |
| 1009 | |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 1010 | // TODO: fit label rect to size of port number. |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 1011 | d3.select('#srcText-' + sid) |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 1012 | .attr('x', pxs - 5) |
| 1013 | .attr('y', pys + 3); |
| 1014 | |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 1015 | d3.select('#tgtText-' + sid) |
Simon Hunt | 3c29c14 | 2014-10-30 21:31:44 -0700 | [diff] [blame] | 1016 | .attr('x', pxt - 5) |
| 1017 | .attr('y', pyt + 3); |
| 1018 | |
Simon Hunt | 69a8d21 | 2014-10-30 17:57:35 -0700 | [diff] [blame] | 1019 | }); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1020 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 1021 | // position each node by translating the node (group) by x,y |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1022 | network.node |
| 1023 | .attr('transform', function(d) { |
| 1024 | return translate(d.x, d.y); |
| 1025 | }); |
| 1026 | |
| 1027 | } |
| 1028 | |
| 1029 | // $('#docs-close').on('click', function() { |
| 1030 | // deselectObject(); |
| 1031 | // return false; |
| 1032 | // }); |
| 1033 | |
| 1034 | // $(document).on('click', '.select-object', function() { |
| 1035 | // var obj = graph.data[$(this).data('name')]; |
| 1036 | // if (obj) { |
| 1037 | // selectObject(obj); |
| 1038 | // } |
| 1039 | // return false; |
| 1040 | // }); |
| 1041 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 1042 | function findNodeFromData(d) { |
| 1043 | var el = null; |
| 1044 | network.node.filter('.' + d.class).each(function(n) { |
| 1045 | if (n.id === d.id) { |
| 1046 | el = d3.select(this); |
| 1047 | } |
| 1048 | }); |
| 1049 | return el; |
| 1050 | } |
| 1051 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1052 | function selectObject(obj, el) { |
| 1053 | var node; |
| 1054 | if (el) { |
| 1055 | node = d3.select(el); |
| 1056 | } else { |
| 1057 | network.node.each(function(d) { |
| 1058 | if (d == obj) { |
| 1059 | node = d3.select(el = this); |
| 1060 | } |
| 1061 | }); |
| 1062 | } |
| 1063 | if (!node) return; |
| 1064 | |
| 1065 | if (node.classed('selected')) { |
| 1066 | deselectObject(); |
Simon Hunt | c586e21 | 2014-10-28 21:24:08 -0700 | [diff] [blame] | 1067 | flyinPane(null); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1068 | return; |
| 1069 | } |
| 1070 | deselectObject(false); |
| 1071 | |
| 1072 | selected = { |
| 1073 | obj : obj, |
| 1074 | el : el |
| 1075 | }; |
| 1076 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1077 | node.classed('selected', true); |
Simon Hunt | c586e21 | 2014-10-28 21:24:08 -0700 | [diff] [blame] | 1078 | flyinPane(obj); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | function deselectObject(doResize) { |
| 1082 | // Review: logic of 'resize(...)' function. |
| 1083 | if (doResize || typeof doResize == 'undefined') { |
| 1084 | resize(false); |
| 1085 | } |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 1086 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1087 | // deselect all nodes in the network... |
| 1088 | network.node.classed('selected', false); |
| 1089 | selected = {}; |
Simon Hunt | c586e21 | 2014-10-28 21:24:08 -0700 | [diff] [blame] | 1090 | flyinPane(null); |
| 1091 | } |
| 1092 | |
| 1093 | function flyinPane(obj) { |
| 1094 | var pane = d3.select('#flyout'), |
Simon Hunt | cc26756 | 2014-10-29 10:22:17 -0700 | [diff] [blame] | 1095 | url; |
Simon Hunt | c586e21 | 2014-10-28 21:24:08 -0700 | [diff] [blame] | 1096 | |
| 1097 | if (obj) { |
Simon Hunt | cc26756 | 2014-10-29 10:22:17 -0700 | [diff] [blame] | 1098 | // go get details of the selected object from the server... |
Simon Hunt | b4d9d4c | 2014-10-30 11:27:23 -0700 | [diff] [blame] | 1099 | url = detailJsonUrl(obj.id); |
Simon Hunt | cc26756 | 2014-10-29 10:22:17 -0700 | [diff] [blame] | 1100 | d3.json(url, function (err, data) { |
| 1101 | if (err) { |
| 1102 | alert('Oops! Error reading JSON...\n\n' + |
| 1103 | 'URL: ' + url + '\n\n' + |
| 1104 | 'Error: ' + err.message); |
| 1105 | return; |
| 1106 | } |
| 1107 | // console.log("JSON data... " + url); |
| 1108 | // console.log(data); |
| 1109 | |
| 1110 | displayDetails(data, pane); |
| 1111 | }); |
| 1112 | |
| 1113 | } else { |
| 1114 | // hide pane |
| 1115 | pane.transition().duration(750) |
| 1116 | .style('right', '-320px') |
| 1117 | .style('opacity', 0.0); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | function displayDetails(data, pane) { |
| 1122 | $('#flyout').empty(); |
| 1123 | |
Thomas Vachuska | 1de6601 | 2014-10-30 03:03:30 -0700 | [diff] [blame] | 1124 | var title = pane.append("h2"), |
| 1125 | table = pane.append("table"), |
Simon Hunt | cc26756 | 2014-10-29 10:22:17 -0700 | [diff] [blame] | 1126 | tbody = table.append("tbody"); |
| 1127 | |
Thomas Vachuska | 1de6601 | 2014-10-30 03:03:30 -0700 | [diff] [blame] | 1128 | $('<img src="img/' + data.type + '.png">').appendTo(title); |
| 1129 | $('<span>').attr('class', 'icon').text(data.id).appendTo(title); |
| 1130 | |
| 1131 | |
Simon Hunt | cc26756 | 2014-10-29 10:22:17 -0700 | [diff] [blame] | 1132 | // TODO: consider using d3 data bind to TR/TD |
| 1133 | |
| 1134 | data.propOrder.forEach(function(p) { |
Thomas Vachuska | 1de6601 | 2014-10-30 03:03:30 -0700 | [diff] [blame] | 1135 | if (p === '-') { |
| 1136 | addSep(tbody); |
| 1137 | } else { |
| 1138 | addProp(tbody, p, data.props[p]); |
| 1139 | } |
Simon Hunt | cc26756 | 2014-10-29 10:22:17 -0700 | [diff] [blame] | 1140 | }); |
| 1141 | |
Thomas Vachuska | 1de6601 | 2014-10-30 03:03:30 -0700 | [diff] [blame] | 1142 | function addSep(tbody) { |
| 1143 | var tr = tbody.append('tr'); |
| 1144 | $('<hr>').appendTo(tr.append('td').attr('colspan', 2)); |
| 1145 | } |
| 1146 | |
Simon Hunt | cc26756 | 2014-10-29 10:22:17 -0700 | [diff] [blame] | 1147 | function addProp(tbody, label, value) { |
| 1148 | var tr = tbody.append('tr'); |
| 1149 | |
| 1150 | tr.append('td') |
| 1151 | .attr('class', 'label') |
| 1152 | .text(label + ' :'); |
| 1153 | |
| 1154 | tr.append('td') |
| 1155 | .attr('class', 'value') |
| 1156 | .text(value); |
Simon Hunt | c586e21 | 2014-10-28 21:24:08 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Simon Hunt | cc26756 | 2014-10-29 10:22:17 -0700 | [diff] [blame] | 1159 | // show pane |
Simon Hunt | c586e21 | 2014-10-28 21:24:08 -0700 | [diff] [blame] | 1160 | pane.transition().duration(750) |
Simon Hunt | cc26756 | 2014-10-29 10:22:17 -0700 | [diff] [blame] | 1161 | .style('right', '20px') |
| 1162 | .style('opacity', 1.0); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | function highlightObject(obj) { |
| 1166 | if (obj) { |
| 1167 | if (obj != highlighted) { |
| 1168 | // TODO set or clear "inactive" class on nodes, based on criteria |
| 1169 | network.node.classed('inactive', function(d) { |
| 1170 | // return (obj !== d && |
| 1171 | // d.relation(obj.id)); |
| 1172 | return (obj !== d); |
| 1173 | }); |
| 1174 | // TODO: same with links |
| 1175 | network.link.classed('inactive', function(d) { |
| 1176 | return (obj !== d.source && obj !== d.target); |
| 1177 | }); |
| 1178 | } |
| 1179 | highlighted = obj; |
| 1180 | } else { |
| 1181 | if (highlighted) { |
| 1182 | // clear the inactive flag (no longer suppressed visually) |
| 1183 | network.node.classed('inactive', false); |
| 1184 | network.link.classed('inactive', false); |
| 1185 | } |
| 1186 | highlighted = null; |
| 1187 | |
| 1188 | } |
| 1189 | } |
| 1190 | |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 1191 | function hoverObject(obj) { |
| 1192 | if (obj) { |
| 1193 | hovered = obj; |
| 1194 | } else { |
| 1195 | if (hovered) { |
| 1196 | hovered = null; |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | |
Simon Hunt | c586e21 | 2014-10-28 21:24:08 -0700 | [diff] [blame] | 1202 | function resize() { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1203 | view.height = window.innerHeight - config.mastHeight; |
| 1204 | view.width = window.innerWidth; |
| 1205 | $('#view') |
| 1206 | .css('height', view.height + 'px') |
| 1207 | .css('width', view.width + 'px'); |
| 1208 | |
| 1209 | network.forceWidth = view.width - config.force.marginLR; |
| 1210 | network.forceHeight = view.height - config.force.marginTB; |
| 1211 | } |
| 1212 | |
| 1213 | // ====================================================================== |
| 1214 | // register with the UI framework |
| 1215 | |
| 1216 | api.addView('network', { |
| 1217 | load: loadNetworkView |
| 1218 | }); |
| 1219 | |
| 1220 | |
| 1221 | }(ONOS)); |
| 1222 | |