document.addEventListener("DOMContentLoaded", function(){
const data=window.covernewsTocData||{};
const headerOffset=parseInt(data.headerOffset, 10)||100;
const smoothScroll=data.smoothScroll ? true:false;
const tocSelector='.covernews-toc-list a';
const tocLinks=Array.from(document.querySelectorAll(tocSelector));
if(!tocLinks.length) return;
let live=document.getElementById('covernews-toc-live');
if(!live){
live=document.createElement('div');
live.id='covernews-toc-live';
live.setAttribute('aria-live', 'polite');
live.setAttribute('aria-atomic', 'true');
live.style.position='absolute';
live.style.width='1px';
live.style.height='1px';
live.style.overflow='hidden';
live.style.clip='rect(1px, 1px, 1px, 1px)';
live.style.clipPath='inset(50%)';
document.body.appendChild(live);
}
const sections=tocLinks.map(link=> {
const id=link.getAttribute('href').replace('#', '');
const el=document.getElementById(id);
return el ? { link: link, section: el }:null;
}).filter(Boolean);
function focusSection(targetEl, announceText){
const targetY=targetEl.getBoundingClientRect().top + window.pageYOffset - headerOffset;
if(smoothScroll){
window.scrollTo({ top: targetY, behavior: 'smooth' });
}else{
window.scrollTo(0, targetY);
}
const start=performance.now();
function check(){
const delta=Math.abs(window.pageYOffset - targetY);
if(delta < 3||performance.now() - start > 1000){
const hadTab=targetEl.hasAttribute('tabindex');
if(!hadTab) targetEl.setAttribute('tabindex', '-1');
targetEl.focus({preventScroll:true});
if(!hadTab) setTimeout(()=> targetEl.removeAttribute('tabindex'), 1000);
if(announceText&&live) live.textContent=announceText + ': ' + targetEl.textContent;
return;
}
requestAnimationFrame(check);
}
requestAnimationFrame(check);
}
sections.forEach(item=> {
item.link.addEventListener('click', function(e){
e.preventDefault();
const announce=data.strings&&data.strings.jump_to_section ? data.strings.jump_to_section:'';
focusSection(item.section, announce);
history.replaceState(null, '', '#' + item.section.id);
});
});
function onScrollSpy(){
const scrollPos=window.pageYOffset + headerOffset + 5;
let current=null;
for (let i=0; i < sections.length; i++){
if(scrollPos >=sections[i].section.offsetTop){
current=sections[i];
}else{
break;
}}
tocLinks.forEach(l=> {
l.removeAttribute('aria-current');
l.classList.remove('active');
});
if(current){
current.link.setAttribute('aria-current', 'true');
current.link.classList.add('active');
}}
});