62 lines
2.4 KiB
JavaScript
62 lines
2.4 KiB
JavaScript
(function(){
|
||
const ENDPOINT = '/public/contact_admin.php';
|
||
|
||
function openContactModal(lang){
|
||
const url = ENDPOINT + '?modal=1' + (lang ? '&lang='+encodeURIComponent(lang) : '');
|
||
fetch(url, { headers: { 'X-Requested-With': 'XMLHttpRequest' } })
|
||
.then(r => r.text())
|
||
.then(html => {
|
||
// On insère TOUT le HTML renvoyé (overlay + <style>)
|
||
const wrap = document.createElement('div');
|
||
wrap.innerHTML = html;
|
||
while (wrap.firstChild) document.body.appendChild(wrap.firstChild);
|
||
})
|
||
.catch(() => alert('Erreur de chargement du formulaire'));
|
||
}
|
||
|
||
// Intercepte les clics sur le lien (classe ou data-attr)
|
||
document.addEventListener('click', function(e){
|
||
const a = e.target.closest('a.contact-admin-link, [data-contact-admin]');
|
||
if (!a) return;
|
||
// si l'utilisateur ouvre au clavier/milieu => laisse le comportement natif
|
||
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || a.target === '_blank') return;
|
||
e.preventDefault();
|
||
openContactModal(a.getAttribute('data-lang') || '');
|
||
});
|
||
|
||
// Intercepte l’envoi du formulaire dans la popup
|
||
document.addEventListener('submit', function(e){
|
||
const form = e.target.closest('#contact-admin-form');
|
||
if (!form) return;
|
||
e.preventDefault();
|
||
|
||
const btn = form.querySelector('.caf-btn');
|
||
const fb = form.querySelector('.caf-feedback');
|
||
const lblSend = btn?.getAttribute('data-label-send') || 'Envoyer';
|
||
const lblSending = btn?.getAttribute('data-label-sending') || 'Envoi...';
|
||
|
||
if (btn) { btn.disabled = true; btn.textContent = lblSending; }
|
||
const fd = new FormData(form);
|
||
|
||
fetch(ENDPOINT, { method:'POST', headers:{'X-Requested-With':'XMLHttpRequest'}, body: fd })
|
||
.then(r => r.json())
|
||
.then(j => {
|
||
fb.textContent = j.msg || '';
|
||
fb.style.color = j.ok ? 'green' : 'crimson';
|
||
if (j.ok) {
|
||
form.reset();
|
||
setTimeout(() => document.querySelector('.caf-overlay')?.remove(), 1200);
|
||
}
|
||
})
|
||
.catch(() => { fb.textContent = 'Network error'; fb.style.color = 'crimson'; })
|
||
.finally(() => { if (btn) { btn.disabled = false; btn.textContent = lblSend; } });
|
||
});
|
||
|
||
// Fermer la popup (clic sur fond ou bouton ×)
|
||
document.addEventListener('click', function(e){
|
||
if (e.target.classList?.contains('caf-overlay') || e.target.classList?.contains('caf-close')) {
|
||
document.querySelector('.caf-overlay')?.remove();
|
||
}
|
||
});
|
||
})();
|