Files
eportfolio/www/js/main.js
T

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 (e.g. site opened as a local file), the form
opens the visitor's mail app with the message already written. */
var CONTACT_ENDPOINT = "contact.php";
var CONTACT_EMAIL = "nelson.thomas.contact@pm.me";
(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 mailtoFallback(d){
var body=d.message+'\n\n'+d.name+(d.company?' ('+d.company+')':'')+'\n'+d.email;
window.location.href='mailto:'+CONTACT_EMAIL+'?subject='+encodeURIComponent(d.subject)+'&body='+encodeURIComponent(body);
say('Your mail app should open with the message ready. If nothing happens, write to '+CONTACT_EMAIL+'.','ok');
}
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(function(){mailtoFallback(d)})
.finally(function(){btn.disabled=false;btn.textContent='Send message'});
});
})();