blob: bdcf244746081df28f0f995e0a9fe2ee026e8377 [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
Kavitha Alagesan21314f02016-11-17 11:59:11 +0530110 if (obj !=null)
111 {
112 if (obj.class === 'link') {
113 if (selections[obj.key]) {
114 deselectObject(obj.key);
115 } else {
116 selections[obj.key] = { obj: obj, el: el };
117 selectOrder.push(obj.key);
118 }
119 updateDetail();
120 return;
Prince Pereira46c82d42016-09-19 13:30:50 +0530121 }
Prince Pereira46c82d42016-09-19 13:30:50 +0530122 }
123
124 if (!n) {
125 return;
126 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800127
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700128 if (nodeEv) {
129 consumeClick = true;
130 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700131
Simon Hunt08f841d02015-02-10 14:39:20 -0800132 if (ev.shiftKey && n.classed('selected')) {
133 deselectObject(obj.id);
134 updateDetail();
135 return;
136 }
137
138 if (!ev.shiftKey) {
Simon Hunta17fa672015-08-19 18:42:22 -0700139 deselectAll(true);
Simon Hunt08f841d02015-02-10 14:39:20 -0800140 }
141
142 selections[obj.id] = { obj: obj, el: el };
143 selectOrder.push(obj.id);
144
145 n.classed('selected', true);
Simon Hunt4766dfb2016-06-14 17:16:22 -0700146 if (n.classed('device')) {
147 api.updateDeviceColors(obj);
148 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800149 updateDetail();
Simon Hunt08f841d02015-02-10 14:39:20 -0800150 }
151
Simon Hunt7faabd52016-08-18 16:16:19 -0700152 function reselect() {
153 selectOrder.forEach(function (id) {
154 var sel = d3.select('g#' + sus.safeId(id));
155 sel.classed('selected', true);
156 });
157 updateDetail();
158 }
159
Simon Hunt08f841d02015-02-10 14:39:20 -0800160 function deselectObject(id) {
161 var obj = selections[id];
162 if (obj) {
163 d3.select(obj.el).classed('selected', false);
164 delete selections[id];
165 fs.removeFromArray(id, selectOrder);
166 api.updateDeviceColors(obj.obj);
167 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800168 }
169
Simon Hunta17fa672015-08-19 18:42:22 -0700170 function deselectAll(skipUpdate) {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700171 var something = (selectOrder.length > 0);
172
Simon Hunt08f841d02015-02-10 14:39:20 -0800173 // deselect all nodes in the network...
174 api.node().classed('selected', false);
175 selections = {};
176 selectOrder = [];
177 api.updateDeviceColors();
Simon Hunta17fa672015-08-19 18:42:22 -0700178 if (!skipUpdate) {
179 updateDetail();
180 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700181
182 // return true if something was selected
183 return something;
Simon Hunt08f841d02015-02-10 14:39:20 -0800184 }
185
186 // === -----------------------------------------------------
187
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700188 function requestDetails(data) {
Simon Hunt237676b52015-03-10 19:04:26 -0700189 wss.sendEvent('requestDetails', {
Simon Hunt08f841d02015-02-10 14:39:20 -0800190 id: data.id,
191 class: data.class
192 });
193 }
194
195 // === -----------------------------------------------------
196
197 function updateDetail() {
198 var nSel = selectOrder.length;
199 if (!nSel) {
200 emptySelect();
201 } else if (nSel === 1) {
202 singleSelect();
203 } else {
204 multiSelect();
205 }
206 }
207
208 function emptySelect() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700209 tov.hooks.emptySelect();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700210 tps.displayNothing();
Simon Hunt08f841d02015-02-10 14:39:20 -0800211 }
212
213 function singleSelect() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700214 var data = getSel(0).obj;
Prince Pereira46c82d42016-09-19 13:30:50 +0530215
216 //the link details are already taken care of in topoLink.js
217 if (data.class === 'link') {
218 return;
219 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700220 requestDetails(data);
221 // NOTE: detail panel is shown as a response to receiving
222 // a 'showDetails' event from the server. See 'showDetails'
223 // callback function below...
Simon Hunt08f841d02015-02-10 14:39:20 -0800224 }
225
226 function multiSelect() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800227 // display the selected nodes in the detail panel
228 tps.displayMulti(selectOrder);
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700229 addHostSelectionActions();
230 tov.hooks.multiSelect(selectOrder);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700231 tps.displaySomething();
Simon Hunt08f841d02015-02-10 14:39:20 -0800232 }
233
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700234 function addHostSelectionActions() {
235 if (allSelectionsClass('host')) {
236 if (nSel() === 2) {
237 tps.addAction({
238 id: 'host-flow-btn',
239 gid: 'endstation',
240 cb: tts.addHostIntent,
241 tt: 'Create Host-to-Host Flow'
242 });
243 } else if (nSel() >= 2) {
244 tps.addAction({
245 id: 'mult-src-flow-btn',
246 gid: 'flows',
247 cb: tts.addMultiSourceIntent,
248 tt: 'Create Multi-Source Flow'
249 });
250 }
251 }
252 }
253
Simon Hunt08f841d02015-02-10 14:39:20 -0800254
255 // === -----------------------------------------------------
256 // Event Handlers
257
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700258 // display the data for the single selected node
Simon Hunt08f841d02015-02-10 14:39:20 -0800259 function showDetails(data) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700260 var buttons = fs.isA(data.buttons) || [];
Simon Hunt08f841d02015-02-10 14:39:20 -0800261 tps.displaySingle(data);
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700262 tov.installButtons(buttons, data, data.props['URI']);
263 tov.hooks.singleSelect(data);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700264 tps.displaySomething();
Simon Hunt6036b192015-02-11 11:20:26 -0800265 }
266
Simon Huntd2862c32015-08-24 17:41:51 -0700267 // returns true if one or more nodes are selected.
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700268 function somethingSelected() {
Simon Huntd2862c32015-08-24 17:41:51 -0700269 return nSel();
Simon Hunt08f841d02015-02-10 14:39:20 -0800270 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800271
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700272 function clickConsumed(x) {
273 var cc = consumeClick;
274 consumeClick = !!x;
275 return cc;
276 }
277
Simon Hunt8d558082015-10-29 21:32:50 -0700278 // returns a selection context, providing info about what is selected
279 function selectionContext() {
280 var devices = [],
281 hosts = [],
282 types = {};
283
284 angular.forEach(selections, function (d) {
285 var o = d.obj,
286 c = o.class;
287
288 if (c === 'device') {
289 devices.push(o.id);
290 types[o.id] = o.type;
291 }
292 if (c === 'host') {
293 hosts.push(o.id);
294 types[o.id] = o.type;
295 }
296 });
297
298 return {
299 devices: devices,
300 hosts: hosts,
301 types: types
302 };
303 }
304
Simon Hunt08f841d02015-02-10 14:39:20 -0800305 // === -----------------------------------------------------
306 // === MODULE DEFINITION ===
307
308 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800309 .factory('TopoSelectService',
Simon Huntfb940112015-07-29 18:36:35 -0700310 ['$log', 'FnService', 'WebSocketService', 'TopoOverlayService',
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700311 'TopoPanelService', 'TopoTrafficService', 'NavService',
Simon Hunt7faabd52016-08-18 16:16:19 -0700312 'SvgUtilService',
Simon Hunt08f841d02015-02-10 14:39:20 -0800313
Simon Hunt7faabd52016-08-18 16:16:19 -0700314 function (_$log_, _fs_, _wss_, _tov_, _tps_, _tts_, _ns_, _sus_) {
Simon Hunt6036b192015-02-11 11:20:26 -0800315 $log = _$log_;
316 fs = _fs_;
Simon Hunt237676b52015-03-10 19:04:26 -0700317 wss = _wss_;
Simon Huntfb940112015-07-29 18:36:35 -0700318 tov = _tov_;
Simon Hunt6036b192015-02-11 11:20:26 -0800319 tps = _tps_;
Simon Huntf542d842015-02-11 16:20:33 -0800320 tts = _tts_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700321 ns = _ns_;
Simon Hunt7faabd52016-08-18 16:16:19 -0700322 sus = _sus_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800323
Simon Hunt6036b192015-02-11 11:20:26 -0800324 function initSelect(_api_) {
325 api = _api_;
Simon Hunt7faabd52016-08-18 16:16:19 -0700326 if (!selections) {
327 setInitialState();
328 }
Simon Hunt6036b192015-02-11 11:20:26 -0800329 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800330
Simon Hunt6036b192015-02-11 11:20:26 -0800331 function destroySelect() { }
Simon Hunt08f841d02015-02-10 14:39:20 -0800332
Simon Hunt6036b192015-02-11 11:20:26 -0800333 return {
334 initSelect: initSelect,
335 destroySelect: destroySelect,
Simon Hunt08f841d02015-02-10 14:39:20 -0800336
Simon Hunt6036b192015-02-11 11:20:26 -0800337 showDetails: showDetails,
Simon Hunt08f841d02015-02-10 14:39:20 -0800338
Simon Hunt6036b192015-02-11 11:20:26 -0800339 nodeMouseOver: nodeMouseOver,
340 nodeMouseOut: nodeMouseOut,
341 selectObject: selectObject,
342 deselectObject: deselectObject,
343 deselectAll: deselectAll,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700344 updateDetail: updateDetail,
Simon Huntf542d842015-02-11 16:20:33 -0800345
Simon Hunta0eb0a82015-02-11 12:30:06 -0800346 hovered: function () { return hovered; },
Simon Huntf542d842015-02-11 16:20:33 -0800347 selectOrder: function () { return selectOrder; },
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700348 somethingSelected: somethingSelected,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700349
Simon Hunt8d558082015-10-29 21:32:50 -0700350 clickConsumed: clickConsumed,
Simon Hunt7faabd52016-08-18 16:16:19 -0700351 selectionContext: selectionContext,
352 reselect: reselect
Simon Hunt6036b192015-02-11 11:20:26 -0800353 };
354 }]);
Simon Hunt08f841d02015-02-10 14:39:20 -0800355}());