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