1let deliveryAutocomplete, billingAutocomplete; 2 3function initAutocomplete() { 4 const autocompleteOptions = { types: ['address'] }; 5 6 // Livraison 7 const deliveryAddressInput = document.getElementById('address'); 8 if (deliveryAddressInput) { 9 deliveryAutocomplete = new google.maps.places.Autocomplete(deliveryAddressInput, autocompleteOptions); 10 deliveryAutocomplete.addListener('place_changed', () => handlePlaceChanged(deliveryAutocomplete, '')); 11 } 12 13 // Facturation 14 const billingAddressInput = document.getElementById('address-billing'); 15 if (billingAddressInput) { 16 billingAutocomplete = new google.maps.places.Autocomplete(billingAddressInput, autocompleteOptions); 17 billingAutocomplete.addListener('place_changed', () => handlePlaceChanged(billingAutocomplete, '-billing')); 18 } 19} 20 21function normalizeText(value) { 22 return String(value || '') 23 .trim() 24 .toLowerCase() 25 .normalize('NFD') 26 .replace(/[\u0300-\u036f]/g, ''); 27} 28 29function normalizeCountryName(value) { 30 const country = String(value || '').trim(); 31 const normalized = normalizeText(country); 32 33 if (!country || normalized === 'france' || normalized === 'france metropolitaine') { 34 return 'France métropolitaine'; 35 } 36 37 return country; 38} 39 40function setFieldValue(field, value) { 41 if (!field) return; 42 43 const nextValue = String(value || '').trim(); 44 45 if (field.tagName === 'SELECT') { 46 const normalizedValue = normalizeText(nextValue); 47 const option = [...field.options].find(opt => normalizeText(opt.value) === normalizedValue); 48 field.value = option ? option.value : ''; 49 } else { 50 field.value = nextValue; 51 } 52 53 field.dispatchEvent(new Event('input', { bubbles: true })); 54 field.dispatchEvent(new Event('change', { bubbles: true })); 55} 56 57function handlePlaceChanged(autocomplete, suffix) { 58 const place = autocomplete.getPlace(); 59 if (!place.address_components) return; 60 61 let address = '', postcode = '', city = '', country = ''; 62 63 place.address_components.forEach(component => { 64 const types = component.types; 65 if (types.includes('street_number')) { 66 address = component.long_name + ' ' + address; 67 } 68 if (types.includes('route')) { 69 address += component.long_name; 70 } 71 if (types.includes('postal_code')) { 72 postcode = component.long_name; 73 } 74 if (types.includes('locality') || types.includes('postal_town')) { 75 city = component.long_name; 76 } 77 if (!city && types.includes('administrative_area_level_2')) { 78 city = component.long_name; 79 } 80 if (types.includes('country')) { 81 country = normalizeCountryName(component.long_name); 82 } 83 }); 84 85 // Remplissage des champs 86 const addressInput = document.getElementById('address' + suffix); 87 const postcodeInput = document.getElementById('postcode' + suffix); 88 const cityInput = document.getElementById('city' + suffix); 89 const countryInput = document.getElementById('country' + suffix); 90 91 if (addressInput) { 92 setFieldValue(addressInput, address); 93 addressInput.blur(); 94 } 95 if (postcodeInput) { 96 setFieldValue(postcodeInput, postcode); 97 } 98 if (cityInput) { 99 setFieldValue(cityInput, city); 100 } 101 if (countryInput) { 102 setFieldValue(countryInput, country); 103 } 104 105 if (suffix === '') { 106 // Mise à jour du localStorage (utile pour les pages panier ou recalc FDP) 107 if (postcode) localStorage.setItem('postalCode', postcode); 108 109 if (countryInput && typeof window.cart !== 'undefined') { 110 window.cart.handleCountryChange(countryInput); 111 } 112 113 if (typeof updateAllShippingCosts === 'function') { 114 updateAllShippingCosts(); 115 } 116 } 117} 118 119 120// Initialisation une fois le DOM chargé 121document.addEventListener('DOMContentLoaded', () => { 122 if (typeof google !== 'undefined' && google.maps && google.maps.places) { 123 initAutocomplete(); 124 } else {
125 console.error('Google Maps Places API nâest pas chargé.'); 126 } 127});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.