Cache allowlisted official media so the in-game browser can load it locally. Match the official catalog snapshot and start pack demos muted and looping on entry.
205 lines
6.8 KiB
JavaScript
205 lines
6.8 KiB
JavaScript
// Images and demo clips for pack details.
|
||
'use strict';
|
||
|
||
const galleryPlayers = new WeakMap();
|
||
|
||
function stopStoreGallery(root) {
|
||
const stage = root.querySelector('.media-stage');
|
||
galleryPlayers.get(stage)?.();
|
||
}
|
||
|
||
function galleryItems(product) {
|
||
const { screenshots, videos } = WtoUi.storeMedia(product.id);
|
||
const cover = () => productImage(product);
|
||
const demoStill = screenshots[0] ? () => productScreenshot(product, screenshots[0]) : cover;
|
||
const entries = videos.map((sources, index) => ({
|
||
label: videos.length === 1 ? t('video') : t('videoNumber', { index: index + 1 }),
|
||
image: demoStill,
|
||
sources,
|
||
}));
|
||
screenshots.forEach((url, index) => {
|
||
entries.push({ label: t('screenshot', { index: index + 1 }), screenshot: url });
|
||
});
|
||
entries.push({ label: t('cover'), image: cover });
|
||
if (product.vehicles && (videos.length || screenshots.length || product.vehicles.length > 1)) {
|
||
for (const vehicle of product.vehicles) {
|
||
entries.push({ label: WtoUi.text(vehicle.title), image: () => WtoUi.vehicleImg(vehicle.unitName, '') });
|
||
}
|
||
}
|
||
return entries;
|
||
}
|
||
|
||
function playStoreClip(stage, sources, poster, autoplay, onReady, onUnavailable) {
|
||
galleryPlayers.get(stage)?.();
|
||
const video = document.createElement('video');
|
||
video.controls = true;
|
||
video.playsInline = true;
|
||
if (autoplay) {
|
||
video.autoplay = true;
|
||
video.defaultMuted = true;
|
||
video.muted = true;
|
||
video.loop = true;
|
||
}
|
||
if (poster) video.poster = poster;
|
||
const playable = sources.filter((url) => video.canPlayType(/\.webm(?:$|\?)/i.test(url) ? 'video/webm' : 'video/mp4'));
|
||
let nextSource = 0;
|
||
let timer;
|
||
|
||
function dispose() {
|
||
clearTimeout(timer);
|
||
galleryPlayers.delete(stage);
|
||
video.onerror = video.oncanplay = null;
|
||
video.pause();
|
||
video.removeAttribute('src');
|
||
video.load();
|
||
video.remove();
|
||
}
|
||
|
||
function attempt() {
|
||
clearTimeout(timer);
|
||
const url = playable[nextSource++];
|
||
if (!url) {
|
||
dispose();
|
||
onUnavailable();
|
||
return;
|
||
}
|
||
video.src = url;
|
||
timer = setTimeout(attempt, 23_000);
|
||
void video.play().catch(() => {});
|
||
}
|
||
|
||
video.onerror = attempt;
|
||
video.oncanplay = () => {
|
||
clearTimeout(timer);
|
||
onReady();
|
||
};
|
||
stage.replaceChildren(video);
|
||
galleryPlayers.set(stage, dispose);
|
||
attempt();
|
||
}
|
||
|
||
function appendScreenshot(container, item, loaded, eager = false) {
|
||
const image = makeElement('img', 'gallery-screenshot');
|
||
image.alt = eager ? item.label : '';
|
||
image.loading = eager ? 'eager' : 'lazy';
|
||
image.onload = () => {
|
||
image.classList.add('ready');
|
||
loaded(true);
|
||
};
|
||
image.onerror = () => loaded(false);
|
||
image.src = item.screenshot;
|
||
container.append(image);
|
||
return image;
|
||
}
|
||
|
||
function mountStoreGallery(root, product, autoplay) {
|
||
const items = galleryItems(product);
|
||
root.innerHTML = '<div class="media-stage"></div><div class="media-navigation"><button type="button">‹</button>'
|
||
+ '<div class="media-thumbs"></div><button type="button">›</button></div>'
|
||
+ '<div class="media-caption"><span class="media-label"></span><span class="media-position"></span></div>'
|
||
+ '<p class="media-status" role="status" hidden></p>';
|
||
root.setAttribute('aria-label', t('gallery'));
|
||
const stage = root.querySelector('.media-stage');
|
||
const nav = root.querySelector('.media-navigation');
|
||
const rail = root.querySelector('.media-thumbs');
|
||
const [prev, next] = nav.querySelectorAll('button');
|
||
const caption = root.querySelector('.media-caption');
|
||
const status = root.querySelector('.media-status');
|
||
let active = 0;
|
||
|
||
function showImage(item, showPlay) {
|
||
stage.replaceChildren();
|
||
if (item.screenshot) {
|
||
showScreenshot(item);
|
||
return;
|
||
}
|
||
const image = item.image();
|
||
image.loading = 'eager';
|
||
image.alt = item.label;
|
||
stage.append(image);
|
||
if (!showPlay) return;
|
||
const play = storeButton('media-play', '▶ ' + t('playVideo'));
|
||
play.onclick = () => playItem(item);
|
||
stage.append(play);
|
||
}
|
||
|
||
function showScreenshot(item) {
|
||
const gameArt = WtoUi.tieredImg([], productArtwork(product), '');
|
||
gameArt.loading = 'eager';
|
||
stage.append(gameArt);
|
||
status.textContent = t('screenshotLoading');
|
||
status.hidden = false;
|
||
const image = appendScreenshot(stage, item, (ok) => {
|
||
if (image.parentNode !== stage) return;
|
||
if (ok) {
|
||
gameArt.remove();
|
||
status.hidden = true;
|
||
} else {
|
||
status.textContent = t('screenshotUnavailable');
|
||
}
|
||
}, true);
|
||
}
|
||
|
||
function playItem(item, autoplay = false) {
|
||
const image = stage.querySelector('img');
|
||
const poster = image?.currentSrc || image?.getAttribute('src');
|
||
status.textContent = t('videoLoading');
|
||
status.hidden = false;
|
||
playStoreClip(stage, item.sources, poster, autoplay, () => { status.hidden = true; }, () => {
|
||
showImage(item, true);
|
||
status.textContent = t('videoUnavailable');
|
||
status.hidden = false;
|
||
});
|
||
}
|
||
|
||
function select(index, play = false, scroll = true) {
|
||
if (index < 0 || index >= items.length) return;
|
||
stopStoreGallery(root);
|
||
buttons[active].setAttribute('aria-pressed', 'false');
|
||
active = index;
|
||
const item = items[index];
|
||
const button = buttons[index];
|
||
button.setAttribute('aria-pressed', 'true');
|
||
if (scroll) button.scrollIntoView({ block: 'nearest', inline: 'nearest' });
|
||
prev.disabled = index === 0;
|
||
next.disabled = index === items.length - 1;
|
||
caption.querySelector('.media-label').textContent = item.label;
|
||
caption.querySelector('.media-position').textContent = (index + 1) + ' / ' + items.length;
|
||
status.hidden = true;
|
||
showImage(item, !!item.sources);
|
||
if (play && item.sources) playItem(item);
|
||
}
|
||
|
||
const buttons = items.map((item, index) => {
|
||
const button = storeButton('media-thumb');
|
||
button.setAttribute('aria-label', item.label);
|
||
button.setAttribute('aria-pressed', 'false');
|
||
button.title = item.label;
|
||
if (item.screenshot) {
|
||
const label = makeElement('span', 'media-placeholder', item.label);
|
||
button.append(label);
|
||
appendScreenshot(button, item, (ok) => { if (ok) label.remove(); });
|
||
} else {
|
||
button.append(item.image());
|
||
}
|
||
if (item.sources) button.append(makeElement('span', 'thumb-play', '▶'));
|
||
button.onclick = () => select(index, true);
|
||
return button;
|
||
});
|
||
prev.setAttribute('aria-label', t('previousMedia'));
|
||
next.setAttribute('aria-label', t('nextMedia'));
|
||
prev.onclick = () => select(active - 1);
|
||
next.onclick = () => select(active + 1);
|
||
rail.append(...buttons);
|
||
rail.onkeydown = (event) => {
|
||
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
|
||
event.preventDefault();
|
||
select(active + (event.key === 'ArrowRight' ? 1 : -1));
|
||
buttons[active].focus({ preventScroll: true });
|
||
};
|
||
nav.hidden = caption.hidden = items.length === 1;
|
||
select(0, false, false);
|
||
const initial = items[0];
|
||
if (autoplay && initial.sources) playItem(initial, true);
|
||
}
|