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