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. |
Simon Hunt | a3dd957 | 2014-11-20 15:22:41 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | (function (onos){ |
| 25 | 'use strict'; |
| 26 | |
| 27 | // API's |
| 28 | var api = onos.api; |
| 29 | |
| 30 | // Config variables |
| 31 | var w = '100%', |
| 32 | h = 200, |
Thomas Vachuska | c59658c | 2014-11-21 13:13:36 -0800 | [diff] [blame] | 33 | fade = 200, |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 34 | showFor = 1200, |
Simon Hunt | a3dd957 | 2014-11-20 15:22:41 -0800 | [diff] [blame] | 35 | vb = '-200 -' + (h/2) + ' 400 ' + h, |
| 36 | xpad = 20, |
| 37 | ypad = 10; |
| 38 | |
| 39 | // State variables |
| 40 | var timer = null, |
| 41 | data = []; |
| 42 | |
| 43 | // DOM elements and the like |
| 44 | var fb = d3.select('#feedback'); |
| 45 | |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 46 | var svg; |
| 47 | |
| 48 | //var svg = fb.append('svg').attr({ |
| 49 | // width: w, |
| 50 | // height: h, |
| 51 | // viewBox: vb |
| 52 | //}); |
Simon Hunt | a3dd957 | 2014-11-20 15:22:41 -0800 | [diff] [blame] | 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() { |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 72 | if (!svg) { |
| 73 | svg = fb.append('svg').attr({ |
| 74 | width: w, |
| 75 | height: h, |
| 76 | viewBox: vb |
| 77 | }); |
| 78 | } |
| 79 | |
Simon Hunt | a3dd957 | 2014-11-20 15:22:41 -0800 | [diff] [blame] | 80 | var items = svg.selectAll('.feedbackItem') |
| 81 | .data(data); |
| 82 | |
| 83 | var entering = items.enter() |
| 84 | .append('g') |
| 85 | .attr({ |
| 86 | class: 'feedbackItem', |
| 87 | opacity: 0 |
| 88 | }) |
| 89 | .transition() |
| 90 | .duration(fade) |
| 91 | .attr('opacity', 1); |
| 92 | |
| 93 | entering.each(function (d) { |
| 94 | var el = d3.select(this), |
| 95 | box; |
| 96 | |
| 97 | d.el = el; |
| 98 | el.append('rect').attr({ rx: 10, ry: 10}); |
| 99 | el.append('text').text(d.label); |
| 100 | box = computeBox(el); |
| 101 | el.select('rect').attr(box); |
| 102 | }); |
| 103 | |
| 104 | items.exit() |
| 105 | .transition() |
| 106 | .duration(fade) |
| 107 | .attr({ opacity: 0}) |
| 108 | .remove(); |
Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -0800 | [diff] [blame] | 109 | |
| 110 | if (svg && data.length === 0) { |
| 111 | svg.transition() |
| 112 | .delay(fade + 10) |
| 113 | .remove(); |
| 114 | svg = null; |
| 115 | } |
Simon Hunt | a3dd957 | 2014-11-20 15:22:41 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | function clearFlash() { |
| 119 | if (timer) { |
| 120 | clearInterval(timer); |
| 121 | } |
| 122 | data = []; |
| 123 | updateFeedback(); |
| 124 | } |
| 125 | |
| 126 | // for now, simply display some text feedback |
| 127 | function flash(text) { |
| 128 | // cancel old scheduled event if there was one |
| 129 | if (timer) { |
| 130 | clearInterval(timer); |
| 131 | } |
| 132 | timer = setInterval(function () { |
| 133 | clearFlash(); |
| 134 | }, showFor); |
| 135 | |
| 136 | data = [{ |
| 137 | label: text |
| 138 | }]; |
| 139 | updateFeedback(); |
| 140 | } |
| 141 | |
| 142 | onos.ui.addLib('feedback', { |
| 143 | flash: flash |
| 144 | }); |
| 145 | }(ONOS)); |