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