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 | /* |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 18 | ONOS GUI -- Quick Help Layer |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 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 | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 36 | vb = '-200 0 400 400'; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 37 | |
| 38 | // State variables |
| 39 | var data = []; |
| 40 | |
| 41 | // DOM elements and the like |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 42 | var qhdiv = d3.select('#quickhelp'), |
| 43 | svg = qhdiv.select('svg'), |
| 44 | pane, |
| 45 | rect, |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 46 | items, |
| 47 | keyAgg; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 48 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 49 | // General functions |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 50 | function isA(a) { |
| 51 | return $.isArray(a) ? a : null; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 54 | var keyDisp = { |
| 55 | equals: '=', |
| 56 | dash: '-', |
| 57 | slash: '/', |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 58 | backQuote: '`', |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 59 | leftArrow: 'L-arrow', |
| 60 | upArrow: 'U-arrow', |
| 61 | rightArrow: 'R-arrow', |
| 62 | downArrow: 'D-arrow' |
| 63 | }; |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 64 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 65 | function cap(s) { |
| 66 | return s.replace(/^[a-z]/, function (m) { return m.toUpperCase(); }); |
| 67 | } |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 68 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 69 | function mkKeyDisp(id) { |
| 70 | var v = keyDisp[id] || id; |
| 71 | return cap(v); |
| 72 | } |
| 73 | |
| 74 | // layout configuration |
| 75 | var pad = 8, |
| 76 | offy = 45, |
| 77 | dy = 14, |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 78 | offDesc = 8; |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 79 | |
| 80 | // D3 magic |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 81 | function updateKeyItems() { |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 82 | var keyItems = items.selectAll('.keyItem') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 83 | .data(data); |
| 84 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 85 | var entering = keyItems.enter() |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 86 | .append('g') |
| 87 | .attr({ |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 88 | id: function (d) { return d.id; }, |
| 89 | class: 'keyItem' |
| 90 | }); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 91 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 92 | entering.each(function (d, i) { |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 93 | var el = d3.select(this), |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 94 | y = offy + dy * i; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 95 | |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 96 | if (d.id[0] === '_') { |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 97 | el.append('line') |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 98 | .attr({ x1: 0, y1: y, x2: 1, y2: y}); |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 99 | } else { |
| 100 | el.append('text') |
| 101 | .text(d.key) |
| 102 | .attr({ |
| 103 | class: 'key', |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 104 | x: 0, |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 105 | y: y |
| 106 | }); |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 107 | // NOTE: used for sizing column width... |
| 108 | keyAgg.append('text').text(d.key).attr('class', 'key'); |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 109 | |
| 110 | el.append('text') |
| 111 | .text(d.desc) |
| 112 | .attr({ |
| 113 | class: 'desc', |
| 114 | x: offDesc, |
| 115 | y: y |
| 116 | }); |
| 117 | } |
| 118 | }); |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 119 | |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 120 | var kbox = keyAgg.node().getBBox(); |
| 121 | items.selectAll('.desc').attr('x', kbox.width + offDesc); |
| 122 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 123 | var box = items.node().getBBox(), |
| 124 | paneW = box.width + pad * 2, |
| 125 | paneH = box.height + offy; |
| 126 | |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 127 | items.selectAll('line').attr('x2', box.width); |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 128 | items.attr('transform', translate(-paneW/2, -pad)); |
| 129 | rect.attr({ |
| 130 | width: paneW, |
| 131 | height: paneH, |
| 132 | transform: translate(-paneW/2-pad, 0) |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | function translate(x, y) { |
| 137 | return 'translate(' + x + ',' + y + ')'; |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | function aggregateData(bindings) { |
| 141 | var gmap = d3.map(bindings.globalKeys), |
| 142 | vmap = d3.map(bindings.viewKeys), |
| 143 | gkeys = gmap.keys(), |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 144 | vkeys = vmap.keys(), |
| 145 | vgest = bindings.viewGestures, |
| 146 | sep = 0; |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 147 | |
| 148 | gkeys.sort(); |
| 149 | vkeys.sort(); |
| 150 | |
| 151 | data = []; |
| 152 | gkeys.forEach(function (k) { |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 153 | addItem('glob', k, gmap.get(k)); |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 154 | }); |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 155 | addItem('sep'); |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 156 | vkeys.forEach(function (k) { |
| 157 | addItem('view', k, vmap.get(k)); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 158 | }); |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 159 | addItem('sep'); |
| 160 | vgest.forEach(function (g) { |
| 161 | if (g.length === 2) { |
| 162 | addItem('gest', g[0], g[1]); |
| 163 | } |
| 164 | }); |
| 165 | |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 166 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 167 | function addItem(type, k, d) { |
| 168 | var id = type + '-' + k, |
| 169 | a = isA(d), |
| 170 | desc = a && a[1]; |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 171 | |
| 172 | if (type === 'sep') { |
| 173 | data.push({ |
| 174 | id: '_' + sep++, |
| 175 | type: type |
| 176 | }); |
| 177 | } else if (type === 'gest') { |
| 178 | data.push({ |
| 179 | id: id, |
| 180 | type: type, |
| 181 | key: k, |
| 182 | desc: d |
| 183 | }); |
| 184 | } else if (desc) { |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 185 | data.push( |
| 186 | { |
| 187 | id: id, |
| 188 | type: type, |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 189 | key: mkKeyDisp(k), |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 190 | desc: desc |
| 191 | } |
| 192 | ); |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 193 | } |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 194 | } |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 197 | function popBind(bindings) { |
| 198 | pane = svg.append('g') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 199 | .attr({ |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 200 | class: 'help', |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 201 | opacity: 0 |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 202 | }); |
| 203 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 204 | rect = pane.append('rect') |
| 205 | .attr('rx', 8); |
| 206 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 207 | pane.append('text') |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 208 | .text('Quick Help') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 209 | .attr({ |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 210 | class: 'title', |
| 211 | dy: '1.2em', |
| 212 | transform: translate(-pad,0) |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 213 | }); |
| 214 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 215 | items = pane.append('g'); |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 216 | keyAgg = pane.append('g').style('visibility', 'hidden'); |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 217 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 218 | aggregateData(bindings); |
| 219 | updateKeyItems(); |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 220 | |
| 221 | _fade(1); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | function fadeBindings() { |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 225 | _fade(0); |
| 226 | } |
| 227 | |
| 228 | function _fade(o) { |
| 229 | svg.selectAll('g.help') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 230 | .transition() |
| 231 | .duration(fade) |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 232 | .attr('opacity', o); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | function addSvg() { |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 236 | svg = qhdiv.append('svg') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 237 | .attr({ |
| 238 | width: w, |
| 239 | height: h, |
| 240 | viewBox: vb |
| 241 | }); |
| 242 | } |
| 243 | |
| 244 | function removeSvg() { |
| 245 | svg.transition() |
| 246 | .delay(fade + 20) |
| 247 | .remove(); |
| 248 | } |
| 249 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 250 | function showQuickHelp(bindings) { |
| 251 | svg = qhdiv.select('svg'); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 252 | if (svg.empty()) { |
| 253 | addSvg(); |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 254 | popBind(bindings); |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 255 | } else { |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 256 | hideQuickHelp(); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 260 | function hideQuickHelp() { |
| 261 | svg = qhdiv.select('svg'); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 262 | if (!svg.empty()) { |
| 263 | fadeBindings(); |
| 264 | removeSvg(); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 265 | return true; |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 270 | onos.ui.addLib('quickHelp', { |
| 271 | show: showQuickHelp, |
| 272 | hide: hideQuickHelp |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 273 | }); |
| 274 | }(ONOS)); |