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