blob: 06285442210e992a63f63691bc1b31a9b4eb27d6 [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 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 },
Simon Hunt2052e5d2015-04-13 17:40:44 -070052 S: { id: 'sprite-tog', gid: 'cloud', isel: false },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070053
54 //X: { id: 'nodelock-tog', gid: 'lock', isel: false },
55 Z: { id: 'oblique-tog', gid: 'oblique', isel: false },
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070056 N: { id: 'filters-btn', gid: 'filters' },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070057 L: { id: 'cycleLabels-btn', gid: 'cycleLabels' },
58 R: { id: 'resetZoom-btn', gid: 'resetZoom' },
59
Simon Hunt8d22c4b2015-08-06 16:24:43 -070060 E: { id: 'eqMaster-btn', gid: 'eqMaster' }
Simon Hunt09060142015-03-18 20:23:32 -070061 };
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070062
Simon Hunt8d22c4b2015-08-06 16:24:43 -070063 var prohibited = [
64 'T', 'backSlash', 'slash',
65 'X' // needed until we re-instate X above.
66 ];
67 prohibited = prohibited.concat(d3.map(k2b).keys());
68
69
Simon Huntc7ae7952015-04-08 18:59:27 -070070 // initial toggle state: default settings and tag to key mapping
71 var defaultPrefsState = {
Simon Huntc7ae7952015-04-08 18:59:27 -070072 summary: 1,
Simon Huntfcbde892015-04-16 12:05:28 -070073 insts: 1,
Simon Huntc7ae7952015-04-08 18:59:27 -070074 detail: 1,
Simon Huntfcbde892015-04-16 12:05:28 -070075 hosts: 0,
76 offdev: 1,
77 porthl: 1,
Simon Huntd0fe66c2015-06-10 10:16:36 -070078 bg: 0,
Simon Huntfcbde892015-04-16 12:05:28 -070079 spr: 0,
Simon Huntd0fe66c2015-06-10 10:16:36 -070080 toolbar: 0
Simon Huntc7ae7952015-04-08 18:59:27 -070081 },
82 prefsMap = {
Simon Huntc7ae7952015-04-08 18:59:27 -070083 summary: 'O',
Simon Huntfcbde892015-04-16 12:05:28 -070084 insts: 'I',
85 detail: 'D',
86 hosts: 'H',
87 offdev: 'M',
88 porthl: 'P',
89 bg: 'B',
90 spr: 'S'
91 // NOTE: toolbar state is handled separately
Simon Huntc7ae7952015-04-08 18:59:27 -070092 };
93
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070094 function init(_api_) {
95 api = _api_;
Simon Huntc7ae7952015-04-08 18:59:27 -070096
97 // retrieve initial toggle button settings from user prefs
98 setInitToggleState();
99 }
100
101 function topoDefPrefs() {
102 return angular.extend({}, defaultPrefsState);
103 }
104
105 function setInitToggleState() {
Simon Huntfcbde892015-04-16 12:05:28 -0700106 cachedState = ps.asNumbers(ps.getPrefs(cooktag));
107 $log.debug('TOOLBAR---- read prefs state:', cachedState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700108
Simon Huntfcbde892015-04-16 12:05:28 -0700109 if (!cachedState) {
110 cachedState = topoDefPrefs();
111 ps.setPrefs(cooktag, cachedState);
112 $log.debug('TOOLBAR---- Set default prefs state:', cachedState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700113 }
114
115 angular.forEach(prefsMap, function (v, k) {
Simon Huntfcbde892015-04-16 12:05:28 -0700116 var cfg = k2b[v];
117 cfg && (cfg.isel = !!cachedState[k]);
Simon Huntc7ae7952015-04-08 18:59:27 -0700118 });
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700119 }
120
Simon Hunt09060142015-03-18 20:23:32 -0700121 function initKeyData() {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700122 // TODO: use angular forEach instead of d3.map
Simon Hunt09060142015-03-18 20:23:32 -0700123 keyData = d3.map(k2b);
124 keyData.forEach(function(key, value) {
125 var data = api.getActionEntry(key);
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700126 value.cb = data[0]; // on-click callback
127 value.tt = data[1] + ' (' + key + ')'; // tooltip
Simon Hunt09060142015-03-18 20:23:32 -0700128 });
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700129 }
130
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700131 function addButton(key) {
132 var v = keyData.get(key);
133 v.btn = toolbar.addButton(v.id, v.gid, v.cb, v.tt);
134 }
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700135
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700136 function addToggle(key, suppressIfMobile) {
Simon Hunt09060142015-03-18 20:23:32 -0700137 var v = keyData.get(key);
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700138 if (suppressIfMobile && fs.isMobile()) { return; }
Simon Hunt09060142015-03-18 20:23:32 -0700139 v.tog = toolbar.addToggle(v.id, v.gid, v.isel, v.cb, v.tt);
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700140 }
141
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700142 function addFirstRow() {
Simon Hunt09060142015-03-18 20:23:32 -0700143 addToggle('I');
Simon Huntda2f3cc2015-03-19 15:11:57 -0700144 addToggle('O');
Simon Hunt09060142015-03-18 20:23:32 -0700145 addToggle('D');
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700146 toolbar.addSeparator();
147
Simon Hunt09060142015-03-18 20:23:32 -0700148 addToggle('H');
149 addToggle('M');
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700150 addToggle('P', true);
Simon Hunt09060142015-03-18 20:23:32 -0700151 addToggle('B');
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700152 addToggle('S', true);
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700153 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700154
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700155 function addSecondRow() {
Bri Prebilic Cole7c980512015-03-25 12:31:29 -0700156 //addToggle('X');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700157 addToggle('Z');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700158 addButton('N');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700159 addButton('L');
160 addButton('R');
Simon Hunt72e44bf2015-07-21 21:34:20 -0700161 toolbar.addSeparator();
162 addButton('E');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700163 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700164
Simon Hunt72e44bf2015-07-21 21:34:20 -0700165 function addOverlays() {
166 toolbar.addSeparator();
167
168 // generate radio button set for overlays; start with 'none'
169 var rset = [{
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700170 gid: 'topo',
Simon Hunt72e44bf2015-07-21 21:34:20 -0700171 tooltip: 'No Overlay',
Simon Hunte05cae42015-07-23 17:35:24 -0700172 cb: function () {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700173 tov.tbSelection(null, switchOverlayActions);
Simon Hunte05cae42015-07-23 17:35:24 -0700174 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700175 }];
Simon Hunta5b53af2015-10-12 15:56:40 -0700176 ovIndex = tov.augmentRbset(rset, switchOverlayActions);
177 ovRset = toolbar.addRadioSet('topo-overlays', rset);
Simon Hunt72e44bf2015-07-21 21:34:20 -0700178 }
179
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700180 // invoked by overlay service to switch out old buttons and switch in new
181 function switchOverlayActions(oid, keyBindings) {
182 var prohibits = [],
183 kb = fs.isO(keyBindings) || {},
184 order = fs.isA(kb._keyOrder) || [];
185
186 if (keyBindings && !keyBindings._keyOrder) {
187 $log.warn(soa + 'no _keyOrder property defined');
188 } else {
189 // sanity removal of reserved property names
190 ['esc', '_keyListener', '_helpFormat'].forEach(function (k) {
191 fs.removeFromArray(k, order);
192 });
193 }
194
195 thirdRow.clear();
196
197 if (!order.length) {
198 thirdRow.setText(selOver);
199 thirdRow.classed('right', true);
200 api.setUpKeys(); // clear previous overlay key bindings
201
202 } else {
203 thirdRow.classed('right', false);
204 angular.forEach(order, function (key) {
205 var value, bid, gid, tt;
206
207 if (prohibited.indexOf(key) > -1) {
208 prohibits.push(key);
209
210 } else {
211 value = keyBindings[key];
212 bid = oid + '-' + key;
213 gid = tov.mkGlyphId(oid, value.gid);
214 tt = value.tt + ' (' + key + ')';
215 thirdRow.addButton(bid, gid, value.cb, tt);
216 }
217 });
218 api.setUpKeys(keyBindings); // add overlay key bindings
219 }
220
221 if (prohibits.length) {
222 $log.warn(soa + 'Prohibited key bindings ignored:', prohibits);
223 }
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700224 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700225
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700226 function createToolbar() {
Simon Hunt09060142015-03-18 20:23:32 -0700227 initKeyData();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700228 toolbar = tbs.createToolbar(name);
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700229 addFirstRow();
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700230 toolbar.addRow();
231 addSecondRow();
Simon Hunt72e44bf2015-07-21 21:34:20 -0700232 addOverlays();
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700233 thirdRow = toolbar.addRow();
234 thirdRow.setText(selOver);
235 thirdRow.classed('right', true);
Simon Hunt72e44bf2015-07-21 21:34:20 -0700236
Simon Huntfcbde892015-04-16 12:05:28 -0700237 if (cachedState.toolbar) {
238 toolbar.show();
239 } else {
240 toolbar.hide();
241 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700242 }
243
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700244 function destroyToolbar() {
245 tbs.destroyToolbar(name);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700246 tov.resetOnToolbarDestroy();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700247 }
248
Simon Hunt09060142015-03-18 20:23:32 -0700249 // allows us to ensure the button states track key strokes
250 function keyListener(key) {
251 var v = keyData.get(key);
252
253 if (v) {
254 // we have a valid button mapping
255 if (v.tog) {
256 // it's a toggle button
257 v.tog.toggleNoCb();
258 }
259 }
260 }
261
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700262 function toggleToolbar() {
263 toolbar.toggle();
264 }
265
Simon Hunta5b53af2015-10-12 15:56:40 -0700266 function setDefaultOverlay() {
267 var idx = ovIndex[defaultOverlay] || 0;
268 ovRset.selectedIndex(idx);
269 }
270
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700271 angular.module('ovTopo')
Simon Huntc7ae7952015-04-08 18:59:27 -0700272 .factory('TopoToolbarService',
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700273 ['$log', 'FnService', 'ToolbarService', 'PrefsService',
Simon Hunt72e44bf2015-07-21 21:34:20 -0700274 'TopoOverlayService',
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700275
Simon Hunt72e44bf2015-07-21 21:34:20 -0700276 function (_$log_, _fs_, _tbs_, _ps_, _tov_) {
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700277 $log = _$log_;
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700278 fs = _fs_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700279 tbs = _tbs_;
Simon Huntc7ae7952015-04-08 18:59:27 -0700280 ps = _ps_;
Simon Hunt72e44bf2015-07-21 21:34:20 -0700281 tov = _tov_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700282
283 return {
284 init: init,
Simon Hunt09060142015-03-18 20:23:32 -0700285 createToolbar: createToolbar,
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700286 destroyToolbar: destroyToolbar,
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700287 keyListener: keyListener,
Simon Hunta5b53af2015-10-12 15:56:40 -0700288 toggleToolbar: toggleToolbar,
289 setDefaultOverlay: setDefaultOverlay
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700290 };
291 }]);
292}());