blob: fda74c70eab3025627bffad287178e51961a585d [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
Steven Burrowsc8468932017-03-17 16:13:41 +000020 var $log, fs, ks, flash, wss, t2ps, t2bgs, ps, t2is, t2sp, t2vs, t2rs, t2fs, t2sls, t2tbs;
Steven Burrows37549ee2016-09-21 14:41:39 +010021
22 // Commmands
Steven Burrowsc8468932017-03-17 16:13:41 +000023 function actionMap() {
24 return {
25 L: [cycleDeviceLabels, 'Cycle device labels'],
26 B: [toggleBackground, 'Toggle background'],
27 I: [toggleInstancePanel, 'Toggle ONOS Instance Panel'],
28 O: [toggleSummary, 'Toggle the Summary Panel'],
29 R: [resetZoom, 'Reset pan / zoom'],
30 P: [togglePorts, 'Toggle Port Highlighting'],
31 E: [equalizeMasters, 'Equalize mastership roles'],
32 X: [resetNodeLocation, 'Reset Node Location'],
33 U: [unpinNode, 'Unpin node (mouse over)'],
Steven Burrowsc0efbf02016-11-16 11:30:47 -060034
Steven Burrowsc8468932017-03-17 16:13:41 +000035 esc: handleEscape,
36
37 _keyListener: t2tbs.keyListener.bind(t2tbs)
38 }
Steven Burrows37549ee2016-09-21 14:41:39 +010039 };
40
Steven Burrowsc8468932017-03-17 16:13:41 +000041 function init(_t2fs_, _t2tbs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -050042 t2fs = _t2fs_;
Steven Burrowsc8468932017-03-17 16:13:41 +000043 t2tbs = _t2tbs_;
Steven Burrows37549ee2016-09-21 14:41:39 +010044 bindCommands();
45 }
46
47 function bindCommands() {
48
Steven Burrowsc8468932017-03-17 16:13:41 +000049 ks.keyBindings(actionMap());
Steven Burrows37549ee2016-09-21 14:41:39 +010050
51 ks.gestureNotes([
52 ['click', 'Select the item and show details'],
53 ['shift-click', 'Toggle selection state'],
54 ['drag', 'Reposition (and pin) device / host'],
55 ['cmd-scroll', 'Zoom in / out'],
56 ['cmd-drag', 'Pan']
57 ]);
58 }
59
Steven Burrowsc0efbf02016-11-16 11:30:47 -060060 function handleEscape() {
Steven Burrows1aa4f582016-12-13 15:05:41 -050061
62 if (false) {
63 // TODO: Cancel show mastership
64 // TODO: Cancel Active overlay
65
66 } else if (t2rs.deselectAllNodes()) {
67 // else if we have node selections, deselect them all
68 // (work already done)
69 } else if (t2rs.deselectLink()) {
70 // else if we have a link selection, deselect it
71 // (work already done)
Steven Burrowsc0efbf02016-11-16 11:30:47 -060072 } else if (t2is.isVisible()) {
Steven Burrows1aa4f582016-12-13 15:05:41 -050073 // If the instance panel is visible, close it
74 t2is.toggle();
75 } else if (t2sp.isVisible()) {
76 // If the summary panel is visible, close it
77 t2sp.toggle();
78 }
Steven Burrowsc0efbf02016-11-16 11:30:47 -060079 }
80
Steven Burrows01ddf602016-09-28 14:21:00 -070081 var prefsState = {};
82
83 function updatePrefsState(what, b) {
84 prefsState[what] = b ? 1 : 0;
Simon Hunt95f4b422017-03-03 13:49:05 -080085 ps.setPrefs('topo2_prefs', prefsState);
Steven Burrows01ddf602016-09-28 14:21:00 -070086 }
87
Steven Burrows583f4be2016-11-04 14:06:50 +010088 function deviceLabelFlashMessage(index) {
Steven Burrows583f4be2016-11-04 14:06:50 +010089 switch (index) {
90 case 0: return 'Hide device labels';
91 case 1: return 'Show friendly device labels';
92 case 2: return 'Show device ID labels';
93 }
94 }
95
Steven Burrows37549ee2016-09-21 14:41:39 +010096 function cycleDeviceLabels() {
Steven Burrows583f4be2016-11-04 14:06:50 +010097 var deviceLabelIndex = t2ps.get('dlbls') + 1,
Steven Burrowsaf96a212016-12-28 12:57:02 +000098 newDeviceLabelIndex = deviceLabelIndex % 3;
Steven Burrows583f4be2016-11-04 14:06:50 +010099
100 t2ps.set('dlbls', newDeviceLabelIndex);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500101 t2fs.updateNodes();
Steven Burrows583f4be2016-11-04 14:06:50 +0100102 flash.flash(deviceLabelFlashMessage(newDeviceLabelIndex));
Steven Burrows37549ee2016-09-21 14:41:39 +0100103 }
104
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000105 function toggleBackground(x) {
106 t2bgs.toggle(x);
Steven Burrowse3a18842016-09-22 15:33:33 +0100107 }
108
Steven Burrows01ddf602016-09-28 14:21:00 -0700109 function toggleInstancePanel(x) {
110 updatePrefsState('insts', t2is.toggle(x));
111 }
112
Steven Burrows5570d1b2016-09-28 14:21:00 -0700113 function toggleSummary() {
114 t2sp.toggle();
115 }
116
Steven Burrows1c5c8612016-10-05 13:45:13 -0500117 function resetZoom() {
Steven Burrows3bbd9af2017-03-16 14:44:14 +0000118 t2bgs.resetZoom();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500119 flash.flash('Pan and zoom reset');
120 }
121
122 function togglePorts(x) {
123 updatePrefsState('porthl', t2vs.togglePortHighlights(x));
124 t2fs.updateLinks();
125 }
126
127 function equalizeMasters() {
128 wss.sendEvent('equalizeMasters');
129 flash.flash('Equalizing master roles');
130 }
131
Steven Burrows892cba62017-03-10 16:31:48 +0000132 function resetNodeLocation() {
133 t2fs.resetNodeLocation();
Steven Burrows1c5c8612016-10-05 13:45:13 -0500134 flash.flash('Reset node locations');
135 }
136
137 function unpinNode() {
138 t2fs.unpin();
139 flash.flash('Unpin node');
140 }
141
Steven Burrowsc8468932017-03-17 16:13:41 +0000142 function notValid(what) {
143 $log.warn('topo.js getActionEntry(): Not a valid ' + what);
144 }
145
146 function getActionEntry(key) {
147 var entry;
148
149 if (!key) {
150 notValid('key');
151 return null;
152 }
153
154 entry = actionMap()[key];
155
156 if (!entry) {
157 notValid('actionMap (' + key + ') entry');
158 return null;
159 }
160 return fs.isA(entry) || [entry, ''];
161 }
162
Steven Burrows37549ee2016-09-21 14:41:39 +0100163 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000164 .factory('Topo2KeyCommandService', [
Steven Burrowsc8468932017-03-17 16:13:41 +0000165 '$log', 'FnService', 'KeyService', 'FlashService', 'WebSocketService', 'Topo2PrefsService',
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000166 'Topo2BackgroundService', 'PrefsService', 'Topo2InstanceService',
Steven Burrowsaf96a212016-12-28 12:57:02 +0000167 'Topo2SummaryPanelService', 'Topo2ViewService', 'Topo2RegionService',
Steven Burrowsea1d1ec2017-02-23 15:39:25 +0000168 'Topo2SpriteLayerService',
Steven Burrowsc8468932017-03-17 16:13:41 +0000169 function (_$log_, _fs_, _ks_, _flash_, _wss_, _t2ps_, _t2bgs_, _ps_, _t2is_, _t2sp_,
Steven Burrowsea1d1ec2017-02-23 15:39:25 +0000170 _t2vs_, _t2rs_, _t2sls_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500171
Steven Burrowsc8468932017-03-17 16:13:41 +0000172 $log = _$log_;
173 fs = _fs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500174 ks = _ks_;
175 flash = _flash_;
176 wss = _wss_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100177 t2ps = _t2ps_;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000178 t2bgs = _t2bgs_;
Steven Burrows01ddf602016-09-28 14:21:00 -0700179 t2is = _t2is_;
180 ps = _ps_;
Steven Burrows5570d1b2016-09-28 14:21:00 -0700181 t2sp = _t2sp_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500182 t2vs = _t2vs_;
Steven Burrows1aa4f582016-12-13 15:05:41 -0500183 t2rs = _t2rs_;
Steven Burrowsea1d1ec2017-02-23 15:39:25 +0000184 t2sls = _t2sls_;
Steven Burrows37549ee2016-09-21 14:41:39 +0100185
186 return {
187 init: init,
Steven Burrowsc8468932017-03-17 16:13:41 +0000188 bindCommands: bindCommands,
189 getActionEntry: getActionEntry
Steven Burrows37549ee2016-09-21 14:41:39 +0100190 };
191 }
192 ]);
193})();