blob: cbf443a34c1ee7624ae5a982b9c7cbd0fc3db12a [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 Hunt09060142015-03-18 20:23:32 -070028 // internal state
Simon Huntfcbde892015-04-16 12:05:28 -070029 var toolbar, keyData, cachedState;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070030
Bri Prebilic Coled6219052015-03-19 14:34:02 -070031 // constants
Simon Huntc7ae7952015-04-08 18:59:27 -070032 var name = 'topo-tbar',
33 cooktag = 'topo_prefs';
Bri Prebilic Coled6219052015-03-19 14:34:02 -070034
Simon Hunt09060142015-03-18 20:23:32 -070035 // key to button mapping data
36 var k2b = {
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070037 O: { id: 'summary-tog', gid: 'summary', isel: true},
Simon Hunt09060142015-03-18 20:23:32 -070038 I: { id: 'instance-tog', gid: 'uiAttached', isel: true },
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070039 D: { id: 'details-tog', gid: 'details', isel: true },
Simon Hunt09060142015-03-18 20:23:32 -070040
41 H: { id: 'hosts-tog', gid: 'endstation', isel: false },
42 M: { id: 'offline-tog', gid: 'switch', isel: true },
Bri Prebilic Coledeca6e92015-03-19 12:03:14 -070043 P: { id: 'ports-tog', gid: 'ports', isel: true },
Simon Huntd0fe66c2015-06-10 10:16:36 -070044 B: { id: 'bkgrnd-tog', gid: 'map', isel: false },
Simon Hunt2052e5d2015-04-13 17:40:44 -070045 S: { id: 'sprite-tog', gid: 'cloud', isel: false },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070046
47 //X: { id: 'nodelock-tog', gid: 'lock', isel: false },
48 Z: { id: 'oblique-tog', gid: 'oblique', isel: false },
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070049 N: { id: 'filters-btn', gid: 'filters' },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070050 L: { id: 'cycleLabels-btn', gid: 'cycleLabels' },
51 R: { id: 'resetZoom-btn', gid: 'resetZoom' },
52
Simon Hunte05cae42015-07-23 17:35:24 -070053 E: { id: 'eqMaster-btn', gid: 'eqMaster' },
54
Bri Prebilic Cole7c980512015-03-25 12:31:29 -070055 V: { id: 'relatedIntents-btn', gid: 'relatedIntents' },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070056 leftArrow: { id: 'prevIntent-btn', gid: 'prevIntent' },
57 rightArrow: { id: 'nextIntent-btn', gid: 'nextIntent' },
58 W: { id: 'intentTraffic-btn', gid: 'intentTraffic' },
59 A: { id: 'allTraffic-btn', gid: 'allTraffic' },
Simon Hunte05cae42015-07-23 17:35:24 -070060 F: { id: 'flows-btn', gid: 'flows' }
Simon Hunt09060142015-03-18 20:23:32 -070061 };
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070062
Simon Huntc7ae7952015-04-08 18:59:27 -070063 // initial toggle state: default settings and tag to key mapping
64 var defaultPrefsState = {
Simon Huntc7ae7952015-04-08 18:59:27 -070065 summary: 1,
Simon Huntfcbde892015-04-16 12:05:28 -070066 insts: 1,
Simon Huntc7ae7952015-04-08 18:59:27 -070067 detail: 1,
Simon Huntfcbde892015-04-16 12:05:28 -070068 hosts: 0,
69 offdev: 1,
70 porthl: 1,
Simon Huntd0fe66c2015-06-10 10:16:36 -070071 bg: 0,
Simon Huntfcbde892015-04-16 12:05:28 -070072 spr: 0,
Simon Huntd0fe66c2015-06-10 10:16:36 -070073 toolbar: 0
Simon Huntc7ae7952015-04-08 18:59:27 -070074 },
75 prefsMap = {
Simon Huntc7ae7952015-04-08 18:59:27 -070076 summary: 'O',
Simon Huntfcbde892015-04-16 12:05:28 -070077 insts: 'I',
78 detail: 'D',
79 hosts: 'H',
80 offdev: 'M',
81 porthl: 'P',
82 bg: 'B',
83 spr: 'S'
84 // NOTE: toolbar state is handled separately
Simon Huntc7ae7952015-04-08 18:59:27 -070085 };
86
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070087 function init(_api_) {
88 api = _api_;
Simon Huntc7ae7952015-04-08 18:59:27 -070089
90 // retrieve initial toggle button settings from user prefs
91 setInitToggleState();
92 }
93
94 function topoDefPrefs() {
95 return angular.extend({}, defaultPrefsState);
96 }
97
98 function setInitToggleState() {
Simon Huntfcbde892015-04-16 12:05:28 -070099 cachedState = ps.asNumbers(ps.getPrefs(cooktag));
100 $log.debug('TOOLBAR---- read prefs state:', cachedState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700101
Simon Huntfcbde892015-04-16 12:05:28 -0700102 if (!cachedState) {
103 cachedState = topoDefPrefs();
104 ps.setPrefs(cooktag, cachedState);
105 $log.debug('TOOLBAR---- Set default prefs state:', cachedState);
Simon Huntc7ae7952015-04-08 18:59:27 -0700106 }
107
108 angular.forEach(prefsMap, function (v, k) {
Simon Huntfcbde892015-04-16 12:05:28 -0700109 var cfg = k2b[v];
110 cfg && (cfg.isel = !!cachedState[k]);
Simon Huntc7ae7952015-04-08 18:59:27 -0700111 });
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700112 }
113
Simon Hunt09060142015-03-18 20:23:32 -0700114 function initKeyData() {
115 keyData = d3.map(k2b);
116 keyData.forEach(function(key, value) {
117 var data = api.getActionEntry(key);
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700118 value.cb = data[0]; // on-click callback
119 value.tt = data[1] + ' (' + key + ')'; // tooltip
Simon Hunt09060142015-03-18 20:23:32 -0700120 });
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700121 }
122
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700123 function addButton(key) {
124 var v = keyData.get(key);
125 v.btn = toolbar.addButton(v.id, v.gid, v.cb, v.tt);
126 }
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700127 function addToggle(key, suppressIfMobile) {
Simon Hunt09060142015-03-18 20:23:32 -0700128 var v = keyData.get(key);
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700129 if (suppressIfMobile && fs.isMobile()) { return; }
Simon Hunt09060142015-03-18 20:23:32 -0700130 v.tog = toolbar.addToggle(v.id, v.gid, v.isel, v.cb, v.tt);
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700131 }
132
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700133 function addFirstRow() {
Simon Hunt09060142015-03-18 20:23:32 -0700134 addToggle('I');
Simon Huntda2f3cc2015-03-19 15:11:57 -0700135 addToggle('O');
Simon Hunt09060142015-03-18 20:23:32 -0700136 addToggle('D');
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700137 toolbar.addSeparator();
138
Simon Hunt09060142015-03-18 20:23:32 -0700139 addToggle('H');
140 addToggle('M');
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700141 addToggle('P', true);
Simon Hunt09060142015-03-18 20:23:32 -0700142 addToggle('B');
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700143 addToggle('S', true);
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700144 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700145
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700146 function addSecondRow() {
Bri Prebilic Cole7c980512015-03-25 12:31:29 -0700147 //addToggle('X');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700148 addToggle('Z');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700149 addButton('N');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700150 addButton('L');
151 addButton('R');
Simon Hunt72e44bf2015-07-21 21:34:20 -0700152 toolbar.addSeparator();
153 addButton('E');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700154 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700155
Simon Hunt72e44bf2015-07-21 21:34:20 -0700156 function addOverlays() {
157 toolbar.addSeparator();
158
159 // generate radio button set for overlays; start with 'none'
160 var rset = [{
161 gid: 'unknown',
162 tooltip: 'No Overlay',
Simon Hunte05cae42015-07-23 17:35:24 -0700163 cb: function () {
164 tov.tbSelection(null);
165 }
Simon Hunt72e44bf2015-07-21 21:34:20 -0700166 }];
167
168 tov.list().forEach(function (key) {
169 var ov = tov.overlay(key);
170 rset.push({
171 gid: ov._glyphId,
172 tooltip: (ov.tooltip || '(no tooltip)'),
173 cb: function () {
Simon Hunte05cae42015-07-23 17:35:24 -0700174 tov.tbSelection(ov.overlayId);
Simon Hunt72e44bf2015-07-21 21:34:20 -0700175 }
176 });
177 });
178
179 toolbar.addRadioSet('topo-overlays', rset);
180 }
181
182 // TODO: 3rd row needs to be swapped in/out based on selected overlay
Simon Hunte05cae42015-07-23 17:35:24 -0700183 // NOTE: This particular row of buttons is for the traffic overlay
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700184 function addThirdRow() {
185 addButton('V');
186 addButton('leftArrow');
187 addButton('rightArrow');
188 addButton('W');
189 addButton('A');
190 addButton('F');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700191 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700192
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700193 function createToolbar() {
Simon Hunt09060142015-03-18 20:23:32 -0700194 initKeyData();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700195 toolbar = tbs.createToolbar(name);
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700196 addFirstRow();
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700197 toolbar.addRow();
198 addSecondRow();
Simon Hunt72e44bf2015-07-21 21:34:20 -0700199 addOverlays();
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700200 toolbar.addRow();
201 addThirdRow();
Simon Hunt72e44bf2015-07-21 21:34:20 -0700202
Simon Huntfcbde892015-04-16 12:05:28 -0700203 if (cachedState.toolbar) {
204 toolbar.show();
205 } else {
206 toolbar.hide();
207 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700208 }
209
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700210 function destroyToolbar() {
211 tbs.destroyToolbar(name);
212 }
213
Simon Hunt09060142015-03-18 20:23:32 -0700214 // allows us to ensure the button states track key strokes
215 function keyListener(key) {
216 var v = keyData.get(key);
217
218 if (v) {
219 // we have a valid button mapping
220 if (v.tog) {
221 // it's a toggle button
222 v.tog.toggleNoCb();
223 }
224 }
225 }
226
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700227 function toggleToolbar() {
228 toolbar.toggle();
229 }
230
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700231 angular.module('ovTopo')
Simon Huntc7ae7952015-04-08 18:59:27 -0700232 .factory('TopoToolbarService',
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700233 ['$log', 'FnService', 'ToolbarService', 'PrefsService',
Simon Hunt72e44bf2015-07-21 21:34:20 -0700234 'TopoOverlayService',
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700235
Simon Hunt72e44bf2015-07-21 21:34:20 -0700236 function (_$log_, _fs_, _tbs_, _ps_, _tov_) {
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700237 $log = _$log_;
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700238 fs = _fs_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700239 tbs = _tbs_;
Simon Huntc7ae7952015-04-08 18:59:27 -0700240 ps = _ps_;
Simon Hunt72e44bf2015-07-21 21:34:20 -0700241 tov = _tov_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700242
243 return {
244 init: init,
Simon Hunt09060142015-03-18 20:23:32 -0700245 createToolbar: createToolbar,
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700246 destroyToolbar: destroyToolbar,
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700247 keyListener: keyListener,
248 toggleToolbar: toggleToolbar
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700249 };
250 }]);
251}());