blob: 8a1895b9baa5452c385a6606215ffd3a81e84acc [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 Huntc7ae7952015-04-08 18:59:27 -070026 var $log, tbs, ps, api;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070027
Simon Hunt09060142015-03-18 20:23:32 -070028 // internal state
29 var toolbar, keyData;
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 },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070044 B: { id: 'bkgrnd-tog', gid: 'map', isel: true },
45
46 //X: { id: 'nodelock-tog', gid: 'lock', isel: false },
47 Z: { id: 'oblique-tog', gid: 'oblique', isel: false },
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070048 N: { id: 'filters-btn', gid: 'filters' },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070049 L: { id: 'cycleLabels-btn', gid: 'cycleLabels' },
50 R: { id: 'resetZoom-btn', gid: 'resetZoom' },
51
Bri Prebilic Cole7c980512015-03-25 12:31:29 -070052 V: { id: 'relatedIntents-btn', gid: 'relatedIntents' },
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070053 leftArrow: { id: 'prevIntent-btn', gid: 'prevIntent' },
54 rightArrow: { id: 'nextIntent-btn', gid: 'nextIntent' },
55 W: { id: 'intentTraffic-btn', gid: 'intentTraffic' },
56 A: { id: 'allTraffic-btn', gid: 'allTraffic' },
57 F: { id: 'flows-btn', gid: 'flows' },
58
59 E: { id: 'eqMaster-btn', gid: 'eqMaster' }
Simon Hunt09060142015-03-18 20:23:32 -070060 };
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070061
Simon Huntc7ae7952015-04-08 18:59:27 -070062 // initial toggle state: default settings and tag to key mapping
63 var defaultPrefsState = {
64 bg: 1,
65 insts: 1,
66 summary: 1,
67 detail: 1,
68 hosts: 0
69 },
70 prefsMap = {
71 bg: 'B',
72 insts: 'I',
73 summary: 'O',
74 details: 'D',
75 hosts: 'H'
76 };
77
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070078 function init(_api_) {
79 api = _api_;
Simon Huntc7ae7952015-04-08 18:59:27 -070080
81 // retrieve initial toggle button settings from user prefs
82 setInitToggleState();
83 }
84
85 function topoDefPrefs() {
86 return angular.extend({}, defaultPrefsState);
87 }
88
89 function setInitToggleState() {
90 var state = ps.getPrefs(cooktag);
91 $log.debug('TOOLBAR---- read prefs state:', state);
92
93 if (!state) {
94 state = topoDefPrefs();
95 ps.setPrefs(cooktag, state);
96 $log.debug('TOOLBAR---- Set default prefs state:', state);
97 }
98
99 angular.forEach(prefsMap, function (v, k) {
100 k2b[v].isel = !!state[k];
101 });
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700102 }
103
Simon Hunt09060142015-03-18 20:23:32 -0700104 function initKeyData() {
105 keyData = d3.map(k2b);
106 keyData.forEach(function(key, value) {
107 var data = api.getActionEntry(key);
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700108 value.cb = data[0]; // on-click callback
109 value.tt = data[1] + ' (' + key + ')'; // tooltip
Simon Hunt09060142015-03-18 20:23:32 -0700110 });
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700111 }
112
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700113 function addButton(key) {
114 var v = keyData.get(key);
115 v.btn = toolbar.addButton(v.id, v.gid, v.cb, v.tt);
116 }
Simon Hunt09060142015-03-18 20:23:32 -0700117 function addToggle(key) {
118 var v = keyData.get(key);
119 v.tog = toolbar.addToggle(v.id, v.gid, v.isel, v.cb, v.tt);
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700120 }
121
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700122 function addFirstRow() {
Simon Hunt09060142015-03-18 20:23:32 -0700123 addToggle('I');
Simon Huntda2f3cc2015-03-19 15:11:57 -0700124 addToggle('O');
Simon Hunt09060142015-03-18 20:23:32 -0700125 addToggle('D');
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700126 toolbar.addSeparator();
127
Simon Hunt09060142015-03-18 20:23:32 -0700128 addToggle('H');
129 addToggle('M');
130 addToggle('P');
131 addToggle('B');
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700132 }
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700133 function addSecondRow() {
Bri Prebilic Cole7c980512015-03-25 12:31:29 -0700134 //addToggle('X');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700135 addToggle('Z');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700136 addButton('N');
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700137 addButton('L');
138 addButton('R');
139 }
140 function addThirdRow() {
141 addButton('V');
142 addButton('leftArrow');
143 addButton('rightArrow');
144 addButton('W');
145 addButton('A');
146 addButton('F');
147 toolbar.addSeparator();
148 addButton('E');
149 }
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700150
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700151 function createToolbar() {
Simon Hunt09060142015-03-18 20:23:32 -0700152 initKeyData();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700153 toolbar = tbs.createToolbar(name);
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700154 addFirstRow();
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700155 toolbar.addRow();
156 addSecondRow();
157 toolbar.addRow();
158 addThirdRow();
Bri Prebilic Cole04b41402015-03-18 17:25:34 -0700159 toolbar.show();
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700160 }
161
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700162 function destroyToolbar() {
163 tbs.destroyToolbar(name);
164 }
165
Simon Hunt09060142015-03-18 20:23:32 -0700166 // allows us to ensure the button states track key strokes
167 function keyListener(key) {
168 var v = keyData.get(key);
169
170 if (v) {
171 // we have a valid button mapping
172 if (v.tog) {
173 // it's a toggle button
174 v.tog.toggleNoCb();
175 }
176 }
177 }
178
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700179 function toggleToolbar() {
180 toolbar.toggle();
181 }
182
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700183 angular.module('ovTopo')
Simon Huntc7ae7952015-04-08 18:59:27 -0700184 .factory('TopoToolbarService',
185 ['$log', 'ToolbarService', 'PrefsService',
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700186
Simon Huntc7ae7952015-04-08 18:59:27 -0700187 function (_$log_, _tbs_, _ps_) {
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700188 $log = _$log_;
189 tbs = _tbs_;
Simon Huntc7ae7952015-04-08 18:59:27 -0700190 ps = _ps_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700191
192 return {
193 init: init,
Simon Hunt09060142015-03-18 20:23:32 -0700194 createToolbar: createToolbar,
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700195 destroyToolbar: destroyToolbar,
Simon Hunt90dcc3e2015-03-25 15:01:27 -0700196 keyListener: keyListener,
197 toggleToolbar: toggleToolbar
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700198 };
199 }]);
200}());