blob: 77010df505a7a50d6c6877ca3efdb07efaf007c5 [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] )
Simon Hunt0c6b2d32015-03-26 17:46:29 -070034 deselectLink()
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 }
109 if (!n) return;
110
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700111 if (nodeEv) {
112 consumeClick = true;
113 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700114 api.deselectLink();
115
Simon Hunt08f841d02015-02-10 14:39:20 -0800116 if (ev.shiftKey && n.classed('selected')) {
117 deselectObject(obj.id);
118 updateDetail();
119 return;
120 }
121
122 if (!ev.shiftKey) {
Simon Hunta17fa672015-08-19 18:42:22 -0700123 deselectAll(true);
Simon Hunt08f841d02015-02-10 14:39:20 -0800124 }
125
126 selections[obj.id] = { obj: obj, el: el };
127 selectOrder.push(obj.id);
128
129 n.classed('selected', true);
Simon Hunt4766dfb2016-06-14 17:16:22 -0700130 if (n.classed('device')) {
131 api.updateDeviceColors(obj);
132 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800133 updateDetail();
Simon Hunt08f841d02015-02-10 14:39:20 -0800134 }
135
Simon Hunt7faabd52016-08-18 16:16:19 -0700136 function reselect() {
137 selectOrder.forEach(function (id) {
138 var sel = d3.select('g#' + sus.safeId(id));
139 sel.classed('selected', true);
140 });
141 updateDetail();
142 }
143
Simon Hunt08f841d02015-02-10 14:39:20 -0800144 function deselectObject(id) {
145 var obj = selections[id];
146 if (obj) {
147 d3.select(obj.el).classed('selected', false);
148 delete selections[id];
149 fs.removeFromArray(id, selectOrder);
150 api.updateDeviceColors(obj.obj);
151 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800152 }
153
Simon Hunta17fa672015-08-19 18:42:22 -0700154 function deselectAll(skipUpdate) {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700155 var something = (selectOrder.length > 0);
156
Simon Hunt08f841d02015-02-10 14:39:20 -0800157 // deselect all nodes in the network...
158 api.node().classed('selected', false);
159 selections = {};
160 selectOrder = [];
161 api.updateDeviceColors();
Simon Hunta17fa672015-08-19 18:42:22 -0700162 if (!skipUpdate) {
163 updateDetail();
164 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700165
166 // return true if something was selected
167 return something;
Simon Hunt08f841d02015-02-10 14:39:20 -0800168 }
169
170 // === -----------------------------------------------------
171
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700172 function requestDetails(data) {
Simon Hunt237676b52015-03-10 19:04:26 -0700173 wss.sendEvent('requestDetails', {
Simon Hunt08f841d02015-02-10 14:39:20 -0800174 id: data.id,
175 class: data.class
176 });
177 }
178
179 // === -----------------------------------------------------
180
181 function updateDetail() {
182 var nSel = selectOrder.length;
183 if (!nSel) {
184 emptySelect();
185 } else if (nSel === 1) {
186 singleSelect();
187 } else {
188 multiSelect();
189 }
190 }
191
192 function emptySelect() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700193 tov.hooks.emptySelect();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700194 tps.displayNothing();
Simon Hunt08f841d02015-02-10 14:39:20 -0800195 }
196
197 function singleSelect() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700198 var data = getSel(0).obj;
199 requestDetails(data);
200 // NOTE: detail panel is shown as a response to receiving
201 // a 'showDetails' event from the server. See 'showDetails'
202 // callback function below...
Simon Hunt08f841d02015-02-10 14:39:20 -0800203 }
204
205 function multiSelect() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800206 // display the selected nodes in the detail panel
207 tps.displayMulti(selectOrder);
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700208 addHostSelectionActions();
209 tov.hooks.multiSelect(selectOrder);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700210 tps.displaySomething();
Simon Hunt08f841d02015-02-10 14:39:20 -0800211 }
212
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700213 function addHostSelectionActions() {
214 if (allSelectionsClass('host')) {
215 if (nSel() === 2) {
216 tps.addAction({
217 id: 'host-flow-btn',
218 gid: 'endstation',
219 cb: tts.addHostIntent,
220 tt: 'Create Host-to-Host Flow'
221 });
222 } else if (nSel() >= 2) {
223 tps.addAction({
224 id: 'mult-src-flow-btn',
225 gid: 'flows',
226 cb: tts.addMultiSourceIntent,
227 tt: 'Create Multi-Source Flow'
228 });
229 }
230 }
231 }
232
Simon Hunt08f841d02015-02-10 14:39:20 -0800233
234 // === -----------------------------------------------------
235 // Event Handlers
236
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700237 // display the data for the single selected node
Simon Hunt08f841d02015-02-10 14:39:20 -0800238 function showDetails(data) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700239 var buttons = fs.isA(data.buttons) || [];
Simon Hunt08f841d02015-02-10 14:39:20 -0800240 tps.displaySingle(data);
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700241 tov.installButtons(buttons, data, data.props['URI']);
242 tov.hooks.singleSelect(data);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700243 tps.displaySomething();
Simon Hunt6036b192015-02-11 11:20:26 -0800244 }
245
Simon Huntd2862c32015-08-24 17:41:51 -0700246 // returns true if one or more nodes are selected.
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700247 function somethingSelected() {
Simon Huntd2862c32015-08-24 17:41:51 -0700248 return nSel();
Simon Hunt08f841d02015-02-10 14:39:20 -0800249 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800250
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700251 function clickConsumed(x) {
252 var cc = consumeClick;
253 consumeClick = !!x;
254 return cc;
255 }
256
Simon Hunt8d558082015-10-29 21:32:50 -0700257 // returns a selection context, providing info about what is selected
258 function selectionContext() {
259 var devices = [],
260 hosts = [],
261 types = {};
262
263 angular.forEach(selections, function (d) {
264 var o = d.obj,
265 c = o.class;
266
267 if (c === 'device') {
268 devices.push(o.id);
269 types[o.id] = o.type;
270 }
271 if (c === 'host') {
272 hosts.push(o.id);
273 types[o.id] = o.type;
274 }
275 });
276
277 return {
278 devices: devices,
279 hosts: hosts,
280 types: types
281 };
282 }
283
Simon Hunt08f841d02015-02-10 14:39:20 -0800284 // === -----------------------------------------------------
285 // === MODULE DEFINITION ===
286
287 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800288 .factory('TopoSelectService',
Simon Huntfb940112015-07-29 18:36:35 -0700289 ['$log', 'FnService', 'WebSocketService', 'TopoOverlayService',
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700290 'TopoPanelService', 'TopoTrafficService', 'NavService',
Simon Hunt7faabd52016-08-18 16:16:19 -0700291 'SvgUtilService',
Simon Hunt08f841d02015-02-10 14:39:20 -0800292
Simon Hunt7faabd52016-08-18 16:16:19 -0700293 function (_$log_, _fs_, _wss_, _tov_, _tps_, _tts_, _ns_, _sus_) {
Simon Hunt6036b192015-02-11 11:20:26 -0800294 $log = _$log_;
295 fs = _fs_;
Simon Hunt237676b52015-03-10 19:04:26 -0700296 wss = _wss_;
Simon Huntfb940112015-07-29 18:36:35 -0700297 tov = _tov_;
Simon Hunt6036b192015-02-11 11:20:26 -0800298 tps = _tps_;
Simon Huntf542d842015-02-11 16:20:33 -0800299 tts = _tts_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700300 ns = _ns_;
Simon Hunt7faabd52016-08-18 16:16:19 -0700301 sus = _sus_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800302
Simon Hunt6036b192015-02-11 11:20:26 -0800303 function initSelect(_api_) {
304 api = _api_;
Simon Hunt7faabd52016-08-18 16:16:19 -0700305 if (!selections) {
306 setInitialState();
307 }
Simon Hunt6036b192015-02-11 11:20:26 -0800308 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800309
Simon Hunt6036b192015-02-11 11:20:26 -0800310 function destroySelect() { }
Simon Hunt08f841d02015-02-10 14:39:20 -0800311
Simon Hunt6036b192015-02-11 11:20:26 -0800312 return {
313 initSelect: initSelect,
314 destroySelect: destroySelect,
Simon Hunt08f841d02015-02-10 14:39:20 -0800315
Simon Hunt6036b192015-02-11 11:20:26 -0800316 showDetails: showDetails,
Simon Hunt08f841d02015-02-10 14:39:20 -0800317
Simon Hunt6036b192015-02-11 11:20:26 -0800318 nodeMouseOver: nodeMouseOver,
319 nodeMouseOut: nodeMouseOut,
320 selectObject: selectObject,
321 deselectObject: deselectObject,
322 deselectAll: deselectAll,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700323 updateDetail: updateDetail,
Simon Huntf542d842015-02-11 16:20:33 -0800324
Simon Hunta0eb0a82015-02-11 12:30:06 -0800325 hovered: function () { return hovered; },
Simon Huntf542d842015-02-11 16:20:33 -0800326 selectOrder: function () { return selectOrder; },
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700327 somethingSelected: somethingSelected,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700328
Simon Hunt8d558082015-10-29 21:32:50 -0700329 clickConsumed: clickConsumed,
Simon Hunt7faabd52016-08-18 16:16:19 -0700330 selectionContext: selectionContext,
331 reselect: reselect
Simon Hunt6036b192015-02-11 11:20:26 -0800332 };
333 }]);
Simon Hunt08f841d02015-02-10 14:39:20 -0800334}());