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 | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 26 | var $log, fs, sus, ts, flash; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 27 | |
| 28 | var api, |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 29 | td3, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 30 | network, |
| 31 | enhancedLink = null; // the link which the mouse is hovering over |
| 32 | |
| 33 | // SVG elements; |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 34 | var svg; |
| 35 | |
| 36 | // internal state |
| 37 | var showPorts = true; // enable port highlighting by default |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 38 | |
| 39 | |
| 40 | // ======== ALGORITHM TO FIND LINK CLOSEST TO MOUSE ======== |
| 41 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 42 | function mouseMoveHandler() { |
| 43 | var m = d3.mouse(this), |
| 44 | sc = api.zoomer.scale(), |
| 45 | tr = api.zoomer.translate(), |
| 46 | mx = (m[0] - tr[0]) / sc, |
| 47 | my = (m[1] - tr[1]) / sc; |
| 48 | computeNearestLink({x: mx, y: my}); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 51 | function computeNearestLink(mouse) { |
| 52 | var proximity = 30 / api.zoomer.scale(), |
| 53 | nearest, minDist; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 54 | |
| 55 | function sq(x) { return x * x; } |
| 56 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 57 | function mdist(p, m) { |
| 58 | return Math.sqrt(sq(p.x - m.x) + sq(p.y - m.y)); |
| 59 | } |
| 60 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 61 | function pdrop(line, mouse) { |
| 62 | var x1 = line.x1, |
| 63 | y1 = line.y1, |
| 64 | x2 = line.x2, |
| 65 | y2 = line.y2, |
| 66 | x3 = mouse.x, |
| 67 | y3 = mouse.y, |
| 68 | k = ((y2-y1) * (x3-x1) - (x2-x1) * (y3-y1)) / |
| 69 | (sq(y2-y1) + sq(x2-x1)), |
| 70 | x4 = x3 - k * (y2-y1), |
| 71 | y4 = y3 + k * (x2-x1); |
| 72 | return {x:x4, y:y4}; |
| 73 | } |
| 74 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 75 | function lineSeg(d) { |
| 76 | return { |
| 77 | x1: d.source.x, |
| 78 | y1: d.source.y, |
| 79 | x2: d.target.x, |
| 80 | y2: d.target.y |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | function lineHit(line, p, m) { |
| 85 | if (p.x < line.x1 && p.x < line.x2) return false; |
| 86 | if (p.x > line.x1 && p.x > line.x2) return false; |
| 87 | if (p.y < line.y1 && p.y < line.y2) return false; |
| 88 | if (p.y > line.y1 && p.y > line.y2) return false; |
Simon Hunt | 5f36108 | 2015-02-25 11:36:38 -0800 | [diff] [blame] | 89 | // line intersects, but are we close enough? |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 90 | return mdist(p, m) <= proximity; |
| 91 | } |
| 92 | |
| 93 | if (network.links.length) { |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 94 | nearest = null; |
| 95 | minDist = proximity * 2; |
| 96 | |
| 97 | network.links.forEach(function (d) { |
| 98 | if (!api.showHosts() && d.type() === 'hostLink') { |
| 99 | return; // skip hidden host links |
| 100 | } |
| 101 | |
| 102 | var line = lineSeg(d), |
| 103 | point = pdrop(line, mouse), |
| 104 | hit = lineHit(line, point, mouse), |
| 105 | dist; |
| 106 | |
| 107 | if (hit) { |
| 108 | dist = mdist(point, mouse); |
| 109 | if (dist < minDist) { |
| 110 | minDist = dist; |
| 111 | nearest = d; |
| 112 | } |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 113 | } |
| 114 | }); |
| 115 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 116 | enhanceNearestLink(nearest); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | function enhanceNearestLink(ldata) { |
| 122 | // if the new link is same as old link, do nothing |
| 123 | if (enhancedLink && ldata && enhancedLink.key === ldata.key) return; |
| 124 | |
| 125 | // first, unenhance the currently enhanced link |
| 126 | if (enhancedLink) { |
| 127 | unenhance(enhancedLink); |
| 128 | } |
| 129 | enhancedLink = ldata; |
| 130 | if (enhancedLink) { |
| 131 | enhance(enhancedLink); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | function unenhance(d) { |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 136 | // guard against link element not set |
| 137 | if (d.el) { |
| 138 | d.el.classed('enhanced', false); |
| 139 | } |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 140 | api.portLabelG().selectAll('.portLabel').remove(); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | function enhance(d) { |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 144 | var data = [], |
| 145 | point; |
| 146 | |
| 147 | // guard against link element not set |
| 148 | if (!d.el) return; |
| 149 | |
Simon Hunt | d526412 | 2015-02-25 10:17:43 -0800 | [diff] [blame] | 150 | d.el.classed('enhanced', true); |
Simon Hunt | 5f36108 | 2015-02-25 11:36:38 -0800 | [diff] [blame] | 151 | $log.debug('[' + (d.srcPort || 'H') + '] ---> [' + d.tgtPort + ']', d.key); |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 152 | |
Simon Hunt | 8ae474b | 2015-02-25 15:39:14 -0800 | [diff] [blame] | 153 | // Define port label data objects. |
| 154 | // NOTE: src port is absent in the case of host-links. |
| 155 | |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 156 | point = locatePortLabel(d); |
| 157 | angular.extend(point, { |
Simon Hunt | 8ae474b | 2015-02-25 15:39:14 -0800 | [diff] [blame] | 158 | id: 'topo-port-tgt', |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 159 | num: d.tgtPort |
| 160 | }); |
| 161 | data.push(point); |
Simon Hunt | 8ae474b | 2015-02-25 15:39:14 -0800 | [diff] [blame] | 162 | |
| 163 | if (d.srcPort) { |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 164 | point = locatePortLabel(d, 1); |
| 165 | angular.extend(point, { |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 166 | id: 'topo-port-src', |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 167 | num: d.srcPort |
Simon Hunt | 8ae474b | 2015-02-25 15:39:14 -0800 | [diff] [blame] | 168 | }); |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 169 | data.push(point); |
Simon Hunt | 8ae474b | 2015-02-25 15:39:14 -0800 | [diff] [blame] | 170 | } |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 171 | |
| 172 | td3.applyPortLabels(data, api.portLabelG()); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Simon Hunt | 969b3c9 | 2015-02-25 18:11:31 -0800 | [diff] [blame] | 175 | function locatePortLabel(link, src) { |
| 176 | var near = src ? 'source' : 'target', |
| 177 | far = src ? 'target' : 'source', |
| 178 | ln = link[near], |
| 179 | lf = link[far], |
| 180 | offset = 32; |
| 181 | |
| 182 | function dist(x, y) { return Math.sqrt(x*x + y*y); } |
| 183 | |
| 184 | var dx = lf.x - ln.x, |
| 185 | dy = lf.y - ln.y, |
| 186 | k = offset / dist(dx, dy); |
| 187 | |
| 188 | return {x: k * dx + ln.x, y: k * dy + ln.y}; |
| 189 | } |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 190 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 191 | function togglePorts() { |
| 192 | showPorts = !showPorts; |
| 193 | |
| 194 | var what = showPorts ? 'Enable' : 'Disable', |
| 195 | handler = showPorts ? mouseMoveHandler : null; |
| 196 | |
| 197 | if (!showPorts) { |
| 198 | enhanceNearestLink(null); |
| 199 | } |
| 200 | svg.on('mousemove', handler); |
| 201 | flash.flash(what + ' port highlighting'); |
| 202 | } |
| 203 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 204 | // ========================== |
| 205 | // Module definition |
| 206 | |
| 207 | angular.module('ovTopo') |
| 208 | .factory('TopoLinkService', |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 209 | ['$log', 'FnService', 'SvgUtilService', 'ThemeService', 'FlashService', |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 210 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 211 | function (_$log_, _fs_, _sus_, _ts_, _flash_) { |
| 212 | $log = _$log_; |
| 213 | fs = _fs_; |
| 214 | sus = _sus_; |
| 215 | ts = _ts_; |
| 216 | flash = _flash_; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 217 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 218 | function initLink(_api_, _td3_) { |
| 219 | api = _api_; |
| 220 | td3 = _td3_; |
| 221 | svg = api.svg; |
| 222 | network = api.network; |
| 223 | if (showPorts) { |
| 224 | svg.on('mousemove', mouseMoveHandler); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 225 | } |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 226 | } |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 227 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 228 | function destroyLink() { |
| 229 | // unconditionally remove any mousemove event handler |
| 230 | svg.on('mousemove', null); |
| 231 | } |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 232 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame^] | 233 | return { |
| 234 | initLink: initLink, |
| 235 | destroyLink: destroyLink, |
| 236 | togglePorts: togglePorts |
| 237 | }; |
| 238 | }]); |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 239 | }()); |