Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 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 |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 26 | var $log, fs, wss, tov, tps, tts, ns, sus, tpis; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 27 | |
| 28 | // api to topoForce |
| 29 | var api; |
| 30 | /* |
| 31 | node() // get ref to D3 selection of nodes |
| 32 | zoomingOrPanning( ev ) |
| 33 | updateDeviceColors( [dev] ) |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 34 | deselectAllLinks() |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | // internal state |
Steven Burrows | 041c1aa | 2016-04-12 15:45:05 +0100 | [diff] [blame] | 38 | 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 Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 44 | consumeClick = false; // used to coordinate with SVG click handler |
Steven Burrows | 041c1aa | 2016-04-12 15:45:05 +0100 | [diff] [blame] | 45 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 46 | |
| 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 Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 68 | if (hovered != m) { |
| 69 | hovered = m; |
Simon Hunt | 584e92d | 2015-08-24 11:27:22 -0700 | [diff] [blame] | 70 | tov.hooks.mouseOver({ |
| 71 | id: m.id, |
| 72 | class: m.class, |
| 73 | type: m.type |
| 74 | }); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | function nodeMouseOut(m) { |
| 80 | if (!m.dragStarted) { |
| 81 | if (hovered) { |
| 82 | hovered = null; |
Simon Hunt | 584e92d | 2015-08-24 11:27:22 -0700 | [diff] [blame] | 83 | tov.hooks.mouseOut(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 84 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | // ========================== |
| 89 | |
| 90 | function selectObject(obj) { |
| 91 | var el = this, |
Simon Hunt | 5aac2fc | 2015-06-09 12:34:07 -0700 | [diff] [blame] | 92 | nodeEv = el && el.tagName === 'g', |
| 93 | ev = d3.event.sourceEvent || {}, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 94 | n; |
| 95 | |
| 96 | if (api.zoomingOrPanning(ev)) { |
| 97 | return; |
| 98 | } |
| 99 | |
Simon Hunt | 5aac2fc | 2015-06-09 12:34:07 -0700 | [diff] [blame] | 100 | if (nodeEv) { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 101 | 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 Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 109 | |
Simon Hunt | f976145 | 2016-11-19 09:06:17 -0800 | [diff] [blame] | 110 | if (obj && obj.class === 'link') { |
| 111 | if (selections[obj.key]) { |
| 112 | deselectObject(obj.key); |
| 113 | } else { |
| 114 | selections[obj.key] = { obj: obj, el: el }; |
| 115 | selectOrder.push(obj.key); |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 116 | } |
Simon Hunt | f976145 | 2016-11-19 09:06:17 -0800 | [diff] [blame] | 117 | updateDetail(); |
| 118 | return; |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if (!n) { |
| 122 | return; |
| 123 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 124 | |
Simon Hunt | 5aac2fc | 2015-06-09 12:34:07 -0700 | [diff] [blame] | 125 | if (nodeEv) { |
| 126 | consumeClick = true; |
| 127 | } |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 128 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 129 | if (ev.shiftKey && n.classed('selected')) { |
| 130 | deselectObject(obj.id); |
| 131 | updateDetail(); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (!ev.shiftKey) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 136 | deselectAll(true); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | selections[obj.id] = { obj: obj, el: el }; |
| 140 | selectOrder.push(obj.id); |
| 141 | |
| 142 | n.classed('selected', true); |
Simon Hunt | 4766dfb | 2016-06-14 17:16:22 -0700 | [diff] [blame] | 143 | if (n.classed('device')) { |
| 144 | api.updateDeviceColors(obj); |
| 145 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 146 | updateDetail(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Simon Hunt | 7faabd5 | 2016-08-18 16:16:19 -0700 | [diff] [blame] | 149 | function reselect() { |
| 150 | selectOrder.forEach(function (id) { |
| 151 | var sel = d3.select('g#' + sus.safeId(id)); |
| 152 | sel.classed('selected', true); |
| 153 | }); |
| 154 | updateDetail(); |
| 155 | } |
| 156 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 157 | function deselectObject(id) { |
| 158 | var obj = selections[id]; |
| 159 | if (obj) { |
| 160 | d3.select(obj.el).classed('selected', false); |
| 161 | delete selections[id]; |
| 162 | fs.removeFromArray(id, selectOrder); |
| 163 | api.updateDeviceColors(obj.obj); |
| 164 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 167 | function deselectAll(skipUpdate) { |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 168 | var something = (selectOrder.length > 0); |
| 169 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 170 | // deselect all nodes in the network... |
| 171 | api.node().classed('selected', false); |
| 172 | selections = {}; |
| 173 | selectOrder = []; |
| 174 | api.updateDeviceColors(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 175 | if (!skipUpdate) { |
| 176 | updateDetail(); |
| 177 | } |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 178 | |
| 179 | // return true if something was selected |
| 180 | return something; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // === ----------------------------------------------------- |
| 184 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 185 | function requestDetails(data) { |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 186 | wss.sendEvent('requestDetails', { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 187 | id: data.id, |
| 188 | class: data.class |
| 189 | }); |
| 190 | } |
| 191 | |
| 192 | // === ----------------------------------------------------- |
| 193 | |
| 194 | function updateDetail() { |
| 195 | var nSel = selectOrder.length; |
| 196 | if (!nSel) { |
| 197 | emptySelect(); |
| 198 | } else if (nSel === 1) { |
| 199 | singleSelect(); |
| 200 | } else { |
| 201 | multiSelect(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | function emptySelect() { |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 206 | tov.hooks.emptySelect(); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 207 | tps.displayNothing(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | function singleSelect() { |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 211 | var data = getSel(0).obj; |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 212 | |
| 213 | //the link details are already taken care of in topoLink.js |
| 214 | if (data.class === 'link') { |
| 215 | return; |
| 216 | } |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 217 | requestDetails(data); |
| 218 | // NOTE: detail panel is shown as a response to receiving |
| 219 | // a 'showDetails' event from the server. See 'showDetails' |
| 220 | // callback function below... |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | function multiSelect() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 224 | // display the selected nodes in the detail panel |
| 225 | tps.displayMulti(selectOrder); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 226 | addHostSelectionActions(); |
| 227 | tov.hooks.multiSelect(selectOrder); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 228 | tps.displaySomething(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 231 | function addHostSelectionActions() { |
| 232 | if (allSelectionsClass('host')) { |
| 233 | if (nSel() === 2) { |
| 234 | tps.addAction({ |
| 235 | id: 'host-flow-btn', |
| 236 | gid: 'endstation', |
| 237 | cb: tts.addHostIntent, |
| 238 | tt: 'Create Host-to-Host Flow' |
| 239 | }); |
| 240 | } else if (nSel() >= 2) { |
| 241 | tps.addAction({ |
| 242 | id: 'mult-src-flow-btn', |
| 243 | gid: 'flows', |
| 244 | cb: tts.addMultiSourceIntent, |
| 245 | tt: 'Create Multi-Source Flow' |
| 246 | }); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 251 | |
| 252 | // === ----------------------------------------------------- |
| 253 | // Event Handlers |
| 254 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 255 | // display the data for the single selected node |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 256 | function showDetails(data) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 257 | var buttons = fs.isA(data.buttons) || []; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 258 | tps.displaySingle(data); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 259 | tov.installButtons(buttons, data, data.props['URI']); |
| 260 | tov.hooks.singleSelect(data); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 261 | tps.displaySomething(); |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 264 | // returns true if one or more nodes are selected. |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 265 | function somethingSelected() { |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 266 | return nSel(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 267 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 268 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 269 | function clickConsumed(x) { |
| 270 | var cc = consumeClick; |
| 271 | consumeClick = !!x; |
| 272 | return cc; |
| 273 | } |
| 274 | |
Simon Hunt | 8d55808 | 2015-10-29 21:32:50 -0700 | [diff] [blame] | 275 | // returns a selection context, providing info about what is selected |
| 276 | function selectionContext() { |
| 277 | var devices = [], |
| 278 | hosts = [], |
| 279 | types = {}; |
| 280 | |
| 281 | angular.forEach(selections, function (d) { |
| 282 | var o = d.obj, |
| 283 | c = o.class; |
| 284 | |
| 285 | if (c === 'device') { |
| 286 | devices.push(o.id); |
| 287 | types[o.id] = o.type; |
| 288 | } |
| 289 | if (c === 'host') { |
| 290 | hosts.push(o.id); |
| 291 | types[o.id] = o.type; |
| 292 | } |
| 293 | }); |
| 294 | |
| 295 | return { |
| 296 | devices: devices, |
| 297 | hosts: hosts, |
| 298 | types: types |
| 299 | }; |
| 300 | } |
| 301 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 302 | // === ----------------------------------------------------- |
| 303 | // === MODULE DEFINITION === |
| 304 | |
| 305 | angular.module('ovTopo') |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame] | 306 | .factory('TopoSelectService', |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 307 | ['$log', 'FnService', 'WebSocketService', 'TopoOverlayService', |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 308 | 'TopoPanelService', 'TopoTrafficService', 'NavService', |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 309 | 'SvgUtilService', 'TopoProtectedIntentsService', |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 310 | |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 311 | function (_$log_, _fs_, _wss_, _tov_, _tps_, _tts_, _ns_, _sus_, _tpis_) { |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 312 | $log = _$log_; |
| 313 | fs = _fs_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 314 | wss = _wss_; |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 315 | tov = _tov_; |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 316 | tps = _tps_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 317 | tts = _tts_; |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 318 | ns = _ns_; |
Simon Hunt | 7faabd5 | 2016-08-18 16:16:19 -0700 | [diff] [blame] | 319 | sus = _sus_; |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 320 | tpis= _tpis_; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 321 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 322 | function initSelect(_api_) { |
| 323 | api = _api_; |
Simon Hunt | 7faabd5 | 2016-08-18 16:16:19 -0700 | [diff] [blame] | 324 | if (!selections) { |
| 325 | setInitialState(); |
| 326 | } |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 327 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 328 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 329 | function destroySelect() { } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 330 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 331 | return { |
| 332 | initSelect: initSelect, |
| 333 | destroySelect: destroySelect, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 334 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 335 | showDetails: showDetails, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 336 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 337 | nodeMouseOver: nodeMouseOver, |
| 338 | nodeMouseOut: nodeMouseOut, |
| 339 | selectObject: selectObject, |
| 340 | deselectObject: deselectObject, |
| 341 | deselectAll: deselectAll, |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 342 | updateDetail: updateDetail, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 343 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 344 | hovered: function () { return hovered; }, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 345 | selectOrder: function () { return selectOrder; }, |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 346 | somethingSelected: somethingSelected, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 347 | |
Simon Hunt | 8d55808 | 2015-10-29 21:32:50 -0700 | [diff] [blame] | 348 | clickConsumed: clickConsumed, |
Simon Hunt | 7faabd5 | 2016-08-18 16:16:19 -0700 | [diff] [blame] | 349 | selectionContext: selectionContext, |
| 350 | reselect: reselect |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 351 | }; |
| 352 | }]); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 353 | }()); |