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