Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 1 | /* |
Simon Hunt | 8ead3a2 | 2015-01-06 11:00:15 -0800 | [diff] [blame] | 2 | * Copyright 2014,2015 Open Networking Laboratory |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 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 | /* |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 18 | ONOS GUI -- Util -- Key Handler Service |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 19 | */ |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 20 | (function () { |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 21 | 'use strict'; |
| 22 | |
| 23 | // references to injected services |
Simon Hunt | 9f5e97d | 2015-12-11 10:32:09 -0800 | [diff] [blame] | 24 | var $log, $timeout, fs, ts, ns, qhs; |
| 25 | |
| 26 | // constants |
| 27 | var eeggMin = 'shiftO', |
| 28 | eeggMax = 'shiftONOS'; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 29 | |
| 30 | // internal state |
Simon Hunt | 36fc15c | 2015-02-12 17:02:58 -0800 | [diff] [blame] | 31 | var enabled = true, |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 32 | globalEnabled = true, |
Simon Hunt | 36fc15c | 2015-02-12 17:02:58 -0800 | [diff] [blame] | 33 | keyHandler = { |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 34 | globalKeys: {}, |
| 35 | maskedKeys: {}, |
| 36 | viewKeys: {}, |
| 37 | viewFn: null, |
| 38 | viewGestures: [] |
Simon Hunt | 9f5e97d | 2015-12-11 10:32:09 -0800 | [diff] [blame] | 39 | }, |
| 40 | eegg = ''; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 41 | |
Simon Hunt | 9f5e97d | 2015-12-11 10:32:09 -0800 | [diff] [blame] | 42 | function layEgg(key) { |
| 43 | eegg += key; |
| 44 | if (eeggMax.indexOf(eegg) === 0) { |
| 45 | if (eegg === eeggMax) { |
| 46 | d3.select('body').append('div').attr('id', 'eegg') |
| 47 | .append('img').attr('src', 'data/img/eegg.png'); |
| 48 | $timeout(function () { d3.select('#eegg').remove(); }, 3000); |
Thomas Vachuska | fe1f01a | 2015-12-10 23:56:02 -0800 | [diff] [blame] | 49 | eegg = ''; |
| 50 | } |
| 51 | return true; |
Simon Hunt | 9f5e97d | 2015-12-11 10:32:09 -0800 | [diff] [blame] | 52 | } |
| 53 | if (eegg !== eeggMin) { |
Thomas Vachuska | fe1f01a | 2015-12-10 23:56:02 -0800 | [diff] [blame] | 54 | eegg = ''; |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 59 | function whatKey(code) { |
| 60 | switch (code) { |
Simon Hunt | 18d4bc8 | 2016-01-08 14:09:35 -0800 | [diff] [blame^] | 61 | case 8: return 'delete'; |
| 62 | case 9: return 'tab'; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 63 | case 13: return 'enter'; |
| 64 | case 16: return 'shift'; |
| 65 | case 17: return 'ctrl'; |
| 66 | case 18: return 'alt'; |
| 67 | case 27: return 'esc'; |
| 68 | case 32: return 'space'; |
| 69 | case 37: return 'leftArrow'; |
| 70 | case 38: return 'upArrow'; |
| 71 | case 39: return 'rightArrow'; |
| 72 | case 40: return 'downArrow'; |
| 73 | case 91: return 'cmdLeft'; |
| 74 | case 93: return 'cmdRight'; |
Simon Hunt | 18d4bc8 | 2016-01-08 14:09:35 -0800 | [diff] [blame^] | 75 | case 186: return 'semicolon'; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 76 | case 187: return 'equals'; |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 77 | case 188: return 'comma'; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 78 | case 189: return 'dash'; |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame] | 79 | case 190: return 'dot'; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 80 | case 191: return 'slash'; |
| 81 | case 192: return 'backQuote'; |
Simon Hunt | 18d4bc8 | 2016-01-08 14:09:35 -0800 | [diff] [blame^] | 82 | case 219: return 'openBracket'; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 83 | case 220: return 'backSlash'; |
Simon Hunt | 18d4bc8 | 2016-01-08 14:09:35 -0800 | [diff] [blame^] | 84 | case 221: return 'closeBracket'; |
| 85 | case 222: return 'quote'; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 86 | default: |
| 87 | if ((code >= 48 && code <= 57) || |
| 88 | (code >= 65 && code <= 90)) { |
| 89 | return String.fromCharCode(code); |
| 90 | } else if (code >= 112 && code <= 123) { |
| 91 | return 'F' + (code - 111); |
| 92 | } |
| 93 | return '.'; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | function keyIn() { |
| 98 | var event = d3.event, |
| 99 | keyCode = event.keyCode, |
| 100 | key = whatKey(keyCode), |
| 101 | kh = keyHandler, |
| 102 | gk = kh.globalKeys[key], |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 103 | gcb = fs.isF(gk) || (fs.isA(gk) && fs.isF(gk[0])), |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 104 | vk = kh.viewKeys[key], |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 105 | kl = fs.isF(kh.viewKeys._keyListener), |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 106 | vcb = fs.isF(vk) || (fs.isA(vk) && fs.isF(vk[0])) || fs.isF(kh.viewFn), |
Simon Hunt | 1bf1e8c | 2015-04-08 10:12:43 -0700 | [diff] [blame] | 107 | token = 'keyev'; // indicate this was a key-pressed event |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 108 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 109 | d3.event.stopPropagation(); |
| 110 | |
Simon Hunt | 36fc15c | 2015-02-12 17:02:58 -0800 | [diff] [blame] | 111 | if (enabled) { |
Thomas Vachuska | fe1f01a | 2015-12-10 23:56:02 -0800 | [diff] [blame] | 112 | if (layEgg(key)) return; |
| 113 | |
Simon Hunt | 36fc15c | 2015-02-12 17:02:58 -0800 | [diff] [blame] | 114 | // global callback? |
| 115 | if (gcb && gcb(token, key, keyCode, event)) { |
| 116 | // if the event was 'handled', we are done |
| 117 | return; |
| 118 | } |
| 119 | // otherwise, let the view callback have a shot |
| 120 | if (vcb) { |
| 121 | vcb(token, key, keyCode, event); |
| 122 | } |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 123 | if (kl) { |
| 124 | kl(key); |
| 125 | } |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | function setupGlobalKeys() { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 130 | angular.extend(keyHandler, { |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 131 | globalKeys: { |
| 132 | backSlash: [quickHelp, 'Show / hide Quick Help'], |
| 133 | slash: [quickHelp, 'Show / hide Quick Help'], |
| 134 | esc: [escapeKey, 'Dismiss dialog or cancel selections'], |
| 135 | T: [toggleTheme, "Toggle theme"] |
| 136 | }, |
| 137 | globalFormat: ['backSlash', 'slash', 'esc', 'T'], |
| 138 | |
| 139 | // Masked keys are global key handlers that always return true. |
| 140 | // That is, the view will never see the event for that key. |
| 141 | maskedKeys: { |
| 142 | slash: true, |
| 143 | backSlash: true, |
| 144 | T: true |
| 145 | } |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | function quickHelp(view, key, code, ev) { |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 150 | if (!globalEnabled) { |
| 151 | return false; |
| 152 | } |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 153 | qhs.showQuickHelp(keyHandler); |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 157 | // returns true if we 'consumed' the ESC keypress, false otherwise |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 158 | function escapeKey(view, key, code, ev) { |
Simon Hunt | 9d28656 | 2015-03-09 13:53:50 -0700 | [diff] [blame] | 159 | return ns.hideIfShown() || qhs.hideQuickHelp(); |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | function toggleTheme(view, key, code, ev) { |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 163 | if (!globalEnabled) { |
| 164 | return false; |
| 165 | } |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 166 | ts.toggleTheme(); |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 170 | function setKeyBindings(keyArg) { |
| 171 | var viewKeys, |
| 172 | masked = []; |
| 173 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 174 | if (fs.isF(keyArg)) { |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 175 | // set general key handler callback |
| 176 | keyHandler.viewFn = keyArg; |
| 177 | } else { |
| 178 | // set specific key filter map |
| 179 | viewKeys = d3.map(keyArg).keys(); |
| 180 | viewKeys.forEach(function (key) { |
| 181 | if (keyHandler.maskedKeys[key]) { |
Simon Hunt | af32207 | 2014-12-18 13:23:40 -0800 | [diff] [blame] | 182 | masked.push('setKeyBindings(): Key "' + key + '" is reserved'); |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 183 | } |
| 184 | }); |
| 185 | |
| 186 | if (masked.length) { |
Simon Hunt | af32207 | 2014-12-18 13:23:40 -0800 | [diff] [blame] | 187 | $log.warn(masked.join('\n')); |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 188 | } |
| 189 | keyHandler.viewKeys = keyArg; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | function getKeyBindings() { |
| 194 | var gkeys = d3.map(keyHandler.globalKeys).keys(), |
| 195 | masked = d3.map(keyHandler.maskedKeys).keys(), |
| 196 | vkeys = d3.map(keyHandler.viewKeys).keys(), |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 197 | vfn = !!fs.isF(keyHandler.viewFn); |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 198 | |
| 199 | return { |
| 200 | globalKeys: gkeys, |
| 201 | maskedKeys: masked, |
| 202 | viewKeys: vkeys, |
| 203 | viewFunction: vfn |
| 204 | }; |
| 205 | } |
| 206 | |
Bri Prebilic Cole | 9dcaea5 | 2015-07-21 14:39:48 -0700 | [diff] [blame] | 207 | function unbindKeys() { |
| 208 | keyHandler.viewKeys = {}; |
| 209 | keyHandler.viewFn = null; |
| 210 | keyHandler.viewGestures = []; |
| 211 | } |
| 212 | |
Simon Hunt | 7189222 | 2015-09-29 13:39:40 -0700 | [diff] [blame] | 213 | function checkNotGlobal(o) { |
| 214 | var oops = []; |
| 215 | if (fs.isO(o)) { |
| 216 | angular.forEach(o, function (val, key) { |
| 217 | if (keyHandler.globalKeys[key]) { |
| 218 | oops.push(key); |
| 219 | } |
| 220 | }); |
| 221 | if (oops.length) { |
| 222 | $log.warn('Ignoring reserved global key(s):', oops.join(',')); |
| 223 | oops.forEach(function (key) { |
| 224 | delete o[key]; |
| 225 | }); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 230 | angular.module('onosUtil') |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 231 | .factory('KeyService', |
Simon Hunt | 9f5e97d | 2015-12-11 10:32:09 -0800 | [diff] [blame] | 232 | ['$log', '$timeout', 'FnService', 'ThemeService', 'NavService', |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 233 | |
Simon Hunt | 9f5e97d | 2015-12-11 10:32:09 -0800 | [diff] [blame] | 234 | function (_$log_, _$timeout_, _fs_, _ts_, _ns_) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 235 | $log = _$log_; |
Simon Hunt | 9f5e97d | 2015-12-11 10:32:09 -0800 | [diff] [blame] | 236 | $timeout = _$timeout_; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 237 | fs = _fs_; |
| 238 | ts = _ts_; |
Simon Hunt | 9d28656 | 2015-03-09 13:53:50 -0700 | [diff] [blame] | 239 | ns = _ns_; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 240 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 241 | return { |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 242 | bindQhs: function (_qhs_) { |
| 243 | qhs = _qhs_; |
| 244 | }, |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 245 | installOn: function (elem) { |
| 246 | elem.on('keydown', keyIn); |
| 247 | setupGlobalKeys(); |
| 248 | }, |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 249 | keyBindings: function (x) { |
| 250 | if (x === undefined) { |
| 251 | return getKeyBindings(); |
| 252 | } else { |
| 253 | setKeyBindings(x); |
| 254 | } |
| 255 | }, |
Bri Prebilic Cole | 9dcaea5 | 2015-07-21 14:39:48 -0700 | [diff] [blame] | 256 | unbindKeys: unbindKeys, |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 257 | gestureNotes: function (g) { |
| 258 | if (g === undefined) { |
| 259 | return keyHandler.viewGestures; |
| 260 | } else { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 261 | keyHandler.viewGestures = fs.isA(g) || []; |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 262 | } |
Simon Hunt | 36fc15c | 2015-02-12 17:02:58 -0800 | [diff] [blame] | 263 | }, |
| 264 | enableKeys: function (b) { |
| 265 | enabled = b; |
Simon Hunt | 7189222 | 2015-09-29 13:39:40 -0700 | [diff] [blame] | 266 | }, |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 267 | enableGlobalKeys: function (b) { |
| 268 | globalEnabled = b; |
| 269 | }, |
Simon Hunt | 7189222 | 2015-09-29 13:39:40 -0700 | [diff] [blame] | 270 | checkNotGlobal: checkNotGlobal |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 271 | }; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 272 | }]); |
| 273 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 274 | }()); |