Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 1 | /* |
| 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 Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 19 | Functions for creating and interacting with the toolbar. |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | (function () { |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 23 | 'use strict'; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 24 | |
| 25 | // injected references |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 26 | var $log, fs, tbs, ps, tov, api; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 27 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 28 | // internal state |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 29 | var toolbar, keyData, cachedState; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 30 | |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 31 | // constants |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 32 | var name = 'topo-tbar', |
| 33 | cooktag = 'topo_prefs'; |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 34 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 35 | // key to button mapping data |
| 36 | var k2b = { |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 37 | O: { id: 'summary-tog', gid: 'summary', isel: true}, |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 38 | I: { id: 'instance-tog', gid: 'uiAttached', isel: true }, |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 39 | D: { id: 'details-tog', gid: 'details', isel: true }, |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 40 | |
| 41 | H: { id: 'hosts-tog', gid: 'endstation', isel: false }, |
| 42 | M: { id: 'offline-tog', gid: 'switch', isel: true }, |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 43 | P: { id: 'ports-tog', gid: 'ports', isel: true }, |
Simon Hunt | d0fe66c | 2015-06-10 10:16:36 -0700 | [diff] [blame] | 44 | B: { id: 'bkgrnd-tog', gid: 'map', isel: false }, |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 45 | S: { id: 'sprite-tog', gid: 'cloud', isel: false }, |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 46 | |
| 47 | //X: { id: 'nodelock-tog', gid: 'lock', isel: false }, |
| 48 | Z: { id: 'oblique-tog', gid: 'oblique', isel: false }, |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 49 | N: { id: 'filters-btn', gid: 'filters' }, |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 50 | L: { id: 'cycleLabels-btn', gid: 'cycleLabels' }, |
| 51 | R: { id: 'resetZoom-btn', gid: 'resetZoom' }, |
| 52 | |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 53 | V: { id: 'relatedIntents-btn', gid: 'relatedIntents' }, |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 54 | leftArrow: { id: 'prevIntent-btn', gid: 'prevIntent' }, |
| 55 | rightArrow: { id: 'nextIntent-btn', gid: 'nextIntent' }, |
| 56 | W: { id: 'intentTraffic-btn', gid: 'intentTraffic' }, |
| 57 | A: { id: 'allTraffic-btn', gid: 'allTraffic' }, |
| 58 | F: { id: 'flows-btn', gid: 'flows' }, |
| 59 | |
| 60 | E: { id: 'eqMaster-btn', gid: 'eqMaster' } |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 61 | }; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 62 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 63 | // initial toggle state: default settings and tag to key mapping |
| 64 | var defaultPrefsState = { |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 65 | summary: 1, |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 66 | insts: 1, |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 67 | detail: 1, |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 68 | hosts: 0, |
| 69 | offdev: 1, |
| 70 | porthl: 1, |
Simon Hunt | d0fe66c | 2015-06-10 10:16:36 -0700 | [diff] [blame] | 71 | bg: 0, |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 72 | spr: 0, |
Simon Hunt | d0fe66c | 2015-06-10 10:16:36 -0700 | [diff] [blame] | 73 | toolbar: 0 |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 74 | }, |
| 75 | prefsMap = { |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 76 | summary: 'O', |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 77 | 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 Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 87 | function init(_api_) { |
| 88 | api = _api_; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 89 | |
| 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 Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 99 | cachedState = ps.asNumbers(ps.getPrefs(cooktag)); |
| 100 | $log.debug('TOOLBAR---- read prefs state:', cachedState); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 101 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 102 | if (!cachedState) { |
| 103 | cachedState = topoDefPrefs(); |
| 104 | ps.setPrefs(cooktag, cachedState); |
| 105 | $log.debug('TOOLBAR---- Set default prefs state:', cachedState); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | angular.forEach(prefsMap, function (v, k) { |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 109 | var cfg = k2b[v]; |
| 110 | cfg && (cfg.isel = !!cachedState[k]); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 111 | }); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 114 | function initKeyData() { |
| 115 | keyData = d3.map(k2b); |
| 116 | keyData.forEach(function(key, value) { |
| 117 | var data = api.getActionEntry(key); |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 118 | value.cb = data[0]; // on-click callback |
| 119 | value.tt = data[1] + ' (' + key + ')'; // tooltip |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 120 | }); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 123 | 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 Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 127 | function addToggle(key, suppressIfMobile) { |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 128 | var v = keyData.get(key); |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 129 | if (suppressIfMobile && fs.isMobile()) { return; } |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 130 | v.tog = toolbar.addToggle(v.id, v.gid, v.isel, v.cb, v.tt); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 133 | function addFirstRow() { |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 134 | addToggle('I'); |
Simon Hunt | da2f3cc | 2015-03-19 15:11:57 -0700 | [diff] [blame] | 135 | addToggle('O'); |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 136 | addToggle('D'); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 137 | toolbar.addSeparator(); |
| 138 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 139 | addToggle('H'); |
| 140 | addToggle('M'); |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 141 | addToggle('P', true); |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 142 | addToggle('B'); |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 143 | addToggle('S', true); |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 144 | } |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 145 | |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 146 | function addSecondRow() { |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 147 | //addToggle('X'); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 148 | addToggle('Z'); |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 149 | addButton('N'); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 150 | addButton('L'); |
| 151 | addButton('R'); |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 152 | toolbar.addSeparator(); |
| 153 | addButton('E'); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 154 | } |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 155 | |
| 156 | function setOverlay(overlayId) { |
| 157 | if (!overlayId) { |
| 158 | $log.debug('CLEAR overlay'); |
| 159 | } else { |
| 160 | $log.debug('SET overlay', overlayId); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | function addOverlays() { |
| 165 | toolbar.addSeparator(); |
| 166 | |
| 167 | // generate radio button set for overlays; start with 'none' |
| 168 | var rset = [{ |
| 169 | gid: 'unknown', |
| 170 | tooltip: 'No Overlay', |
| 171 | cb: function () { setOverlay(); } |
| 172 | }]; |
| 173 | |
| 174 | tov.list().forEach(function (key) { |
| 175 | var ov = tov.overlay(key); |
| 176 | rset.push({ |
| 177 | gid: ov._glyphId, |
| 178 | tooltip: (ov.tooltip || '(no tooltip)'), |
| 179 | cb: function () { |
| 180 | setOverlay(ov.overlayId); |
| 181 | } |
| 182 | }); |
| 183 | }); |
| 184 | |
| 185 | toolbar.addRadioSet('topo-overlays', rset); |
| 186 | } |
| 187 | |
| 188 | // TODO: 3rd row needs to be swapped in/out based on selected overlay |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 189 | function addThirdRow() { |
| 190 | addButton('V'); |
| 191 | addButton('leftArrow'); |
| 192 | addButton('rightArrow'); |
| 193 | addButton('W'); |
| 194 | addButton('A'); |
| 195 | addButton('F'); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 196 | } |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 197 | |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 198 | function createToolbar() { |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 199 | initKeyData(); |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 200 | toolbar = tbs.createToolbar(name); |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 201 | addFirstRow(); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 202 | toolbar.addRow(); |
| 203 | addSecondRow(); |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 204 | addOverlays(); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 205 | toolbar.addRow(); |
| 206 | addThirdRow(); |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 207 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 208 | if (cachedState.toolbar) { |
| 209 | toolbar.show(); |
| 210 | } else { |
| 211 | toolbar.hide(); |
| 212 | } |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 215 | function destroyToolbar() { |
| 216 | tbs.destroyToolbar(name); |
| 217 | } |
| 218 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 219 | // allows us to ensure the button states track key strokes |
| 220 | function keyListener(key) { |
| 221 | var v = keyData.get(key); |
| 222 | |
| 223 | if (v) { |
| 224 | // we have a valid button mapping |
| 225 | if (v.tog) { |
| 226 | // it's a toggle button |
| 227 | v.tog.toggleNoCb(); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 232 | function toggleToolbar() { |
| 233 | toolbar.toggle(); |
| 234 | } |
| 235 | |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 236 | angular.module('ovTopo') |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 237 | .factory('TopoToolbarService', |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 238 | ['$log', 'FnService', 'ToolbarService', 'PrefsService', |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 239 | 'TopoOverlayService', |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 240 | |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 241 | function (_$log_, _fs_, _tbs_, _ps_, _tov_) { |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 242 | $log = _$log_; |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 243 | fs = _fs_; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 244 | tbs = _tbs_; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 245 | ps = _ps_; |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame^] | 246 | tov = _tov_; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 247 | |
| 248 | return { |
| 249 | init: init, |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 250 | createToolbar: createToolbar, |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 251 | destroyToolbar: destroyToolbar, |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 252 | keyListener: keyListener, |
| 253 | toggleToolbar: toggleToolbar |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 254 | }; |
| 255 | }]); |
| 256 | }()); |