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