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