Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | (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, |
| 21 | t2fs, t2sls, t2tbs; |
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'], |
| 28 | I: [toggleInstancePanel, 'Toggle ONOS Instance Panel'], |
| 29 | O: [toggleSummary, 'Toggle the Summary Panel'], |
| 30 | R: [resetZoom, 'Reset pan / zoom'], |
| 31 | P: [togglePorts, 'Toggle Port Highlighting'], |
| 32 | E: [equalizeMasters, 'Equalize mastership roles'], |
| 33 | X: [resetNodeLocation, 'Reset Node Location'], |
| 34 | U: [unpinNode, 'Unpin node (mouse over)'], |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 35 | H: [toggleHosts, 'Toggle host visibility'], |
Steven Burrows | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 36 | M: [toggleOfflineDevices, 'Toggle offline visibility'], |
Simon Hunt | 47f8fb7 | 2017-04-11 13:49:31 -0700 | [diff] [blame] | 37 | dot: [toggleToolbar, 'Toggle Toolbar'], |
Steven Burrows | c0efbf0 | 2016-11-16 11:30:47 -0600 | [diff] [blame] | 38 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 39 | esc: handleEscape, |
| 40 | |
| 41 | _keyListener: t2tbs.keyListener.bind(t2tbs) |
| 42 | } |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 45 | function init(_t2fs_, _t2tbs_) { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 46 | t2fs = _t2fs_; |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 47 | t2tbs = _t2tbs_; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 48 | bindCommands(); |
| 49 | } |
| 50 | |
| 51 | function bindCommands() { |
| 52 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 53 | ks.keyBindings(actionMap()); |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 54 | |
| 55 | ks.gestureNotes([ |
| 56 | ['click', 'Select the item and show details'], |
| 57 | ['shift-click', 'Toggle selection state'], |
| 58 | ['drag', 'Reposition (and pin) device / host'], |
| 59 | ['cmd-scroll', 'Zoom in / out'], |
| 60 | ['cmd-drag', 'Pan'] |
| 61 | ]); |
| 62 | } |
| 63 | |
Steven Burrows | c0efbf0 | 2016-11-16 11:30:47 -0600 | [diff] [blame] | 64 | function handleEscape() { |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 65 | |
| 66 | if (false) { |
| 67 | // TODO: Cancel show mastership |
| 68 | // TODO: Cancel Active overlay |
| 69 | |
| 70 | } else if (t2rs.deselectAllNodes()) { |
| 71 | // else if we have node selections, deselect them all |
| 72 | // (work already done) |
| 73 | } else if (t2rs.deselectLink()) { |
| 74 | // else if we have a link selection, deselect it |
| 75 | // (work already done) |
Steven Burrows | c0efbf0 | 2016-11-16 11:30:47 -0600 | [diff] [blame] | 76 | } else if (t2is.isVisible()) { |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 77 | // If the instance panel is visible, close it |
| 78 | t2is.toggle(); |
| 79 | } else if (t2sp.isVisible()) { |
| 80 | // If the summary panel is visible, close it |
| 81 | t2sp.toggle(); |
| 82 | } |
Steven Burrows | c0efbf0 | 2016-11-16 11:30:47 -0600 | [diff] [blame] | 83 | } |
| 84 | |
Steven Burrows | 01ddf60 | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 85 | var prefsState = {}; |
| 86 | |
| 87 | function updatePrefsState(what, b) { |
| 88 | prefsState[what] = b ? 1 : 0; |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 89 | ps.setPrefs('topo2_prefs', prefsState); |
Steven Burrows | 01ddf60 | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 92 | function deviceLabelFlashMessage(index) { |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 93 | switch (index) { |
| 94 | case 0: return 'Hide device labels'; |
| 95 | case 1: return 'Show friendly device labels'; |
| 96 | case 2: return 'Show device ID labels'; |
| 97 | } |
| 98 | } |
| 99 | |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 100 | function cycleDeviceLabels() { |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 101 | var deviceLabelIndex = t2ps.get('dlbls') + 1, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 102 | newDeviceLabelIndex = deviceLabelIndex % 3; |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 103 | |
| 104 | t2ps.set('dlbls', newDeviceLabelIndex); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 105 | t2fs.updateNodes(); |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 106 | flash.flash(deviceLabelFlashMessage(newDeviceLabelIndex)); |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 109 | function toggleBackground(x) { |
| 110 | t2bgs.toggle(x); |
Steven Burrows | e3a1884 | 2016-09-22 15:33:33 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Steven Burrows | 01ddf60 | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 113 | function toggleInstancePanel(x) { |
| 114 | updatePrefsState('insts', t2is.toggle(x)); |
| 115 | } |
| 116 | |
Steven Burrows | 5570d1b | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 117 | function toggleSummary() { |
| 118 | t2sp.toggle(); |
| 119 | } |
| 120 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 121 | function resetZoom() { |
Steven Burrows | 3bbd9af | 2017-03-16 14:44:14 +0000 | [diff] [blame] | 122 | t2bgs.resetZoom(); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 123 | flash.flash('Pan and zoom reset'); |
| 124 | } |
| 125 | |
| 126 | function togglePorts(x) { |
| 127 | updatePrefsState('porthl', t2vs.togglePortHighlights(x)); |
| 128 | t2fs.updateLinks(); |
| 129 | } |
| 130 | |
| 131 | function equalizeMasters() { |
| 132 | wss.sendEvent('equalizeMasters'); |
| 133 | flash.flash('Equalizing master roles'); |
| 134 | } |
| 135 | |
Steven Burrows | 892cba6 | 2017-03-10 16:31:48 +0000 | [diff] [blame] | 136 | function resetNodeLocation() { |
| 137 | t2fs.resetNodeLocation(); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 138 | flash.flash('Reset node locations'); |
| 139 | } |
| 140 | |
| 141 | function unpinNode() { |
| 142 | t2fs.unpin(); |
| 143 | flash.flash('Unpin node'); |
| 144 | } |
| 145 | |
Simon Hunt | 47f8fb7 | 2017-04-11 13:49:31 -0700 | [diff] [blame] | 146 | function toggleToolbar() { |
| 147 | t2tbs.toggle(); |
| 148 | } |
| 149 | |
Steven Burrows | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 150 | function actionedFlashed(action, message) { |
| 151 | flash.flash(action + ' ' + message); |
| 152 | } |
| 153 | |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 154 | function toggleHosts() { |
Steven Burrows | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 155 | var on = t2rs.toggleHosts(); |
| 156 | actionedFlashed(on ? 'Show': 'Hide', 'Hosts') |
| 157 | } |
| 158 | |
| 159 | function toggleOfflineDevices() { |
| 160 | var on = t2rs.toggleOfflineDevices(); |
| 161 | actionedFlashed(on ? 'Show': 'Hide', 'offline devices') |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 164 | function notValid(what) { |
| 165 | $log.warn('topo.js getActionEntry(): Not a valid ' + what); |
| 166 | } |
| 167 | |
| 168 | function getActionEntry(key) { |
| 169 | var entry; |
| 170 | |
| 171 | if (!key) { |
| 172 | notValid('key'); |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | entry = actionMap()[key]; |
| 177 | |
| 178 | if (!entry) { |
| 179 | notValid('actionMap (' + key + ') entry'); |
| 180 | return null; |
| 181 | } |
| 182 | return fs.isA(entry) || [entry, '']; |
| 183 | } |
| 184 | |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 185 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 186 | .factory('Topo2KeyCommandService', [ |
Simon Hunt | 47f8fb7 | 2017-04-11 13:49:31 -0700 | [diff] [blame] | 187 | '$log', 'FnService', 'KeyService', 'FlashService', 'WebSocketService', |
| 188 | 'Topo2PrefsService', 'Topo2BackgroundService', 'PrefsService', |
| 189 | 'Topo2InstanceService', 'Topo2SummaryPanelService', 'Topo2ViewService', |
| 190 | 'Topo2RegionService', 'Topo2SpriteLayerService', |
| 191 | |
| 192 | function (_$log_, _fs_, _ks_, _flash_, _wss_, _t2ps_, _t2bgs_, _ps_, |
| 193 | _t2is_, _t2sp_, _t2vs_, _t2rs_, _t2sls_) { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 194 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 195 | $log = _$log_; |
| 196 | fs = _fs_; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 197 | ks = _ks_; |
| 198 | flash = _flash_; |
| 199 | wss = _wss_; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 200 | t2ps = _t2ps_; |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 201 | t2bgs = _t2bgs_; |
Steven Burrows | 01ddf60 | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 202 | t2is = _t2is_; |
| 203 | ps = _ps_; |
Steven Burrows | 5570d1b | 2016-09-28 14:21:00 -0700 | [diff] [blame] | 204 | t2sp = _t2sp_; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 205 | t2vs = _t2vs_; |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 206 | t2rs = _t2rs_; |
Steven Burrows | ea1d1ec | 2017-02-23 15:39:25 +0000 | [diff] [blame] | 207 | t2sls = _t2sls_; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 208 | |
| 209 | return { |
| 210 | init: init, |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 211 | bindCommands: bindCommands, |
| 212 | getActionEntry: getActionEntry |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 213 | }; |
| 214 | } |
| 215 | ]); |
| 216 | })(); |