179 lines
6.8 KiB
JavaScript
179 lines
6.8 KiB
JavaScript
/*
|
|
hero.js — rotating billboard for the home page.
|
|
|
|
billboard(items, { eyebrow: (i) => string, interval = 8000 }) → { el, destroy() }
|
|
items: tmdb.toItem() shapes (need backdrop_path). Details (logo art, genres, certification,
|
|
runtime/seasons) are fetched per slide and patched in when they arrive.
|
|
*/
|
|
import { h, runtimeLabel, plural, reducedMotion } from '../util.js';
|
|
import { details as fetchDetails, img, logoFor, certificationFor } from '../tmdb.js';
|
|
import { href } from '../router.js';
|
|
import { icon } from './icons.js';
|
|
import { playTarget, titlePath, myListButton } from './actions.js';
|
|
|
|
function backdrop(path, eager) {
|
|
const el = h('img.hero__img', {
|
|
src: img(path, 'w1280'),
|
|
srcset: `${img(path, 'w780')} 780w, ${img(path, 'w1280')} 1280w, ${img(path, 'original')} 1920w`,
|
|
sizes: '100vw',
|
|
alt: '',
|
|
decoding: 'async',
|
|
loading: eager ? 'eager' : 'lazy',
|
|
fetchpriority: eager ? 'high' : 'low',
|
|
});
|
|
el.addEventListener('load', () => el.classList.add('is-loaded'), { once: true });
|
|
return el;
|
|
}
|
|
|
|
function slide(item, i, opts) {
|
|
const titleText = h('h2.hero__title', item.title);
|
|
const titleBox = h('div.hero__titlebox', titleText);
|
|
const meta = h('div.hero__meta',
|
|
item.rating > 0 ? h('span.rating.rating--lg', icon('star', { size: 16 }), item.rating.toFixed(1)) : null,
|
|
item.year ? h('span', item.year) : null,
|
|
h('span.chip', item.type === 'tv' ? 'Series' : 'Movie'),
|
|
);
|
|
const play = h('a.btn.btn--primary.btn--lg', { href: href(playTarget(item.type, item.id).path) }, icon('play'), h('span', playTarget(item.type, item.id).label));
|
|
const more = h('a.btn.btn--glass.btn--lg', { href: href(titlePath(item.type, item.id)) }, icon('info'), h('span', 'More Info'));
|
|
const el = h('article.hero__slide', { 'aria-roledescription': 'slide', 'aria-label': `${i + 1} of ${opts.count}: ${item.title}`, 'aria-hidden': i === 0 ? 'false' : 'true' },
|
|
h('div.hero__media', backdrop(item.backdrop_path, i === 0)),
|
|
h('div.hero__scrim'),
|
|
h('div.hero__content.container',
|
|
opts.eyebrow ? h('div.hero__eyebrow', icon('fire', { size: 18 }), opts.eyebrow(i, item)) : null,
|
|
titleBox,
|
|
meta,
|
|
item.overview ? h('p.hero__overview', item.overview) : null,
|
|
h('div.hero__actions', play, more, myListButton(item, { className: 'btn btn--icon btn--glass btn--lg', iconOnly: true })),
|
|
),
|
|
);
|
|
if (i !== 0) el.inert = true;
|
|
|
|
el._hydrate = async () => {
|
|
let d;
|
|
try {
|
|
d = await fetchDetails(item.type, item.id);
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
const logo = logoFor(d);
|
|
if (logo) {
|
|
const im = h('img.hero__logo', { src: img(logo.file_path, 'w500'), alt: item.title, decoding: 'async' });
|
|
im.addEventListener('load', () => {
|
|
titleBox.classList.add('has-logo');
|
|
titleText.classList.add('sr-only');
|
|
}, { once: true });
|
|
im.addEventListener('error', () => im.remove(), { once: true });
|
|
titleBox.prepend(im);
|
|
}
|
|
const genres = (d.genres || []).slice(0, 3).map((g) => g.name);
|
|
const cert = certificationFor(d);
|
|
const extra = item.type === 'tv'
|
|
? d.number_of_seasons ? plural(d.number_of_seasons, 'Season') : ''
|
|
: runtimeLabel(d.runtime);
|
|
meta.replaceChildren(...[
|
|
item.rating > 0 ? h('span.rating.rating--lg', icon('star', { size: 16 }), item.rating.toFixed(1)) : null,
|
|
item.year ? h('span', item.year) : null,
|
|
extra ? h('span', extra) : null,
|
|
genres.length ? h('span.hero__genres', genres.join(' · ')) : null,
|
|
cert ? h('span.chip.chip--outline', cert) : null,
|
|
].filter(Boolean));
|
|
const target = playTarget(item.type, item.id, d);
|
|
play.href = href(target.path);
|
|
play.lastChild.textContent = target.label;
|
|
};
|
|
return el;
|
|
}
|
|
|
|
export function billboard(items, opts = {}) {
|
|
const data = (items || []).filter((x) => x && x.backdrop_path).slice(0, opts.max || 5);
|
|
const interval = opts.interval || 8000;
|
|
if (!data.length) return { el: h('div.hero.hero--empty'), destroy() {} };
|
|
|
|
const slides = data.map((it, i) => slide(it, i, { count: data.length, eyebrow: opts.eyebrow }));
|
|
const dots = data.map((it, i) => h('button.hero__dot', { type: 'button', 'aria-label': `Show ${it.title}`, style: { '--dur': `${interval}ms` } }, h('i')));
|
|
const el = h('section.hero', { 'aria-roledescription': 'carousel', 'aria-label': 'Featured' },
|
|
h('div.hero__slides', slides),
|
|
data.length > 1 ? h('div.hero__dots.container', h('div.hero__dots-inner', dots)) : null,
|
|
);
|
|
|
|
let cur = 0;
|
|
let timer = null;
|
|
let paused = false;
|
|
let destroyed = false;
|
|
|
|
function show(i) {
|
|
const nextI = (i + data.length) % data.length;
|
|
slides.forEach((s, k) => {
|
|
const on = k === nextI;
|
|
s.classList.toggle('is-active', on);
|
|
s.setAttribute('aria-hidden', on ? 'false' : 'true');
|
|
s.inert = !on;
|
|
});
|
|
dots.forEach((d, k) => {
|
|
d.classList.toggle('is-active', k === nextI);
|
|
d.setAttribute('aria-current', k === nextI ? 'true' : 'false');
|
|
});
|
|
cur = nextI;
|
|
// restart the dot fill animation
|
|
const fill = dots[cur] && dots[cur].firstChild;
|
|
if (fill) {
|
|
fill.style.animation = 'none';
|
|
void fill.offsetWidth;
|
|
fill.style.animation = '';
|
|
}
|
|
schedule();
|
|
}
|
|
|
|
function schedule() {
|
|
clearTimeout(timer);
|
|
if (destroyed || paused || document.hidden || data.length < 2 || reducedMotion()) return;
|
|
timer = setTimeout(() => show(cur + 1), interval);
|
|
}
|
|
|
|
function setPaused(p) {
|
|
paused = p;
|
|
el.classList.toggle('is-paused', p);
|
|
if (p) clearTimeout(timer);
|
|
else schedule();
|
|
}
|
|
|
|
dots.forEach((d, i) => d.addEventListener('click', () => show(i)));
|
|
el.addEventListener('mouseenter', () => setPaused(true));
|
|
el.addEventListener('mouseleave', () => setPaused(false));
|
|
el.addEventListener('focusin', () => setPaused(true));
|
|
el.addEventListener('focusout', (e) => {
|
|
if (!el.contains(e.relatedTarget)) setPaused(false);
|
|
});
|
|
el.addEventListener('keydown', (e) => {
|
|
if (e.target.closest('.hero__dots') && (e.key === 'ArrowRight' || e.key === 'ArrowLeft')) {
|
|
e.preventDefault();
|
|
show(cur + (e.key === 'ArrowRight' ? 1 : -1));
|
|
dots[cur].focus();
|
|
}
|
|
});
|
|
// swipe on touch
|
|
let sx = null;
|
|
el.addEventListener('touchstart', (e) => { sx = e.touches[0].clientX; }, { passive: true });
|
|
el.addEventListener('touchend', (e) => {
|
|
if (sx == null) return;
|
|
const dx = e.changedTouches[0].clientX - sx;
|
|
sx = null;
|
|
if (Math.abs(dx) > 50) show(cur + (dx < 0 ? 1 : -1));
|
|
});
|
|
const onVis = () => (document.hidden ? clearTimeout(timer) : schedule());
|
|
document.addEventListener('visibilitychange', onVis);
|
|
|
|
show(0);
|
|
slides[0]._hydrate();
|
|
setTimeout(() => slides.slice(1).forEach((s) => s._hydrate()), 600);
|
|
|
|
return {
|
|
el,
|
|
destroy() {
|
|
destroyed = true;
|
|
clearTimeout(timer);
|
|
document.removeEventListener('visibilitychange', onVis);
|
|
},
|
|
};
|
|
}
|