From 08c8cfa8be8ab3be01c8b79c1ee07137feb31629 Mon Sep 17 00:00:00 2001 From: Henrik Fehlauer Date: Thu, 28 May 2020 18:00:00 +0000 Subject: [PATCH] Prevent more accidental shortcut triggers Do not allow browser keyboard shortcuts to trigger route functions, e.g. pressing Ctrl+P triggers printing in most browsers, but should not at the same time switch BRouter-Web to POI mode. This can be prevented by filtering for modifiers. In the same fashion, when modal dialogs or dropdowns are open it should not be possible to activate map functions in the background now. Finally, inhibit shortcuts in number input fields too, as found in the editable number input fields in the customize profile options. Previously only regular text input fields were protected. To make those checks easier to use, they are deduplicated and moved to Util.js. --- js/Util.js | 30 ++++++++++++++++++++++++++++++ js/control/OpacitySlider.js | 14 ++------------ js/plugin/POIMarkers.js | 7 +++---- js/plugin/Routing.js | 7 +++---- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/js/Util.js b/js/Util.js index 643ae0d..c3f2a3b 100644 --- a/js/Util.js +++ b/js/Util.js @@ -67,5 +67,35 @@ BR.Util = { } $el.remove(); return env; + }, + + keyboardShortcutsAllowed: function(keyEvent) { + // Skip auto-repeating key events + if (keyEvent.repeat) { + return false; + } + + // Suppress shortcut handling when a text or number input field is focussed + if ( + document.activeElement.type == 'number' || + document.activeElement.type == 'text' || + document.activeElement.type == 'textarea' + ) { + return false; + } + + // Only allow shortcuts without modifiers for now, to prevent triggering map functions + // when browser shortcuts are triggered (e.g. Ctrl+P for print should not add a POI) + if (keyEvent.ctrlKey || keyEvent.metaKey || keyEvent.altKey) { + return false; + } + + // Do not allow shortcuts triggering actions behind Bootstrap's + // modal dialogs or when dropdown menus are opened + if ($('.modal.show').length || $('.dropdown.show').length) { + return false; + } + + return true; } }; diff --git a/js/control/OpacitySlider.js b/js/control/OpacitySlider.js index 14f0439..e675b2c 100644 --- a/js/control/OpacitySlider.js +++ b/js/control/OpacitySlider.js @@ -52,23 +52,13 @@ BR.OpacitySlider = L.Class.extend({ }, _keydownListener: function(e) { - // Suppress shortcut handling when a text input field is focussed - if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') { - return; - } - - if (e.keyCode === this.options.muteKeyCode && !e.repeat) { + if (BR.Util.keyboardShortcutsAllowed(e) && e.keyCode === this.options.muteKeyCode) { this.options.callback(0); } }, _keyupListener: function(e) { - // Suppress shortcut handling when a text input field is focussed - if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') { - return; - } - - if (e.keyCode === this.options.muteKeyCode && !e.repeat) { + if (BR.Util.keyboardShortcutsAllowed(e) && e.keyCode === this.options.muteKeyCode) { this.options.callback(this.input.val() / 100); } }, diff --git a/js/plugin/POIMarkers.js b/js/plugin/POIMarkers.js index 25efd33..dce84ae 100644 --- a/js/plugin/POIMarkers.js +++ b/js/plugin/POIMarkers.js @@ -61,13 +61,12 @@ BR.PoiMarkers = L.Control.extend({ }, _keydownListener: function(e) { - // Suppress shortcut handling when a text input field is focussed - if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') { + if (!BR.Util.keyboardShortcutsAllowed(e)) { return; } - if (e.keyCode === this.options.shortcut.draw.disable && !e.repeat) { + if (e.keyCode === this.options.shortcut.draw.disable) { this.draw(false); - } else if (e.keyCode === this.options.shortcut.draw.enable && !e.repeat) { + } else if (e.keyCode === this.options.shortcut.draw.enable) { this.draw(true); } }, diff --git a/js/plugin/Routing.js b/js/plugin/Routing.js index 668818c..e9e1b2b 100644 --- a/js/plugin/Routing.js +++ b/js/plugin/Routing.js @@ -342,13 +342,12 @@ BR.Routing = L.Routing.extend({ }, _keydownListener: function(e) { - // Suppress shortcut handling when a text input field is focussed - if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') { + if (!BR.Util.keyboardShortcutsAllowed(e)) { return; } - if (e.keyCode === this.options.shortcut.draw.disable && !e.repeat) { + if (e.keyCode === this.options.shortcut.draw.disable) { this._draw.disable(); - } else if (e.keyCode === this.options.shortcut.draw.enable && !e.repeat) { + } else if (e.keyCode === this.options.shortcut.draw.enable) { this._draw.enable(); } },