site_videos.products (the old product column is kept and copied); one floating video per product checks the overlap and names it. The floating player starts muted, with a sound button that says its state and an expand button to the large player. A vertical video stands upright on the floating and the large player. The home's videos move to its foot as muted looping cards that play only on screen, not at all with reduced motion, and open the large player. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
241 lines
9.9 KiB
JavaScript
241 lines
9.9 KiB
JavaScript
/* Site DTF — the public catalog is filled by videos published in Gestão. */
|
||
const VIDEOS = [
|
||
{id: 'subindo', title: 'Subindo a arte pelo celular',
|
||
desc: 'Do primeiro toque ao preço, sem instalar nada.', product: null,
|
||
src: null, captions: null},
|
||
{id: 'nota', title: 'O que a nota da arte mostra',
|
||
desc: 'A nota explica o arquivo; ela não muda o preço.', product: 'file',
|
||
src: null, captions: null},
|
||
{id: 'png', title: 'Por que PNG com fundo transparente',
|
||
desc: 'O que acontece com JPG e por que o branco sai no filme.', product: 'uvfile',
|
||
src: null, captions: null},
|
||
{id: 'avulsas', title: 'Montando artes avulsas e vendo a folha ao vivo',
|
||
desc: 'A montagem peça por peça, com o encaixe e a metragem.', product: 'avulsa',
|
||
src: null, captions: null},
|
||
{id: 'retirada', title: 'Retirada em Franca em 24 h',
|
||
desc: 'O balcão, o recibo e o que levar.', product: null,
|
||
src: null, captions: null},
|
||
{id: 'sala', title: 'A sala às 3 da manhã',
|
||
desc: 'Por onde o pedido passa até virar filme.', product: null,
|
||
src: null, captions: null},
|
||
];
|
||
|
||
// A video may appear on more than one product page; the old single `product`
|
||
// still stands for one product.
|
||
function videoProducts(video) {
|
||
return video.products && video.products.length ? video.products : [video.product].filter(Boolean);
|
||
}
|
||
|
||
const SOUND_OFF = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 9h3l4-4v14l-4-4H4z"/><path d="M15 9l6 6M21 9l-6 6"/></svg>';
|
||
const SOUND_ON = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 9h3l4-4v14l-4-4H4z"/><path d="M15 8a5.5 5.5 0 0 1 0 8"/><path d="M18 5a9 9 0 0 1 0 14"/></svg>';
|
||
const EXPAND = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5"/></svg>';
|
||
|
||
function videoElement(video, {controls = false} = {}) {
|
||
const el = document.createElement('video');
|
||
el.src = video.src;
|
||
el.muted = true;
|
||
el.playsInline = true;
|
||
el.preload = 'metadata';
|
||
if (controls) el.controls = true;
|
||
if (controls) el.tabIndex = 0;
|
||
if (video.captions) {
|
||
const track = document.createElement('track');
|
||
track.kind = 'captions';
|
||
track.label = 'Português';
|
||
track.srclang = 'pt';
|
||
track.src = video.captions;
|
||
track.default = true;
|
||
el.append(track);
|
||
}
|
||
return el;
|
||
}
|
||
|
||
// The home cards: every published video plays muted, in loop, and opens the
|
||
// large player on click. Only the cards on screen play.
|
||
const reduceMotion = matchMedia('(prefers-reduced-motion: reduce)');
|
||
const cardPlayers = new WeakMap();
|
||
let cardObserver = null;
|
||
function observeCards() {
|
||
const grid = document.getElementById('videosGrid');
|
||
if (!grid) return;
|
||
if (!cardObserver) {
|
||
cardObserver = new IntersectionObserver(entries => {
|
||
for (const entry of entries) {
|
||
const player = cardPlayers.get(entry.target);
|
||
if (!player) continue;
|
||
if (entry.isIntersecting && !reduceMotion.matches) player.play().catch(() => {});
|
||
else player.pause();
|
||
}
|
||
}, {threshold: 0.5});
|
||
}
|
||
cardObserver.disconnect();
|
||
for (const card of grid.querySelectorAll('.videoCard')) cardObserver.observe(card);
|
||
}
|
||
|
||
// The section shows only videos with a published file.
|
||
function videosSection() {
|
||
const grid = document.getElementById('videosGrid');
|
||
if (!grid) return;
|
||
const published=VIDEOS.filter(video=>video.src);
|
||
document.getElementById('videos').hidden=!published.length;
|
||
grid.replaceChildren();
|
||
for (const video of published) {
|
||
const card = document.createElement('button');
|
||
card.type = 'button';
|
||
card.className = 'videoCard';
|
||
card.dataset.video = video.id;
|
||
const player = videoElement(video);
|
||
player.loop = true;
|
||
player.addEventListener('loadedmetadata', () => {
|
||
if (player.videoHeight > player.videoWidth) card.classList.add('vertical');
|
||
});
|
||
// The loop stops at 30 s, like the rest of the catalog.
|
||
player.addEventListener('timeupdate', () => { if (player.currentTime >= 30) player.currentTime = 0; });
|
||
const title = document.createElement('b');
|
||
title.textContent = video.title;
|
||
const desc = document.createElement('span');
|
||
desc.textContent = video.desc;
|
||
card.append(player, title, desc);
|
||
card.addEventListener('click', () => openVideo(video, card));
|
||
cardPlayers.set(card, player);
|
||
grid.append(card);
|
||
}
|
||
observeCards();
|
||
}
|
||
videosSection();
|
||
|
||
// The category player, opened from a card.
|
||
let videoModal = null;
|
||
let videoReturnFocus = null;
|
||
function openVideo(video, trigger=document.activeElement) {
|
||
videoReturnFocus = trigger;
|
||
if (!videoModal) {
|
||
videoModal = document.createElement('div');
|
||
videoModal.className = 'videoModal';
|
||
videoModal.setAttribute('role', 'dialog');
|
||
videoModal.setAttribute('aria-modal', 'true');
|
||
videoModal.setAttribute('aria-label', 'Vídeo: '+video.title);
|
||
videoModal.hidden = true;
|
||
videoModal.innerHTML = '<div class="vmBox"><button type="button" class="vmClose" aria-label="Fechar vídeo">×</button></div>';
|
||
videoModal.addEventListener('click', event => {
|
||
if (event.target === videoModal || event.target.closest('.vmClose')) closeVideo();
|
||
});
|
||
videoModal.addEventListener('keydown', event => {
|
||
if (event.key === 'Escape') { event.preventDefault(); closeVideo(); return; }
|
||
if (event.key !== 'Tab') return;
|
||
const focusable = [...videoModal.querySelectorAll('button,video[controls]')];
|
||
const first=focusable[0], last=focusable[focusable.length-1];
|
||
if (event.shiftKey && document.activeElement === first) { event.preventDefault(); last.focus(); }
|
||
else if (!event.shiftKey && document.activeElement === last) { event.preventDefault(); first.focus(); }
|
||
});
|
||
document.body.append(videoModal);
|
||
}
|
||
const box = videoModal.querySelector('.vmBox');
|
||
box.querySelectorAll('video').forEach(el => el.remove());
|
||
videoModal.classList.remove('vertical');
|
||
const player = videoElement(video, {controls: true});
|
||
player.addEventListener('loadedmetadata', () => {
|
||
if (player.videoHeight > player.videoWidth) videoModal.classList.add('vertical');
|
||
});
|
||
player.addEventListener('timeupdate', () => { if (player.currentTime >= 30) player.pause(); });
|
||
box.append(player);
|
||
videoModal.hidden = false;
|
||
videoModal.setAttribute('aria-label', 'Vídeo: '+video.title);
|
||
videoModal.querySelector('.vmClose').focus();
|
||
player.play().catch(() => {});
|
||
document.documentElement.classList.add('videoAberto');
|
||
}
|
||
function closeVideo() {
|
||
if (!videoModal) return;
|
||
videoModal.querySelectorAll('video').forEach(el => { el.pause(); el.removeAttribute('src'); el.load(); });
|
||
videoModal.hidden = true;
|
||
document.documentElement.classList.remove('videoAberto');
|
||
videoReturnFocus?.focus();
|
||
videoReturnFocus = null;
|
||
}
|
||
|
||
// A floating, muted 30 s video on each product page. It only appears when the
|
||
// product has a recorded video; until then the page stays clean.
|
||
let floating = null, floatingId = null;
|
||
function videoForProduct() {
|
||
if (document.documentElement.dataset.rota !== 'produto') return null;
|
||
return VIDEOS.find(video => video.src && videoProducts(video).includes(modo)) || null;
|
||
}
|
||
function syncFloatingVideo() {
|
||
const video = videoForProduct();
|
||
if (!video) {
|
||
if (floating) { floating.remove(); floating = null; floatingId = null; }
|
||
return;
|
||
}
|
||
if (floating && floatingId === video.id) return;
|
||
if (floating) { floating.remove(); floating = null; }
|
||
floatingId = video.id;
|
||
floating = document.createElement('div');
|
||
floating.className = 'videoFloat';
|
||
const actions = document.createElement('div');
|
||
actions.className = 'vfActions';
|
||
const sound = document.createElement('button');
|
||
sound.type = 'button';
|
||
sound.className = 'vfSound';
|
||
const expand = document.createElement('button');
|
||
expand.type = 'button';
|
||
expand.className = 'vfExpand';
|
||
expand.setAttribute('aria-label', 'Expandir vídeo');
|
||
expand.innerHTML = EXPAND;
|
||
const close = document.createElement('button');
|
||
close.type = 'button';
|
||
close.className = 'vfClose';
|
||
close.setAttribute('aria-label', 'Fechar vídeo');
|
||
close.textContent = '×';
|
||
close.addEventListener('click', () => { floating.remove(); floating = null; floatingId = null; });
|
||
const player = videoElement(video);
|
||
player.autoplay = true;
|
||
player.loop = false;
|
||
// 30 segundos no máximo, como o PDF pede.
|
||
player.addEventListener('timeupdate', () => { if (player.currentTime >= 30) player.pause(); });
|
||
player.addEventListener('loadedmetadata', () => {
|
||
if (player.videoHeight > player.videoWidth) floating.classList.add('vertical');
|
||
});
|
||
// It starts muted; the button turns the sound on and off.
|
||
let muted = true;
|
||
const paintSound = () => {
|
||
sound.setAttribute('aria-pressed', String(!muted));
|
||
sound.setAttribute('aria-label', muted ? 'Ativar som' : 'Desativar som');
|
||
sound.innerHTML = muted ? SOUND_OFF : SOUND_ON;
|
||
};
|
||
paintSound();
|
||
sound.addEventListener('click', () => {
|
||
muted = !muted;
|
||
player.muted = muted;
|
||
paintSound();
|
||
desc.textContent = (muted ? 'Mudo' : 'Com som') + (video.captions ? ' · com legenda' : '') + ' · 30 s';
|
||
});
|
||
expand.addEventListener('click', () => {
|
||
player.pause();
|
||
openVideo(video, expand);
|
||
});
|
||
actions.append(sound, expand, close);
|
||
const label = document.createElement('div');
|
||
label.className = 'vfLabel';
|
||
const title = document.createElement('b');
|
||
title.textContent = video.title;
|
||
const desc = document.createElement('span');
|
||
desc.textContent = video.captions ? 'Mudo · com legenda · 30 s' : 'Mudo · 30 s';
|
||
label.append(title, desc);
|
||
floating.append(player, actions, label);
|
||
document.body.append(floating);
|
||
player.play().catch(() => {});
|
||
}
|
||
|
||
addEventListener('dtf-page-changed', syncFloatingVideo);
|
||
syncFloatingVideo();
|
||
window.siteVideosReady=fetch('/api/site/videos').then(response=>response.ok?response.json():null).then(data=>{
|
||
for(const published of data?.videos||[]){
|
||
const video=VIDEOS.find(item=>item.id===published.id);
|
||
if(video){video.src=published.src;video.captions=published.captions;
|
||
video.product=published.product;video.products=published.products||[];}
|
||
}
|
||
videosSection();
|
||
syncFloatingVideo();
|
||
}).catch(()=>{});
|