blob: a0da6aaaa5ff04f117137f223fc2e67da6004e90 [file] [log] [blame]
Simon Hunt988c6fc2014-11-20 17:43:03 -08001/*
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 Hunt5cef9062014-11-24 15:24:35 -080018 ONOS GUI -- Quick Help Layer
Simon Hunt988c6fc2014-11-20 17:43:03 -080019
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 Hunt988c6fc2014-11-20 17:43:03 -080029 // Config variables
30 var w = '100%',
31 h = '80%',
Simon Hunt209155e2014-11-21 12:16:09 -080032 fade = 500,
Simon Hunt5cef9062014-11-24 15:24:35 -080033 vb = '-200 0 400 400';
Simon Hunt988c6fc2014-11-20 17:43:03 -080034
Simon Hunta1a00c22014-12-04 16:10:40 -080035 // layout configuration
36 var pad = 10,
37 offy = 45,
38 sepYDelta = 20,
39 colXDelta = 16,
40 yTextSpc = 12,
41 offDesc = 8;
42
Simon Hunt988c6fc2014-11-20 17:43:03 -080043 // State variables
Simon Hunta1a00c22014-12-04 16:10:40 -080044 var data = [],
45 yCount;
Simon Hunt988c6fc2014-11-20 17:43:03 -080046
47 // DOM elements and the like
Simon Hunt5cef9062014-11-24 15:24:35 -080048 var qhdiv = d3.select('#quickhelp'),
49 svg = qhdiv.select('svg'),
Simon Hunta1a00c22014-12-04 16:10:40 -080050 pane, rect, items;
Simon Hunt988c6fc2014-11-20 17:43:03 -080051
Simon Hunt5cef9062014-11-24 15:24:35 -080052 // General functions
Simon Hunta1a00c22014-12-04 16:10:40 -080053 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 Hunt988c6fc2014-11-20 17:43:03 -080058 }
59
Simon Hunt5cef9062014-11-24 15:24:35 -080060 var keyDisp = {
61 equals: '=',
62 dash: '-',
63 slash: '/',
Simon Hunt41effbe2014-12-04 09:41:44 -080064 backSlash: '\\',
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -080065 backQuote: '`',
Simon Hunt5cef9062014-11-24 15:24:35 -080066 leftArrow: 'L-arrow',
67 upArrow: 'U-arrow',
68 rightArrow: 'R-arrow',
69 downArrow: 'D-arrow'
70 };
Simon Hunt56ef0fe2014-11-21 08:24:43 -080071
Simon Hunt5cef9062014-11-24 15:24:35 -080072 function mkKeyDisp(id) {
73 var v = keyDisp[id] || id;
74 return cap(v);
75 }
76
Simon Hunta1a00c22014-12-04 16:10:40 -080077 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 Hunt5cef9062014-11-24 15:24:35 -080082
Simon Hunta1a00c22014-12-04 16:10:40 -080083 function addContent(el, data, ri) {
84 var xCount = 0,
85 clsPfx = 'qh-r' + ri + '-c';
Simon Hunt988c6fc2014-11-20 17:43:03 -080086
Simon Hunta1a00c22014-12-04 16:10:40 -080087 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 Hunt56ef0fe2014-11-21 08:24:43 -0800116 });
Simon Hunt988c6fc2014-11-20 17:43:03 -0800117
Simon Hunta1a00c22014-12-04 16:10:40 -0800118 // 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 Hunt988c6fc2014-11-20 17:43:03 -0800149 var el = d3.select(this),
Simon Hunta1a00c22014-12-04 16:10:40 -0800150 sep = r.type === 'sep',
151 dy;
Simon Hunt988c6fc2014-11-20 17:43:03 -0800152
Simon Hunta1a00c22014-12-04 16:10:40 -0800153 el.attr('transform', translate(0, yCount));
154
155 if (sep) {
156 addSeparator(el, i);
157 yCount += sepYDelta;
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800158 } else {
Simon Hunta1a00c22014-12-04 16:10:40 -0800159 dy = addContent(el, r.data, i);
160 yCount += dy;
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800161 }
162 });
Simon Hunt5cef9062014-11-24 15:24:35 -0800163
Simon Hunta1a00c22014-12-04 16:10:40 -0800164 // size the backing rectangle
165 var ibox = items.node().getBBox(),
166 paneW = ibox.width + pad * 2,
167 paneH = ibox.height + offy;
Simon Hunt87514342014-11-24 16:41:27 -0800168
Simon Hunta1a00c22014-12-04 16:10:40 -0800169 items.selectAll('.qhrowsep').attr('x2', ibox.width);
Simon Hunt5cef9062014-11-24 15:24:35 -0800170 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 Hunta1a00c22014-12-04 16:10:40 -0800176
Simon Hunt5cef9062014-11-24 15:24:35 -0800177 }
178
179 function translate(x, y) {
180 return 'translate(' + x + ',' + y + ')';
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800181 }
182
Simon Hunta1a00c22014-12-04 16:10:40 -0800183 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 Hunt56ef0fe2014-11-21 08:24:43 -0800250 function aggregateData(bindings) {
Simon Hunta1162d82014-12-03 14:34:43 -0800251 var hf = '_helpFormat',
252 gmap = d3.map(bindings.globalKeys),
Simon Hunta1a00c22014-12-04 16:10:40 -0800253 gfmt = bindings.globalFormat,
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800254 vmap = d3.map(bindings.viewKeys),
Simon Hunt87514342014-11-24 16:41:27 -0800255 vgest = bindings.viewGestures,
Simon Hunta1a00c22014-12-04 16:10:40 -0800256 vfmt, vkeys;
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800257
Simon Hunta1162d82014-12-03 14:34:43 -0800258 // filter out help format entry
Simon Hunta1a00c22014-12-04 16:10:40 -0800259 vfmt = checkFmt(vmap.get(hf));
Simon Hunta1162d82014-12-03 14:34:43 -0800260 vmap.remove(hf);
Simon Hunta1162d82014-12-03 14:34:43 -0800261
Simon Hunta1a00c22014-12-04 16:10:40 -0800262 // if bad (or no) format, fallback to sorted keys
263 if (!vfmt) {
264 vkeys = vmap.keys();
265 vfmt = vkeys.sort();
266 }
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800267
268 data = [];
Simon Hunt87514342014-11-24 16:41:27 -0800269
Simon Hunta1a00c22014-12-04 16:10:40 -0800270 addRow(mkMapRow(gmap, gfmt));
271 addRow();
272 addRow(isA(vfmt[0]) ? mkColumnarRow(vmap, vfmt) : mkMapRow(vmap, vfmt));
273 addRow();
274 addRow(mkArrRow(vgest));
Simon Hunt988c6fc2014-11-20 17:43:03 -0800275 }
276
Simon Hunta1a00c22014-12-04 16:10:40 -0800277
Simon Hunt5cef9062014-11-24 15:24:35 -0800278 function popBind(bindings) {
279 pane = svg.append('g')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800280 .attr({
Simon Hunt5cef9062014-11-24 15:24:35 -0800281 class: 'help',
Simon Hunt988c6fc2014-11-20 17:43:03 -0800282 opacity: 0
Simon Hunt988c6fc2014-11-20 17:43:03 -0800283 });
284
Simon Hunt5cef9062014-11-24 15:24:35 -0800285 rect = pane.append('rect')
286 .attr('rx', 8);
287
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800288 pane.append('text')
Simon Hunt5cef9062014-11-24 15:24:35 -0800289 .text('Quick Help')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800290 .attr({
Simon Hunt5cef9062014-11-24 15:24:35 -0800291 class: 'title',
292 dy: '1.2em',
293 transform: translate(-pad,0)
Simon Hunt988c6fc2014-11-20 17:43:03 -0800294 });
295
Simon Hunt5cef9062014-11-24 15:24:35 -0800296 items = pane.append('g');
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800297 aggregateData(bindings);
298 updateKeyItems();
Simon Hunt5cef9062014-11-24 15:24:35 -0800299
300 _fade(1);
Simon Hunt988c6fc2014-11-20 17:43:03 -0800301 }
302
303 function fadeBindings() {
Simon Hunt5cef9062014-11-24 15:24:35 -0800304 _fade(0);
305 }
306
307 function _fade(o) {
308 svg.selectAll('g.help')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800309 .transition()
310 .duration(fade)
Simon Hunt5cef9062014-11-24 15:24:35 -0800311 .attr('opacity', o);
Simon Hunt988c6fc2014-11-20 17:43:03 -0800312 }
313
314 function addSvg() {
Simon Hunt5cef9062014-11-24 15:24:35 -0800315 svg = qhdiv.append('svg')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800316 .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 Hunt5cef9062014-11-24 15:24:35 -0800329 function showQuickHelp(bindings) {
330 svg = qhdiv.select('svg');
Simon Hunt988c6fc2014-11-20 17:43:03 -0800331 if (svg.empty()) {
332 addSvg();
Simon Hunt5cef9062014-11-24 15:24:35 -0800333 popBind(bindings);
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800334 } else {
Simon Hunt5cef9062014-11-24 15:24:35 -0800335 hideQuickHelp();
Simon Hunt988c6fc2014-11-20 17:43:03 -0800336 }
337 }
338
Simon Hunt5cef9062014-11-24 15:24:35 -0800339 function hideQuickHelp() {
340 svg = qhdiv.select('svg');
Simon Hunt988c6fc2014-11-20 17:43:03 -0800341 if (!svg.empty()) {
342 fadeBindings();
343 removeSvg();
Simon Hunt988c6fc2014-11-20 17:43:03 -0800344 return true;
345 }
346 return false;
347 }
348
Simon Hunt5cef9062014-11-24 15:24:35 -0800349 onos.ui.addLib('quickHelp', {
350 show: showQuickHelp,
351 hide: hideQuickHelp
Simon Hunt988c6fc2014-11-20 17:43:03 -0800352 });
353}(ONOS));