blob: 5fd90187b4277afe9f5f48c14e39eb4104ea56f5 [file] [log] [blame]
Steven Burrows37549ee2016-09-21 14:41:39 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002* Copyright 2016-present Open Networking Foundation
Steven Burrows37549ee2016-09-21 14:41:39 +01003*
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,
Steven Burrowsb8ebd402017-11-01 15:31:31 +000021 t2fs, t2tbs, t2dp;
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'],
Steven Burrowsb8ebd402017-11-01 15:31:31 +000028 D: [toggleDetails, 'Toggle details panel'],
Steven Burrowsc8468932017-03-17 16:13:41 +000029 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 Burrowsaea509d2017-04-12 14:17:47 -070036 H: [toggleHosts, 'Toggle host visibility'],
Steven Burrows02e67f42017-04-13 13:59:43 -070037 M: [toggleOfflineDevices, 'Toggle offline visibility'],
Simon Hunt47f8fb72017-04-11 13:49:31 -070038 dot: [toggleToolbar, 'Toggle Toolbar'],
Steven Burrowscc6f6de2017-08-30 12:50:38 +010039 'shift-L': [cycleHostLabels, 'Cycle host labels'],
Steven Burrowsc0efbf02016-11-16 11:30:47 -060040
Steven Burrowsc8468932017-03-17 16:13:41 +000041 esc: handleEscape,
42
Steven Burrows1c2a9682017-07-14 16:52:46 +010043 _keyListener: t2tbs.keyListener.bind(t2tbs),
Steven Burrowsa58cb822017-11-01 16:15:13 +000044
45 _helpFormat: [
46 ['I', 'O', 'D', 'H', 'M', 'P', 'dash', 'B'],
47 ['X', 'Z', 'N', 'L', 'shift-L', 'U', 'R', 'E', 'dot'],
48 [], // this column reserved for overlay actions
49 ],
Steven Burrows1c2a9682017-07-14 16:52:46 +010050 };
Simon Hunte6f64612017-04-28 00:01:48 -070051 }
Steven Burrows37549ee2016-09-21 14:41:39 +010052
Steven Burrowsc8468932017-03-17 16:13:41 +000053 function init(_t2fs_, _t2tbs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -050054 t2fs = _t2fs_;
Steven Burrowsc8468932017-03-17 16:13:41 +000055 t2tbs = _t2tbs_;
Steven Burrows37549ee2016-09-21 14:41:39 +010056 bindCommands();
57 }
58
Simon Hunte6f64612017-04-28 00:01:48 -070059 function bindCommands(additional) {
Steven Burrows37549ee2016-09-21 14:41:39 +010060
Simon Hunte6f64612017-04-28 00:01:48 -070061 var am = actionMap(),
62 add = fs.isO(additional);
63
64 if (add) {
65 _.each(add, function (value, key) {
66 // filter out meta properties (e.g. _keyOrder)
Steven Burrows202034f2017-08-11 12:08:01 +010067 if (!(_.startsWith(key, '_'))) {
Simon Hunte6f64612017-04-28 00:01:48 -070068 // don't allow re-definition of existing key bindings
69 if (am[key]) {
70 $log.warn('keybind: ' + key + ' already exists');
71 } else {
72 am[key] = [value.cb, value.tt];
73 }
74 }
75 });
76 }
77
78 ks.keyBindings(am);
Steven Burrows37549ee2016-09-21 14:41:39 +010079
80 ks.gestureNotes([
81 ['click', 'Select the item and show details'],
82 ['shift-click', 'Toggle selection state'],
83 ['drag', 'Reposition (and pin) device / host'],
84 ['cmd-scroll', 'Zoom in / out'],
Steven Burrows1c2a9682017-07-14 16:52:46 +010085 ['cmd-drag', 'Pan'],
Steven Burrows37549ee2016-09-21 14:41:39 +010086 ]);
87 }
88
Steven Burrowsc0efbf02016-11-16 11:30:47 -060089 function handleEscape() {
Steven Burrows1aa4f582016-12-13 15:05:41 -050090
91 if (false) {
92 // TODO: Cancel show mastership
93 // TODO: Cancel Active overlay
94
95 } else if (t2rs.deselectAllNodes()) {
96 // else if we have node selections, deselect them all
97 // (work already done)
98 } else if (t2rs.deselectLink()) {
99 // else if we have a link selection, deselect it
100 // (work already done)
Steven Burrowsc0efbf02016-11-16 11:30:47 -0600101 } else if (t2is.isVisible()) {
Steven Burrows1aa4f582016-12-13 15:05:41 -0500102 // If the instance panel is visible, close it
103 t2is.toggle();
104 } else if (t2sp.isVisible()) {
105 // If the summary panel is visible, close it
106 t2sp.toggle();
107 }
Steven Burrowsc0efbf02016-11-16 11:30:47 -0600108 }
109
Steven Burrows01ddf602016-09-28 14:21:00 -0700110 var prefsState = {};
111
112 function updatePrefsState(what, b) {
113 prefsState[what] = b ? 1 : 0;
Simon Hunt95f4b422017-03-03 13:49:05 -0800114 ps.setPrefs('topo2_prefs', prefsState);
Steven Burrows01ddf602016-09-28 14:21:00 -0700115 }
116
Steven Burrows583f4be2016-11-04 14:06:50 +0100117 function deviceLabelFlashMessage(index) {
Steven Burrows583f4be2016-11-04 14:06:50 +0100118 switch (index) {
119 case 0: return 'Hide device labels';
120 case 1: return 'Show friendly device labels';
121 case 2: return 'Show device ID labels';
122 }
123 }
124
Steven Burrowscc6f6de2017-08-30 12:50:38 +0100125 function hostLabelFlashMessage(index) {
126 switch (index) {
127 case 0: return 'Hide host labels';
128 case 1: return 'Show friendly host labels';
129 case 2: return 'Show host IP labels';
130 case 3: return 'Show host MAC Address labels';
131 }
132 }
133
Steven Burrows37549ee2016-09-21 14:41:39 +0100134 function cycleDeviceLabels() {
Steven Burrows583f4be2016-11-04 14:06:50 +0100135 var deviceLabelIndex = t2ps.get('dlbls') + 1,
Steven Burrowsaf96a212016-12-28 12:57:02 +0000136 newDeviceLabelIndex = deviceLabelIndex % 3;
Steven Burrows583f4be2016-11-04 14:06:50 +0100137
138 t2ps.set('dlbls', newDeviceLabelIndex);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500139 t2fs.updateNodes();
Steven Burrows583f4be2016-11-04 14:06:50 +0100140 flash.flash(deviceLabelFlashMessage(newDeviceLabelIndex));
Steven Burrows37549ee2016-09-21 14:41:39 +0100141 }
142
Steven Burrowscc6f6de2017-08-30 12:50:38 +0100143 function cycleHostLabels() {
144 var hostLabelIndex = t2ps.get('hlbls') + 1,
145 newHostLabelIndex = hostLabelIndex % 4;
146
147 t2ps.set('hlbls', newHostLabelIndex);
148 t2fs.updateNodes();
149 flash.flash(hostLabelFlashMessage(newHostLabelIndex));
150 }
151
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000152 function toggleBackground(x) {
153 t2bgs.toggle(x);
Steven Burrowse3a18842016-09-22 15:33:33 +0100154 }
155
Steven Burrowsb8ebd402017-11-01 15:31:31 +0000156 function toggleDetails(x) {
157 t2dp().toggleUseDetailsFlag(x);
158 }
159
Steven Burrows01ddf602016-09-28 14:21:00 -0700160 function toggleInstancePanel(x) {
161 updatePrefsState('insts', t2is.toggle(x));
162 }
163
Steven Burrows5570d1b2016-09-28 14:21:00 -0700164 function toggleSummary() {
165 t2sp.toggle();
166 }
167
Steven Burrows1c5c8612016-10-05 13:45:13 -0500168 function resetZoom() {
Steven Burrows3bbd9af2017-03-16 14:44:14 +0000169 t2bgs.resetZoom();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500170 flash.flash('Pan and zoom reset');
171 }
172
173 function togglePorts(x) {
174 updatePrefsState('porthl', t2vs.togglePortHighlights(x));
175 t2fs.updateLinks();
176 }
177
178 function equalizeMasters() {
179 wss.sendEvent('equalizeMasters');
180 flash.flash('Equalizing master roles');
181 }
182
Steven Burrows892cba62017-03-10 16:31:48 +0000183 function resetNodeLocation() {
184 t2fs.resetNodeLocation();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500185 flash.flash('Reset node locations');
186 }
187
188 function unpinNode() {
189 t2fs.unpin();
190 flash.flash('Unpin node');
191 }
192
Simon Hunt47f8fb72017-04-11 13:49:31 -0700193 function toggleToolbar() {
194 t2tbs.toggle();
195 }
196
Steven Burrows02e67f42017-04-13 13:59:43 -0700197 function actionedFlashed(action, message) {
198 flash.flash(action + ' ' + message);
199 }
200
Steven Burrowsaea509d2017-04-12 14:17:47 -0700201 function toggleHosts() {
Steven Burrows02e67f42017-04-13 13:59:43 -0700202 var on = t2rs.toggleHosts();
Steven Burrows1c2a9682017-07-14 16:52:46 +0100203 actionedFlashed(on ? 'Show': 'Hide', 'Hosts');
Steven Burrows02e67f42017-04-13 13:59:43 -0700204 }
205
206 function toggleOfflineDevices() {
207 var on = t2rs.toggleOfflineDevices();
Steven Burrows1c2a9682017-07-14 16:52:46 +0100208 actionedFlashed(on ? 'Show': 'Hide', 'offline devices');
Steven Burrowsaea509d2017-04-12 14:17:47 -0700209 }
210
Steven Burrowsc8468932017-03-17 16:13:41 +0000211 function notValid(what) {
212 $log.warn('topo.js getActionEntry(): Not a valid ' + what);
213 }
214
215 function getActionEntry(key) {
216 var entry;
217
218 if (!key) {
219 notValid('key');
220 return null;
221 }
222
223 entry = actionMap()[key];
224
225 if (!entry) {
226 notValid('actionMap (' + key + ') entry');
227 return null;
228 }
229 return fs.isA(entry) || [entry, ''];
230 }
231
Steven Burrows37549ee2016-09-21 14:41:39 +0100232 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000233 .factory('Topo2KeyCommandService', [
Simon Hunt47f8fb72017-04-11 13:49:31 -0700234 '$log', 'FnService', 'KeyService', 'FlashService', 'WebSocketService',
235 'Topo2PrefsService', 'Topo2BackgroundService', 'PrefsService',
236 'Topo2InstanceService', 'Topo2SummaryPanelService', 'Topo2ViewService',
Steven Burrowsb8ebd402017-11-01 15:31:31 +0000237 'Topo2RegionService', 'Topo2DetailsPanelService',
Simon Hunt47f8fb72017-04-11 13:49:31 -0700238
239 function (_$log_, _fs_, _ks_, _flash_, _wss_, _t2ps_, _t2bgs_, _ps_,
Steven Burrowsb8ebd402017-11-01 15:31:31 +0000240 _t2is_, _t2sp_, _t2vs_, _t2rs_, _t2dp_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500241
Steven Burrowsc8468932017-03-17 16:13:41 +0000242 $log = _$log_;
243 fs = _fs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500244 ks = _ks_;
245 flash = _flash_;
246 wss = _wss_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100247 t2ps = _t2ps_;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000248 t2bgs = _t2bgs_;
Steven Burrows01ddf602016-09-28 14:21:00 -0700249 t2is = _t2is_;
250 ps = _ps_;
Steven Burrows5570d1b2016-09-28 14:21:00 -0700251 t2sp = _t2sp_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500252 t2vs = _t2vs_;
Steven Burrows1aa4f582016-12-13 15:05:41 -0500253 t2rs = _t2rs_;
Steven Burrowsb8ebd402017-11-01 15:31:31 +0000254 t2dp = _t2dp_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100255
256 return {
257 init: init,
Steven Burrowsc8468932017-03-17 16:13:41 +0000258 bindCommands: bindCommands,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100259 getActionEntry: getActionEntry,
Steven Burrows37549ee2016-09-21 14:41:39 +0100260 };
Steven Burrows1c2a9682017-07-14 16:52:46 +0100261 },
Steven Burrows37549ee2016-09-21 14:41:39 +0100262 ]);
263})();