Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1 | /* |
| 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 |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 26 | var $log, fs, wss, 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 |
| 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 Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 41 | consumeClick = false; // used to coordinate with SVG click handler |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 42 | |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 43 | // constants |
Bri Prebilic Cole | 17c6d0a | 2015-07-16 14:56:40 -0700 | [diff] [blame] | 44 | var devPath = 'device', |
| 45 | flowPath = 'flow', |
Bri Prebilic Cole | 5c60d6e | 2015-05-12 09:11:03 -0700 | [diff] [blame] | 46 | portPath ='port', |
| 47 | groupPath = 'group'; |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 48 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 49 | // ========================== |
| 50 | |
| 51 | function nSel() { |
| 52 | return selectOrder.length; |
| 53 | } |
| 54 | function getSel(idx) { |
| 55 | return selections[selectOrder[idx]]; |
| 56 | } |
| 57 | function allSelectionsClass(cls) { |
| 58 | for (var i=0, n=nSel(); i<n; i++) { |
| 59 | if (getSel(i).obj.class !== cls) { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | // ========================== |
| 67 | |
| 68 | function nodeMouseOver(m) { |
| 69 | if (!m.dragStarted) { |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 70 | //$log.debug("MouseOver()...", m); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 71 | if (hovered != m) { |
| 72 | hovered = m; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 73 | tts.requestTrafficForMode(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | function nodeMouseOut(m) { |
| 79 | if (!m.dragStarted) { |
| 80 | if (hovered) { |
| 81 | hovered = null; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 82 | tts.requestTrafficForMode(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 83 | } |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 84 | //$log.debug("MouseOut()...", m); |
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) { |
| 123 | deselectAll(); |
| 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 | |
| 144 | function deselectAll() { |
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(); |
| 152 | updateDetail(); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 153 | |
| 154 | // return true if something was selected |
| 155 | return something; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // === ----------------------------------------------------- |
| 159 | |
| 160 | function requestDetails() { |
| 161 | var data = getSel(0).obj; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 162 | wss.sendEvent('requestDetails', { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 163 | id: data.id, |
| 164 | class: data.class |
| 165 | }); |
| 166 | } |
| 167 | |
| 168 | // === ----------------------------------------------------- |
| 169 | |
| 170 | function updateDetail() { |
| 171 | var nSel = selectOrder.length; |
| 172 | if (!nSel) { |
| 173 | emptySelect(); |
| 174 | } else if (nSel === 1) { |
| 175 | singleSelect(); |
| 176 | } else { |
| 177 | multiSelect(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | function emptySelect() { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 182 | tts.cancelTraffic(); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 183 | tps.displayNothing(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | function singleSelect() { |
| 187 | // NOTE: detail is shown from 'showDetails' event callback |
| 188 | requestDetails(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 189 | tts.cancelTraffic(); |
| 190 | tts.requestTrafficForMode(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | function multiSelect() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 194 | // display the selected nodes in the detail panel |
| 195 | tps.displayMulti(selectOrder); |
| 196 | |
| 197 | // always add the 'show traffic' action |
Bri Prebilic Cole | f5e48b1 | 2015-04-21 14:52:36 -0700 | [diff] [blame] | 198 | tps.addAction({ |
| 199 | id: '-mult-rel-traf-btn', |
| 200 | gid: 'allTraffic', |
| 201 | cb: tts.showRelatedIntentsAction, |
| 202 | tt: 'Show Related Traffic' |
| 203 | }); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 204 | |
| 205 | // add other actions, based on what is selected... |
| 206 | if (nSel() === 2 && allSelectionsClass('host')) { |
Bri Prebilic Cole | f5e48b1 | 2015-04-21 14:52:36 -0700 | [diff] [blame] | 207 | tps.addAction({ |
| 208 | id: 'host-flow-btn', |
| 209 | gid: 'endstation', |
| 210 | cb: tts.addHostIntentAction, |
| 211 | tt: 'Create Host-to-Host Flow' |
| 212 | }); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 213 | } else if (nSel() >= 2 && allSelectionsClass('host')) { |
Bri Prebilic Cole | f5e48b1 | 2015-04-21 14:52:36 -0700 | [diff] [blame] | 214 | tps.addAction({ |
| 215 | id: 'mult-src-flow-btn', |
| 216 | gid: 'flows', |
| 217 | cb: tts.addMultiSourceIntentAction, |
| 218 | tt: 'Create Multi-Source Flow' |
| 219 | }); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 222 | tts.cancelTraffic(); |
| 223 | tts.requestTrafficForMode(); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 224 | tps.displaySomething(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
| 228 | // === ----------------------------------------------------- |
| 229 | // Event Handlers |
| 230 | |
| 231 | function showDetails(data) { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 232 | // display the data for the single selected node |
| 233 | tps.displaySingle(data); |
| 234 | |
| 235 | // always add the 'show traffic' action |
Bri Prebilic Cole | f5e48b1 | 2015-04-21 14:52:36 -0700 | [diff] [blame] | 236 | tps.addAction({ |
| 237 | id: '-sin-rel-traf-btn', |
| 238 | gid: 'intentTraffic', |
| 239 | cb: tts.showRelatedIntentsAction, |
| 240 | tt: 'Show Related Traffic' |
| 241 | }); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 242 | |
| 243 | // add other actions, based on what is selected... |
| 244 | if (data.type === 'switch') { |
Bri Prebilic Cole | f5e48b1 | 2015-04-21 14:52:36 -0700 | [diff] [blame] | 245 | tps.addAction({ |
| 246 | id: 'sin-dev-flows-btn', |
| 247 | gid: 'flows', |
| 248 | cb: tts.showDeviceLinkFlowsAction, |
| 249 | tt: 'Show Device Flows' |
| 250 | }); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 251 | } |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 252 | // TODO: have the server return explicit class and ID of each node |
| 253 | // for now, we assume the node is a device if it has a URI |
| 254 | if ((data.props).hasOwnProperty('URI')) { |
| 255 | tps.addAction({ |
Bri Prebilic Cole | 17c6d0a | 2015-07-16 14:56:40 -0700 | [diff] [blame] | 256 | id: 'device-table-btn', |
| 257 | gid: data.type, |
| 258 | cb: function () { |
| 259 | ns.navTo(devPath, { devId: data.props['URI'] }); |
| 260 | }, |
| 261 | tt: 'Show device view' |
| 262 | }); |
| 263 | tps.addAction({ |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 264 | id: 'flows-table-btn', |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 265 | gid: 'flowTable', |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 266 | cb: function () { |
Bri Prebilic Cole | cdc188d | 2015-04-24 16:40:11 -0700 | [diff] [blame] | 267 | ns.navTo(flowPath, { devId: data.props['URI'] }); |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 268 | }, |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 269 | tt: 'Show flow view for this device' |
| 270 | }); |
| 271 | tps.addAction({ |
| 272 | id: 'ports-table-btn', |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 273 | gid: 'portTable', |
Bri Prebilic Cole | ac829e4 | 2015-05-05 13:42:06 -0700 | [diff] [blame] | 274 | cb: function () { |
| 275 | ns.navTo(portPath, { devId: data.props['URI'] }); |
| 276 | }, |
| 277 | tt: 'Show port view for this device' |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 278 | }); |
Bri Prebilic Cole | 5c60d6e | 2015-05-12 09:11:03 -0700 | [diff] [blame] | 279 | tps.addAction({ |
| 280 | id: 'groups-table-btn', |
| 281 | gid: 'groupTable', |
| 282 | cb: function () { |
| 283 | ns.navTo(groupPath, { devId: data.props['URI'] }); |
| 284 | }, |
| 285 | tt: 'Show group view for this device' |
| 286 | }); |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 287 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 288 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 289 | tps.displaySomething(); |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 292 | function validateSelectionContext() { |
| 293 | if (!hovered && !nSel()) { |
| 294 | tts.cancelTraffic(); |
| 295 | return false; |
| 296 | } |
| 297 | return true; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 298 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 299 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 300 | function clickConsumed(x) { |
| 301 | var cc = consumeClick; |
| 302 | consumeClick = !!x; |
| 303 | return cc; |
| 304 | } |
| 305 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 306 | // === ----------------------------------------------------- |
| 307 | // === MODULE DEFINITION === |
| 308 | |
| 309 | angular.module('ovTopo') |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame] | 310 | .factory('TopoSelectService', |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 311 | ['$log', 'FnService', 'WebSocketService', |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 312 | 'TopoPanelService', 'TopoTrafficService', 'NavService', |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 313 | |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 314 | function (_$log_, _fs_, _wss_, _tps_, _tts_, _ns_) { |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 315 | $log = _$log_; |
| 316 | fs = _fs_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 317 | wss = _wss_; |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 318 | tps = _tps_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 319 | tts = _tts_; |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 320 | ns = _ns_; |
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_; |
| 324 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 325 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 326 | function destroySelect() { } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 327 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 328 | return { |
| 329 | initSelect: initSelect, |
| 330 | destroySelect: destroySelect, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 331 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 332 | showDetails: showDetails, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 333 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 334 | nodeMouseOver: nodeMouseOver, |
| 335 | nodeMouseOut: nodeMouseOut, |
| 336 | selectObject: selectObject, |
| 337 | deselectObject: deselectObject, |
| 338 | deselectAll: deselectAll, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 339 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 340 | hovered: function () { return hovered; }, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 341 | selectOrder: function () { return selectOrder; }, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 342 | validateSelectionContext: validateSelectionContext, |
| 343 | |
| 344 | clickConsumed: clickConsumed |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 345 | }; |
| 346 | }]); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 347 | }()); |