push
This commit is contained in:
1 parent
bcf05e8063
commit
f37985b4c8
109 files changed
+22810
-6143
No files matched your search
+96
-32
@@ -1,41 +1,105 @@
|
||||
/*
|
||||
APP — shared chrome for every page: footer API label, toast notifications.
|
||||
(Settings moved to its own page: settings.html)
|
||||
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.
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
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';
|
||||
|
||||
function initChrome() {
|
||||
const label = document.getElementById('apiBaseLabel');
|
||||
if (label) {
|
||||
const base = SR.config.getBase();
|
||||
label.textContent = base;
|
||||
label.href = base;
|
||||
}
|
||||
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());
|
||||
|
||||
let toastTimer = null;
|
||||
|
||||
function toast(message, isError) {
|
||||
let el = document.getElementById('toast');
|
||||
if (!el) {
|
||||
el = document.createElement('div');
|
||||
el.id = 'toast';
|
||||
el.className = 'toast';
|
||||
el.setAttribute('role', 'status');
|
||||
document.body.appendChild(el);
|
||||
}
|
||||
el.textContent = message;
|
||||
el.classList.toggle('toast--error', !!isError);
|
||||
el.classList.add('toast--show');
|
||||
clearTimeout(toastTimer);
|
||||
toastTimer = setTimeout(() => el.classList.remove('toast--show'), 4500);
|
||||
// "/" 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();
|
||||
}
|
||||
});
|
||||
|
||||
function showError(err, fallback) {
|
||||
toast(err && err.message ? err.message : fallback || 'Something went wrong', true);
|
||||
// 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();
|
||||
});
|
||||
|
||||
window.SR = window.SR || {};
|
||||
SR.app = { initChrome, toast, showError };
|
||||
})();
|
||||
// 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);
|
||||
Reference in new issue
Block a user