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 |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 26 | var $log, fs, wss, tov, tps, tts, ns; |
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] ) |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 34 | deselectLink() |
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 | } |
| 109 | if (!n) return; |
| 110 | |
Simon Hunt | 5aac2fc | 2015-06-09 12:34:07 -0700 | [diff] [blame] | 111 | if (nodeEv) { |
| 112 | consumeClick = true; |
| 113 | } |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 114 | api.deselectLink(); |
| 115 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 116 | if (ev.shiftKey && n.classed('selected')) { |
| 117 | deselectObject(obj.id); |
| 118 | updateDetail(); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if (!ev.shiftKey) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 123 | deselectAll(true); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | selections[obj.id] = { obj: obj, el: el }; |
| 127 | selectOrder.push(obj.id); |
| 128 | |
| 129 | n.classed('selected', true); |
| 130 | api.updateDeviceColors(obj); |
| 131 | updateDetail(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | function deselectObject(id) { |
| 135 | var obj = selections[id]; |
| 136 | if (obj) { |
| 137 | d3.select(obj.el).classed('selected', false); |
| 138 | delete selections[id]; |
| 139 | fs.removeFromArray(id, selectOrder); |
| 140 | api.updateDeviceColors(obj.obj); |
| 141 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 144 | function deselectAll(skipUpdate) { |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 145 | var something = (selectOrder.length > 0); |
| 146 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 147 | // deselect all nodes in the network... |
| 148 | api.node().classed('selected', false); |
| 149 | selections = {}; |
| 150 | selectOrder = []; |
| 151 | api.updateDeviceColors(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 152 | if (!skipUpdate) { |
| 153 | updateDetail(); |
| 154 | } |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 155 | |
| 156 | // return true if something was selected |
| 157 | return something; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // === ----------------------------------------------------- |
| 161 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 162 | function requestDetails(data) { |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 163 | wss.sendEvent('requestDetails', { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 164 | id: data.id, |
| 165 | class: data.class |
| 166 | }); |
| 167 | } |
| 168 | |
| 169 | // === ----------------------------------------------------- |
| 170 | |
| 171 | function updateDetail() { |
| 172 | var nSel = selectOrder.length; |
| 173 | if (!nSel) { |
| 174 | emptySelect(); |
| 175 | } else if (nSel === 1) { |
| 176 | singleSelect(); |
| 177 | } else { |
| 178 | multiSelect(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | function emptySelect() { |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 183 | tov.hooks.emptySelect(); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 184 | tps.displayNothing(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | function singleSelect() { |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 188 | var data = getSel(0).obj; |
| 189 | requestDetails(data); |
| 190 | // NOTE: detail panel is shown as a response to receiving |
| 191 | // a 'showDetails' event from the server. See 'showDetails' |
| 192 | // callback function below... |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | function multiSelect() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 196 | // display the selected nodes in the detail panel |
| 197 | tps.displayMulti(selectOrder); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 198 | addHostSelectionActions(); |
| 199 | tov.hooks.multiSelect(selectOrder); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 200 | tps.displaySomething(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 203 | function addHostSelectionActions() { |
| 204 | if (allSelectionsClass('host')) { |
| 205 | if (nSel() === 2) { |
| 206 | tps.addAction({ |
| 207 | id: 'host-flow-btn', |
| 208 | gid: 'endstation', |
| 209 | cb: tts.addHostIntent, |
| 210 | tt: 'Create Host-to-Host Flow' |
| 211 | }); |
| 212 | } else if (nSel() >= 2) { |
| 213 | tps.addAction({ |
| 214 | id: 'mult-src-flow-btn', |
| 215 | gid: 'flows', |
| 216 | cb: tts.addMultiSourceIntent, |
| 217 | tt: 'Create Multi-Source Flow' |
| 218 | }); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 223 | |
| 224 | // === ----------------------------------------------------- |
| 225 | // Event Handlers |
| 226 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 227 | // display the data for the single selected node |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 228 | function showDetails(data) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 229 | var buttons = fs.isA(data.buttons) || []; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 230 | tps.displaySingle(data); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 231 | tov.installButtons(buttons, data, data.props['URI']); |
| 232 | tov.hooks.singleSelect(data); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 233 | tps.displaySomething(); |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 236 | // returns true if one or more nodes are selected. |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 237 | function somethingSelected() { |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 238 | return nSel(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 239 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 240 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 241 | function clickConsumed(x) { |
| 242 | var cc = consumeClick; |
| 243 | consumeClick = !!x; |
| 244 | return cc; |
| 245 | } |
| 246 | |
Simon Hunt | 8d55808 | 2015-10-29 21:32:50 -0700 | [diff] [blame] | 247 | // returns a selection context, providing info about what is selected |
| 248 | function selectionContext() { |
| 249 | var devices = [], |
| 250 | hosts = [], |
| 251 | types = {}; |
| 252 | |
| 253 | angular.forEach(selections, function (d) { |
| 254 | var o = d.obj, |
| 255 | c = o.class; |
| 256 | |
| 257 | if (c === 'device') { |
| 258 | devices.push(o.id); |
| 259 | types[o.id] = o.type; |
| 260 | } |
| 261 | if (c === 'host') { |
| 262 | hosts.push(o.id); |
| 263 | types[o.id] = o.type; |
| 264 | } |
| 265 | }); |
| 266 | |
| 267 | return { |
| 268 | devices: devices, |
| 269 | hosts: hosts, |
| 270 | types: types |
| 271 | }; |
| 272 | } |
| 273 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 274 | // === ----------------------------------------------------- |
| 275 | // === MODULE DEFINITION === |
| 276 | |
| 277 | angular.module('ovTopo') |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame] | 278 | .factory('TopoSelectService', |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 279 | ['$log', 'FnService', 'WebSocketService', 'TopoOverlayService', |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 280 | 'TopoPanelService', 'TopoTrafficService', 'NavService', |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 281 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 282 | function (_$log_, _fs_, _wss_, _tov_, _tps_, _tts_, _ns_) { |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 283 | $log = _$log_; |
| 284 | fs = _fs_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 285 | wss = _wss_; |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 286 | tov = _tov_; |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 287 | tps = _tps_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 288 | tts = _tts_; |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 289 | ns = _ns_; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 290 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 291 | function initSelect(_api_) { |
| 292 | api = _api_; |
Steven Burrows | 041c1aa | 2016-04-12 15:45:05 +0100 | [diff] [blame] | 293 | setInitialState(); |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 294 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 295 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 296 | function destroySelect() { } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 297 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 298 | return { |
| 299 | initSelect: initSelect, |
| 300 | destroySelect: destroySelect, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 301 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 302 | showDetails: showDetails, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 303 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 304 | nodeMouseOver: nodeMouseOver, |
| 305 | nodeMouseOut: nodeMouseOut, |
| 306 | selectObject: selectObject, |
| 307 | deselectObject: deselectObject, |
| 308 | deselectAll: deselectAll, |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 309 | updateDetail: updateDetail, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 310 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 311 | hovered: function () { return hovered; }, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 312 | selectOrder: function () { return selectOrder; }, |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 313 | somethingSelected: somethingSelected, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 314 | |
Simon Hunt | 8d55808 | 2015-10-29 21:32:50 -0700 | [diff] [blame] | 315 | clickConsumed: clickConsumed, |
| 316 | selectionContext: selectionContext |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 317 | }; |
| 318 | }]); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 319 | }()); |