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 | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 26 | var $log, fs, flash, 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] ) |
| 34 | sendEvent( type, {payload} ) |
| 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 |
| 41 | haveDetails = false, // do we have details of one or more nodes? |
| 42 | useDetails = true; // should we show details if we have 'em? |
| 43 | |
| 44 | // ========================== |
| 45 | |
| 46 | function nSel() { |
| 47 | return selectOrder.length; |
| 48 | } |
| 49 | function getSel(idx) { |
| 50 | return selections[selectOrder[idx]]; |
| 51 | } |
| 52 | function allSelectionsClass(cls) { |
| 53 | for (var i=0, n=nSel(); i<n; i++) { |
| 54 | if (getSel(i).obj.class !== cls) { |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | // ========================== |
| 62 | |
| 63 | function nodeMouseOver(m) { |
| 64 | if (!m.dragStarted) { |
| 65 | $log.debug("MouseOver()...", m); |
| 66 | if (hovered != m) { |
| 67 | hovered = m; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 68 | tts.requestTrafficForMode(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | function nodeMouseOut(m) { |
| 74 | if (!m.dragStarted) { |
| 75 | if (hovered) { |
| 76 | hovered = null; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 77 | tts.requestTrafficForMode(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 78 | } |
| 79 | $log.debug("MouseOut()...", m); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // ========================== |
| 84 | |
| 85 | function selectObject(obj) { |
| 86 | var el = this, |
| 87 | ev = d3.event.sourceEvent, |
| 88 | n; |
| 89 | |
| 90 | if (api.zoomingOrPanning(ev)) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (el) { |
| 95 | 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 | |
| 105 | if (ev.shiftKey && n.classed('selected')) { |
| 106 | deselectObject(obj.id); |
| 107 | updateDetail(); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | if (!ev.shiftKey) { |
| 112 | deselectAll(); |
| 113 | } |
| 114 | |
| 115 | selections[obj.id] = { obj: obj, el: el }; |
| 116 | selectOrder.push(obj.id); |
| 117 | |
| 118 | n.classed('selected', true); |
| 119 | api.updateDeviceColors(obj); |
| 120 | updateDetail(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | function deselectObject(id) { |
| 124 | var obj = selections[id]; |
| 125 | if (obj) { |
| 126 | d3.select(obj.el).classed('selected', false); |
| 127 | delete selections[id]; |
| 128 | fs.removeFromArray(id, selectOrder); |
| 129 | api.updateDeviceColors(obj.obj); |
| 130 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | function deselectAll() { |
| 134 | // deselect all nodes in the network... |
| 135 | api.node().classed('selected', false); |
| 136 | selections = {}; |
| 137 | selectOrder = []; |
| 138 | api.updateDeviceColors(); |
| 139 | updateDetail(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // === ----------------------------------------------------- |
| 143 | |
| 144 | function requestDetails() { |
| 145 | var data = getSel(0).obj; |
| 146 | api.sendEvent('requestDetails', { |
| 147 | id: data.id, |
| 148 | class: data.class |
| 149 | }); |
| 150 | } |
| 151 | |
| 152 | // === ----------------------------------------------------- |
| 153 | |
| 154 | function updateDetail() { |
| 155 | var nSel = selectOrder.length; |
| 156 | if (!nSel) { |
| 157 | emptySelect(); |
| 158 | } else if (nSel === 1) { |
| 159 | singleSelect(); |
| 160 | } else { |
| 161 | multiSelect(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | function emptySelect() { |
| 166 | haveDetails = false; |
| 167 | tps.hideDetailPanel(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 168 | tts.cancelTraffic(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | function singleSelect() { |
| 172 | // NOTE: detail is shown from 'showDetails' event callback |
| 173 | requestDetails(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 174 | tts.cancelTraffic(); |
| 175 | tts.requestTrafficForMode(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | function multiSelect() { |
| 179 | haveDetails = true; |
| 180 | |
| 181 | // display the selected nodes in the detail panel |
| 182 | tps.displayMulti(selectOrder); |
| 183 | |
| 184 | // always add the 'show traffic' action |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 185 | tps.addAction('Show Related Traffic', tts.showRelatedIntentsAction); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 186 | |
| 187 | // add other actions, based on what is selected... |
| 188 | if (nSel() === 2 && allSelectionsClass('host')) { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 189 | tps.addAction('Create Host-to-Host Flow', tts.addHostIntentAction); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 190 | } else if (nSel() >= 2 && allSelectionsClass('host')) { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 191 | tps.addAction('Create Multi-Source Flow', tts.addMultiSourceIntentAction); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 194 | tts.cancelTraffic(); |
| 195 | tts.requestTrafficForMode(); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | // === ----------------------------------------------------- |
| 200 | // Event Handlers |
| 201 | |
| 202 | function showDetails(data) { |
| 203 | haveDetails = true; |
| 204 | |
| 205 | // display the data for the single selected node |
| 206 | tps.displaySingle(data); |
| 207 | |
| 208 | // always add the 'show traffic' action |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 209 | tps.addAction('Show Related Traffic', tts.showRelatedIntentsAction); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 210 | |
| 211 | // add other actions, based on what is selected... |
| 212 | if (data.type === 'switch') { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 213 | tps.addAction('Show Device Flows', tts.showDeviceLinkFlowsAction); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // only show the details panel if the user hasn't "hidden" it |
| 217 | if (useDetails) { |
| 218 | tps.showDetailPanel(); |
| 219 | } |
| 220 | } |
| 221 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 222 | function toggleDetails() { |
| 223 | useDetails = !useDetails; |
| 224 | if (useDetails) { |
| 225 | flash.flash('Enable details panel'); |
| 226 | if (haveDetails) { |
| 227 | tps.showDetailPanel(); |
| 228 | } |
| 229 | } else { |
| 230 | flash.flash('Disable details panel'); |
| 231 | tps.hideDetailPanel(); |
| 232 | } |
| 233 | } |
| 234 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 235 | function validateSelectionContext() { |
| 236 | if (!hovered && !nSel()) { |
| 237 | tts.cancelTraffic(); |
| 238 | return false; |
| 239 | } |
| 240 | return true; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 241 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 242 | |
| 243 | // === ----------------------------------------------------- |
| 244 | // === MODULE DEFINITION === |
| 245 | |
| 246 | angular.module('ovTopo') |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame] | 247 | .factory('TopoSelectService', |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 248 | ['$log', 'FnService', 'FlashService', 'TopoPanelService', |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 249 | 'TopoTrafficService', |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 250 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 251 | function (_$log_, _fs_, _flash_, _tps_, _tts_) { |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 252 | $log = _$log_; |
| 253 | fs = _fs_; |
| 254 | flash = _flash_; |
| 255 | tps = _tps_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 256 | tts = _tts_; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 257 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 258 | function initSelect(_api_) { |
| 259 | api = _api_; |
| 260 | } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 261 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 262 | function destroySelect() { } |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 263 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 264 | return { |
| 265 | initSelect: initSelect, |
| 266 | destroySelect: destroySelect, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 267 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 268 | showDetails: showDetails, |
| 269 | toggleDetails: toggleDetails, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 270 | |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 271 | nodeMouseOver: nodeMouseOver, |
| 272 | nodeMouseOut: nodeMouseOut, |
| 273 | selectObject: selectObject, |
| 274 | deselectObject: deselectObject, |
| 275 | deselectAll: deselectAll, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 276 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 277 | hovered: function () { return hovered; }, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 278 | haveDetails: function () { return haveDetails; }, |
| 279 | selectOrder: function () { return selectOrder; }, |
| 280 | validateSelectionContext: validateSelectionContext |
Simon Hunt | 6036b19 | 2015-02-11 11:20:26 -0800 | [diff] [blame] | 281 | }; |
| 282 | }]); |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 283 | }()); |