blob: 3d96a56ffb3cd1ef2c7b58b389c3dc801e7a1094 [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/*
18 ONOS GUI -- Keymap Layer
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 Hunt209155e2014-11-21 12:16:09 -080035 fade = 500,
Simon Hunt56ef0fe2014-11-21 08:24:43 -080036 vb = '-220 -220 440 440',
37 paneW = 400,
Thomas Vachuska47635c62014-11-22 01:21:36 -080038 paneH = 280,
Simon Hunt56ef0fe2014-11-21 08:24:43 -080039 offy = 65,
Thomas Vachuska47635c62014-11-22 01:21:36 -080040 dy = 14,
Simon Hunt56ef0fe2014-11-21 08:24:43 -080041 offKey = 40,
42 offDesc = offKey + 50,
43 lineW = paneW - (2*offKey);
Simon Hunt988c6fc2014-11-20 17:43:03 -080044
45 // State variables
46 var data = [];
47
48 // DOM elements and the like
49 var kmdiv = d3.select('#keymap');
50
Simon Hunt56ef0fe2014-11-21 08:24:43 -080051 function isA(a) {
52 return $.isArray(a) ? a : null;
Simon Hunt988c6fc2014-11-20 17:43:03 -080053 }
54
Simon Hunt56ef0fe2014-11-21 08:24:43 -080055
56 var svg = kmdiv.select('svg'),
57 pane;
58
59 function updateKeyItems() {
60 var items = pane.selectAll('.keyItem')
Simon Hunt988c6fc2014-11-20 17:43:03 -080061 .data(data);
62
63 var entering = items.enter()
64 .append('g')
65 .attr({
Simon Hunt56ef0fe2014-11-21 08:24:43 -080066 id: function (d) { return d.id; },
67 class: 'keyItem'
68 });
Simon Hunt988c6fc2014-11-20 17:43:03 -080069
Simon Hunt56ef0fe2014-11-21 08:24:43 -080070 entering.each(function (d, i) {
Simon Hunt988c6fc2014-11-20 17:43:03 -080071 var el = d3.select(this),
Simon Hunt56ef0fe2014-11-21 08:24:43 -080072 y = offy + dy * i;
Simon Hunt988c6fc2014-11-20 17:43:03 -080073
Simon Hunt56ef0fe2014-11-21 08:24:43 -080074 if (d.id === '_') {
75 el.append('line')
76 .attr({
77 class: 'sep',
78 x1: offKey,
79 y1: y,
80 x2: offKey + lineW,
81 y2: y
82 });
83 } else {
84 el.append('text')
85 .text(d.key)
86 .attr({
87 class: 'key',
88 x: offKey,
89 y: y
90 });
91
92 el.append('text')
93 .text(d.desc)
94 .attr({
95 class: 'desc',
96 x: offDesc,
97 y: y
98 });
99 }
100 });
101 }
102
103 function aggregateData(bindings) {
104 var gmap = d3.map(bindings.globalKeys),
105 vmap = d3.map(bindings.viewKeys),
106 gkeys = gmap.keys(),
107 vkeys = vmap.keys();
108
109 gkeys.sort();
110 vkeys.sort();
111
112 data = [];
113 gkeys.forEach(function (k) {
114 addItem('global', k, gmap.get(k));
115 });
116 addItem('separator');
117 vkeys.forEach(function (k) {
118 addItem('view', k, vmap.get(k));
Simon Hunt988c6fc2014-11-20 17:43:03 -0800119 });
120
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800121 function addItem(type, k, d) {
122 var id = type + '-' + k,
123 a = isA(d),
124 desc = a && a[1];
125 if (desc) {
126 data.push(
127 {
128 id: id,
129 type: type,
130 key: k,
131 desc: desc
132 }
133 );
134 } else if (type === 'separator') {
135 data.push({
136 id: '_',
137 type: type
138 });
139 }
Simon Hunt988c6fc2014-11-20 17:43:03 -0800140 }
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800141
Simon Hunt988c6fc2014-11-20 17:43:03 -0800142 }
143
Simon Hunt988c6fc2014-11-20 17:43:03 -0800144 function populateBindings(bindings) {
145 svg.append('g')
146 .attr({
147 class: 'keyBindings',
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800148 transform: 'translate(-200,-200)',
Simon Hunt988c6fc2014-11-20 17:43:03 -0800149 opacity: 0
150 })
151 .transition()
152 .duration(fade)
153 .attr('opacity', 1);
154
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800155 pane = svg.select('g');
Simon Hunt988c6fc2014-11-20 17:43:03 -0800156
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800157 pane.append('rect')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800158 .attr({
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800159 width: paneW,
160 height: paneH,
Simon Hunt988c6fc2014-11-20 17:43:03 -0800161 rx: 8
162 });
163
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800164 pane.append('text')
165 .text('Keyboard Shortcuts')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800166 .attr({
167 x: 200,
168 y: 0,
169 dy: '1.4em',
170 class: 'title'
171 });
172
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800173 aggregateData(bindings);
174 updateKeyItems();
Simon Hunt988c6fc2014-11-20 17:43:03 -0800175 }
176
177 function fadeBindings() {
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800178 svg.selectAll('g.keyBindings')
Simon Hunt988c6fc2014-11-20 17:43:03 -0800179 .transition()
180 .duration(fade)
181 .attr('opacity', 0);
182 }
183
184 function addSvg() {
185 svg = kmdiv.append('svg')
186 .attr({
187 width: w,
188 height: h,
189 viewBox: vb
190 });
191 }
192
193 function removeSvg() {
194 svg.transition()
195 .delay(fade + 20)
196 .remove();
197 }
198
199 function showKeyMap(bindings) {
200 svg = kmdiv.select('svg');
201 if (svg.empty()) {
202 addSvg();
203 populateBindings(bindings);
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800204 } else {
205 hideKeyMap();
Simon Hunt988c6fc2014-11-20 17:43:03 -0800206 }
207 }
208
209 function hideKeyMap() {
210 svg = kmdiv.select('svg');
211 if (!svg.empty()) {
212 fadeBindings();
213 removeSvg();
Simon Hunt988c6fc2014-11-20 17:43:03 -0800214 return true;
215 }
216 return false;
217 }
218
219 onos.ui.addLib('keymap', {
220 show: showKeyMap,
221 hide: hideKeyMap
222 });
223}(ONOS));