Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -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 | /* |
| 18 | ONOS GUI -- Topology Link Module. |
| 19 | Functions for highlighting/selecting links |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 26 | var $log, fs, sus, ts, flash, tss, tps; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 27 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 28 | // internal state |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 29 | var api, |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 30 | td3, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 31 | network, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 32 | showPorts = true, // enable port highlighting by default |
| 33 | enhancedLink = null, // the link over which the mouse is hovering |
| 34 | selectedLink = null; // the link which is currently selected |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 35 | |
| 36 | // SVG elements; |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 37 | var svg; |
| 38 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 39 | |
| 40 | // ======== ALGORITHM TO FIND LINK CLOSEST TO MOUSE ======== |
| 41 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 42 | function getLogicalMousePosition(container) { |
| 43 | var m = d3.mouse(container), |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 44 | sc = api.zoomer.scale(), |
| 45 | tr = api.zoomer.translate(), |
| 46 | mx = (m[0] - tr[0]) / sc, |
| 47 | my = (m[1] - tr[1]) / sc; |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 48 | return {x: mx, y: my}; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Simon Hunt | 5aac2fc | 2015-06-09 12:34:07 -0700 | [diff] [blame] | 51 | |
| 52 | function sq(x) { return x * x; } |
| 53 | |
| 54 | function mdist(p, m) { |
| 55 | return Math.sqrt(sq(p.x - m.x) + sq(p.y - m.y)); |
| 56 | } |
| 57 | |
| 58 | function prox(dist) { |
| 59 | return dist / api.zoomer.scale(); |
| 60 | } |
| 61 | |
| 62 | function computeNearestNode(mouse) { |
| 63 | var proximity = prox(30), |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 64 | nearest = null, |
| 65 | minDist; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 66 | |
Simon Hunt | 5aac2fc | 2015-06-09 12:34:07 -0700 | [diff] [blame] | 67 | if (network.nodes.length) { |
| 68 | minDist = proximity * 2; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 69 | |
Simon Hunt | 5aac2fc | 2015-06-09 12:34:07 -0700 | [diff] [blame] | 70 | network.nodes.forEach(function (d) { |
| 71 | var dist; |
| 72 | |
| 73 | if (!api.showHosts() && d.class === 'host') { |
| 74 | return; // skip hidden hosts |
| 75 | } |
| 76 | |
| 77 | dist = mdist({x: d.x, y: d.y}, mouse); |
| 78 | if (dist < minDist && dist < proximity) { |
| 79 | minDist = dist; |
| 80 | nearest = d; |
| 81 | } |
| 82 | }); |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 83 | } |
Simon Hunt | 5aac2fc | 2015-06-09 12:34:07 -0700 | [diff] [blame] | 84 | return nearest; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | function computeNearestLink(mouse) { |
| 89 | var proximity = prox(30), |
| 90 | nearest = null, |
| 91 | minDist; |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 92 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 93 | function pdrop(line, mouse) { |
| 94 | var x1 = line.x1, |
| 95 | y1 = line.y1, |
| 96 | x2 = line.x2, |
| 97 | y2 = line.y2, |
| 98 | x3 = mouse.x, |
| 99 | y3 = mouse.y, |
| 100 | k = ((y2-y1) * (x3-x1) - (x2-x1) * (y3-y1)) / |
| 101 | (sq(y2-y1) + sq(x2-x1)), |
| 102 | x4 = x3 - k * (y2-y1), |
| 103 | y4 = y3 + k * (x2-x1); |
| 104 | return {x:x4, y:y4}; |
| 105 | } |
| 106 | |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame^] | 107 | // FIXME: x and y position calculated here, use link.position |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 108 | function lineSeg(d) { |
| 109 | return { |
| 110 | x1: d.source.x, |
| 111 | y1: d.source.y, |
| 112 | x2: d.target.x, |
| 113 | y2: d.target.y |
| 114 | }; |
| 115 | } |
| 116 | |
| 117 | function lineHit(line, p, m) { |
| 118 | if (p.x < line.x1 && p.x < line.x2) return false; |
| 119 | if (p.x > line.x1 && p.x > line.x2) return false; |
| 120 | if (p.y < line.y1 && p.y < line.y2) return false; |
| 121 | if (p.y > line.y1 && p.y > line.y2) return false; |
Simon Hunt | 5f36108 | 2015-02-25 11:36:38 -0800 | [diff] [blame] | 122 | // line intersects, but are we close enough? |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 123 | return mdist(p, m) <= proximity; |
| 124 | } |
| 125 | |
| 126 | if (network.links.length) { |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 127 | minDist = proximity * 2; |
| 128 | |
| 129 | network.links.forEach(function (d) { |
| 130 | if (!api.showHosts() && d.type() === 'hostLink') { |
| 131 | return; // skip hidden host links |
| 132 | } |
| 133 | |
| 134 | var line = lineSeg(d), |
| 135 | point = pdrop(line, mouse), |
| 136 | hit = lineHit(line, point, mouse), |
| 137 | dist; |
| 138 | |
| 139 | if (hit) { |
| 140 | dist = mdist(point, mouse); |
| 141 | if (dist < minDist) { |
| 142 | minDist = dist; |
| 143 | nearest = d; |
| 144 | } |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 145 | } |
| 146 | }); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 147 | } |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 148 | return nearest; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 151 | function enhanceLink(ldata) { |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 152 | // if the new link is same as old link, do nothing |
| 153 | if (enhancedLink && ldata && enhancedLink.key === ldata.key) return; |
| 154 | |
| 155 | // first, unenhance the currently enhanced link |
| 156 | if (enhancedLink) { |
| 157 | unenhance(enhancedLink); |
| 158 | } |
| 159 | enhancedLink = ldata; |
| 160 | if (enhancedLink) { |
| 161 | enhance(enhancedLink); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | function unenhance(d) { |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 166 | // guard against link element not set |
| 167 | if (d.el) { |
| 168 | d.el.classed('enhanced', false); |
| 169 | } |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 170 | api.portLabelG().selectAll('.portLabel').remove(); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | function enhance(d) { |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 174 | var data = [], |
| 175 | point; |
| 176 | |
| 177 | // guard against link element not set |
| 178 | if (!d.el) return; |
| 179 | |
Simon Hunt | d526412 | 2015-02-25 10:17:43 -0800 | [diff] [blame] | 180 | d.el.classed('enhanced', true); |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 181 | |
Simon Hunt | 8ae474b | 2015-02-25 15:39:14 -0800 | [diff] [blame] | 182 | // Define port label data objects. |
| 183 | // NOTE: src port is absent in the case of host-links. |
| 184 | |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 185 | point = locatePortLabel(d); |
| 186 | angular.extend(point, { |
Simon Hunt | 8ae474b | 2015-02-25 15:39:14 -0800 | [diff] [blame] | 187 | id: 'topo-port-tgt', |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 188 | num: d.tgtPort |
| 189 | }); |
| 190 | data.push(point); |
Simon Hunt | 8ae474b | 2015-02-25 15:39:14 -0800 | [diff] [blame] | 191 | |
| 192 | if (d.srcPort) { |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 193 | point = locatePortLabel(d, 1); |
| 194 | angular.extend(point, { |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 195 | id: 'topo-port-src', |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 196 | num: d.srcPort |
Simon Hunt | 8ae474b | 2015-02-25 15:39:14 -0800 | [diff] [blame] | 197 | }); |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 198 | data.push(point); |
Simon Hunt | 8ae474b | 2015-02-25 15:39:14 -0800 | [diff] [blame] | 199 | } |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 200 | |
| 201 | td3.applyPortLabels(data, api.portLabelG()); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame^] | 204 | // FIXME: x and y position calculated here somewhere |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 205 | function locatePortLabel(link, src) { |
| 206 | var near = src ? 'source' : 'target', |
| 207 | far = src ? 'target' : 'source', |
| 208 | ln = link[near], |
| 209 | lf = link[far], |
| 210 | offset = 32; |
| 211 | |
| 212 | function dist(x, y) { return Math.sqrt(x*x + y*y); } |
| 213 | |
| 214 | var dx = lf.x - ln.x, |
| 215 | dy = lf.y - ln.y, |
| 216 | k = offset / dist(dx, dy); |
| 217 | |
| 218 | return {x: k * dx + ln.x, y: k * dy + ln.y}; |
| 219 | } |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 220 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 221 | |
| 222 | function selectLink(ldata) { |
| 223 | // if the new link is same as old link, do nothing |
| 224 | if (selectedLink && ldata && selectedLink.key === ldata.key) return; |
| 225 | |
| 226 | // make sure no nodes are selected |
| 227 | tss.deselectAll(); |
| 228 | |
| 229 | // first, unenhance the currently enhanced link |
| 230 | if (selectedLink) { |
| 231 | unselLink(selectedLink); |
| 232 | } |
| 233 | selectedLink = ldata; |
| 234 | if (selectedLink) { |
| 235 | selLink(selectedLink); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | function unselLink(d) { |
| 240 | // guard against link element not set |
| 241 | if (d.el) { |
| 242 | d.el.classed('selected', false); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | function selLink(d) { |
| 247 | // guard against link element not set |
| 248 | if (!d.el) return; |
| 249 | |
| 250 | d.el.classed('selected', true); |
| 251 | |
| 252 | tps.displayLink(d); |
| 253 | tps.displaySomething(); |
| 254 | } |
| 255 | |
| 256 | // ====== MOUSE EVENT HANDLERS ====== |
| 257 | |
| 258 | function mouseMoveHandler() { |
| 259 | var mp = getLogicalMousePosition(this), |
| 260 | link = computeNearestLink(mp); |
| 261 | enhanceLink(link); |
| 262 | } |
| 263 | |
| 264 | function mouseClickHandler() { |
Simon Hunt | 5aac2fc | 2015-06-09 12:34:07 -0700 | [diff] [blame] | 265 | var mp, link, node; |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 266 | |
| 267 | if (!tss.clickConsumed()) { |
| 268 | mp = getLogicalMousePosition(this); |
Simon Hunt | 5aac2fc | 2015-06-09 12:34:07 -0700 | [diff] [blame] | 269 | node = computeNearestNode(mp); |
| 270 | if (node) { |
| 271 | $log.debug('found nearest node:', node.labels[1]); |
| 272 | tss.selectObject(node); |
| 273 | } else { |
| 274 | link = computeNearestLink(mp); |
| 275 | selectLink(link); |
| 276 | } |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | // ====================== |
| 282 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 283 | function togglePorts(x) { |
| 284 | var kev = (x === 'keyev'), |
| 285 | on = kev ? !showPorts : !!x, |
| 286 | what = on ? 'Enable' : 'Disable', |
| 287 | handler = on ? mouseMoveHandler : null; |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 288 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 289 | showPorts = on; |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 290 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 291 | if (!on) { |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 292 | enhanceLink(null); |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 293 | } |
| 294 | svg.on('mousemove', handler); |
| 295 | flash.flash(what + ' port highlighting'); |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 296 | return on; |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 299 | function deselectLink() { |
| 300 | if (selectedLink) { |
| 301 | unselLink(selectedLink); |
| 302 | selectedLink = null; |
| 303 | return true; |
| 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 308 | // ========================== |
| 309 | // Module definition |
| 310 | |
| 311 | angular.module('ovTopo') |
| 312 | .factory('TopoLinkService', |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 313 | ['$log', 'FnService', 'SvgUtilService', 'ThemeService', 'FlashService', |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 314 | 'TopoSelectService', 'TopoPanelService', |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 315 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 316 | function (_$log_, _fs_, _sus_, _ts_, _flash_, _tss_, _tps_) { |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 317 | $log = _$log_; |
| 318 | fs = _fs_; |
| 319 | sus = _sus_; |
| 320 | ts = _ts_; |
| 321 | flash = _flash_; |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 322 | tss = _tss_; |
| 323 | tps = _tps_; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 324 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 325 | function initLink(_api_, _td3_) { |
| 326 | api = _api_; |
| 327 | td3 = _td3_; |
| 328 | svg = api.svg; |
| 329 | network = api.network; |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 330 | if (showPorts && !fs.isMobile()) { |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 331 | svg.on('mousemove', mouseMoveHandler); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 332 | } |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 333 | svg.on('click', mouseClickHandler); |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 334 | } |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 335 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 336 | function destroyLink() { |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 337 | // unconditionally remove any event handlers |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 338 | svg.on('mousemove', null); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 339 | svg.on('click', null); |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 340 | } |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 341 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 342 | return { |
| 343 | initLink: initLink, |
| 344 | destroyLink: destroyLink, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 345 | togglePorts: togglePorts, |
| 346 | deselectLink: deselectLink |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 347 | }; |
| 348 | }]); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 349 | }()); |