From 11a3995722626ee725437c243189f2806783fef8 Mon Sep 17 00:00:00 2001 From: antelle Date: Sat, 12 Oct 2019 12:26:22 +0200 Subject: [PATCH] extracted focus manager --- app/scripts/comp/app/focus-manager.js | 18 ++++++++++++++++++ app/scripts/comp/browser/key-handler.js | 21 +++++---------------- app/scripts/framework/views/view.js | 13 +++++++------ 3 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 app/scripts/comp/app/focus-manager.js diff --git a/app/scripts/comp/app/focus-manager.js b/app/scripts/comp/app/focus-manager.js new file mode 100644 index 00000000..e804f272 --- /dev/null +++ b/app/scripts/comp/app/focus-manager.js @@ -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 }; diff --git a/app/scripts/comp/browser/key-handler.js b/app/scripts/comp/browser/key-handler.js index c45be930..f24a40fc 100644 --- a/app/scripts/comp/browser/key-handler.js +++ b/app/scripts/comp/browser/key-handler.js @@ -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); } } diff --git a/app/scripts/framework/views/view.js b/app/scripts/framework/views/view.js index 1fffb838..313abbcd 100644 --- a/app/scripts/framework/views/view.js +++ b/app/scripts/framework/views/view.js @@ -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');