Skip to content

Commit f7b8a7a

Browse files
committed
Harmonize event handling with L.Control.Layers
This harmonizes the toggle/expand/collapse handling with `L.Control.Layers`. Now there are two choices for `options.expand`: - "click": expands the control upon click, closes it on map click/pan - "hover" on desktop: expands/collapses upon mouse enter/leave - "hover" on mobile: expands the control upon click
1 parent ddaea04 commit f7b8a7a

File tree

3 files changed

+21
-44
lines changed

3 files changed

+21
-44
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ L.Control.geocoder(options)
9797
| Option | Type | Default | Description |
9898
| ----------------- | ---------------- | ------------------- | ----------- |
9999
| `collapsed` | Boolean | `true` | Collapse control unless hovered/clicked |
100-
| `expand` | String | `"touch"` | How to expand a collapsed control: `touch` or `click` or `hover` |
100+
| `expand` | String | `"hover"` | How to expand a collapsed control: `click` or `hover` |
101101
| `position` | String | `"topright"` | Control [position](http://leafletjs.com/reference.html#control-positions) |
102102
| `placeholder` | String | `"Search..."` | Placeholder text for text input
103103
| `errorMessage` | String | `"Nothing found."` | Message when no result found / geocoding error occurs |

demo/index.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
<script type="text/javascript">
2828
var map = L.map('map').setView([0, 0], 2),
2929
geocoder = L.Control.Geocoder.mapzen('search-DopSHJw'),
30-
control = L.Control.geocoder({
30+
hoverControl = L.Control.geocoder({
31+
expand: 'hover',
32+
geocoder: geocoder
33+
}).addTo(map),
34+
clickControl = L.Control.geocoder({
35+
expand: 'click',
3136
geocoder: geocoder
3237
}).addTo(map),
3338
marker;

src/control.js

+14-42
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
options: {
77
showResultIcons: false,
88
collapsed: true,
9-
expand: 'touch', // options: touch, click, anythingelse
9+
expand: 'hover', // options: click or anything else (hover or touch)
1010
position: 'topright',
1111
placeholder: 'Search...',
1212
errorMessage: 'Nothing found.',
@@ -69,54 +69,26 @@ export default {
6969
this
7070
);
7171

72+
// based on https://github.com/leaflet/leaflet/blob/2dbda53/src/control/Control.Layers.js#L187-L211
7273
if (this.options.collapsed) {
74+
this._map.on('click', this._collapse, this);
75+
this._map.on('movestart', this._collapse, this);
7376
if (this.options.expand === 'click') {
74-
L.DomEvent.addListener(
75-
container,
76-
'click',
77-
function(e) {
78-
if (e.button === 0 && e.detail !== 2) {
79-
this._toggle();
80-
}
81-
},
82-
this
83-
);
84-
} else if (L.Browser.touch && this.options.expand === 'touch') {
85-
L.DomEvent.addListener(
86-
container,
87-
'touchstart mousedown',
88-
function(e) {
89-
this._toggle();
90-
e.preventDefault(); // mobile: clicking focuses the icon, so UI expands and immediately collapses
91-
e.stopPropagation();
92-
},
93-
this
94-
);
95-
} else {
96-
L.DomEvent.addListener(container, 'mouseover', this._expand, this);
97-
L.DomEvent.addListener(container, 'mouseout', this._collapse, this);
98-
this._map.on('movestart', this._collapse, this);
77+
L.DomEvent.on(container, 'click', L.DomEvent.stop);
78+
L.DomEvent.on(container, 'click', this._expand, this);
79+
} else if (!L.Browser.android) {
80+
L.DomEvent.addListener(container, 'mouseenter', this._expand, this);
81+
L.DomEvent.addListener(container, 'mouseleave', this._collapse, this);
9982
}
10083
} else {
10184
this._expand();
85+
}
86+
if (this.options.expand !== 'click') {
10287
if (L.Browser.touch) {
103-
L.DomEvent.addListener(
104-
container,
105-
'touchstart',
106-
function() {
107-
this._geocode();
108-
},
109-
this
110-
);
88+
L.DomEvent.on(container, 'click', L.DomEvent.stop);
89+
L.DomEvent.on(container, 'click', this._expand, this);
11190
} else {
112-
L.DomEvent.addListener(
113-
container,
114-
'click',
115-
function() {
116-
this._geocode();
117-
},
118-
this
119-
);
91+
L.DomEvent.on(container, 'focus', this._expand, this);
12092
}
12193
}
12294

0 commit comments

Comments
 (0)