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 | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 31 | debugOn: false, |
| 32 | debug: { |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 33 | showNodeXY: false, |
| 34 | showKeyHandler: true |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 35 | }, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 36 | options: { |
Simon Hunt | 19cb098 | 2014-10-23 16:44:49 -0700 | [diff] [blame] | 37 | layering: true, |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 38 | collisionPrevention: true |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 39 | }, |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 40 | XjsonUrl: 'rs/topology/graph', |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 41 | jsonUrl: 'network.json', |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 42 | iconUrl: { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 43 | device: 'img/device.png', |
| 44 | host: 'img/host.png', |
| 45 | pkt: 'img/pkt.png', |
| 46 | opt: 'img/opt.png' |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 47 | }, |
Simon Hunt | 19cb098 | 2014-10-23 16:44:49 -0700 | [diff] [blame] | 48 | mastHeight: 36, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 49 | force: { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 50 | note: 'node.class or link.class is used to differentiate', |
| 51 | linkDistance: { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 52 | infra: 200, |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 53 | host: 40 |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 54 | }, |
| 55 | linkStrength: { |
| 56 | infra: 1.0, |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 57 | host: 1.0 |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 58 | }, |
| 59 | charge: { |
| 60 | device: -800, |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 61 | host: -1000 |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 62 | }, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 63 | ticksWithoutCollisions: 50, |
| 64 | marginLR: 20, |
| 65 | marginTB: 20, |
| 66 | translate: function() { |
| 67 | return 'translate(' + |
| 68 | config.force.marginLR + ',' + |
| 69 | config.force.marginTB + ')'; |
| 70 | } |
| 71 | }, |
| 72 | labels: { |
Simon Hunt | 19cb098 | 2014-10-23 16:44:49 -0700 | [diff] [blame] | 73 | imgPad: 16, |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 74 | padLR: 8, |
| 75 | padTB: 6, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 76 | marginLR: 3, |
| 77 | marginTB: 2 |
| 78 | }, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 79 | icons: { |
| 80 | w: 32, |
| 81 | h: 32, |
| 82 | xoff: -12, |
Simon Hunt | 19cb098 | 2014-10-23 16:44:49 -0700 | [diff] [blame] | 83 | yoff: -8 |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 84 | }, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 85 | constraints: { |
| 86 | ypos: { |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 87 | host: 0.05, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 88 | switch: 0.3, |
| 89 | roadm: 0.7 |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 90 | } |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 91 | }, |
| 92 | hostLinkWidth: 1.0, |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 93 | hostRadius: 7, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 94 | mouseOutTimerDelayMs: 120 |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | // state variables |
| 98 | var view = {}, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 99 | network = {}, |
| 100 | selected = {}, |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 101 | highlighted = null, |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 102 | hovered = null, |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 103 | viewMode = 'showAll'; |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 104 | |
| 105 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 106 | function debug(what) { |
| 107 | return config.debugOn && config.debug[what]; |
| 108 | } |
| 109 | |
| 110 | // load the topology view of the network |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 111 | function loadNetworkView() { |
| 112 | // Hey, here I am, calling something on the ONOS api: |
| 113 | api.printTime(); |
| 114 | |
| 115 | resize(); |
| 116 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 117 | // go get our network data from the server... |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 118 | d3.json(config.jsonUrl, function (err, data) { |
| 119 | if (err) { |
| 120 | alert('Oops! Error reading JSON...\n\n' + |
Simon Hunt | ae968a6 | 2014-10-22 14:54:41 -0700 | [diff] [blame] | 121 | 'URL: ' + config.jsonUrl + '\n\n' + |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 122 | 'Error: ' + err.message); |
| 123 | return; |
| 124 | } |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 125 | // console.log("here is the JSON data..."); |
| 126 | // console.log(data); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 127 | |
| 128 | network.data = data; |
| 129 | drawNetwork(); |
| 130 | }); |
| 131 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 132 | // while we wait for the data, set up the handlers... |
| 133 | setUpClickHandler(); |
| 134 | setUpRadioButtonHandler(); |
| 135 | setUpKeyHandler(); |
| 136 | $(window).on('resize', resize); |
| 137 | } |
| 138 | |
| 139 | function setUpClickHandler() { |
| 140 | // click handler for "selectable" objects |
| 141 | $(document).on('click', '.select-object', function () { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 142 | // when any object of class "select-object" is clicked... |
| 143 | // TODO: get a reference to the object via lookup... |
| 144 | var obj = network.lookup[$(this).data('id')]; |
| 145 | if (obj) { |
| 146 | selectObject(obj); |
| 147 | } |
| 148 | // stop propagation of event (I think) ... |
| 149 | return false; |
| 150 | }); |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 151 | } |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 152 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 153 | function setUpRadioButtonHandler() { |
| 154 | d3.selectAll('#displayModes .radio').on('click', function () { |
| 155 | var id = d3.select(this).attr('id'); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 156 | if (id !== viewMode) { |
| 157 | radioButton('displayModes', id); |
| 158 | viewMode = id; |
Simon Hunt | f967d51 | 2014-10-28 20:34:29 -0700 | [diff] [blame^] | 159 | doRadioAction(id); |
| 160 | } |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | function doRadioAction(id) { |
| 165 | showAllLayers(); |
| 166 | if (id === 'showPkt') { |
| 167 | showPacketLayer(); |
| 168 | } else if (id === 'showOpt') { |
| 169 | showOpticalLayer(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | function showAllLayers() { |
| 174 | network.node.classed('inactive', false); |
| 175 | network.link.classed('inactive', false); |
| 176 | } |
| 177 | |
| 178 | function showPacketLayer() { |
| 179 | network.node.each(function(d) { |
| 180 | // deactivate nodes that are not hosts or switches |
| 181 | if (d.class === 'device' && d.type !== 'switch') { |
| 182 | d3.select(this).classed('inactive', true); |
| 183 | } |
| 184 | }); |
| 185 | |
| 186 | network.link.each(function(lnk) { |
| 187 | // deactivate infrastructure links that have opt's as endpoints |
| 188 | if (lnk.source.type === 'roadm' || lnk.target.type === 'roadm') { |
| 189 | d3.select(this).classed('inactive', true); |
| 190 | } |
| 191 | }); |
| 192 | } |
| 193 | |
| 194 | function showOpticalLayer() { |
| 195 | network.node.each(function(d) { |
| 196 | // deactivate nodes that are not optical devices |
| 197 | if (d.type !== 'roadm') { |
| 198 | d3.select(this).classed('inactive', true); |
| 199 | } |
| 200 | }); |
| 201 | |
| 202 | network.link.each(function(lnk) { |
| 203 | // deactivate infrastructure links that have opt's as endpoints |
| 204 | if (lnk.source.type !== 'roadm' || lnk.target.type !== 'roadm') { |
| 205 | d3.select(this).classed('inactive', true); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 206 | } |
| 207 | }); |
| 208 | } |
| 209 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 210 | function setUpKeyHandler() { |
| 211 | d3.select('body') |
| 212 | .on('keydown', function () { |
| 213 | processKeyEvent(); |
| 214 | if (debug('showKeyHandler')) { |
| 215 | network.svg.append('text') |
| 216 | .attr('x', 5) |
| 217 | .attr('y', 15) |
| 218 | .style('font-size', '20pt') |
| 219 | .text('keyCode: ' + d3.event.keyCode + |
| 220 | ' applied to : ' + contextLabel()) |
| 221 | .transition().duration(2000) |
| 222 | .style('font-size', '2pt') |
| 223 | .style('fill-opacity', 0.01) |
| 224 | .remove(); |
| 225 | } |
| 226 | }); |
| 227 | } |
| 228 | |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 229 | function contextLabel() { |
| 230 | return hovered === null ? "(nothing)" : hovered.id; |
| 231 | } |
| 232 | |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 233 | function radioButton(group, id) { |
| 234 | d3.selectAll("#" + group + " .radio").classed("active", false); |
| 235 | d3.select("#" + group + " #" + id).classed("active", true); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 238 | function processKeyEvent() { |
| 239 | var code = d3.event.keyCode; |
| 240 | switch (code) { |
| 241 | case 76: // L |
| 242 | cycleLabels(); |
| 243 | break; |
| 244 | case 80: // P |
| 245 | togglePorts(); |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 246 | break; |
| 247 | case 85: // U |
| 248 | unpin(); |
| 249 | break; |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | } |
| 253 | |
| 254 | function cycleLabels() { |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 255 | console.log('Cycle Labels - context = ' + contextLabel()); |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | function togglePorts() { |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 259 | console.log('Toggle Ports - context = ' + contextLabel()); |
| 260 | } |
| 261 | |
| 262 | function unpin() { |
| 263 | if (hovered) { |
| 264 | hovered.fixed = false; |
Simon Hunt | f967d51 | 2014-10-28 20:34:29 -0700 | [diff] [blame^] | 265 | findNodeFromData(hovered).classed('fixed', false); |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 266 | network.force.resume(); |
| 267 | } |
| 268 | console.log('Unpin - context = ' + contextLabel()); |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 271 | |
| 272 | // ======================================================== |
| 273 | |
| 274 | function drawNetwork() { |
| 275 | $('#view').empty(); |
| 276 | |
| 277 | prepareNodesAndLinks(); |
| 278 | createLayout(); |
| 279 | console.log("\n\nHere is the augmented network object..."); |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 280 | console.log(network); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | function prepareNodesAndLinks() { |
| 284 | network.lookup = {}; |
| 285 | network.nodes = []; |
| 286 | network.links = []; |
| 287 | |
| 288 | var nw = network.forceWidth, |
| 289 | nh = network.forceHeight; |
| 290 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 291 | function yPosConstraintForNode(n) { |
| 292 | return config.constraints.ypos[n.type || 'host']; |
| 293 | } |
| 294 | |
| 295 | // Note that both 'devices' and 'hosts' get mapped into the nodes array |
| 296 | |
| 297 | // first, the devices... |
| 298 | network.data.devices.forEach(function(n) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 299 | var ypc = yPosConstraintForNode(n), |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 300 | ix = Math.random() * 0.6 * nw + 0.2 * nw, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 301 | iy = ypc * nh, |
| 302 | node = { |
| 303 | id: n.id, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 304 | labels: n.labels, |
| 305 | class: 'device', |
| 306 | icon: 'device', |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 307 | type: n.type, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 308 | x: ix, |
| 309 | y: iy, |
| 310 | constraint: { |
| 311 | weight: 0.7, |
| 312 | y: iy |
| 313 | } |
| 314 | }; |
| 315 | network.lookup[n.id] = node; |
| 316 | network.nodes.push(node); |
| 317 | }); |
| 318 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 319 | // then, the hosts... |
| 320 | network.data.hosts.forEach(function(n) { |
| 321 | var ypc = yPosConstraintForNode(n), |
| 322 | ix = Math.random() * 0.6 * nw + 0.2 * nw, |
| 323 | iy = ypc * nh, |
| 324 | node = { |
| 325 | id: n.id, |
| 326 | labels: n.labels, |
| 327 | class: 'host', |
| 328 | icon: 'host', |
| 329 | type: n.type, |
| 330 | x: ix, |
| 331 | y: iy, |
| 332 | constraint: { |
| 333 | weight: 0.7, |
| 334 | y: iy |
| 335 | } |
| 336 | }; |
| 337 | network.lookup[n.id] = node; |
| 338 | network.nodes.push(node); |
| 339 | }); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 340 | |
| 341 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 342 | // now, process the explicit links... |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 343 | network.data.links.forEach(function(lnk) { |
| 344 | var src = network.lookup[lnk.src], |
| 345 | dst = network.lookup[lnk.dst], |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 346 | id = src.id + "~" + dst.id; |
| 347 | |
| 348 | var link = { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 349 | class: 'infra', |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 350 | id: id, |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 351 | type: lnk.type, |
| 352 | width: lnk.linkWidth, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 353 | source: src, |
| 354 | target: dst, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 355 | strength: config.force.linkStrength.infra |
| 356 | }; |
| 357 | network.links.push(link); |
| 358 | }); |
| 359 | |
| 360 | // finally, infer host links... |
| 361 | network.data.hosts.forEach(function(n) { |
| 362 | var src = network.lookup[n.id], |
| 363 | dst = network.lookup[n.cp.device], |
| 364 | id = src.id + "~" + dst.id; |
| 365 | |
| 366 | var link = { |
| 367 | class: 'host', |
| 368 | id: id, |
| 369 | type: 'hostLink', |
| 370 | width: config.hostLinkWidth, |
| 371 | source: src, |
| 372 | target: dst, |
| 373 | strength: config.force.linkStrength.host |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 374 | }; |
| 375 | network.links.push(link); |
| 376 | }); |
| 377 | } |
| 378 | |
| 379 | function createLayout() { |
| 380 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 381 | var cfg = config.force; |
| 382 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 383 | network.force = d3.layout.force() |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 384 | .size([network.forceWidth, network.forceHeight]) |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 385 | .nodes(network.nodes) |
| 386 | .links(network.links) |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 387 | .linkStrength(function(d) { return cfg.linkStrength[d.class]; }) |
| 388 | .linkDistance(function(d) { return cfg.linkDistance[d.class]; }) |
| 389 | .charge(function(d) { return cfg.charge[d.class]; }) |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 390 | .on('tick', tick); |
| 391 | |
| 392 | network.svg = d3.select('#view').append('svg') |
| 393 | .attr('width', view.width) |
| 394 | .attr('height', view.height) |
| 395 | .append('g') |
Simon Hunt | ae968a6 | 2014-10-22 14:54:41 -0700 | [diff] [blame] | 396 | .attr('transform', config.force.translate()); |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 397 | // .attr('id', 'zoomable') |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 398 | // .call(d3.behavior.zoom().on("zoom", zoomRedraw)); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 399 | |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 400 | // function zoomRedraw() { |
| 401 | // d3.select("#zoomable").attr("transform", |
| 402 | // "translate(" + d3.event.translate + ")" |
| 403 | // + " scale(" + d3.event.scale + ")"); |
| 404 | // } |
| 405 | |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 406 | // TODO: move glow/blur stuff to util script |
| 407 | var glow = network.svg.append('filter') |
| 408 | .attr('x', '-50%') |
| 409 | .attr('y', '-50%') |
| 410 | .attr('width', '200%') |
| 411 | .attr('height', '200%') |
| 412 | .attr('id', 'blue-glow'); |
| 413 | |
| 414 | glow.append('feColorMatrix') |
| 415 | .attr('type', 'matrix') |
| 416 | .attr('values', '0 0 0 0 0 ' + |
| 417 | '0 0 0 0 0 ' + |
| 418 | '0 0 0 0 .7 ' + |
| 419 | '0 0 0 1 0 '); |
| 420 | |
| 421 | glow.append('feGaussianBlur') |
| 422 | .attr('stdDeviation', 3) |
| 423 | .attr('result', 'coloredBlur'); |
| 424 | |
| 425 | glow.append('feMerge').selectAll('feMergeNode') |
| 426 | .data(['coloredBlur', 'SourceGraphic']) |
| 427 | .enter().append('feMergeNode') |
| 428 | .attr('in', String); |
| 429 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 430 | // TODO: legend (and auto adjust on scroll) |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 431 | // $('#view').on('scroll', function() { |
| 432 | // |
| 433 | // }); |
| 434 | |
| 435 | |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 436 | // add links to the display |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 437 | network.link = network.svg.append('g').selectAll('.link') |
| 438 | .data(network.force.links(), function(d) {return d.id}) |
| 439 | .enter().append('line') |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 440 | .attr('class', function(d) {return 'link ' + d.class}); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 441 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 442 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 443 | // TODO: move drag behavior into separate method. |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 444 | // == define node drag behavior... |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 445 | network.draggedThreshold = d3.scale.linear() |
| 446 | .domain([0, 0.1]) |
| 447 | .range([5, 20]) |
| 448 | .clamp(true); |
| 449 | |
| 450 | function dragged(d) { |
| 451 | var threshold = network.draggedThreshold(network.force.alpha()), |
| 452 | dx = d.oldX - d.px, |
| 453 | dy = d.oldY - d.py; |
| 454 | if (Math.abs(dx) >= threshold || Math.abs(dy) >= threshold) { |
| 455 | d.dragged = true; |
| 456 | } |
| 457 | return d.dragged; |
| 458 | } |
| 459 | |
| 460 | network.drag = d3.behavior.drag() |
| 461 | .origin(function(d) { return d; }) |
| 462 | .on('dragstart', function(d) { |
| 463 | d.oldX = d.x; |
| 464 | d.oldY = d.y; |
| 465 | d.dragged = false; |
| 466 | d.fixed |= 2; |
| 467 | }) |
| 468 | .on('drag', function(d) { |
| 469 | d.px = d3.event.x; |
| 470 | d.py = d3.event.y; |
| 471 | if (dragged(d)) { |
| 472 | if (!network.force.alpha()) { |
| 473 | network.force.alpha(.025); |
| 474 | } |
| 475 | } |
| 476 | }) |
| 477 | .on('dragend', function(d) { |
| 478 | if (!dragged(d)) { |
| 479 | selectObject(d, this); |
| 480 | } |
| 481 | d.fixed &= ~6; |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 482 | |
| 483 | // once we've finished moving, pin the node in position, |
Simon Hunt | f967d51 | 2014-10-28 20:34:29 -0700 | [diff] [blame^] | 484 | // if it is a device (not a host) |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 485 | if (d.class === 'device') { |
| 486 | d.fixed = true; |
Simon Hunt | f967d51 | 2014-10-28 20:34:29 -0700 | [diff] [blame^] | 487 | d3.select(this).classed('fixed', true) |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 488 | } |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 489 | }); |
| 490 | |
| 491 | $('#view').on('click', function(e) { |
| 492 | if (!$(e.target).closest('.node').length) { |
| 493 | deselectObject(); |
| 494 | } |
| 495 | }); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 496 | |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 497 | |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 498 | // add nodes to the display |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 499 | network.node = network.svg.selectAll('.node') |
| 500 | .data(network.force.nodes(), function(d) {return d.id}) |
| 501 | .enter().append('g') |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 502 | .attr('class', function(d) { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 503 | var cls = 'node ' + d.class; |
| 504 | if (d.type) { |
| 505 | cls += ' ' + d.type; |
| 506 | } |
| 507 | return cls; |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 508 | }) |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 509 | .attr('transform', function(d) { |
| 510 | return translate(d.x, d.y); |
| 511 | }) |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 512 | .call(network.drag) |
| 513 | .on('mouseover', function(d) { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 514 | // TODO: show tooltip |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 515 | if (network.mouseoutTimeout) { |
| 516 | clearTimeout(network.mouseoutTimeout); |
| 517 | network.mouseoutTimeout = null; |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 518 | } |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 519 | hoverObject(d); |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 520 | }) |
| 521 | .on('mouseout', function(d) { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 522 | // TODO: hide tooltip |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 523 | if (network.mouseoutTimeout) { |
| 524 | clearTimeout(network.mouseoutTimeout); |
| 525 | network.mouseoutTimeout = null; |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 526 | } |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 527 | network.mouseoutTimeout = setTimeout(function() { |
| 528 | hoverObject(null); |
| 529 | }, config.mouseOutTimerDelayMs); |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 530 | }); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 531 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 532 | |
| 533 | // deal with device nodes first |
| 534 | network.nodeRect = network.node.filter('.device') |
| 535 | .append('rect') |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 536 | .attr({ |
| 537 | rx: 5, |
| 538 | ry: 5, |
| 539 | width: 100, |
| 540 | height: 12 |
| 541 | }); |
| 542 | // note that width/height are adjusted to fit the label text |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 543 | // then padded, and space made for the icon. |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 544 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 545 | network.node.filter('.device').each(function(d) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 546 | var node = d3.select(this), |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 547 | icon = iconUrl(d); |
| 548 | |
| 549 | node.append('text') |
| 550 | // TODO: add label cycle behavior |
| 551 | .text(d.id) |
| 552 | .attr('dy', '1.1em'); |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 553 | |
| 554 | if (icon) { |
| 555 | var cfg = config.icons; |
| 556 | node.append('svg:image') |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 557 | .attr({ |
| 558 | width: cfg.w, |
| 559 | height: cfg.h, |
| 560 | 'xlink:href': icon |
| 561 | }); |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 562 | // note, icon relative positioning (x,y) is done after we have |
| 563 | // adjusted the bounds of the rectangle... |
| 564 | } |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 565 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 566 | // debug function to show the modelled x,y coordinates of nodes... |
| 567 | if (debug('showNodeXY')) { |
| 568 | node.select('rect').attr('fill-opacity', 0.5); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 569 | node.append('circle') |
| 570 | .attr({ |
| 571 | class: 'debug', |
| 572 | cx: 0, |
| 573 | cy: 0, |
| 574 | r: '3px' |
| 575 | }); |
| 576 | } |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 577 | }); |
| 578 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 579 | // now process host nodes |
| 580 | network.nodeCircle = network.node.filter('.host') |
| 581 | .append('circle') |
| 582 | .attr({ |
| 583 | r: config.hostRadius |
| 584 | }); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 585 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 586 | network.node.filter('.host').each(function(d) { |
| 587 | var node = d3.select(this), |
| 588 | icon = iconUrl(d); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 589 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 590 | // debug function to show the modelled x,y coordinates of nodes... |
| 591 | if (debug('showNodeXY')) { |
| 592 | node.select('circle').attr('fill-opacity', 0.5); |
| 593 | node.append('circle') |
| 594 | .attr({ |
| 595 | class: 'debug', |
| 596 | cx: 0, |
| 597 | cy: 0, |
| 598 | r: '3px' |
| 599 | }); |
| 600 | } |
| 601 | }); |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 602 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 603 | // this function is scheduled to happen soon after the given thread ends |
| 604 | setTimeout(function() { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 605 | // post process the device nodes, to pad their size to fit the |
| 606 | // label text and attach the icon to the right location. |
| 607 | network.node.filter('.device').each(function(d) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 608 | // for every node, recompute size, padding, etc. so text fits |
| 609 | var node = d3.select(this), |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 610 | text = node.select('text'), |
| 611 | box = adjustRectToFitText(node), |
| 612 | lab = config.labels; |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 613 | |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 614 | // now make the computed adjustment |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 615 | node.select('rect') |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 616 | .attr(box); |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 617 | |
| 618 | node.select('image') |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 619 | .attr('x', box.x + config.icons.xoff) |
| 620 | .attr('y', box.y + config.icons.yoff); |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 621 | |
Simon Hunt | 5cd0e8f | 2014-10-27 16:18:40 -0700 | [diff] [blame] | 622 | var bounds = boundsFromBox(box); |
| 623 | |
| 624 | // todo: clean up extent and edge work.. |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 625 | d.extent = { |
| 626 | left: bounds.x1 - lab.marginLR, |
| 627 | right: bounds.x2 + lab.marginLR, |
| 628 | top: bounds.y1 - lab.marginTB, |
| 629 | bottom: bounds.y2 + lab.marginTB |
| 630 | }; |
| 631 | |
| 632 | d.edge = { |
| 633 | left : new geo.LineSegment(bounds.x1, bounds.y1, bounds.x1, bounds.y2), |
| 634 | right : new geo.LineSegment(bounds.x2, bounds.y1, bounds.x2, bounds.y2), |
| 635 | top : new geo.LineSegment(bounds.x1, bounds.y1, bounds.x2, bounds.y1), |
| 636 | bottom : new geo.LineSegment(bounds.x1, bounds.y2, bounds.x2, bounds.y2) |
| 637 | }; |
| 638 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 639 | }); |
| 640 | |
| 641 | network.numTicks = 0; |
| 642 | network.preventCollisions = false; |
| 643 | network.force.start(); |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 644 | for (var i = 0; i < config.force.ticksWithoutCollisions; i++) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 645 | network.force.tick(); |
| 646 | } |
| 647 | network.preventCollisions = true; |
| 648 | $('#view').css('visibility', 'visible'); |
| 649 | }); |
| 650 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 651 | |
| 652 | // returns the newly computed bounding box of the rectangle |
| 653 | function adjustRectToFitText(n) { |
| 654 | var text = n.select('text'), |
| 655 | box = text.node().getBBox(), |
| 656 | lab = config.labels; |
| 657 | |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 658 | // not sure why n.data() returns an array of 1 element... |
| 659 | var data = n.data()[0]; |
| 660 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 661 | text.attr('text-anchor', 'middle') |
| 662 | .attr('y', '-0.8em') |
| 663 | .attr('x', lab.imgPad/2) |
| 664 | ; |
| 665 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 666 | // translate the bbox so that it is centered on [x,y] |
| 667 | box.x = -box.width / 2; |
| 668 | box.y = -box.height / 2; |
| 669 | |
| 670 | // add padding |
| 671 | box.x -= (lab.padLR + lab.imgPad/2); |
| 672 | box.width += lab.padLR * 2 + lab.imgPad; |
| 673 | box.y -= lab.padTB; |
| 674 | box.height += lab.padTB * 2; |
| 675 | |
| 676 | return box; |
| 677 | } |
| 678 | |
| 679 | function boundsFromBox(box) { |
| 680 | return { |
| 681 | x1: box.x, |
| 682 | y1: box.y, |
| 683 | x2: box.x + box.width, |
| 684 | y2: box.y + box.height |
| 685 | }; |
| 686 | } |
| 687 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 690 | function iconUrl(d) { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 691 | return config.iconUrl[d.icon]; |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 694 | function translate(x, y) { |
| 695 | return 'translate(' + x + ',' + y + ')'; |
| 696 | } |
| 697 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 698 | // prevents collisions amongst device nodes |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 699 | function preventCollisions() { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 700 | var quadtree = d3.geom.quadtree(network.nodes), |
| 701 | hrad = config.hostRadius; |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 702 | |
| 703 | network.nodes.forEach(function(n) { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 704 | var nx1, nx2, ny1, ny2; |
| 705 | |
| 706 | if (n.class === 'device') { |
| 707 | nx1 = n.x + n.extent.left; |
| 708 | nx2 = n.x + n.extent.right; |
| 709 | ny1 = n.y + n.extent.top; |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 710 | ny2 = n.y + n.extent.bottom; |
| 711 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 712 | } else { |
| 713 | nx1 = n.x - hrad; |
| 714 | nx2 = n.x + hrad; |
| 715 | ny1 = n.y - hrad; |
| 716 | ny2 = n.y + hrad; |
| 717 | } |
| 718 | |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 719 | quadtree.visit(function(quad, x1, y1, x2, y2) { |
| 720 | if (quad.point && quad.point !== n) { |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 721 | // check if the rectangles/circles intersect |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 722 | var p = quad.point, |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 723 | px1, px2, py1, py2, ix; |
| 724 | |
| 725 | if (p.class === 'device') { |
| 726 | px1 = p.x + p.extent.left; |
| 727 | px2 = p.x + p.extent.right; |
| 728 | py1 = p.y + p.extent.top; |
| 729 | py2 = p.y + p.extent.bottom; |
| 730 | |
| 731 | } else { |
| 732 | px1 = p.x - hrad; |
| 733 | px2 = p.x + hrad; |
| 734 | py1 = p.y - hrad; |
| 735 | py2 = p.y + hrad; |
| 736 | } |
| 737 | |
| 738 | ix = (px1 <= nx2 && nx1 <= px2 && py1 <= ny2 && ny1 <= py2); |
| 739 | |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 740 | if (ix) { |
| 741 | var xa1 = nx2 - px1, // shift n left , p right |
| 742 | xa2 = px2 - nx1, // shift n right, p left |
| 743 | ya1 = ny2 - py1, // shift n up , p down |
| 744 | ya2 = py2 - ny1, // shift n down , p up |
| 745 | adj = Math.min(xa1, xa2, ya1, ya2); |
| 746 | |
| 747 | if (adj == xa1) { |
| 748 | n.x -= adj / 2; |
| 749 | p.x += adj / 2; |
| 750 | } else if (adj == xa2) { |
| 751 | n.x += adj / 2; |
| 752 | p.x -= adj / 2; |
| 753 | } else if (adj == ya1) { |
| 754 | n.y -= adj / 2; |
| 755 | p.y += adj / 2; |
| 756 | } else if (adj == ya2) { |
| 757 | n.y += adj / 2; |
| 758 | p.y -= adj / 2; |
| 759 | } |
| 760 | } |
| 761 | return ix; |
| 762 | } |
| 763 | }); |
| 764 | |
| 765 | }); |
| 766 | } |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 767 | |
| 768 | function tick(e) { |
| 769 | network.numTicks++; |
| 770 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 771 | if (config.options.layering) { |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 772 | // adjust the y-coord of each node, based on y-pos constraints |
| 773 | network.nodes.forEach(function (n) { |
| 774 | var z = e.alpha * n.constraint.weight; |
| 775 | if (!isNaN(n.constraint.y)) { |
| 776 | n.y = (n.constraint.y * z + n.y * (1 - z)); |
| 777 | } |
| 778 | }); |
| 779 | } |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 780 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 781 | if (config.options.collisionPrevention && network.preventCollisions) { |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 782 | preventCollisions(); |
| 783 | } |
| 784 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 785 | // clip visualization of links at bounds of nodes... |
| 786 | network.link.each(function(d) { |
| 787 | var xs = d.source.x, |
| 788 | ys = d.source.y, |
| 789 | xt = d.target.x, |
| 790 | yt = d.target.y, |
| 791 | line = new geo.LineSegment(xs, ys, xt, yt), |
| 792 | e, ix; |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 793 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 794 | for (e in d.source.edge) { |
| 795 | ix = line.intersect(d.source.edge[e].offset(xs, ys)); |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 796 | if (ix.in1 && ix.in2) { |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 797 | xs = ix.x; |
| 798 | ys = ix.y; |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | for (e in d.target.edge) { |
| 804 | ix = line.intersect(d.target.edge[e].offset(xt, yt)); |
| 805 | if (ix.in1 && ix.in2) { |
| 806 | xt = ix.x; |
| 807 | yt = ix.y; |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 808 | break; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | d3.select(this) |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 813 | .attr('x1', xs) |
| 814 | .attr('y1', ys) |
| 815 | .attr('x2', xt) |
| 816 | .attr('y2', yt); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 817 | }); |
| 818 | |
Simon Hunt | d35961b | 2014-10-28 08:49:48 -0700 | [diff] [blame] | 819 | // position each node by translating the node (group) by x,y |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 820 | network.node |
| 821 | .attr('transform', function(d) { |
| 822 | return translate(d.x, d.y); |
| 823 | }); |
| 824 | |
| 825 | } |
| 826 | |
| 827 | // $('#docs-close').on('click', function() { |
| 828 | // deselectObject(); |
| 829 | // return false; |
| 830 | // }); |
| 831 | |
| 832 | // $(document).on('click', '.select-object', function() { |
| 833 | // var obj = graph.data[$(this).data('name')]; |
| 834 | // if (obj) { |
| 835 | // selectObject(obj); |
| 836 | // } |
| 837 | // return false; |
| 838 | // }); |
| 839 | |
Simon Hunt | 6f376a3 | 2014-10-28 12:38:30 -0700 | [diff] [blame] | 840 | function findNodeFromData(d) { |
| 841 | var el = null; |
| 842 | network.node.filter('.' + d.class).each(function(n) { |
| 843 | if (n.id === d.id) { |
| 844 | el = d3.select(this); |
| 845 | } |
| 846 | }); |
| 847 | return el; |
| 848 | } |
| 849 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 850 | function selectObject(obj, el) { |
| 851 | var node; |
| 852 | if (el) { |
| 853 | node = d3.select(el); |
| 854 | } else { |
| 855 | network.node.each(function(d) { |
| 856 | if (d == obj) { |
| 857 | node = d3.select(el = this); |
| 858 | } |
| 859 | }); |
| 860 | } |
| 861 | if (!node) return; |
| 862 | |
| 863 | if (node.classed('selected')) { |
| 864 | deselectObject(); |
| 865 | return; |
| 866 | } |
| 867 | deselectObject(false); |
| 868 | |
| 869 | selected = { |
| 870 | obj : obj, |
| 871 | el : el |
| 872 | }; |
| 873 | |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 874 | // highlightObject(obj); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 875 | |
| 876 | node.classed('selected', true); |
| 877 | |
| 878 | // TODO animate incoming info pane |
| 879 | // resize(true); |
| 880 | // TODO: check bounds of selected node and scroll into view if needed |
| 881 | } |
| 882 | |
| 883 | function deselectObject(doResize) { |
| 884 | // Review: logic of 'resize(...)' function. |
| 885 | if (doResize || typeof doResize == 'undefined') { |
| 886 | resize(false); |
| 887 | } |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 888 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 889 | // deselect all nodes in the network... |
| 890 | network.node.classed('selected', false); |
| 891 | selected = {}; |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 892 | // highlightObject(null); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | function highlightObject(obj) { |
| 896 | if (obj) { |
| 897 | if (obj != highlighted) { |
| 898 | // TODO set or clear "inactive" class on nodes, based on criteria |
| 899 | network.node.classed('inactive', function(d) { |
| 900 | // return (obj !== d && |
| 901 | // d.relation(obj.id)); |
| 902 | return (obj !== d); |
| 903 | }); |
| 904 | // TODO: same with links |
| 905 | network.link.classed('inactive', function(d) { |
| 906 | return (obj !== d.source && obj !== d.target); |
| 907 | }); |
| 908 | } |
| 909 | highlighted = obj; |
| 910 | } else { |
| 911 | if (highlighted) { |
| 912 | // clear the inactive flag (no longer suppressed visually) |
| 913 | network.node.classed('inactive', false); |
| 914 | network.link.classed('inactive', false); |
| 915 | } |
| 916 | highlighted = null; |
| 917 | |
| 918 | } |
| 919 | } |
| 920 | |
Simon Hunt | 9a16c82 | 2014-10-28 16:09:19 -0700 | [diff] [blame] | 921 | function hoverObject(obj) { |
| 922 | if (obj) { |
| 923 | hovered = obj; |
| 924 | } else { |
| 925 | if (hovered) { |
| 926 | hovered = null; |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 932 | function resize(showDetails) { |
| 933 | console.log("resize() called..."); |
| 934 | |
| 935 | var $details = $('#details'); |
| 936 | |
| 937 | if (typeof showDetails == 'boolean') { |
| 938 | var showingDetails = showDetails; |
| 939 | // TODO: invoke $details.show() or $details.hide()... |
| 940 | // $details[showingDetails ? 'show' : 'hide'](); |
| 941 | } |
| 942 | |
| 943 | view.height = window.innerHeight - config.mastHeight; |
| 944 | view.width = window.innerWidth; |
| 945 | $('#view') |
| 946 | .css('height', view.height + 'px') |
| 947 | .css('width', view.width + 'px'); |
| 948 | |
| 949 | network.forceWidth = view.width - config.force.marginLR; |
| 950 | network.forceHeight = view.height - config.force.marginTB; |
| 951 | } |
| 952 | |
| 953 | // ====================================================================== |
| 954 | // register with the UI framework |
| 955 | |
| 956 | api.addView('network', { |
| 957 | load: loadNetworkView |
| 958 | }); |
| 959 | |
| 960 | |
| 961 | }(ONOS)); |
| 962 | |