blob: 2e73ea2ba5c5ecee19e6c9dcd7d22c9cb32af25c [file] [log] [blame]
Simon Hunt08f841d02015-02-10 14:39:20 -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 Selection Module.
19 Defines behavior when selecting nodes.
20 */
21
22(function () {
23 'use strict';
24
25 // injected refs
Simon Huntfb940112015-07-29 18:36:35 -070026 var $log, fs, wss, tov, tps, tts, ns;
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
38 var hovered, // the node over which the mouse is hovering
39 selections = {}, // currently selected nodes (by id)
40 selectOrder = [], // the order in which we made selections
Simon Hunt0c6b2d32015-03-26 17:46:29 -070041 consumeClick = false; // used to coordinate with SVG click handler
Simon Hunt08f841d02015-02-10 14:39:20 -080042
43 // ==========================
44
45 function nSel() {
46 return selectOrder.length;
47 }
48 function getSel(idx) {
49 return selections[selectOrder[idx]];
50 }
51 function allSelectionsClass(cls) {
52 for (var i=0, n=nSel(); i<n; i++) {
53 if (getSel(i).obj.class !== cls) {
54 return false;
55 }
56 }
57 return true;
58 }
59
60 // ==========================
61
62 function nodeMouseOver(m) {
63 if (!m.dragStarted) {
Simon Hunt36a58c62015-04-08 11:00:07 -070064 //$log.debug("MouseOver()...", m);
Simon Hunt08f841d02015-02-10 14:39:20 -080065 if (hovered != m) {
66 hovered = m;
Simon Huntf542d842015-02-11 16:20:33 -080067 tts.requestTrafficForMode();
Simon Hunt08f841d02015-02-10 14:39:20 -080068 }
69 }
70 }
71
72 function nodeMouseOut(m) {
73 if (!m.dragStarted) {
74 if (hovered) {
75 hovered = null;
Simon Huntf542d842015-02-11 16:20:33 -080076 tts.requestTrafficForMode();
Simon Hunt08f841d02015-02-10 14:39:20 -080077 }
Simon Hunt36a58c62015-04-08 11:00:07 -070078 //$log.debug("MouseOut()...", m);
Simon Hunt08f841d02015-02-10 14:39:20 -080079 }
80 }
81
82 // ==========================
83
84 function selectObject(obj) {
85 var el = this,
Simon Hunt5aac2fc2015-06-09 12:34:07 -070086 nodeEv = el && el.tagName === 'g',
87 ev = d3.event.sourceEvent || {},
Simon Hunt08f841d02015-02-10 14:39:20 -080088 n;
89
90 if (api.zoomingOrPanning(ev)) {
91 return;
92 }
93
Simon Hunt5aac2fc2015-06-09 12:34:07 -070094 if (nodeEv) {
Simon Hunt08f841d02015-02-10 14:39:20 -080095 n = d3.select(el);
96 } else {
97 api.node().each(function (d) {
98 if (d == obj) {
99 n = d3.select(el = this);
100 }
101 });
102 }
103 if (!n) return;
104
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700105 if (nodeEv) {
106 consumeClick = true;
107 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700108 api.deselectLink();
109
Simon Hunt08f841d02015-02-10 14:39:20 -0800110 if (ev.shiftKey && n.classed('selected')) {
111 deselectObject(obj.id);
112 updateDetail();
113 return;
114 }
115
116 if (!ev.shiftKey) {
117 deselectAll();
118 }
119
120 selections[obj.id] = { obj: obj, el: el };
121 selectOrder.push(obj.id);
122
123 n.classed('selected', true);
124 api.updateDeviceColors(obj);
125 updateDetail();
Simon Hunt08f841d02015-02-10 14:39:20 -0800126 }
127
128 function deselectObject(id) {
129 var obj = selections[id];
130 if (obj) {
131 d3.select(obj.el).classed('selected', false);
132 delete selections[id];
133 fs.removeFromArray(id, selectOrder);
134 api.updateDeviceColors(obj.obj);
135 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800136 }
137
138 function deselectAll() {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700139 var something = (selectOrder.length > 0);
140
Simon Hunt08f841d02015-02-10 14:39:20 -0800141 // deselect all nodes in the network...
142 api.node().classed('selected', false);
143 selections = {};
144 selectOrder = [];
145 api.updateDeviceColors();
146 updateDetail();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700147
148 // return true if something was selected
149 return something;
Simon Hunt08f841d02015-02-10 14:39:20 -0800150 }
151
152 // === -----------------------------------------------------
153
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700154 function requestDetails(data) {
Simon Hunt237676b52015-03-10 19:04:26 -0700155 wss.sendEvent('requestDetails', {
Simon Hunt08f841d02015-02-10 14:39:20 -0800156 id: data.id,
157 class: data.class
158 });
159 }
160
161 // === -----------------------------------------------------
162
163 function updateDetail() {
164 var nSel = selectOrder.length;
165 if (!nSel) {
166 emptySelect();
167 } else if (nSel === 1) {
168 singleSelect();
169 } else {
170 multiSelect();
171 }
172 }
173
174 function emptySelect() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700175 tov.hooks.emptySelect();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700176 tps.displayNothing();
Simon Hunt08f841d02015-02-10 14:39:20 -0800177 }
178
179 function singleSelect() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700180 var data = getSel(0).obj;
181 requestDetails(data);
182 // NOTE: detail panel is shown as a response to receiving
183 // a 'showDetails' event from the server. See 'showDetails'
184 // callback function below...
Simon Hunt08f841d02015-02-10 14:39:20 -0800185 }
186
187 function multiSelect() {
Simon Hunt08f841d02015-02-10 14:39:20 -0800188 // display the selected nodes in the detail panel
189 tps.displayMulti(selectOrder);
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700190 addHostSelectionActions();
191 tov.hooks.multiSelect(selectOrder);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700192 tps.displaySomething();
Simon Hunt08f841d02015-02-10 14:39:20 -0800193 }
194
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700195 function addHostSelectionActions() {
196 if (allSelectionsClass('host')) {
197 if (nSel() === 2) {
198 tps.addAction({
199 id: 'host-flow-btn',
200 gid: 'endstation',
201 cb: tts.addHostIntent,
202 tt: 'Create Host-to-Host Flow'
203 });
204 } else if (nSel() >= 2) {
205 tps.addAction({
206 id: 'mult-src-flow-btn',
207 gid: 'flows',
208 cb: tts.addMultiSourceIntent,
209 tt: 'Create Multi-Source Flow'
210 });
211 }
212 }
213 }
214
Simon Hunt08f841d02015-02-10 14:39:20 -0800215
216 // === -----------------------------------------------------
217 // Event Handlers
218
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700219 // display the data for the single selected node
Simon Hunt08f841d02015-02-10 14:39:20 -0800220 function showDetails(data) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700221 var buttons = fs.isA(data.buttons) || [];
Simon Hunt08f841d02015-02-10 14:39:20 -0800222 tps.displaySingle(data);
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700223 tov.installButtons(buttons, data, data.props['URI']);
224 tov.hooks.singleSelect(data);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700225 tps.displaySomething();
Simon Hunt6036b192015-02-11 11:20:26 -0800226 }
227
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700228 // returns true if we are hovering over a node, or any nodes are selected
229 function somethingSelected() {
230 return hovered || nSel();
Simon Hunt08f841d02015-02-10 14:39:20 -0800231 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800232
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700233 function clickConsumed(x) {
234 var cc = consumeClick;
235 consumeClick = !!x;
236 return cc;
237 }
238
Simon Hunt08f841d02015-02-10 14:39:20 -0800239 // === -----------------------------------------------------
240 // === MODULE DEFINITION ===
241
242 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800243 .factory('TopoSelectService',
Simon Huntfb940112015-07-29 18:36:35 -0700244 ['$log', 'FnService', 'WebSocketService', 'TopoOverlayService',
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700245 'TopoPanelService', 'TopoTrafficService', 'NavService',
Simon Hunt08f841d02015-02-10 14:39:20 -0800246
Simon Huntfb940112015-07-29 18:36:35 -0700247 function (_$log_, _fs_, _wss_, _tov_, _tps_, _tts_, _ns_) {
Simon Hunt6036b192015-02-11 11:20:26 -0800248 $log = _$log_;
249 fs = _fs_;
Simon Hunt237676b52015-03-10 19:04:26 -0700250 wss = _wss_;
Simon Huntfb940112015-07-29 18:36:35 -0700251 tov = _tov_;
Simon Hunt6036b192015-02-11 11:20:26 -0800252 tps = _tps_;
Simon Huntf542d842015-02-11 16:20:33 -0800253 tts = _tts_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700254 ns = _ns_;
Simon Hunt08f841d02015-02-10 14:39:20 -0800255
Simon Hunt6036b192015-02-11 11:20:26 -0800256 function initSelect(_api_) {
257 api = _api_;
258 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800259
Simon Hunt6036b192015-02-11 11:20:26 -0800260 function destroySelect() { }
Simon Hunt08f841d02015-02-10 14:39:20 -0800261
Simon Hunt6036b192015-02-11 11:20:26 -0800262 return {
263 initSelect: initSelect,
264 destroySelect: destroySelect,
Simon Hunt08f841d02015-02-10 14:39:20 -0800265
Simon Hunt6036b192015-02-11 11:20:26 -0800266 showDetails: showDetails,
Simon Hunt08f841d02015-02-10 14:39:20 -0800267
Simon Hunt6036b192015-02-11 11:20:26 -0800268 nodeMouseOver: nodeMouseOver,
269 nodeMouseOut: nodeMouseOut,
270 selectObject: selectObject,
271 deselectObject: deselectObject,
272 deselectAll: deselectAll,
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700273 updateDetail: updateDetail,
Simon Huntf542d842015-02-11 16:20:33 -0800274
Simon Hunta0eb0a82015-02-11 12:30:06 -0800275 hovered: function () { return hovered; },
Simon Huntf542d842015-02-11 16:20:33 -0800276 selectOrder: function () { return selectOrder; },
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700277 somethingSelected: somethingSelected,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700278
279 clickConsumed: clickConsumed
Simon Hunt6036b192015-02-11 11:20:26 -0800280 };
281 }]);
Simon Hunt08f841d02015-02-10 14:39:20 -0800282}());