blob: d1fa72d082485513ac665cf6e0706753754a0ded [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)'],
Simon Hunt47f8fb72017-04-11 13:49:31 -070035 dot: [toggleToolbar, 'Toggle Toolbar'],
Steven Burrowsc0efbf02016-11-16 11:30:47 -060036
Steven Burrowsc8468932017-03-17 16:13:41 +000037 esc: handleEscape,
38
39 _keyListener: t2tbs.keyListener.bind(t2tbs)
40 }
Steven Burrows37549ee2016-09-21 14:41:39 +010041 };
42
Steven Burrowsc8468932017-03-17 16:13:41 +000043 function init(_t2fs_, _t2tbs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -050044 t2fs = _t2fs_;
Steven Burrowsc8468932017-03-17 16:13:41 +000045 t2tbs = _t2tbs_;
Steven Burrows37549ee2016-09-21 14:41:39 +010046 bindCommands();
47 }
48
49 function bindCommands() {
50
Steven Burrowsc8468932017-03-17 16:13:41 +000051 ks.keyBindings(actionMap());
Steven Burrows37549ee2016-09-21 14:41:39 +010052
53 ks.gestureNotes([
54 ['click', 'Select the item and show details'],
55 ['shift-click', 'Toggle selection state'],
56 ['drag', 'Reposition (and pin) device / host'],
57 ['cmd-scroll', 'Zoom in / out'],
58 ['cmd-drag', 'Pan']
59 ]);
60 }
61
Steven Burrowsc0efbf02016-11-16 11:30:47 -060062 function handleEscape() {
Steven Burrows1aa4f582016-12-13 15:05:41 -050063
64 if (false) {
65 // TODO: Cancel show mastership
66 // TODO: Cancel Active overlay
67
68 } else if (t2rs.deselectAllNodes()) {
69 // else if we have node selections, deselect them all
70 // (work already done)
71 } else if (t2rs.deselectLink()) {
72 // else if we have a link selection, deselect it
73 // (work already done)
Steven Burrowsc0efbf02016-11-16 11:30:47 -060074 } else if (t2is.isVisible()) {
Steven Burrows1aa4f582016-12-13 15:05:41 -050075 // If the instance panel is visible, close it
76 t2is.toggle();
77 } else if (t2sp.isVisible()) {
78 // If the summary panel is visible, close it
79 t2sp.toggle();
80 }
Steven Burrowsc0efbf02016-11-16 11:30:47 -060081 }
82
Steven Burrows01ddf602016-09-28 14:21:00 -070083 var prefsState = {};
84
85 function updatePrefsState(what, b) {
86 prefsState[what] = b ? 1 : 0;
Simon Hunt95f4b422017-03-03 13:49:05 -080087 ps.setPrefs('topo2_prefs', prefsState);
Steven Burrows01ddf602016-09-28 14:21:00 -070088 }
89
Steven Burrows583f4be2016-11-04 14:06:50 +010090 function deviceLabelFlashMessage(index) {
Steven Burrows583f4be2016-11-04 14:06:50 +010091 switch (index) {
92 case 0: return 'Hide device labels';
93 case 1: return 'Show friendly device labels';
94 case 2: return 'Show device ID labels';
95 }
96 }
97
Steven Burrows37549ee2016-09-21 14:41:39 +010098 function cycleDeviceLabels() {
Steven Burrows583f4be2016-11-04 14:06:50 +010099 var deviceLabelIndex = t2ps.get('dlbls') + 1,
Steven Burrowsaf96a212016-12-28 12:57:02 +0000100 newDeviceLabelIndex = deviceLabelIndex % 3;
Steven Burrows583f4be2016-11-04 14:06:50 +0100101
102 t2ps.set('dlbls', newDeviceLabelIndex);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500103 t2fs.updateNodes();
Steven Burrows583f4be2016-11-04 14:06:50 +0100104 flash.flash(deviceLabelFlashMessage(newDeviceLabelIndex));
Steven Burrows37549ee2016-09-21 14:41:39 +0100105 }
106
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000107 function toggleBackground(x) {
108 t2bgs.toggle(x);
Steven Burrowse3a18842016-09-22 15:33:33 +0100109 }
110
Steven Burrows01ddf602016-09-28 14:21:00 -0700111 function toggleInstancePanel(x) {
112 updatePrefsState('insts', t2is.toggle(x));
113 }
114
Steven Burrows5570d1b2016-09-28 14:21:00 -0700115 function toggleSummary() {
116 t2sp.toggle();
117 }
118
Steven Burrows1c5c8612016-10-05 13:45:13 -0500119 function resetZoom() {
Steven Burrows3bbd9af2017-03-16 14:44:14 +0000120 t2bgs.resetZoom();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500121 flash.flash('Pan and zoom reset');
122 }
123
124 function togglePorts(x) {
125 updatePrefsState('porthl', t2vs.togglePortHighlights(x));
126 t2fs.updateLinks();
127 }
128
129 function equalizeMasters() {
130 wss.sendEvent('equalizeMasters');
131 flash.flash('Equalizing master roles');
132 }
133
Steven Burrows892cba62017-03-10 16:31:48 +0000134 function resetNodeLocation() {
135 t2fs.resetNodeLocation();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500136 flash.flash('Reset node locations');
137 }
138
139 function unpinNode() {
140 t2fs.unpin();
141 flash.flash('Unpin node');
142 }
143
Simon Hunt47f8fb72017-04-11 13:49:31 -0700144 function toggleToolbar() {
145 t2tbs.toggle();
146 }
147
Steven Burrowsc8468932017-03-17 16:13:41 +0000148 function notValid(what) {
149 $log.warn('topo.js getActionEntry(): Not a valid ' + what);
150 }
151
152 function getActionEntry(key) {
153 var entry;
154
155 if (!key) {
156 notValid('key');
157 return null;
158 }
159
160 entry = actionMap()[key];
161
162 if (!entry) {
163 notValid('actionMap (' + key + ') entry');
164 return null;
165 }
166 return fs.isA(entry) || [entry, ''];
167 }
168
Steven Burrows37549ee2016-09-21 14:41:39 +0100169 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000170 .factory('Topo2KeyCommandService', [
Simon Hunt47f8fb72017-04-11 13:49:31 -0700171 '$log', 'FnService', 'KeyService', 'FlashService', 'WebSocketService',
172 'Topo2PrefsService', 'Topo2BackgroundService', 'PrefsService',
173 'Topo2InstanceService', 'Topo2SummaryPanelService', 'Topo2ViewService',
174 'Topo2RegionService', 'Topo2SpriteLayerService',
175
176 function (_$log_, _fs_, _ks_, _flash_, _wss_, _t2ps_, _t2bgs_, _ps_,
177 _t2is_, _t2sp_, _t2vs_, _t2rs_, _t2sls_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500178
Steven Burrowsc8468932017-03-17 16:13:41 +0000179 $log = _$log_;
180 fs = _fs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500181 ks = _ks_;
182 flash = _flash_;
183 wss = _wss_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100184 t2ps = _t2ps_;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000185 t2bgs = _t2bgs_;
Steven Burrows01ddf602016-09-28 14:21:00 -0700186 t2is = _t2is_;
187 ps = _ps_;
Steven Burrows5570d1b2016-09-28 14:21:00 -0700188 t2sp = _t2sp_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500189 t2vs = _t2vs_;
Steven Burrows1aa4f582016-12-13 15:05:41 -0500190 t2rs = _t2rs_;
Steven Burrowsea1d1ec2017-02-23 15:39:25 +0000191 t2sls = _t2sls_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100192
193 return {
194 init: init,
Steven Burrowsc8468932017-03-17 16:13:41 +0000195 bindCommands: bindCommands,
196 getActionEntry: getActionEntry
Steven Burrows37549ee2016-09-21 14:41:39 +0100197 };
198 }
199 ]);
200})();