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