catalog.js 1.7 KB

1234567891011121314151617181920212223242526272829
  1. /* Instruments and effects: the category chips filter the groups. Without this script they are plain links to them. */
  2. (() => {
  3. const bar = document.getElementById('cfilter');
  4. if (!bar) return;
  5. const chips = [...bar.querySelectorAll('a[data-f]')];
  6. const show = f => {
  7. const [kind, v] = f.split(':');
  8. document.querySelectorAll('.cgroup').forEach(g => { g.hidden = !(f === 'all' || (kind === 't' && g.dataset.type === v) || (kind === 'c' && g.id === 'g-' + v)); });
  9. document.querySelectorAll('.ctype').forEach(h => { h.hidden = !(f === 'all' || (kind === 't' && h.dataset.type === v)); });
  10. chips.forEach(c => c.classList.toggle('on', c.dataset.f === f));
  11. };
  12. // with motion on (html.mo): the page cross-fades to the filtered list where the browser has view transitions, else the cards fade in
  13. const cat = document.getElementById('cat'), mo = document.documentElement.classList.contains('mo');
  14. const filter = f => {
  15. if (!mo) return show(f);
  16. if (window.LYNX_MOTION) window.LYNX_MOTION.flush(cat);
  17. if (document.startViewTransition) return document.startViewTransition(() => show(f));
  18. show(f); cat.classList.remove('flt'); void cat.offsetWidth; cat.classList.add('flt');
  19. };
  20. chips.forEach(c => c.addEventListener('click', e => {
  21. e.preventDefault();
  22. filter(c.dataset.f);
  23. try { history.replaceState(null, '', c.dataset.f === 'all' ? location.pathname + location.search : c.getAttribute('href')); } catch (x) {}
  24. }));
  25. // an address with #g-eq or #t-effect opens filtered; #p-<plugin> shows everything and scrolls to the card
  26. const h = location.hash.slice(1);
  27. const hit = chips.find(c => c.getAttribute('href') === '#' + h && c.dataset.f !== 'all');
  28. if (hit) show(hit.dataset.f);
  29. })();