From 9fceef362dd615666c62089fea6ea53d4de03ab7 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 3 Dec 2016 17:28:58 +0300 Subject: [PATCH] some refactoring... Signed-off-by: Alex A. Naanou --- ui (gen4)/features/keyboard.js | 39 +++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/ui (gen4)/features/keyboard.js b/ui (gen4)/features/keyboard.js index 506b365c..bc66dcf4 100755 --- a/ui (gen4)/features/keyboard.js +++ b/ui (gen4)/features/keyboard.js @@ -400,6 +400,21 @@ var KeyboardActions = actions.Actions({ 'max-key-repeat-rate': 0, 'keyboard-repeat-pause-check': 100, + + // Sets the target element to which the keyboard event handler + // is bound... + // + // Supported values: + // 'window' - window element + // 'document' - document element + // 'viewer' - the viewer (default) + // null - default element + // - any css selector + // + // NOTE: this value is not live, to update the target restart + // the handler by cycling the toggler off and on... + // NOTE: the target element must be focusable... + 'keyboard-event-source': 'window', }, get keyboard(){ @@ -407,9 +422,8 @@ var KeyboardActions = actions.Actions({ }, pauseKeyboardRepeat: ['- Interface/', - function(){ - this.__keyboard_repeat_paused = true - }], + function(){ + this.__keyboard_repeat_paused = true }], toggleKeyboardHandling: ['- Interface/Toggle keyboard handling', toggler.Toggler(null, function(_, state){ @@ -435,14 +449,19 @@ var KeyboardActions = actions.Actions({ return true }).bind(this) - // XXX this does not work yet... - //var target = this.ribbons.viewer - var target = $(document) - // start/reset keyboard handling... if(state == 'on'){ var that = this + // NOTE: the target element must be focusable... + var target = + this.__keyboard_event_source = + this.config['keyboard-event-source'] == null + || this.config['keyboard-event-source'] == 'viewer' ? this.ribbons.viewer + : this.config['keyboard-event-source'] == 'window' ? $(window) + : this.config['keyboard-event-source'] == 'document' ? $(document) + : $(this.config['keyboard-event-source']) + // need to reset... if(this.__keyboard_handler != null){ target.off('keydown', this.__keyboard_handler) @@ -483,8 +502,12 @@ var KeyboardActions = actions.Actions({ // stop keyboard handling... } else { - target.off('keydown', this.__keyboard_handler) + this.__keyboard_event_source + && this.__keyboard_event_source + .off('keydown', this.__keyboard_handler) + delete this.__keyboard_handler + delete this.__keyboard_event_source } }, ['on', 'off'])],