blob: f1776dfa768ed5018d64aca2aeda2378c344d190 [file] [log] [blame]
Simon Hunta3dd9572014-11-20 15:22:41 -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 -- Feedback layer
19
20 Defines the feedback layer for the UI. Used to give user visual feedback
21 of choices, typically responding to keystrokes.
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 = 200,
35 fade = 750,
36 showFor = 2000,
37 vb = '-200 -' + (h/2) + ' 400 ' + h,
38 xpad = 20,
39 ypad = 10;
40
41 // State variables
42 var timer = null,
43 data = [];
44
45 // DOM elements and the like
46 var fb = d3.select('#feedback');
47
48 var svg = fb.append('svg').attr({
49 width: w,
50 height: h,
51 viewBox: vb
52 });
53
54 function computeBox(el) {
55 var text = el.select('text'),
56 box = text.node().getBBox();
57
58 // center
59 box.x = -box.width / 2;
60 box.y = -box.height / 2;
61
62 // add some padding
63 box.x -= xpad;
64 box.width += xpad * 2;
65 box.y -= ypad;
66 box.height += ypad * 2;
67
68 return box;
69 }
70
71 function updateFeedback() {
72 var items = svg.selectAll('.feedbackItem')
73 .data(data);
74
75 var entering = items.enter()
76 .append('g')
77 .attr({
78 class: 'feedbackItem',
79 opacity: 0
80 })
81 .transition()
82 .duration(fade)
83 .attr('opacity', 1);
84
85 entering.each(function (d) {
86 var el = d3.select(this),
87 box;
88
89 d.el = el;
90 el.append('rect').attr({ rx: 10, ry: 10});
91 el.append('text').text(d.label);
92 box = computeBox(el);
93 el.select('rect').attr(box);
94 });
95
96 items.exit()
97 .transition()
98 .duration(fade)
99 .attr({ opacity: 0})
100 .remove();
101 }
102
103 function clearFlash() {
104 if (timer) {
105 clearInterval(timer);
106 }
107 data = [];
108 updateFeedback();
109 }
110
111 // for now, simply display some text feedback
112 function flash(text) {
113 // cancel old scheduled event if there was one
114 if (timer) {
115 clearInterval(timer);
116 }
117 timer = setInterval(function () {
118 clearFlash();
119 }, showFor);
120
121 data = [{
122 label: text
123 }];
124 updateFeedback();
125 }
126
127 onos.ui.addLib('feedback', {
128 flash: flash
129 });
130}(ONOS));