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 | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 28 | // API: |
| 29 | // getActionEntry |
| 30 | // setUpKeys |
| 31 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 32 | // internal state |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 33 | var toolbar, keyData, cachedState, thirdRow; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 34 | |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 35 | // constants |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 36 | var name = 'topo-tbar', |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 37 | cooktag = 'topo_prefs', |
| 38 | soa = 'switchOverlayActions: ', |
| 39 | selOver = 'Select overlay here ⇧'; |
| 40 | |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 41 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 42 | // key to button mapping data |
| 43 | var k2b = { |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 44 | O: { id: 'summary-tog', gid: 'summary', isel: true}, |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 45 | I: { id: 'instance-tog', gid: 'uiAttached', isel: true }, |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 46 | D: { id: 'details-tog', gid: 'details', isel: true }, |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 47 | H: { id: 'hosts-tog', gid: 'endstation', isel: false }, |
| 48 | M: { id: 'offline-tog', gid: 'switch', isel: true }, |
Bri Prebilic Cole | deca6e9 | 2015-03-19 12:03:14 -0700 | [diff] [blame] | 49 | P: { id: 'ports-tog', gid: 'ports', isel: true }, |
Simon Hunt | d0fe66c | 2015-06-10 10:16:36 -0700 | [diff] [blame] | 50 | B: { id: 'bkgrnd-tog', gid: 'map', isel: false }, |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 51 | S: { id: 'sprite-tog', gid: 'cloud', isel: false }, |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 52 | |
| 53 | //X: { id: 'nodelock-tog', gid: 'lock', isel: false }, |
| 54 | Z: { id: 'oblique-tog', gid: 'oblique', isel: false }, |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 55 | N: { id: 'filters-btn', gid: 'filters' }, |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 56 | L: { id: 'cycleLabels-btn', gid: 'cycleLabels' }, |
| 57 | R: { id: 'resetZoom-btn', gid: 'resetZoom' }, |
| 58 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 59 | E: { id: 'eqMaster-btn', gid: 'eqMaster' } |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 60 | }; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 61 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 62 | 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 Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 69 | // initial toggle state: default settings and tag to key mapping |
| 70 | var defaultPrefsState = { |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 71 | summary: 1, |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 72 | insts: 1, |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 73 | detail: 1, |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 74 | hosts: 0, |
| 75 | offdev: 1, |
| 76 | porthl: 1, |
Simon Hunt | d0fe66c | 2015-06-10 10:16:36 -0700 | [diff] [blame] | 77 | bg: 0, |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 78 | spr: 0, |
Simon Hunt | d0fe66c | 2015-06-10 10:16:36 -0700 | [diff] [blame] | 79 | toolbar: 0 |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 80 | }, |
| 81 | prefsMap = { |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 82 | summary: 'O', |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 83 | 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 Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 93 | function init(_api_) { |
| 94 | api = _api_; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 95 | |
| 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 Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 105 | cachedState = ps.asNumbers(ps.getPrefs(cooktag)); |
| 106 | $log.debug('TOOLBAR---- read prefs state:', cachedState); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 107 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 108 | if (!cachedState) { |
| 109 | cachedState = topoDefPrefs(); |
| 110 | ps.setPrefs(cooktag, cachedState); |
| 111 | $log.debug('TOOLBAR---- Set default prefs state:', cachedState); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | angular.forEach(prefsMap, function (v, k) { |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 115 | var cfg = k2b[v]; |
| 116 | cfg && (cfg.isel = !!cachedState[k]); |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 117 | }); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 120 | function initKeyData() { |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 121 | // TODO: use angular forEach instead of d3.map |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 122 | keyData = d3.map(k2b); |
| 123 | keyData.forEach(function(key, value) { |
| 124 | var data = api.getActionEntry(key); |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 125 | value.cb = data[0]; // on-click callback |
| 126 | value.tt = data[1] + ' (' + key + ')'; // tooltip |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 127 | }); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 130 | function addButton(key) { |
| 131 | var v = keyData.get(key); |
| 132 | v.btn = toolbar.addButton(v.id, v.gid, v.cb, v.tt); |
| 133 | } |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 134 | |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 135 | function addToggle(key, suppressIfMobile) { |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 136 | var v = keyData.get(key); |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 137 | if (suppressIfMobile && fs.isMobile()) { return; } |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 138 | 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] | 139 | } |
| 140 | |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 141 | function addFirstRow() { |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 142 | addToggle('I'); |
Simon Hunt | da2f3cc | 2015-03-19 15:11:57 -0700 | [diff] [blame] | 143 | addToggle('O'); |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 144 | addToggle('D'); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 145 | toolbar.addSeparator(); |
| 146 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 147 | addToggle('H'); |
| 148 | addToggle('M'); |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 149 | addToggle('P', true); |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 150 | addToggle('B'); |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 151 | addToggle('S', true); |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 152 | } |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 153 | |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 154 | function addSecondRow() { |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 155 | //addToggle('X'); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 156 | addToggle('Z'); |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 157 | addButton('N'); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 158 | addButton('L'); |
| 159 | addButton('R'); |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 160 | toolbar.addSeparator(); |
| 161 | addButton('E'); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 162 | } |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 163 | |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 164 | function addOverlays() { |
| 165 | toolbar.addSeparator(); |
| 166 | |
| 167 | // generate radio button set for overlays; start with 'none' |
| 168 | var rset = [{ |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 169 | gid: 'topo', |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 170 | tooltip: 'No Overlay', |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 171 | cb: function () { |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 172 | tov.tbSelection(null, switchOverlayActions); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 173 | } |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 174 | }]; |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 175 | tov.augmentRbset(rset, switchOverlayActions); |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 176 | toolbar.addRadioSet('topo-overlays', rset); |
| 177 | } |
| 178 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 179 | // 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 Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 223 | } |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 224 | |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 225 | function createToolbar() { |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 226 | initKeyData(); |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 227 | toolbar = tbs.createToolbar(name); |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 228 | addFirstRow(); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 229 | toolbar.addRow(); |
| 230 | addSecondRow(); |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 231 | addOverlays(); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame^] | 232 | thirdRow = toolbar.addRow(); |
| 233 | thirdRow.setText(selOver); |
| 234 | thirdRow.classed('right', true); |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 235 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 236 | if (cachedState.toolbar) { |
| 237 | toolbar.show(); |
| 238 | } else { |
| 239 | toolbar.hide(); |
| 240 | } |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 243 | function destroyToolbar() { |
| 244 | tbs.destroyToolbar(name); |
| 245 | } |
| 246 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 247 | // 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 Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 260 | function toggleToolbar() { |
| 261 | toolbar.toggle(); |
| 262 | } |
| 263 | |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 264 | angular.module('ovTopo') |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 265 | .factory('TopoToolbarService', |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 266 | ['$log', 'FnService', 'ToolbarService', 'PrefsService', |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 267 | 'TopoOverlayService', |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 268 | |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 269 | function (_$log_, _fs_, _tbs_, _ps_, _tov_) { |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 270 | $log = _$log_; |
Bri Prebilic Cole | d874546 | 2015-06-01 16:08:57 -0700 | [diff] [blame] | 271 | fs = _fs_; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 272 | tbs = _tbs_; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 273 | ps = _ps_; |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 274 | tov = _tov_; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 275 | |
| 276 | return { |
| 277 | init: init, |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 278 | createToolbar: createToolbar, |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 279 | destroyToolbar: destroyToolbar, |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 280 | keyListener: keyListener, |
| 281 | toggleToolbar: toggleToolbar |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 282 | }; |
| 283 | }]); |
| 284 | }()); |