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 | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 26 | var $log, $timeout, fs, sus, is, ts, flash, wss, |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 27 | tis, tms, td3, tss, tts, tos, fltr, tls, |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 28 | icfg, uplink, svg; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 29 | |
| 30 | // configuration |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 31 | var linkConfig = { |
| 32 | light: { |
| 33 | baseColor: '#666', |
| 34 | inColor: '#66f', |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 35 | outColor: '#f00' |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 36 | }, |
| 37 | dark: { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 38 | baseColor: '#aaa', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 39 | inColor: '#66f', |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 40 | outColor: '#f66' |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 41 | }, |
| 42 | inWidth: 12, |
| 43 | outWidth: 10 |
| 44 | }; |
| 45 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 46 | // internal state |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 47 | var settings, // merged default settings and options |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 48 | force, // force layout object |
| 49 | drag, // drag behavior handler |
| 50 | network = { |
| 51 | nodes: [], |
| 52 | links: [], |
| 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 | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 61 | dim; // the dimensions of the force layout [w,h] |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 62 | |
| 63 | // SVG elements; |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 64 | var linkG, linkLabelG, portLabelG, nodeG; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 65 | |
| 66 | // D3 selections; |
| 67 | var link, linkLabel, node; |
| 68 | |
| 69 | // default settings for force layout |
| 70 | var defaultSettings = { |
| 71 | gravity: 0.4, |
| 72 | friction: 0.7, |
| 73 | charge: { |
| 74 | // note: key is node.class |
| 75 | device: -8000, |
| 76 | host: -5000, |
| 77 | _def_: -12000 |
| 78 | }, |
| 79 | linkDistance: { |
| 80 | // note: key is link.type |
| 81 | direct: 100, |
| 82 | optical: 120, |
| 83 | hostLink: 3, |
| 84 | _def_: 50 |
| 85 | }, |
| 86 | linkStrength: { |
| 87 | // note: key is link.type |
| 88 | // range: {0.0 ... 1.0} |
| 89 | //direct: 1.0, |
| 90 | //optical: 1.0, |
| 91 | //hostLink: 1.0, |
| 92 | _def_: 1.0 |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 97 | // ========================== |
| 98 | // === EVENT HANDLERS |
| 99 | |
| 100 | function addDevice(data) { |
| 101 | var id = data.id, |
| 102 | d; |
| 103 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 104 | uplink.showNoDevs(false); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 105 | |
| 106 | // although this is an add device event, if we already have the |
| 107 | // device, treat it as an update instead.. |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 108 | if (lu[id]) { |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 109 | updateDevice(data); |
| 110 | return; |
| 111 | } |
| 112 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 113 | d = tms.createDeviceNode(data); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 114 | network.nodes.push(d); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 115 | lu[id] = d; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 116 | updateNodes(); |
| 117 | fStart(); |
| 118 | } |
| 119 | |
| 120 | function updateDevice(data) { |
| 121 | var id = data.id, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 122 | d = lu[id], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 123 | wasOnline; |
| 124 | |
| 125 | if (d) { |
| 126 | wasOnline = d.online; |
| 127 | angular.extend(d, data); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 128 | if (tms.positionNode(d, true)) { |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 129 | sendUpdateMeta(d); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 130 | } |
| 131 | updateNodes(); |
| 132 | if (wasOnline !== d.online) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 133 | tms.findAttachedLinks(d.id).forEach(restyleLinkElement); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 134 | updateOfflineVisibility(d); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 135 | } |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 139 | function removeDevice(data) { |
| 140 | var id = data.id, |
| 141 | d = lu[id]; |
| 142 | if (d) { |
| 143 | removeDeviceElement(d); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | function addHost(data) { |
| 148 | var id = data.id, |
| 149 | d, lnk; |
| 150 | |
| 151 | // although this is an add host event, if we already have the |
| 152 | // host, treat it as an update instead.. |
| 153 | if (lu[id]) { |
| 154 | updateHost(data); |
| 155 | return; |
| 156 | } |
| 157 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 158 | d = tms.createHostNode(data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 159 | network.nodes.push(d); |
| 160 | lu[id] = d; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 161 | updateNodes(); |
| 162 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 163 | lnk = tms.createHostLink(data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 164 | if (lnk) { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 165 | d.linkData = lnk; // cache ref on its host |
| 166 | network.links.push(lnk); |
| 167 | lu[d.ingress] = lnk; |
| 168 | lu[d.egress] = lnk; |
| 169 | updateLinks(); |
| 170 | } |
| 171 | |
| 172 | fStart(); |
| 173 | } |
| 174 | |
| 175 | function updateHost(data) { |
| 176 | var id = data.id, |
| 177 | d = lu[id]; |
| 178 | if (d) { |
| 179 | angular.extend(d, data); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 180 | if (tms.positionNode(d, true)) { |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 181 | sendUpdateMeta(d); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 182 | } |
| 183 | updateNodes(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | function removeHost(data) { |
| 188 | var id = data.id, |
| 189 | d = lu[id]; |
| 190 | if (d) { |
| 191 | removeHostElement(d, true); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | function addLink(data) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 196 | var result = tms.findLink(data, 'add'), |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 197 | bad = result.badLogic, |
| 198 | d = result.ldata; |
| 199 | |
| 200 | if (bad) { |
| 201 | //logicError(bad + ': ' + link.id); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (d) { |
| 206 | // we already have a backing store link for src/dst nodes |
| 207 | addLinkUpdate(d, data); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | // no backing store link yet |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 212 | d = tms.createLink(data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 213 | if (d) { |
| 214 | network.links.push(d); |
| 215 | lu[d.key] = d; |
| 216 | updateLinks(); |
| 217 | fStart(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | function updateLink(data) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 222 | var result = tms.findLink(data, 'update'), |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 223 | bad = result.badLogic; |
| 224 | if (bad) { |
| 225 | //logicError(bad + ': ' + link.id); |
| 226 | return; |
| 227 | } |
| 228 | result.updateWith(link); |
| 229 | } |
| 230 | |
| 231 | function removeLink(data) { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 232 | var result = tms.findLink(data, 'remove'); |
| 233 | |
| 234 | if (!result.badLogic) { |
| 235 | result.removeRawLink(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 236 | } |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // ======================== |
| 240 | |
| 241 | function addLinkUpdate(ldata, link) { |
| 242 | // add link event, but we already have the reverse link installed |
| 243 | ldata.fromTarget = link; |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 244 | rlk[link.id] = ldata.key; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 245 | restyleLinkElement(ldata); |
| 246 | } |
| 247 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 248 | |
| 249 | var widthRatio = 1.4, |
| 250 | linkScale = d3.scale.linear() |
| 251 | .domain([1, 12]) |
| 252 | .range([widthRatio, 12 * widthRatio]) |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 253 | .clamp(true), |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 254 | allLinkTypes = 'direct indirect optical tunnel'; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 255 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 256 | function restyleLinkElement(ldata, immediate) { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 257 | // this fn's job is to look at raw links and decide what svg classes |
| 258 | // need to be applied to the line element in the DOM |
| 259 | var th = ts.theme(), |
| 260 | el = ldata.el, |
| 261 | type = ldata.type(), |
| 262 | lw = ldata.linkWidth(), |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 263 | online = ldata.online(), |
| 264 | delay = immediate ? 0 : 1000; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 265 | |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 266 | // TODO: understand why el is sometimes undefined on addLink events... |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 267 | el.classed('link', true); |
| 268 | el.classed('inactive', !online); |
| 269 | el.classed(allLinkTypes, false); |
| 270 | if (type) { |
| 271 | el.classed(type, true); |
| 272 | } |
| 273 | el.transition() |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 274 | .duration(delay) |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 275 | .attr('stroke-width', linkScale(lw)) |
| 276 | .attr('stroke', linkConfig[th].baseColor); |
| 277 | } |
| 278 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 279 | function removeLinkElement(d) { |
| 280 | var idx = fs.find(d.key, network.links, 'key'), |
| 281 | removed; |
| 282 | if (idx >=0) { |
| 283 | // remove from links array |
| 284 | removed = network.links.splice(idx, 1); |
| 285 | // remove from lookup cache |
| 286 | delete lu[removed[0].key]; |
| 287 | updateLinks(); |
| 288 | fResume(); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | function removeHostElement(d, upd) { |
| 293 | // first, remove associated hostLink... |
| 294 | removeLinkElement(d.linkData); |
| 295 | |
| 296 | // remove hostLink bindings |
| 297 | delete lu[d.ingress]; |
| 298 | delete lu[d.egress]; |
| 299 | |
| 300 | // remove from lookup cache |
| 301 | delete lu[d.id]; |
| 302 | // remove from nodes array |
| 303 | var idx = fs.find(d.id, network.nodes); |
| 304 | network.nodes.splice(idx, 1); |
| 305 | |
| 306 | // remove from SVG |
| 307 | // NOTE: upd is false if we were called from removeDeviceElement() |
| 308 | if (upd) { |
| 309 | updateNodes(); |
| 310 | fResume(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | function removeDeviceElement(d) { |
| 315 | var id = d.id; |
| 316 | // first, remove associated hosts and links.. |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 317 | tms.findAttachedHosts(id).forEach(removeHostElement); |
| 318 | tms.findAttachedLinks(id).forEach(removeLinkElement); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 319 | |
| 320 | // remove from lookup cache |
| 321 | delete lu[id]; |
| 322 | // remove from nodes array |
| 323 | var idx = fs.find(id, network.nodes); |
| 324 | network.nodes.splice(idx, 1); |
| 325 | |
| 326 | if (!network.nodes.length) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 327 | uplink.showNoDevs(true); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // remove from SVG |
| 331 | updateNodes(); |
| 332 | fResume(); |
| 333 | } |
| 334 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 335 | function updateHostVisibility() { |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 336 | sus.visible(nodeG.selectAll('.host'), showHosts); |
| 337 | sus.visible(linkG.selectAll('.hostLink'), showHosts); |
Simon Hunt | 8eb4d3a | 2015-02-23 18:23:29 -0800 | [diff] [blame] | 338 | sus.visible(linkLabelG.selectAll('.hostLinkLabel'), showHosts); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | function updateOfflineVisibility(dev) { |
| 342 | function updDev(d, show) { |
Simon Hunt | 8eb4d3a | 2015-02-23 18:23:29 -0800 | [diff] [blame] | 343 | var b; |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 344 | sus.visible(d.el, show); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 345 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 346 | tms.findAttachedLinks(d.id).forEach(function (link) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 347 | b = show && ((link.type() !== 'hostLink') || showHosts); |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 348 | sus.visible(link.el, b); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 349 | }); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 350 | tms.findAttachedHosts(d.id).forEach(function (host) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 351 | b = show && showHosts; |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 352 | sus.visible(host.el, b); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 353 | }); |
| 354 | } |
| 355 | |
| 356 | if (dev) { |
| 357 | // updating a specific device that just toggled off/on-line |
| 358 | updDev(dev, dev.online || showOffline); |
| 359 | } else { |
| 360 | // updating all offline devices |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 361 | tms.findDevices(true).forEach(function (d) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 362 | updDev(d, showOffline); |
| 363 | }); |
| 364 | } |
| 365 | } |
| 366 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 367 | |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 368 | function sendUpdateMeta(d, clearPos) { |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 369 | var metaUi = {}, |
| 370 | ll; |
| 371 | |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 372 | // if we are not clearing the position data (unpinning), |
| 373 | // attach the x, y, longitude, latitude... |
| 374 | if (!clearPos) { |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 375 | ll = tms.lngLatFromCoord([d.x, d.y]); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 376 | 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] | 377 | } |
| 378 | d.metaUi = metaUi; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 379 | wss.sendEvent('updateMeta', { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 380 | id: d.id, |
| 381 | 'class': d.class, |
| 382 | memento: metaUi |
| 383 | }); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 386 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 387 | function mkSvgClass(d) { |
| 388 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 389 | } |
| 390 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 391 | function vis(b) { |
| 392 | return b ? 'visible' : 'hidden'; |
| 393 | } |
| 394 | |
| 395 | function toggleHosts() { |
| 396 | showHosts = !showHosts; |
| 397 | updateHostVisibility(); |
| 398 | flash.flash('Hosts ' + vis(showHosts)); |
| 399 | } |
| 400 | |
| 401 | function toggleOffline() { |
| 402 | showOffline = !showOffline; |
| 403 | updateOfflineVisibility(); |
| 404 | flash.flash('Offline devices ' + vis(showOffline)); |
| 405 | } |
| 406 | |
| 407 | function cycleDeviceLabels() { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 408 | td3.incDevLabIndex(); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 409 | tms.findDevices().forEach(function (d) { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 410 | td3.updateDeviceLabel(d); |
Simon Hunt | 1c36711 | 2015-02-05 18:02:46 -0800 | [diff] [blame] | 411 | }); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 414 | function unpin() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 415 | var hov = tss.hovered(); |
| 416 | if (hov) { |
| 417 | sendUpdateMeta(hov, true); |
| 418 | hov.fixed = false; |
| 419 | hov.el.classed('fixed', false); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 420 | fResume(); |
| 421 | } |
| 422 | } |
| 423 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 424 | function showMastership(masterId) { |
| 425 | if (!masterId) { |
| 426 | restoreLayerState(); |
| 427 | } else { |
| 428 | showMastershipFor(masterId); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | function restoreLayerState() { |
| 433 | // NOTE: this level of indirection required, for when we have |
| 434 | // the layer filter functionality re-implemented |
| 435 | suppressLayers(false); |
| 436 | } |
| 437 | |
| 438 | function showMastershipFor(id) { |
| 439 | suppressLayers(true); |
| 440 | node.each(function (n) { |
| 441 | if (n.master === id) { |
| 442 | n.el.classed('suppressed', false); |
| 443 | } |
| 444 | }); |
| 445 | } |
| 446 | |
| 447 | function suppressLayers(b) { |
| 448 | node.classed('suppressed', b); |
| 449 | link.classed('suppressed', b); |
| 450 | // d3.selectAll('svg .port').classed('inactive', b); |
| 451 | // d3.selectAll('svg .portText').classed('inactive', b); |
| 452 | } |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 453 | |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 454 | function showBadLinks() { |
| 455 | var badLinks = tms.findBadLinks(); |
| 456 | flash.flash('Bad Links: ' + badLinks.length); |
| 457 | $log.debug('Bad Link List (' + badLinks.length + '):'); |
| 458 | badLinks.forEach(function (d) { |
| 459 | $log.debug('bad link: (' + d.bad + ') ' + d.key, d); |
| 460 | if (d.el) { |
| 461 | d.el.attr('stroke-width', linkScale(2.8)) |
| 462 | .attr('stroke', 'red'); |
| 463 | } |
| 464 | }); |
| 465 | // back to normal after 2 seconds... |
| 466 | $timeout(updateLinks, 2000); |
| 467 | } |
| 468 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 469 | // ========================================== |
| 470 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 471 | function updateNodes() { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 472 | // select all the nodes in the layout: |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 473 | node = nodeG.selectAll('.node') |
| 474 | .data(network.nodes, function (d) { return d.id; }); |
| 475 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 476 | // operate on existing nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 477 | node.filter('.device').each(td3.deviceExisting); |
| 478 | node.filter('.host').each(td3.hostExisting); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 479 | |
| 480 | // operate on entering nodes: |
| 481 | var entering = node.enter() |
| 482 | .append('g') |
| 483 | .attr({ |
| 484 | id: function (d) { return sus.safeId(d.id); }, |
| 485 | class: mkSvgClass, |
| 486 | transform: function (d) { return sus.translate(d.x, d.y); }, |
| 487 | opacity: 0 |
| 488 | }) |
| 489 | .call(drag) |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 490 | .on('mouseover', tss.nodeMouseOver) |
| 491 | .on('mouseout', tss.nodeMouseOut) |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 492 | .transition() |
| 493 | .attr('opacity', 1); |
| 494 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 495 | // augment entering nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 496 | entering.filter('.device').each(td3.deviceEnter); |
| 497 | entering.filter('.host').each(td3.hostEnter); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 498 | |
Simon Hunt | 5105659 | 2015-02-03 21:48:07 -0800 | [diff] [blame] | 499 | // operate on both existing and new nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 500 | td3.updateDeviceColors(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 501 | |
| 502 | // operate on exiting nodes: |
| 503 | // Note that the node is removed after 2 seconds. |
| 504 | // Sub element animations should be shorter than 2 seconds. |
| 505 | var exiting = node.exit() |
| 506 | .transition() |
| 507 | .duration(2000) |
| 508 | .style('opacity', 0) |
| 509 | .remove(); |
| 510 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 511 | // exiting node specifics: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 512 | exiting.filter('.host').each(td3.hostExit); |
| 513 | exiting.filter('.device').each(td3.deviceExit); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 514 | |
Simon Hunt | 5105659 | 2015-02-03 21:48:07 -0800 | [diff] [blame] | 515 | // finally, resume the force layout |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 516 | fResume(); |
| 517 | } |
| 518 | |
Simon Hunt | 5105659 | 2015-02-03 21:48:07 -0800 | [diff] [blame] | 519 | // ========================== |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 520 | |
| 521 | function updateLinks() { |
| 522 | var th = ts.theme(); |
| 523 | |
| 524 | link = linkG.selectAll('.link') |
| 525 | .data(network.links, function (d) { return d.key; }); |
| 526 | |
| 527 | // operate on existing links: |
Simon Hunt | d526412 | 2015-02-25 10:17:43 -0800 | [diff] [blame] | 528 | link.each(function (d) { |
| 529 | // this is supposed to be an existing link, but we have observed |
| 530 | // occasions (where links are deleted and added rapidly?) where |
| 531 | // the DOM element has not been defined. So protect against that... |
| 532 | if (d.el) { |
| 533 | restyleLinkElement(d, true); |
| 534 | } |
| 535 | }); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 536 | |
| 537 | // operate on entering links: |
| 538 | var entering = link.enter() |
| 539 | .append('line') |
| 540 | .attr({ |
Simon Hunt | d526412 | 2015-02-25 10:17:43 -0800 | [diff] [blame] | 541 | x1: function (d) { return d.source.x; }, |
| 542 | y1: function (d) { return d.source.y; }, |
| 543 | x2: function (d) { return d.target.x; }, |
| 544 | y2: function (d) { return d.target.y; }, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 545 | stroke: linkConfig[th].inColor, |
| 546 | 'stroke-width': linkConfig.inWidth |
| 547 | }); |
| 548 | |
| 549 | // augment links |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 550 | entering.each(td3.linkEntering); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 551 | |
| 552 | // operate on both existing and new links: |
| 553 | //link.each(...) |
| 554 | |
| 555 | // apply or remove labels |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 556 | td3.applyLinkLabels(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 557 | |
| 558 | // operate on exiting links: |
| 559 | link.exit() |
| 560 | .attr('stroke-dasharray', '3 3') |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 561 | .attr('stroke', linkConfig[th].outColor) |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 562 | .style('opacity', 0.5) |
| 563 | .transition() |
| 564 | .duration(1500) |
| 565 | .attr({ |
| 566 | 'stroke-dasharray': '3 12', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 567 | 'stroke-width': linkConfig.outWidth |
| 568 | }) |
| 569 | .style('opacity', 0.0) |
| 570 | .remove(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 573 | |
| 574 | // ========================== |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 575 | // force layout tick function |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 576 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 577 | function fResume() { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 578 | if (!tos.isOblique()) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 579 | force.resume(); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | function fStart() { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 584 | if (!tos.isOblique()) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 585 | force.start(); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | var tickStuff = { |
| 590 | nodeAttr: { |
| 591 | transform: function (d) { return sus.translate(d.x, d.y); } |
| 592 | }, |
| 593 | linkAttr: { |
| 594 | x1: function (d) { return d.source.x; }, |
| 595 | y1: function (d) { return d.source.y; }, |
| 596 | x2: function (d) { return d.target.x; }, |
| 597 | y2: function (d) { return d.target.y; } |
| 598 | }, |
| 599 | linkLabelAttr: { |
| 600 | transform: function (d) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 601 | var lnk = tms.findLinkById(d.key); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 602 | if (lnk) { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 603 | return td3.transformLabel({ |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 604 | x1: lnk.source.x, |
| 605 | y1: lnk.source.y, |
| 606 | x2: lnk.target.x, |
| 607 | y2: lnk.target.y |
| 608 | }); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | }; |
| 613 | |
| 614 | function tick() { |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 615 | // guard against null (which can happen when our view pages out)... |
| 616 | if (node) node.attr(tickStuff.nodeAttr); |
| 617 | if (link) link.attr(tickStuff.linkAttr); |
| 618 | if (linkLabel) linkLabel.attr(tickStuff.linkLabelAttr); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 622 | // ========================== |
| 623 | // === MOUSE GESTURE HANDLERS |
| 624 | |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 625 | function zoomingOrPanning(ev) { |
| 626 | return ev.metaKey || ev.altKey; |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | function atDragEnd(d) { |
| 630 | // once we've finished moving, pin the node in position |
| 631 | d.fixed = true; |
| 632 | d3.select(this).classed('fixed', true); |
| 633 | sendUpdateMeta(d); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 634 | tss.clickConsumed(true); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | // predicate that indicates when dragging is active |
| 638 | function dragEnabled() { |
| 639 | var ev = d3.event.sourceEvent; |
| 640 | // nodeLock means we aren't allowing nodes to be dragged... |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 641 | return !nodeLock && !zoomingOrPanning(ev); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | // predicate that indicates when clicking is active |
| 645 | function clickEnabled() { |
| 646 | return true; |
| 647 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 648 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 649 | // ========================== |
| 650 | // function entry points for traffic module |
| 651 | |
| 652 | var allTrafficClasses = 'primary secondary animated optical'; |
| 653 | |
| 654 | function clearLinkTrafficStyle() { |
| 655 | link.style('stroke-width', null) |
| 656 | .classed(allTrafficClasses, false); |
| 657 | } |
| 658 | |
| 659 | function removeLinkLabels() { |
| 660 | network.links.forEach(function (d) { |
| 661 | d.label = ''; |
| 662 | }); |
| 663 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 664 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 665 | function updateLinkLabelModel() { |
| 666 | // create the backing data for showing labels.. |
| 667 | var data = []; |
| 668 | link.each(function (d) { |
| 669 | if (d.label) { |
| 670 | data.push({ |
| 671 | id: 'lab-' + d.key, |
| 672 | key: d.key, |
| 673 | label: d.label, |
| 674 | ldata: d |
| 675 | }); |
| 676 | } |
| 677 | }); |
| 678 | |
| 679 | linkLabel = linkLabelG.selectAll('.linkLabel') |
| 680 | .data(data, function (d) { return d.id; }); |
| 681 | } |
| 682 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 683 | // ========================== |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 684 | // Module definition |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 685 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 686 | function mkModelApi(uplink) { |
| 687 | return { |
| 688 | projection: uplink.projection, |
| 689 | network: network, |
| 690 | restyleLinkElement: restyleLinkElement, |
| 691 | removeLinkElement: removeLinkElement |
| 692 | }; |
| 693 | } |
| 694 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 695 | function mkD3Api(uplink) { |
| 696 | return { |
| 697 | node: function () { return node; }, |
| 698 | link: function () { return link; }, |
| 699 | linkLabel: function () { return linkLabel; }, |
| 700 | instVisible: function () { return tis.isVisible(); }, |
| 701 | posNode: tms.positionNode, |
| 702 | showHosts: function () { return showHosts; }, |
| 703 | restyleLinkElement: restyleLinkElement, |
| 704 | updateLinkLabelModel: updateLinkLabelModel |
| 705 | } |
| 706 | } |
| 707 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 708 | function mkSelectApi(uplink) { |
| 709 | return { |
| 710 | node: function () { return node; }, |
| 711 | zoomingOrPanning: zoomingOrPanning, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 712 | updateDeviceColors: td3.updateDeviceColors, |
| 713 | deselectLink: tls.deselectLink |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 714 | }; |
| 715 | } |
| 716 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 717 | function mkTrafficApi(uplink) { |
| 718 | return { |
| 719 | clearLinkTrafficStyle: clearLinkTrafficStyle, |
| 720 | removeLinkLabels: removeLinkLabels, |
| 721 | updateLinks: updateLinks, |
| 722 | findLinkById: tms.findLinkById, |
| 723 | hovered: tss.hovered, |
| 724 | validateSelectionContext: tss.validateSelectionContext, |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 725 | selectOrder: tss.selectOrder |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 729 | function mkObliqueApi(uplink, fltr) { |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 730 | return { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 731 | force: function() { return force; }, |
| 732 | zoomLayer: uplink.zoomLayer, |
| 733 | nodeGBBox: function() { return nodeG.node().getBBox(); }, |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 734 | node: function () { return node; }, |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 735 | link: function () { return link; }, |
| 736 | linkLabel: function () { return linkLabel; }, |
| 737 | nodes: function () { return network.nodes; }, |
| 738 | tickStuff: tickStuff, |
| 739 | nodeLock: function (b) { |
| 740 | var old = nodeLock; |
| 741 | nodeLock = b; |
| 742 | return old; |
| 743 | }, |
| 744 | opacifyMap: uplink.opacifyMap, |
| 745 | inLayer: fltr.inLayer |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 746 | }; |
| 747 | } |
| 748 | |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 749 | function mkFilterApi(uplink) { |
| 750 | return { |
| 751 | node: function () { return node; }, |
| 752 | link: function () { return link; } |
| 753 | }; |
| 754 | } |
| 755 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 756 | function mkLinkApi(svg, uplink) { |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 757 | return { |
| 758 | svg: svg, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 759 | zoomer: uplink.zoomer(), |
| 760 | network: network, |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 761 | portLabelG: function () { return portLabelG; }, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 762 | showHosts: function () { return showHosts; } |
| 763 | }; |
| 764 | } |
| 765 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 766 | angular.module('ovTopo') |
| 767 | .factory('TopoForceService', |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 768 | ['$log', '$timeout', 'FnService', 'SvgUtilService', 'IconService', |
| 769 | 'ThemeService', 'FlashService', 'WebSocketService', |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 770 | 'TopoInstService', 'TopoModelService', |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 771 | 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService', |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 772 | 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService', |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 773 | |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 774 | function (_$log_, _$timeout_, _fs_, _sus_, _is_, _ts_, _flash_, _wss_, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 775 | _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_) { |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 776 | $log = _$log_; |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 777 | $timeout = _$timeout_; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 778 | fs = _fs_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 779 | sus = _sus_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 780 | is = _is_; |
| 781 | ts = _ts_; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 782 | flash = _flash_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 783 | wss = _wss_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 784 | tis = _tis_; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 785 | tms = _tms_; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 786 | td3 = _td3_; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 787 | tss = _tss_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 788 | tts = _tts_; |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 789 | tos = _tos_; |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 790 | fltr = _fltr_; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 791 | tls = _tls_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 792 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 793 | icfg = is.iconConfig(); |
| 794 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 795 | var themeListener = ts.addListener(function () { |
| 796 | updateLinks(); |
| 797 | updateNodes(); |
| 798 | }); |
| 799 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 800 | // forceG is the SVG group to display the force layout in |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 801 | // uplink is the api from the main topo source file |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 802 | // dim is the initial dimensions of the SVG as [w,h] |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 803 | // opts are, well, optional :) |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 804 | function initForce(_svg_, forceG, _uplink_, _dim_, opts) { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 805 | uplink = _uplink_; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 806 | dim = _dim_; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 807 | svg = _svg_; |
| 808 | |
| 809 | lu = network.lookup; |
| 810 | rlk = network.revLinkToKey; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 811 | |
| 812 | $log.debug('initForce().. dim = ' + dim); |
| 813 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 814 | tms.initModel(mkModelApi(uplink), dim); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 815 | td3.initD3(mkD3Api(uplink)); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 816 | tss.initSelect(mkSelectApi(uplink)); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 817 | tts.initTraffic(mkTrafficApi(uplink)); |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 818 | tos.initOblique(mkObliqueApi(uplink, fltr)); |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 819 | fltr.initFilter(mkFilterApi(uplink)); |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 820 | tls.initLink(mkLinkApi(svg, uplink), td3); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 821 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 822 | settings = angular.extend({}, defaultSettings, opts); |
| 823 | |
| 824 | linkG = forceG.append('g').attr('id', 'topo-links'); |
| 825 | linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels'); |
| 826 | nodeG = forceG.append('g').attr('id', 'topo-nodes'); |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 827 | portLabelG = forceG.append('g').attr('id', 'topo-portLabels'); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 828 | |
| 829 | link = linkG.selectAll('.link'); |
| 830 | linkLabel = linkLabelG.selectAll('.linkLabel'); |
| 831 | node = nodeG.selectAll('.node'); |
| 832 | |
| 833 | force = d3.layout.force() |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 834 | .size(dim) |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 835 | .nodes(network.nodes) |
| 836 | .links(network.links) |
| 837 | .gravity(settings.gravity) |
| 838 | .friction(settings.friction) |
| 839 | .charge(settings.charge._def_) |
| 840 | .linkDistance(settings.linkDistance._def_) |
| 841 | .linkStrength(settings.linkStrength._def_) |
| 842 | .on('tick', tick); |
| 843 | |
| 844 | drag = sus.createDragBehavior(force, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 845 | tss.selectObject, atDragEnd, dragEnabled, clickEnabled); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 846 | } |
| 847 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 848 | function newDim(_dim_) { |
| 849 | dim = _dim_; |
| 850 | force.size(dim); |
| 851 | tms.newDim(dim); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 854 | function destroyForce() { |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 855 | force.stop(); |
| 856 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 857 | tls.destroyLink(); |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 858 | tos.destroyOblique(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 859 | tts.destroyTraffic(); |
| 860 | tss.destroySelect(); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 861 | td3.destroyD3(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 862 | tms.destroyModel(); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 863 | ts.removeListener(themeListener); |
| 864 | themeListener = null; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 865 | |
| 866 | // clean up the DOM |
| 867 | svg.selectAll('g').remove(); |
| 868 | svg.selectAll('defs').remove(); |
| 869 | |
| 870 | // clean up internal state |
| 871 | network.nodes = []; |
| 872 | network.links = []; |
| 873 | network.lookup = {}; |
| 874 | network.revLinkToKey = {}; |
| 875 | |
| 876 | linkG = linkLabelG = nodeG = portLabelG = null; |
| 877 | link = linkLabel = node = null; |
| 878 | force = drag = null; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 881 | return { |
| 882 | initForce: initForce, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 883 | newDim: newDim, |
| 884 | destroyForce: destroyForce, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 885 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 886 | updateDeviceColors: td3.updateDeviceColors, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 887 | toggleHosts: toggleHosts, |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 888 | togglePorts: tls.togglePorts, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 889 | toggleOffline: toggleOffline, |
| 890 | cycleDeviceLabels: cycleDeviceLabels, |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 891 | unpin: unpin, |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 892 | showMastership: showMastership, |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 893 | showBadLinks: showBadLinks, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 894 | |
| 895 | addDevice: addDevice, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 896 | updateDevice: updateDevice, |
| 897 | removeDevice: removeDevice, |
| 898 | addHost: addHost, |
| 899 | updateHost: updateHost, |
| 900 | removeHost: removeHost, |
| 901 | addLink: addLink, |
| 902 | updateLink: updateLink, |
| 903 | removeLink: removeLink |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 904 | }; |
| 905 | }]); |
| 906 | }()); |