Files
eportfolio/www/js/main.js
T
2026-10-10 15:36:06 +02:00

76 lines
4.0 KiB
JavaScript

/* ===== Contact form settings =====
The form posts to contact.php on your own server, which sends the message
to your Proton inbox over SMTP (see LISEZMOI.txt).
If the server can't be reached, the visitor is invited to try again later
or to use LinkedIn. No email address is shown on the site. */
var CONTACT_ENDPOINT = "contact.php";
var LINKEDIN_URL = "https://www.linkedin.com/in/nelson-thomas-pr/";
(function(){
var root=document.documentElement;
try{var s=localStorage.getItem('theme');if(s)root.dataset.theme=s}catch(e){}
document.getElementById('theme').addEventListener('click',function(){
var dark=root.dataset.theme?root.dataset.theme==='dark':!matchMedia('(prefers-color-scheme: light)').matches;
root.dataset.theme=dark?'light':'dark';
try{localStorage.setItem('theme',root.dataset.theme)}catch(e){}
});
// Scroll-spy: highlight the section on screen
var links=[].slice.call(document.querySelectorAll('nav a'));
var nav=document.querySelector('nav');
function setActive(id){
links.forEach(function(a){
var on=a.getAttribute('href')==='#'+id;
a.classList.toggle('active',on);
if(on){a.setAttribute('aria-current','true');
var l=a.offsetLeft-nav.offsetLeft, r=l+a.offsetWidth;
if(l<nav.scrollLeft||r>nav.scrollLeft+nav.clientWidth) nav.scrollTo({left:l-16,behavior:'smooth'});
} else a.removeAttribute('aria-current');
});
}
var sections=[].slice.call(document.querySelectorAll('main>section'));
if('IntersectionObserver' in window){
var io=new IntersectionObserver(function(entries){
entries.forEach(function(e){if(e.isIntersecting)setActive(e.target.id)});
},{rootMargin:'-45% 0px -50% 0px'});
sections.forEach(function(s){io.observe(s)});
}
// Reading progress line under the header
var header=document.querySelector('header');
function progress(){
var h=document.documentElement, max=h.scrollHeight-h.clientHeight;
header.style.setProperty('--progress',(max>0?h.scrollTop/max*100:0)+'%');
if(h.scrollTop+h.clientHeight>=h.scrollHeight-4) setActive('contact');
}
window.addEventListener('scroll',progress,{passive:true}); progress();
// Contact form
var form=document.getElementById('contact-form'), btn=document.getElementById('f-send'), st=document.getElementById('f-status');
function say(msg,cls){st.textContent=msg;st.className='status '+(cls||'')}
function sendFailed(){
st.className='status err'; st.textContent='The message could not be sent. Try again in a few minutes, or reach me on ';
var a=document.createElement('a'); a.href=LINKEDIN_URL; a.target='_blank'; a.rel='noopener'; a.textContent='LinkedIn';
st.appendChild(a); st.appendChild(document.createTextNode('.'));
}
form.addEventListener('submit',function(e){
e.preventDefault();
if(!form.checkValidity()){form.reportValidity();say('Fill in your name, a valid email, a subject and a message.','err');return}
if(form.botcheck.checked)return;
var d={name:form.name.value.trim(),email:form.email.value.trim(),company:form.company.value.trim(),subject:form.subject.value.trim(),message:form.message.value.trim()};
btn.disabled=true;btn.textContent='Sending...';say('');
fetch(CONTACT_ENDPOINT,{method:'POST',headers:{'Content-Type':'application/json',Accept:'application/json'},
body:JSON.stringify({name:d.name,email:d.email,company:d.company,subject:d.subject,message:d.message,botcheck:false})})
.then(function(r){
return r.json().catch(function(){throw new Error('network')}).then(function(j){return {status:r.status,body:j}});
})
.then(function(r){
if(r.body.success){form.reset();say(r.body.message,'ok');return}
// Visitor-side problems (bad field, too many messages): show the server's message
if(r.status===422||r.status===429||r.status===400){say(r.body.message,'err');return}
throw new Error('server');
})
.catch(sendFailed)
.finally(function(){btn.disabled=false;btn.textContent='Send message'});
});
})();