Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 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 D3 Module. |
| 19 | Functions for manipulating the D3 visualizations of the Topology |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 26 | var $log, fs, sus, is, ts, ps, ttbs; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 27 | |
| 28 | // api to topoForce |
| 29 | var api; |
| 30 | /* |
| 31 | node() // get ref to D3 selection of nodes |
| 32 | link() // get ref to D3 selection of links |
| 33 | linkLabel() // get ref to D3 selection of link labels |
| 34 | instVisible() // true if instances panel is visible |
| 35 | posNode() // position node |
| 36 | showHosts() // true if hosts are to be shown |
| 37 | restyleLinkElement() // update link styles based on backing data |
| 38 | updateLinkLabelModel() // update backing data for link labels |
| 39 | */ |
| 40 | |
| 41 | // configuration |
Simon Hunt | a5487ad | 2016-06-16 13:10:41 -0700 | [diff] [blame] | 42 | var devIconDim = 36, |
| 43 | labelPad = 4, |
| 44 | hostRadius = 14, |
| 45 | badgeConfig = { |
Simon Hunt | 5674db9 | 2015-10-22 16:12:48 -0700 | [diff] [blame] | 46 | radius: 12, |
Simon Hunt | 004fc2c | 2015-10-23 11:55:58 -0700 | [diff] [blame] | 47 | yoff: 5, |
| 48 | gdelta: 10 |
Simon Hunt | a5487ad | 2016-06-16 13:10:41 -0700 | [diff] [blame] | 49 | }, |
| 50 | halfDevIcon = devIconDim / 2, |
| 51 | devBadgeOff = { dx: -halfDevIcon, dy: -halfDevIcon }, |
| 52 | hostBadgeOff = { dx: -hostRadius, dy: -hostRadius }, |
| 53 | status = { |
| 54 | i: 'badgeInfo', |
| 55 | w: 'badgeWarn', |
| 56 | e: 'badgeError' |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Simon Hunt | 1eee51d | 2016-02-26 19:12:13 -0800 | [diff] [blame] | 59 | // NOTE: this type of hack should go away once we have implemented |
| 60 | // the server-side UiModel code. |
| 61 | // {virtual -> cord} is for the E-CORD demo at ONS 2016 |
| 62 | var remappedDeviceTypes = { |
| 63 | virtual: 'cord' |
| 64 | }; |
| 65 | |
| 66 | function mapDeviceTypeToGlyph(type) { |
| 67 | return remappedDeviceTypes[type] || type || 'unknown'; |
| 68 | } |
| 69 | |
Simon Hunt | 5674db9 | 2015-10-22 16:12:48 -0700 | [diff] [blame] | 70 | function badgeStatus(badge) { |
| 71 | return status[badge.status] || status.i; |
| 72 | } |
| 73 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 74 | // internal state |
| 75 | var deviceLabelIndex = 0, |
| 76 | hostLabelIndex = 0; |
| 77 | |
Simon Hunt | a5487ad | 2016-06-16 13:10:41 -0700 | [diff] [blame] | 78 | // note: these are the device icon colors without affinity (no master) |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 79 | var dColTheme = { |
| 80 | light: { |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 81 | online: '#444444', |
| 82 | offline: '#cccccc' |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 83 | }, |
| 84 | dark: { |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 85 | // TODO: theme |
| 86 | online: '#444444', |
| 87 | offline: '#cccccc' |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 88 | } |
| 89 | }; |
| 90 | |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 91 | function devGlyphColor(d) { |
| 92 | var o = d.online, |
| 93 | id = d.master, |
| 94 | otag = o ? 'online' : 'offline'; |
| 95 | return o ? sus.cat7().getColor(id, 0, ts.theme()) |
| 96 | : dColTheme[ts.theme()][otag]; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | function setDeviceColor(d) { |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 100 | d.el.select('use') |
| 101 | .style('fill', devGlyphColor(d)); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 104 | function incDevLabIndex() { |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 105 | setDevLabIndex(deviceLabelIndex+1); |
Bri Prebilic Cole | 9cf1a8d | 2015-04-21 13:15:29 -0700 | [diff] [blame] | 106 | switch(deviceLabelIndex) { |
| 107 | case 0: return 'Hide device labels'; |
| 108 | case 1: return 'Show friendly device labels'; |
| 109 | case 2: return 'Show device ID labels'; |
| 110 | } |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 113 | function setDevLabIndex(mode) { |
| 114 | deviceLabelIndex = mode % 3; |
| 115 | var p = ps.getPrefs('topo_prefs', ttbs.defaultPrefs); |
| 116 | p.dlbls = deviceLabelIndex; |
| 117 | ps.setPrefs('topo_prefs', p); |
| 118 | } |
| 119 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 120 | function hostLabel(d) { |
| 121 | var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0; |
| 122 | return d.labels[idx]; |
| 123 | } |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 124 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 125 | function deviceLabel(d) { |
| 126 | var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0; |
| 127 | return d.labels[idx]; |
| 128 | } |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 129 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 130 | function trimLabel(label) { |
| 131 | return (label && label.trim()) || ''; |
| 132 | } |
| 133 | |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 134 | function computeLabelWidth(n) { |
| 135 | var text = n.select('text'), |
| 136 | box = text.node().getBBox(); |
| 137 | return box.width + labelPad * 2; |
| 138 | } |
| 139 | |
| 140 | function iconBox(dim, labelWidth) { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 141 | return { |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 142 | x: -dim/2, |
| 143 | y: -dim/2, |
| 144 | width: dim + labelWidth, |
| 145 | height: dim |
| 146 | } |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Simon Hunt | 5674db9 | 2015-10-22 16:12:48 -0700 | [diff] [blame] | 149 | function updateDeviceRendering(d) { |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 150 | var node = d.el, |
| 151 | bdg = d.badge, |
| 152 | label = trimLabel(deviceLabel(d)), |
| 153 | labelWidth; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 154 | |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 155 | node.select('text').text(label); |
| 156 | labelWidth = label ? computeLabelWidth(node) : 0; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 157 | |
| 158 | node.select('rect') |
| 159 | .transition() |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 160 | .attr(iconBox(devIconDim, labelWidth)); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 161 | |
Simon Hunt | 5674db9 | 2015-10-22 16:12:48 -0700 | [diff] [blame] | 162 | if (bdg) { |
Simon Hunt | a5487ad | 2016-06-16 13:10:41 -0700 | [diff] [blame] | 163 | renderBadge(node, bdg, devBadgeOff); |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 167 | function updateHostRendering(d) { |
| 168 | var node = d.el, |
Simon Hunt | c2bfe33 | 2015-12-04 11:01:24 -0800 | [diff] [blame] | 169 | bdg = d.badge; |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 170 | |
| 171 | updateHostLabel(d); |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 172 | |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 173 | if (bdg) { |
Simon Hunt | a5487ad | 2016-06-16 13:10:41 -0700 | [diff] [blame] | 174 | renderBadge(node, bdg, hostBadgeOff); |
Simon Hunt | c2bfe33 | 2015-12-04 11:01:24 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 177 | |
Simon Hunt | c2bfe33 | 2015-12-04 11:01:24 -0800 | [diff] [blame] | 178 | function renderBadge(node, bdg, boff) { |
| 179 | var bsel, |
| 180 | bcr = badgeConfig.radius, |
| 181 | bcgd = badgeConfig.gdelta; |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 182 | |
Simon Hunt | c2bfe33 | 2015-12-04 11:01:24 -0800 | [diff] [blame] | 183 | node.select('g.badge').remove(); |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 184 | |
Simon Hunt | c2bfe33 | 2015-12-04 11:01:24 -0800 | [diff] [blame] | 185 | bsel = node.append('g') |
| 186 | .classed('badge', true) |
| 187 | .classed(badgeStatus(bdg), true) |
| 188 | .attr('transform', sus.translate(boff.dx, boff.dy)); |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 189 | |
Simon Hunt | c2bfe33 | 2015-12-04 11:01:24 -0800 | [diff] [blame] | 190 | bsel.append('circle') |
| 191 | .attr('r', bcr); |
| 192 | |
| 193 | if (bdg.txt) { |
| 194 | bsel.append('text') |
| 195 | .attr('dy', badgeConfig.yoff) |
| 196 | .attr('text-anchor', 'middle') |
| 197 | .text(bdg.txt); |
| 198 | } else if (bdg.gid) { |
| 199 | bsel.append('use') |
| 200 | .attr({ |
| 201 | width: bcgd * 2, |
| 202 | height: bcgd * 2, |
| 203 | transform: sus.translate(-bcgd, -bcgd), |
| 204 | 'xlink:href': '#' + bdg.gid |
| 205 | }); |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 209 | function updateHostLabel(d) { |
| 210 | var label = trimLabel(hostLabel(d)); |
| 211 | d.el.select('text').text(label); |
| 212 | } |
| 213 | |
| 214 | function updateDeviceColors(d) { |
| 215 | if (d) { |
| 216 | setDeviceColor(d); |
| 217 | } else { |
| 218 | api.node().filter('.device').each(function (d) { |
| 219 | setDeviceColor(d); |
| 220 | }); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | // ========================== |
| 226 | // updateNodes - subfunctions |
| 227 | |
| 228 | function deviceExisting(d) { |
| 229 | var node = d.el; |
| 230 | node.classed('online', d.online); |
Simon Hunt | 5674db9 | 2015-10-22 16:12:48 -0700 | [diff] [blame] | 231 | updateDeviceRendering(d); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 232 | api.posNode(d, true); |
| 233 | } |
| 234 | |
| 235 | function hostExisting(d) { |
Andrea Campanella | 5212541 | 2015-12-03 14:50:40 -0800 | [diff] [blame] | 236 | updateHostRendering(d); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 237 | api.posNode(d, true); |
| 238 | } |
| 239 | |
| 240 | function deviceEnter(d) { |
| 241 | var node = d3.select(this), |
Simon Hunt | 1eee51d | 2016-02-26 19:12:13 -0800 | [diff] [blame] | 242 | glyphId = mapDeviceTypeToGlyph(d.type), |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 243 | label = trimLabel(deviceLabel(d)), |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 244 | rect, text, glyph, labelWidth; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 245 | |
| 246 | d.el = node; |
| 247 | |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 248 | rect = node.append('rect'); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 249 | |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 250 | text = node.append('text').text(label) |
| 251 | .attr('text-anchor', 'left') |
| 252 | .attr('y', '0.3em') |
Simon Hunt | a5487ad | 2016-06-16 13:10:41 -0700 | [diff] [blame] | 253 | .attr('x', halfDevIcon + labelPad); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 254 | |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 255 | glyph = is.addDeviceIcon(node, glyphId, devIconDim); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 256 | |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 257 | labelWidth = label ? computeLabelWidth(node) : 0; |
| 258 | |
| 259 | rect.attr(iconBox(devIconDim, labelWidth)); |
| 260 | glyph.attr(iconBox(devIconDim, 0)); |
| 261 | |
Simon Hunt | a5487ad | 2016-06-16 13:10:41 -0700 | [diff] [blame] | 262 | node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon)); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | function hostEnter(d) { |
| 266 | var node = d3.select(this), |
| 267 | gid = d.type || 'unknown', |
Simon Hunt | a5487ad | 2016-06-16 13:10:41 -0700 | [diff] [blame] | 268 | textDy = hostRadius + 10; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 269 | |
| 270 | d.el = node; |
| 271 | sus.visible(node, api.showHosts()); |
| 272 | |
Simon Hunt | a5487ad | 2016-06-16 13:10:41 -0700 | [diff] [blame] | 273 | is.addHostIcon(node, hostRadius, gid); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 274 | |
| 275 | node.append('text') |
| 276 | .text(hostLabel) |
| 277 | .attr('dy', textDy) |
| 278 | .attr('text-anchor', 'middle'); |
| 279 | } |
| 280 | |
| 281 | function hostExit(d) { |
| 282 | var node = d.el; |
| 283 | node.select('use') |
| 284 | .style('opacity', 0.5) |
| 285 | .transition() |
| 286 | .duration(800) |
| 287 | .style('opacity', 0); |
| 288 | |
| 289 | node.select('text') |
| 290 | .style('opacity', 0.5) |
| 291 | .transition() |
| 292 | .duration(800) |
| 293 | .style('opacity', 0); |
| 294 | |
| 295 | node.select('circle') |
| 296 | .style('stroke-fill', '#555') |
| 297 | .style('fill', '#888') |
| 298 | .style('opacity', 0.5) |
| 299 | .transition() |
| 300 | .duration(1500) |
| 301 | .attr('r', 0); |
| 302 | } |
| 303 | |
| 304 | function deviceExit(d) { |
| 305 | var node = d.el; |
| 306 | node.select('use') |
| 307 | .style('opacity', 0.5) |
| 308 | .transition() |
| 309 | .duration(800) |
| 310 | .style('opacity', 0); |
| 311 | |
| 312 | node.selectAll('rect') |
| 313 | .style('stroke-fill', '#555') |
| 314 | .style('fill', '#888') |
| 315 | .style('opacity', 0.5); |
| 316 | } |
| 317 | |
| 318 | |
| 319 | // ========================== |
| 320 | // updateLinks - subfunctions |
| 321 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 322 | function linkEntering(d) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 323 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 324 | var link = d3.select(this); |
| 325 | d.el = link; |
| 326 | api.restyleLinkElement(d); |
| 327 | if (d.type() === 'hostLink') { |
| 328 | sus.visible(link, api.showHosts()); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | var linkLabelOffset = '0.3em'; |
| 333 | |
| 334 | function applyLinkLabels() { |
| 335 | var entering; |
| 336 | |
| 337 | api.updateLinkLabelModel(); |
| 338 | |
| 339 | // for elements already existing, we need to update the text |
| 340 | // and adjust the rectangle size to fit |
| 341 | api.linkLabel().each(function (d) { |
| 342 | var el = d3.select(this), |
| 343 | rect = el.select('rect'), |
| 344 | text = el.select('text'); |
| 345 | text.text(d.label); |
| 346 | rect.attr(rectAroundText(el)); |
| 347 | }); |
| 348 | |
| 349 | entering = api.linkLabel().enter().append('g') |
| 350 | .classed('linkLabel', true) |
| 351 | .attr('id', function (d) { return d.id; }); |
| 352 | |
| 353 | entering.each(function (d) { |
| 354 | var el = d3.select(this), |
| 355 | rect, |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 356 | text; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 357 | |
| 358 | if (d.ldata.type() === 'hostLink') { |
| 359 | el.classed('hostLinkLabel', true); |
| 360 | sus.visible(el, api.showHosts()); |
| 361 | } |
| 362 | |
| 363 | d.el = el; |
| 364 | rect = el.append('rect'); |
| 365 | text = el.append('text').text(d.label); |
| 366 | rect.attr(rectAroundText(el)); |
| 367 | text.attr('dy', linkLabelOffset); |
| 368 | |
Carmelo Cascone | d01eda6 | 2016-08-02 10:19:15 -0700 | [diff] [blame] | 369 | el.attr('transform', transformLabel(d.ldata.position, d.key)); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 370 | }); |
| 371 | |
| 372 | // Remove any labels that are no longer required. |
| 373 | api.linkLabel().exit().remove(); |
| 374 | } |
| 375 | |
| 376 | function rectAroundText(el) { |
| 377 | var text = el.select('text'), |
| 378 | box = text.node().getBBox(); |
| 379 | |
| 380 | // translate the bbox so that it is centered on [x,y] |
| 381 | box.x = -box.width / 2; |
| 382 | box.y = -box.height / 2; |
| 383 | |
| 384 | // add padding |
| 385 | box.x -= 1; |
| 386 | box.width += 2; |
| 387 | return box; |
| 388 | } |
| 389 | |
Carmelo Cascone | d01eda6 | 2016-08-02 10:19:15 -0700 | [diff] [blame] | 390 | function generateLabelFunction() { |
| 391 | var labels = []; |
| 392 | var xGap = 15; |
| 393 | var yGap = 17; |
| 394 | |
| 395 | return function(newId, newX, newY) { |
| 396 | |
| 397 | var idx = -1; |
| 398 | |
| 399 | labels.forEach(function(l, i) { |
| 400 | if (l.id === newId) { |
| 401 | idx = i; |
| 402 | return; |
| 403 | } |
| 404 | var minX = l.x - xGap; |
| 405 | var maxX = l.x + xGap; |
| 406 | var minY = l.y - yGap; |
| 407 | var maxY = l.y + yGap; |
| 408 | |
| 409 | if (newX > minX && newX < maxX && newY > minY && newY < maxY) { |
| 410 | // labels are overlapped |
| 411 | newX = newX - xGap; |
| 412 | newY = newY - yGap; |
| 413 | } |
| 414 | }); |
| 415 | |
| 416 | if (idx === -1) { |
| 417 | labels.push({id: newId, x: newX, y: newY}); |
| 418 | } |
| 419 | else { |
| 420 | labels[idx] = {id: newId, x: newX, y: newY}; |
| 421 | } |
| 422 | |
| 423 | return {x: newX, y: newY}; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | var getLabelPosNoOverlap = generateLabelFunction(); |
| 428 | |
| 429 | function transformLabel(p, id) { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 430 | var dx = p.x2 - p.x1, |
| 431 | dy = p.y2 - p.y1, |
| 432 | xMid = dx/2 + p.x1, |
| 433 | yMid = dy/2 + p.y1; |
Carmelo Cascone | d01eda6 | 2016-08-02 10:19:15 -0700 | [diff] [blame] | 434 | |
| 435 | if (id) { |
| 436 | var pos = getLabelPosNoOverlap(id, xMid, yMid); |
| 437 | return sus.translate(pos.x, pos.y); |
| 438 | } |
| 439 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 440 | return sus.translate(xMid, yMid); |
| 441 | } |
| 442 | |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 443 | function applyPortLabels(data, portLabelG) { |
| 444 | var entering = portLabelG.selectAll('.portLabel') |
| 445 | .data(data).enter().append('g') |
| 446 | .classed('portLabel', true) |
| 447 | .attr('id', function (d) { return d.id; }); |
| 448 | |
| 449 | entering.each(function (d) { |
| 450 | var el = d3.select(this), |
| 451 | rect = el.append('rect'), |
| 452 | text = el.append('text').text(d.num); |
| 453 | |
| 454 | rect.attr(rectAroundText(el)); |
| 455 | text.attr('dy', linkLabelOffset); |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 456 | el.attr('transform', sus.translate(d.x, d.y)); |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 457 | }); |
| 458 | } |
| 459 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 460 | function labelPoint(linkPos) { |
| 461 | var lengthUpLine = 1 / 3, |
| 462 | dx = linkPos.x2 - linkPos.x1, |
| 463 | dy = linkPos.y2 - linkPos.y1, |
| 464 | movedX = dx * lengthUpLine, |
| 465 | movedY = dy * lengthUpLine; |
| 466 | |
| 467 | return { |
| 468 | x: movedX, |
| 469 | y: movedY |
| 470 | }; |
| 471 | } |
| 472 | |
| 473 | function calcGroupPos(linkPos) { |
| 474 | var moved = labelPoint(linkPos); |
| 475 | return sus.translate(linkPos.x1 + moved.x, linkPos.y1 + moved.y); |
| 476 | } |
| 477 | |
| 478 | // calculates where on the link that the hash line for 5+ label appears |
| 479 | function hashAttrs(linkPos) { |
| 480 | var hashLength = 25, |
| 481 | halfLength = hashLength / 2, |
| 482 | dx = linkPos.x2 - linkPos.x1, |
| 483 | dy = linkPos.y2 - linkPos.y1, |
| 484 | length = Math.sqrt((dx * dx) + (dy * dy)), |
| 485 | moveAmtX = (dx / length) * halfLength, |
| 486 | moveAmtY = (dy / length) * halfLength, |
| 487 | mid = labelPoint(linkPos), |
| 488 | angle = Math.atan(dy / dx) + 45; |
| 489 | |
| 490 | return { |
| 491 | x1: mid.x - moveAmtX, |
| 492 | y1: mid.y - moveAmtY, |
| 493 | x2: mid.x + moveAmtX, |
| 494 | y2: mid.y + moveAmtY, |
| 495 | stroke: api.linkConfig()[ts.theme()].baseColor, |
| 496 | transform: 'rotate(' + angle + ',' + mid.x + ',' + mid.y + ')' |
| 497 | }; |
| 498 | } |
| 499 | |
| 500 | function textLabelPos(linkPos) { |
| 501 | var point = labelPoint(linkPos), |
| 502 | dist = 20; |
| 503 | return { |
| 504 | x: point.x + dist, |
| 505 | y: point.y + dist |
| 506 | }; |
| 507 | } |
| 508 | |
| 509 | function applyNumLinkLabels(data, lblsG) { |
| 510 | var labels = lblsG.selectAll('g.numLinkLabel') |
| 511 | .data(data, function (d) { return 'pair-' + d.id; }), |
| 512 | entering; |
| 513 | |
| 514 | // update existing labels |
| 515 | labels.each(function (d) { |
| 516 | var el = d3.select(this); |
| 517 | |
| 518 | el.attr({ |
| 519 | transform: function (d) { return calcGroupPos(d.linkCoords); } |
| 520 | }); |
| 521 | el.select('line') |
| 522 | .attr(hashAttrs(d.linkCoords)); |
| 523 | el.select('text') |
| 524 | .attr(textLabelPos(d.linkCoords)) |
| 525 | .text(d.num); |
| 526 | }); |
| 527 | |
| 528 | // add new labels |
| 529 | entering = labels |
| 530 | .enter() |
| 531 | .append('g') |
| 532 | .attr({ |
| 533 | transform: function (d) { return calcGroupPos(d.linkCoords); }, |
| 534 | id: function (d) { return 'pair-' + d.id; } |
| 535 | }) |
| 536 | .classed('numLinkLabel', true); |
| 537 | |
| 538 | entering.each(function (d) { |
| 539 | var el = d3.select(this); |
| 540 | |
| 541 | el.append('line') |
| 542 | .classed('numLinkHash', true) |
| 543 | .attr(hashAttrs(d.linkCoords)); |
| 544 | el.append('text') |
| 545 | .classed('numLinkText', true) |
| 546 | .attr(textLabelPos(d.linkCoords)) |
| 547 | .text(d.num); |
| 548 | }); |
| 549 | |
| 550 | // remove old labels |
| 551 | labels.exit().remove(); |
| 552 | } |
| 553 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 554 | // ========================== |
| 555 | // Module definition |
| 556 | |
| 557 | angular.module('ovTopo') |
| 558 | .factory('TopoD3Service', |
| 559 | ['$log', 'FnService', 'SvgUtilService', 'IconService', 'ThemeService', |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 560 | 'PrefsService', 'TopoToolbarService', |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 561 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 562 | function (_$log_, _fs_, _sus_, _is_, _ts_, _ps_, _ttbs_) { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 563 | $log = _$log_; |
| 564 | fs = _fs_; |
| 565 | sus = _sus_; |
| 566 | is = _is_; |
| 567 | ts = _ts_; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 568 | ps = _ps_; |
| 569 | ttbs = _ttbs_; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 570 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 571 | function initD3(_api_) { |
| 572 | api = _api_; |
| 573 | } |
| 574 | |
| 575 | function destroyD3() { } |
| 576 | |
| 577 | return { |
| 578 | initD3: initD3, |
| 579 | destroyD3: destroyD3, |
| 580 | |
| 581 | incDevLabIndex: incDevLabIndex, |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 582 | setDevLabIndex: setDevLabIndex, |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 583 | hostLabel: hostLabel, |
| 584 | deviceLabel: deviceLabel, |
| 585 | trimLabel: trimLabel, |
| 586 | |
Simon Hunt | 5674db9 | 2015-10-22 16:12:48 -0700 | [diff] [blame] | 587 | updateDeviceLabel: updateDeviceRendering, |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 588 | updateHostLabel: updateHostLabel, |
| 589 | updateDeviceColors: updateDeviceColors, |
| 590 | |
| 591 | deviceExisting: deviceExisting, |
| 592 | hostExisting: hostExisting, |
| 593 | deviceEnter: deviceEnter, |
| 594 | hostEnter: hostEnter, |
| 595 | hostExit: hostExit, |
| 596 | deviceExit: deviceExit, |
| 597 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 598 | linkEntering: linkEntering, |
| 599 | applyLinkLabels: applyLinkLabels, |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 600 | transformLabel: transformLabel, |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 601 | applyPortLabels: applyPortLabels, |
| 602 | applyNumLinkLabels: applyNumLinkLabels |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 603 | }; |
| 604 | }]); |
| 605 | }()); |