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 |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 26 | var $log, fs, wss, tps, tts; |
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 | |
| 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 Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 64 | //$log.debug("MouseOver()...", m); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 65 | if (hovered != m) { |
| 66 | hovered = m; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 67 | tts.requestTrafficForMode(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | function nodeMouseOut(m) { |
| 73 | if (!m.dragStarted) { |
| 74 | if (hovered) { |
| 75 | hovered = null; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 76 | tts.requestTrafficForMode(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 77 | } |
Simon Hunt | 36a58c6 | 2015-04-08 11:00:07 -0700 | [diff] [blame] | 78 | //$log.debug("MouseOut()...", m); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | // ========================== |
| 83 | |
| 84 | function selectObject(obj) { |
| 85 | var el = this, |
| 86 | ev = d3.event.sourceEvent, |
| 87 | n; |
| 88 | |
| 89 | if (api.zoomingOrPanning(ev)) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (el) { |
| 94 | n = d3.select(el); |
| 95 | } else { |
| 96 | api.node().each(function (d) { |
| 97 | if (d == obj) { |
| 98 | n = d3.select(el = this); |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | if (!n) return; |
| 103 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 104 | consumeClick = true; |
| 105 | api.deselectLink(); |
| 106 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 107 | if (ev.shiftKey && n.classed('selected')) { |
| 108 | deselectObject(obj.id); |
| 109 | updateDetail(); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | if (!ev.shiftKey) { |
| 114 | deselectAll(); |
| 115 | } |
| 116 | |
| 117 | selections[obj.id] = { obj: obj, el: el }; |
| 118 | selectOrder.push(obj.id); |
| 119 | |
| 120 | n.classed('selected', true); |
| 121 | api.updateDeviceColors(obj); |
| 122 | updateDetail(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | function deselectObject(id) { |
| 126 | var obj = selections[id]; |
| 127 | if (obj) { |
| 128 | d3.select(obj.el).classed('selected', false); |
| 129 | delete selections[id]; |
| 130 | fs.removeFromArray(id, selectOrder); |
| 131 | api.updateDeviceColors(obj.obj); |
| 132 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | function deselectAll() { |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 136 | var something = (selectOrder.length > 0); |
| 137 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 138 | // deselect all nodes in the network... |
| 139 | api.node().classed('selected', false); |
| 140 | selections = {}; |
| 141 | selectOrder = []; |
| 142 | api.updateDeviceColors(); |
| 143 | updateDetail(); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 144 | |
| 145 | // return true if something was selected |
| 146 | return something; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // === ----------------------------------------------------- |
| 150 | |
| 151 | function requestDetails() { |
| 152 | var data = getSel(0).obj; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 153 | wss.sendEvent('requestDetails', { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 154 | id: data.id, |
| 155 | class: data.class |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | // === ----------------------------------------------------- |
| 160 | |
| 161 | function updateDetail() { |
| 162 | var nSel = selectOrder.length; |
| 163 | if (!nSel) { |
| 164 | emptySelect(); |
| 165 | } else if (nSel === 1) { |
| 166 | singleSelect(); |
| 167 | } else { |
| 168 | multiSelect(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | function emptySelect() { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 173 | tts.cancelTraffic(); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 174 | tps.displayNothing(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | function singleSelect() { |
| 178 | // NOTE: detail is shown from 'showDetails' event callback |
| 179 | requestDetails(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 180 | tts.cancelTraffic(); |
| 181 | tts.requestTrafficForMode(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | function multiSelect() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 185 | // display the selected nodes in the detail panel |
| 186 | tps.displayMulti(selectOrder); |
| 187 | |
| 188 | // always add the 'show traffic' action |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 189 | tps.addAction('Show Related Traffic', tts.showRelatedIntentsAction); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 190 | |
| 191 | // add other actions, based on what is selected... |
| 192 | if (nSel() === 2 && allSelectionsClass('host')) { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 193 | tps.addAction('Create Host-to-Host Flow', tts.addHostIntentAction); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 194 | } else if (nSel() >= 2 && allSelectionsClass('host')) { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 195 | tps.addAction('Create Multi-Source Flow', tts.addMultiSourceIntentAction); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 198 | tts.cancelTraffic(); |
| 199 | tts.requestTrafficForMode(); |
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 | |
| 203 | |
| 204 | // === ----------------------------------------------------- |
| 205 | // Event Handlers |
| 206 | |
| 207 | function showDetails(data) { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 208 | // display the data for the single selected node |
| 209 | tps.displaySingle(data); |
| 210 | |
| 211 | // always add the 'show traffic' action |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 212 | tps.addAction('Show Related Traffic', tts.showRelatedIntentsAction); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 213 | |
| 214 | // add other actions, based on what is selected... |
| 215 | if (data.type === 'switch') { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 216 | tps.addAction('Show Device Flows', tts.showDeviceLinkFlowsAction); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 219 | tps.displaySomething(); |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 222 | function validateSelectionContext() { |
| 223 | if (!hovered && !nSel()) { |
| 224 | tts.cancelTraffic(); |
| 225 | return false; |
| 226 | } |
| 227 | return true; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 228 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 229 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 230 | function clickConsumed(x) { |
| 231 | var cc = consumeClick; |
| 232 | consumeClick = !!x; |
| 233 | return cc; |
| 234 | } |
| 235 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 236 | // === ----------------------------------------------------- |
| 237 | // === MODULE DEFINITION === |
| 238 | |
| 239 | angular.module('ovTopo') |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame] | 240 | .factory('TopoSelectService', |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 241 | ['$log', 'FnService', 'WebSocketService', |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 242 | 'TopoPanelService', 'TopoTrafficService', |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 243 | |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 244 | function (_$log_, _fs_, _wss_, _tps_, _tts_) { |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 245 | $log = _$log_; |
| 246 | fs = _fs_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 247 | wss = _wss_; |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 248 | tps = _tps_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 249 | tts = _tts_; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 250 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 251 | function initSelect(_api_) { |
| 252 | api = _api_; |
| 253 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 254 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 255 | function destroySelect() { } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 256 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 257 | return { |
| 258 | initSelect: initSelect, |
| 259 | destroySelect: destroySelect, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 260 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 261 | showDetails: showDetails, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 262 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 263 | nodeMouseOver: nodeMouseOver, |
| 264 | nodeMouseOut: nodeMouseOut, |
| 265 | selectObject: selectObject, |
| 266 | deselectObject: deselectObject, |
| 267 | deselectAll: deselectAll, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 268 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 269 | hovered: function () { return hovered; }, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 270 | selectOrder: function () { return selectOrder; }, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 271 | validateSelectionContext: validateSelectionContext, |
| 272 | |
| 273 | clickConsumed: clickConsumed |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 274 | }; |
| 275 | }]); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 276 | }()); |