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