Code cleanup for hotkeys

This commit is contained in:
cptran777 2018-08-20 10:33:08 -07:00
parent 9034573d72
commit cb0bfc050a
5 changed files with 15 additions and 18 deletions

View File

@ -12,14 +12,14 @@ export default class GlobalHotkeys extends Component {
classNames = ['global-hotkey-binder']; classNames = ['global-hotkey-binder'];
/** /**
* Contains a set of elements that we deem to be inelligible in any circumstance. Targets * Contains a set of elements that we deem to be inEligible in any circumstance. Targets
* with these tags will never be passed through for global hotkeys * with these tags will never be passed through for global hotkeys
* @type {Set<string>} * @type {Set<string>}
*/ */
inElligibleTargets = new Set(['INPUT', 'TEXTAREA']); inEligibleTargets = new Set(['INPUT', 'TEXTAREA']);
/** /**
* Service that assists with actually triggering the actions tied to a particular elligible * Service that assists with actually triggering the actions tied to a particular Eligible
* target hotkey * target hotkey
* @type {Ember.Service} * @type {Ember.Service}
*/ */
@ -31,10 +31,10 @@ export default class GlobalHotkeys extends Component {
* @param {HTMLElement} target - target element * @param {HTMLElement} target - target element
* @returns {boolean} * @returns {boolean}
*/ */
isElligibleTarget(target: HTMLElement): boolean { isEligibleTarget(target: HTMLElement): boolean {
return ( return (
!!target && !!target &&
!get(this, 'inElligibleTargets').has(target.tagName) && !get(this, 'inEligibleTargets').has(target.tagName) &&
!(target.tagName === 'DIV' && target.attributes.getNamedItem('contenteditable')) !(target.tagName === 'DIV' && target.attributes.getNamedItem('contenteditable'))
); );
} }
@ -45,9 +45,9 @@ export default class GlobalHotkeys extends Component {
*/ */
onKeyUp(e: KeyboardEvent) { onKeyUp(e: KeyboardEvent) {
// KeyboardEvent.target is not well defined in our TS, casting any to return as HTMLElement // KeyboardEvent.target is not well defined in our TS, casting any to return as HTMLElement
const target: HTMLElement = <any>e.target; const target = <HTMLElement>e.target;
if (this.isElligibleTarget(target)) { if (this.isEligibleTarget(target)) {
get(this, 'hotKeys').applyKeyMapping(e.keyCode); get(this, 'hotKeys').applyKeyMapping(e.keyCode);
} }
} }

View File

@ -55,7 +55,8 @@ export default Component.extend({
* Sets to the focus to the input in the search bar * Sets to the focus to the input in the search bar
*/ */
setSearchBarFocus() { setSearchBarFocus() {
this.$('input.nacho-global-search__text-input:eq(1)').trigger('focus'); const searchBar = document.getElementsByClassName('nacho-global-search__text-input')[1];
searchBar && searchBar.focus();
}, },
didInsertElement() { didInsertElement() {
@ -64,9 +65,9 @@ export default Component.extend({
get(this, 'hotKeys').registerKeyMapping(Keyboard.Slash, this.setSearchBarFocus.bind(this)); get(this, 'hotKeys').registerKeyMapping(Keyboard.Slash, this.setSearchBarFocus.bind(this));
}, },
didDestroyElement() { willDestroyElement() {
this._super(...arguments); this._super(...arguments);
get(this, 'hotKeys').clearKeyMapping(Keyboard.Slash); get(this, 'hotKeys').unregisterKeyMapping(Keyboard.Slash);
}, },
actions: { actions: {

View File

@ -29,7 +29,7 @@ export default class HotKeys extends Service {
* this lets us clear that out * this lets us clear that out
* @param keyCode - keycode that has been registered * @param keyCode - keycode that has been registered
*/ */
clearKeyMapping(keyCode: Keyboard): void { unregisterKeyMapping(keyCode: Keyboard): void {
get(this, 'keyMappings')[keyCode] = noop; get(this, 'keyMappings')[keyCode] = noop;
} }
@ -45,9 +45,3 @@ export default class HotKeys extends Service {
action && action(); action && action();
} }
} }
declare module '@ember/service' {
interface Registry {
'hot-keys': HotKeys;
}
}

View File

@ -6,6 +6,7 @@ declare module '@ember/service' {
import BannerService from 'wherehows-web/services/banners'; import BannerService from 'wherehows-web/services/banners';
import Notifications from 'wherehows-web/services/notifications'; import Notifications from 'wherehows-web/services/notifications';
import UserLookup from 'wherehows-web/services/user-lookup'; import UserLookup from 'wherehows-web/services/user-lookup';
import HotKeys from 'wherehows-web/services/hot-keys';
// eslint-disable-next-line typescript/interface-name-prefix // eslint-disable-next-line typescript/interface-name-prefix
interface Registry { interface Registry {
@ -16,5 +17,6 @@ declare module '@ember/service' {
notifications: Notifications; notifications: Notifications;
'current-user': CurrentUser; 'current-user': CurrentUser;
'user-lookup': UserLookup; 'user-lookup': UserLookup;
'hot-keys': HotKeys;
} }
} }

View File

@ -25,7 +25,7 @@ module('Unit | Service | hot-keys', function(hooks) {
service.applyKeyMapping(Keyboard.ArrowUp); service.applyKeyMapping(Keyboard.ArrowUp);
service.registerKeyMapping(Keyboard.Enter, theChairsArePeople); service.registerKeyMapping(Keyboard.Enter, theChairsArePeople);
service.clearKeyMapping(Keyboard.Enter); service.unregisterKeyMapping(Keyboard.Enter);
service.applyKeyMapping(Keyboard.Enter); service.applyKeyMapping(Keyboard.Enter);
}); });
}); });