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