diff --git a/code/search.js b/code/search.js index 0f4119a2d..6f6b5eb69 100644 --- a/code/search.js +++ b/code/search.js @@ -65,6 +65,7 @@ window.search.Query.prototype.show = function() { window.search.Query.prototype.hide = function() { this.container.remove(); this.removeSelectedResult(); + this.removeHoverResult(); }; window.search.Query.prototype.addResult = function(result) { if(this.results.length == 0) { @@ -79,6 +80,12 @@ window.search.Query.prototype.addResult = function(result) { .on('click dblclick', function(ev) { this.onResultSelected(result, ev); }.bind(this)) + .on('mouseover', function(ev) { + this.onResultHoverStart(result, ev); + }.bind(this)) + .on('mouseout', function(ev) { + this.onResultHoverEnd(result, ev); + }.bind(this)) .keypress(function(ev) { if((ev.keyCode || ev.charCode || ev.which) == 32) { ev.preventDefault(); @@ -109,9 +116,33 @@ window.search.Query.prototype.addResult = function(result) { .append($('') .append(result.description)); } + +}; + +window.search.Query.prototype.resultLayer = function(result) { + if(result.layer !== null && !result.layer) { + result.layer = L.layerGroup(); + + if(result.position) { + createGenericMarker(result.position, 'red', { + title: result.title + }).addTo(result.layer); + } + + if(result.bounds) { + L.rectangle(result.bounds, { + title: result.title, + clickable: false, + color: 'red', + fill: false, + }).addTo(result.layer); + } + } +return result.layer; }; window.search.Query.prototype.onResultSelected = function(result, ev) { + this.removeHoverResult(); this.removeSelectedResult(); this.selectedResult = result; @@ -133,30 +164,14 @@ window.search.Query.prototype.onResultSelected = function(result, ev) { } } - if(result.layer !== null && !result.layer) { - result.layer = L.layerGroup(); - - if(result.position) { - createGenericMarker(result.position, 'red', { - title: result.title - }).addTo(result.layer); - } - - if(result.bounds) { - L.rectangle(result.bounds, { - title: result.title, - clickable: false, - color: 'red', - fill: false, - }).addTo(result.layer); - } - } + result.layer = this.resultLayer(result); if(result.layer) map.addLayer(result.layer); if(window.isSmartphone()) window.show('map'); } + window.search.Query.prototype.removeSelectedResult = function() { if(this.selectedResult) { if(this.selectedResult.layer) map.removeLayer(this.selectedResult.layer); @@ -164,6 +179,30 @@ window.search.Query.prototype.removeSelectedResult = function() { } } +window.search.Query.prototype.onResultHoverStart = function(result, ev) { + this.removeHoverResult(); + this.hoverResult = result; + + if(result === this.selectedResult) return; + + result.layer = this.resultLayer(result); + + if(result.layer) map.addLayer(result.layer); +}; + +window.search.Query.prototype.removeHoverResult = function() { + if(this.hoverResult !== this.selectedResult) { + if(this.hoverResult) { + if(this.hoverResult.layer) { map.removeLayer(this.hoverResult.layer); } + } + } + this.hoverResult = null; +}; + +window.search.Query.prototype.onResultHoverEnd = function(result, ev) { + this.removeHoverResult(); +}; + window.search.doSearch = function(term, confirmed) { term = term.trim();