From cb0bfc050a2956118fd223c6839d3d015cf3870d Mon Sep 17 00:00:00 2001 From: cptran777 Date: Mon, 20 Aug 2018 10:33:08 -0700 Subject: [PATCH] Code cleanup for hotkeys --- .../app/components/hotkeys/global-hotkeys.ts | 14 +++++++------- wherehows-web/app/components/search-bar-form.js | 7 ++++--- wherehows-web/app/services/hot-keys.ts | 8 +------- wherehows-web/app/typings/app/services.d.ts | 2 ++ wherehows-web/tests/unit/services/hot-keys-test.js | 2 +- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/wherehows-web/app/components/hotkeys/global-hotkeys.ts b/wherehows-web/app/components/hotkeys/global-hotkeys.ts index 2a45853701..6f4100f08e 100644 --- a/wherehows-web/app/components/hotkeys/global-hotkeys.ts +++ b/wherehows-web/app/components/hotkeys/global-hotkeys.ts @@ -12,14 +12,14 @@ export default class GlobalHotkeys extends Component { 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 * @type {Set} */ - 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 * @type {Ember.Service} */ @@ -31,10 +31,10 @@ export default class GlobalHotkeys extends Component { * @param {HTMLElement} target - target element * @returns {boolean} */ - isElligibleTarget(target: HTMLElement): boolean { + isEligibleTarget(target: HTMLElement): boolean { return ( !!target && - !get(this, 'inElligibleTargets').has(target.tagName) && + !get(this, 'inEligibleTargets').has(target.tagName) && !(target.tagName === 'DIV' && target.attributes.getNamedItem('contenteditable')) ); } @@ -45,9 +45,9 @@ export default class GlobalHotkeys extends Component { */ onKeyUp(e: KeyboardEvent) { // KeyboardEvent.target is not well defined in our TS, casting any to return as HTMLElement - const target: HTMLElement = e.target; + const target = e.target; - if (this.isElligibleTarget(target)) { + if (this.isEligibleTarget(target)) { get(this, 'hotKeys').applyKeyMapping(e.keyCode); } } diff --git a/wherehows-web/app/components/search-bar-form.js b/wherehows-web/app/components/search-bar-form.js index 1b7d21be3f..5e20d04d04 100644 --- a/wherehows-web/app/components/search-bar-form.js +++ b/wherehows-web/app/components/search-bar-form.js @@ -55,7 +55,8 @@ export default Component.extend({ * Sets to the focus to the input in the search bar */ 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() { @@ -64,9 +65,9 @@ export default Component.extend({ get(this, 'hotKeys').registerKeyMapping(Keyboard.Slash, this.setSearchBarFocus.bind(this)); }, - didDestroyElement() { + willDestroyElement() { this._super(...arguments); - get(this, 'hotKeys').clearKeyMapping(Keyboard.Slash); + get(this, 'hotKeys').unregisterKeyMapping(Keyboard.Slash); }, actions: { diff --git a/wherehows-web/app/services/hot-keys.ts b/wherehows-web/app/services/hot-keys.ts index a43656cd9f..6ffbe8e0e1 100644 --- a/wherehows-web/app/services/hot-keys.ts +++ b/wherehows-web/app/services/hot-keys.ts @@ -29,7 +29,7 @@ export default class HotKeys extends Service { * this lets us clear that out * @param keyCode - keycode that has been registered */ - clearKeyMapping(keyCode: Keyboard): void { + unregisterKeyMapping(keyCode: Keyboard): void { get(this, 'keyMappings')[keyCode] = noop; } @@ -45,9 +45,3 @@ export default class HotKeys extends Service { action && action(); } } - -declare module '@ember/service' { - interface Registry { - 'hot-keys': HotKeys; - } -} diff --git a/wherehows-web/app/typings/app/services.d.ts b/wherehows-web/app/typings/app/services.d.ts index 4f437b7900..e40bb52189 100644 --- a/wherehows-web/app/typings/app/services.d.ts +++ b/wherehows-web/app/typings/app/services.d.ts @@ -6,6 +6,7 @@ declare module '@ember/service' { import BannerService from 'wherehows-web/services/banners'; import Notifications from 'wherehows-web/services/notifications'; import UserLookup from 'wherehows-web/services/user-lookup'; + import HotKeys from 'wherehows-web/services/hot-keys'; // eslint-disable-next-line typescript/interface-name-prefix interface Registry { @@ -16,5 +17,6 @@ declare module '@ember/service' { notifications: Notifications; 'current-user': CurrentUser; 'user-lookup': UserLookup; + 'hot-keys': HotKeys; } } diff --git a/wherehows-web/tests/unit/services/hot-keys-test.js b/wherehows-web/tests/unit/services/hot-keys-test.js index c9267b8b50..29f6578e71 100644 --- a/wherehows-web/tests/unit/services/hot-keys-test.js +++ b/wherehows-web/tests/unit/services/hot-keys-test.js @@ -25,7 +25,7 @@ module('Unit | Service | hot-keys', function(hooks) { service.applyKeyMapping(Keyboard.ArrowUp); service.registerKeyMapping(Keyboard.Enter, theChairsArePeople); - service.clearKeyMapping(Keyboard.Enter); + service.unregisterKeyMapping(Keyboard.Enter); service.applyKeyMapping(Keyboard.Enter); }); });