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