From 37ab05310b6a3381811adf83a1893ba57e19b281 Mon Sep 17 00:00:00 2001 From: Henrik Fehlauer Date: Tue, 2 Jun 2020 18:00:00 +0000 Subject: [PATCH] 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. --- js/index.js | 1 + js/plugin/Search.js | 14 +++++++++++++- locales/en.json | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/js/index.js b/js/index.js index 59fcc22..14432d2 100644 --- a/js/index.js +++ b/js/index.js @@ -46,6 +46,7 @@ search = new BR.Search(); map.addControl(search); + $('#map .leaflet-control-geocoder > button')[0].title = i18next.t('map.geocoder'); router = L.bRouter(); //brouterCgi dummyRouter diff --git a/js/plugin/Search.js b/js/plugin/Search.js index 7e0cabb..4985caa 100644 --- a/js/plugin/Search.js +++ b/js/plugin/Search.js @@ -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(); + } } }); diff --git a/locales/en.json b/locales/en.json index 1399b44..eb9c6ef 100644 --- a/locales/en.json +++ b/locales/en.json @@ -91,6 +91,7 @@ "draw-route-start": "Draw route (D key)", "draw-route-stop": "Stop drawing route (ESC key)", "enter-poi-name": "Enter Point of Interest name", + "geocoder": "Search (F key)", "geocoder-placeholder": "Search…", "hikebike-hillshading": "Hillshading", "hiking": "Hiking",