2026-10-22 16:49:25 +08:00
|
|
|
'use strict';
|
|
|
|
|
(()=>{
|
|
|
|
|
const $=s=>document.querySelector(s), $$=s=>[...document.querySelectorAll(s)];
|
|
|
|
|
const menu=$('.sf-menu-toggle');if(menu){menu.addEventListener('click',()=>{let open=menu.getAttribute('aria-expanded')!=='true';menu.setAttribute('aria-expanded',String(open));$('#sf-primary-links')?.classList.toggle('is-open',open);});}
|
2026-10-26 18:03:07 +08:00
|
|
|
// V8.34.1: accessible, manual carousel. Native scroll/swipe works without JS.
|
|
|
|
|
$$('.sf-hero-slider[data-sf-hero-slider]').forEach(slider=>{
|
|
|
|
|
const track=slider.querySelector('.sf-hero-track');const panels=[...slider.querySelectorAll('.sf-hero-panel')];
|
|
|
|
|
if(!track||panels.length<2)return;
|
|
|
|
|
const counter=slider.querySelector('.sf-hero-count');let index=0;
|
|
|
|
|
const reduced=window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
|
|
|
|
|
function choose(next){index=(next+panels.length)%panels.length;track.scrollTo({left:panels[index].offsetLeft-panels[0].offsetLeft,behavior:reduced?'instant':'smooth'});if(counter)counter.textContent=`${index+1} / ${panels.length}`;}
|
|
|
|
|
slider.querySelectorAll('[data-sf-slide-step]').forEach(btn=>btn.addEventListener('click',()=>choose(index+Number(btn.dataset.sfSlideStep))));
|
|
|
|
|
let raf=false;track.addEventListener('scroll',()=>{if(raf)return;raf=true;requestAnimationFrame(()=>{const left=track.scrollLeft;let best=0,delta=Infinity;panels.forEach((panel,i)=>{const d=Math.abs(panel.offsetLeft-panels[0].offsetLeft-left);if(d<delta){best=i;delta=d;}});index=best;if(counter)counter.textContent=`${index+1} / ${panels.length}`;raf=false;});},{passive:true});
|
|
|
|
|
});
|
2026-10-22 16:49:25 +08:00
|
|
|
// Filter actual static content cards, no API is needed at runtime.
|
2026-10-26 14:27:17 +08:00
|
|
|
$$('.sf-collection[data-sf-filterable="true"]').forEach(grid=>{
|
2026-10-22 16:49:25 +08:00
|
|
|
const cards=$$('.sf-item',grid);
|
|
|
|
|
if(cards.length<2)return;
|
|
|
|
|
const toolbar=document.createElement('div');toolbar.className='sf-filters';
|
|
|
|
|
const search=document.createElement('input');search.type='search';search.placeholder='筛选当前列表';search.setAttribute('aria-label','筛选当前列表');
|
|
|
|
|
const status=document.createElement('span');status.setAttribute('aria-live','polite');status.style.fontSize='12px';
|
|
|
|
|
const brand=document.createElement('select');brand.setAttribute('aria-label','按品牌筛选');
|
|
|
|
|
const category=document.createElement('select');category.setAttribute('aria-label','按分类筛选');
|
|
|
|
|
function fill(el,field,all){el.append(new Option(all,''));const vals=[...new Set(cards.map(x=>x.dataset[field]||'').filter(Boolean))].sort();vals.forEach(v=>el.append(new Option(v,v)));if(!vals.length)el.hidden=true;}
|
|
|
|
|
fill(brand,'brand','全部品牌');fill(category,'category','全部分类');
|
|
|
|
|
toolbar.append(search,brand,category,status);grid.parentNode.insertBefore(toolbar,grid);
|
|
|
|
|
function apply(){let val=search.value.trim().toLocaleLowerCase();let visible=0;cards.forEach(card=>{let hit=card.textContent.toLocaleLowerCase().includes(val)&&(!brand.value||card.dataset.brand===brand.value)&&(!category.value||card.dataset.category===category.value);card.hidden=!hit;if(hit)visible++;});status.textContent=`共 ${visible} 项`;}
|
|
|
|
|
search.addEventListener('input',apply);brand.addEventListener('change',apply);category.addEventListener('change',apply);apply();
|
|
|
|
|
});
|
|
|
|
|
const input=$('#sf-search'),root=$('#sf-search-results'),count=$('#sf-search-count');
|
|
|
|
|
if(input&&root){
|
|
|
|
|
let data=[];const params=new URLSearchParams(location.search);input.value=params.get('q')||'';
|
|
|
|
|
let debounce=null;
|
|
|
|
|
const render=()=>{let q=input.value.trim().toLocaleLowerCase();let found=q?data.filter(d=>`${d.title} ${d.summary} ${d.brand} ${d.type}`.toLocaleLowerCase().includes(q)).slice(0,100):data.slice(0,30);
|
|
|
|
|
root.replaceChildren();count.textContent=`${found.length} 条结果${!q?'(显示前30条)':''}`;
|
|
|
|
|
found.forEach(d=>{let article=document.createElement('article'),a=document.createElement('a'),small=document.createElement('small'),p=document.createElement('p');article.className='sf-search-result';a.textContent=d.title;a.href=d.url.replace(/^\//,'');small.textContent=d.type;p.textContent=d.summary;article.append(a,small,p);root.append(article);});
|
|
|
|
|
};const base=document.querySelector('base')?.href||location.href;
|
|
|
|
|
fetch(new URL('search-index.json',base)).then(r=>{if(!r.ok)throw Error('index unavailable');return r.json();}).then(v=>{data=v;render()}).catch(()=>{count.textContent='搜索索引加载失败,请检查站点文件部署路径';});
|
|
|
|
|
input.addEventListener('input',()=>{clearTimeout(debounce);debounce=setTimeout(render,100)});
|
|
|
|
|
}
|
|
|
|
|
})();
|