Files
tv/web/js/ui/card.js
T
2026-10-27 05:49:38 -05:00

133 lines
5.3 KiB
JavaScript

/*
card.js — media cards.
posterCard(item) 2:3 poster, hover lift + quick info
wideCard(item, { progress, kicker, meta, href, onRemove, image }) 16:9 backdrop, title overlay
logoCard({ name, logo, href }) network/studio tile
skeletonCard(variant)
grid(items, render?) responsive poster grid
`item` is the shape from tmdb.toItem(): { type, id, title, year, rating, poster_path, backdrop_path, overview }.
Every card root carries class "card" and its focus target is the <a class="card__link">.
*/
import { h } from '../util.js';
import { img, srcset } from '../tmdb.js';
import { titleHref, playTarget } from './actions.js';
import { navigate } from '../router.js';
import { icon } from './icons.js';
function typeLabel(type) {
return type === 'tv' ? 'Series' : 'Movie';
}
function ratingNode(r) {
if (!r || r <= 0) return null;
return h('span.rating', icon('star', { size: 12 }), Number(r).toFixed(1));
}
function image(path, sizes, alt, sizesAttr, cls = 'card__img') {
if (!path) return null;
const el = h('img', {
class: cls,
src: img(path, sizes[1] || sizes[0]),
srcset: srcset(path, sizes),
sizes: sizesAttr,
alt: alt || '',
loading: 'lazy',
decoding: 'async',
});
el.addEventListener('load', () => el.classList.add('is-loaded'), { once: true });
el.addEventListener('error', () => el.remove(), { once: true });
return el;
}
// the hover play button skips the title page and starts playback (resuming if there's progress)
function playButton(item, size, extraClass = '') {
return h(`span.card__play${extraClass}`, {
role: 'button',
'aria-label': `Play ${item.title}`,
title: 'Play',
onclick: (e) => {
e.preventDefault();
e.stopPropagation();
navigate(playTarget(item.type, item.id).path);
},
}, icon('play', { size }));
}
export function posterCard(item, opts = {}) {
const meta = [item.year, typeLabel(item.type)].filter(Boolean).join(' · ');
return h('div.card.card--poster', { role: 'listitem' },
h('a.card__link', { href: opts.href || titleHref(item.type, item.id), 'aria-label': `${item.title}${item.year ? ` (${item.year})` : ''}` },
h('div.card__media',
h('div.card__fallback', h('span', item.title)),
image(item.poster_path, ['w185', 'w342', 'w500'], '', '(max-width: 720px) 34vw, 200px'),
opts.rank ? h('span.card__rank', String(opts.rank)) : null,
h('div.card__hover',
playButton(item, 18),
h('div.card__hover-meta', ratingNode(item.rating), item.year ? h('span', item.year) : null),
),
opts.progress > 0 ? h('div.progress.card__progress', h('i', { style: { width: `${Math.round(opts.progress * 100)}%` } })) : null,
),
h('div.card__body',
h('div.card__title', item.title),
h('div.card__meta', opts.meta || meta),
),
),
);
}
export function wideCard(item, opts = {}) {
const path = opts.image !== undefined ? opts.image : item.backdrop_path;
const imgEl = typeof path === 'string' && /^https?:/.test(path)
? h('img.card__img', { src: path, alt: '', loading: 'lazy', decoding: 'async', onload: (e) => e.target.classList.add('is-loaded'), onerror: (e) => e.target.remove() })
: image(path, ['w300', 'w780'], '', '(max-width: 720px) 75vw, 340px');
const card = h('div.card.card--wide', { role: 'listitem' },
h('a.card__link', { href: opts.href || titleHref(item.type, item.id), 'aria-label': [item.title, opts.kicker, opts.meta].filter(Boolean).join(' — ') },
h('div.card__media',
h('div.card__fallback', h('span', item.title)),
imgEl,
h('div.card__shade'),
h('div.card__overlay',
opts.kicker ? h('div.card__kicker', opts.kicker) : null,
h('div.card__title', item.title),
opts.meta ? h('div.card__meta', opts.meta) : h('div.card__meta', ratingNode(item.rating), [item.year, typeLabel(item.type)].filter(Boolean).join(' · ')),
opts.progress != null ? h('div.progress', h('i', { style: { width: `${Math.round(Math.min(1, opts.progress) * 100)}%` } })) : null,
),
playButton(item, 22, '.card__play--center'),
),
),
);
if (opts.onRemove) {
card.appendChild(h('button.card__remove', {
type: 'button',
'aria-label': `Remove ${item.title} from Continue Watching`,
title: 'Remove',
onclick: (e) => {
e.preventDefault();
opts.onRemove(card);
},
}, icon('close', { size: 16 })));
}
return card;
}
export function logoCard({ name, logo, href }) {
return h('div.card.card--logo', { role: 'listitem' },
h('a.card__link', { href, 'aria-label': name },
h('div.card__media',
logo ? h('img.card__logo', { src: img(logo, 'w300'), alt: name, loading: 'lazy', decoding: 'async', onerror: (e) => { e.target.replaceWith(h('span.card__logo-text', name)); } }) : h('span.card__logo-text', name),
),
),
);
}
export function skeletonCard(variant = 'poster') {
return h('div.card.card--skeleton', { class: `card--${variant}`, 'aria-hidden': 'true' },
h('div.card__media.skeleton'),
variant === 'poster' ? h('div.card__body', h('div.skeleton.skeleton--line'), h('div.skeleton.skeleton--line.skeleton--short')) : null,
);
}
export function grid(items, render = (it) => posterCard(it)) {
return h('div.grid', { role: 'list' }, items.map(render));
}