Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | ONOS network topology viewer - PoC version 1.0 |
| 3 | |
| 4 | @author Simon Hunt |
| 5 | */ |
| 6 | |
| 7 | (function (onos) { |
| 8 | 'use strict'; |
| 9 | |
| 10 | var api = onos.api; |
| 11 | |
| 12 | var config = { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 13 | options: { |
| 14 | layering: false, |
| 15 | collisionPrevention: true |
| 16 | }, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 17 | jsonUrl: 'network.json', |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 18 | iconUrl: { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 19 | logo: 'img/onos-logo.tiff', |
| 20 | device: 'img/device.png', |
| 21 | host: 'img/host.png', |
| 22 | pkt: 'img/pkt.png', |
| 23 | opt: 'img/opt.png' |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 24 | }, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 25 | mastHeight: 32, |
| 26 | force: { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 27 | note: 'node.class or link.class is used to differentiate', |
| 28 | linkDistance: { |
| 29 | infra: 240, |
| 30 | host: 100 |
| 31 | }, |
| 32 | linkStrength: { |
| 33 | infra: 1.0, |
| 34 | host: 0.4 |
| 35 | }, |
| 36 | charge: { |
| 37 | device: -800, |
| 38 | host: -400 |
| 39 | }, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 40 | ticksWithoutCollisions: 50, |
| 41 | marginLR: 20, |
| 42 | marginTB: 20, |
| 43 | translate: function() { |
| 44 | return 'translate(' + |
| 45 | config.force.marginLR + ',' + |
| 46 | config.force.marginTB + ')'; |
| 47 | } |
| 48 | }, |
| 49 | labels: { |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 50 | imgPad: 22, |
| 51 | padLR: 8, |
| 52 | padTB: 6, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 53 | marginLR: 3, |
| 54 | marginTB: 2 |
| 55 | }, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 56 | icons: { |
| 57 | w: 32, |
| 58 | h: 32, |
| 59 | xoff: -12, |
| 60 | yoff: -10 |
| 61 | }, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 62 | constraints: { |
| 63 | ypos: { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 64 | host: 0.15, |
| 65 | switch: 0.3, |
| 66 | roadm: 0.7 |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 67 | } |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 68 | }, |
| 69 | hostLinkWidth: 1.0, |
| 70 | mouseOutTimerDelayMs: 120 |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 71 | }, |
| 72 | view = {}, |
| 73 | network = {}, |
| 74 | selected = {}, |
| 75 | highlighted = null; |
| 76 | |
| 77 | |
| 78 | function loadNetworkView() { |
| 79 | // Hey, here I am, calling something on the ONOS api: |
| 80 | api.printTime(); |
| 81 | |
| 82 | resize(); |
| 83 | |
| 84 | d3.json(config.jsonUrl, function (err, data) { |
| 85 | if (err) { |
| 86 | alert('Oops! Error reading JSON...\n\n' + |
Simon Hunt | ae968a6 | 2014-10-22 14:54:41 -0700 | [diff] [blame] | 87 | 'URL: ' + config.jsonUrl + '\n\n' + |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 88 | 'Error: ' + err.message); |
| 89 | return; |
| 90 | } |
| 91 | console.log("here is the JSON data..."); |
| 92 | console.log(data); |
| 93 | |
| 94 | network.data = data; |
| 95 | drawNetwork(); |
| 96 | }); |
| 97 | |
| 98 | $(document).on('click', '.select-object', function() { |
| 99 | // when any object of class "select-object" is clicked... |
| 100 | // TODO: get a reference to the object via lookup... |
| 101 | var obj = network.lookup[$(this).data('id')]; |
| 102 | if (obj) { |
| 103 | selectObject(obj); |
| 104 | } |
| 105 | // stop propagation of event (I think) ... |
| 106 | return false; |
| 107 | }); |
| 108 | |
| 109 | $(window).on('resize', resize); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | // ======================================================== |
| 114 | |
| 115 | function drawNetwork() { |
| 116 | $('#view').empty(); |
| 117 | |
| 118 | prepareNodesAndLinks(); |
| 119 | createLayout(); |
| 120 | console.log("\n\nHere is the augmented network object..."); |
| 121 | console.warn(network); |
| 122 | } |
| 123 | |
| 124 | function prepareNodesAndLinks() { |
| 125 | network.lookup = {}; |
| 126 | network.nodes = []; |
| 127 | network.links = []; |
| 128 | |
| 129 | var nw = network.forceWidth, |
| 130 | nh = network.forceHeight; |
| 131 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 132 | function yPosConstraintForNode(n) { |
| 133 | return config.constraints.ypos[n.type || 'host']; |
| 134 | } |
| 135 | |
| 136 | // Note that both 'devices' and 'hosts' get mapped into the nodes array |
| 137 | |
| 138 | // first, the devices... |
| 139 | network.data.devices.forEach(function(n) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 140 | var ypc = yPosConstraintForNode(n), |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 141 | ix = Math.random() * 0.6 * nw + 0.2 * nw, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 142 | iy = ypc * nh, |
| 143 | node = { |
| 144 | id: n.id, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 145 | labels: n.labels, |
| 146 | class: 'device', |
| 147 | icon: 'device', |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 148 | type: n.type, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 149 | x: ix, |
| 150 | y: iy, |
| 151 | constraint: { |
| 152 | weight: 0.7, |
| 153 | y: iy |
| 154 | } |
| 155 | }; |
| 156 | network.lookup[n.id] = node; |
| 157 | network.nodes.push(node); |
| 158 | }); |
| 159 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 160 | // then, the hosts... |
| 161 | network.data.hosts.forEach(function(n) { |
| 162 | var ypc = yPosConstraintForNode(n), |
| 163 | ix = Math.random() * 0.6 * nw + 0.2 * nw, |
| 164 | iy = ypc * nh, |
| 165 | node = { |
| 166 | id: n.id, |
| 167 | labels: n.labels, |
| 168 | class: 'host', |
| 169 | icon: 'host', |
| 170 | type: n.type, |
| 171 | x: ix, |
| 172 | y: iy, |
| 173 | constraint: { |
| 174 | weight: 0.7, |
| 175 | y: iy |
| 176 | } |
| 177 | }; |
| 178 | network.lookup[n.id] = node; |
| 179 | network.nodes.push(node); |
| 180 | }); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 181 | |
| 182 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 183 | // now, process the explicit links... |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 184 | network.data.links.forEach(function(n) { |
| 185 | var src = network.lookup[n.src], |
| 186 | dst = network.lookup[n.dst], |
| 187 | id = src.id + "~" + dst.id; |
| 188 | |
| 189 | var link = { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 190 | class: 'infra', |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 191 | id: id, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 192 | type: n.type, |
| 193 | width: n.linkWidth, |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 194 | source: src, |
| 195 | target: dst, |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 196 | strength: config.force.linkStrength.infra |
| 197 | }; |
| 198 | network.links.push(link); |
| 199 | }); |
| 200 | |
| 201 | // finally, infer host links... |
| 202 | network.data.hosts.forEach(function(n) { |
| 203 | var src = network.lookup[n.id], |
| 204 | dst = network.lookup[n.cp.device], |
| 205 | id = src.id + "~" + dst.id; |
| 206 | |
| 207 | var link = { |
| 208 | class: 'host', |
| 209 | id: id, |
| 210 | type: 'hostLink', |
| 211 | width: config.hostLinkWidth, |
| 212 | source: src, |
| 213 | target: dst, |
| 214 | strength: config.force.linkStrength.host |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 215 | }; |
| 216 | network.links.push(link); |
| 217 | }); |
| 218 | } |
| 219 | |
| 220 | function createLayout() { |
| 221 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 222 | var cfg = config.force; |
| 223 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 224 | network.force = d3.layout.force() |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 225 | .size([network.forceWidth, network.forceHeight]) |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 226 | .nodes(network.nodes) |
| 227 | .links(network.links) |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 228 | .linkStrength(function(d) { return cfg.linkStrength[d.class]; }) |
| 229 | .linkDistance(function(d) { return cfg.linkDistance[d.class]; }) |
| 230 | .charge(function(d) { return cfg.charge[d.class]; }) |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 231 | .on('tick', tick); |
| 232 | |
| 233 | network.svg = d3.select('#view').append('svg') |
| 234 | .attr('width', view.width) |
| 235 | .attr('height', view.height) |
| 236 | .append('g') |
Simon Hunt | ae968a6 | 2014-10-22 14:54:41 -0700 | [diff] [blame] | 237 | .attr('transform', config.force.translate()); |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 238 | // .attr('id', 'zoomable') |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 239 | // .call(d3.behavior.zoom().on("zoom", zoomRedraw)); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 240 | |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 241 | // function zoomRedraw() { |
| 242 | // d3.select("#zoomable").attr("transform", |
| 243 | // "translate(" + d3.event.translate + ")" |
| 244 | // + " scale(" + d3.event.scale + ")"); |
| 245 | // } |
| 246 | |
| 247 | // TODO: svg.append('defs') for markers? |
| 248 | |
| 249 | // TODO: move glow/blur stuff to util script |
| 250 | var glow = network.svg.append('filter') |
| 251 | .attr('x', '-50%') |
| 252 | .attr('y', '-50%') |
| 253 | .attr('width', '200%') |
| 254 | .attr('height', '200%') |
| 255 | .attr('id', 'blue-glow'); |
| 256 | |
| 257 | glow.append('feColorMatrix') |
| 258 | .attr('type', 'matrix') |
| 259 | .attr('values', '0 0 0 0 0 ' + |
| 260 | '0 0 0 0 0 ' + |
| 261 | '0 0 0 0 .7 ' + |
| 262 | '0 0 0 1 0 '); |
| 263 | |
| 264 | glow.append('feGaussianBlur') |
| 265 | .attr('stdDeviation', 3) |
| 266 | .attr('result', 'coloredBlur'); |
| 267 | |
| 268 | glow.append('feMerge').selectAll('feMergeNode') |
| 269 | .data(['coloredBlur', 'SourceGraphic']) |
| 270 | .enter().append('feMergeNode') |
| 271 | .attr('in', String); |
| 272 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 273 | // TODO: legend (and auto adjust on scroll) |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 274 | // $('#view').on('scroll', function() { |
| 275 | // |
| 276 | // }); |
| 277 | |
| 278 | |
| 279 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 280 | |
| 281 | network.link = network.svg.append('g').selectAll('.link') |
| 282 | .data(network.force.links(), function(d) {return d.id}) |
| 283 | .enter().append('line') |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 284 | .attr('class', function(d) {return 'link ' + d.class}); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 285 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 286 | |
| 287 | // == define node drag behavior... |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 288 | network.draggedThreshold = d3.scale.linear() |
| 289 | .domain([0, 0.1]) |
| 290 | .range([5, 20]) |
| 291 | .clamp(true); |
| 292 | |
| 293 | function dragged(d) { |
| 294 | var threshold = network.draggedThreshold(network.force.alpha()), |
| 295 | dx = d.oldX - d.px, |
| 296 | dy = d.oldY - d.py; |
| 297 | if (Math.abs(dx) >= threshold || Math.abs(dy) >= threshold) { |
| 298 | d.dragged = true; |
| 299 | } |
| 300 | return d.dragged; |
| 301 | } |
| 302 | |
| 303 | network.drag = d3.behavior.drag() |
| 304 | .origin(function(d) { return d; }) |
| 305 | .on('dragstart', function(d) { |
| 306 | d.oldX = d.x; |
| 307 | d.oldY = d.y; |
| 308 | d.dragged = false; |
| 309 | d.fixed |= 2; |
| 310 | }) |
| 311 | .on('drag', function(d) { |
| 312 | d.px = d3.event.x; |
| 313 | d.py = d3.event.y; |
| 314 | if (dragged(d)) { |
| 315 | if (!network.force.alpha()) { |
| 316 | network.force.alpha(.025); |
| 317 | } |
| 318 | } |
| 319 | }) |
| 320 | .on('dragend', function(d) { |
| 321 | if (!dragged(d)) { |
| 322 | selectObject(d, this); |
| 323 | } |
| 324 | d.fixed &= ~6; |
| 325 | }); |
| 326 | |
| 327 | $('#view').on('click', function(e) { |
| 328 | if (!$(e.target).closest('.node').length) { |
| 329 | deselectObject(); |
| 330 | } |
| 331 | }); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 332 | |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 333 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 334 | network.node = network.svg.selectAll('.node') |
| 335 | .data(network.force.nodes(), function(d) {return d.id}) |
| 336 | .enter().append('g') |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 337 | .attr('class', function(d) { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 338 | var cls = 'node ' + d.class; |
| 339 | if (d.type) { |
| 340 | cls += ' ' + d.type; |
| 341 | } |
| 342 | return cls; |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 343 | }) |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 344 | .attr('transform', function(d) { |
| 345 | return translate(d.x, d.y); |
| 346 | }) |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 347 | .call(network.drag) |
| 348 | .on('mouseover', function(d) { |
| 349 | if (!selected.obj) { |
| 350 | if (network.mouseoutTimeout) { |
| 351 | clearTimeout(network.mouseoutTimeout); |
| 352 | network.mouseoutTimeout = null; |
| 353 | } |
| 354 | highlightObject(d); |
| 355 | } |
| 356 | }) |
| 357 | .on('mouseout', function(d) { |
| 358 | if (!selected.obj) { |
| 359 | if (network.mouseoutTimeout) { |
| 360 | clearTimeout(network.mouseoutTimeout); |
| 361 | network.mouseoutTimeout = null; |
| 362 | } |
| 363 | network.mouseoutTimeout = setTimeout(function() { |
| 364 | highlightObject(null); |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 365 | }, config.mouseOutTimerDelayMs); |
Simon Hunt | 3ab76a8 | 2014-10-22 13:07:32 -0700 | [diff] [blame] | 366 | } |
| 367 | }); |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 368 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 369 | network.nodeRect = network.node.append('rect') |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 370 | .attr('rx', 5) |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 371 | .attr('ry', 5); |
| 372 | // note that width/height are adjusted to fit the label text |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 373 | |
| 374 | network.node.each(function(d) { |
| 375 | var node = d3.select(this), |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 376 | rect = node.select('rect'), |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 377 | icon = iconUrl(d), |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 378 | text = node.append('text') |
| 379 | .text(d.id) |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 380 | .attr('dy', '1.1em'); |
| 381 | |
| 382 | if (icon) { |
| 383 | var cfg = config.icons; |
| 384 | node.append('svg:image') |
| 385 | .attr('width', cfg.w) |
| 386 | .attr('height', cfg.h) |
| 387 | .attr('xlink:href', icon); |
| 388 | // note, icon relative positioning (x,y) is done after we have |
| 389 | // adjusted the bounds of the rectangle... |
| 390 | } |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 391 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 392 | }); |
| 393 | |
| 394 | // this function is scheduled to happen soon after the given thread ends |
| 395 | setTimeout(function() { |
| 396 | network.node.each(function(d) { |
| 397 | // for every node, recompute size, padding, etc. so text fits |
| 398 | var node = d3.select(this), |
| 399 | text = node.selectAll('text'), |
| 400 | bounds = {}, |
| 401 | first = true; |
| 402 | |
| 403 | // NOTE: probably unnecessary code if we only have one line. |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 404 | text.each(function() { |
| 405 | var box = this.getBBox(); |
| 406 | if (first || box.x < bounds.x1) { |
| 407 | bounds.x1 = box.x; |
| 408 | } |
| 409 | if (first || box.y < bounds.y1) { |
| 410 | bounds.y1 = box.y; |
| 411 | } |
| 412 | if (first || box.x + box.width < bounds.x2) { |
| 413 | bounds.x2 = box.x + box.width; |
| 414 | } |
| 415 | if (first || box.y + box.height < bounds.y2) { |
| 416 | bounds.y2 = box.y + box.height; |
| 417 | } |
| 418 | first = false; |
| 419 | }).attr('text-anchor', 'middle'); |
| 420 | |
| 421 | var lab = config.labels, |
| 422 | oldWidth = bounds.x2 - bounds.x1; |
| 423 | |
| 424 | bounds.x1 -= oldWidth / 2; |
| 425 | bounds.x2 -= oldWidth / 2; |
| 426 | |
| 427 | bounds.x1 -= (lab.padLR + lab.imgPad); |
| 428 | bounds.y1 -= lab.padTB; |
| 429 | bounds.x2 += lab.padLR; |
| 430 | bounds.y2 += lab.padTB; |
| 431 | |
| 432 | node.select('rect') |
| 433 | .attr('x', bounds.x1) |
| 434 | .attr('y', bounds.y1) |
| 435 | .attr('width', bounds.x2 - bounds.x1) |
| 436 | .attr('height', bounds.y2 - bounds.y1); |
| 437 | |
| 438 | node.select('image') |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 439 | .attr('x', bounds.x1 + config.icons.xoff) |
| 440 | .attr('y', bounds.y1 + config.icons.yoff); |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 441 | |
| 442 | d.extent = { |
| 443 | left: bounds.x1 - lab.marginLR, |
| 444 | right: bounds.x2 + lab.marginLR, |
| 445 | top: bounds.y1 - lab.marginTB, |
| 446 | bottom: bounds.y2 + lab.marginTB |
| 447 | }; |
| 448 | |
| 449 | d.edge = { |
| 450 | left : new geo.LineSegment(bounds.x1, bounds.y1, bounds.x1, bounds.y2), |
| 451 | right : new geo.LineSegment(bounds.x2, bounds.y1, bounds.x2, bounds.y2), |
| 452 | top : new geo.LineSegment(bounds.x1, bounds.y1, bounds.x2, bounds.y1), |
| 453 | bottom : new geo.LineSegment(bounds.x1, bounds.y2, bounds.x2, bounds.y2) |
| 454 | }; |
| 455 | |
Simon Hunt | 1c5f8b6 | 2014-10-22 14:43:01 -0700 | [diff] [blame] | 456 | // ==== |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 457 | }); |
| 458 | |
| 459 | network.numTicks = 0; |
| 460 | network.preventCollisions = false; |
| 461 | network.force.start(); |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 462 | for (var i = 0; i < config.force.ticksWithoutCollisions; i++) { |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 463 | network.force.tick(); |
| 464 | } |
| 465 | network.preventCollisions = true; |
| 466 | $('#view').css('visibility', 'visible'); |
| 467 | }); |
| 468 | |
| 469 | } |
| 470 | |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 471 | function iconUrl(d) { |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 472 | return config.iconUrl[d.icon]; |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 475 | function translate(x, y) { |
| 476 | return 'translate(' + x + ',' + y + ')'; |
| 477 | } |
| 478 | |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 479 | function preventCollisions() { |
| 480 | var quadtree = d3.geom.quadtree(network.nodes); |
| 481 | |
| 482 | network.nodes.forEach(function(n) { |
| 483 | var nx1 = n.x + n.extent.left, |
| 484 | nx2 = n.x + n.extent.right, |
| 485 | ny1 = n.y + n.extent.top, |
| 486 | ny2 = n.y + n.extent.bottom; |
| 487 | |
| 488 | quadtree.visit(function(quad, x1, y1, x2, y2) { |
| 489 | if (quad.point && quad.point !== n) { |
| 490 | // check if the rectangles intersect |
| 491 | var p = quad.point, |
| 492 | px1 = p.x + p.extent.left, |
| 493 | px2 = p.x + p.extent.right, |
| 494 | py1 = p.y + p.extent.top, |
| 495 | py2 = p.y + p.extent.bottom, |
| 496 | ix = (px1 <= nx2 && nx1 <= px2 && py1 <= ny2 && ny1 <= py2); |
| 497 | if (ix) { |
| 498 | var xa1 = nx2 - px1, // shift n left , p right |
| 499 | xa2 = px2 - nx1, // shift n right, p left |
| 500 | ya1 = ny2 - py1, // shift n up , p down |
| 501 | ya2 = py2 - ny1, // shift n down , p up |
| 502 | adj = Math.min(xa1, xa2, ya1, ya2); |
| 503 | |
| 504 | if (adj == xa1) { |
| 505 | n.x -= adj / 2; |
| 506 | p.x += adj / 2; |
| 507 | } else if (adj == xa2) { |
| 508 | n.x += adj / 2; |
| 509 | p.x -= adj / 2; |
| 510 | } else if (adj == ya1) { |
| 511 | n.y -= adj / 2; |
| 512 | p.y += adj / 2; |
| 513 | } else if (adj == ya2) { |
| 514 | n.y += adj / 2; |
| 515 | p.y -= adj / 2; |
| 516 | } |
| 517 | } |
| 518 | return ix; |
| 519 | } |
| 520 | }); |
| 521 | |
| 522 | }); |
| 523 | } |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 524 | |
| 525 | function tick(e) { |
| 526 | network.numTicks++; |
| 527 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 528 | if (config.options.layering) { |
Simon Hunt | 68ae665 | 2014-10-22 13:58:07 -0700 | [diff] [blame] | 529 | // adjust the y-coord of each node, based on y-pos constraints |
| 530 | network.nodes.forEach(function (n) { |
| 531 | var z = e.alpha * n.constraint.weight; |
| 532 | if (!isNaN(n.constraint.y)) { |
| 533 | n.y = (n.constraint.y * z + n.y * (1 - z)); |
| 534 | } |
| 535 | }); |
| 536 | } |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 537 | |
Simon Hunt | 2c9e0c2 | 2014-10-23 15:12:58 -0700 | [diff] [blame] | 538 | if (config.options.collisionPrevention && network.preventCollisions) { |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 539 | preventCollisions(); |
| 540 | } |
| 541 | |
| 542 | // TODO: use intersection technique for source end of link also |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 543 | network.link |
| 544 | .attr('x1', function(d) { |
| 545 | return d.source.x; |
| 546 | }) |
| 547 | .attr('y1', function(d) { |
| 548 | return d.source.y; |
| 549 | }) |
Simon Hunt | 1c21989 | 2014-10-22 16:32:39 -0700 | [diff] [blame] | 550 | .each(function(d) { |
| 551 | var x = d.target.x, |
| 552 | y = d.target.y, |
| 553 | line = new geo.LineSegment(d.source.x, d.source.y, x, y); |
| 554 | |
| 555 | for (var e in d.target.edge) { |
| 556 | var ix = line.intersect(d.target.edge[e].offset(x,y)); |
| 557 | if (ix.in1 && ix.in2) { |
| 558 | x = ix.x; |
| 559 | y = ix.y; |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | d3.select(this) |
| 565 | .attr('x2', x) |
| 566 | .attr('y2', y); |
| 567 | |
Simon Hunt | 0b05d4a | 2014-10-21 21:50:15 -0700 | [diff] [blame] | 568 | }); |
| 569 | |
| 570 | network.node |
| 571 | .attr('transform', function(d) { |
| 572 | return translate(d.x, d.y); |
| 573 | }); |
| 574 | |
| 575 | } |
| 576 | |
| 577 | // $('#docs-close').on('click', function() { |
| 578 | // deselectObject(); |
| 579 | // return false; |
| 580 | // }); |
| 581 | |
| 582 | // $(document).on('click', '.select-object', function() { |
| 583 | // var obj = graph.data[$(this).data('name')]; |
| 584 | // if (obj) { |
| 585 | // selectObject(obj); |
| 586 | // } |
| 587 | // return false; |
| 588 | // }); |
| 589 | |
| 590 | function selectObject(obj, el) { |
| 591 | var node; |
| 592 | if (el) { |
| 593 | node = d3.select(el); |
| 594 | } else { |
| 595 | network.node.each(function(d) { |
| 596 | if (d == obj) { |
| 597 | node = d3.select(el = this); |
| 598 | } |
| 599 | }); |
| 600 | } |
| 601 | if (!node) return; |
| 602 | |
| 603 | if (node.classed('selected')) { |
| 604 | deselectObject(); |
| 605 | return; |
| 606 | } |
| 607 | deselectObject(false); |
| 608 | |
| 609 | selected = { |
| 610 | obj : obj, |
| 611 | el : el |
| 612 | }; |
| 613 | |
| 614 | highlightObject(obj); |
| 615 | |
| 616 | node.classed('selected', true); |
| 617 | |
| 618 | // TODO animate incoming info pane |
| 619 | // resize(true); |
| 620 | // TODO: check bounds of selected node and scroll into view if needed |
| 621 | } |
| 622 | |
| 623 | function deselectObject(doResize) { |
| 624 | // Review: logic of 'resize(...)' function. |
| 625 | if (doResize || typeof doResize == 'undefined') { |
| 626 | resize(false); |
| 627 | } |
| 628 | // deselect all nodes in the network... |
| 629 | network.node.classed('selected', false); |
| 630 | selected = {}; |
| 631 | highlightObject(null); |
| 632 | } |
| 633 | |
| 634 | function highlightObject(obj) { |
| 635 | if (obj) { |
| 636 | if (obj != highlighted) { |
| 637 | // TODO set or clear "inactive" class on nodes, based on criteria |
| 638 | network.node.classed('inactive', function(d) { |
| 639 | // return (obj !== d && |
| 640 | // d.relation(obj.id)); |
| 641 | return (obj !== d); |
| 642 | }); |
| 643 | // TODO: same with links |
| 644 | network.link.classed('inactive', function(d) { |
| 645 | return (obj !== d.source && obj !== d.target); |
| 646 | }); |
| 647 | } |
| 648 | highlighted = obj; |
| 649 | } else { |
| 650 | if (highlighted) { |
| 651 | // clear the inactive flag (no longer suppressed visually) |
| 652 | network.node.classed('inactive', false); |
| 653 | network.link.classed('inactive', false); |
| 654 | } |
| 655 | highlighted = null; |
| 656 | |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | function resize(showDetails) { |
| 661 | console.log("resize() called..."); |
| 662 | |
| 663 | var $details = $('#details'); |
| 664 | |
| 665 | if (typeof showDetails == 'boolean') { |
| 666 | var showingDetails = showDetails; |
| 667 | // TODO: invoke $details.show() or $details.hide()... |
| 668 | // $details[showingDetails ? 'show' : 'hide'](); |
| 669 | } |
| 670 | |
| 671 | view.height = window.innerHeight - config.mastHeight; |
| 672 | view.width = window.innerWidth; |
| 673 | $('#view') |
| 674 | .css('height', view.height + 'px') |
| 675 | .css('width', view.width + 'px'); |
| 676 | |
| 677 | network.forceWidth = view.width - config.force.marginLR; |
| 678 | network.forceHeight = view.height - config.force.marginTB; |
| 679 | } |
| 680 | |
| 681 | // ====================================================================== |
| 682 | // register with the UI framework |
| 683 | |
| 684 | api.addView('network', { |
| 685 | load: loadNetworkView |
| 686 | }); |
| 687 | |
| 688 | |
| 689 | }(ONOS)); |
| 690 | |