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

@ -46,6 +46,7 @@
search = new BR.Search(); search = new BR.Search();
map.addControl(search); map.addControl(search);
$('#map .leaflet-control-geocoder > button')[0].title = i18next.t('map.geocoder');
router = L.bRouter(); //brouterCgi dummyRouter router = L.bRouter(); //brouterCgi dummyRouter

View file

@ -6,7 +6,10 @@ BR.Search = L.Control.Geocoder.extend({
}), }),
sizeInMeters: 800 sizeInMeters: 800
}), }),
position: 'topleft' position: 'topleft',
shortcut: {
search: 70 // char code for 'f'
}
}, },
initialize: function(options) { 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 // (e.g. when added directly to "options:" above), so we have to call it here
placeholder: i18next.t('map.geocoder-placeholder') placeholder: i18next.t('map.geocoder-placeholder')
}); });
L.DomEvent.addListener(document, 'keydown', this._keydownListener, this);
}, },
markGeocode: function(result) { markGeocode: function(result) {
@ -38,5 +43,12 @@ BR.Search = L.Control.Geocoder.extend({
if (this._geocodeMarker) { if (this._geocodeMarker) {
this._map.removeLayer(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();
}
} }
}); });

View file

@ -91,6 +91,7 @@
"draw-route-start": "Draw route (D key)", "draw-route-start": "Draw route (D key)",
"draw-route-stop": "Stop drawing route (ESC key)", "draw-route-stop": "Stop drawing route (ESC key)",
"enter-poi-name": "Enter Point of Interest name", "enter-poi-name": "Enter Point of Interest name",
"geocoder": "Search (F key)",
"geocoder-placeholder": "Search…", "geocoder-placeholder": "Search…",
"hikebike-hillshading": "Hillshading", "hikebike-hillshading": "Hillshading",
"hiking": "Hiking", "hiking": "Hiking",