Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 -- Keymap Layer |
| 19 | |
| 20 | Defines the key-map layer for the UI. Used to give user a list of |
| 21 | key bindings; both global, and for the current view. |
| 22 | |
| 23 | @author Simon Hunt |
| 24 | */ |
| 25 | |
| 26 | (function (onos){ |
| 27 | 'use strict'; |
| 28 | |
| 29 | // API's |
| 30 | var api = onos.api; |
| 31 | |
| 32 | // Config variables |
| 33 | var w = '100%', |
| 34 | h = '80%', |
Simon Hunt | 209155e | 2014-11-21 12:16:09 -0800 | [diff] [blame] | 35 | fade = 500, |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 36 | vb = '-220 -220 440 440', |
| 37 | paneW = 400, |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 38 | paneH = 280, |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 39 | offy = 65, |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 40 | dy = 14, |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 41 | offKey = 40, |
| 42 | offDesc = offKey + 50, |
| 43 | lineW = paneW - (2*offKey); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 44 | |
| 45 | // State variables |
| 46 | var data = []; |
| 47 | |
| 48 | // DOM elements and the like |
| 49 | var kmdiv = d3.select('#keymap'); |
| 50 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 51 | function isA(a) { |
| 52 | return $.isArray(a) ? a : null; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 55 | |
| 56 | var svg = kmdiv.select('svg'), |
| 57 | pane; |
| 58 | |
| 59 | function updateKeyItems() { |
| 60 | var items = pane.selectAll('.keyItem') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 61 | .data(data); |
| 62 | |
| 63 | var entering = items.enter() |
| 64 | .append('g') |
| 65 | .attr({ |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 66 | id: function (d) { return d.id; }, |
| 67 | class: 'keyItem' |
| 68 | }); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 69 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 70 | entering.each(function (d, i) { |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 71 | var el = d3.select(this), |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 72 | y = offy + dy * i; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 73 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 74 | if (d.id === '_') { |
| 75 | el.append('line') |
| 76 | .attr({ |
| 77 | class: 'sep', |
| 78 | x1: offKey, |
| 79 | y1: y, |
| 80 | x2: offKey + lineW, |
| 81 | y2: y |
| 82 | }); |
| 83 | } else { |
| 84 | el.append('text') |
| 85 | .text(d.key) |
| 86 | .attr({ |
| 87 | class: 'key', |
| 88 | x: offKey, |
| 89 | y: y |
| 90 | }); |
| 91 | |
| 92 | el.append('text') |
| 93 | .text(d.desc) |
| 94 | .attr({ |
| 95 | class: 'desc', |
| 96 | x: offDesc, |
| 97 | y: y |
| 98 | }); |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | function aggregateData(bindings) { |
| 104 | var gmap = d3.map(bindings.globalKeys), |
| 105 | vmap = d3.map(bindings.viewKeys), |
| 106 | gkeys = gmap.keys(), |
| 107 | vkeys = vmap.keys(); |
| 108 | |
| 109 | gkeys.sort(); |
| 110 | vkeys.sort(); |
| 111 | |
| 112 | data = []; |
| 113 | gkeys.forEach(function (k) { |
| 114 | addItem('global', k, gmap.get(k)); |
| 115 | }); |
| 116 | addItem('separator'); |
| 117 | vkeys.forEach(function (k) { |
| 118 | addItem('view', k, vmap.get(k)); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 119 | }); |
| 120 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 121 | function addItem(type, k, d) { |
| 122 | var id = type + '-' + k, |
| 123 | a = isA(d), |
| 124 | desc = a && a[1]; |
| 125 | if (desc) { |
| 126 | data.push( |
| 127 | { |
| 128 | id: id, |
| 129 | type: type, |
| 130 | key: k, |
| 131 | desc: desc |
| 132 | } |
| 133 | ); |
| 134 | } else if (type === 'separator') { |
| 135 | data.push({ |
| 136 | id: '_', |
| 137 | type: type |
| 138 | }); |
| 139 | } |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 140 | } |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 141 | |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 144 | function populateBindings(bindings) { |
| 145 | svg.append('g') |
| 146 | .attr({ |
| 147 | class: 'keyBindings', |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 148 | transform: 'translate(-200,-200)', |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 149 | opacity: 0 |
| 150 | }) |
| 151 | .transition() |
| 152 | .duration(fade) |
| 153 | .attr('opacity', 1); |
| 154 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 155 | pane = svg.select('g'); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 156 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 157 | pane.append('rect') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 158 | .attr({ |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 159 | width: paneW, |
| 160 | height: paneH, |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 161 | rx: 8 |
| 162 | }); |
| 163 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 164 | pane.append('text') |
| 165 | .text('Keyboard Shortcuts') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 166 | .attr({ |
| 167 | x: 200, |
| 168 | y: 0, |
| 169 | dy: '1.4em', |
| 170 | class: 'title' |
| 171 | }); |
| 172 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 173 | aggregateData(bindings); |
| 174 | updateKeyItems(); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | function fadeBindings() { |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 178 | svg.selectAll('g.keyBindings') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 179 | .transition() |
| 180 | .duration(fade) |
| 181 | .attr('opacity', 0); |
| 182 | } |
| 183 | |
| 184 | function addSvg() { |
| 185 | svg = kmdiv.append('svg') |
| 186 | .attr({ |
| 187 | width: w, |
| 188 | height: h, |
| 189 | viewBox: vb |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | function removeSvg() { |
| 194 | svg.transition() |
| 195 | .delay(fade + 20) |
| 196 | .remove(); |
| 197 | } |
| 198 | |
| 199 | function showKeyMap(bindings) { |
| 200 | svg = kmdiv.select('svg'); |
| 201 | if (svg.empty()) { |
| 202 | addSvg(); |
| 203 | populateBindings(bindings); |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 204 | } else { |
| 205 | hideKeyMap(); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | function hideKeyMap() { |
| 210 | svg = kmdiv.select('svg'); |
| 211 | if (!svg.empty()) { |
| 212 | fadeBindings(); |
| 213 | removeSvg(); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 214 | return true; |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | onos.ui.addLib('keymap', { |
| 220 | show: showKeyMap, |
| 221 | hide: hideKeyMap |
| 222 | }); |
| 223 | }(ONOS)); |