2018-08-17 11:58:47 -07:00
|
|
|
import Component from '@ember/component';
|
2020-08-26 15:44:50 -07:00
|
|
|
import { action } from '@ember/object';
|
2019-08-31 20:51:14 -07:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-08-26 15:44:50 -07:00
|
|
|
import HotKeys from 'datahub-web/services/hot-keys';
|
2018-08-17 11:58:47 -07:00
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
// Top level component class selector
|
|
|
|
const emberAppElementSelector = '.ember-application';
|
2018-08-20 11:06:04 -07:00
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
/**
|
|
|
|
* GlobalHotkeys component provides a way to listen for registered keypress events on the DOM by handling the binding or unbinding of listener
|
|
|
|
* event
|
|
|
|
* @export
|
|
|
|
* @class GlobalHotkeys
|
|
|
|
* @extends {Component}
|
|
|
|
*/
|
|
|
|
export default class GlobalHotkeys extends Component {
|
2018-08-17 11:58:47 -07:00
|
|
|
/**
|
2020-08-26 15:44:50 -07:00
|
|
|
* A set of DOM keyup event targets for which the hotkey listener will be ignored. Ineligible targets are not useful for the hotkey feature, or
|
|
|
|
* conflict with the default behavior of the target element for example input elements
|
2018-08-17 11:58:47 -07:00
|
|
|
*/
|
2018-08-20 10:33:08 -07:00
|
|
|
inEligibleTargets = new Set(['INPUT', 'TEXTAREA']);
|
2018-08-17 11:58:47 -07:00
|
|
|
|
|
|
|
/**
|
2020-08-26 15:44:50 -07:00
|
|
|
* Service that assists with actually triggering the actions tied to a particular
|
2018-08-17 11:58:47 -07:00
|
|
|
* target hotkey
|
|
|
|
*/
|
|
|
|
@service
|
2020-08-26 15:44:50 -07:00
|
|
|
hotKeys: HotKeys;
|
2018-08-17 11:58:47 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if target exists, is not an input, and is not an editable div
|
2020-08-26 15:44:50 -07:00
|
|
|
* @param {Element} target - target element
|
2018-08-17 11:58:47 -07:00
|
|
|
*/
|
2020-08-26 15:44:50 -07:00
|
|
|
isEligibleTarget(target: Element): boolean {
|
2018-08-17 11:58:47 -07:00
|
|
|
return (
|
2020-08-26 15:44:50 -07:00
|
|
|
!this.inEligibleTargets.has(target.tagName) &&
|
2018-08-17 11:58:47 -07:00
|
|
|
!(target.tagName === 'DIV' && target.attributes.getNamedItem('contenteditable'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method for handling the global keyup.
|
|
|
|
* @param {KeyboardEvent} e - KeyboardEvent triggered by user input
|
|
|
|
*/
|
2020-08-26 15:44:50 -07:00
|
|
|
@action
|
|
|
|
onKeyUp(e: KeyboardEvent): void {
|
|
|
|
const target = e.target as Element | null;
|
2018-08-17 11:58:47 -07:00
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
if (target && this.isEligibleTarget(target)) {
|
|
|
|
this.hotKeys.applyKeyMapping(e.keyCode);
|
2018-08-17 11:58:47 -07:00
|
|
|
}
|
2020-08-26 15:44:50 -07:00
|
|
|
}
|
2018-09-06 16:21:38 -07:00
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
/**
|
|
|
|
* On DOM insertion, apply handler for keyup events
|
|
|
|
*/
|
|
|
|
didInsertElement(): void {
|
|
|
|
const app = document.querySelector(emberAppElementSelector);
|
2018-09-06 16:21:38 -07:00
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
if (app) {
|
|
|
|
app.addEventListener('keyup', this.onKeyUp);
|
2018-09-06 16:21:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
/**
|
|
|
|
* On component destruction, remove hot-keys event handler
|
|
|
|
*/
|
|
|
|
willDestroyElement(): void {
|
|
|
|
const app = document.querySelector(emberAppElementSelector);
|
2018-09-06 16:21:38 -07:00
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
if (app) {
|
|
|
|
app.removeEventListener('keyup', this.onKeyUp);
|
2018-09-06 16:21:38 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-17 11:58:47 -07:00
|
|
|
}
|