Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- Topology Layout Module. |
| 19 | Module that contains the d3.force.layout logic |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 25 | var instance, |
| 26 | updateTimer; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 27 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 28 | // default settings for force layout |
| 29 | var defaultSettings = { |
| 30 | gravity: 0.4, |
| 31 | friction: 0.7, |
| 32 | charge: { |
| 33 | // note: key is node.class |
| 34 | device: -8000, |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 35 | host: -20000, |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 36 | region: -8000, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 37 | _def_: -12000 |
| 38 | }, |
| 39 | linkDistance: { |
| 40 | // note: key is link.type |
| 41 | direct: 100, |
| 42 | optical: 120, |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 43 | UiEdgeLink: 100, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 44 | _def_: 50 |
| 45 | }, |
| 46 | linkStrength: { |
| 47 | // note: key is link.type |
| 48 | // range: {0.0 ... 1.0} |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 49 | _def_: 1.0 |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | // configuration |
| 54 | var linkConfig = { |
| 55 | light: { |
| 56 | baseColor: '#939598', |
| 57 | inColor: '#66f', |
| 58 | outColor: '#f00' |
| 59 | }, |
| 60 | dark: { |
| 61 | // TODO : theme |
| 62 | baseColor: '#939598', |
| 63 | inColor: '#66f', |
| 64 | outColor: '#f00' |
| 65 | }, |
| 66 | inWidth: 12, |
| 67 | outWidth: 10 |
| 68 | }; |
| 69 | |
| 70 | // internal state |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 71 | var nodeLock = false; // whether nodes can be dragged or not (locked) |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 72 | |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 73 | // predicate that indicates when clicking is active |
| 74 | function clickEnabled() { |
| 75 | return true; |
| 76 | } |
| 77 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 78 | function getDefaultPosition(link) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 79 | return { |
| 80 | x1: link.get('source').x, |
| 81 | y1: link.get('source').y, |
| 82 | x2: link.get('target').x, |
| 83 | y2: link.get('target').y |
| 84 | }; |
| 85 | } |
| 86 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 87 | angular.module('ovTopo2') |
| 88 | .factory('Topo2LayoutService', |
| 89 | [ |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 90 | '$log', '$timeout', 'WebSocketService', 'SvgUtilService', 'Topo2RegionService', |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 91 | 'Topo2D3Service', 'Topo2ViewService', 'Topo2SelectService', 'Topo2ZoomService', |
| 92 | 'Topo2ViewController', |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 93 | function ($log, $timeout, wss, sus, t2rs, t2d3, t2vs, t2ss, t2zs, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 94 | ViewController) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 95 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 96 | var Layout = ViewController.extend({ |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 97 | init: function (svg, forceG, uplink, dim, zoomer, opts) { |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 98 | $log.debug('initialize Layout'); |
| 99 | instance = this; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 100 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 101 | this.svg = svg; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 102 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 103 | // Append all the SVG Group elements to the forceG object |
| 104 | this.createForceElements(); |
| 105 | |
| 106 | this.uplink = uplink; |
| 107 | this.dim = dim; |
| 108 | this.zoomer = zoomer; |
| 109 | |
| 110 | this.settings = angular.extend({}, defaultSettings, opts); |
| 111 | |
| 112 | this.link = this.elements.linkG.selectAll('.link'); |
| 113 | this.elements.linkLabelG.selectAll('.linkLabel'); |
| 114 | this.node = this.elements.nodeG.selectAll('.node'); |
| 115 | }, |
| 116 | createForceElements: function () { |
| 117 | |
| 118 | this.prevForce = this.forceG; |
| 119 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 120 | this.forceG = d3.select('#topo2-zoomlayer') |
| 121 | .append('g').attr('class', 'topo2-force'); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 122 | |
| 123 | this.elements = { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 124 | linkG: this.addElement(this.forceG, 'topo2-links'), |
| 125 | linkLabelG: this.addElement(this.forceG, 'topo2-linkLabels'), |
| 126 | numLinksLabels: this.addElement(this.forceG, 'topo2-numLinkLabels'), |
| 127 | nodeG: this.addElement(this.forceG, 'topo2-nodes'), |
| 128 | portLabels: this.addElement(this.forceG, 'topo2-portLabels') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 129 | }; |
| 130 | }, |
| 131 | addElement: function (parent, className) { |
| 132 | return parent.append('g').attr('class', className); |
| 133 | }, |
| 134 | settingOrDefault: function (settingName, node) { |
| 135 | var nodeType = node.get('nodeType'); |
| 136 | return this.settings[settingName][nodeType] || this.settings[settingName]._def_; |
| 137 | }, |
| 138 | createForceLayout: function () { |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 139 | var regionLinks = t2rs.regionLinks(), |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 140 | regionNodes = t2rs.regionNodes(); |
| 141 | |
| 142 | this.force = d3.layout.force() |
| 143 | .size(t2vs.getDimensions()) |
| 144 | .gravity(this.settings.gravity) |
| 145 | .friction(this.settings.friction) |
| 146 | .charge(this.settingOrDefault.bind(this, 'charge')) |
| 147 | .linkDistance(this.settingOrDefault.bind(this, 'linkDistance')) |
| 148 | .linkStrength(this.settingOrDefault.bind(this, 'linkStrength')) |
| 149 | .nodes(regionNodes) |
| 150 | .links(regionLinks) |
| 151 | .on("tick", this.tick.bind(this)) |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 152 | .start(); |
| 153 | |
| 154 | this.link = this.elements.linkG.selectAll('.link') |
| 155 | .data(regionLinks, function (d) { return d.get('key'); }); |
| 156 | |
| 157 | this.node = this.elements.nodeG.selectAll('.node') |
| 158 | .data(regionNodes, function (d) { return d.get('id'); }); |
| 159 | |
| 160 | this.drag = sus.createDragBehavior(this.force, |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 161 | function () {}, // click event is no longer handled in the drag service |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 162 | this.atDragEnd, |
| 163 | this.dragEnabled.bind(this), |
| 164 | clickEnabled |
| 165 | ); |
| 166 | |
| 167 | this.update(); |
| 168 | }, |
| 169 | centerLayout: function () { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 170 | d3.select('#topo2-zoomlayer').attr('data-layout', t2rs.model.get('id')); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 171 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 172 | var zoomer = d3.select('#topo2-zoomlayer').node().getBBox(), |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 173 | layoutBBox = this.forceG.node().getBBox(), |
| 174 | scale = (zoomer.height - 150) / layoutBBox.height, |
| 175 | x = (zoomer.width / 2) - ((layoutBBox.x + layoutBBox.width / 2) * scale), |
| 176 | y = (zoomer.height / 2) - ((layoutBBox.y + layoutBBox.height / 2) * scale); |
| 177 | |
| 178 | t2zs.panAndZoom([x, y], scale, 1000); |
| 179 | }, |
| 180 | tick: function () { |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 181 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 182 | this.link |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 183 | .attr("x1", function (d) { return d.get('source').x; }) |
| 184 | .attr("y1", function (d) { return d.get('source').y; }) |
| 185 | .attr("x2", function (d) { return d.get('target').x; }) |
| 186 | .attr("y2", function (d) { return d.get('target').y; }); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 187 | |
| 188 | this.node |
| 189 | .attr({ |
| 190 | transform: function (d) { |
| 191 | var dx = isNaN(d.x) ? 0 : d.x, |
| 192 | dy = isNaN(d.y) ? 0 : d.y; |
| 193 | return sus.translate(dx, dy); |
| 194 | } |
| 195 | }); |
| 196 | }, |
| 197 | |
| 198 | start: function () { |
| 199 | this.force.start(); |
| 200 | }, |
| 201 | update: function () { |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 202 | |
| 203 | if (updateTimer) { |
| 204 | $timeout.cancel(updateTimer); |
| 205 | } |
| 206 | updateTimer = $timeout(this._update.bind(this), 150); |
| 207 | }, |
| 208 | _update: function () { |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 209 | this.updateNodes(); |
| 210 | this.updateLinks(); |
Steven Burrows | 3d8d933 | 2017-03-30 15:05:52 +0100 | [diff] [blame] | 211 | this.force.start(); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 212 | }, |
| 213 | updateNodes: function () { |
| 214 | var regionNodes = t2rs.regionNodes(); |
| 215 | |
| 216 | // select all the nodes in the layout: |
| 217 | this.node = this.elements.nodeG.selectAll('.node') |
| 218 | .data(regionNodes, function (d) { return d.get('id'); }); |
| 219 | |
| 220 | var entering = this.node.enter() |
| 221 | .append('g') |
| 222 | .attr({ |
| 223 | id: function (d) { return sus.safeId(d.get('id')); }, |
| 224 | class: function (d) { return d.svgClassName(); }, |
| 225 | transform: function (d) { |
| 226 | // Need to guard against NaN here ?? |
| 227 | return sus.translate(d.node.x, d.node.y); |
| 228 | }, |
| 229 | opacity: 0 |
| 230 | }) |
| 231 | .call(this.drag) |
| 232 | .transition() |
| 233 | .attr('opacity', 1); |
| 234 | |
| 235 | entering.filter('.device').each(t2d3.nodeEnter); |
| 236 | entering.filter('.sub-region').each(t2d3.nodeEnter); |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 237 | entering.filter('.host').each(t2d3.nodeEnter); |
| 238 | entering.filter('.peer-region').each(t2d3.nodeEnter); |
Steven Burrows | 3d8d933 | 2017-03-30 15:05:52 +0100 | [diff] [blame] | 239 | |
| 240 | this.force.nodes(regionNodes); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 241 | }, |
| 242 | updateLinks: function () { |
| 243 | |
| 244 | var regionLinks = t2rs.regionLinks(); |
| 245 | |
| 246 | this.link = this.elements.linkG.selectAll('.link') |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 247 | .data(regionLinks, function (d) { return d.get('key'); }); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 248 | |
| 249 | // operate on entering links: |
| 250 | var entering = this.link.enter() |
| 251 | .append('line') |
| 252 | .call(this.calcPosition) |
| 253 | .attr({ |
| 254 | x1: function (d) { return d.get('position').x1; }, |
| 255 | y1: function (d) { return d.get('position').y1; }, |
| 256 | x2: function (d) { return d.get('position').x2; }, |
| 257 | y2: function (d) { return d.get('position').y2; }, |
| 258 | stroke: linkConfig.light.inColor, |
| 259 | 'stroke-width': linkConfig.inWidth |
| 260 | }); |
| 261 | |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 262 | entering.each(t2d3.nodeEnter); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 263 | |
| 264 | // operate on exiting links: |
| 265 | this.link.exit() |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 266 | .attr('stroke-dasharray', '3 3') |
| 267 | .style('opacity', 0.5) |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 268 | .transition() |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 269 | .duration(1500) |
| 270 | .attr({ |
| 271 | 'stroke-dasharray': '3 12', |
| 272 | }) |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 273 | .style('opacity', 0.0) |
| 274 | .remove(); |
Steven Burrows | 3d8d933 | 2017-03-30 15:05:52 +0100 | [diff] [blame] | 275 | |
| 276 | this.force.links(regionLinks); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 277 | }, |
| 278 | calcPosition: function () { |
| 279 | var lines = this; |
| 280 | |
| 281 | lines.each(function (d) { |
| 282 | if (d.get('type') === 'hostLink') { |
| 283 | d.set('position', getDefaultPosition(d)); |
| 284 | } |
| 285 | }); |
| 286 | |
| 287 | lines.each(function (d) { |
| 288 | d.set('position', getDefaultPosition(d)); |
| 289 | }); |
| 290 | }, |
| 291 | sendUpdateMeta: function (d, clearPos) { |
| 292 | var metaUi = {}, |
| 293 | ll; |
| 294 | |
| 295 | // if we are not clearing the position data (unpinning), |
| 296 | // attach the x, y, (and equivalent longitude, latitude)... |
| 297 | if (!clearPos) { |
| 298 | ll = d.lngLatFromCoord([d.x, d.y]); |
| 299 | metaUi = { |
| 300 | x: d.x, |
| 301 | y: d.y, |
| 302 | equivLoc: { |
| 303 | lng: ll[0], |
| 304 | lat: ll[1] |
| 305 | } |
| 306 | }; |
| 307 | } |
| 308 | d.metaUi = metaUi; |
| 309 | wss.sendEvent('updateMeta2', { |
| 310 | id: d.get('id'), |
| 311 | class: d.get('class'), |
| 312 | memento: metaUi |
| 313 | }); |
| 314 | }, |
| 315 | setDimensions: function () { |
| 316 | if (this.force) { |
| 317 | this.force.size(t2vs.getDimensions()); |
| 318 | } |
| 319 | }, |
| 320 | dragEnabled: function () { |
| 321 | var ev = d3.event.sourceEvent; |
| 322 | // nodeLock means we aren't allowing nodes to be dragged... |
| 323 | return !nodeLock && !this.zoomingOrPanning(ev); |
| 324 | }, |
| 325 | zoomingOrPanning: function (ev) { |
| 326 | return ev.metaKey || ev.altKey; |
| 327 | }, |
| 328 | atDragEnd: function (d) { |
| 329 | // once we've finished moving, pin the node in position |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 330 | d.fix(true); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 331 | instance.sendUpdateMeta(d); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 332 | t2ss.clickConsumed(true); |
| 333 | }, |
| 334 | transitionDownRegion: function () { |
| 335 | |
| 336 | this.prevForce.transition() |
| 337 | .duration(1500) |
| 338 | .style('opacity', 0) |
| 339 | .remove(); |
| 340 | |
| 341 | this.forceG |
| 342 | .style('opacity', 0) |
| 343 | .transition() |
| 344 | .delay(500) |
| 345 | .duration(500) |
| 346 | .style('opacity', 1); |
| 347 | }, |
| 348 | transitionUpRegion: function () { |
| 349 | this.prevForce.transition() |
| 350 | .duration(1000) |
| 351 | .style('opacity', 0) |
| 352 | .remove(); |
| 353 | |
| 354 | this.forceG |
| 355 | .style('opacity', 0) |
| 356 | .transition() |
| 357 | .delay(500) |
| 358 | .duration(500) |
| 359 | .style('opacity', 1); |
| 360 | } |
| 361 | }); |
| 362 | |
| 363 | function getInstance(svg, forceG, uplink, dim, zoomer, opts) { |
| 364 | return instance || new Layout(svg, forceG, uplink, dim, zoomer, opts); |
| 365 | } |
| 366 | |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 367 | return getInstance(); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 368 | } |
| 369 | ] |
| 370 | ); |
| 371 | })(); |