blob: bfe0df562b9a31944292071452c5b07da7668b38 [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) {
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
Prince Pereira46c82d42016-09-19 13:30:50 +0530213 if (d3.event.shiftKey && ldata.el.classed('selected')) {
214 unselLink(ldata);
215 return;
216 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700217
Prince Pereira46c82d42016-09-19 13:30:50 +0530218 if (d3.event.shiftKey && !ldata.el.classed('selected')) {
219 selLink(ldata);
220 return;
221 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700222
Prince Pereira46c82d42016-09-19 13:30:50 +0530223 tss.deselectAll();
224
225 if (!ldata.el.classed('selected')) {
226 selLink(ldata);
227 return;
228 }
229
230 if (ldata.el.classed('selected')) {
231 unselLink(ldata);
232 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700233 }
234
235 function unselLink(d) {
236 // guard against link element not set
237 if (d.el) {
238 d.el.classed('selected', false);
Prince Pereira46c82d42016-09-19 13:30:50 +0530239 delete selectedLinks[d.key];
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700240 }
241 }
242
243 function selLink(d) {
244 // guard against link element not set
245 if (!d.el) return;
246
247 d.el.classed('selected', true);
Prince Pereira46c82d42016-09-19 13:30:50 +0530248 selectedLinks[d.key] = {key : d};
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700249
Simon Hunt5c1a9382016-06-01 19:35:35 -0700250 tps.displayLink(d, tov.hooks.modifyLinkData);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700251 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 Hunt5aac2fc2015-06-09 12:34:07 -0700263 var mp, link, node;
Prince Pereira46c82d42016-09-19 13:30:50 +0530264 if (!d3.event.shiftKey) {
265 deselectAllLinks();
266 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700267
268 if (!tss.clickConsumed()) {
269 mp = getLogicalMousePosition(this);
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700270 node = computeNearestNode(mp);
271 if (node) {
272 $log.debug('found nearest node:', node.labels[1]);
273 tss.selectObject(node);
274 } else {
275 link = computeNearestLink(mp);
276 selectLink(link);
Prince Pereira46c82d42016-09-19 13:30:50 +0530277 tss.selectObject(link);
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700278 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700279 }
280 }
281
282
283 // ======================
284
Simon Huntfcbde892015-04-16 12:05:28 -0700285 function togglePorts(x) {
286 var kev = (x === 'keyev'),
287 on = kev ? !showPorts : !!x,
288 what = on ? 'Enable' : 'Disable',
289 handler = on ? mouseMoveHandler : null;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800290
Simon Huntfcbde892015-04-16 12:05:28 -0700291 showPorts = on;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800292
Simon Huntfcbde892015-04-16 12:05:28 -0700293 if (!on) {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700294 enhanceLink(null);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800295 }
296 svg.on('mousemove', handler);
297 flash.flash(what + ' port highlighting');
Simon Huntfcbde892015-04-16 12:05:28 -0700298 return on;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800299 }
300
Prince Pereira46c82d42016-09-19 13:30:50 +0530301 function deselectAllLinks() {
302
303 if (Object.keys(selectedLinks).length > 0) {
304 network.links.forEach(function (d) {
305 if (selectedLinks[d.key]) {
306 unselLink(d);
307 }
308 });
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700309 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700310 }
311
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800312 // ==========================
313 // Module definition
314
315 angular.module('ovTopo')
316 .factory('TopoLinkService',
Simon Hunt9e2104c2015-02-26 10:48:59 -0800317 ['$log', 'FnService', 'SvgUtilService', 'ThemeService', 'FlashService',
Simon Hunt5c1a9382016-06-01 19:35:35 -0700318 'TopoSelectService', 'TopoPanelService', 'TopoOverlayService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800319
Simon Hunt5c1a9382016-06-01 19:35:35 -0700320 function (_$log_, _fs_, _sus_, _ts_, _flash_, _tss_, _tps_, _tov_) {
Simon Hunt9e2104c2015-02-26 10:48:59 -0800321 $log = _$log_;
322 fs = _fs_;
323 sus = _sus_;
324 ts = _ts_;
325 flash = _flash_;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700326 tss = _tss_;
327 tps = _tps_;
Simon Hunt5c1a9382016-06-01 19:35:35 -0700328 tov = _tov_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800329
Simon Hunt9e2104c2015-02-26 10:48:59 -0800330 function initLink(_api_, _td3_) {
331 api = _api_;
332 td3 = _td3_;
333 svg = api.svg;
334 network = api.network;
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700335 if (showPorts && !fs.isMobile()) {
Simon Hunt9e2104c2015-02-26 10:48:59 -0800336 svg.on('mousemove', mouseMoveHandler);
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800337 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700338 svg.on('click', mouseClickHandler);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800339 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800340
Simon Hunt9e2104c2015-02-26 10:48:59 -0800341 function destroyLink() {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700342 // unconditionally remove any event handlers
Simon Hunt9e2104c2015-02-26 10:48:59 -0800343 svg.on('mousemove', null);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700344 svg.on('click', null);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800345 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800346
Simon Hunt9e2104c2015-02-26 10:48:59 -0800347 return {
348 initLink: initLink,
349 destroyLink: destroyLink,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700350 togglePorts: togglePorts,
Prince Pereira46c82d42016-09-19 13:30:50 +0530351 deselectAllLinks: deselectAllLinks
Simon Hunt9e2104c2015-02-26 10:48:59 -0800352 };
353 }]);
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800354}());