1(function () { 2 let currentPage = window.http_origin + '/recherche-entreprise.php'; 3 let codeInsee = document.getElementById('codeInsee'); 4 let allButtonsSubmit = document.querySelectorAll('#buttonSubmit'); 5 6 // Init 7 allButtonsSubmit.forEach(abs => abs.setAttribute('disabled', '')); 8 if (document.querySelector('.recherche-entreprise__cta')) { 9 document.querySelector('.recherche-entreprise__cta').style.display = 'none'; 10 } 11 if (document.querySelector('.recherche-entreprises__entete .loading')) { 12 document.querySelector('.recherche-entreprises__entete .loading').style.display = 'block'; 13 } 14 if (codeInsee.value !== '') { 15 allButtonsSubmit.forEach(abs => abs.removeAttribute('disabled')); 16 callSearch(document.getElementById('recherche-entreprise__form')); 17 } else { 18 if (document.querySelector('.recherche-entreprises__entete .loading')) { 19 document.querySelector('.recherche-entreprises__entete .loading').style.display = 'none'; 20 } 21 if (document.querySelector('.recherche-entreprises__entete #h1-title-total-zipcode')) { 22 document.querySelector('.recherche-entreprises__entete #h1-title-total-zipcode').style.display = 'flex'; 23 // document.querySelector('.recherche-entreprises__entete #h1-title-total-zipcode').innerHTML = 'Aucun résultat' 24 } 25 if (document.querySelector('.recherche-entreprise__cta')) { 26 document.querySelector('.recherche-entreprise__cta').style.display = 'flex'; 27 } 28 } 29 30 codeInsee.addEventListener('input', function (e) { 31 if (this.value !== '') { 32 allButtonsSubmit.forEach(abs => abs.removeAttribute('disabled')); 33 } else { 34 allButtonsSubmit.forEach(abs => abs.setAttribute('disabled', '')); 35 } 36 }); 37 38 async function postData(url = '', data = {}) { 39 const response = await fetch(url, { 40 method: 'POST', 41 mode: 'cors', 42 cache: 'no-cache', 43 credentials: 'same-origin', 44 headers: { 45 'Content-Type': 'application/x-www-form-urlencoded', 46 }, 47 redirect: 'follow', 48 referrerPolicy: 'no-referrer', 49 body: data, 50 }); 51 return {'data': await response.text(), 'status': response.status}; 52 } 53 54 function callSearch(form) { 55 const formData = new URLSearchParams(); 56 for (const pair of new FormData(form)) { 57 formData.append(pair[0], pair[1]); 58 } 59 postData( 60 'async/searchFranchise.php', 61 formData 62 ).then((response) => { 63 const h1TitleTotalZipCode = document.getElementById('h1-title-total-zipcode'); 64 const companyHTMLText = 'entreprises proches de'; 65 const singleCompanyHTMLText = '1 entreprise proche de'; 66 const emptyCompanyHTMLText = 'Aucune entreprise trouvé proches de'; 67 68 const cityZipCode = document.querySelector('#recherche-entreprise__form #searchCity').value.split(' - '); 69 const cityZipCodeHTMLText = ' <span><strong>' + cityZipCode[1] + ' (' + cityZipCode[0] + ')</strong></span>'; 70 if (response.status === 204) { 71 let HTMLText = '<span>' + emptyCompanyHTMLText + '</span>'; 72 HTMLText += cityZipCodeHTMLText; 73 h1TitleTotalZipCode.innerHTML = HTMLText; 74 document.getElementById('list_result').innerHTML = ''; 75 76 document.querySelector('.recherche-entreprise__cta').style.display = 'flex'; 77 h1TitleTotalZipCode.style.display = 'flex'; 78 document.querySelector('.recherche-entreprises__entete .loading').style.display = 'none'; 79 return; 80 } 81 82 document.getElementById('list_result').innerHTML = response.data; 83 const total = document.getElementById('list_result').querySelectorAll('.franchise__card').length; 84 85 let HTMLText = '<span>' + total + ' ' + companyHTMLText + '</span>'; 86 if (total === 1) { 87 HTMLText = '<span>' + singleCompanyHTMLText + '</span>'; 88 } 89 HTMLText += cityZipCodeHTMLText; 90 h1TitleTotalZipCode.innerHTML = HTMLText 91 document.querySelector('.recherche-entreprise__cta').style.display = 'flex'; 92 h1TitleTotalZipCode.style.display = 'flex'; 93 document.querySelector('.recherche-entreprises__entete .loading').style.display = 'none'; 94 }); 95 } 96 97 function escapeHtml(text) { 98 const map = { 99 '&': '&', 100 '<': '<', 101 '>': '>', 102 '"': '"', 103 "'": ''' 104 }; 105 106 return text.replace(/[&<>"']/g, function(m) { return map[m]; }); 107 } 108
109 function slugify(input) { 110 if (!input) 111 return ''; 112 113 // make lower case and trim 114 let slug = input.toLowerCase().trim(); 115 116 // remove accents from charaters 117 slug = slug.normalize('NFD').replace(/[\u0300-\u036f]/g, '') 118 119 // replace invalid chars with spaces 120 slug = slug.replace(/[^a-z0-9\s-]/g, ' ').trim(); 121 122 // replace multiple spaces or hyphens with a single hyphen 123 slug = slug.replace(/[\s-]+/g, '-'); 124 125 return slug; 126 } 127 128 // let form = document.getElementById('recherche-entreprise__form'); 129 const forms = document.getElementsByClassName('recherche-entreprise__form'); 130 Array.from(forms).forEach((form) => { 131 form.addEventListener('submit', function (e) { 132 let searchCity = ''; 133 let postalCode = ''; 134 let inseeCode = ''; 135 // Pas ouf mais comme on a le même formulaire à 2 endroits avec le même id et pareil pour les champs, getElementById ne fonctionne pas :/ 136 // Donc en attendant de pouvoir faire mieux on récupère tous les champs dont le nom est xxx et on prend le premier qui a une valeur. Et ça fonctionne. 137 Array.from(document.getElementsByName('searchCity')).forEach((cityEl) => { 138 if(cityEl.value !== '') { 139 searchCity = cityEl.value; 140 } 141 }); 142 Array.from(document.getElementsByName('codePostal')).forEach((postalCodeEl) => { 143 if(postalCodeEl.value !== '') { 144 postalCode = postalCodeEl.value; 145 } 146 }); 147 Array.from(document.getElementsByName('codeInsee')).forEach((inseeCodeEl) => { 148 if(inseeCodeEl.value !== '') { 149 inseeCode = inseeCodeEl.value; 150 } 151 }); 152 const cityStartIndex = searchCity.indexOf('-'); 153 if(cityStartIndex === -1 || inseeCode === '' || postalCode === '') { 154 e.preventDefault(); 155 this.action = "recherche-entreprise.php"; 156 return; 157 } 158 159 // On ajoute 2 à cityStartIndex parce qu'il y a un espace entre le tiret et le début du nom de la ville 160 const city= searchCity.substring(cityStartIndex + 2, searchCity.length).toLowerCase(); 161 const slugifiedCity= slugify(city); 162 postalCode = escapeHtml(postalCode); 163 inseeCode = escapeHtml(inseeCode); 164 // Si la recherche aboutit on change l'action pour rediriger vers une url de type /amenagement-exterieur ... partageable Oui mais il y a un risque de boucle infini non ? 165 this.action = `/amenagement-exterieur-${escapeHtml(slugifiedCity)}-${postalCode}-${inseeCode}`; 166 167 // Si on est sur la bonne page. 168 document.querySelector('.recherche-entreprise__cta').style.display = 'none'; 169 document.querySelector('.recherche-entreprises__entete #h1-title-total-zipcode').style.display = 'none'; 170 document.querySelector('.recherche-entreprises__entete .loading').style.display = 'block'; 171 document.getElementById('list_result').innerHTML = ''; 172 callSearch(this); 173 }); 174 }); 175 176})()
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.