mirror of
https://forge.chapril.org/tykayn/osm-commerces
synced 2025-06-20 01:44:42 +02:00
116 lines
3.8 KiB
JavaScript
116 lines
3.8 KiB
JavaScript
![]() |
// Fonction pour gérer la recherche de villes
|
||
|
function setupCitySearch(searchInputId, suggestionListId, onCitySelected) {
|
||
|
const searchInput = document.getElementById(searchInputId);
|
||
|
const suggestionList = document.getElementById(suggestionListId);
|
||
|
|
||
|
// Vérifier si les éléments existent avant de continuer
|
||
|
if (!searchInput || !suggestionList) {
|
||
|
console.debug(`Éléments de recherche non trouvés: ${searchInputId}, ${suggestionListId}`);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
let searchTimeout;
|
||
|
|
||
|
// Fonction pour nettoyer la liste des suggestions
|
||
|
function clearSuggestions() {
|
||
|
suggestionList.innerHTML = '';
|
||
|
suggestionList.style.display = 'none';
|
||
|
}
|
||
|
|
||
|
// Fonction pour afficher les suggestions
|
||
|
function displaySuggestions(suggestions) {
|
||
|
clearSuggestions();
|
||
|
if (suggestions.length === 0) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
suggestions.forEach(suggestion => {
|
||
|
const item = document.createElement('div');
|
||
|
item.className = 'suggestion-item';
|
||
|
item.innerHTML = `
|
||
|
<div class="suggestion-name">${suggestion.display_name}</div>
|
||
|
<div class="suggestion-details">
|
||
|
<span class="suggestion-type">${suggestion.type}</span>
|
||
|
<span class="suggestion-postcode">${suggestion.postcode || ''}</span>
|
||
|
</div>
|
||
|
`;
|
||
|
|
||
|
item.addEventListener('click', () => {
|
||
|
searchInput.value = suggestion.display_name;
|
||
|
clearSuggestions();
|
||
|
if (onCitySelected) {
|
||
|
onCitySelected(suggestion);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
suggestionList.appendChild(item);
|
||
|
});
|
||
|
|
||
|
suggestionList.style.display = 'block';
|
||
|
}
|
||
|
|
||
|
// Fonction pour effectuer la recherche
|
||
|
async function performSearch(query) {
|
||
|
if (!query || query.length < 3) {
|
||
|
clearSuggestions();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
// Recherche avec Nominatim
|
||
|
const response = await fetch(`https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(query)}&countrycodes=fr&limit=5`);
|
||
|
const data = await response.json();
|
||
|
|
||
|
// Filtrer pour ne garder que les villes
|
||
|
const citySuggestions = data.filter(item =>
|
||
|
item.type === 'city' ||
|
||
|
item.type === 'town' ||
|
||
|
item.type === 'village' ||
|
||
|
item.type === 'administrative'
|
||
|
);
|
||
|
|
||
|
displaySuggestions(citySuggestions);
|
||
|
} catch (error) {
|
||
|
console.error('Erreur lors de la recherche:', error);
|
||
|
clearSuggestions();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Gestionnaire d'événements pour l'input
|
||
|
searchInput.addEventListener('input', (e) => {
|
||
|
const query = e.target.value.trim();
|
||
|
|
||
|
// Annuler la recherche précédente
|
||
|
if (searchTimeout) {
|
||
|
clearTimeout(searchTimeout);
|
||
|
}
|
||
|
|
||
|
// Attendre 300ms après la dernière frappe avant de lancer la recherche
|
||
|
searchTimeout = setTimeout(() => {
|
||
|
performSearch(query);
|
||
|
}, 300);
|
||
|
});
|
||
|
|
||
|
// Fermer la liste des suggestions en cliquant en dehors
|
||
|
document.addEventListener('click', (e) => {
|
||
|
if (!searchInput.contains(e.target) && !suggestionList.contains(e.target)) {
|
||
|
clearSuggestions();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// Fonction pour formater l'URL de labourage
|
||
|
function getLabourerUrl(zipCode) {
|
||
|
return `/admin/labourer/${zipCode}`;
|
||
|
}
|
||
|
|
||
|
// Fonction pour gérer la soumission du formulaire d'ajout de ville
|
||
|
window.handleAddCityFormSubmit = function(event) {
|
||
|
event.preventDefault();
|
||
|
const form = event.target;
|
||
|
const zipCode = form.querySelector('input[name="zip_code"]').value;
|
||
|
|
||
|
if (zipCode) {
|
||
|
window.location.href = getLabourerUrl(zipCode);
|
||
|
}
|
||
|
};
|