blob: 939b9d28e9663897f7bd309d07a030aa23eb3b69 [file] [log] [blame]
Steven Burrows37549ee2016-09-21 14:41:39 +01001/*
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 Hunt47f8fb72017-04-11 13:49:31 -070020 var $log, fs, ks, flash, wss, t2ps, t2bgs, ps, t2is, t2sp, t2vs, t2rs,
21 t2fs, t2sls, t2tbs;
Steven Burrows37549ee2016-09-21 14:41:39 +010022
23 // Commmands
Steven Burrowsc8468932017-03-17 16:13:41 +000024 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 Burrowsaea509d2017-04-12 14:17:47 -070035 H: [toggleHosts, 'Toggle host visibility'],
Steven Burrows02e67f42017-04-13 13:59:43 -070036 M: [toggleOfflineDevices, 'Toggle offline visibility'],
Simon Hunt47f8fb72017-04-11 13:49:31 -070037 dot: [toggleToolbar, 'Toggle Toolbar'],
Steven Burrowsc0efbf02016-11-16 11:30:47 -060038
Steven Burrowsc8468932017-03-17 16:13:41 +000039 esc: handleEscape,
40
41 _keyListener: t2tbs.keyListener.bind(t2tbs)
42 }
Simon Hunte6f64612017-04-28 00:01:48 -070043 }
Steven Burrows37549ee2016-09-21 14:41:39 +010044
Steven Burrowsc8468932017-03-17 16:13:41 +000045 function init(_t2fs_, _t2tbs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -050046 t2fs = _t2fs_;
Steven Burrowsc8468932017-03-17 16:13:41 +000047 t2tbs = _t2tbs_;
Steven Burrows37549ee2016-09-21 14:41:39 +010048 bindCommands();
49 }
50
Simon Hunte6f64612017-04-28 00:01:48 -070051 function bindCommands(additional) {
Steven Burrows37549ee2016-09-21 14:41:39 +010052
Simon Hunte6f64612017-04-28 00:01:48 -070053 var am = actionMap(),
54 add = fs.isO(additional);
55
56 if (add) {
57 _.each(add, function (value, key) {
58 // filter out meta properties (e.g. _keyOrder)
59 if (!(key.startsWith('_'))) {
60 // don't allow re-definition of existing key bindings
61 if (am[key]) {
62 $log.warn('keybind: ' + key + ' already exists');
63 } else {
64 am[key] = [value.cb, value.tt];
65 }
66 }
67 });
68 }
69
70 ks.keyBindings(am);
Steven Burrows37549ee2016-09-21 14:41:39 +010071
72 ks.gestureNotes([
73 ['click', 'Select the item and show details'],
74 ['shift-click', 'Toggle selection state'],
75 ['drag', 'Reposition (and pin) device / host'],
76 ['cmd-scroll', 'Zoom in / out'],
77 ['cmd-drag', 'Pan']
78 ]);
79 }
80
Steven Burrowsc0efbf02016-11-16 11:30:47 -060081 function handleEscape() {
Steven Burrows1aa4f582016-12-13 15:05:41 -050082
83 if (false) {
84 // TODO: Cancel show mastership
85 // TODO: Cancel Active overlay
86
87 } else if (t2rs.deselectAllNodes()) {
88 // else if we have node selections, deselect them all
89 // (work already done)
90 } else if (t2rs.deselectLink()) {
91 // else if we have a link selection, deselect it
92 // (work already done)
Steven Burrowsc0efbf02016-11-16 11:30:47 -060093 } else if (t2is.isVisible()) {
Steven Burrows1aa4f582016-12-13 15:05:41 -050094 // If the instance panel is visible, close it
95 t2is.toggle();
96 } else if (t2sp.isVisible()) {
97 // If the summary panel is visible, close it
98 t2sp.toggle();
99 }
Steven Burrowsc0efbf02016-11-16 11:30:47 -0600100 }
101
Steven Burrows01ddf602016-09-28 14:21:00 -0700102 var prefsState = {};
103
104 function updatePrefsState(what, b) {
105 prefsState[what] = b ? 1 : 0;
Simon Hunt95f4b422017-03-03 13:49:05 -0800106 ps.setPrefs('topo2_prefs', prefsState);
Steven Burrows01ddf602016-09-28 14:21:00 -0700107 }
108
Steven Burrows583f4be2016-11-04 14:06:50 +0100109 function deviceLabelFlashMessage(index) {
Steven Burrows583f4be2016-11-04 14:06:50 +0100110 switch (index) {
111 case 0: return 'Hide device labels';
112 case 1: return 'Show friendly device labels';
113 case 2: return 'Show device ID labels';
114 }
115 }
116
Steven Burrows37549ee2016-09-21 14:41:39 +0100117 function cycleDeviceLabels() {
Steven Burrows583f4be2016-11-04 14:06:50 +0100118 var deviceLabelIndex = t2ps.get('dlbls') + 1,
Steven Burrowsaf96a212016-12-28 12:57:02 +0000119 newDeviceLabelIndex = deviceLabelIndex % 3;
Steven Burrows583f4be2016-11-04 14:06:50 +0100120
121 t2ps.set('dlbls', newDeviceLabelIndex);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500122 t2fs.updateNodes();
Steven Burrows583f4be2016-11-04 14:06:50 +0100123 flash.flash(deviceLabelFlashMessage(newDeviceLabelIndex));
Steven Burrows37549ee2016-09-21 14:41:39 +0100124 }
125
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000126 function toggleBackground(x) {
127 t2bgs.toggle(x);
Steven Burrowse3a18842016-09-22 15:33:33 +0100128 }
129
Steven Burrows01ddf602016-09-28 14:21:00 -0700130 function toggleInstancePanel(x) {
131 updatePrefsState('insts', t2is.toggle(x));
132 }
133
Steven Burrows5570d1b2016-09-28 14:21:00 -0700134 function toggleSummary() {
135 t2sp.toggle();
136 }
137
Steven Burrows1c5c8612016-10-05 13:45:13 -0500138 function resetZoom() {
Steven Burrows3bbd9af2017-03-16 14:44:14 +0000139 t2bgs.resetZoom();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500140 flash.flash('Pan and zoom reset');
141 }
142
143 function togglePorts(x) {
144 updatePrefsState('porthl', t2vs.togglePortHighlights(x));
145 t2fs.updateLinks();
146 }
147
148 function equalizeMasters() {
149 wss.sendEvent('equalizeMasters');
150 flash.flash('Equalizing master roles');
151 }
152
Steven Burrows892cba62017-03-10 16:31:48 +0000153 function resetNodeLocation() {
154 t2fs.resetNodeLocation();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500155 flash.flash('Reset node locations');
156 }
157
158 function unpinNode() {
159 t2fs.unpin();
160 flash.flash('Unpin node');
161 }
162
Simon Hunt47f8fb72017-04-11 13:49:31 -0700163 function toggleToolbar() {
164 t2tbs.toggle();
165 }
166
Steven Burrows02e67f42017-04-13 13:59:43 -0700167 function actionedFlashed(action, message) {
168 flash.flash(action + ' ' + message);
169 }
170
Steven Burrowsaea509d2017-04-12 14:17:47 -0700171 function toggleHosts() {
Steven Burrows02e67f42017-04-13 13:59:43 -0700172 var on = t2rs.toggleHosts();
173 actionedFlashed(on ? 'Show': 'Hide', 'Hosts')
174 }
175
176 function toggleOfflineDevices() {
177 var on = t2rs.toggleOfflineDevices();
178 actionedFlashed(on ? 'Show': 'Hide', 'offline devices')
Steven Burrowsaea509d2017-04-12 14:17:47 -0700179 }
180
Steven Burrowsc8468932017-03-17 16:13:41 +0000181 function notValid(what) {
182 $log.warn('topo.js getActionEntry(): Not a valid ' + what);
183 }
184
185 function getActionEntry(key) {
186 var entry;
187
188 if (!key) {
189 notValid('key');
190 return null;
191 }
192
193 entry = actionMap()[key];
194
195 if (!entry) {
196 notValid('actionMap (' + key + ') entry');
197 return null;
198 }
199 return fs.isA(entry) || [entry, ''];
200 }
201
Steven Burrows37549ee2016-09-21 14:41:39 +0100202 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000203 .factory('Topo2KeyCommandService', [
Simon Hunt47f8fb72017-04-11 13:49:31 -0700204 '$log', 'FnService', 'KeyService', 'FlashService', 'WebSocketService',
205 'Topo2PrefsService', 'Topo2BackgroundService', 'PrefsService',
206 'Topo2InstanceService', 'Topo2SummaryPanelService', 'Topo2ViewService',
207 'Topo2RegionService', 'Topo2SpriteLayerService',
208
209 function (_$log_, _fs_, _ks_, _flash_, _wss_, _t2ps_, _t2bgs_, _ps_,
210 _t2is_, _t2sp_, _t2vs_, _t2rs_, _t2sls_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500211
Steven Burrowsc8468932017-03-17 16:13:41 +0000212 $log = _$log_;
213 fs = _fs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500214 ks = _ks_;
215 flash = _flash_;
216 wss = _wss_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100217 t2ps = _t2ps_;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000218 t2bgs = _t2bgs_;
Steven Burrows01ddf602016-09-28 14:21:00 -0700219 t2is = _t2is_;
220 ps = _ps_;
Steven Burrows5570d1b2016-09-28 14:21:00 -0700221 t2sp = _t2sp_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500222 t2vs = _t2vs_;
Steven Burrows1aa4f582016-12-13 15:05:41 -0500223 t2rs = _t2rs_;
Steven Burrowsea1d1ec2017-02-23 15:39:25 +0000224 t2sls = _t2sls_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100225
226 return {
227 init: init,
Steven Burrowsc8468932017-03-17 16:13:41 +0000228 bindCommands: bindCommands,
229 getActionEntry: getActionEntry
Steven Burrows37549ee2016-09-21 14:41:39 +0100230 };
231 }
232 ]);
233})();