Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [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 | (function () { |
| 18 | |
| 19 | // Injected Services |
Simon Hunt | 47f8fb7 | 2017-04-11 13:49:31 -0700 | [diff] [blame] | 20 | var $log, fs, ks, flash, wss, t2ps, t2bgs, ps, t2is, t2sp, t2vs, t2rs, |
Steven Burrows | 31e00c61 | 2018-02-06 10:38:57 +0000 | [diff] [blame] | 21 | t2fs, t2tbs, t2dp, svg, sus; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 22 | |
| 23 | // Commmands |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 24 | function actionMap() { |
| 25 | return { |
| 26 | L: [cycleDeviceLabels, 'Cycle device labels'], |
| 27 | B: [toggleBackground, 'Toggle background'], |
Steven Burrows | b8ebd40 | 2017-11-01 15:31:31 +0000 | [diff] [blame] | 28 | D: [toggleDetails, 'Toggle details panel'], |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 29 | I: [toggleInstancePanel, 'Toggle ONOS Instance Panel'], |
| 30 | O: [toggleSummary, 'Toggle the Summary Panel'], |
| 31 | R: [resetZoom, 'Reset pan / zoom'], |
| 32 | P: [togglePorts, 'Toggle Port Highlighting'], |
| 33 | E: [equalizeMasters, 'Equalize mastership roles'], |
| 34 | X: [resetNodeLocation, 'Reset Node Location'], |
| 35 | U: [unpinNode, 'Unpin node (mouse over)'], |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 36 | H: [toggleHosts, 'Toggle host visibility'], |
Steven Burrows | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 37 | M: [toggleOfflineDevices, 'Toggle offline visibility'], |
Simon Hunt | 47f8fb7 | 2017-04-11 13:49:31 -0700 | [diff] [blame] | 38 | dot: [toggleToolbar, 'Toggle Toolbar'], |
Steven Burrows | cc6f6de | 2017-08-30 12:50:38 +0100 | [diff] [blame] | 39 | 'shift-L': [cycleHostLabels, 'Cycle host labels'], |
Steven Burrows | c0efbf0 | 2016-11-16 11:30:47 -0600 | [diff] [blame] | 40 | |
Steven Burrows | 31e00c61 | 2018-02-06 10:38:57 +0000 | [diff] [blame] | 41 | // -- instance color palette debug |
| 42 | 9: function () { sus.cat7().testCard(svg); }, |
| 43 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 44 | esc: handleEscape, |
| 45 | |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 46 | // topology overlay selections |
| 47 | F1: function () { t2tbs.fnKey(0); }, |
| 48 | F2: function () { t2tbs.fnKey(1); }, |
| 49 | F3: function () { t2tbs.fnKey(2); }, |
| 50 | F4: function () { t2tbs.fnKey(3); }, |
| 51 | F5: function () { t2tbs.fnKey(4); }, |
| 52 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 53 | _keyListener: t2tbs.keyListener.bind(t2tbs), |
Steven Burrows | a58cb82 | 2017-11-01 16:15:13 +0000 | [diff] [blame] | 54 | |
| 55 | _helpFormat: [ |
| 56 | ['I', 'O', 'D', 'H', 'M', 'P', 'dash', 'B'], |
| 57 | ['X', 'Z', 'N', 'L', 'shift-L', 'U', 'R', 'E', 'dot'], |
| 58 | [], // this column reserved for overlay actions |
| 59 | ], |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 60 | }; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 61 | } |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 62 | |
Steven Burrows | 31e00c61 | 2018-02-06 10:38:57 +0000 | [diff] [blame] | 63 | function init(_t2fs_, _t2tbs_, _svg_) { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 64 | t2fs = _t2fs_; |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 65 | t2tbs = _t2tbs_; |
Steven Burrows | 31e00c61 | 2018-02-06 10:38:57 +0000 | [diff] [blame] | 66 | svg = _svg_; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 67 | bindCommands(); |
| 68 | } |
| 69 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 70 | function bindCommands(additional) { |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 71 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 72 | var am = actionMap(), |
| 73 | add = fs.isO(additional); |
| 74 | |
| 75 | if (add) { |
| 76 | _.each(add, function (value, key) { |
| 77 | // filter out meta properties (e.g. _keyOrder) |
Steven Burrows | 202034f | 2017-08-11 12:08:01 +0100 | [diff] [blame] | 78 | if (!(_.startsWith(key, '_'))) { |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 79 | // don't allow re-definition of existing key bindings |
| 80 | if (am[key]) { |
| 81 | $log.warn('keybind: ' + key + ' already exists'); |
| 82 | } else { |
| 83 | am[key] = [value.cb, value.tt]; |
| 84 | } |
| 85 | } |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | ks.keyBindings(am); |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 90 | |
| 91 | ks.gestureNotes([ |
| 92 | ['click', 'Select the item and show details'], |
| 93 | ['shift-click', 'Toggle selection state'], |
| 94 | ['drag', 'Reposition (and pin) device / host'], |
| 95 | ['cmd-scroll', 'Zoom in / out'], |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 96 | ['cmd-drag', 'Pan'], |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 97 | ]); |
| 98 | } |
| 99 | |
Steven Burrows | c0efbf0 | 2016-11-16 11:30:47 -0600 | [diff] [blame] | 100 | function handleEscape() { |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 101 | |
| 102 | if (false) { |
| 103 | // TODO: Cancel show mastership |
| 104 | // TODO: Cancel Active overlay |
| 105 | |
| 106 | } else if (t2rs.deselectAllNodes()) { |
| 107 | // else if we have node selections, deselect them all |
| 108 | // (work already done) |
| 109 | } else if (t2rs.deselectLink()) { |
| 110 | // else if we have a link selection, deselect it |
| 111 | // (work already done) |
Steven Burrows | c0efbf0 | 2016-11-16 11:30:47 -0600 | [diff] [blame] | 112 | } else if (t2is.isVisible()) { |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 113 | // If the instance panel is visible, close it |
| 114 | t2is.toggle(); |
| 115 | } else if (t2sp.isVisible()) { |
| 116 | // If the summary panel is visible, close it |
| 117 | t2sp.toggle(); |
| 118 | } |
Steven Burrows | c0efbf0 | 2016-11-16 11:30:47 -0600 | [diff] [blame] | 119 | } |
| 120 | |
Steven Burrows | 01ddf60 | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 121 | var prefsState = {}; |
| 122 | |
| 123 | function updatePrefsState(what, b) { |
| 124 | prefsState[what] = b ? 1 : 0; |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 125 | ps.setPrefs('topo2_prefs', prefsState); |
Steven Burrows | 01ddf60 | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 128 | function deviceLabelFlashMessage(index) { |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 129 | switch (index) { |
| 130 | case 0: return 'Hide device labels'; |
| 131 | case 1: return 'Show friendly device labels'; |
| 132 | case 2: return 'Show device ID labels'; |
| 133 | } |
| 134 | } |
| 135 | |
Steven Burrows | cc6f6de | 2017-08-30 12:50:38 +0100 | [diff] [blame] | 136 | function hostLabelFlashMessage(index) { |
| 137 | switch (index) { |
| 138 | case 0: return 'Hide host labels'; |
| 139 | case 1: return 'Show friendly host labels'; |
| 140 | case 2: return 'Show host IP labels'; |
| 141 | case 3: return 'Show host MAC Address labels'; |
| 142 | } |
| 143 | } |
| 144 | |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 145 | function cycleDeviceLabels() { |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 146 | var deviceLabelIndex = t2ps.get('dlbls') + 1, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 147 | newDeviceLabelIndex = deviceLabelIndex % 3; |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 148 | |
| 149 | t2ps.set('dlbls', newDeviceLabelIndex); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 150 | t2fs.updateNodes(); |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 151 | flash.flash(deviceLabelFlashMessage(newDeviceLabelIndex)); |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Steven Burrows | cc6f6de | 2017-08-30 12:50:38 +0100 | [diff] [blame] | 154 | function cycleHostLabels() { |
| 155 | var hostLabelIndex = t2ps.get('hlbls') + 1, |
| 156 | newHostLabelIndex = hostLabelIndex % 4; |
| 157 | |
| 158 | t2ps.set('hlbls', newHostLabelIndex); |
| 159 | t2fs.updateNodes(); |
| 160 | flash.flash(hostLabelFlashMessage(newHostLabelIndex)); |
| 161 | } |
| 162 | |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 163 | function toggleBackground(x) { |
| 164 | t2bgs.toggle(x); |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Steven Burrows | b8ebd40 | 2017-11-01 15:31:31 +0000 | [diff] [blame] | 167 | function toggleDetails(x) { |
| 168 | t2dp().toggleUseDetailsFlag(x); |
| 169 | } |
| 170 | |
Steven Burrows | 01ddf60 | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 171 | function toggleInstancePanel(x) { |
| 172 | updatePrefsState('insts', t2is.toggle(x)); |
| 173 | } |
| 174 | |
Steven Burrows | 5570d1b | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 175 | function toggleSummary() { |
| 176 | t2sp.toggle(); |
| 177 | } |
| 178 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 179 | function resetZoom() { |
Steven Burrows | 3bbd9af | 2017-03-16 14:44:14 +0000 | [diff] [blame] | 180 | t2bgs.resetZoom(); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 181 | flash.flash('Pan and zoom reset'); |
| 182 | } |
| 183 | |
| 184 | function togglePorts(x) { |
| 185 | updatePrefsState('porthl', t2vs.togglePortHighlights(x)); |
| 186 | t2fs.updateLinks(); |
| 187 | } |
| 188 | |
| 189 | function equalizeMasters() { |
| 190 | wss.sendEvent('equalizeMasters'); |
| 191 | flash.flash('Equalizing master roles'); |
| 192 | } |
| 193 | |
Steven Burrows | 892cba6 | 2017-03-10 16:31:48 +0000 | [diff] [blame] | 194 | function resetNodeLocation() { |
| 195 | t2fs.resetNodeLocation(); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 196 | flash.flash('Reset node locations'); |
| 197 | } |
| 198 | |
| 199 | function unpinNode() { |
| 200 | t2fs.unpin(); |
| 201 | flash.flash('Unpin node'); |
| 202 | } |
| 203 | |
Simon Hunt | 47f8fb7 | 2017-04-11 13:49:31 -0700 | [diff] [blame] | 204 | function toggleToolbar() { |
| 205 | t2tbs.toggle(); |
| 206 | } |
| 207 | |
Steven Burrows | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 208 | function actionedFlashed(action, message) { |
| 209 | flash.flash(action + ' ' + message); |
| 210 | } |
| 211 | |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 212 | function toggleHosts() { |
Steven Burrows | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 213 | var on = t2rs.toggleHosts(); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 214 | actionedFlashed(on ? 'Show': 'Hide', 'Hosts'); |
Steven Burrows | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | function toggleOfflineDevices() { |
| 218 | var on = t2rs.toggleOfflineDevices(); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 219 | actionedFlashed(on ? 'Show': 'Hide', 'offline devices'); |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 222 | function notValid(what) { |
| 223 | $log.warn('topo.js getActionEntry(): Not a valid ' + what); |
| 224 | } |
| 225 | |
| 226 | function getActionEntry(key) { |
| 227 | var entry; |
| 228 | |
| 229 | if (!key) { |
| 230 | notValid('key'); |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | entry = actionMap()[key]; |
| 235 | |
| 236 | if (!entry) { |
| 237 | notValid('actionMap (' + key + ') entry'); |
| 238 | return null; |
| 239 | } |
| 240 | return fs.isA(entry) || [entry, '']; |
| 241 | } |
| 242 | |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 243 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 244 | .factory('Topo2KeyCommandService', [ |
Simon Hunt | 47f8fb7 | 2017-04-11 13:49:31 -0700 | [diff] [blame] | 245 | '$log', 'FnService', 'KeyService', 'FlashService', 'WebSocketService', |
| 246 | 'Topo2PrefsService', 'Topo2BackgroundService', 'PrefsService', |
| 247 | 'Topo2InstanceService', 'Topo2SummaryPanelService', 'Topo2ViewService', |
Steven Burrows | 31e00c61 | 2018-02-06 10:38:57 +0000 | [diff] [blame] | 248 | 'Topo2RegionService', 'Topo2DetailsPanelService', 'SvgUtilService', |
Simon Hunt | 47f8fb7 | 2017-04-11 13:49:31 -0700 | [diff] [blame] | 249 | |
| 250 | function (_$log_, _fs_, _ks_, _flash_, _wss_, _t2ps_, _t2bgs_, _ps_, |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 251 | _t2is_, _t2sp_, _t2vs_, _t2rs_, _t2dp_, _t2tbs_, _sus_) { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 252 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 253 | $log = _$log_; |
| 254 | fs = _fs_; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 255 | ks = _ks_; |
| 256 | flash = _flash_; |
| 257 | wss = _wss_; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 258 | t2ps = _t2ps_; |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 259 | t2bgs = _t2bgs_; |
Steven Burrows | 01ddf60 | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 260 | t2is = _t2is_; |
| 261 | ps = _ps_; |
Steven Burrows | 5570d1b | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 262 | t2sp = _t2sp_; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 263 | t2vs = _t2vs_; |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 264 | t2rs = _t2rs_; |
Steven Burrows | b8ebd40 | 2017-11-01 15:31:31 +0000 | [diff] [blame] | 265 | t2dp = _t2dp_; |
Steven Burrows | 31e00c61 | 2018-02-06 10:38:57 +0000 | [diff] [blame] | 266 | sus = _sus_; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 267 | |
| 268 | return { |
| 269 | init: init, |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 270 | bindCommands: bindCommands, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 271 | getActionEntry: getActionEntry, |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 272 | }; |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 273 | }, |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 274 | ]); |
| 275 | })(); |