blob: 1cd211b4542f42273a9b549b5215c9a7de43300c [file] [log] [blame]
Simon Hunt08f841d02015-02-10 14:39:20 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt08f841d02015-02-10 14:39:20 -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 Selection Module.
19 Defines behavior when selecting nodes.
20 */
21
22(function () {
23 'use strict';
24
25 // injected refs
Simon Hunt7faabd52016-08-18 16:16:19 -070026 var $log, fs, wss, tov, tps, tts, ns, sus;
Simon Hunt08f841d02015-02-10 14:39:20 -080027
28 // api to topoForce
29 var api;
30 /*
31 node() // get ref to D3 selection of nodes
32 zoomingOrPanning( ev )
33 updateDeviceColors( [dev] )
Prince Pereira46c82d42016-09-19 13:30:50 +053034 deselectAllLinks()
Simon Hunt08f841d02015-02-10 14:39:20 -080035 */
36
37 // internal state
Steven Burrows041c1aa2016-04-12 15:45:05 +010038 var hovered, selections, selectOrder, consumeClick;
39
40 function setInitialState () {
41 hovered = null; // the node over which the mouse is hovering
42 selections = {}; // currently selected nodes (by id)
43 selectOrder = []; // the order in which we made selections
Simon Hunt0c6b2d32015-03-26 17:46:29 -070044 consumeClick = false; // used to coordinate with SVG click handler
Steven Burrows041c1aa2016-04-12 15:45:05 +010045 }
Simon Hunt08f841d02015-02-10 14:39:20 -080046
47 // ==========================
48
49 function nSel() {
50 return selectOrder.length;
51 }
52 function getSel(idx) {
53 return selections[selectOrder[idx]];
54 }
55 function allSelectionsClass(cls) {
56 for (var i=0, n=nSel(); i<n; i++) {
57 if (getSel(i).obj.class !== cls) {
58 return false;
59 }
60 }
61 return true;
62 }
63
64 // ==========================
65
66 function nodeMouseOver(m) {
67 if (!m.dragStarted) {
Simon Hunt08f841d02015-02-10 14:39:20 -080068 if (hovered != m) {
69 hovered = m;
Simon Hunt584e92d2015-08-24 11:27:22 -070070 tov.hooks.mouseOver({
71 id: m.id,
72 class: m.class,
73 type: m.type
74 });
Simon Hunt08f841d02015-02-10 14:39:20 -080075 }
76 }
77 }
78
79 function nodeMouseOut(m) {
80 if (!m.dragStarted) {
81 if (hovered) {
82 hovered = null;
Simon Hunt584e92d2015-08-24 11:27:22 -070083 tov.hooks.mouseOut();
Simon Hunt08f841d02015-02-10 14:39:20 -080084 }
Simon Hunt08f841d02015-02-10 14:39:20 -080085 }
86 }
87
88 // ==========================
89
90 function selectObject(obj) {
91 var el = this,
Simon Hunt5aac2fc2015-06-09 12:34:07 -070092 nodeEv = el && el.tagName === 'g',
93 ev = d3.event.sourceEvent || {},
Simon Hunt08f841d02015-02-10 14:39:20 -080094 n;
95
96 if (api.zoomingOrPanning(ev)) {
97 return;
98 }
99
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700100 if (nodeEv) {
Simon Hunt08f841d02015-02-10 14:39:20 -0800101 n = d3.select(el);
102 } else {
103 api.node().each(function (d) {
104 if (d == obj) {
105 n = d3.select(el = this);
106 }
107 });
108 }
Prince Pereira46c82d42016-09-19 13:30:50 +0530109
110 if (obj.class === 'link') {
111
112 if (selections[obj.key]) {
113 deselectObject(obj.key);
114 } else {
115 selections[obj.key] = { obj: obj, el: el };
116 selectOrder.push(obj.key);
117 }
118
119 updateDetail();
120 return;
121 }
122
123 if (!n) {
124 return;
125 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800126
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700127 if (nodeEv) {
128 consumeClick = true;
129 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700130
Simon Hunt08f841d02015-02-10 14:39:20 -0800131 if (ev.shiftKey && n.classed('selected')) {
132 deselectObject(obj.id);
133 updateDetail();
134 return;
135 }
136
137 if (!ev.shiftKey) {
Simon Hunta17fa672015-08-19 18:42:22 -0700138 deselectAll(true);
Simon Hunt08f841d02015-02-10 14:39:20 -0800139 }
140
141 selections[obj.id] = { obj: obj, el: el };
142 selectOrder.push(obj.id);
143
144 n.classed('selected', true);
Simon Hunt4766dfb2016-06-14 17:16:22 -0700145 if (n.classed('device')) {
146 api.updateDeviceColors(obj);
147 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800148 updateDetail();
Simon Hunt08f841d02015-02-10 14:39:20 -0800149 }
150
Simon Hunt7faabd52016-08-18 16:16:19 -0700151 function reselect() {
152 selectOrder.forEach(function (id) {
153 var sel = d3.select('g#' + sus.safeId(id));
154 sel.classed('selected', true);
155 });
156 updateDetail();
157 }
158
Simon Hunt08f841d02015-02-10 14:39:20 -0800159 function deselectObject(id) {
160 var obj = selections[id];
161 if (obj) {
162 d3.select(obj.el).classed('selected', false);
163 delete selections[id];
164 fs.removeFromArray(id, selectOrder);
165 api.updateDeviceColors(obj.obj);
166 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800167 }
168
Simon Hunta17fa672015-08-19 18:42:22 -0700169 function deselectAll(skipUpdate) {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700170 var something = (selectOrder.length > 0);
171
Simon Hunt08f841d02015-02-10 14:39:20 -0800172 // deselect all nodes in the network...
173 api.node().classed('selected', false);
174 selections = {};
175 selectOrder = [];
176 api.updateDeviceColors();
Simon Hunta17fa672015-08-19 18:42:22 -0700177 if (!skipUpdate) {
178 updateDetail();
179 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700180
181 // return true if something was selected
182 return something;
Simon Hunt08f841d02015-02-10 14:39:20 -0800183 }
184
185 // === -----------------------------------------------------
186
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700187 function requestDetails(data) {
Simon Hunt237676b52015-03-10 19:04:26 -0700188 wss.sendEvent('requestDetails', {
Simon Hunt08f841d02015-02-10 14:39:20 -0800189 id: data.id,
190 class: data.class
191 });
192 }
193
194 // === -----------------------------------------------------
195
196 function updateDetail() {
197 var nSel = selectOrder.length;
198 if (!nSel) {
199 emptySelect();
200 } else if (nSel === 1) {
201 singleSelect();
202 } else {
203 multiSelect();
204 }
205 }
206
207 function emptySelect() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700208 tov.hooks.emptySelect();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700209 tps.displayNothing();
Simon Hunt08f841d02015-02-10 14:39:20 -0800210 }
211
212 function singleSelect() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700213 var data = getSel(0).obj;
Prince Pereira46c82d42016-09-19 13:30:50 +0530214
215 //the link details are already taken care of in topoLink.js
216 if (data.class === 'link') {
217 return;
218 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700219 requestDetails(data);
220 // NOTE: detail panel is shown as a response to receiving
221 // a 'showDetails' event from the server. See 'showDetails'
222 // callback function below...
Simon Hunt08f841d02015-02-10 14:39:20 -0800223 }
224
225 function multiSelect() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800226 // display the selected nodes in the detail panel
227 tps.displayMulti(selectOrder);
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700228 addHostSelectionActions();
229 tov.hooks.multiSelect(selectOrder);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700230 tps.displaySomething();
Simon Hunt08f841d02015-02-10 14:39:20 -0800231 }
232
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700233 function addHostSelectionActions() {
234 if (allSelectionsClass('host')) {
235 if (nSel() === 2) {
236 tps.addAction({
237 id: 'host-flow-btn',
238 gid: 'endstation',
239 cb: tts.addHostIntent,
240 tt: 'Create Host-to-Host Flow'
241 });
242 } else if (nSel() >= 2) {
243 tps.addAction({
244 id: 'mult-src-flow-btn',
245 gid: 'flows',
246 cb: tts.addMultiSourceIntent,
247 tt: 'Create Multi-Source Flow'
248 });
249 }
250 }
251 }
252
Simon Hunt08f841d02015-02-10 14:39:20 -0800253
254 // === -----------------------------------------------------
255 // Event Handlers
256
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700257 // display the data for the single selected node
Simon Hunt08f841d02015-02-10 14:39:20 -0800258 function showDetails(data) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700259 var buttons = fs.isA(data.buttons) || [];
Simon Hunt08f841d02015-02-10 14:39:20 -0800260 tps.displaySingle(data);
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700261 tov.installButtons(buttons, data, data.props['URI']);
262 tov.hooks.singleSelect(data);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700263 tps.displaySomething();
Simon Hunt6036b192015-02-11 11:20:26 -0800264 }
265
Simon Huntd2862c32015-08-24 17:41:51 -0700266 // returns true if one or more nodes are selected.
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700267 function somethingSelected() {
Simon Huntd2862c32015-08-24 17:41:51 -0700268 return nSel();
Simon Hunt08f841d02015-02-10 14:39:20 -0800269 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800270
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700271 function clickConsumed(x) {
272 var cc = consumeClick;
273 consumeClick = !!x;
274 return cc;
275 }
276
Simon Hunt8d558082015-10-29 21:32:50 -0700277 // returns a selection context, providing info about what is selected
278 function selectionContext() {
279 var devices = [],
280 hosts = [],
281 types = {};
282
283 angular.forEach(selections, function (d) {
284 var o = d.obj,
285 c = o.class;
286
287 if (c === 'device') {
288 devices.push(o.id);
289 types[o.id] = o.type;
290 }
291 if (c === 'host') {
292 hosts.push(o.id);
293 types[o.id] = o.type;
294 }
295 });
296
297 return {
298 devices: devices,
299 hosts: hosts,
300 types: types
301 };
302 }
303
Simon Hunt08f841d02015-02-10 14:39:20 -0800304 // === -----------------------------------------------------
305 // === MODULE DEFINITION ===
306
307 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800308 .factory('TopoSelectService',
Simon Huntfb940112015-07-29 18:36:35 -0700309 ['$log', 'FnService', 'WebSocketService', 'TopoOverlayService',
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700310 'TopoPanelService', 'TopoTrafficService', 'NavService',
Simon Hunt7faabd52016-08-18 16:16:19 -0700311 'SvgUtilService',
Simon Hunt08f841d02015-02-10 14:39:20 -0800312
Simon Hunt7faabd52016-08-18 16:16:19 -0700313 function (_$log_, _fs_, _wss_, _tov_, _tps_, _tts_, _ns_, _sus_) {
Simon Hunt6036b192015-02-11 11:20:26 -0800314 $log = _$log_;
315 fs = _fs_;
Simon Hunt237676b52015-03-10 19:04:26 -0700316 wss = _wss_;
Simon Huntfb940112015-07-29 18:36:35 -0700317 tov = _tov_;
Simon Hunt6036b192015-02-11 11:20:26 -0800318 tps = _tps_;
Simon Huntf542d842015-02-11 16:20:33 -0800319 tts = _tts_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700320 ns = _ns_;
Simon Hunt7faabd52016-08-18 16:16:19 -0700321 sus = _sus_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800322
Simon Hunt6036b192015-02-11 11:20:26 -0800323 function initSelect(_api_) {
324 api = _api_;
Simon Hunt7faabd52016-08-18 16:16:19 -0700325 if (!selections) {
326 setInitialState();
327 }
Simon Hunt6036b192015-02-11 11:20:26 -0800328 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800329
Simon Hunt6036b192015-02-11 11:20:26 -0800330 function destroySelect() { }
Simon Hunt08f841d02015-02-10 14:39:20 -0800331
Simon Hunt6036b192015-02-11 11:20:26 -0800332 return {
333 initSelect: initSelect,
334 destroySelect: destroySelect,
Simon Hunt08f841d02015-02-10 14:39:20 -0800335
Simon Hunt6036b192015-02-11 11:20:26 -0800336 showDetails: showDetails,
Simon Hunt08f841d02015-02-10 14:39:20 -0800337
Simon Hunt6036b192015-02-11 11:20:26 -0800338 nodeMouseOver: nodeMouseOver,
339 nodeMouseOut: nodeMouseOut,
340 selectObject: selectObject,
341 deselectObject: deselectObject,
342 deselectAll: deselectAll,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700343 updateDetail: updateDetail,
Simon Huntf542d842015-02-11 16:20:33 -0800344
Simon Hunta0eb0a82015-02-11 12:30:06 -0800345 hovered: function () { return hovered; },
Simon Huntf542d842015-02-11 16:20:33 -0800346 selectOrder: function () { return selectOrder; },
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700347 somethingSelected: somethingSelected,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700348
Simon Hunt8d558082015-10-29 21:32:50 -0700349 clickConsumed: clickConsumed,
Simon Hunt7faabd52016-08-18 16:16:19 -0700350 selectionContext: selectionContext,
351 reselect: reselect
Simon Hunt6036b192015-02-11 11:20:26 -0800352 };
353 }]);
Simon Hunt08f841d02015-02-10 14:39:20 -0800354}());