blob: b396f30d2da0ac8dd58a1fe2bd42197813f592d1 [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 Burrows1c2a9682017-07-14 16:52:46 +010021 t2fs, 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 Burrowscc6f6de2017-08-30 12:50:38 +010038 'shift-L': [cycleHostLabels, 'Cycle host labels'],
Steven Burrowsc0efbf02016-11-16 11:30:47 -060039
Steven Burrowsc8468932017-03-17 16:13:41 +000040 esc: handleEscape,
41
Steven Burrows1c2a9682017-07-14 16:52:46 +010042 _keyListener: t2tbs.keyListener.bind(t2tbs),
43 };
Simon Hunte6f64612017-04-28 00:01:48 -070044 }
Steven Burrows37549ee2016-09-21 14:41:39 +010045
Steven Burrowsc8468932017-03-17 16:13:41 +000046 function init(_t2fs_, _t2tbs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -050047 t2fs = _t2fs_;
Steven Burrowsc8468932017-03-17 16:13:41 +000048 t2tbs = _t2tbs_;
Steven Burrows37549ee2016-09-21 14:41:39 +010049 bindCommands();
50 }
51
Simon Hunte6f64612017-04-28 00:01:48 -070052 function bindCommands(additional) {
Steven Burrows37549ee2016-09-21 14:41:39 +010053
Simon Hunte6f64612017-04-28 00:01:48 -070054 var am = actionMap(),
55 add = fs.isO(additional);
56
57 if (add) {
58 _.each(add, function (value, key) {
59 // filter out meta properties (e.g. _keyOrder)
Steven Burrows202034f2017-08-11 12:08:01 +010060 if (!(_.startsWith(key, '_'))) {
Simon Hunte6f64612017-04-28 00:01:48 -070061 // don't allow re-definition of existing key bindings
62 if (am[key]) {
63 $log.warn('keybind: ' + key + ' already exists');
64 } else {
65 am[key] = [value.cb, value.tt];
66 }
67 }
68 });
69 }
70
71 ks.keyBindings(am);
Steven Burrows37549ee2016-09-21 14:41:39 +010072
73 ks.gestureNotes([
74 ['click', 'Select the item and show details'],
75 ['shift-click', 'Toggle selection state'],
76 ['drag', 'Reposition (and pin) device / host'],
77 ['cmd-scroll', 'Zoom in / out'],
Steven Burrows1c2a9682017-07-14 16:52:46 +010078 ['cmd-drag', 'Pan'],
Steven Burrows37549ee2016-09-21 14:41:39 +010079 ]);
80 }
81
Steven Burrowsc0efbf02016-11-16 11:30:47 -060082 function handleEscape() {
Steven Burrows1aa4f582016-12-13 15:05:41 -050083
84 if (false) {
85 // TODO: Cancel show mastership
86 // TODO: Cancel Active overlay
87
88 } else if (t2rs.deselectAllNodes()) {
89 // else if we have node selections, deselect them all
90 // (work already done)
91 } else if (t2rs.deselectLink()) {
92 // else if we have a link selection, deselect it
93 // (work already done)
Steven Burrowsc0efbf02016-11-16 11:30:47 -060094 } else if (t2is.isVisible()) {
Steven Burrows1aa4f582016-12-13 15:05:41 -050095 // If the instance panel is visible, close it
96 t2is.toggle();
97 } else if (t2sp.isVisible()) {
98 // If the summary panel is visible, close it
99 t2sp.toggle();
100 }
Steven Burrowsc0efbf02016-11-16 11:30:47 -0600101 }
102
Steven Burrows01ddf602016-09-28 14:21:00 -0700103 var prefsState = {};
104
105 function updatePrefsState(what, b) {
106 prefsState[what] = b ? 1 : 0;
Simon Hunt95f4b422017-03-03 13:49:05 -0800107 ps.setPrefs('topo2_prefs', prefsState);
Steven Burrows01ddf602016-09-28 14:21:00 -0700108 }
109
Steven Burrows583f4be2016-11-04 14:06:50 +0100110 function deviceLabelFlashMessage(index) {
Steven Burrows583f4be2016-11-04 14:06:50 +0100111 switch (index) {
112 case 0: return 'Hide device labels';
113 case 1: return 'Show friendly device labels';
114 case 2: return 'Show device ID labels';
115 }
116 }
117
Steven Burrowscc6f6de2017-08-30 12:50:38 +0100118 function hostLabelFlashMessage(index) {
119 switch (index) {
120 case 0: return 'Hide host labels';
121 case 1: return 'Show friendly host labels';
122 case 2: return 'Show host IP labels';
123 case 3: return 'Show host MAC Address labels';
124 }
125 }
126
Steven Burrows37549ee2016-09-21 14:41:39 +0100127 function cycleDeviceLabels() {
Steven Burrows583f4be2016-11-04 14:06:50 +0100128 var deviceLabelIndex = t2ps.get('dlbls') + 1,
Steven Burrowsaf96a212016-12-28 12:57:02 +0000129 newDeviceLabelIndex = deviceLabelIndex % 3;
Steven Burrows583f4be2016-11-04 14:06:50 +0100130
131 t2ps.set('dlbls', newDeviceLabelIndex);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500132 t2fs.updateNodes();
Steven Burrows583f4be2016-11-04 14:06:50 +0100133 flash.flash(deviceLabelFlashMessage(newDeviceLabelIndex));
Steven Burrows37549ee2016-09-21 14:41:39 +0100134 }
135
Steven Burrowscc6f6de2017-08-30 12:50:38 +0100136 function cycleHostLabels() {
137 var hostLabelIndex = t2ps.get('hlbls') + 1,
138 newHostLabelIndex = hostLabelIndex % 4;
139
140 t2ps.set('hlbls', newHostLabelIndex);
141 t2fs.updateNodes();
142 flash.flash(hostLabelFlashMessage(newHostLabelIndex));
143 }
144
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000145 function toggleBackground(x) {
146 t2bgs.toggle(x);
Steven Burrowse3a18842016-09-22 15:33:33 +0100147 }
148
Steven Burrows01ddf602016-09-28 14:21:00 -0700149 function toggleInstancePanel(x) {
150 updatePrefsState('insts', t2is.toggle(x));
151 }
152
Steven Burrows5570d1b2016-09-28 14:21:00 -0700153 function toggleSummary() {
154 t2sp.toggle();
155 }
156
Steven Burrows1c5c8612016-10-05 13:45:13 -0500157 function resetZoom() {
Steven Burrows3bbd9af2017-03-16 14:44:14 +0000158 t2bgs.resetZoom();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500159 flash.flash('Pan and zoom reset');
160 }
161
162 function togglePorts(x) {
163 updatePrefsState('porthl', t2vs.togglePortHighlights(x));
164 t2fs.updateLinks();
165 }
166
167 function equalizeMasters() {
168 wss.sendEvent('equalizeMasters');
169 flash.flash('Equalizing master roles');
170 }
171
Steven Burrows892cba62017-03-10 16:31:48 +0000172 function resetNodeLocation() {
173 t2fs.resetNodeLocation();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500174 flash.flash('Reset node locations');
175 }
176
177 function unpinNode() {
178 t2fs.unpin();
179 flash.flash('Unpin node');
180 }
181
Simon Hunt47f8fb72017-04-11 13:49:31 -0700182 function toggleToolbar() {
183 t2tbs.toggle();
184 }
185
Steven Burrows02e67f42017-04-13 13:59:43 -0700186 function actionedFlashed(action, message) {
187 flash.flash(action + ' ' + message);
188 }
189
Steven Burrowsaea509d2017-04-12 14:17:47 -0700190 function toggleHosts() {
Steven Burrows02e67f42017-04-13 13:59:43 -0700191 var on = t2rs.toggleHosts();
Steven Burrows1c2a9682017-07-14 16:52:46 +0100192 actionedFlashed(on ? 'Show': 'Hide', 'Hosts');
Steven Burrows02e67f42017-04-13 13:59:43 -0700193 }
194
195 function toggleOfflineDevices() {
196 var on = t2rs.toggleOfflineDevices();
Steven Burrows1c2a9682017-07-14 16:52:46 +0100197 actionedFlashed(on ? 'Show': 'Hide', 'offline devices');
Steven Burrowsaea509d2017-04-12 14:17:47 -0700198 }
199
Steven Burrowsc8468932017-03-17 16:13:41 +0000200 function notValid(what) {
201 $log.warn('topo.js getActionEntry(): Not a valid ' + what);
202 }
203
204 function getActionEntry(key) {
205 var entry;
206
207 if (!key) {
208 notValid('key');
209 return null;
210 }
211
212 entry = actionMap()[key];
213
214 if (!entry) {
215 notValid('actionMap (' + key + ') entry');
216 return null;
217 }
218 return fs.isA(entry) || [entry, ''];
219 }
220
Steven Burrows37549ee2016-09-21 14:41:39 +0100221 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000222 .factory('Topo2KeyCommandService', [
Simon Hunt47f8fb72017-04-11 13:49:31 -0700223 '$log', 'FnService', 'KeyService', 'FlashService', 'WebSocketService',
224 'Topo2PrefsService', 'Topo2BackgroundService', 'PrefsService',
225 'Topo2InstanceService', 'Topo2SummaryPanelService', 'Topo2ViewService',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100226 'Topo2RegionService',
Simon Hunt47f8fb72017-04-11 13:49:31 -0700227
228 function (_$log_, _fs_, _ks_, _flash_, _wss_, _t2ps_, _t2bgs_, _ps_,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100229 _t2is_, _t2sp_, _t2vs_, _t2rs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500230
Steven Burrowsc8468932017-03-17 16:13:41 +0000231 $log = _$log_;
232 fs = _fs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500233 ks = _ks_;
234 flash = _flash_;
235 wss = _wss_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100236 t2ps = _t2ps_;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000237 t2bgs = _t2bgs_;
Steven Burrows01ddf602016-09-28 14:21:00 -0700238 t2is = _t2is_;
239 ps = _ps_;
Steven Burrows5570d1b2016-09-28 14:21:00 -0700240 t2sp = _t2sp_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500241 t2vs = _t2vs_;
Steven Burrows1aa4f582016-12-13 15:05:41 -0500242 t2rs = _t2rs_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100243
244 return {
245 init: init,
Steven Burrowsc8468932017-03-17 16:13:41 +0000246 bindCommands: bindCommands,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100247 getActionEntry: getActionEntry,
Steven Burrows37549ee2016-09-21 14:41:39 +0100248 };
Steven Burrows1c2a9682017-07-14 16:52:46 +0100249 },
Steven Burrows37549ee2016-09-21 14:41:39 +0100250 ]);
251})();