Skip to content

Commit

Permalink
Prevent control from collapsing on mousedown; close #142
Browse files Browse the repository at this point in the history
  • Loading branch information
Per Liedman committed Oct 7, 2016
1 parent 4c3453a commit 391f841
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ module.exports = {
this._alts = L.DomUtil.create('ul',
className + '-alternatives leaflet-control-geocoder-alternatives-minimized',
container);
L.DomEvent.disableClickPropagation(this._alts);

L.DomEvent.addListener(input, 'keydown', this._keydown, this);
L.DomEvent.addListener(input, 'blur', function() {
if (this.options.collapsed) {
if (this.options.collapsed && !this._preventBlurCollapse) {
this._collapse();
}
this._preventBlurCollapse = false;
}, this);


Expand Down Expand Up @@ -145,9 +147,7 @@ module.exports = {
},

_geocodeResultSelected: function(result) {
if (this.options.collapsed) {
this._collapse();
} else {
if (!this.options.collapsed) {
this._clearResults();
}

Expand Down Expand Up @@ -186,9 +186,20 @@ module.exports = {
a = L.DomUtil.create('a', '', li),
icon = this.options.showResultIcons && result.icon ? L.DomUtil.create('img', '', a) : null,
text = result.html ? undefined : document.createTextNode(result.name),
clickHandler = function clickHandler(e) {
L.DomEvent.preventDefault(e);
mouseDownHandler = function mouseDownHandler(e) {
// In some browsers, a click will fire on the map if the control is
// collapsed directly after mousedown. To work around this, we
// wait until the click is completed, and _then_ collapse the
// control. Messy, but this is the workaround I could come up with
// for #142.
this._preventBlurCollapse = true;
L.DomEvent.stop(e);
this._geocodeResultSelected(result);
L.DomEvent.on(li, 'click', function() {
if (this.options.collapsed) {
this._collapse();
}
}, this);
};

if (icon) {
Expand All @@ -203,7 +214,10 @@ module.exports = {
a.appendChild(text);
}

L.DomEvent.addListener(li, 'mousedown', clickHandler, this);
// Use mousedown and not click, since click will fire _after_ blur,
// causing the control to have collapsed and removed the items
// before the click can fire.
L.DomEvent.addListener(li, 'mousedown', mouseDownHandler, this);

return li;
},
Expand Down

0 comments on commit 391f841

Please sign in to comment.