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 | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 26 | var $log, tbs, ps, 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 |
| 29 | var toolbar, keyData; |
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 }, |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 44 | B: { id: 'bkgrnd-tog', gid: 'map', isel: true }, |
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 = { |
| 65 | bg: 1, |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 66 | sprites: 0, |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 67 | insts: 1, |
| 68 | summary: 1, |
| 69 | detail: 1, |
| 70 | hosts: 0 |
| 71 | }, |
| 72 | prefsMap = { |
| 73 | bg: 'B', |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 74 | sprites: 'S', |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 75 | insts: 'I', |
| 76 | summary: 'O', |
| 77 | details: 'D', |
| 78 | hosts: 'H' |
| 79 | }; |
| 80 | |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 81 | function init(_api_) { |
| 82 | api = _api_; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 83 | |
| 84 | // retrieve initial toggle button settings from user prefs |
| 85 | setInitToggleState(); |
| 86 | } |
| 87 | |
| 88 | function topoDefPrefs() { |
| 89 | return angular.extend({}, defaultPrefsState); |
| 90 | } |
| 91 | |
| 92 | function setInitToggleState() { |
| 93 | var state = ps.getPrefs(cooktag); |
| 94 | $log.debug('TOOLBAR---- read prefs state:', state); |
| 95 | |
| 96 | if (!state) { |
| 97 | state = topoDefPrefs(); |
| 98 | ps.setPrefs(cooktag, state); |
| 99 | $log.debug('TOOLBAR---- Set default prefs state:', state); |
| 100 | } |
| 101 | |
| 102 | angular.forEach(prefsMap, function (v, k) { |
| 103 | k2b[v].isel = !!state[k]; |
| 104 | }); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 107 | function initKeyData() { |
| 108 | keyData = d3.map(k2b); |
| 109 | keyData.forEach(function(key, value) { |
| 110 | var data = api.getActionEntry(key); |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 111 | value.cb = data[0]; // on-click callback |
| 112 | value.tt = data[1] + ' (' + key + ')'; // tooltip |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 113 | }); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 116 | function addButton(key) { |
| 117 | var v = keyData.get(key); |
| 118 | v.btn = toolbar.addButton(v.id, v.gid, v.cb, v.tt); |
| 119 | } |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 120 | function addToggle(key) { |
| 121 | var v = keyData.get(key); |
| 122 | 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] | 123 | } |
| 124 | |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 125 | function addFirstRow() { |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 126 | addToggle('I'); |
Simon Hunt | da2f3cc | 2015-03-19 15:11:57 -0700 | [diff] [blame] | 127 | addToggle('O'); |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 128 | addToggle('D'); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 129 | toolbar.addSeparator(); |
| 130 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 131 | addToggle('H'); |
| 132 | addToggle('M'); |
| 133 | addToggle('P'); |
| 134 | addToggle('B'); |
Simon Hunt | 2052e5d | 2015-04-13 17:40:44 -0700 | [diff] [blame] | 135 | addToggle('S'); |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 136 | } |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 137 | function addSecondRow() { |
Bri Prebilic Cole | 7c98051 | 2015-03-25 12:31:29 -0700 | [diff] [blame] | 138 | //addToggle('X'); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 139 | addToggle('Z'); |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 140 | addButton('N'); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 141 | addButton('L'); |
| 142 | addButton('R'); |
| 143 | } |
| 144 | function addThirdRow() { |
| 145 | addButton('V'); |
| 146 | addButton('leftArrow'); |
| 147 | addButton('rightArrow'); |
| 148 | addButton('W'); |
| 149 | addButton('A'); |
| 150 | addButton('F'); |
| 151 | toolbar.addSeparator(); |
| 152 | addButton('E'); |
| 153 | } |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 154 | |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 155 | function createToolbar() { |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 156 | initKeyData(); |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 157 | toolbar = tbs.createToolbar(name); |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 158 | addFirstRow(); |
Bri Prebilic Cole | 812f6c0 | 2015-03-24 17:10:33 -0700 | [diff] [blame] | 159 | toolbar.addRow(); |
| 160 | addSecondRow(); |
| 161 | toolbar.addRow(); |
| 162 | addThirdRow(); |
Bri Prebilic Cole | 04b4140 | 2015-03-18 17:25:34 -0700 | [diff] [blame] | 163 | toolbar.show(); |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 166 | function destroyToolbar() { |
| 167 | tbs.destroyToolbar(name); |
| 168 | } |
| 169 | |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 170 | // allows us to ensure the button states track key strokes |
| 171 | function keyListener(key) { |
| 172 | var v = keyData.get(key); |
| 173 | |
| 174 | if (v) { |
| 175 | // we have a valid button mapping |
| 176 | if (v.tog) { |
| 177 | // it's a toggle button |
| 178 | v.tog.toggleNoCb(); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 183 | function toggleToolbar() { |
| 184 | toolbar.toggle(); |
| 185 | } |
| 186 | |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 187 | angular.module('ovTopo') |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 188 | .factory('TopoToolbarService', |
| 189 | ['$log', 'ToolbarService', 'PrefsService', |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 190 | |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 191 | function (_$log_, _tbs_, _ps_) { |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 192 | $log = _$log_; |
| 193 | tbs = _tbs_; |
Simon Hunt | c7ae795 | 2015-04-08 18:59:27 -0700 | [diff] [blame] | 194 | ps = _ps_; |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 195 | |
| 196 | return { |
| 197 | init: init, |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 198 | createToolbar: createToolbar, |
Bri Prebilic Cole | d621905 | 2015-03-19 14:34:02 -0700 | [diff] [blame] | 199 | destroyToolbar: destroyToolbar, |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 200 | keyListener: keyListener, |
| 201 | toggleToolbar: toggleToolbar |
Bri Prebilic Cole | 4db8dce | 2015-03-18 13:57:24 -0700 | [diff] [blame] | 202 | }; |
| 203 | }]); |
| 204 | }()); |