Skip to content

Commit f2adebe

Browse files
DennisOSRMCopilot
andauthored
Fix geocoder close button click-through to map (#409)
* Fix geocoder close button click-through to map (fixes #375) - Add src/geocoder_patches.js to monkey-patch the leaflet-routing-machine geocoder - Stop click propagation on the .leaflet-routing-geocoders container - Prevents unintended waypoint creation when closing geocoder entries - Preserves close button functionality while blocking map interaction Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Update package-lock.json --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b02b674 commit f2adebe

8 files changed

Lines changed: 49 additions & 8 deletions

File tree

.DS_Store

8 KB
Binary file not shown.

bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"routing",
2626
"api"
2727
],
28-
"author": "amy lee walton",
28+
"author": "OSRM contributors",
2929
"jest": {
3030
"testEnvironment": "node",
3131
"testMatch": [
@@ -51,6 +51,6 @@
5151
"leaflet.locatecontrol": "^0.89.0",
5252
"local-storage": "^2.0.0",
5353
"osrm-text-instructions": "^0.15.0",
54-
"qs": "^6.15.0"
54+
"qs": "^6.15.1"
5555
}
5656
}

src/geocoder.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ var geocoder = function(i, num) {
1818
name = String.fromCharCode(65 + i),
1919
icon = L.DomUtil.create('div', 'leaflet-osrm-geocoder-label', label);
2020
icon.innerHTML = name;
21+
22+
// Disable click propagation on the entire container to prevent any clicks from bubbling to the map
23+
L.DomEvent.disableClickPropagation(container);
24+
25+
// Also explicitly handle click on the close button
26+
L.DomEvent.on(close, 'click', function(e) {
27+
e.stopPropagation();
28+
e.preventDefault();
29+
});
30+
2131
return {
2232
container: container,
2333
input: input,

src/geocoder_patches.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
var L = require('leaflet');
4+
5+
/**
6+
* Patches for geocoder widgets to prevent click propagation to the map.
7+
* Without this patch, clicks on geocoder elements (especially the close button)
8+
* bubble up to the map and trigger waypoint creation at the cursor position.
9+
*/
10+
module.exports = function() {
11+
// Patch the main geocoders container to prevent click propagation
12+
var patchGeocodersContainer = setInterval(function() {
13+
var geocoderContainers = document.querySelectorAll('.leaflet-routing-geocoders');
14+
if (geocoderContainers.length > 0) {
15+
geocoderContainers.forEach(function(container) {
16+
if (!container._clickPropagationPatched) {
17+
L.DomEvent.disableClickPropagation(container);
18+
// Also explicitly stop click events from propagating up to the map
19+
L.DomEvent.on(container, 'click', function(e) {
20+
e.stopPropagation();
21+
});
22+
container._clickPropagationPatched = true;
23+
}
24+
});
25+
clearInterval(patchGeocodersContainer);
26+
}
27+
}, 100);
28+
};
29+

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var L = require('leaflet');
44
var routerPatches = require('./router_patches');
55
var createGeocoder = require('./geocoder');
66
require('leaflet-control-geocoder');
7+
var geocoderPatches = require('./geocoder_patches');
8+
geocoderPatches();
79
var LRM = require('leaflet-routing-machine');
810
// leaflet.locatecontrol@0.89 UMD has a bug: after the CJS IIFE it tries
911
// `window.L.Control.Locate.locate` but never sets L.Control.Locate in the

0 commit comments

Comments
 (0)