blob: a7b5bc74226d321d4ecc103e419572770b632844 [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
Prince Pereira46c82d42016-09-19 13:30:50 +053034 selectedLinks = {}; // the links which are already 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) {
Simon Huntf9761452016-11-19 09:06:17 -0800120 var line = d.position,
121 point,
122 hit,
123 dist;
124
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800125 if (!api.showHosts() && d.type() === 'hostLink') {
126 return; // skip hidden host links
127 }
128
Simon Huntf9761452016-11-19 09:06:17 -0800129 if (line) {
130 point = pdrop(line, mouse);
131 hit = lineHit(line, point, mouse);
132 if (hit) {
133 dist = mdist(point, mouse);
134 if (dist < minDist) {
135 minDist = dist;
136 nearest = d;
137 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800138 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800139 }
140 });
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800141 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700142 return nearest;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800143 }
144
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700145 function enhanceLink(ldata) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800146 // if the new link is same as old link, do nothing
147 if (enhancedLink && ldata && enhancedLink.key === ldata.key) return;
148
149 // first, unenhance the currently enhanced link
150 if (enhancedLink) {
151 unenhance(enhancedLink);
152 }
153 enhancedLink = ldata;
154 if (enhancedLink) {
155 enhance(enhancedLink);
156 }
157 }
158
159 function unenhance(d) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800160 // guard against link element not set
161 if (d.el) {
162 d.el.classed('enhanced', false);
163 }
Simon Hunt1a5301e2015-02-25 15:31:25 -0800164 api.portLabelG().selectAll('.portLabel').remove();
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800165 }
166
167 function enhance(d) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800168 var data = [],
169 point;
170
171 // guard against link element not set
172 if (!d.el) return;
173
Simon Huntd5264122015-02-25 10:17:43 -0800174 d.el.classed('enhanced', true);
Simon Hunt1a5301e2015-02-25 15:31:25 -0800175
Simon Hunt8ae474b2015-02-25 15:39:14 -0800176 // Define port label data objects.
177 // NOTE: src port is absent in the case of host-links.
178
Simon Hunt969b3c92015-02-25 18:11:31 -0800179 point = locatePortLabel(d);
180 angular.extend(point, {
Simon Hunt8ae474b2015-02-25 15:39:14 -0800181 id: 'topo-port-tgt',
Simon Hunt969b3c92015-02-25 18:11:31 -0800182 num: d.tgtPort
183 });
184 data.push(point);
Simon Hunt8ae474b2015-02-25 15:39:14 -0800185
186 if (d.srcPort) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800187 point = locatePortLabel(d, 1);
188 angular.extend(point, {
Simon Hunt1a5301e2015-02-25 15:31:25 -0800189 id: 'topo-port-src',
Simon Hunt969b3c92015-02-25 18:11:31 -0800190 num: d.srcPort
Simon Hunt8ae474b2015-02-25 15:39:14 -0800191 });
Simon Hunt969b3c92015-02-25 18:11:31 -0800192 data.push(point);
Simon Hunt8ae474b2015-02-25 15:39:14 -0800193 }
Simon Hunt1a5301e2015-02-25 15:31:25 -0800194
195 td3.applyPortLabels(data, api.portLabelG());
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800196 }
197
Simon Hunt969b3c92015-02-25 18:11:31 -0800198 function locatePortLabel(link, src) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700199 var offset = 32,
200 pos = link.position,
201 nearX = src ? pos.x1 : pos.x2,
Steven Burrowsaefd5862016-10-19 14:20:46 -0500202 nearY = src ? pos.y1 : pos.y2,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700203 farX = src ? pos.x2 : pos.x1,
Steven Burrowsaefd5862016-10-19 14:20:46 -0500204 farY = src ? pos.y2 : pos.y1;
Simon Hunt969b3c92015-02-25 18:11:31 -0800205
206 function dist(x, y) { return Math.sqrt(x*x + y*y); }
207
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700208 var dx = farX - nearX,
209 dy = farY - nearY,
Simon Hunt969b3c92015-02-25 18:11:31 -0800210 k = offset / dist(dx, dy);
211
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700212 return {x: k * dx + nearX, y: k * dy + nearY};
Simon Hunt969b3c92015-02-25 18:11:31 -0800213 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800214
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700215 function selectLink(ldata) {
216 // if the new link is same as old link, do nothing
Prince Pereira46c82d42016-09-19 13:30:50 +0530217 if (d3.event.shiftKey && ldata.el.classed('selected')) {
218 unselLink(ldata);
219 return;
220 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700221
Prince Pereira46c82d42016-09-19 13:30:50 +0530222 if (d3.event.shiftKey && !ldata.el.classed('selected')) {
223 selLink(ldata);
224 return;
225 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700226
Prince Pereira46c82d42016-09-19 13:30:50 +0530227 tss.deselectAll();
228
Simon Huntf9761452016-11-19 09:06:17 -0800229 if (ldata) {
Kavitha Alagesan21314f02016-11-17 11:59:11 +0530230 if (ldata.el.classed('selected')) {
231 unselLink(ldata);
Simon Huntf9761452016-11-19 09:06:17 -0800232 } else {
233 selLink(ldata);
Kavitha Alagesan21314f02016-11-17 11:59:11 +0530234 }
Prince Pereira46c82d42016-09-19 13:30:50 +0530235 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700236 }
237
238 function unselLink(d) {
239 // guard against link element not set
240 if (d.el) {
241 d.el.classed('selected', false);
Prince Pereira46c82d42016-09-19 13:30:50 +0530242 delete selectedLinks[d.key];
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700243 }
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);
Prince Pereira46c82d42016-09-19 13:30:50 +0530251 selectedLinks[d.key] = {key : d};
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700252
Simon Hunt5c1a9382016-06-01 19:35:35 -0700253 tps.displayLink(d, tov.hooks.modifyLinkData);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700254 tps.displaySomething();
255 }
256
257 // ====== MOUSE EVENT HANDLERS ======
258
259 function mouseMoveHandler() {
260 var mp = getLogicalMousePosition(this),
261 link = computeNearestLink(mp);
262 enhanceLink(link);
263 }
264
265 function mouseClickHandler() {
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700266 var mp, link, node;
Prince Pereira46c82d42016-09-19 13:30:50 +0530267 if (!d3.event.shiftKey) {
268 deselectAllLinks();
269 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700270
271 if (!tss.clickConsumed()) {
272 mp = getLogicalMousePosition(this);
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700273 node = computeNearestNode(mp);
274 if (node) {
275 $log.debug('found nearest node:', node.labels[1]);
276 tss.selectObject(node);
277 } else {
278 link = computeNearestLink(mp);
279 selectLink(link);
Prince Pereira46c82d42016-09-19 13:30:50 +0530280 tss.selectObject(link);
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700281 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700282 }
283 }
284
285
286 // ======================
287
Simon Huntfcbde892015-04-16 12:05:28 -0700288 function togglePorts(x) {
289 var kev = (x === 'keyev'),
290 on = kev ? !showPorts : !!x,
291 what = on ? 'Enable' : 'Disable',
292 handler = on ? mouseMoveHandler : null;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800293
Simon Huntfcbde892015-04-16 12:05:28 -0700294 showPorts = on;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800295
Simon Huntfcbde892015-04-16 12:05:28 -0700296 if (!on) {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700297 enhanceLink(null);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800298 }
299 svg.on('mousemove', handler);
300 flash.flash(what + ' port highlighting');
Simon Huntfcbde892015-04-16 12:05:28 -0700301 return on;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800302 }
303
Prince Pereira46c82d42016-09-19 13:30:50 +0530304 function deselectAllLinks() {
305
306 if (Object.keys(selectedLinks).length > 0) {
307 network.links.forEach(function (d) {
308 if (selectedLinks[d.key]) {
309 unselLink(d);
310 }
311 });
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700312 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700313 }
314
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800315 // ==========================
316 // Module definition
317
318 angular.module('ovTopo')
319 .factory('TopoLinkService',
Simon Hunt9e2104c2015-02-26 10:48:59 -0800320 ['$log', 'FnService', 'SvgUtilService', 'ThemeService', 'FlashService',
Simon Hunt5c1a9382016-06-01 19:35:35 -0700321 'TopoSelectService', 'TopoPanelService', 'TopoOverlayService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800322
Simon Hunt5c1a9382016-06-01 19:35:35 -0700323 function (_$log_, _fs_, _sus_, _ts_, _flash_, _tss_, _tps_, _tov_) {
Simon Hunt9e2104c2015-02-26 10:48:59 -0800324 $log = _$log_;
325 fs = _fs_;
326 sus = _sus_;
327 ts = _ts_;
328 flash = _flash_;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700329 tss = _tss_;
330 tps = _tps_;
Simon Hunt5c1a9382016-06-01 19:35:35 -0700331 tov = _tov_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800332
Simon Hunt9e2104c2015-02-26 10:48:59 -0800333 function initLink(_api_, _td3_) {
334 api = _api_;
335 td3 = _td3_;
336 svg = api.svg;
337 network = api.network;
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700338 if (showPorts && !fs.isMobile()) {
Simon Hunt9e2104c2015-02-26 10:48:59 -0800339 svg.on('mousemove', mouseMoveHandler);
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800340 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700341 svg.on('click', mouseClickHandler);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800342 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800343
Simon Hunt9e2104c2015-02-26 10:48:59 -0800344 function destroyLink() {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700345 // unconditionally remove any event handlers
Simon Hunt9e2104c2015-02-26 10:48:59 -0800346 svg.on('mousemove', null);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700347 svg.on('click', null);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800348 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800349
Simon Hunt9e2104c2015-02-26 10:48:59 -0800350 return {
351 initLink: initLink,
352 destroyLink: destroyLink,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700353 togglePorts: togglePorts,
Prince Pereira46c82d42016-09-19 13:30:50 +0530354 deselectAllLinks: deselectAllLinks
Simon Hunt9e2104c2015-02-26 10:48:59 -0800355 };
356 }]);
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800357}());