blob: 9cd49dcac24d52d7bb544c32edd3100b3341f5d1 [file] [log] [blame]
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001/*
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 Hunt0c6b2d32015-03-26 17:46:29 -070026 var $log, fs, sus, ts, flash, tss, tps;
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 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 Hunt5f361082015-02-25 11:36:38 -0800121 // line intersects, but are we close enough?
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800122 return mdist(p, m) <= proximity;
123 }
124
125 if (network.links.length) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800126 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 Huntfb8ea1f2015-02-24 21:38:09 -0800144 }
145 });
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800146 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700147 return nearest;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800148 }
149
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700150 function enhanceLink(ldata) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800151 // 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 Hunt969b3c92015-02-25 18:11:31 -0800165 // guard against link element not set
166 if (d.el) {
167 d.el.classed('enhanced', false);
168 }
Simon Hunt1a5301e2015-02-25 15:31:25 -0800169 api.portLabelG().selectAll('.portLabel').remove();
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800170 }
171
172 function enhance(d) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800173 var data = [],
174 point;
175
176 // guard against link element not set
177 if (!d.el) return;
178
Simon Huntd5264122015-02-25 10:17:43 -0800179 d.el.classed('enhanced', true);
Simon Hunt1a5301e2015-02-25 15:31:25 -0800180
Simon Hunt8ae474b2015-02-25 15:39:14 -0800181 // Define port label data objects.
182 // NOTE: src port is absent in the case of host-links.
183
Simon Hunt969b3c92015-02-25 18:11:31 -0800184 point = locatePortLabel(d);
185 angular.extend(point, {
Simon Hunt8ae474b2015-02-25 15:39:14 -0800186 id: 'topo-port-tgt',
Simon Hunt969b3c92015-02-25 18:11:31 -0800187 num: d.tgtPort
188 });
189 data.push(point);
Simon Hunt8ae474b2015-02-25 15:39:14 -0800190
191 if (d.srcPort) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800192 point = locatePortLabel(d, 1);
193 angular.extend(point, {
Simon Hunt1a5301e2015-02-25 15:31:25 -0800194 id: 'topo-port-src',
Simon Hunt969b3c92015-02-25 18:11:31 -0800195 num: d.srcPort
Simon Hunt8ae474b2015-02-25 15:39:14 -0800196 });
Simon Hunt969b3c92015-02-25 18:11:31 -0800197 data.push(point);
Simon Hunt8ae474b2015-02-25 15:39:14 -0800198 }
Simon Hunt1a5301e2015-02-25 15:31:25 -0800199
200 td3.applyPortLabels(data, api.portLabelG());
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800201 }
202
Simon Hunt969b3c92015-02-25 18:11:31 -0800203 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 Huntfb8ea1f2015-02-24 21:38:09 -0800218
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700219
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 Hunt5aac2fc2015-06-09 12:34:07 -0700263 var mp, link, node;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700264
265 if (!tss.clickConsumed()) {
266 mp = getLogicalMousePosition(this);
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700267 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 Hunt0c6b2d32015-03-26 17:46:29 -0700275 }
276 }
277
278
279 // ======================
280
Simon Huntfcbde892015-04-16 12:05:28 -0700281 function togglePorts(x) {
282 var kev = (x === 'keyev'),
283 on = kev ? !showPorts : !!x,
284 what = on ? 'Enable' : 'Disable',
285 handler = on ? mouseMoveHandler : null;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800286
Simon Huntfcbde892015-04-16 12:05:28 -0700287 showPorts = on;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800288
Simon Huntfcbde892015-04-16 12:05:28 -0700289 if (!on) {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700290 enhanceLink(null);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800291 }
292 svg.on('mousemove', handler);
293 flash.flash(what + ' port highlighting');
Simon Huntfcbde892015-04-16 12:05:28 -0700294 return on;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800295 }
296
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700297 function deselectLink() {
298 if (selectedLink) {
299 unselLink(selectedLink);
300 selectedLink = null;
301 return true;
302 }
303 return false;
304 }
305
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800306 // ==========================
307 // Module definition
308
309 angular.module('ovTopo')
310 .factory('TopoLinkService',
Simon Hunt9e2104c2015-02-26 10:48:59 -0800311 ['$log', 'FnService', 'SvgUtilService', 'ThemeService', 'FlashService',
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700312 'TopoSelectService', 'TopoPanelService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800313
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700314 function (_$log_, _fs_, _sus_, _ts_, _flash_, _tss_, _tps_) {
Simon Hunt9e2104c2015-02-26 10:48:59 -0800315 $log = _$log_;
316 fs = _fs_;
317 sus = _sus_;
318 ts = _ts_;
319 flash = _flash_;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700320 tss = _tss_;
321 tps = _tps_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800322
Simon Hunt9e2104c2015-02-26 10:48:59 -0800323 function initLink(_api_, _td3_) {
324 api = _api_;
325 td3 = _td3_;
326 svg = api.svg;
327 network = api.network;
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700328 if (showPorts && !fs.isMobile()) {
Simon Hunt9e2104c2015-02-26 10:48:59 -0800329 svg.on('mousemove', mouseMoveHandler);
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800330 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700331 svg.on('click', mouseClickHandler);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800332 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800333
Simon Hunt9e2104c2015-02-26 10:48:59 -0800334 function destroyLink() {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700335 // unconditionally remove any event handlers
Simon Hunt9e2104c2015-02-26 10:48:59 -0800336 svg.on('mousemove', null);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700337 svg.on('click', null);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800338 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800339
Simon Hunt9e2104c2015-02-26 10:48:59 -0800340 return {
341 initLink: initLink,
342 destroyLink: destroyLink,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700343 togglePorts: togglePorts,
344 deselectLink: deselectLink
Simon Hunt9e2104c2015-02-26 10:48:59 -0800345 };
346 }]);
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800347}());