106 lines
3.9 KiB
JavaScript
106 lines
3.9 KiB
JavaScript
/*
|
|
app.js — boots the shell: header (glass → solid on scroll, nav state, search box),
|
|
mobile tab bar, footer label, then starts the router.
|
|
Exposes window.bugtv = { navigate, href, toast } for non-module callers / debugging.
|
|
*/
|
|
import { h, debounce } from './util.js';
|
|
import { getBase } from './config.js';
|
|
import { start, navigate, href, onChange, current } from './router.js';
|
|
import { toast } from './ui/toast.js';
|
|
import { icon } from './ui/icons.js';
|
|
|
|
const header = document.querySelector('.site-header');
|
|
const outlet = document.getElementById('app');
|
|
|
|
// links declared as data-href="/path" in index.html get mode-aware hrefs
|
|
function wireLinks(scope = document) {
|
|
scope.querySelectorAll('[data-href]').forEach((a) => a.setAttribute('href', href(a.dataset.href)));
|
|
}
|
|
|
|
// icons for static markup: <span data-icon="search"></span>
|
|
document.querySelectorAll('[data-icon]').forEach((el) => el.replaceWith(icon(el.dataset.icon, { size: Number(el.dataset.size) || 20 })));
|
|
wireLinks();
|
|
|
|
// header: transparent over the hero, solid once scrolled
|
|
const onScroll = () => header.classList.toggle('is-solid', window.scrollY > 24);
|
|
window.addEventListener('scroll', onScroll, { passive: true });
|
|
onScroll();
|
|
|
|
// search box in the header
|
|
const searchForm = document.querySelector('.header-search');
|
|
const searchInput = searchForm.querySelector('input');
|
|
const searchGo = debounce(() => {
|
|
const q = searchInput.value.trim();
|
|
const onSearch = current().name === 'search';
|
|
if (!q && !onSearch) return;
|
|
navigate(`/search${q ? `?q=${encodeURIComponent(q)}` : ''}`, { replace: onSearch });
|
|
}, 300);
|
|
searchInput.addEventListener('input', searchGo);
|
|
searchForm.addEventListener('submit', (e) => {
|
|
e.preventDefault();
|
|
searchGo.cancel();
|
|
const q = searchInput.value.trim();
|
|
navigate(`/search${q ? `?q=${encodeURIComponent(q)}` : ''}`, { replace: current().name === 'search' });
|
|
});
|
|
searchInput.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Escape') {
|
|
searchInput.value = '';
|
|
searchInput.blur();
|
|
}
|
|
});
|
|
searchForm.querySelector('.header-search__icon').addEventListener('click', () => searchInput.focus());
|
|
|
|
// "/" focuses search (like most streaming sites)
|
|
document.addEventListener('keydown', (e) => {
|
|
if (e.key === '/' && !/^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement.tagName) && !e.metaKey && !e.ctrlKey) {
|
|
e.preventDefault();
|
|
if (getComputedStyle(searchForm).display === 'none') navigate('/search');
|
|
else searchInput.focus();
|
|
}
|
|
});
|
|
|
|
// active nav state + keep the header search in sync with the URL
|
|
const navMap = { home: '/', movies: '/movies', tv: '/tv', list: '/list', search: '/search', settings: '/settings' };
|
|
onChange((r) => {
|
|
const active = navMap[r.name] || (r.name === 'browse' ? (r.params.type === 'tv' ? '/tv' : '/movies') : null);
|
|
document.querySelectorAll('[data-nav]').forEach((a) => {
|
|
const on = a.dataset.href === active;
|
|
a.classList.toggle('is-active', on);
|
|
if (on) a.setAttribute('aria-current', 'page');
|
|
else a.removeAttribute('aria-current');
|
|
});
|
|
if (r.name === 'search') {
|
|
if (document.activeElement !== searchInput) searchInput.value = r.query.get('q') || '';
|
|
} else {
|
|
searchInput.value = '';
|
|
}
|
|
header.classList.toggle('is-over-hero', r.name === 'home' || r.name === 'movies' || r.name === 'tv' || r.name === 'title');
|
|
onScroll();
|
|
});
|
|
|
|
// footer API label
|
|
const apiLabel = document.getElementById('apiBaseLabel');
|
|
if (apiLabel) {
|
|
const paint = () => {
|
|
apiLabel.textContent = getBase().replace(/^https?:\/\//, '');
|
|
apiLabel.href = getBase();
|
|
};
|
|
paint();
|
|
window.addEventListener('storage', paint);
|
|
onChange(paint);
|
|
}
|
|
|
|
// skip link (fragment links would navigate because of <base>)
|
|
const skip = document.querySelector('.skip-link');
|
|
if (skip) {
|
|
skip.addEventListener('click', (e) => {
|
|
e.preventDefault();
|
|
outlet.setAttribute('tabindex', '-1');
|
|
outlet.focus();
|
|
});
|
|
}
|
|
|
|
window.bugtv = Object.assign(window.bugtv || {}, { navigate, href, toast, h });
|
|
|
|
start(outlet);
|