Simon Hunt | 737c89f | 2015-01-28 12:23:19 -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 | 737c89f | 2015-01-28 12:23:19 -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 | /* |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 18 | ONOS GUI -- Topology Force Module. |
| 19 | Visualization of the topology in an SVG layer, using a D3 Force Layout. |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 26 | var $log, $timeout, fs, sus, ts, flash, wss, tov, |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 27 | tis, tms, td3, tss, tts, tos, fltr, tls, uplink, svg, tpis; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 28 | |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 29 | // function to be replaced by the localization bundle function |
| 30 | var topoLion = function (x) { |
| 31 | return '#tfs#' + x + '#'; |
| 32 | }; |
| 33 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 34 | // configuration |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 35 | var linkConfig = { |
| 36 | light: { |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 37 | baseColor: '#939598', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 38 | inColor: '#66f', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 39 | outColor: '#f00', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 40 | }, |
| 41 | dark: { |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 42 | // TODO : theme |
| 43 | baseColor: '#939598', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 44 | inColor: '#66f', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 45 | outColor: '#f00', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 46 | }, |
| 47 | inWidth: 12, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 48 | outWidth: 10, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 51 | // internal state |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 52 | var settings, // merged default settings and options |
| 53 | force, // force layout object |
| 54 | drag, // drag behavior handler |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 55 | network = { |
| 56 | nodes: [], |
| 57 | links: [], |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 58 | linksByDevice: {}, |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 59 | lookup: {}, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 60 | revLinkToKey: {}, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 61 | }, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 62 | lu, // shorthand for lookup |
| 63 | rlk, // shorthand for revLinktoKey |
| 64 | showHosts = false, // whether hosts are displayed |
| 65 | showOffline = true, // whether offline devices are displayed |
| 66 | nodeLock = false, // whether nodes can be dragged or not (locked) |
| 67 | fTimer, // timer for delayed force layout |
| 68 | fNodesTimer, // timer for delayed nodes update |
| 69 | fLinksTimer, // timer for delayed links update |
| 70 | dim, // the dimensions of the force layout [w,h] |
| 71 | linkNums = [], // array of link number labels |
| 72 | devIconDim = 36, // node target dimension |
| 73 | devIconDimMin = 20, // node minimum dimension when zoomed out |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 74 | devIconDimMax = 70, // node maximum dimension when zoomed in |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 75 | portLabelDim = 30; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 76 | |
| 77 | // SVG elements; |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 78 | var linkG, linkLabelG, numLinkLblsG, portLabelG, nodeG; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 79 | |
| 80 | // D3 selections; |
| 81 | var link, linkLabel, node; |
| 82 | |
| 83 | // default settings for force layout |
| 84 | var defaultSettings = { |
| 85 | gravity: 0.4, |
| 86 | friction: 0.7, |
| 87 | charge: { |
| 88 | // note: key is node.class |
| 89 | device: -8000, |
| 90 | host: -5000, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 91 | _def_: -12000, |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 92 | }, |
| 93 | linkDistance: { |
| 94 | // note: key is link.type |
| 95 | direct: 100, |
| 96 | optical: 120, |
| 97 | hostLink: 3, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 98 | _def_: 50, |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 99 | }, |
| 100 | linkStrength: { |
| 101 | // note: key is link.type |
| 102 | // range: {0.0 ... 1.0} |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 103 | // direct: 1.0, |
| 104 | // optical: 1.0, |
| 105 | // hostLink: 1.0, |
| 106 | _def_: 1.0, |
| 107 | }, |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 110 | var hostScaleFactor = {icon: 1.0, text: 1.0}; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 111 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 112 | // ========================== |
| 113 | // === EVENT HANDLERS |
| 114 | |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 115 | function mergeNodeData(o, n) { |
| 116 | angular.extend(o, n); |
| 117 | if (!n.location) { |
| 118 | delete o.location; |
| 119 | } |
| 120 | } |
| 121 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 122 | function addDevice(data) { |
| 123 | var id = data.id, |
| 124 | d; |
| 125 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 126 | uplink.showNoDevs(false); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 127 | |
| 128 | // although this is an add device event, if we already have the |
| 129 | // device, treat it as an update instead.. |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 130 | if (lu[id]) { |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 131 | updateDevice(data); |
| 132 | return; |
| 133 | } |
| 134 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 135 | d = tms.createDeviceNode(data); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 136 | network.nodes.push(d); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 137 | lu[id] = d; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 138 | updateNodes(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 139 | fStart(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | function updateDevice(data) { |
| 143 | var id = data.id, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 144 | d = lu[id], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 145 | wasOnline; |
| 146 | |
| 147 | if (d) { |
| 148 | wasOnline = d.online; |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 149 | mergeNodeData(d, data); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 150 | if (tms.positionNode(d, true)) { |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 151 | sendUpdateMeta(d); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 152 | } |
| 153 | updateNodes(); |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 154 | tick(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 155 | if (wasOnline !== d.online) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 156 | tms.findAttachedLinks(d.id).forEach(restyleLinkElement); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 157 | updateOfflineVisibility(d); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 158 | } |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 159 | fStart(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 163 | function removeDevice(data) { |
| 164 | var id = data.id, |
| 165 | d = lu[id]; |
| 166 | if (d) { |
| 167 | removeDeviceElement(d); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | function addHost(data) { |
| 172 | var id = data.id, |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 173 | d; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 174 | |
| 175 | // although this is an add host event, if we already have the |
| 176 | // host, treat it as an update instead.. |
| 177 | if (lu[id]) { |
| 178 | updateHost(data); |
| 179 | return; |
| 180 | } |
| 181 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 182 | d = tms.createHostNode(data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 183 | network.nodes.push(d); |
| 184 | lu[id] = d; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 185 | updateNodes(); |
| 186 | |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 187 | // need to handle possible multiple links (multi-homed host) |
Simon Hunt | 7df764f | 2017-09-14 21:26:14 -0700 | [diff] [blame] | 188 | createHostLinks(data.allCps, d); |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 189 | |
| 190 | if (d.links.length) { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 191 | updateLinks(); |
| 192 | } |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 193 | fStart(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | function updateHost(data) { |
| 197 | var id = data.id, |
| 198 | d = lu[id]; |
| 199 | if (d) { |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 200 | mergeNodeData(d, data); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 201 | if (tms.positionNode(d, true)) { |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 202 | sendUpdateMeta(d); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 203 | } |
| 204 | updateNodes(); |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 205 | tick(); |
| 206 | fStart(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Simon Hunt | 7df764f | 2017-09-14 21:26:14 -0700 | [diff] [blame] | 210 | function createHostLinks(cps, model) { |
| 211 | model.links = []; |
| 212 | cps.forEach(function (cp) { |
| 213 | var linkData = { |
| 214 | key: model.id + '/0-' + cp.device + '/' + cp.port, |
| 215 | dst: cp.device, |
| 216 | dstPort: cp.port, |
| 217 | }; |
| 218 | model.links.push(linkData); |
| 219 | |
| 220 | var lnk = tms.createHostLink(model.id, cp.device, cp.port); |
| 221 | if (lnk) { |
| 222 | network.links.push(lnk); |
| 223 | lu[linkData.key] = lnk; |
| 224 | } |
| 225 | }); |
| 226 | } |
| 227 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 228 | function moveHost(data) { |
| 229 | var id = data.id, |
Simon Hunt | 7df764f | 2017-09-14 21:26:14 -0700 | [diff] [blame] | 230 | d = lu[id]; |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 231 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 232 | if (d) { |
Simon Hunt | 7df764f | 2017-09-14 21:26:14 -0700 | [diff] [blame] | 233 | removeAllLinkElements(d.links); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 234 | |
| 235 | // merge new data |
| 236 | angular.extend(d, data); |
| 237 | if (tms.positionNode(d, true)) { |
| 238 | sendUpdateMeta(d); |
| 239 | } |
| 240 | |
Simon Hunt | 7df764f | 2017-09-14 21:26:14 -0700 | [diff] [blame] | 241 | // now create new host link(s) |
| 242 | createHostLinks(data.allCps, d); |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 243 | |
| 244 | updateNodes(); |
| 245 | updateLinks(); |
| 246 | fResume(); |
| 247 | } |
| 248 | } |
| 249 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 250 | function removeHost(data) { |
| 251 | var id = data.id, |
| 252 | d = lu[id]; |
| 253 | if (d) { |
| 254 | removeHostElement(d, true); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | function addLink(data) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 259 | var result = tms.findLink(data, 'add'), |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 260 | bad = result.badLogic, |
| 261 | d = result.ldata; |
| 262 | |
| 263 | if (bad) { |
Simon Hunt | eb18f52 | 2016-01-28 19:22:23 -0800 | [diff] [blame] | 264 | $log.debug(bad + ': ' + link.id); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | |
| 268 | if (d) { |
| 269 | // we already have a backing store link for src/dst nodes |
| 270 | addLinkUpdate(d, data); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | // no backing store link yet |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 275 | d = tms.createLink(data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 276 | if (d) { |
| 277 | network.links.push(d); |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 278 | aggregateLink(d, data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 279 | lu[d.key] = d; |
| 280 | updateLinks(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 281 | fStart(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | function updateLink(data) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 286 | var result = tms.findLink(data, 'update'), |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 287 | bad = result.badLogic; |
| 288 | if (bad) { |
Simon Hunt | eb18f52 | 2016-01-28 19:22:23 -0800 | [diff] [blame] | 289 | $log.debug(bad + ': ' + link.id); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 290 | return; |
| 291 | } |
Simon Hunt | eb18f52 | 2016-01-28 19:22:23 -0800 | [diff] [blame] | 292 | result.updateWith(data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | function removeLink(data) { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 296 | var result = tms.findLink(data, 'remove'); |
| 297 | |
| 298 | if (!result.badLogic) { |
| 299 | result.removeRawLink(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 300 | } |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 303 | function topoStartDone(data) { |
| 304 | // called when the initial barrage of data has been sent from server |
| 305 | uplink.topoStartDone(); |
| 306 | } |
| 307 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 308 | // ======================== |
| 309 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 310 | function nodeById(id) { |
| 311 | return lu[id]; |
| 312 | } |
| 313 | |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 314 | function makeNodeKey(node1, node2) { |
| 315 | return node1 + '-' + node2; |
| 316 | } |
| 317 | |
| 318 | function findNodePair(key, keyRev) { |
| 319 | if (network.linksByDevice[key]) { |
| 320 | return key; |
| 321 | } else if (network.linksByDevice[keyRev]) { |
| 322 | return keyRev; |
| 323 | } else { |
| 324 | return false; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | function aggregateLink(ldata, link) { |
| 329 | var key = makeNodeKey(link.src, link.dst), |
| 330 | keyRev = makeNodeKey(link.dst, link.src), |
| 331 | found = findNodePair(key, keyRev); |
| 332 | |
| 333 | if (found) { |
| 334 | network.linksByDevice[found].push(ldata); |
| 335 | ldata.devicePair = found; |
| 336 | } else { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 337 | network.linksByDevice[key] = [ldata]; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 338 | ldata.devicePair = key; |
| 339 | } |
| 340 | } |
| 341 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 342 | function addLinkUpdate(ldata, link) { |
| 343 | // add link event, but we already have the reverse link installed |
| 344 | ldata.fromTarget = link; |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 345 | rlk[link.id] = ldata.key; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 346 | // possible solution to el being undefined in restyleLinkElement: |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 347 | // _updateLinks(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 348 | restyleLinkElement(ldata); |
| 349 | } |
| 350 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 351 | |
| 352 | var widthRatio = 1.4, |
| 353 | linkScale = d3.scale.linear() |
| 354 | .domain([1, 12]) |
| 355 | .range([widthRatio, 12 * widthRatio]) |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 356 | .clamp(true), |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 357 | allLinkTypes = 'direct indirect optical tunnel', |
| 358 | allLinkSubTypes = 'inactive not-permitted'; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 359 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 360 | function restyleLinkElement(ldata, immediate) { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 361 | // this fn's job is to look at raw links and decide what svg classes |
| 362 | // need to be applied to the line element in the DOM |
| 363 | var th = ts.theme(), |
| 364 | el = ldata.el, |
| 365 | type = ldata.type(), |
| 366 | lw = ldata.linkWidth(), |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 367 | online = ldata.online(), |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 368 | modeCls = ldata.expected() ? 'inactive' : 'not-permitted', |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 369 | delay = immediate ? 0 : 1000; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 370 | |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 371 | // NOTE: understand why el is sometimes undefined on addLink events... |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 372 | // Investigated: |
| 373 | // el is undefined when it's a reverse link that is being added. |
| 374 | // updateLinks (which sets ldata.el) isn't called before this is called. |
| 375 | // Calling _updateLinks in addLinkUpdate fixes it, but there might be |
| 376 | // a more efficient way to fix it. |
| 377 | if (el && !el.empty()) { |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 378 | el.classed('link', true); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 379 | el.classed(allLinkSubTypes, false); |
| 380 | el.classed(modeCls, !online); |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 381 | el.classed(allLinkTypes, false); |
| 382 | if (type) { |
| 383 | el.classed(type, true); |
| 384 | } |
| 385 | el.transition() |
| 386 | .duration(delay) |
| 387 | .attr('stroke-width', linkScale(lw)) |
| 388 | .attr('stroke', linkConfig[th].baseColor); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 389 | } |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Simon Hunt | 7df764f | 2017-09-14 21:26:14 -0700 | [diff] [blame] | 392 | function removeAllLinkElements(links) { |
| 393 | links.forEach(function (lnk) { |
| 394 | removeLinkElement(lnk); |
| 395 | }); |
| 396 | } |
| 397 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 398 | function removeLinkElement(d) { |
| 399 | var idx = fs.find(d.key, network.links, 'key'), |
| 400 | removed; |
| 401 | if (idx >=0) { |
| 402 | // remove from links array |
| 403 | removed = network.links.splice(idx, 1); |
| 404 | // remove from lookup cache |
| 405 | delete lu[removed[0].key]; |
| 406 | updateLinks(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 407 | fResume(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | function removeHostElement(d, upd) { |
Simon Hunt | 1fb0055 | 2017-09-15 09:21:14 -0700 | [diff] [blame] | 412 | // first, remove associated hostLink(s)... |
| 413 | removeAllLinkElements(d.links); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 414 | |
| 415 | // remove from lookup cache |
| 416 | delete lu[d.id]; |
| 417 | // remove from nodes array |
| 418 | var idx = fs.find(d.id, network.nodes); |
| 419 | network.nodes.splice(idx, 1); |
| 420 | |
| 421 | // remove from SVG |
| 422 | // NOTE: upd is false if we were called from removeDeviceElement() |
| 423 | if (upd) { |
| 424 | updateNodes(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 425 | fResume(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | function removeDeviceElement(d) { |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 430 | var id = d.id, |
| 431 | idx; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 432 | // first, remove associated hosts and links.. |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 433 | tms.findAttachedHosts(id).forEach(removeHostElement); |
| 434 | tms.findAttachedLinks(id).forEach(removeLinkElement); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 435 | |
| 436 | // remove from lookup cache |
| 437 | delete lu[id]; |
| 438 | // remove from nodes array |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 439 | idx = fs.find(id, network.nodes); |
| 440 | if (idx > -1) { |
| 441 | network.nodes.splice(idx, 1); |
| 442 | } |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 443 | |
| 444 | if (!network.nodes.length) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 445 | uplink.showNoDevs(true); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // remove from SVG |
| 449 | updateNodes(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 450 | fResume(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 453 | function updateHostVisibility() { |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 454 | sus.visible(nodeG.selectAll('.host'), showHosts); |
| 455 | sus.visible(linkG.selectAll('.hostLink'), showHosts); |
Simon Hunt | 8eb4d3a | 2015-02-23 18:23:29 -0800 | [diff] [blame] | 456 | sus.visible(linkLabelG.selectAll('.hostLinkLabel'), showHosts); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | function updateOfflineVisibility(dev) { |
| 460 | function updDev(d, show) { |
Simon Hunt | 8eb4d3a | 2015-02-23 18:23:29 -0800 | [diff] [blame] | 461 | var b; |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 462 | sus.visible(d.el, show); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 463 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 464 | tms.findAttachedLinks(d.id).forEach(function (link) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 465 | b = show && ((link.type() !== 'hostLink') || showHosts); |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 466 | sus.visible(link.el, b); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 467 | }); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 468 | tms.findAttachedHosts(d.id).forEach(function (host) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 469 | b = show && showHosts; |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 470 | sus.visible(host.el, b); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 471 | }); |
| 472 | } |
| 473 | |
| 474 | if (dev) { |
| 475 | // updating a specific device that just toggled off/on-line |
| 476 | updDev(dev, dev.online || showOffline); |
| 477 | } else { |
| 478 | // updating all offline devices |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 479 | tms.findDevices(true).forEach(function (d) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 480 | updDev(d, showOffline); |
| 481 | }); |
| 482 | } |
| 483 | } |
| 484 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 485 | |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 486 | function sendUpdateMeta(d, clearPos) { |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 487 | var metaUi = {}, |
| 488 | ll; |
| 489 | |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 490 | // if we are not clearing the position data (unpinning), |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 491 | // attach the x, y, (and equivalent longitude, latitude)... |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 492 | if (!clearPos) { |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 493 | ll = tms.lngLatFromCoord([d.x, d.y]); |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 494 | metaUi = { |
| 495 | x: d.x, |
| 496 | y: d.y, |
| 497 | equivLoc: { |
| 498 | lng: ll[0], |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 499 | lat: ll[1], |
| 500 | }, |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 501 | }; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 502 | } |
| 503 | d.metaUi = metaUi; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 504 | wss.sendEvent('updateMeta', { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 505 | id: d.id, |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 506 | class: d.class, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 507 | memento: metaUi, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 508 | }); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 511 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 512 | function mkSvgClass(d) { |
| 513 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 514 | } |
| 515 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 516 | function vis(b) { |
Simon Hunt | 1603c69 | 2017-08-10 19:53:35 -0700 | [diff] [blame] | 517 | return topoLion(b ? 'visible' : 'hidden'); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 520 | function toggleHosts(x) { |
| 521 | var kev = (x === 'keyev'), |
| 522 | on = kev ? !showHosts : !!x; |
| 523 | |
| 524 | showHosts = on; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 525 | updateHostVisibility(); |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 526 | flash.flash(topoLion('hosts') + ' ' + vis(on)); |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 527 | return on; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 530 | function toggleOffline(x) { |
| 531 | var kev = (x === 'keyev'), |
| 532 | on = kev ? !showOffline : !!x; |
| 533 | |
| 534 | showOffline = on; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 535 | updateOfflineVisibility(); |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 536 | flash.flash(topoLion('fl_offline_devices') + ' ' + vis(on)); |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 537 | return on; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | function cycleDeviceLabels() { |
Bri Prebilic Cole | 9cf1a8d | 2015-04-21 13:15:29 -0700 | [diff] [blame] | 541 | flash.flash(td3.incDevLabIndex()); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 542 | tms.findDevices().forEach(function (d) { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 543 | td3.updateDeviceLabel(d); |
Simon Hunt | 1c36711 | 2015-02-05 18:02:46 -0800 | [diff] [blame] | 544 | }); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 545 | } |
| 546 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 547 | function cycleHostLabels() { |
| 548 | flash.flash(td3.incHostLabIndex()); |
| 549 | tms.findHosts().forEach(function (d) { |
| 550 | td3.updateHostLabel(d); |
| 551 | }); |
| 552 | } |
| 553 | |
Thomas Vachuska | a4eac6a | 2021-03-01 15:11:59 -0800 | [diff] [blame^] | 554 | function cycleLinkLabels() { |
| 555 | td3.toggleLinkLabels(); |
| 556 | } |
| 557 | |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 558 | function unpin() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 559 | var hov = tss.hovered(); |
| 560 | if (hov) { |
| 561 | sendUpdateMeta(hov, true); |
| 562 | hov.fixed = false; |
| 563 | hov.el.classed('fixed', false); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 564 | fResume(); |
| 565 | } |
| 566 | } |
| 567 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 568 | function showMastership(masterId) { |
| 569 | if (!masterId) { |
| 570 | restoreLayerState(); |
| 571 | } else { |
| 572 | showMastershipFor(masterId); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | function restoreLayerState() { |
| 577 | // NOTE: this level of indirection required, for when we have |
| 578 | // the layer filter functionality re-implemented |
| 579 | suppressLayers(false); |
| 580 | } |
| 581 | |
| 582 | function showMastershipFor(id) { |
| 583 | suppressLayers(true); |
| 584 | node.each(function (n) { |
| 585 | if (n.master === id) { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 586 | n.el.classed('suppressedmax', false); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 587 | } |
| 588 | }); |
| 589 | } |
| 590 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 591 | function supAmt(less) { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 592 | return less ? 'suppressed' : 'suppressedmax'; |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | function suppressLayers(b, less) { |
| 596 | var cls = supAmt(less); |
| 597 | node.classed(cls, b); |
| 598 | link.classed(cls, b); |
| 599 | } |
| 600 | |
| 601 | function unsuppressNode(id, less) { |
| 602 | var cls = supAmt(less); |
| 603 | node.each(function (n) { |
| 604 | if (n.id === id) { |
| 605 | n.el.classed(cls, false); |
| 606 | } |
| 607 | }); |
| 608 | } |
| 609 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 610 | function unsuppressLink(key, less) { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 611 | var cls = supAmt(less); |
| 612 | link.each(function (n) { |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 613 | if (n.key === key) { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 614 | n.el.classed(cls, false); |
| 615 | } |
| 616 | }); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 617 | } |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 618 | |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 619 | function showBadLinks() { |
| 620 | var badLinks = tms.findBadLinks(); |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 621 | flash.flash(topoLion('fl_bad_links') + ': ' + badLinks.length); |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 622 | $log.debug('Bad Link List (' + badLinks.length + '):'); |
| 623 | badLinks.forEach(function (d) { |
| 624 | $log.debug('bad link: (' + d.bad + ') ' + d.key, d); |
| 625 | if (d.el) { |
| 626 | d.el.attr('stroke-width', linkScale(2.8)) |
| 627 | .attr('stroke', 'red'); |
| 628 | } |
| 629 | }); |
| 630 | // back to normal after 2 seconds... |
| 631 | $timeout(updateLinks, 2000); |
| 632 | } |
| 633 | |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 634 | function deviceScale(scaleFactor) { |
| 635 | var scale = uplink.zoomer().scale() * scaleFactor, |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 636 | dim = devIconDim, |
| 637 | multiplier = 1; |
| 638 | |
| 639 | if (dim * scale < devIconDimMin) { |
| 640 | multiplier = devIconDimMin / (dim * scale); |
| 641 | } else if (dim * scale > devIconDimMax) { |
| 642 | multiplier = devIconDimMax / (dim * scale); |
| 643 | } |
| 644 | |
| 645 | return multiplier; |
| 646 | } |
| 647 | |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 648 | function linkWidthScale() { |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 649 | var scale = uplink.zoomer().scale(); |
| 650 | return linkScale(widthRatio) / scale; |
| 651 | } |
| 652 | |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 653 | function portLabelScale() { |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 654 | var scale = uplink.zoomer().scale(); |
| 655 | return portLabelDim / (portLabelDim * scale); |
| 656 | } |
| 657 | |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 658 | function adjustNodeScale() { |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 659 | // Scale the network nodes |
| 660 | _.each(network.nodes, function (node) { |
| 661 | if (node.class === 'host') { |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 662 | node.el.selectAll('g').style('transform', 'scale(' + deviceScale(hostScaleFactor.icon) + ')'); |
| 663 | node.el.selectAll('text').style('transform', 'scale(' + deviceScale(hostScaleFactor.text) + ')'); |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 664 | return; |
| 665 | } |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 666 | node.el.selectAll('*').style('transform', 'scale(' + deviceScale(1.0) + ')'); |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 667 | }); |
| 668 | |
| 669 | // Scale the network links |
| 670 | _.each(network.links, function (link) { |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 671 | link.el.style('stroke-width', linkWidthScale() + 'px'); |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 672 | }); |
| 673 | |
| 674 | d3.select('#topo-portLabels') |
| 675 | .selectAll('.portLabel') |
| 676 | .selectAll('*') |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 677 | .style('transform', 'scale(' + portLabelScale() + ')'); |
| 678 | } |
| 679 | |
| 680 | |
| 681 | function toggleScale(scale) { |
| 682 | if (scale < 0.5) { |
| 683 | return 1.0 |
| 684 | } |
| 685 | return scale - 0.2; |
| 686 | } |
| 687 | |
| 688 | function toggleHostTextSize() { |
| 689 | hostScaleFactor.text = toggleScale(hostScaleFactor.text); |
| 690 | adjustNodeScale(); |
| 691 | } |
| 692 | |
| 693 | function toggleHostIconSize() { |
| 694 | hostScaleFactor.icon = toggleScale(hostScaleFactor.icon); |
| 695 | adjustNodeScale(); |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 698 | function resetAllLocations() { |
| 699 | tms.resetAllLocations(); |
| 700 | updateNodes(); |
| 701 | tick(); // force nodes to be redrawn in their new locations |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 702 | flash.flash(topoLion('fl_reset_node_locations')); |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 705 | // ========================================== |
| 706 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 707 | function updateNodes() { |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 708 | if (fNodesTimer) { |
| 709 | $timeout.cancel(fNodesTimer); |
| 710 | } |
| 711 | fNodesTimer = $timeout(_updateNodes, 150); |
| 712 | } |
| 713 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 714 | // IMPLEMENTATION NOTE: _updateNodes() should NOT stop, start, or resume |
| 715 | // the force layout; that needs to be determined and implemented elsewhere |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 716 | function _updateNodes() { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 717 | // select all the nodes in the layout: |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 718 | node = nodeG.selectAll('.node') |
| 719 | .data(network.nodes, function (d) { return d.id; }); |
| 720 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 721 | // operate on existing nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 722 | node.filter('.device').each(td3.deviceExisting); |
| 723 | node.filter('.host').each(td3.hostExisting); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 724 | |
| 725 | // operate on entering nodes: |
| 726 | var entering = node.enter() |
| 727 | .append('g') |
| 728 | .attr({ |
| 729 | id: function (d) { return sus.safeId(d.id); }, |
| 730 | class: mkSvgClass, |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 731 | transform: function (d) { |
| 732 | // Need to guard against NaN here ?? |
| 733 | return sus.translate(d.x, d.y); |
| 734 | }, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 735 | opacity: 0, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 736 | }) |
| 737 | .call(drag) |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 738 | .on('mouseover', tss.nodeMouseOver) |
| 739 | .on('mouseout', tss.nodeMouseOut) |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 740 | .transition() |
| 741 | .attr('opacity', 1); |
| 742 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 743 | // augment entering nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 744 | entering.filter('.device').each(td3.deviceEnter); |
| 745 | entering.filter('.host').each(td3.hostEnter); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 746 | |
Simon Hunt | 5105659 | 2015-02-03 21:48:07 -0800 | [diff] [blame] | 747 | // operate on both existing and new nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 748 | td3.updateDeviceColors(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 749 | |
| 750 | // operate on exiting nodes: |
| 751 | // Note that the node is removed after 2 seconds. |
| 752 | // Sub element animations should be shorter than 2 seconds. |
| 753 | var exiting = node.exit() |
| 754 | .transition() |
| 755 | .duration(2000) |
| 756 | .style('opacity', 0) |
| 757 | .remove(); |
| 758 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 759 | // exiting node specifics: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 760 | exiting.filter('.host').each(td3.hostExit); |
| 761 | exiting.filter('.device').each(td3.deviceExit); |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 762 | tick(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 763 | } |
| 764 | |
Simon Hunt | 5105659 | 2015-02-03 21:48:07 -0800 | [diff] [blame] | 765 | // ========================== |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 766 | |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 767 | function getDefaultPos(link) { |
| 768 | return { |
| 769 | x1: link.source.x, |
| 770 | y1: link.source.y, |
| 771 | x2: link.target.x, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 772 | y2: link.target.y, |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 773 | }; |
| 774 | } |
| 775 | |
| 776 | // returns amount of adjustment along the normal for given link |
| 777 | function amt(numLinks, linkIdx) { |
| 778 | var gap = 6; |
| 779 | return (linkIdx - ((numLinks - 1) / 2)) * gap; |
| 780 | } |
| 781 | |
| 782 | function calcMovement(d, amt, flipped) { |
| 783 | var pos = getDefaultPos(d), |
| 784 | mult = flipped ? -amt : amt, |
| 785 | dx = pos.x2 - pos.x1, |
| 786 | dy = pos.y2 - pos.y1, |
| 787 | length = Math.sqrt((dx * dx) + (dy * dy)); |
| 788 | |
| 789 | return { |
| 790 | x1: pos.x1 + (mult * dy / length), |
| 791 | y1: pos.y1 + (mult * -dx / length), |
| 792 | x2: pos.x2 + (mult * dy / length), |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 793 | y2: pos.y2 + (mult * -dx / length), |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 794 | }; |
| 795 | } |
| 796 | |
| 797 | function calcPosition() { |
| 798 | var lines = this, |
| 799 | linkSrcId; |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 800 | linkNums = []; |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 801 | lines.each(function (d) { |
| 802 | if (d.type() === 'hostLink') { |
| 803 | d.position = getDefaultPos(d); |
| 804 | } |
| 805 | }); |
| 806 | |
| 807 | function normalizeLinkSrc(link) { |
| 808 | // ensure source device is consistent across set of links |
| 809 | // temporary measure until link modeling is refactored |
| 810 | if (!linkSrcId) { |
| 811 | linkSrcId = link.source.id; |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | return link.source.id !== linkSrcId; |
| 816 | } |
| 817 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 818 | angular.forEach(network.linksByDevice, function (linkArr, key) { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 819 | var numLinks = linkArr.length, |
| 820 | link; |
| 821 | |
| 822 | if (numLinks === 1) { |
| 823 | link = linkArr[0]; |
| 824 | link.position = getDefaultPos(link); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 825 | link.position.multiLink = false; |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 826 | } else if (numLinks >= 5) { |
| 827 | // this code is inefficient, in the future the way links |
| 828 | // are modeled will be changed |
| 829 | angular.forEach(linkArr, function (link) { |
| 830 | link.position = getDefaultPos(link); |
| 831 | link.position.multiLink = true; |
| 832 | }); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 833 | linkNums.push({ |
| 834 | id: key, |
| 835 | num: numLinks, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 836 | linkCoords: linkArr[0].position, |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 837 | }); |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 838 | } else { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 839 | linkSrcId = null; |
| 840 | angular.forEach(linkArr, function (link, index) { |
| 841 | var offsetAmt = amt(numLinks, index), |
| 842 | needToFlip = normalizeLinkSrc(link); |
| 843 | link.position = calcMovement(link, offsetAmt, needToFlip); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 844 | link.position.multiLink = false; |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 845 | }); |
| 846 | } |
| 847 | }); |
| 848 | } |
| 849 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 850 | function updateLinks() { |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 851 | if (fLinksTimer) { |
| 852 | $timeout.cancel(fLinksTimer); |
| 853 | } |
| 854 | fLinksTimer = $timeout(_updateLinks, 150); |
| 855 | } |
| 856 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 857 | // IMPLEMENTATION NOTE: _updateLinks() should NOT stop, start, or resume |
| 858 | // the force layout; that needs to be determined and implemented elsewhere |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 859 | function _updateLinks() { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 860 | var th = ts.theme(); |
| 861 | |
| 862 | link = linkG.selectAll('.link') |
| 863 | .data(network.links, function (d) { return d.key; }); |
| 864 | |
| 865 | // operate on existing links: |
Simon Hunt | d526412 | 2015-02-25 10:17:43 -0800 | [diff] [blame] | 866 | link.each(function (d) { |
| 867 | // this is supposed to be an existing link, but we have observed |
| 868 | // occasions (where links are deleted and added rapidly?) where |
| 869 | // the DOM element has not been defined. So protect against that... |
| 870 | if (d.el) { |
| 871 | restyleLinkElement(d, true); |
| 872 | } |
| 873 | }); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 874 | |
| 875 | // operate on entering links: |
| 876 | var entering = link.enter() |
| 877 | .append('line') |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 878 | .call(calcPosition) |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 879 | .attr({ |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 880 | x1: function (d) { return d.position.x1; }, |
| 881 | y1: function (d) { return d.position.y1; }, |
| 882 | x2: function (d) { return d.position.x2; }, |
| 883 | y2: function (d) { return d.position.y2; }, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 884 | stroke: linkConfig[th].inColor, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 885 | 'stroke-width': linkConfig.inWidth, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 886 | }); |
| 887 | |
| 888 | // augment links |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 889 | entering.each(td3.linkEntering); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 890 | |
| 891 | // operate on both existing and new links: |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 892 | // link.each(...) |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 893 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 894 | // add labels for how many links are in a thick line |
| 895 | td3.applyNumLinkLabels(linkNums, numLinkLblsG); |
| 896 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 897 | // apply or remove labels |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 898 | td3.applyLinkLabels(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 899 | |
| 900 | // operate on exiting links: |
| 901 | link.exit() |
| 902 | .attr('stroke-dasharray', '3 3') |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 903 | .attr('stroke', linkConfig[th].outColor) |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 904 | .style('opacity', 0.5) |
| 905 | .transition() |
| 906 | .duration(1500) |
| 907 | .attr({ |
| 908 | 'stroke-dasharray': '3 12', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 909 | 'stroke-width': linkConfig.outWidth, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 910 | }) |
| 911 | .style('opacity', 0.0) |
| 912 | .remove(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 915 | |
| 916 | // ========================== |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 917 | // force layout tick function |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 918 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 919 | function fResume() { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 920 | if (!tos.isOblique()) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 921 | force.resume(); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | function fStart() { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 926 | if (!tos.isOblique()) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 927 | if (fTimer) { |
| 928 | $timeout.cancel(fTimer); |
| 929 | } |
| 930 | fTimer = $timeout(function () { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 931 | $log.debug('Starting force-layout'); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 932 | force.start(); |
| 933 | }, 200); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | var tickStuff = { |
| 938 | nodeAttr: { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 939 | transform: function (d) { |
| 940 | var dx = isNaN(d.x) ? 0 : d.x, |
| 941 | dy = isNaN(d.y) ? 0 : d.y; |
| 942 | return sus.translate(dx, dy); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 943 | }, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 944 | }, |
| 945 | linkAttr: { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 946 | x1: function (d) { return d.position.x1; }, |
| 947 | y1: function (d) { return d.position.y1; }, |
| 948 | x2: function (d) { return d.position.x2; }, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 949 | y2: function (d) { return d.position.y2; }, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 950 | }, |
| 951 | linkLabelAttr: { |
| 952 | transform: function (d) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 953 | var lnk = tms.findLinkById(d.key); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 954 | if (lnk) { |
Carmelo Cascone | d01eda6 | 2016-08-02 10:19:15 -0700 | [diff] [blame] | 955 | return td3.transformLabel(lnk.position, d.key); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 956 | } |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 957 | }, |
| 958 | }, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 959 | }; |
| 960 | |
| 961 | function tick() { |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 962 | // guard against null (which can happen when our view pages out)... |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 963 | if (node && node.size()) { |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 964 | node.attr(tickStuff.nodeAttr); |
| 965 | } |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 966 | if (link && link.size()) { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 967 | link.call(calcPosition) |
| 968 | .attr(tickStuff.linkAttr); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 969 | td3.applyNumLinkLabels(linkNums, numLinkLblsG); |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 970 | } |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 971 | if (linkLabel && linkLabel.size()) { |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 972 | linkLabel.attr(tickStuff.linkLabelAttr); |
| 973 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 977 | // ========================== |
| 978 | // === MOUSE GESTURE HANDLERS |
| 979 | |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 980 | function zoomingOrPanning(ev) { |
| 981 | return ev.metaKey || ev.altKey; |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | function atDragEnd(d) { |
| 985 | // once we've finished moving, pin the node in position |
| 986 | d.fixed = true; |
| 987 | d3.select(this).classed('fixed', true); |
| 988 | sendUpdateMeta(d); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 989 | tss.clickConsumed(true); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | // predicate that indicates when dragging is active |
| 993 | function dragEnabled() { |
| 994 | var ev = d3.event.sourceEvent; |
| 995 | // nodeLock means we aren't allowing nodes to be dragged... |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 996 | return !nodeLock && !zoomingOrPanning(ev); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | // predicate that indicates when clicking is active |
| 1000 | function clickEnabled() { |
| 1001 | return true; |
| 1002 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1003 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1004 | // ============================================= |
| 1005 | // function entry points for overlay module |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1006 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1007 | // TODO: find an automatic way of tracking via the "showHighlights" events |
Simon Hunt | e50829c | 2015-06-09 08:39:28 -0700 | [diff] [blame] | 1008 | var allTrafficClasses = 'primary secondary optical animated ' + |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 1009 | 'port-traffic-green port-traffic-yellow port-traffic-orange ' + |
| 1010 | 'port-traffic-red'; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1011 | |
| 1012 | function clearLinkTrafficStyle() { |
| 1013 | link.style('stroke-width', null) |
| 1014 | .classed(allTrafficClasses, false); |
| 1015 | } |
| 1016 | |
| 1017 | function removeLinkLabels() { |
| 1018 | network.links.forEach(function (d) { |
| 1019 | d.label = ''; |
| 1020 | }); |
| 1021 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1022 | |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 1023 | function clearNodeDeco() { |
| 1024 | node.selectAll('g.badge').remove(); |
| 1025 | } |
| 1026 | |
| 1027 | function removeNodeBadges() { |
| 1028 | network.nodes.forEach(function (d) { |
| 1029 | d.badge = null; |
| 1030 | }); |
| 1031 | } |
| 1032 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1033 | function updateLinkLabelModel() { |
| 1034 | // create the backing data for showing labels.. |
| 1035 | var data = []; |
| 1036 | link.each(function (d) { |
| 1037 | if (d.label) { |
| 1038 | data.push({ |
| 1039 | id: 'lab-' + d.key, |
| 1040 | key: d.key, |
| 1041 | label: d.label, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1042 | ldata: d, |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1043 | }); |
| 1044 | } |
| 1045 | }); |
| 1046 | |
| 1047 | linkLabel = linkLabelG.selectAll('.linkLabel') |
| 1048 | .data(data, function (d) { return d.id; }); |
| 1049 | } |
| 1050 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1051 | // ========================== |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1052 | // Module definition |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1053 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 1054 | function mkModelApi(uplink) { |
| 1055 | return { |
| 1056 | projection: uplink.projection, |
| 1057 | network: network, |
| 1058 | restyleLinkElement: restyleLinkElement, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1059 | removeLinkElement: removeLinkElement, |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 1060 | }; |
| 1061 | } |
| 1062 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1063 | function mkD3Api() { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1064 | return { |
| 1065 | node: function () { return node; }, |
| 1066 | link: function () { return link; }, |
| 1067 | linkLabel: function () { return linkLabel; }, |
| 1068 | instVisible: function () { return tis.isVisible(); }, |
| 1069 | posNode: tms.positionNode, |
| 1070 | showHosts: function () { return showHosts; }, |
| 1071 | restyleLinkElement: restyleLinkElement, |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1072 | updateLinkLabelModel: updateLinkLabelModel, |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 1073 | linkConfig: function () { return linkConfig; }, |
| 1074 | deviceScale: deviceScale, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1075 | linkWidthScale: linkWidthScale, |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1076 | }; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1079 | function mkSelectApi() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1080 | return { |
| 1081 | node: function () { return node; }, |
| 1082 | zoomingOrPanning: zoomingOrPanning, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 1083 | updateDeviceColors: td3.updateDeviceColors, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1084 | deselectAllLinks: tls.deselectAllLinks, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1085 | }; |
| 1086 | } |
| 1087 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1088 | function mkTrafficApi() { |
| 1089 | return { |
| 1090 | hovered: tss.hovered, |
| 1091 | somethingSelected: tss.somethingSelected, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1092 | selectOrder: tss.selectOrder, |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1093 | }; |
| 1094 | } |
| 1095 | |
| 1096 | function mkOverlayApi() { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1097 | return { |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 1098 | clearNodeDeco: clearNodeDeco, |
| 1099 | removeNodeBadges: removeNodeBadges, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1100 | clearLinkTrafficStyle: clearLinkTrafficStyle, |
| 1101 | removeLinkLabels: removeLinkLabels, |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 1102 | findLinkById: tms.findLinkById, |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 1103 | findNodeById: nodeById, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1104 | updateLinks: updateLinks, |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 1105 | updateNodes: updateNodes, |
| 1106 | supLayers: suppressLayers, |
| 1107 | unsupNode: unsuppressNode, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1108 | unsupLink: unsuppressLink, |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1109 | }; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1112 | function mkObliqueApi(uplink, fltr) { |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1113 | return { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1114 | force: function () { return force; }, |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1115 | zoomLayer: uplink.zoomLayer, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1116 | nodeGBBox: function () { return nodeG.node().getBBox(); }, |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1117 | node: function () { return node; }, |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1118 | link: function () { return link; }, |
| 1119 | linkLabel: function () { return linkLabel; }, |
| 1120 | nodes: function () { return network.nodes; }, |
| 1121 | tickStuff: tickStuff, |
| 1122 | nodeLock: function (b) { |
| 1123 | var old = nodeLock; |
| 1124 | nodeLock = b; |
| 1125 | return old; |
| 1126 | }, |
| 1127 | opacifyMap: uplink.opacifyMap, |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 1128 | inLayer: fltr.inLayer, |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1129 | calcLinkPos: calcPosition, |
| 1130 | applyNumLinkLabels: function () { |
| 1131 | td3.applyNumLinkLabels(linkNums, numLinkLblsG); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1132 | }, |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1133 | }; |
| 1134 | } |
| 1135 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1136 | function mkFilterApi() { |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 1137 | return { |
| 1138 | node: function () { return node; }, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1139 | link: function () { return link; }, |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 1140 | }; |
| 1141 | } |
| 1142 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 1143 | function mkLinkApi(svg, uplink) { |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1144 | return { |
| 1145 | svg: svg, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1146 | zoomer: uplink.zoomer(), |
| 1147 | network: network, |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 1148 | portLabelG: function () { return portLabelG; }, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1149 | showHosts: function () { return showHosts; }, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1150 | }; |
| 1151 | } |
| 1152 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 1153 | function updateLinksAndNodes() { |
| 1154 | updateLinks(); |
| 1155 | updateNodes(); |
| 1156 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 1157 | |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 1158 | // invoked after the localization bundle has been received from the server |
| 1159 | function setLionBundle(bundle) { |
| 1160 | topoLion = bundle; |
| 1161 | td3.setLionBundle(bundle); |
| 1162 | fltr.setLionBundle(bundle); |
| 1163 | tls.setLionBundle(bundle); |
Simon Hunt | 1603c69 | 2017-08-10 19:53:35 -0700 | [diff] [blame] | 1164 | tos.setLionBundle(bundle); |
| 1165 | tov.setLionBundle(bundle); |
Simon Hunt | caed041 | 2017-08-12 13:49:17 -0700 | [diff] [blame] | 1166 | tss.setLionBundle(bundle); |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1169 | angular.module('ovTopo') |
| 1170 | .factory('TopoForceService', |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1171 | ['$log', '$timeout', 'FnService', 'SvgUtilService', |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 1172 | 'ThemeService', 'FlashService', 'WebSocketService', |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1173 | 'TopoOverlayService', 'TopoInstService', 'TopoModelService', |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1174 | 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService', |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1175 | 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService', |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1176 | 'TopoProtectedIntentsService', |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1177 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1178 | function (_$log_, _$timeout_, _fs_, _sus_, _ts_, _flash_, _wss_, _tov_, |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1179 | _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_, _tpis_) { |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1180 | $log = _$log_; |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 1181 | $timeout = _$timeout_; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1182 | fs = _fs_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1183 | sus = _sus_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1184 | ts = _ts_; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 1185 | flash = _flash_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 1186 | wss = _wss_; |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1187 | tov = _tov_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1188 | tis = _tis_; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1189 | tms = _tms_; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1190 | td3 = _td3_; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1191 | tss = _tss_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1192 | tts = _tts_; |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1193 | tos = _tos_; |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 1194 | fltr = _fltr_; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1195 | tls = _tls_; |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1196 | tpis = _tpis_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1197 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 1198 | ts.addListener(updateLinksAndNodes); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 1199 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1200 | // forceG is the SVG group to display the force layout in |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 1201 | // uplink is the api from the main topo source file |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1202 | // dim is the initial dimensions of the SVG as [w,h] |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1203 | // opts are, well, optional :) |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1204 | function initForce(_svg_, forceG, _uplink_, _dim_, opts) { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1205 | uplink = _uplink_; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1206 | dim = _dim_; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1207 | svg = _svg_; |
| 1208 | |
| 1209 | lu = network.lookup; |
| 1210 | rlk = network.revLinkToKey; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1211 | |
| 1212 | $log.debug('initForce().. dim = ' + dim); |
| 1213 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1214 | tov.setApi(mkOverlayApi(), tss); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 1215 | tms.initModel(mkModelApi(uplink), dim); |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 1216 | td3.initD3(mkD3Api(), uplink.zoomer()); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1217 | tss.initSelect(mkSelectApi()); |
| 1218 | tts.initTraffic(mkTrafficApi()); |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1219 | tpis.initProtectedIntents(mkTrafficApi()); |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1220 | tos.initOblique(mkObliqueApi(uplink, fltr)); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1221 | fltr.initFilter(mkFilterApi()); |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 1222 | tls.initLink(mkLinkApi(svg, uplink), td3); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 1223 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1224 | settings = angular.extend({}, defaultSettings, opts); |
| 1225 | |
| 1226 | linkG = forceG.append('g').attr('id', 'topo-links'); |
| 1227 | linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels'); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1228 | numLinkLblsG = forceG.append('g').attr('id', 'topo-numLinkLabels'); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1229 | nodeG = forceG.append('g').attr('id', 'topo-nodes'); |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 1230 | portLabelG = forceG.append('g').attr('id', 'topo-portLabels'); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1231 | |
| 1232 | link = linkG.selectAll('.link'); |
| 1233 | linkLabel = linkLabelG.selectAll('.linkLabel'); |
| 1234 | node = nodeG.selectAll('.node'); |
| 1235 | |
| 1236 | force = d3.layout.force() |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1237 | .size(dim) |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1238 | .nodes(network.nodes) |
| 1239 | .links(network.links) |
| 1240 | .gravity(settings.gravity) |
| 1241 | .friction(settings.friction) |
| 1242 | .charge(settings.charge._def_) |
| 1243 | .linkDistance(settings.linkDistance._def_) |
| 1244 | .linkStrength(settings.linkStrength._def_) |
| 1245 | .on('tick', tick); |
| 1246 | |
| 1247 | drag = sus.createDragBehavior(force, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1248 | tss.selectObject, atDragEnd, dragEnabled, clickEnabled); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1249 | } |
| 1250 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1251 | function newDim(_dim_) { |
| 1252 | dim = _dim_; |
| 1253 | force.size(dim); |
| 1254 | tms.newDim(dim); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1257 | function destroyForce() { |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1258 | force.stop(); |
| 1259 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1260 | tls.destroyLink(); |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1261 | tos.destroyOblique(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1262 | tts.destroyTraffic(); |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1263 | tpis.destroyProtectedIntents(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1264 | tss.destroySelect(); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1265 | td3.destroyD3(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1266 | tms.destroyModel(); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1267 | // note: no need to destroy overlay service |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 1268 | ts.removeListener(updateLinksAndNodes); |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1269 | |
| 1270 | // clean up the DOM |
| 1271 | svg.selectAll('g').remove(); |
| 1272 | svg.selectAll('defs').remove(); |
| 1273 | |
| 1274 | // clean up internal state |
| 1275 | network.nodes = []; |
| 1276 | network.links = []; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1277 | network.linksByDevice = {}; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1278 | network.lookup = {}; |
| 1279 | network.revLinkToKey = {}; |
| 1280 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1281 | linkNums = []; |
| 1282 | |
| 1283 | linkG = linkLabelG = numLinkLblsG = nodeG = portLabelG = null; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1284 | link = linkLabel = node = null; |
| 1285 | force = drag = null; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1286 | |
| 1287 | // clean up $timeout promises |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 1288 | if (fTimer) { |
| 1289 | $timeout.cancel(fTimer); |
| 1290 | } |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1291 | if (fNodesTimer) { |
| 1292 | $timeout.cancel(fNodesTimer); |
| 1293 | } |
| 1294 | if (fLinksTimer) { |
| 1295 | $timeout.cancel(fLinksTimer); |
| 1296 | } |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1299 | return { |
| 1300 | initForce: initForce, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1301 | newDim: newDim, |
| 1302 | destroyForce: destroyForce, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1303 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1304 | updateDeviceColors: td3.updateDeviceColors, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 1305 | toggleHosts: toggleHosts, |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 1306 | togglePorts: tls.togglePorts, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 1307 | toggleOffline: toggleOffline, |
| 1308 | cycleDeviceLabels: cycleDeviceLabels, |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 1309 | cycleHostLabels: cycleHostLabels, |
Thomas Vachuska | a4eac6a | 2021-03-01 15:11:59 -0800 | [diff] [blame^] | 1310 | cycleLinkLabels: cycleLinkLabels, |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 1311 | unpin: unpin, |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 1312 | showMastership: showMastership, |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 1313 | showBadLinks: showBadLinks, |
Thomas Vachuska | c8c8f46 | 2021-03-01 11:22:56 -0800 | [diff] [blame] | 1314 | adjustNodeScale: adjustNodeScale, |
| 1315 | |
| 1316 | toggleHostTextSize: toggleHostTextSize, |
| 1317 | toggleHostIconSize: toggleHostIconSize, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1318 | |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 1319 | resetAllLocations: resetAllLocations, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1320 | addDevice: addDevice, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1321 | updateDevice: updateDevice, |
| 1322 | removeDevice: removeDevice, |
| 1323 | addHost: addHost, |
| 1324 | updateHost: updateHost, |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 1325 | moveHost: moveHost, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1326 | removeHost: removeHost, |
| 1327 | addLink: addLink, |
| 1328 | updateLink: updateLink, |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 1329 | removeLink: removeLink, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1330 | topoStartDone: topoStartDone, |
Simon Hunt | e2d9dc7 | 2017-08-10 15:21:04 -0700 | [diff] [blame] | 1331 | |
| 1332 | setLionBundle: setLionBundle, |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1333 | }; |
| 1334 | }]); |
| 1335 | }()); |