Simon Hunt

GUI -- Added sample view to demonstrate radio buttons.

...@@ -81,6 +81,7 @@ ...@@ -81,6 +81,7 @@
81 <!-- TODO: replace with template marker and inject refs server-side --> 81 <!-- TODO: replace with template marker and inject refs server-side -->
82 <script src="sample2.js"></script> 82 <script src="sample2.js"></script>
83 <script src="sampleAlt2.js"></script> 83 <script src="sampleAlt2.js"></script>
84 + <script src="sampleRadio.js"></script>
84 85
85 <!-- finally, build the UI--> 86 <!-- finally, build the UI-->
86 <script type="text/javascript"> 87 <script type="text/javascript">
......
...@@ -48,12 +48,8 @@ ...@@ -48,12 +48,8 @@
48 } 48 }
49 49
50 #mast span.radio { 50 #mast span.radio {
51 - color: darkslateblue;
52 font-size: 10pt; 51 font-size: 10pt;
53 -} 52 + margin: 4px 2px;
54 -
55 -#mast span.radio {
56 - margin: 4px 0;
57 border: 1px dotted #222; 53 border: 1px dotted #222;
58 padding: 1px 6px; 54 padding: 1px 6px;
59 color: #eee; 55 color: #eee;
......
...@@ -51,7 +51,8 @@ ...@@ -51,7 +51,8 @@
51 errorCount = 0; 51 errorCount = 0;
52 52
53 // DOM elements etc. 53 // DOM elements etc.
54 - var $view; 54 + var $view,
55 + $mastRadio;
55 56
56 57
57 // .......................................................... 58 // ..........................................................
...@@ -204,6 +205,9 @@ ...@@ -204,6 +205,9 @@
204 // the incoming view, then unload it... 205 // the incoming view, then unload it...
205 if (current.view && (current.view.vid !== view.vid)) { 206 if (current.view && (current.view.vid !== view.vid)) {
206 current.view.unload(); 207 current.view.unload();
208 + // detach radio buttons, if they were there..
209 + $('#mastRadio').children().detach();
210 +
207 } 211 }
208 212
209 // cache new view and context 213 // cache new view and context
...@@ -223,6 +227,61 @@ ...@@ -223,6 +227,61 @@
223 view.load(current.ctx); 227 view.load(current.ctx);
224 } 228 }
225 229
230 + // generate 'unique' id by prefixing view id
231 + function uid(view, id) {
232 + return view.vid + '-' + id;
233 + }
234 +
235 + // restore id by removing view id prefix
236 + function unUid(view, uid) {
237 + var re = new RegExp('^' + view.vid + '-');
238 + return uid.replace(re, '');
239 + }
240 +
241 + function setRadioButtons(vid, btnSet, callback) {
242 + var view = views[vid],
243 + btnG;
244 +
245 + // lazily create the buttons...
246 + if (!(btnG = view.radioButtons)) {
247 + btnG = d3.select(document.createElement('div'));
248 +
249 + btnSet.forEach(function (btn, i) {
250 + var bid = btn.id || 'b' + i,
251 + txt = btn.text || 'Button #' + i,
252 + b = btnG.append('span')
253 + .attr({
254 + id: uid(view, bid),
255 + class: 'radio'
256 + })
257 + .text(txt);
258 + if (i === 0) {
259 + b.classed('active', true);
260 + }
261 + });
262 +
263 + btnG.selectAll('span')
264 + .on('click', function (d) {
265 + var btn = d3.select(this),
266 + bid = btn.attr('id'),
267 + act = btn.classed('active');
268 +
269 + if (!act) {
270 + $mastRadio.selectAll('span')
271 + .classed('active', false);
272 + btn.classed('active', true);
273 +
274 + callback(view.token(), unUid(view, bid));
275 + }
276 + });
277 +
278 + view.radioButtons = btnG;
279 + }
280 +
281 + // attach the buttons to the masthead
282 + $mastRadio.node().appendChild(btnG.node());
283 + }
284 +
226 function resize(e) { 285 function resize(e) {
227 d3.selectAll('.onosView').call(setViewDimensions); 286 d3.selectAll('.onosView').call(setViewDimensions);
228 // allow current view to react to resize event... 287 // allow current view to react to resize event...
...@@ -256,8 +315,9 @@ ...@@ -256,8 +315,9 @@
256 if (validateViewArgs(vid)) { 315 if (validateViewArgs(vid)) {
257 this.nid = nid; // explicit navitem id (can be null) 316 this.nid = nid; // explicit navitem id (can be null)
258 this.cb = $.isPlainObject(cb) ? cb : {}; // callbacks 317 this.cb = $.isPlainObject(cb) ? cb : {}; // callbacks
259 - this.$div = null; // view not yet added to DOM 318 + this.$div = null; // view not yet added to DOM
260 - this.ok = true; // valid view 319 + this.radioButtons = null; // no radio buttons yet
320 + this.ok = true; // valid view
261 } 321 }
262 322
263 } 323 }
...@@ -289,7 +349,8 @@ ...@@ -289,7 +349,8 @@
289 349
290 // functions 350 // functions
291 width: this.width, 351 width: this.width,
292 - height: this.height 352 + height: this.height,
353 + setRadio: this.setRadio
293 } 354 }
294 }, 355 },
295 356
...@@ -368,8 +429,11 @@ ...@@ -368,8 +429,11 @@
368 429
369 height: function () { 430 height: function () {
370 return $(this.$div.node()).height(); 431 return $(this.$div.node()).height();
371 - } 432 + },
372 433
434 + setRadio: function (btnSet, cb) {
435 + setRadioButtons(this.vid, btnSet, cb);
436 + }
373 437
374 // TODO: consider schedule, clearTimer, etc. 438 // TODO: consider schedule, clearTimer, etc.
375 }; 439 };
...@@ -465,6 +529,7 @@ ...@@ -465,6 +529,7 @@
465 built = true; 529 built = true;
466 530
467 $view = d3.select('#view'); 531 $view = d3.select('#view');
532 + $mastRadio = d3.select('#mastRadio');
468 533
469 $(window).on('hashchange', hash); 534 $(window).on('hashchange', hash);
470 $(window).on('resize', resize); 535 $(window).on('resize', resize);
...@@ -487,4 +552,4 @@ ...@@ -487,4 +552,4 @@
487 }; 552 };
488 }; 553 };
489 554
490 -}(jQuery));
...\ No newline at end of file ...\ No newline at end of file
555 +}(jQuery));
......
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 +/*
18 + Sample view to illustrate radio buttons.
19 +
20 + @author Simon Hunt
21 + */
22 +
23 +(function (onos) {
24 + 'use strict';
25 +
26 + var data = [ 'Yo, radio button set...', 'Time to shine' ],
27 + btnSet = [
28 + { id: 'b1', text: 'First Button' },
29 + { id: 'b2', text: 'Second Button' },
30 + { id: 'b3', text: 'Third Button' }
31 + ],
32 + btnLookup = {};
33 +
34 + btnSet.forEach(function (b) {
35 + btnLookup[b.id] = b;
36 + });
37 +
38 + // invoked when the view is loaded
39 + function load(view, ctx) {
40 + view.setRadio(btnSet, doRadio);
41 +
42 + view.$div.selectAll('p')
43 + .data(data)
44 + .enter()
45 + .append('p')
46 + .text(function (d) { return d; })
47 + .style('padding', '2px 8px');
48 + }
49 +
50 + function doRadio(view, id) {
51 + view.$div.append('p')
52 + .text('You pressed the ' + btnLookup[id].text)
53 + .style({
54 + 'font-size': '10pt',
55 + color: 'green',
56 + padding: '0 20px',
57 + margin: '2px'
58 + });
59 + }
60 +
61 + // == register the view here, with links to lifecycle callbacks
62 +
63 + onos.ui.addView('sampleRadio', {
64 + reset: true, // empty the div on reset
65 + load: load
66 + });
67 +
68 +}(ONOS));