Simon Hunt | a3dd957 | 2014-11-20 15:22:41 -0800 | [diff] [blame] | 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 | 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, |
Thomas Vachuska | c59658c | 2014-11-21 13:13:36 -0800 | [diff] [blame] | 35 | fade = 200, |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 36 | showFor = 1200, |
Simon Hunt | a3dd957 | 2014-11-20 15:22:41 -0800 | [diff] [blame] | 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 | |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 48 | var svg; |
| 49 | |
| 50 | //var svg = fb.append('svg').attr({ |
| 51 | // width: w, |
| 52 | // height: h, |
| 53 | // viewBox: vb |
| 54 | //}); |
Simon Hunt | a3dd957 | 2014-11-20 15:22:41 -0800 | [diff] [blame] | 55 | |
| 56 | function computeBox(el) { |
| 57 | var text = el.select('text'), |
| 58 | box = text.node().getBBox(); |
| 59 | |
| 60 | // center |
| 61 | box.x = -box.width / 2; |
| 62 | box.y = -box.height / 2; |
| 63 | |
| 64 | // add some padding |
| 65 | box.x -= xpad; |
| 66 | box.width += xpad * 2; |
| 67 | box.y -= ypad; |
| 68 | box.height += ypad * 2; |
| 69 | |
| 70 | return box; |
| 71 | } |
| 72 | |
| 73 | function updateFeedback() { |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 74 | if (!svg) { |
| 75 | svg = fb.append('svg').attr({ |
| 76 | width: w, |
| 77 | height: h, |
| 78 | viewBox: vb |
| 79 | }); |
| 80 | } |
| 81 | |
Simon Hunt | a3dd957 | 2014-11-20 15:22:41 -0800 | [diff] [blame] | 82 | var items = svg.selectAll('.feedbackItem') |
| 83 | .data(data); |
| 84 | |
| 85 | var entering = items.enter() |
| 86 | .append('g') |
| 87 | .attr({ |
| 88 | class: 'feedbackItem', |
| 89 | opacity: 0 |
| 90 | }) |
| 91 | .transition() |
| 92 | .duration(fade) |
| 93 | .attr('opacity', 1); |
| 94 | |
| 95 | entering.each(function (d) { |
| 96 | var el = d3.select(this), |
| 97 | box; |
| 98 | |
| 99 | d.el = el; |
| 100 | el.append('rect').attr({ rx: 10, ry: 10}); |
| 101 | el.append('text').text(d.label); |
| 102 | box = computeBox(el); |
| 103 | el.select('rect').attr(box); |
| 104 | }); |
| 105 | |
| 106 | items.exit() |
| 107 | .transition() |
| 108 | .duration(fade) |
| 109 | .attr({ opacity: 0}) |
| 110 | .remove(); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 111 | |
| 112 | if (svg && data.length === 0) { |
| 113 | svg.transition() |
| 114 | .delay(fade + 10) |
| 115 | .remove(); |
| 116 | svg = null; |
| 117 | } |
Simon Hunt | a3dd957 | 2014-11-20 15:22:41 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | function clearFlash() { |
| 121 | if (timer) { |
| 122 | clearInterval(timer); |
| 123 | } |
| 124 | data = []; |
| 125 | updateFeedback(); |
| 126 | } |
| 127 | |
| 128 | // for now, simply display some text feedback |
| 129 | function flash(text) { |
| 130 | // cancel old scheduled event if there was one |
| 131 | if (timer) { |
| 132 | clearInterval(timer); |
| 133 | } |
| 134 | timer = setInterval(function () { |
| 135 | clearFlash(); |
| 136 | }, showFor); |
| 137 | |
| 138 | data = [{ |
| 139 | label: text |
| 140 | }]; |
| 141 | updateFeedback(); |
| 142 | } |
| 143 | |
| 144 | onos.ui.addLib('feedback', { |
| 145 | flash: flash |
| 146 | }); |
| 147 | }(ONOS)); |