Add shortcut for geocoder search field

Press 'F', type a query and press 'Enter' to find places effortlessly.

Note that the button already triggers on mousedown events, even though
by convention it should react on mouseup (i.e. a regular click)
only. However, that's an issue in the external dependency, and can
be worked around for now.
This commit is contained in:
Henrik Fehlauer 2020-06-02 18:00:00 +00:00
parent f23e3751b5
commit 37ab05310b
3 changed files with 15 additions and 1 deletions

View file

@ -6,7 +6,10 @@ BR.Search = L.Control.Geocoder.extend({
}),
sizeInMeters: 800
}),
position: 'topleft'
position: 'topleft',
shortcut: {
search: 70 // char code for 'f'
}
},
initialize: function(options) {
@ -16,6 +19,8 @@ BR.Search = L.Control.Geocoder.extend({
// (e.g. when added directly to "options:" above), so we have to call it here
placeholder: i18next.t('map.geocoder-placeholder')
});
L.DomEvent.addListener(document, 'keydown', this._keydownListener, this);
},
markGeocode: function(result) {
@ -38,5 +43,12 @@ BR.Search = L.Control.Geocoder.extend({
if (this._geocodeMarker) {
this._map.removeLayer(this._geocodeMarker);
}
},
_keydownListener: function(e) {
if (BR.Util.keyboardShortcutsAllowed(e) && e.keyCode === this.options.shortcut.search) {
$('#map .leaflet-control-geocoder')[0].dispatchEvent(new MouseEvent('mousedown'));
e.preventDefault();
}
}
});