blob: 95a9daa292fda3e6d0e3399c7b7547b0795de176 [file] [log] [blame]
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Huntfb8ea1f2015-02-24 21:38:09 -08003 *
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 Hunt5c1a9382016-06-01 19:35:35 -070026 var $log, fs, sus, ts, flash, tss, tps, tov;
Simon Huntfb8ea1f2015-02-24 21:38:09 -080027
Simon Hunt0c6b2d32015-03-26 17:46:29 -070028 // internal state
Simon Huntfb8ea1f2015-02-24 21:38:09 -080029 var api,
Simon Hunt1a5301e2015-02-25 15:31:25 -080030 td3,
Simon Huntfb8ea1f2015-02-24 21:38:09 -080031 network,
Simon Hunt0c6b2d32015-03-26 17:46:29 -070032 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 Huntfb8ea1f2015-02-24 21:38:09 -080035
36 // SVG elements;
Simon Hunt9e2104c2015-02-26 10:48:59 -080037 var svg;
38
Simon Huntfb8ea1f2015-02-24 21:38:09 -080039
40 // ======== ALGORITHM TO FIND LINK CLOSEST TO MOUSE ========
41
Simon Hunt0c6b2d32015-03-26 17:46:29 -070042 function getLogicalMousePosition(container) {
43 var m = d3.mouse(container),
Simon Hunt9e2104c2015-02-26 10:48:59 -080044 sc = api.zoomer.scale(),
45 tr = api.zoomer.translate(),
46 mx = (m[0] - tr[0]) / sc,
47 my = (m[1] - tr[1]) / sc;
Simon Hunt0c6b2d32015-03-26 17:46:29 -070048 return {x: mx, y: my};
Simon Huntfb8ea1f2015-02-24 21:38:09 -080049 }
50
Simon Hunt5aac2fc2015-06-09 12:34:07 -070051
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 Hunt0c6b2d32015-03-26 17:46:29 -070064 nearest = null,
65 minDist;
Simon Huntfb8ea1f2015-02-24 21:38:09 -080066
Simon Hunt5aac2fc2015-06-09 12:34:07 -070067 if (network.nodes.length) {
68 minDist = proximity * 2;
Simon Huntfb8ea1f2015-02-24 21:38:09 -080069
Simon Hunt5aac2fc2015-06-09 12:34:07 -070070 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 Hunt9e2104c2015-02-26 10:48:59 -080083 }
Simon Hunt5aac2fc2015-06-09 12:34:07 -070084 return nearest;
85 }
86
87
88 function computeNearestLink(mouse) {
89 var proximity = prox(30),
90 nearest = null,
91 minDist;
Simon Hunt9e2104c2015-02-26 10:48:59 -080092
Simon Huntfb8ea1f2015-02-24 21:38:09 -080093 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 Huntfb8ea1f2015-02-24 21:38:09 -0800107 function lineHit(line, p, m) {
108 if (p.x < line.x1 && p.x < line.x2) return false;
109 if (p.x > line.x1 && p.x > line.x2) return false;
110 if (p.y < line.y1 && p.y < line.y2) return false;
111 if (p.y > line.y1 && p.y > line.y2) return false;
Simon Hunt5f361082015-02-25 11:36:38 -0800112 // line intersects, but are we close enough?
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800113 return mdist(p, m) <= proximity;
114 }
115
116 if (network.links.length) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800117 minDist = proximity * 2;
118
119 network.links.forEach(function (d) {
120 if (!api.showHosts() && d.type() === 'hostLink') {
121 return; // skip hidden host links
122 }
123
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700124 var line = d.position,
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800125 point = pdrop(line, mouse),
126 hit = lineHit(line, point, mouse),
127 dist;
128
129 if (hit) {
130 dist = mdist(point, mouse);
131 if (dist < minDist) {
132 minDist = dist;
133 nearest = d;
134 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800135 }
136 });
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800137 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700138 return nearest;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800139 }
140
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700141 function enhanceLink(ldata) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800142 // if the new link is same as old link, do nothing
143 if (enhancedLink && ldata && enhancedLink.key === ldata.key) return;
144
145 // first, unenhance the currently enhanced link
146 if (enhancedLink) {
147 unenhance(enhancedLink);
148 }
149 enhancedLink = ldata;
150 if (enhancedLink) {
151 enhance(enhancedLink);
152 }
153 }
154
155 function unenhance(d) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800156 // guard against link element not set
157 if (d.el) {
158 d.el.classed('enhanced', false);
159 }
Simon Hunt1a5301e2015-02-25 15:31:25 -0800160 api.portLabelG().selectAll('.portLabel').remove();
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800161 }
162
163 function enhance(d) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800164 var data = [],
165 point;
166
167 // guard against link element not set
168 if (!d.el) return;
169
Simon Huntd5264122015-02-25 10:17:43 -0800170 d.el.classed('enhanced', true);
Simon Hunt1a5301e2015-02-25 15:31:25 -0800171
Simon Hunt8ae474b2015-02-25 15:39:14 -0800172 // Define port label data objects.
173 // NOTE: src port is absent in the case of host-links.
174
Simon Hunt969b3c92015-02-25 18:11:31 -0800175 point = locatePortLabel(d);
176 angular.extend(point, {
Simon Hunt8ae474b2015-02-25 15:39:14 -0800177 id: 'topo-port-tgt',
Simon Hunt969b3c92015-02-25 18:11:31 -0800178 num: d.tgtPort
179 });
180 data.push(point);
Simon Hunt8ae474b2015-02-25 15:39:14 -0800181
182 if (d.srcPort) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800183 point = locatePortLabel(d, 1);
184 angular.extend(point, {
Simon Hunt1a5301e2015-02-25 15:31:25 -0800185 id: 'topo-port-src',
Simon Hunt969b3c92015-02-25 18:11:31 -0800186 num: d.srcPort
Simon Hunt8ae474b2015-02-25 15:39:14 -0800187 });
Simon Hunt969b3c92015-02-25 18:11:31 -0800188 data.push(point);
Simon Hunt8ae474b2015-02-25 15:39:14 -0800189 }
Simon Hunt1a5301e2015-02-25 15:31:25 -0800190
191 td3.applyPortLabels(data, api.portLabelG());
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800192 }
193
Simon Hunt969b3c92015-02-25 18:11:31 -0800194 function locatePortLabel(link, src) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700195 var offset = 32,
196 pos = link.position,
197 nearX = src ? pos.x1 : pos.x2,
198 nearY = src ? pos.y1 : pos.y2,
199 farX = src ? pos.x2 : pos.x1,
200 farY = src ? pos.y2 : pos.y1;
Simon Hunt969b3c92015-02-25 18:11:31 -0800201
202 function dist(x, y) { return Math.sqrt(x*x + y*y); }
203
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700204 var dx = farX - nearX,
205 dy = farY - nearY,
Simon Hunt969b3c92015-02-25 18:11:31 -0800206 k = offset / dist(dx, dy);
207
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700208 return {x: k * dx + nearX, y: k * dy + nearY};
Simon Hunt969b3c92015-02-25 18:11:31 -0800209 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800210
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700211 function selectLink(ldata) {
212 // if the new link is same as old link, do nothing
213 if (selectedLink && ldata && selectedLink.key === ldata.key) return;
214
215 // make sure no nodes are selected
216 tss.deselectAll();
217
218 // first, unenhance the currently enhanced link
219 if (selectedLink) {
220 unselLink(selectedLink);
221 }
222 selectedLink = ldata;
223 if (selectedLink) {
224 selLink(selectedLink);
225 }
226 }
227
228 function unselLink(d) {
229 // guard against link element not set
230 if (d.el) {
231 d.el.classed('selected', false);
232 }
233 }
234
235 function selLink(d) {
236 // guard against link element not set
237 if (!d.el) return;
238
239 d.el.classed('selected', true);
240
Simon Hunt5c1a9382016-06-01 19:35:35 -0700241 tps.displayLink(d, tov.hooks.modifyLinkData);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700242 tps.displaySomething();
243 }
244
245 // ====== MOUSE EVENT HANDLERS ======
246
247 function mouseMoveHandler() {
248 var mp = getLogicalMousePosition(this),
249 link = computeNearestLink(mp);
250 enhanceLink(link);
251 }
252
253 function mouseClickHandler() {
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700254 var mp, link, node;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700255
256 if (!tss.clickConsumed()) {
257 mp = getLogicalMousePosition(this);
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700258 node = computeNearestNode(mp);
259 if (node) {
260 $log.debug('found nearest node:', node.labels[1]);
261 tss.selectObject(node);
262 } else {
263 link = computeNearestLink(mp);
264 selectLink(link);
265 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700266 }
267 }
268
269
270 // ======================
271
Simon Huntfcbde892015-04-16 12:05:28 -0700272 function togglePorts(x) {
273 var kev = (x === 'keyev'),
274 on = kev ? !showPorts : !!x,
275 what = on ? 'Enable' : 'Disable',
276 handler = on ? mouseMoveHandler : null;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800277
Simon Huntfcbde892015-04-16 12:05:28 -0700278 showPorts = on;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800279
Simon Huntfcbde892015-04-16 12:05:28 -0700280 if (!on) {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700281 enhanceLink(null);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800282 }
283 svg.on('mousemove', handler);
284 flash.flash(what + ' port highlighting');
Simon Huntfcbde892015-04-16 12:05:28 -0700285 return on;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800286 }
287
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700288 function deselectLink() {
289 if (selectedLink) {
290 unselLink(selectedLink);
291 selectedLink = null;
292 return true;
293 }
294 return false;
295 }
296
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800297 // ==========================
298 // Module definition
299
300 angular.module('ovTopo')
301 .factory('TopoLinkService',
Simon Hunt9e2104c2015-02-26 10:48:59 -0800302 ['$log', 'FnService', 'SvgUtilService', 'ThemeService', 'FlashService',
Simon Hunt5c1a9382016-06-01 19:35:35 -0700303 'TopoSelectService', 'TopoPanelService', 'TopoOverlayService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800304
Simon Hunt5c1a9382016-06-01 19:35:35 -0700305 function (_$log_, _fs_, _sus_, _ts_, _flash_, _tss_, _tps_, _tov_) {
Simon Hunt9e2104c2015-02-26 10:48:59 -0800306 $log = _$log_;
307 fs = _fs_;
308 sus = _sus_;
309 ts = _ts_;
310 flash = _flash_;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700311 tss = _tss_;
312 tps = _tps_;
Simon Hunt5c1a9382016-06-01 19:35:35 -0700313 tov = _tov_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800314
Simon Hunt9e2104c2015-02-26 10:48:59 -0800315 function initLink(_api_, _td3_) {
316 api = _api_;
317 td3 = _td3_;
318 svg = api.svg;
319 network = api.network;
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700320 if (showPorts && !fs.isMobile()) {
Simon Hunt9e2104c2015-02-26 10:48:59 -0800321 svg.on('mousemove', mouseMoveHandler);
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800322 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700323 svg.on('click', mouseClickHandler);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800324 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800325
Simon Hunt9e2104c2015-02-26 10:48:59 -0800326 function destroyLink() {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700327 // unconditionally remove any event handlers
Simon Hunt9e2104c2015-02-26 10:48:59 -0800328 svg.on('mousemove', null);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700329 svg.on('click', null);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800330 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800331
Simon Hunt9e2104c2015-02-26 10:48:59 -0800332 return {
333 initLink: initLink,
334 destroyLink: destroyLink,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700335 togglePorts: togglePorts,
336 deselectLink: deselectLink
Simon Hunt9e2104c2015-02-26 10:48:59 -0800337 };
338 }]);
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800339}());