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