Files
WT-Offline/packages/local-service/src/store/web/gallery.js
T
N0VAN0M4LY b260d2899e chore(battle): snapshot the LocalMP rework baseline
Records the uncommitted battle-session work exactly as it was left
(local service matchmaking, settlement, chat, native LocalMP hooks,
battle gui/exe patches) so the following review and rework commits
show every correction as a diff against this state.

This commit is not reviewed and is known to be incomplete: in-game
testing reported wrong settlement rewards, single-country teams,
missing random/fixed supplement slots, misattributed hit messages and
other regressions. Do not merge this branch before the rework lands.
2026-11-28 03:15:47 +08:00

231 lines
7.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Images and demo clips for pack details.
'use strict';
const galleryPlayers = new WeakMap();
const CLIP_TIMEOUT_MS = 23_000;
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 createClipVideoElement(poster, autoplay) {
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;
return video;
}
function playStoreClip(stage, sources, poster, autoplay, onReady, onUnavailable) {
galleryPlayers.get(stage)?.();
const video = createClipVideoElement(poster, autoplay);
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, CLIP_TIMEOUT_MS);
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 candidates = storeImageCandidates(item.screenshot);
const image = makeElement('img', 'gallery-screenshot');
image.alt = eager ? item.label : '';
image.loading = eager ? 'eager' : 'lazy';
image.crossOrigin = 'anonymous';
image.referrerPolicy = 'no-referrer';
let index = 0;
function tryNext() {
const nextUrl = candidates[index++];
if (!nextUrl) {
image.onload = image.onerror = null;
image.remove();
loaded(false);
return;
}
image.src = nextUrl;
}
image.onload = () => {
image.onload = image.onerror = null;
image.classList.add('ready');
loaded(true);
};
image.onerror = tryNext;
container.append(image);
tryNext();
return image;
}
function renderStageScreenshot(stage, status, product, 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 renderStageMedia(stage, status, product, item, showPlay, onPlay) {
stage.replaceChildren();
if (item.screenshot) {
renderStageScreenshot(stage, status, product, 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 = () => onPlay(item, false);
stage.append(play);
}
function startStageVideo(stage, status, product, item, autoplay) {
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; }, () => {
renderStageMedia(stage, status, product, item, true, (target, auto) => startStageVideo(stage, status, product, target, auto));
status.textContent = t('videoUnavailable');
status.hidden = false;
});
}
function createGalleryThumb(item, index, onSelect) {
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 = () => onSelect(index, true);
return button;
}
function bindGalleryControls(nav, rail, prev, next, buttons, itemsLength, getActive, onSelect) {
prev.setAttribute('aria-label', t('previousMedia'));
next.setAttribute('aria-label', t('nextMedia'));
prev.onclick = () => onSelect(getActive() - 1);
next.onclick = () => onSelect(getActive() + 1);
rail.append(...buttons);
rail.onkeydown = (event) => {
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
event.preventDefault();
onSelect(getActive() + (event.key === 'ArrowRight' ? 1 : -1));
buttons[getActive()].focus({ preventScroll: true });
};
nav.hidden = itemsLength === 1;
}
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;
const playItem = (item, auto = false) => startStageVideo(stage, status, product, item, auto);
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];
buttons[index].setAttribute('aria-pressed', 'true');
if (scroll) buttons[index].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;
renderStageMedia(stage, status, product, item, !!item.sources, playItem);
if (play && item.sources) playItem(item, false);
}
const buttons = items.map((item, index) => createGalleryThumb(item, index, select));
bindGalleryControls(nav, rail, prev, next, buttons, items.length, () => active, select);
caption.hidden = items.length === 1;
select(0, false, false);
if (autoplay && items[0].sources) playItem(items[0], true);
}