blob: 84de261ba0145b29d609e37cffccba7d4ccc0c86 [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 Hunt72e44bf2015-07-21 21:34:20 -070026 var $log, fs, tbs, ps, tov, 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 Hunt8d22c4b2015-08-06 16:24:43 -070033 var toolbar, keyData, cachedState, thirdRow;
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: ',
39 selOver = 'Select overlay here ⇧';
40
Bri Prebilic Coled6219052015-03-19 14:34:02 -070041
Simon Hunt09060142015-03-18 20:23:32 -070042 // key to button mapping data
43 var k2b = {
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070044 O: { id: 'summary-tog', gid: 'summary', isel: true},
Simon Hunt09060142015-03-18 20:23:32 -070045 I: { id: 'instance-tog', gid: 'uiAttached', isel: true },
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070046 D: { id: 'details-tog', gid: 'details', isel: true },
Simon Hunt09060142015-03-18 20:23:32 -070047 H: { id: 'hosts-tog', gid: 'endstation', isel: false },
48 M: { id: 'offline-tog', gid: 'switch', isel: true },
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070049 P: { id: 'ports-tog', gid: 'ports', isel: true },
Simon Huntd0fe66c2015-06-10 10:16:36 -070050 B: { id: 'bkgrnd-tog', gid: 'map', isel: false },
Simon Hunt2052e5d2015-04-13 17:40:44 -070051 S: { id: 'sprite-tog', gid: 'cloud', isel: false },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070052
53 //X: { id: 'nodelock-tog', gid: 'lock', isel: false },
54 Z: { id: 'oblique-tog', gid: 'oblique', isel: false },
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070055 N: { id: 'filters-btn', gid: 'filters' },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070056 L: { id: 'cycleLabels-btn', gid: 'cycleLabels' },
57 R: { id: 'resetZoom-btn', gid: 'resetZoom' },
58
Simon Hunt8d22c4b2015-08-06 16:24:43 -070059 E: { id: 'eqMaster-btn', gid: 'eqMaster' }
Simon Hunt09060142015-03-18 20:23:32 -070060 };
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070061
Simon Hunt8d22c4b2015-08-06 16:24:43 -070062 var prohibited = [
63 'T', 'backSlash', 'slash',
64 'X' // needed until we re-instate X above.
65 ];
66 prohibited = prohibited.concat(d3.map(k2b).keys());
67
68
Simon Huntc7ae7952015-04-08 18:59:27 -070069 // initial toggle state: default settings and tag to key mapping
70 var defaultPrefsState = {
Simon Huntc7ae7952015-04-08 18:59:27 -070071 summary: 1,
Simon Huntfcbde892015-04-16 12:05:28 -070072 insts: 1,
Simon Huntc7ae7952015-04-08 18:59:27 -070073 detail: 1,
Simon Huntfcbde892015-04-16 12:05:28 -070074 hosts: 0,
75 offdev: 1,
76 porthl: 1,
Simon Huntd0fe66c2015-06-10 10:16:36 -070077 bg: 0,
Simon Huntfcbde892015-04-16 12:05:28 -070078 spr: 0,
Simon Huntd0fe66c2015-06-10 10:16:36 -070079 toolbar: 0
Simon Huntc7ae7952015-04-08 18:59:27 -070080 },
81 prefsMap = {
Simon Huntc7ae7952015-04-08 18:59:27 -070082 summary: 'O',
Simon Huntfcbde892015-04-16 12:05:28 -070083 insts: 'I',
84 detail: 'D',
85 hosts: 'H',
86 offdev: 'M',
87 porthl: 'P',
88 bg: 'B',
89 spr: 'S'
90 // NOTE: toolbar state is handled separately
Simon Huntc7ae7952015-04-08 18:59:27 -070091 };
92
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070093 function init(_api_) {
94 api = _api_;
Simon Huntc7ae7952015-04-08 18:59:27 -070095
96 // retrieve initial toggle button settings from user prefs
97 setInitToggleState();
98 }
99
100 function topoDefPrefs() {
101 return angular.extend({}, defaultPrefsState);
102 }
103
104 function setInitToggleState() {
Simon Huntfcbde892015-04-16 12:05:28 -0700105 cachedState = ps.asNumbers(ps.getPrefs(cooktag));
106 $log.debug('TOOLBAR---- read prefs state:', cachedState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700107
Simon Huntfcbde892015-04-16 12:05:28 -0700108 if (!cachedState) {
109 cachedState = topoDefPrefs();
110 ps.setPrefs(cooktag, cachedState);
111 $log.debug('TOOLBAR---- Set default prefs state:', cachedState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700112 }
113
114 angular.forEach(prefsMap, function (v, k) {
Simon Huntfcbde892015-04-16 12:05:28 -0700115 var cfg = k2b[v];
116 cfg && (cfg.isel = !!cachedState[k]);
Simon Huntc7ae7952015-04-08 18:59:27 -0700117 });
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700118 }
119
Simon Hunt09060142015-03-18 20:23:32 -0700120 function initKeyData() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700121 // TODO: use angular forEach instead of d3.map
Simon Hunt09060142015-03-18 20:23:32 -0700122 keyData = d3.map(k2b);
123 keyData.forEach(function(key, value) {
124 var data = api.getActionEntry(key);
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700125 value.cb = data[0]; // on-click callback
126 value.tt = data[1] + ' (' + key + ')'; // tooltip
Simon Hunt09060142015-03-18 20:23:32 -0700127 });
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700128 }
129
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700130 function addButton(key) {
131 var v = keyData.get(key);
132 v.btn = toolbar.addButton(v.id, v.gid, v.cb, v.tt);
133 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700134
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700135 function addToggle(key, suppressIfMobile) {
Simon Hunt09060142015-03-18 20:23:32 -0700136 var v = keyData.get(key);
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700137 if (suppressIfMobile && fs.isMobile()) { return; }
Simon Hunt09060142015-03-18 20:23:32 -0700138 v.tog = toolbar.addToggle(v.id, v.gid, v.isel, v.cb, v.tt);
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700139 }
140
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700141 function addFirstRow() {
Simon Hunt09060142015-03-18 20:23:32 -0700142 addToggle('I');
Simon Huntda2f3cc2015-03-19 15:11:57 -0700143 addToggle('O');
Simon Hunt09060142015-03-18 20:23:32 -0700144 addToggle('D');
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700145 toolbar.addSeparator();
146
Simon Hunt09060142015-03-18 20:23:32 -0700147 addToggle('H');
148 addToggle('M');
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700149 addToggle('P', true);
Simon Hunt09060142015-03-18 20:23:32 -0700150 addToggle('B');
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700151 addToggle('S', true);
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700152 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700153
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700154 function addSecondRow() {
Bri Prebilic Cole7c980512015-03-25 12:31:29 -0700155 //addToggle('X');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700156 addToggle('Z');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700157 addButton('N');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700158 addButton('L');
159 addButton('R');
Simon Hunt72e44bf2015-07-21 21:34:20 -0700160 toolbar.addSeparator();
161 addButton('E');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700162 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700163
Simon Hunt72e44bf2015-07-21 21:34:20 -0700164 function addOverlays() {
165 toolbar.addSeparator();
166
167 // generate radio button set for overlays; start with 'none'
168 var rset = [{
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700169 gid: 'topo',
Simon Hunt72e44bf2015-07-21 21:34:20 -0700170 tooltip: 'No Overlay',
Simon Hunte05cae42015-07-23 17:35:24 -0700171 cb: function () {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700172 tov.tbSelection(null, switchOverlayActions);
Simon Hunte05cae42015-07-23 17:35:24 -0700173 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700174 }];
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700175 tov.augmentRbset(rset, switchOverlayActions);
Simon Hunt72e44bf2015-07-21 21:34:20 -0700176 toolbar.addRadioSet('topo-overlays', rset);
177 }
178
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700179 // invoked by overlay service to switch out old buttons and switch in new
180 function switchOverlayActions(oid, keyBindings) {
181 var prohibits = [],
182 kb = fs.isO(keyBindings) || {},
183 order = fs.isA(kb._keyOrder) || [];
184
185 if (keyBindings && !keyBindings._keyOrder) {
186 $log.warn(soa + 'no _keyOrder property defined');
187 } else {
188 // sanity removal of reserved property names
189 ['esc', '_keyListener', '_helpFormat'].forEach(function (k) {
190 fs.removeFromArray(k, order);
191 });
192 }
193
194 thirdRow.clear();
195
196 if (!order.length) {
197 thirdRow.setText(selOver);
198 thirdRow.classed('right', true);
199 api.setUpKeys(); // clear previous overlay key bindings
200
201 } else {
202 thirdRow.classed('right', false);
203 angular.forEach(order, function (key) {
204 var value, bid, gid, tt;
205
206 if (prohibited.indexOf(key) > -1) {
207 prohibits.push(key);
208
209 } else {
210 value = keyBindings[key];
211 bid = oid + '-' + key;
212 gid = tov.mkGlyphId(oid, value.gid);
213 tt = value.tt + ' (' + key + ')';
214 thirdRow.addButton(bid, gid, value.cb, tt);
215 }
216 });
217 api.setUpKeys(keyBindings); // add overlay key bindings
218 }
219
220 if (prohibits.length) {
221 $log.warn(soa + 'Prohibited key bindings ignored:', prohibits);
222 }
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700223 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700224
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700225 function createToolbar() {
Simon Hunt09060142015-03-18 20:23:32 -0700226 initKeyData();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700227 toolbar = tbs.createToolbar(name);
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700228 addFirstRow();
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700229 toolbar.addRow();
230 addSecondRow();
Simon Hunt72e44bf2015-07-21 21:34:20 -0700231 addOverlays();
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700232 thirdRow = toolbar.addRow();
233 thirdRow.setText(selOver);
234 thirdRow.classed('right', true);
Simon Hunt72e44bf2015-07-21 21:34:20 -0700235
Simon Huntfcbde892015-04-16 12:05:28 -0700236 if (cachedState.toolbar) {
237 toolbar.show();
238 } else {
239 toolbar.hide();
240 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700241 }
242
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700243 function destroyToolbar() {
244 tbs.destroyToolbar(name);
245 }
246
Simon Hunt09060142015-03-18 20:23:32 -0700247 // allows us to ensure the button states track key strokes
248 function keyListener(key) {
249 var v = keyData.get(key);
250
251 if (v) {
252 // we have a valid button mapping
253 if (v.tog) {
254 // it's a toggle button
255 v.tog.toggleNoCb();
256 }
257 }
258 }
259
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700260 function toggleToolbar() {
261 toolbar.toggle();
262 }
263
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700264 angular.module('ovTopo')
Simon Huntc7ae7952015-04-08 18:59:27 -0700265 .factory('TopoToolbarService',
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700266 ['$log', 'FnService', 'ToolbarService', 'PrefsService',
Simon Hunt72e44bf2015-07-21 21:34:20 -0700267 'TopoOverlayService',
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700268
Simon Hunt72e44bf2015-07-21 21:34:20 -0700269 function (_$log_, _fs_, _tbs_, _ps_, _tov_) {
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700270 $log = _$log_;
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700271 fs = _fs_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700272 tbs = _tbs_;
Simon Huntc7ae7952015-04-08 18:59:27 -0700273 ps = _ps_;
Simon Hunt72e44bf2015-07-21 21:34:20 -0700274 tov = _tov_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700275
276 return {
277 init: init,
Simon Hunt09060142015-03-18 20:23:32 -0700278 createToolbar: createToolbar,
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700279 destroyToolbar: destroyToolbar,
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700280 keyListener: keyListener,
281 toggleToolbar: toggleToolbar
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700282 };
283 }]);
284}());