Skip to content

Commit

Permalink
Add suggest functionality.
Browse files Browse the repository at this point in the history
Close #53.
  • Loading branch information
perliedman committed Apr 15, 2016
1 parent 960465a commit 2645813
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module.exports = {
expand: 'click',
position: 'topright',
placeholder: 'Search...',
errorMessage: 'Nothing found.'
errorMessage: 'Nothing found.',
suggestMinLength: 3,
suggestTimeout: 250
},

_callbackId: 0,
Expand Down Expand Up @@ -78,9 +80,9 @@ module.exports = {
return container;
},

_geocodeResult: function (results) {
_geocodeResult: function (results, suggest) {
L.DomUtil.removeClass(this._container, 'leaflet-control-geocoder-throbber');
if (results.length === 1) {
if (!suggest && results.length === 1) {
this._geocodeResultSelected(results[0]);
} else if (results.length > 0) {
this._alts.innerHTML = '';
Expand Down Expand Up @@ -109,12 +111,15 @@ module.exports = {
return this;
},

_geocode: function(event) {
_geocode: function(suggest) {
this._lastGeocode = this._input.value;
L.DomUtil.addClass(this._container, 'leaflet-control-geocoder-throbber');
this._clearResults();
this.options.geocoder.geocode(this._input.value, this._geocodeResult, this);

return false;
if (!suggest) {
this._clearResults();
}
this.options.geocoder.geocode(this._input.value, function(results) {
this._geocodeResult(results, suggest);
}, this);
},

_geocodeResultSelected: function(result) {
Expand Down Expand Up @@ -221,8 +226,20 @@ module.exports = {
} else {
this._geocode();
}
break;
default:
var v = this._input.value;
if (this.options.geocoder.suggest && v !== this._lastGeocode) {
clearTimeout(this._suggestTimeout);
if (v.length >= this.options.suggestMinLength) {
this._suggestTimeout = setTimeout(L.bind(function() {
this._geocode(true);
}, this), this.options.suggestTimeout);
} else {
this._clearResults();
}
}
}
return true;
}
}),
factory: function(options) {
Expand Down

0 comments on commit 2645813

Please sign in to comment.