blob: f2ea3247d60518fd011107884cc119bc55f9847f [file] [log] [blame]
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -07001/*
2 * Copyright 2015 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/*
18 ONOS GUI -- Topology Toolbar Module.
Simon Hunt09060142015-03-18 20:23:32 -070019 Functions for creating and interacting with the toolbar.
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070020 */
21
22(function () {
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070023 'use strict';
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070024
25 // injected references
Simon Hunta211f7f2015-11-09 12:48:23 -080026 var $log, fs, tbs, ps, tov, tds, api;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070027
Simon Hunt8d22c4b2015-08-06 16:24:43 -070028 // API:
29 // getActionEntry
30 // setUpKeys
31
Simon Hunt09060142015-03-18 20:23:32 -070032 // internal state
Simon Hunta5b53af2015-10-12 15:56:40 -070033 var toolbar, keyData, cachedState, thirdRow, ovRset, ovIndex;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070034
Bri Prebilic Coled6219052015-03-19 14:34:02 -070035 // constants
Simon Huntc7ae7952015-04-08 18:59:27 -070036 var name = 'topo-tbar',
Simon Hunt8d22c4b2015-08-06 16:24:43 -070037 cooktag = 'topo_prefs',
38 soa = 'switchOverlayActions: ',
Simon Hunta5b53af2015-10-12 15:56:40 -070039 selOver = 'Select overlay here ⇧',
40 defaultOverlay = 'traffic';
Simon Hunt8d22c4b2015-08-06 16:24:43 -070041
Bri Prebilic Coled6219052015-03-19 14:34:02 -070042
Simon Hunt09060142015-03-18 20:23:32 -070043 // key to button mapping data
44 var k2b = {
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070045 O: { id: 'summary-tog', gid: 'summary', isel: true},
Simon Hunt09060142015-03-18 20:23:32 -070046 I: { id: 'instance-tog', gid: 'uiAttached', isel: true },
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070047 D: { id: 'details-tog', gid: 'details', isel: true },
Simon Hunt09060142015-03-18 20:23:32 -070048 H: { id: 'hosts-tog', gid: 'endstation', isel: false },
49 M: { id: 'offline-tog', gid: 'switch', isel: true },
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070050 P: { id: 'ports-tog', gid: 'ports', isel: true },
Simon Huntd0fe66c2015-06-10 10:16:36 -070051 B: { id: 'bkgrnd-tog', gid: 'map', isel: false },
Thomas Vachuska26be4f32016-03-31 01:10:27 -070052 G: { id: 'bkgrnd-sel', gid: 'filters' },
Simon Hunt2052e5d2015-04-13 17:40:44 -070053 S: { id: 'sprite-tog', gid: 'cloud', isel: false },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070054
Simon Huntfd7106c2016-02-09 15:05:26 -080055 // TODO: add reset-node-locations button to toolbar
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070056 //X: { id: 'nodelock-tog', gid: 'lock', isel: false },
57 Z: { id: 'oblique-tog', gid: 'oblique', isel: false },
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070058 N: { id: 'filters-btn', gid: 'filters' },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070059 L: { id: 'cycleLabels-btn', gid: 'cycleLabels' },
60 R: { id: 'resetZoom-btn', gid: 'resetZoom' },
61
Simon Hunt8d22c4b2015-08-06 16:24:43 -070062 E: { id: 'eqMaster-btn', gid: 'eqMaster' }
Simon Hunt09060142015-03-18 20:23:32 -070063 };
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070064
Simon Hunt8d22c4b2015-08-06 16:24:43 -070065 var prohibited = [
66 'T', 'backSlash', 'slash',
67 'X' // needed until we re-instate X above.
68 ];
69 prohibited = prohibited.concat(d3.map(k2b).keys());
70
71
Simon Huntc7ae7952015-04-08 18:59:27 -070072 // initial toggle state: default settings and tag to key mapping
73 var defaultPrefsState = {
Simon Huntfcbde892015-04-16 12:05:28 -070074 insts: 1,
Thomas Vachuska0af26912016-03-21 21:37:30 -070075 summary: 1,
Simon Huntc7ae7952015-04-08 18:59:27 -070076 detail: 1,
Simon Huntfcbde892015-04-16 12:05:28 -070077 hosts: 0,
78 offdev: 1,
Thomas Vachuska0af26912016-03-21 21:37:30 -070079 dlbls: 0,
Simon Huntfcbde892015-04-16 12:05:28 -070080 porthl: 1,
Simon Huntd0fe66c2015-06-10 10:16:36 -070081 bg: 0,
Simon Huntfcbde892015-04-16 12:05:28 -070082 spr: 0,
Simon Huntd0fe66c2015-06-10 10:16:36 -070083 toolbar: 0
Simon Huntc7ae7952015-04-08 18:59:27 -070084 },
85 prefsMap = {
Simon Huntc7ae7952015-04-08 18:59:27 -070086 summary: 'O',
Simon Huntfcbde892015-04-16 12:05:28 -070087 insts: 'I',
88 detail: 'D',
89 hosts: 'H',
90 offdev: 'M',
91 porthl: 'P',
92 bg: 'B',
93 spr: 'S'
94 // NOTE: toolbar state is handled separately
Simon Huntc7ae7952015-04-08 18:59:27 -070095 };
96
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070097 function init(_api_) {
98 api = _api_;
Simon Huntc7ae7952015-04-08 18:59:27 -070099
100 // retrieve initial toggle button settings from user prefs
101 setInitToggleState();
102 }
103
104 function topoDefPrefs() {
105 return angular.extend({}, defaultPrefsState);
106 }
107
108 function setInitToggleState() {
Thomas Vachuska0af26912016-03-21 21:37:30 -0700109 cachedState = ps.asNumbers(ps.getPrefs(cooktag, defaultPrefsState));
Simon Huntfcbde892015-04-16 12:05:28 -0700110 $log.debug('TOOLBAR---- read prefs state:', cachedState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700111
Simon Huntfcbde892015-04-16 12:05:28 -0700112 if (!cachedState) {
113 cachedState = topoDefPrefs();
114 ps.setPrefs(cooktag, cachedState);
115 $log.debug('TOOLBAR---- Set default prefs state:', cachedState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700116 }
117
118 angular.forEach(prefsMap, function (v, k) {
Simon Huntfcbde892015-04-16 12:05:28 -0700119 var cfg = k2b[v];
120 cfg && (cfg.isel = !!cachedState[k]);
Simon Huntc7ae7952015-04-08 18:59:27 -0700121 });
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700122 }
123
Simon Hunt09060142015-03-18 20:23:32 -0700124 function initKeyData() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700125 // TODO: use angular forEach instead of d3.map
Simon Hunt09060142015-03-18 20:23:32 -0700126 keyData = d3.map(k2b);
127 keyData.forEach(function(key, value) {
128 var data = api.getActionEntry(key);
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700129 value.cb = data[0]; // on-click callback
130 value.tt = data[1] + ' (' + key + ')'; // tooltip
Simon Hunt09060142015-03-18 20:23:32 -0700131 });
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700132 }
133
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700134 function addButton(key) {
135 var v = keyData.get(key);
136 v.btn = toolbar.addButton(v.id, v.gid, v.cb, v.tt);
137 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700138
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700139 function addToggle(key, suppressIfMobile) {
Simon Hunt09060142015-03-18 20:23:32 -0700140 var v = keyData.get(key);
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700141 if (suppressIfMobile && fs.isMobile()) { return; }
Simon Hunt09060142015-03-18 20:23:32 -0700142 v.tog = toolbar.addToggle(v.id, v.gid, v.isel, v.cb, v.tt);
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700143 }
144
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700145 function addFirstRow() {
Simon Hunt09060142015-03-18 20:23:32 -0700146 addToggle('I');
Simon Huntda2f3cc2015-03-19 15:11:57 -0700147 addToggle('O');
Simon Hunt09060142015-03-18 20:23:32 -0700148 addToggle('D');
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700149 toolbar.addSeparator();
150
Simon Hunt09060142015-03-18 20:23:32 -0700151 addToggle('H');
152 addToggle('M');
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700153 addToggle('P', true);
Simon Hunt09060142015-03-18 20:23:32 -0700154 addToggle('B');
Thomas Vachuska26be4f32016-03-31 01:10:27 -0700155 addButton('G');
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700156 addToggle('S', true);
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700157 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700158
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700159 function addSecondRow() {
Bri Prebilic Cole7c980512015-03-25 12:31:29 -0700160 //addToggle('X');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700161 addToggle('Z');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700162 addButton('N');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700163 addButton('L');
164 addButton('R');
Simon Hunt72e44bf2015-07-21 21:34:20 -0700165 toolbar.addSeparator();
166 addButton('E');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700167 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700168
Simon Hunt72e44bf2015-07-21 21:34:20 -0700169 function addOverlays() {
170 toolbar.addSeparator();
171
172 // generate radio button set for overlays; start with 'none'
173 var rset = [{
Andrea Campanella0c17a0a2015-12-01 09:53:51 -0800174 gid: 'unknown',
Simon Hunt72e44bf2015-07-21 21:34:20 -0700175 tooltip: 'No Overlay',
Simon Hunte05cae42015-07-23 17:35:24 -0700176 cb: function () {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700177 tov.tbSelection(null, switchOverlayActions);
Simon Hunte05cae42015-07-23 17:35:24 -0700178 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700179 }];
Simon Hunta5b53af2015-10-12 15:56:40 -0700180 ovIndex = tov.augmentRbset(rset, switchOverlayActions);
181 ovRset = toolbar.addRadioSet('topo-overlays', rset);
Simon Hunt72e44bf2015-07-21 21:34:20 -0700182 }
183
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700184 // invoked by overlay service to switch out old buttons and switch in new
185 function switchOverlayActions(oid, keyBindings) {
186 var prohibits = [],
187 kb = fs.isO(keyBindings) || {},
188 order = fs.isA(kb._keyOrder) || [];
189
190 if (keyBindings && !keyBindings._keyOrder) {
191 $log.warn(soa + 'no _keyOrder property defined');
192 } else {
193 // sanity removal of reserved property names
194 ['esc', '_keyListener', '_helpFormat'].forEach(function (k) {
195 fs.removeFromArray(k, order);
196 });
197 }
198
Simon Hunta211f7f2015-11-09 12:48:23 -0800199 // ensure dialog has closed (if opened by outgoing overlay)
200 tds.closeDialog();
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700201 thirdRow.clear();
202
203 if (!order.length) {
204 thirdRow.setText(selOver);
205 thirdRow.classed('right', true);
206 api.setUpKeys(); // clear previous overlay key bindings
207
208 } else {
209 thirdRow.classed('right', false);
210 angular.forEach(order, function (key) {
211 var value, bid, gid, tt;
212
213 if (prohibited.indexOf(key) > -1) {
214 prohibits.push(key);
215
216 } else {
217 value = keyBindings[key];
218 bid = oid + '-' + key;
219 gid = tov.mkGlyphId(oid, value.gid);
220 tt = value.tt + ' (' + key + ')';
221 thirdRow.addButton(bid, gid, value.cb, tt);
222 }
223 });
224 api.setUpKeys(keyBindings); // add overlay key bindings
225 }
226
227 if (prohibits.length) {
228 $log.warn(soa + 'Prohibited key bindings ignored:', prohibits);
229 }
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700230 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700231
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700232 function createToolbar() {
Simon Hunt09060142015-03-18 20:23:32 -0700233 initKeyData();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700234 toolbar = tbs.createToolbar(name);
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700235 addFirstRow();
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700236 toolbar.addRow();
237 addSecondRow();
Simon Hunt72e44bf2015-07-21 21:34:20 -0700238 addOverlays();
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700239 thirdRow = toolbar.addRow();
240 thirdRow.setText(selOver);
241 thirdRow.classed('right', true);
Simon Hunt72e44bf2015-07-21 21:34:20 -0700242
Simon Huntfcbde892015-04-16 12:05:28 -0700243 if (cachedState.toolbar) {
244 toolbar.show();
245 } else {
246 toolbar.hide();
247 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700248 }
249
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700250 function destroyToolbar() {
251 tbs.destroyToolbar(name);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700252 tov.resetOnToolbarDestroy();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700253 }
254
Simon Hunt09060142015-03-18 20:23:32 -0700255 // allows us to ensure the button states track key strokes
256 function keyListener(key) {
257 var v = keyData.get(key);
258
259 if (v) {
260 // we have a valid button mapping
261 if (v.tog) {
262 // it's a toggle button
263 v.tog.toggleNoCb();
264 }
265 }
266 }
267
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700268 function toggleToolbar() {
269 toolbar.toggle();
Thomas Vachuska0af26912016-03-21 21:37:30 -0700270 var prefs = ps.getPrefs(cooktag, defaultPrefsState);
271 prefs.toolbar = !prefs.toolbar;
272 ps.setPrefs('topo_prefs', prefs);
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700273 }
274
Simon Hunta5b53af2015-10-12 15:56:40 -0700275 function setDefaultOverlay() {
276 var idx = ovIndex[defaultOverlay] || 0;
277 ovRset.selectedIndex(idx);
278 }
279
Simon Hunt5b024d72016-01-29 11:02:43 -0800280 // an overlay was selected via Function-Key press
281 function fnkey(idx) {
282 if (idx < ovRset.size() && idx !== ovRset.selectedIndex()) {
283 ovRset.selectedIndex(idx);
284 }
285 }
286
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700287 angular.module('ovTopo')
Simon Huntc7ae7952015-04-08 18:59:27 -0700288 .factory('TopoToolbarService',
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700289 ['$log', 'FnService', 'ToolbarService', 'PrefsService',
Simon Hunta211f7f2015-11-09 12:48:23 -0800290 'TopoOverlayService', 'TopoDialogService',
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700291
Simon Hunta211f7f2015-11-09 12:48:23 -0800292 function (_$log_, _fs_, _tbs_, _ps_, _tov_, _tds_) {
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700293 $log = _$log_;
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700294 fs = _fs_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700295 tbs = _tbs_;
Simon Huntc7ae7952015-04-08 18:59:27 -0700296 ps = _ps_;
Simon Hunt72e44bf2015-07-21 21:34:20 -0700297 tov = _tov_;
Simon Hunta211f7f2015-11-09 12:48:23 -0800298 tds = _tds_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700299
300 return {
301 init: init,
Simon Hunt09060142015-03-18 20:23:32 -0700302 createToolbar: createToolbar,
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700303 destroyToolbar: destroyToolbar,
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700304 keyListener: keyListener,
Simon Hunta5b53af2015-10-12 15:56:40 -0700305 toggleToolbar: toggleToolbar,
Simon Hunt5b024d72016-01-29 11:02:43 -0800306 setDefaultOverlay: setDefaultOverlay,
Thomas Vachuska0af26912016-03-21 21:37:30 -0700307 defaultPrefs: defaultPrefsState,
Simon Hunt5b024d72016-01-29 11:02:43 -0800308 fnkey: fnkey
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700309 };
310 }]);
311}());