Simon Hunt | 988c6fc | 2014-11-20 17:43:03 -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 -- 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%', |
| 35 | fade = 750, |
| 36 | vb = '-200 -200 400 400', |
| 37 | xpad = 20, |
| 38 | ypad = 10; |
| 39 | |
| 40 | // State variables |
| 41 | var data = []; |
| 42 | |
| 43 | // DOM elements and the like |
| 44 | var kmdiv = d3.select('#keymap'); |
| 45 | |
| 46 | function computeBox(el) { |
| 47 | var text = el.select('text'), |
| 48 | box = text.node().getBBox(); |
| 49 | |
| 50 | // center |
| 51 | box.x = -box.width / 2; |
| 52 | box.y = -box.height / 2; |
| 53 | |
| 54 | // add some padding |
| 55 | box.x -= xpad; |
| 56 | box.width += xpad * 2; |
| 57 | box.y -= ypad; |
| 58 | box.height += ypad * 2; |
| 59 | |
| 60 | return box; |
| 61 | } |
| 62 | |
| 63 | function updateKeymap() { |
| 64 | var items = svg.selectAll('.bindingItem') |
| 65 | .data(data); |
| 66 | |
| 67 | var entering = items.enter() |
| 68 | .append('g') |
| 69 | .attr({ |
| 70 | class: 'bindingItem', |
| 71 | opacity: 0 |
| 72 | }) |
| 73 | .transition() |
| 74 | .duration(fade) |
| 75 | .attr('opacity', 1); |
| 76 | |
| 77 | entering.each(function (d) { |
| 78 | var el = d3.select(this), |
| 79 | box; |
| 80 | |
| 81 | d.el = el; |
| 82 | el.append('rect').attr({ rx: 10, ry: 10}); |
| 83 | el.append('text').text(d.label); |
| 84 | box = computeBox(el); |
| 85 | el.select('rect').attr(box); |
| 86 | }); |
| 87 | |
| 88 | items.exit() |
| 89 | .transition() |
| 90 | .duration(fade) |
| 91 | .attr({ opacity: 0}) |
| 92 | .remove(); |
| 93 | } |
| 94 | |
| 95 | function clearFlash() { |
| 96 | if (timer) { |
| 97 | clearInterval(timer); |
| 98 | } |
| 99 | data = []; |
| 100 | updateFeedback(); |
| 101 | } |
| 102 | |
| 103 | // for now, simply display some text feedback |
| 104 | function flash(text) { |
| 105 | // cancel old scheduled event if there was one |
| 106 | if (timer) { |
| 107 | clearInterval(timer); |
| 108 | } |
| 109 | timer = setInterval(function () { |
| 110 | clearFlash(); |
| 111 | }, showFor); |
| 112 | |
| 113 | data = [{ |
| 114 | label: text |
| 115 | }]; |
| 116 | updateFeedback(); |
| 117 | } |
| 118 | |
| 119 | // ===================================== |
| 120 | var svg = kmdiv.select('svg'); |
| 121 | |
| 122 | function populateBindings(bindings) { |
| 123 | svg.append('g') |
| 124 | .attr({ |
| 125 | class: 'keyBindings', |
| 126 | transform: 'translate(-200,-120)', |
| 127 | opacity: 0 |
| 128 | }) |
| 129 | .transition() |
| 130 | .duration(fade) |
| 131 | .attr('opacity', 1); |
| 132 | |
| 133 | var g = svg.select('g'); |
| 134 | |
| 135 | g.append('rect') |
| 136 | .attr({ |
| 137 | width: 400, |
| 138 | height: 240, |
| 139 | rx: 8 |
| 140 | }); |
| 141 | |
| 142 | g.append('text') |
| 143 | .text('Key Bindings') |
| 144 | .attr({ |
| 145 | x: 200, |
| 146 | y: 0, |
| 147 | dy: '1.4em', |
| 148 | class: 'title' |
| 149 | }); |
| 150 | |
| 151 | // TODO: append .keyItems to rectangle |
| 152 | } |
| 153 | |
| 154 | function fadeBindings() { |
| 155 | svg.selectAll('g') |
| 156 | .transition() |
| 157 | .duration(fade) |
| 158 | .attr('opacity', 0); |
| 159 | } |
| 160 | |
| 161 | function addSvg() { |
| 162 | svg = kmdiv.append('svg') |
| 163 | .attr({ |
| 164 | width: w, |
| 165 | height: h, |
| 166 | viewBox: vb |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | function removeSvg() { |
| 171 | svg.transition() |
| 172 | .delay(fade + 20) |
| 173 | .remove(); |
| 174 | } |
| 175 | |
| 176 | function showKeyMap(bindings) { |
| 177 | svg = kmdiv.select('svg'); |
| 178 | if (svg.empty()) { |
| 179 | addSvg(); |
| 180 | populateBindings(bindings); |
| 181 | console.log("SHOW KEY MAP"); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | function hideKeyMap() { |
| 186 | svg = kmdiv.select('svg'); |
| 187 | if (!svg.empty()) { |
| 188 | fadeBindings(); |
| 189 | removeSvg(); |
| 190 | console.log("HIDE KEY MAP"); |
| 191 | return true; |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | onos.ui.addLib('keymap', { |
| 197 | show: showKeyMap, |
| 198 | hide: hideKeyMap |
| 199 | }); |
| 200 | }(ONOS)); |