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(); } },