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, |
| 28 | keyHandler = { |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 29 | globalKeys: {}, |
| 30 | maskedKeys: {}, |
| 31 | viewKeys: {}, |
| 32 | viewFn: null, |
| 33 | viewGestures: [] |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 34 | }; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 35 | |
Simon Hunt | 9d28656 | 2015-03-09 13:53:50 -0700 | [diff] [blame] | 36 | // TODO: do we still need to have the concept of view token here..? |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 37 | function getViewToken() { |
| 38 | return 'NotYetAViewToken'; |
| 39 | } |
| 40 | |
| 41 | function whatKey(code) { |
| 42 | switch (code) { |
| 43 | case 13: return 'enter'; |
| 44 | case 16: return 'shift'; |
| 45 | case 17: return 'ctrl'; |
| 46 | case 18: return 'alt'; |
| 47 | case 27: return 'esc'; |
| 48 | case 32: return 'space'; |
| 49 | case 37: return 'leftArrow'; |
| 50 | case 38: return 'upArrow'; |
| 51 | case 39: return 'rightArrow'; |
| 52 | case 40: return 'downArrow'; |
| 53 | case 91: return 'cmdLeft'; |
| 54 | case 93: return 'cmdRight'; |
| 55 | case 187: return 'equals'; |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame^] | 56 | case 188: return 'comma'; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 57 | case 189: return 'dash'; |
Simon Hunt | 90dcc3e | 2015-03-25 15:01:27 -0700 | [diff] [blame^] | 58 | case 190: return 'dot'; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 59 | case 191: return 'slash'; |
| 60 | case 192: return 'backQuote'; |
| 61 | case 220: return 'backSlash'; |
| 62 | default: |
| 63 | if ((code >= 48 && code <= 57) || |
| 64 | (code >= 65 && code <= 90)) { |
| 65 | return String.fromCharCode(code); |
| 66 | } else if (code >= 112 && code <= 123) { |
| 67 | return 'F' + (code - 111); |
| 68 | } |
| 69 | return '.'; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | function keyIn() { |
| 74 | var event = d3.event, |
| 75 | keyCode = event.keyCode, |
| 76 | key = whatKey(keyCode), |
| 77 | kh = keyHandler, |
| 78 | gk = kh.globalKeys[key], |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 79 | gcb = fs.isF(gk) || (fs.isA(gk) && fs.isF(gk[0])), |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 80 | vk = kh.viewKeys[key], |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 81 | kl = fs.isF(kh.viewKeys._keyListener), |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 82 | vcb = fs.isF(vk) || (fs.isA(vk) && fs.isF(vk[0])) || fs.isF(kh.viewFn), |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 83 | token = getViewToken(); |
| 84 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 85 | d3.event.stopPropagation(); |
| 86 | |
Simon Hunt | 36fc15c | 2015-02-12 17:02:58 -0800 | [diff] [blame] | 87 | if (enabled) { |
| 88 | // global callback? |
| 89 | if (gcb && gcb(token, key, keyCode, event)) { |
| 90 | // if the event was 'handled', we are done |
| 91 | return; |
| 92 | } |
| 93 | // otherwise, let the view callback have a shot |
| 94 | if (vcb) { |
| 95 | vcb(token, key, keyCode, event); |
| 96 | } |
Simon Hunt | 0906014 | 2015-03-18 20:23:32 -0700 | [diff] [blame] | 97 | if (kl) { |
| 98 | kl(key); |
| 99 | } |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | function setupGlobalKeys() { |
Simon Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 104 | angular.extend(keyHandler, { |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 105 | globalKeys: { |
| 106 | backSlash: [quickHelp, 'Show / hide Quick Help'], |
| 107 | slash: [quickHelp, 'Show / hide Quick Help'], |
| 108 | esc: [escapeKey, 'Dismiss dialog or cancel selections'], |
| 109 | T: [toggleTheme, "Toggle theme"] |
| 110 | }, |
| 111 | globalFormat: ['backSlash', 'slash', 'esc', 'T'], |
| 112 | |
| 113 | // Masked keys are global key handlers that always return true. |
| 114 | // That is, the view will never see the event for that key. |
| 115 | maskedKeys: { |
| 116 | slash: true, |
| 117 | backSlash: true, |
| 118 | T: true |
| 119 | } |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | function quickHelp(view, key, code, ev) { |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 124 | qhs.showQuickHelp(keyHandler); |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
Simon Hunt | a0eb0a8 | 2015-02-11 12:30:06 -0800 | [diff] [blame] | 128 | // returns true if we 'consumed' the ESC keypress, false otherwise |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 129 | function escapeKey(view, key, code, ev) { |
Simon Hunt | 9d28656 | 2015-03-09 13:53:50 -0700 | [diff] [blame] | 130 | return ns.hideIfShown() || qhs.hideQuickHelp(); |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | function toggleTheme(view, key, code, ev) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 134 | ts.toggleTheme(); |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 138 | function setKeyBindings(keyArg) { |
| 139 | var viewKeys, |
| 140 | masked = []; |
| 141 | |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 142 | if (fs.isF(keyArg)) { |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 143 | // set general key handler callback |
| 144 | keyHandler.viewFn = keyArg; |
| 145 | } else { |
| 146 | // set specific key filter map |
| 147 | viewKeys = d3.map(keyArg).keys(); |
| 148 | viewKeys.forEach(function (key) { |
| 149 | if (keyHandler.maskedKeys[key]) { |
Simon Hunt | af32207 | 2014-12-18 13:23:40 -0800 | [diff] [blame] | 150 | masked.push('setKeyBindings(): Key "' + key + '" is reserved'); |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 151 | } |
| 152 | }); |
| 153 | |
| 154 | if (masked.length) { |
Simon Hunt | af32207 | 2014-12-18 13:23:40 -0800 | [diff] [blame] | 155 | $log.warn(masked.join('\n')); |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 156 | } |
| 157 | keyHandler.viewKeys = keyArg; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | function getKeyBindings() { |
| 162 | var gkeys = d3.map(keyHandler.globalKeys).keys(), |
| 163 | masked = d3.map(keyHandler.maskedKeys).keys(), |
| 164 | vkeys = d3.map(keyHandler.viewKeys).keys(), |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 165 | vfn = !!fs.isF(keyHandler.viewFn); |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 166 | |
| 167 | return { |
| 168 | globalKeys: gkeys, |
| 169 | maskedKeys: masked, |
| 170 | viewKeys: vkeys, |
| 171 | viewFunction: vfn |
| 172 | }; |
| 173 | } |
| 174 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 175 | angular.module('onosUtil') |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 176 | .factory('KeyService', |
Simon Hunt | 9d28656 | 2015-03-09 13:53:50 -0700 | [diff] [blame] | 177 | ['$log', 'FnService', 'ThemeService', 'NavService', |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 178 | |
Simon Hunt | 9d28656 | 2015-03-09 13:53:50 -0700 | [diff] [blame] | 179 | function (_$log_, _fs_, _ts_, _ns_) { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 180 | $log = _$log_; |
| 181 | fs = _fs_; |
| 182 | ts = _ts_; |
Simon Hunt | 9d28656 | 2015-03-09 13:53:50 -0700 | [diff] [blame] | 183 | ns = _ns_; |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 184 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 185 | return { |
Simon Hunt | 639dc66 | 2015-02-18 14:19:20 -0800 | [diff] [blame] | 186 | bindQhs: function (_qhs_) { |
| 187 | qhs = _qhs_; |
| 188 | }, |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 189 | installOn: function (elem) { |
| 190 | elem.on('keydown', keyIn); |
| 191 | setupGlobalKeys(); |
| 192 | }, |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 193 | keyBindings: function (x) { |
| 194 | if (x === undefined) { |
| 195 | return getKeyBindings(); |
| 196 | } else { |
| 197 | setKeyBindings(x); |
| 198 | } |
| 199 | }, |
| 200 | gestureNotes: function (g) { |
| 201 | if (g === undefined) { |
| 202 | return keyHandler.viewGestures; |
| 203 | } else { |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 204 | keyHandler.viewGestures = fs.isA(g) || []; |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 205 | } |
Simon Hunt | 36fc15c | 2015-02-12 17:02:58 -0800 | [diff] [blame] | 206 | }, |
| 207 | enableKeys: function (b) { |
| 208 | enabled = b; |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 209 | } |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 210 | }; |
Simon Hunt | 1eecfa2 | 2014-12-16 14:46:29 -0800 | [diff] [blame] | 211 | }]); |
| 212 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 213 | }()); |