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.
This commit is contained in:
Henrik Fehlauer 2020-05-28 18:00:00 +00:00
parent fbd3d3d383
commit 08c8cfa8be
4 changed files with 38 additions and 20 deletions

View file

@ -67,5 +67,35 @@ BR.Util = {
} }
$el.remove(); $el.remove();
return env; 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;
} }
}; };

View file

@ -52,23 +52,13 @@ BR.OpacitySlider = L.Class.extend({
}, },
_keydownListener: function(e) { _keydownListener: function(e) {
// Suppress shortcut handling when a text input field is focussed if (BR.Util.keyboardShortcutsAllowed(e) && e.keyCode === this.options.muteKeyCode) {
if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') {
return;
}
if (e.keyCode === this.options.muteKeyCode && !e.repeat) {
this.options.callback(0); this.options.callback(0);
} }
}, },
_keyupListener: function(e) { _keyupListener: function(e) {
// Suppress shortcut handling when a text input field is focussed if (BR.Util.keyboardShortcutsAllowed(e) && e.keyCode === this.options.muteKeyCode) {
if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') {
return;
}
if (e.keyCode === this.options.muteKeyCode && !e.repeat) {
this.options.callback(this.input.val() / 100); this.options.callback(this.input.val() / 100);
} }
}, },

View file

@ -61,13 +61,12 @@ BR.PoiMarkers = L.Control.extend({
}, },
_keydownListener: function(e) { _keydownListener: function(e) {
// Suppress shortcut handling when a text input field is focussed if (!BR.Util.keyboardShortcutsAllowed(e)) {
if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') {
return; return;
} }
if (e.keyCode === this.options.shortcut.draw.disable && !e.repeat) { if (e.keyCode === this.options.shortcut.draw.disable) {
this.draw(false); 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); this.draw(true);
} }
}, },

View file

@ -342,13 +342,12 @@ BR.Routing = L.Routing.extend({
}, },
_keydownListener: function(e) { _keydownListener: function(e) {
// Suppress shortcut handling when a text input field is focussed if (!BR.Util.keyboardShortcutsAllowed(e)) {
if (document.activeElement.type == 'text' || document.activeElement.type == 'textarea') {
return; return;
} }
if (e.keyCode === this.options.shortcut.draw.disable && !e.repeat) { if (e.keyCode === this.options.shortcut.draw.disable) {
this._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(); this._draw.enable();
} }
}, },