feat: construct search results better

This commit is contained in:
moanos [he/him] 2025-01-09 23:15:10 +01:00
parent a2ef91e89a
commit bc1f4e7ab7
2 changed files with 16 additions and 3 deletions

View File

@ -4,5 +4,18 @@ function ifdef(variable, prefix = "", suffix = "") {
} else { } else {
return ""; return "";
} }
}
function geojson_to_summary(location) {
if (ifdef(location.properties.name) !== "") {
return location.properties.name + ifdef(location.properties.city, " (", ")");
} else {
return ifdef(location.properties.street, "", ifdef(location.properties.housenumber, " ","")) + ifdef(location.properties.city, ", ", "") + ifdef(location.properties.countrycode, ", ", "")
}
}
function geojson_to_searchable_string(location) {
return ifdef(location.properties.name, "", ", ") + ifdef(location.properties.street, "", ifdef(location.properties.housenumber, " ","")) + ifdef(location.properties.city, ", ", "") + ifdef(location.properties.country, ", ", "")
}
}

View File

@ -62,12 +62,12 @@
locations.forEach(location => { locations.forEach(location => {
const listItem = document.createElement('li'); const listItem = document.createElement('li');
listItem.classList.add('result-item'); listItem.classList.add('result-item');
listItem.textContent = location.properties.name + ifdef(location.properties.city, " (", ")"); listItem.textContent = geojson_to_summary(location);
// Add event when user clicks on a result location // Add event when user clicks on a result location
listItem.addEventListener('click', () => { listItem.addEventListener('click', () => {
locationInput.value = location.properties.name + ifdef(location.properties.city, ", ", "") + ifdef(location.properties.country, ", ", ""); // Set input field to selected location locationInput.value = geojson_to_searchable_string(location); // Set input field to selected location
resultsList.innerHTML = ''; // Clear the results after selecting a location resultsList.innerHTML = ''; // Clear the results after selecting a location
}); });