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 | |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 29 | // Config variables |
| 30 | var w = '100%', |
| 31 | h = '80%', |
Simon Hunt | 209155e | 2014-11-21 12:16:09 -0800 | [diff] [blame] | 32 | fade = 500, |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 33 | vb = '-200 0 400 400'; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 34 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 35 | // layout configuration |
| 36 | var pad = 10, |
| 37 | offy = 45, |
| 38 | sepYDelta = 20, |
| 39 | colXDelta = 16, |
| 40 | yTextSpc = 12, |
| 41 | offDesc = 8; |
| 42 | |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 43 | // State variables |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 44 | var data = [], |
| 45 | yCount; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 46 | |
| 47 | // DOM elements and the like |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 48 | var qhdiv = d3.select('#quickhelp'), |
| 49 | svg = qhdiv.select('svg'), |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 50 | pane, rect, items; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 51 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 52 | // General functions |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 53 | function isA(a) { return $.isArray(a) ? a : null; } |
| 54 | function isS(s) { return typeof s === 'string'; } |
| 55 | |
| 56 | function cap(s) { |
| 57 | return s.replace(/^[a-z]/, function (m) { return m.toUpperCase(); }); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 60 | var keyDisp = { |
| 61 | equals: '=', |
| 62 | dash: '-', |
| 63 | slash: '/', |
Simon Hunt | 41effbe | 2014-12-04 09:41:44 -0800 | [diff] [blame] | 64 | backSlash: '\\', |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 65 | backQuote: '`', |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 66 | leftArrow: 'L-arrow', |
| 67 | upArrow: 'U-arrow', |
| 68 | rightArrow: 'R-arrow', |
| 69 | downArrow: 'D-arrow' |
| 70 | }; |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 71 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 72 | function mkKeyDisp(id) { |
| 73 | var v = keyDisp[id] || id; |
| 74 | return cap(v); |
| 75 | } |
| 76 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 77 | function addSeparator(el, i) { |
| 78 | var y = sepYDelta/2 - 5; |
| 79 | el.append('line') |
| 80 | .attr({ 'class': 'qhrowsep', x1: 0, y1: y, x2: 0, y2: y }); |
| 81 | } |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 82 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 83 | function addContent(el, data, ri) { |
| 84 | var xCount = 0, |
| 85 | clsPfx = 'qh-r' + ri + '-c'; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 86 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 87 | function addColumn(el, c, i) { |
| 88 | var cls = clsPfx + i, |
| 89 | oy = 0, |
| 90 | aggKey = el.append('g').attr('visibility', 'hidden'), |
| 91 | gcol = el.append('g').attr({ |
| 92 | 'class': cls, |
| 93 | transform: translate(xCount, 0) |
| 94 | }); |
| 95 | |
| 96 | c.forEach(function (j) { |
| 97 | var k = j[0], |
| 98 | v = j[1]; |
| 99 | |
| 100 | if (k !== '-') { |
| 101 | aggKey.append('text').text(k); |
| 102 | |
| 103 | gcol.append('text').text(k) |
| 104 | .attr({ |
| 105 | 'class': 'key', |
| 106 | y: oy |
| 107 | }); |
| 108 | gcol.append('text').text(v) |
| 109 | .attr({ |
| 110 | 'class': 'desc', |
| 111 | y: oy |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | oy += yTextSpc; |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 116 | }); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 117 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 118 | // adjust position of descriptions, based on widest key |
| 119 | var kbox = aggKey.node().getBBox(), |
| 120 | ox = kbox.width + offDesc; |
| 121 | gcol.selectAll('.desc').attr('x', ox); |
| 122 | aggKey.remove(); |
| 123 | |
| 124 | // now update x-offset for next column |
| 125 | var bbox = gcol.node().getBBox(); |
| 126 | xCount += bbox.width + colXDelta; |
| 127 | } |
| 128 | |
| 129 | data.forEach(function (d, i) { |
| 130 | addColumn(el, d, i); |
| 131 | }); |
| 132 | |
| 133 | // finally, return the height of the row.. |
| 134 | return el.node().getBBox().height; |
| 135 | } |
| 136 | |
| 137 | function updateKeyItems() { |
| 138 | var rows = items.selectAll('.qhRow').data(data); |
| 139 | |
| 140 | yCount = offy; |
| 141 | |
| 142 | var entering = rows.enter() |
| 143 | .append('g') |
| 144 | .attr({ |
| 145 | 'class': 'qhrow' |
| 146 | }); |
| 147 | |
| 148 | entering.each(function (r, i) { |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 149 | var el = d3.select(this), |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 150 | sep = r.type === 'sep', |
| 151 | dy; |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 152 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 153 | el.attr('transform', translate(0, yCount)); |
| 154 | |
| 155 | if (sep) { |
| 156 | addSeparator(el, i); |
| 157 | yCount += sepYDelta; |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 158 | } else { |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 159 | dy = addContent(el, r.data, i); |
| 160 | yCount += dy; |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 161 | } |
| 162 | }); |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 163 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 164 | // size the backing rectangle |
| 165 | var ibox = items.node().getBBox(), |
| 166 | paneW = ibox.width + pad * 2, |
| 167 | paneH = ibox.height + offy; |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 168 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 169 | items.selectAll('.qhrowsep').attr('x2', ibox.width); |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 170 | items.attr('transform', translate(-paneW/2, -pad)); |
| 171 | rect.attr({ |
| 172 | width: paneW, |
| 173 | height: paneH, |
| 174 | transform: translate(-paneW/2-pad, 0) |
| 175 | }); |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 176 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | function translate(x, y) { |
| 180 | return 'translate(' + x + ',' + y + ')'; |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 183 | function checkFmt(fmt) { |
| 184 | // should be a single array of keys, |
| 185 | // or array of arrays of keys (one per column). |
| 186 | // return null if there is a problem. |
| 187 | var a = isA(fmt), |
| 188 | n = a && a.length, |
| 189 | ns = 0, |
| 190 | na = 0; |
| 191 | |
| 192 | if (n) { |
| 193 | // it is an array which has some content |
| 194 | a.forEach(function (d) { |
| 195 | isA(d) && na++; |
| 196 | isS(d) && ns++; |
| 197 | }); |
| 198 | if (na === n || ns === n) { |
| 199 | // all arrays or all strings... |
| 200 | return a; |
| 201 | } |
| 202 | } |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | function buildBlock(map, fmt) { |
| 207 | var b = []; |
| 208 | fmt.forEach(function (k) { |
| 209 | var v = map.get(k), |
| 210 | a = isA(v), |
| 211 | d = (a && a[1]); |
| 212 | |
| 213 | // '-' marks a separator; d is the description |
| 214 | if (k === '-' || d) { |
| 215 | b.push([mkKeyDisp(k), d]); |
| 216 | } |
| 217 | }); |
| 218 | return b; |
| 219 | } |
| 220 | |
| 221 | function emptyRow() { |
| 222 | return { type: 'row', data: []}; |
| 223 | } |
| 224 | |
| 225 | function mkArrRow(fmt) { |
| 226 | var d = emptyRow(); |
| 227 | d.data.push(fmt); |
| 228 | return d; |
| 229 | } |
| 230 | |
| 231 | function mkColumnarRow(map, fmt) { |
| 232 | var d = emptyRow(); |
| 233 | fmt.forEach(function (a) { |
| 234 | d.data.push(buildBlock(map, a)); |
| 235 | }); |
| 236 | return d; |
| 237 | } |
| 238 | |
| 239 | function mkMapRow(map, fmt) { |
| 240 | var d = emptyRow(); |
| 241 | d.data.push(buildBlock(map, fmt)); |
| 242 | return d; |
| 243 | } |
| 244 | |
| 245 | function addRow(row) { |
| 246 | var d = row || { type: 'sep' }; |
| 247 | data.push(d); |
| 248 | } |
| 249 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 250 | function aggregateData(bindings) { |
Simon Hunt | a1162d8 | 2014-12-03 14:34:43 -0800 | [diff] [blame] | 251 | var hf = '_helpFormat', |
| 252 | gmap = d3.map(bindings.globalKeys), |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 253 | gfmt = bindings.globalFormat, |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 254 | vmap = d3.map(bindings.viewKeys), |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 255 | vgest = bindings.viewGestures, |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 256 | vfmt, vkeys; |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 257 | |
Simon Hunt | a1162d8 | 2014-12-03 14:34:43 -0800 | [diff] [blame] | 258 | // filter out help format entry |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 259 | vfmt = checkFmt(vmap.get(hf)); |
Simon Hunt | a1162d8 | 2014-12-03 14:34:43 -0800 | [diff] [blame] | 260 | vmap.remove(hf); |
Simon Hunt | a1162d8 | 2014-12-03 14:34:43 -0800 | [diff] [blame] | 261 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 262 | // if bad (or no) format, fallback to sorted keys |
| 263 | if (!vfmt) { |
| 264 | vkeys = vmap.keys(); |
| 265 | vfmt = vkeys.sort(); |
| 266 | } |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 267 | |
| 268 | data = []; |
Simon Hunt | 8751434 | 2014-11-24 16:41:27 -0800 | [diff] [blame] | 269 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 270 | addRow(mkMapRow(gmap, gfmt)); |
| 271 | addRow(); |
| 272 | addRow(isA(vfmt[0]) ? mkColumnarRow(vmap, vfmt) : mkMapRow(vmap, vfmt)); |
| 273 | addRow(); |
| 274 | addRow(mkArrRow(vgest)); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Simon Hunt | a1a00c2 | 2014-12-04 16:10:40 -0800 | [diff] [blame] | 277 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 278 | function popBind(bindings) { |
| 279 | pane = svg.append('g') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 280 | .attr({ |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 281 | class: 'help', |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 282 | opacity: 0 |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 283 | }); |
| 284 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 285 | rect = pane.append('rect') |
| 286 | .attr('rx', 8); |
| 287 | |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 288 | pane.append('text') |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 289 | .text('Quick Help') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 290 | .attr({ |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 291 | class: 'title', |
| 292 | dy: '1.2em', |
| 293 | transform: translate(-pad,0) |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 294 | }); |
| 295 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 296 | items = pane.append('g'); |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 297 | aggregateData(bindings); |
| 298 | updateKeyItems(); |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 299 | |
| 300 | _fade(1); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | function fadeBindings() { |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 304 | _fade(0); |
| 305 | } |
| 306 | |
| 307 | function _fade(o) { |
| 308 | svg.selectAll('g.help') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 309 | .transition() |
| 310 | .duration(fade) |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 311 | .attr('opacity', o); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | function addSvg() { |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 315 | svg = qhdiv.append('svg') |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 316 | .attr({ |
| 317 | width: w, |
| 318 | height: h, |
| 319 | viewBox: vb |
| 320 | }); |
| 321 | } |
| 322 | |
| 323 | function removeSvg() { |
| 324 | svg.transition() |
| 325 | .delay(fade + 20) |
| 326 | .remove(); |
| 327 | } |
| 328 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 329 | function showQuickHelp(bindings) { |
| 330 | svg = qhdiv.select('svg'); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 331 | if (svg.empty()) { |
| 332 | addSvg(); |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 333 | popBind(bindings); |
Simon Hunt | 56ef0fe | 2014-11-21 08:24:43 -0800 | [diff] [blame] | 334 | } else { |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 335 | hideQuickHelp(); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 339 | function hideQuickHelp() { |
| 340 | svg = qhdiv.select('svg'); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 341 | if (!svg.empty()) { |
| 342 | fadeBindings(); |
| 343 | removeSvg(); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 344 | return true; |
| 345 | } |
| 346 | return false; |
| 347 | } |
| 348 | |
Simon Hunt | 5cef906 | 2014-11-24 15:24:35 -0800 | [diff] [blame] | 349 | onos.ui.addLib('quickHelp', { |
| 350 | show: showQuickHelp, |
| 351 | hide: hideQuickHelp |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 352 | }); |
| 353 | }(ONOS)); |