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