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