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