109 lines
5.8 KiB
JavaScript
109 lines
5.8 KiB
JavaScript
/* Mataba static interactions (vanilla JS) */
|
|
(function(){
|
|
"use strict";
|
|
var body=document.body;
|
|
// Mobile menu (layoutContext port)
|
|
var burger=document.getElementById('hamburger');
|
|
var backdrop=document.getElementById('drawerBackdrop');
|
|
var closeBtn=document.getElementById('drawerClose');
|
|
function setMenu(open){
|
|
body.classList.toggle('menu-open',open);
|
|
if(burger)burger.setAttribute('aria-expanded',open?'true':'false');
|
|
}
|
|
if(burger)burger.addEventListener('click',function(){setMenu(!body.classList.contains('menu-open'));});
|
|
if(backdrop)backdrop.addEventListener('click',function(){setMenu(false);});
|
|
if(closeBtn)closeBtn.addEventListener('click',function(){setMenu(false);});
|
|
document.querySelectorAll('.nav a').forEach(function(a){a.addEventListener('click',function(){setMenu(false);});});
|
|
document.addEventListener('keydown',function(e){if(e.key==='Escape'){setMenu(false);closeModal();}});
|
|
|
|
// Login modal (sign-in-modal port, validation from login.schema.ts)
|
|
var modal=document.getElementById('loginModal');
|
|
var phone=document.getElementById('phoneInput');
|
|
var err=document.getElementById('phoneError');
|
|
var okMsg=document.getElementById('phoneOk');
|
|
function openModal(){if(modal){modal.classList.add('open');modal.setAttribute('aria-hidden','false');if(phone)phone.focus();}}
|
|
function closeModal(){if(modal){modal.classList.remove('open');modal.setAttribute('aria-hidden','true');}}
|
|
window.matabaOpenLogin=openModal;
|
|
document.querySelectorAll('[data-open-login]').forEach(function(b){b.addEventListener('click',openModal);});
|
|
document.querySelectorAll('[data-close-login]').forEach(function(b){b.addEventListener('click',closeModal);});
|
|
if(modal)modal.addEventListener('click',function(e){if(e.target===modal)closeModal();});
|
|
var form=document.getElementById('loginForm');
|
|
if(form)form.addEventListener('submit',function(e){
|
|
e.preventDefault();
|
|
var v=(phone.value||'').trim();
|
|
var valid=/^09\d{9}$/.test(v);
|
|
if(!valid){
|
|
err.textContent='شماره موبایل باید با 09 شروع شود و ۱۱ رقم باشد (مثلاً 09181234567).';
|
|
if(okMsg)okMsg.textContent='';
|
|
phone.setAttribute('aria-invalid','true');
|
|
return;
|
|
}
|
|
err.textContent='';
|
|
phone.removeAttribute('aria-invalid');
|
|
if(okMsg)okMsg.textContent='درخواست شما با موفقیت ثبت شد. کارشناسان مطبا به زودی با شما تماس میگیرند.';
|
|
});
|
|
|
|
// Testimonials carousel (DoctorsCmSlider port, replaces Swiper)
|
|
var track=document.getElementById('tTrack');
|
|
function step(){if(!track)return 0;var c=track.querySelector('.tcard');if(!c)return 320;return c.getBoundingClientRect().width+20;}
|
|
var prev=document.getElementById('tPrev'),next=document.getElementById('tNext');
|
|
if(prev&&track)prev.addEventListener('click',function(){track.scrollBy({left:step(),top:0,behavior:'smooth'});});
|
|
if(next&&track)next.addEventListener('click',function(){track.scrollBy({left:-step(),top:0,behavior:'smooth'});});
|
|
// autoplay 3s like original, pause on hover
|
|
var timer=null;
|
|
function start(){stop();timer=setInterval(function(){
|
|
if(!track)return;
|
|
var max=track.scrollWidth-track.clientWidth-4;
|
|
if(track.scrollLeft<=4){track.scrollTo({left:max,behavior:'smooth'});}
|
|
else{track.scrollBy({left:-step(),behavior:'smooth'});}
|
|
},3000);}
|
|
function stop(){if(timer)clearInterval(timer);timer=null;}
|
|
if(track){start();track.addEventListener('mouseenter',stop);track.addEventListener('mouseleave',start);}
|
|
|
|
var dForm=document.getElementById('domainForm');
|
|
if(dForm)dForm.addEventListener('submit',function(e){
|
|
e.preventDefault();
|
|
var q=document.getElementById('domainInput').value.trim();
|
|
var msg=document.getElementById('domainMsg');
|
|
if(!q){msg.textContent='نام دامنه را وارد کنید.';return;}
|
|
msg.textContent='نام دامنه «'+q+'» در دسترس است. برای ثبت با مطبا تماس بگیرید.';
|
|
});
|
|
document.querySelectorAll('.domain').forEach(function(b){
|
|
b.addEventListener('click',function(){
|
|
var inp=document.getElementById('domainInput');
|
|
if(inp){inp.value=inp.value.replace(/\.(ir|com|net|doctor|site|shop)\.?$/i,'')+b.textContent.trim();inp.focus();}
|
|
});
|
|
});
|
|
|
|
function wireLead(id){
|
|
var f=document.getElementById(id);
|
|
if(!f)return;
|
|
f.addEventListener('submit',function(e){
|
|
e.preventDefault();
|
|
var name=f.querySelector('[name=name]'),mobile=f.querySelector('[name=mobile]'),
|
|
spec=f.querySelector('[name=spec]'),msg=f.querySelector('textarea'),
|
|
eEl=f.querySelector('.ferr'),oEl=f.querySelector('.fok');
|
|
var problems=[];
|
|
if(name&&!name.value.trim())problems.push('نام و نام خانوادگی لازم است.');
|
|
var mv=mobile?mobile.value.trim():'';
|
|
if(mv&&!/^09\d{9}$/.test(mv))problems.push('شماره تماس معتبر نیست (11 رقم با 09).');
|
|
if(msg&&!msg.value.trim())problems.push('توضیحات را بنویسید.');
|
|
if(problems.length){eEl.textContent=problems.join(' ');oEl.textContent='';return;}
|
|
eEl.textContent='';
|
|
oEl.textContent='درخواست شما با موفقیت ثبت شد. کارشناسان مطبا به زودی با شما تماس میگیرند.';
|
|
f.reset();
|
|
});
|
|
}
|
|
wireLead('leadForm');wireLead('leadFormPage');wireLead('contactFormPage');
|
|
|
|
// Active nav on scroll
|
|
var links=document.querySelectorAll('.nav a[href^="#"]');
|
|
var secs=[].map.call(links,function(a){return document.querySelector(a.getAttribute('href'));}).filter(Boolean);
|
|
function onScroll(){
|
|
var y=window.scrollY+140,cur=null;
|
|
secs.forEach(function(s){if(s.offsetTop<=y)cur=s;});
|
|
links.forEach(function(a){a.classList.toggle('active',cur&&a.getAttribute('href')==='#'+cur.id);});
|
|
}
|
|
window.addEventListener('scroll',onScroll,{passive:true});onScroll();
|
|
})();
|