blob: c0767ef104cf82cdf09be897df6b2ce2d6d87300 [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.
Simon Hunt988c6fc2014-11-20 17:43:03 -080022 */
23
24(function (onos){
25 'use strict';
26
Simon Hunt988c6fc2014-11-20 17:43:03 -080027 // Config variables
28 var w = '100%',
29 h = '80%',
Simon Hunt209155e2014-11-21 12:16:09 -080030 fade = 500,
Simon Hunt5cef9062014-11-24 15:24:35 -080031 vb = '-200 0 400 400';
Simon Hunt988c6fc2014-11-20 17:43:03 -080032
Simon Hunta1a00c22014-12-04 16:10:40 -080033 // layout configuration
34 var pad = 10,
35 offy = 45,
36 sepYDelta = 20,
37 colXDelta = 16,
38 yTextSpc = 12,
39 offDesc = 8;
40
Simon Hunt988c6fc2014-11-20 17:43:03 -080041 // State variables
Simon Hunta1a00c22014-12-04 16:10:40 -080042 var data = [],
43 yCount;
Simon Hunt988c6fc2014-11-20 17:43:03 -080044
45 // DOM elements and the like
Simon Hunt5cef9062014-11-24 15:24:35 -080046 var qhdiv = d3.select('#quickhelp'),
47 svg = qhdiv.select('svg'),
Simon Hunta1a00c22014-12-04 16:10:40 -080048 pane, rect, items;
Simon Hunt988c6fc2014-11-20 17:43:03 -080049
Simon Hunt5cef9062014-11-24 15:24:35 -080050 // General functions
Simon Hunta1a00c22014-12-04 16:10:40 -080051 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 Hunt988c6fc2014-11-20 17:43:03 -080056 }
57
Simon Hunt5cef9062014-11-24 15:24:35 -080058 var keyDisp = {
59 equals: '=',
60 dash: '-',
61 slash: '/',
Simon Hunt41effbe2014-12-04 09:41:44 -080062 backSlash: '\\',
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -080063 backQuote: '`',
Simon Hunt5cef9062014-11-24 15:24:35 -080064 leftArrow: 'L-arrow',
65 upArrow: 'U-arrow',
66 rightArrow: 'R-arrow',
67 downArrow: 'D-arrow'
68 };
Simon Hunt56ef0fe2014-11-21 08:24:43 -080069
Simon Hunt5cef9062014-11-24 15:24:35 -080070 function mkKeyDisp(id) {
71 var v = keyDisp[id] || id;
72 return cap(v);
73 }
74
Simon Hunta1a00c22014-12-04 16:10:40 -080075 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 Hunt5cef9062014-11-24 15:24:35 -080080
Simon Hunta1a00c22014-12-04 16:10:40 -080081 function addContent(el, data, ri) {
82 var xCount = 0,
83 clsPfx = 'qh-r' + ri + '-c';
Simon Hunt988c6fc2014-11-20 17:43:03 -080084
Simon Hunta1a00c22014-12-04 16:10:40 -080085 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 Hunt56ef0fe2014-11-21 08:24:43 -0800114 });
Simon Hunt988c6fc2014-11-20 17:43:03 -0800115
Simon Hunta1a00c22014-12-04 16:10:40 -0800116 // 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 Hunt988c6fc2014-11-20 17:43:03 -0800147 var el = d3.select(this),
Simon Hunta1a00c22014-12-04 16:10:40 -0800148 sep = r.type === 'sep',
149 dy;
Simon Hunt988c6fc2014-11-20 17:43:03 -0800150
Simon Hunta1a00c22014-12-04 16:10:40 -0800151 el.attr('transform', translate(0, yCount));
152
153 if (sep) {
154 addSeparator(el, i);
155 yCount += sepYDelta;
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800156 } else {
Simon Hunta1a00c22014-12-04 16:10:40 -0800157 dy = addContent(el, r.data, i);
158 yCount += dy;
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800159 }
160 });
Simon Hunt5cef9062014-11-24 15:24:35 -0800161
Simon Hunta1a00c22014-12-04 16:10:40 -0800162 // size the backing rectangle
163 var ibox = items.node().getBBox(),
164 paneW = ibox.width + pad * 2,
165 paneH = ibox.height + offy;
Simon Hunt87514342014-11-24 16:41:27 -0800166
Simon Hunta1a00c22014-12-04 16:10:40 -0800167 items.selectAll('.qhrowsep').attr('x2', ibox.width);
Simon Hunt5cef9062014-11-24 15:24:35 -0800168 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 Hunta1a00c22014-12-04 16:10:40 -0800174
Simon Hunt5cef9062014-11-24 15:24:35 -0800175 }
176
177 function translate(x, y) {
178 return 'translate(' + x + ',' + y + ')';
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800179 }
180
Simon Hunta1a00c22014-12-04 16:10:40 -0800181 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 Hunt56ef0fe2014-11-21 08:24:43 -0800248 function aggregateData(bindings) {
Simon Hunta1162d82014-12-03 14:34:43 -0800249 var hf = '_helpFormat',
250 gmap = d3.map(bindings.globalKeys),
Simon Hunta1a00c22014-12-04 16:10:40 -0800251 gfmt = bindings.globalFormat,
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800252 vmap = d3.map(bindings.viewKeys),
Simon Hunt87514342014-11-24 16:41:27 -0800253 vgest = bindings.viewGestures,
Simon Hunta1a00c22014-12-04 16:10:40 -0800254 vfmt, vkeys;
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800255
Simon Hunta1162d82014-12-03 14:34:43 -0800256 // filter out help format entry
Simon Hunta1a00c22014-12-04 16:10:40 -0800257 vfmt = checkFmt(vmap.get(hf));
Simon Hunta1162d82014-12-03 14:34:43 -0800258 vmap.remove(hf);
Simon Hunta1162d82014-12-03 14:34:43 -0800259
Simon Hunta1a00c22014-12-04 16:10:40 -0800260 // if bad (or no) format, fallback to sorted keys
261 if (!vfmt) {
262 vkeys = vmap.keys();
263 vfmt = vkeys.sort();
264 }
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800265
266 data = [];
Simon Hunt87514342014-11-24 16:41:27 -0800267
Simon Hunta1a00c22014-12-04 16:10:40 -0800268 addRow(mkMapRow(gmap, gfmt));
269 addRow();
270 addRow(isA(vfmt[0]) ? mkColumnarRow(vmap, vfmt) : mkMapRow(vmap, vfmt));
271 addRow();
272 addRow(mkArrRow(vgest));
Simon Hunt988c6fc2014-11-20 17:43:03 -0800273 }
274
Simon Hunta1a00c22014-12-04 16:10:40 -0800275
Simon Hunt5cef9062014-11-24 15:24:35 -0800276 function popBind(bindings) {
277 pane = svg.append('g')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800278 .attr({
Simon Hunt5cef9062014-11-24 15:24:35 -0800279 class: 'help',
Simon Hunt988c6fc2014-11-20 17:43:03 -0800280 opacity: 0
Simon Hunt988c6fc2014-11-20 17:43:03 -0800281 });
282
Simon Hunt5cef9062014-11-24 15:24:35 -0800283 rect = pane.append('rect')
284 .attr('rx', 8);
285
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800286 pane.append('text')
Simon Hunt5cef9062014-11-24 15:24:35 -0800287 .text('Quick Help')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800288 .attr({
Simon Hunt5cef9062014-11-24 15:24:35 -0800289 class: 'title',
290 dy: '1.2em',
291 transform: translate(-pad,0)
Simon Hunt988c6fc2014-11-20 17:43:03 -0800292 });
293
Simon Hunt5cef9062014-11-24 15:24:35 -0800294 items = pane.append('g');
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800295 aggregateData(bindings);
296 updateKeyItems();
Simon Hunt5cef9062014-11-24 15:24:35 -0800297
298 _fade(1);
Simon Hunt988c6fc2014-11-20 17:43:03 -0800299 }
300
301 function fadeBindings() {
Simon Hunt5cef9062014-11-24 15:24:35 -0800302 _fade(0);
303 }
304
305 function _fade(o) {
306 svg.selectAll('g.help')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800307 .transition()
308 .duration(fade)
Simon Hunt5cef9062014-11-24 15:24:35 -0800309 .attr('opacity', o);
Simon Hunt988c6fc2014-11-20 17:43:03 -0800310 }
311
312 function addSvg() {
Simon Hunt5cef9062014-11-24 15:24:35 -0800313 svg = qhdiv.append('svg')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800314 .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 Hunt5cef9062014-11-24 15:24:35 -0800327 function showQuickHelp(bindings) {
328 svg = qhdiv.select('svg');
Simon Hunt988c6fc2014-11-20 17:43:03 -0800329 if (svg.empty()) {
330 addSvg();
Simon Hunt5cef9062014-11-24 15:24:35 -0800331 popBind(bindings);
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800332 } else {
Simon Hunt5cef9062014-11-24 15:24:35 -0800333 hideQuickHelp();
Simon Hunt988c6fc2014-11-20 17:43:03 -0800334 }
335 }
336
Simon Hunt5cef9062014-11-24 15:24:35 -0800337 function hideQuickHelp() {
338 svg = qhdiv.select('svg');
Simon Hunt988c6fc2014-11-20 17:43:03 -0800339 if (!svg.empty()) {
340 fadeBindings();
341 removeSvg();
Simon Hunt988c6fc2014-11-20 17:43:03 -0800342 return true;
343 }
344 return false;
345 }
346
Simon Hunt5cef9062014-11-24 15:24:35 -0800347 onos.ui.addLib('quickHelp', {
348 show: showQuickHelp,
349 hide: hideQuickHelp
Simon Hunt988c6fc2014-11-20 17:43:03 -0800350 });
351}(ONOS));