blob: 8dab3ce33575ad3b9e152a9192b9fd31db841798 [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'],
Simon Hunt47f8fb72017-04-11 13:49:31 -070036 dot: [toggleToolbar, 'Toggle Toolbar'],
Steven Burrowsc0efbf02016-11-16 11:30:47 -060037
Steven Burrowsc8468932017-03-17 16:13:41 +000038 esc: handleEscape,
39
40 _keyListener: t2tbs.keyListener.bind(t2tbs)
41 }
Steven Burrows37549ee2016-09-21 14:41:39 +010042 };
43
Steven Burrowsc8468932017-03-17 16:13:41 +000044 function init(_t2fs_, _t2tbs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -050045 t2fs = _t2fs_;
Steven Burrowsc8468932017-03-17 16:13:41 +000046 t2tbs = _t2tbs_;
Steven Burrows37549ee2016-09-21 14:41:39 +010047 bindCommands();
48 }
49
50 function bindCommands() {
51
Steven Burrowsc8468932017-03-17 16:13:41 +000052 ks.keyBindings(actionMap());
Steven Burrows37549ee2016-09-21 14:41:39 +010053
54 ks.gestureNotes([
55 ['click', 'Select the item and show details'],
56 ['shift-click', 'Toggle selection state'],
57 ['drag', 'Reposition (and pin) device / host'],
58 ['cmd-scroll', 'Zoom in / out'],
59 ['cmd-drag', 'Pan']
60 ]);
61 }
62
Steven Burrowsc0efbf02016-11-16 11:30:47 -060063 function handleEscape() {
Steven Burrows1aa4f582016-12-13 15:05:41 -050064
65 if (false) {
66 // TODO: Cancel show mastership
67 // TODO: Cancel Active overlay
68
69 } else if (t2rs.deselectAllNodes()) {
70 // else if we have node selections, deselect them all
71 // (work already done)
72 } else if (t2rs.deselectLink()) {
73 // else if we have a link selection, deselect it
74 // (work already done)
Steven Burrowsc0efbf02016-11-16 11:30:47 -060075 } else if (t2is.isVisible()) {
Steven Burrows1aa4f582016-12-13 15:05:41 -050076 // If the instance panel is visible, close it
77 t2is.toggle();
78 } else if (t2sp.isVisible()) {
79 // If the summary panel is visible, close it
80 t2sp.toggle();
81 }
Steven Burrowsc0efbf02016-11-16 11:30:47 -060082 }
83
Steven Burrows01ddf602016-09-28 14:21:00 -070084 var prefsState = {};
85
86 function updatePrefsState(what, b) {
87 prefsState[what] = b ? 1 : 0;
Simon Hunt95f4b422017-03-03 13:49:05 -080088 ps.setPrefs('topo2_prefs', prefsState);
Steven Burrows01ddf602016-09-28 14:21:00 -070089 }
90
Steven Burrows583f4be2016-11-04 14:06:50 +010091 function deviceLabelFlashMessage(index) {
Steven Burrows583f4be2016-11-04 14:06:50 +010092 switch (index) {
93 case 0: return 'Hide device labels';
94 case 1: return 'Show friendly device labels';
95 case 2: return 'Show device ID labels';
96 }
97 }
98
Steven Burrows37549ee2016-09-21 14:41:39 +010099 function cycleDeviceLabels() {
Steven Burrows583f4be2016-11-04 14:06:50 +0100100 var deviceLabelIndex = t2ps.get('dlbls') + 1,
Steven Burrowsaf96a212016-12-28 12:57:02 +0000101 newDeviceLabelIndex = deviceLabelIndex % 3;
Steven Burrows583f4be2016-11-04 14:06:50 +0100102
103 t2ps.set('dlbls', newDeviceLabelIndex);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500104 t2fs.updateNodes();
Steven Burrows583f4be2016-11-04 14:06:50 +0100105 flash.flash(deviceLabelFlashMessage(newDeviceLabelIndex));
Steven Burrows37549ee2016-09-21 14:41:39 +0100106 }
107
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000108 function toggleBackground(x) {
109 t2bgs.toggle(x);
Steven Burrowse3a18842016-09-22 15:33:33 +0100110 }
111
Steven Burrows01ddf602016-09-28 14:21:00 -0700112 function toggleInstancePanel(x) {
113 updatePrefsState('insts', t2is.toggle(x));
114 }
115
Steven Burrows5570d1b2016-09-28 14:21:00 -0700116 function toggleSummary() {
117 t2sp.toggle();
118 }
119
Steven Burrows1c5c8612016-10-05 13:45:13 -0500120 function resetZoom() {
Steven Burrows3bbd9af2017-03-16 14:44:14 +0000121 t2bgs.resetZoom();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500122 flash.flash('Pan and zoom reset');
123 }
124
125 function togglePorts(x) {
126 updatePrefsState('porthl', t2vs.togglePortHighlights(x));
127 t2fs.updateLinks();
128 }
129
130 function equalizeMasters() {
131 wss.sendEvent('equalizeMasters');
132 flash.flash('Equalizing master roles');
133 }
134
Steven Burrows892cba62017-03-10 16:31:48 +0000135 function resetNodeLocation() {
136 t2fs.resetNodeLocation();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500137 flash.flash('Reset node locations');
138 }
139
140 function unpinNode() {
141 t2fs.unpin();
142 flash.flash('Unpin node');
143 }
144
Simon Hunt47f8fb72017-04-11 13:49:31 -0700145 function toggleToolbar() {
146 t2tbs.toggle();
147 }
148
Steven Burrowsaea509d2017-04-12 14:17:47 -0700149 function toggleHosts() {
150 t2rs.toggleHosts();
151 }
152
Steven Burrowsc8468932017-03-17 16:13:41 +0000153 function notValid(what) {
154 $log.warn('topo.js getActionEntry(): Not a valid ' + what);
155 }
156
157 function getActionEntry(key) {
158 var entry;
159
160 if (!key) {
161 notValid('key');
162 return null;
163 }
164
165 entry = actionMap()[key];
166
167 if (!entry) {
168 notValid('actionMap (' + key + ') entry');
169 return null;
170 }
171 return fs.isA(entry) || [entry, ''];
172 }
173
Steven Burrows37549ee2016-09-21 14:41:39 +0100174 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000175 .factory('Topo2KeyCommandService', [
Simon Hunt47f8fb72017-04-11 13:49:31 -0700176 '$log', 'FnService', 'KeyService', 'FlashService', 'WebSocketService',
177 'Topo2PrefsService', 'Topo2BackgroundService', 'PrefsService',
178 'Topo2InstanceService', 'Topo2SummaryPanelService', 'Topo2ViewService',
179 'Topo2RegionService', 'Topo2SpriteLayerService',
180
181 function (_$log_, _fs_, _ks_, _flash_, _wss_, _t2ps_, _t2bgs_, _ps_,
182 _t2is_, _t2sp_, _t2vs_, _t2rs_, _t2sls_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500183
Steven Burrowsc8468932017-03-17 16:13:41 +0000184 $log = _$log_;
185 fs = _fs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500186 ks = _ks_;
187 flash = _flash_;
188 wss = _wss_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100189 t2ps = _t2ps_;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000190 t2bgs = _t2bgs_;
Steven Burrows01ddf602016-09-28 14:21:00 -0700191 t2is = _t2is_;
192 ps = _ps_;
Steven Burrows5570d1b2016-09-28 14:21:00 -0700193 t2sp = _t2sp_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500194 t2vs = _t2vs_;
Steven Burrows1aa4f582016-12-13 15:05:41 -0500195 t2rs = _t2rs_;
Steven Burrowsea1d1ec2017-02-23 15:39:25 +0000196 t2sls = _t2sls_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100197
198 return {
199 init: init,
Steven Burrowsc8468932017-03-17 16:13:41 +0000200 bindCommands: bindCommands,
201 getActionEntry: getActionEntry
Steven Burrows37549ee2016-09-21 14:41:39 +0100202 };
203 }
204 ]);
205})();