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