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