extracted focus manager

This commit is contained in:
antelle 2019-10-12 12:26:22 +02:00
parent eb68da635f
commit 11a3995722
3 changed files with 30 additions and 22 deletions

View File

@ -0,0 +1,18 @@
import { Logger } from 'util/logger';
const logger = new Logger(
'focus-manager',
undefined,
localStorage.debugFocusManager ? Logger.Level.Debug : Logger.Level.Info
);
const FocusManager = {
modal: null,
setModal(modal) {
this.modal = modal;
logger.debug('Set modal', modal);
}
};
export { FocusManager };

View File

@ -1,14 +1,9 @@
import { Events } from 'framework/events';
import { IdleTracker } from 'comp/browser/idle-tracker';
import { Keys } from 'const/keys';
import { Logger } from 'util/logger';
import { FocusManager } from 'comp/app/focus-manager';
const shortcutKeyProp = navigator.platform.indexOf('Mac') >= 0 ? 'metaKey' : 'ctrlKey';
const logger = new Logger(
'key-handler',
undefined,
localStorage.debugKeyHandler ? Logger.Level.Debug : Logger.Level.Info
);
class KeyHandler {
SHORTCUT_ACTION = 1;
@ -16,7 +11,6 @@ class KeyHandler {
SHORTCUT_SHIFT = 4;
shortcuts = {};
modal = false;
init() {
$(document).bind('keypress', this.keypress.bind(this));
@ -55,11 +49,6 @@ class KeyHandler {
}
}
setModal(modal) {
this.modal = modal;
logger.debug('Set modal', modal);
}
isActionKey(e) {
return e[shortcutKeyProp];
}
@ -70,7 +59,7 @@ class KeyHandler {
const keyShortcuts = this.shortcuts[code];
if (keyShortcuts && keyShortcuts.length) {
for (const sh of keyShortcuts) {
if (this.modal && (sh.modal !== this.modal && sh.modal !== '*')) {
if (FocusManager.modal && (sh.modal !== FocusManager.modal && sh.modal !== '*')) {
e.stopPropagation();
continue;
}
@ -115,7 +104,7 @@ class KeyHandler {
keypress(e) {
if (
!this.modal &&
!FocusManager.modal &&
e.which !== Keys.DOM_VK_RETURN &&
e.which !== Keys.DOM_VK_ESCAPE &&
e.which !== Keys.DOM_VK_TAB &&
@ -124,8 +113,8 @@ class KeyHandler {
!e.metaKey
) {
Events.emit('keypress', e);
} else if (this.modal) {
Events.emit('keypress:' + this.modal, e);
} else if (FocusManager.modal) {
Events.emit('keypress:' + FocusManager.modal, e);
}
}

View File

@ -2,6 +2,7 @@ import morphdom from 'morphdom';
import EventEmitter from 'events';
import { Tip } from 'util/ui/tip';
import { KeyHandler } from 'comp/browser/key-handler';
import { FocusManager } from 'comp/app/focus-manager';
import { Logger } from 'util/logger';
const DoesNotBubble = {
@ -90,7 +91,7 @@ class View extends EventEmitter {
parent.appendChild(this.el);
}
if (this.modal) {
KeyHandler.setModal(this.modal);
FocusManager.setModal(this.modal);
}
this.bindEvents();
} else {
@ -186,8 +187,8 @@ class View extends EventEmitter {
}
remove() {
if (this.modal && KeyHandler.modal === this.modal) {
KeyHandler.setModal(null);
if (this.modal && FocusManager.modal === this.modal) {
FocusManager.setModal(null);
}
this.emit('remove');
@ -237,9 +238,9 @@ class View extends EventEmitter {
this.hidden = !visible;
if (this.modal) {
if (visible) {
KeyHandler.setModal(this.modal);
} else if (KeyHandler.modal === this.modal) {
KeyHandler.setModal(null);
FocusManager.setModal(this.modal);
} else if (FocusManager.modal === this.modal) {
FocusManager.setModal(null);
}
}
this.emit(visible ? 'show' : 'hide');