31 lines
1.3 KiB
JavaScript
31 lines
1.3 KiB
JavaScript
/*
|
|
cast.js — "Top Cast" carousel: round avatars, name + character.
|
|
castRow(credits, { limit = 15 }) → row section (or null when there is no cast).
|
|
*/
|
|
import { h } from '../util.js';
|
|
import { img } from '../tmdb.js';
|
|
import { row } from './row.js';
|
|
import { icon } from './icons.js';
|
|
|
|
function initials(name) {
|
|
return String(name || '?').split(/\s+/).slice(0, 2).map((p) => p[0] || '').join('').toUpperCase();
|
|
}
|
|
|
|
export function castCard(p) {
|
|
const avatar = p.profile_path
|
|
? h('img.cast__img', { src: img(p.profile_path, 'w185'), alt: '', loading: 'lazy', decoding: 'async', onerror: (e) => e.target.replaceWith(h('span.cast__initials', initials(p.name))) })
|
|
: h('span.cast__initials', initials(p.name) || icon('user'));
|
|
return h('div.card.card--cast', { role: 'listitem' },
|
|
h('div.cast', { tabindex: '0', 'data-focus': '', 'aria-label': `${p.name}${p.character ? ` as ${p.character}` : ''}` },
|
|
h('div.cast__avatar', avatar),
|
|
h('div.cast__text',
|
|
h('div.cast__name', p.name),
|
|
p.character ? h('div.cast__role', p.character) : null)));
|
|
}
|
|
|
|
export function castRow(credits, opts = {}) {
|
|
const cast = ((credits && credits.cast) || []).slice(0, opts.limit || 15);
|
|
if (!cast.length) return null;
|
|
return row({ title: opts.title || 'Top Cast', variant: 'cast', items: cast, render: castCard, lazy: false });
|
|
}
|