const $ = (s) => document.querySelector(s); const debounce = (fn, d = 250) => { let t; return (...a) => { clearTimeout(t); t = setTimeout(() => fn(...a), d); }; }; async function fetchSuggest(field, q) { const url = `/?ajax=suggest&field=${encodeURIComponent(field)}&q=${encodeURIComponent(q)}`; const res = await fetch(url, { headers: { "X-Requested-With": "XMLHttpRequest" }, }); if (!res.ok) return []; return res.json(); } function bindSuggest(inputEl, listEl, field) { const render = (items) => { listEl.innerHTML = items.map((v) => `
  • ${v}
  • `).join(""); listEl.hidden = items.length === 0; }; inputEl.addEventListener( "input", debounce(async (e) => { const q = e.target.value.trim(); if (!q) { listEl.hidden = true; listEl.innerHTML = ""; return; } const items = await fetchSuggest(field, q); render(items); }, 200), ); listEl.addEventListener("click", (e) => { if (e.target.tagName === "LI") { inputEl.value = e.target.textContent; listEl.hidden = true; } }); document.addEventListener("click", (e) => { if (!listEl.contains(e.target) && e.target !== inputEl) { listEl.hidden = true; } }); } bindSuggest($("#specialite"), $("#sg-specialite"), "specialite"); bindSuggest($("#ville"), $("#sg-ville"), "ville"); $("#submitBtn")?.addEventListener("click", () => { const sp = $("#specialite")?.value?.trim() || ""; const vi = $("#ville")?.value?.trim() || ""; const url = `result_search.php?specialite=${encodeURIComponent(sp)}&ville=${encodeURIComponent(vi)}`; window.location.href = url; }); document.querySelectorAll(".searchbar input").forEach((i) => { i.addEventListener("keydown", (e) => { if (e.key === "Enter") { e.preventDefault(); $("#submitBtn")?.click(); } }); }); if ("serviceWorker" in navigator) { window.addEventListener("load", () => { navigator.serviceWorker .register("/sw.js", { scope: "/" }) .catch(console.error); }); } (() => { const btn = document.getElementById("pwaInstallBtn"); const iosHint = document.getElementById("pwaIosHint"); const iosClose = document.getElementById("pwaIosClose"); let deferredPrompt = null; function isInstalled() { const mm = window.matchMedia && window.matchMedia("(display-mode: standalone)").matches; const iosStandalone = !!window.navigator.standalone; return mm || iosStandalone; } function isMobile() { return window.matchMedia && window.matchMedia("(max-width: 991px)").matches; } function isIos() { return /iphone|ipad|ipod/i.test(navigator.userAgent); } function isSafari() { const ua = navigator.userAgent.toLowerCase(); return ( ua.includes("safari") && !ua.includes("crios") && !ua.includes("fxios") && !ua.includes("edgios") ); } function hideAll() { if (btn) btn.classList.add("d-none"); if (iosHint) iosHint.classList.add("d-none"); } function showBtn() { if (!btn) return; btn.classList.remove("d-none"); } window.addEventListener("beforeinstallprompt", (e) => { if (!isMobile() || isInstalled()) return; e.preventDefault(); deferredPrompt = e; showBtn(); }); btn?.addEventListener("click", async () => { if (isInstalled()) return hideAll(); if (deferredPrompt) { deferredPrompt.prompt(); await deferredPrompt.userChoice.catch(() => {}); deferredPrompt = null; hideAll(); return; } if (isIos() && isSafari()) { iosHint?.classList.remove("d-none"); return; } }); iosClose?.addEventListener("click", () => { iosHint?.classList.add("d-none"); localStorage.setItem("pwa_ios_hint_seen", Date.now().toString()); }); window.addEventListener("appinstalled", () => hideAll()); document.addEventListener("DOMContentLoaded", () => { if (!isMobile() || isInstalled()) return hideAll(); const seen = Number(localStorage.getItem("pwa_ios_hint_seen") || "0"); const days7 = 7 * 24 * 60 * 60 * 1000; if (isIos() && isSafari()) { if (!seen || Date.now() - seen > days7) showBtn(); } }); })();