mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Vue kit: Bhote Koshi's buttons and media cards are kit controls and a view
The reconstruction panel's buttons (previous and next beat, Play, Play scene, Cinematic, Full story, Open original, Lower gorge, the field reports and Open public map) are ghost UiButtons that keep their data-action hooks. Cinematic is a pressed toggle: the event layer now presents its state as the record's `pressed` instead of writing an aria-pressed attribute, so it is no longer an exception in viewControls.test.mjs; bhote-koshi.css still reads aria-pressed, so it takes no pressed class. The embedded media cards (frame, leader line, title, status, original link) and the hidden YouTube preload are drawn by BhoteKoshiMediaCards, which bhoteKoshiEmbeddedMedia.js renders into its overlay root with h() and render() on every change and on a locale change, in place of the markup it built with createElement and innerHTML. Placement after each frame, provider loading, preloads, timers and teardown are unchanged. The script still builds the SDK script and preconnect tags, the overlay root and the Facebook player node, which is warmed hidden before its card exists and carried loaded into the card, where the SDK replaces it. bhoteKoshiControls.test.mjs pins it; the media tests run on a DOM. Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
1 parent
bcc25be07a
commit
545beaee87
12 files changed
+614
-269
No files matched your search
+173
-179
@@ -6,6 +6,8 @@ import {
|
||||
} from '../render/rendererOf.js';
|
||||
import { dot, sub } from '../geo/vector.js';
|
||||
import { t, msg, onLocaleChange } from '../i18n/index.js';
|
||||
import { h, render as renderVue } from 'vue';
|
||||
import BhoteKoshiMediaCards from '../ui/bhoteKoshi/BhoteKoshiMediaCards.vue';
|
||||
|
||||
const YOUTUBE_HOSTS = new Set([
|
||||
'youtube.com',
|
||||
@@ -98,6 +100,7 @@ export function bhoteEventText(value) {
|
||||
let _xWidgetsPromise = null;
|
||||
let _facebookSdkPromise = null;
|
||||
let _facebookEmbedSequence = 0;
|
||||
let _cardSequence = 0;
|
||||
const youtubeApis = new WeakMap();
|
||||
|
||||
function loadYouTubeApi(documentRef, globalRef) {
|
||||
@@ -682,32 +685,61 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
let record = null;
|
||||
let warmRecord = null;
|
||||
let generation = 0;
|
||||
// Text the cards show, kept as the calls that word it so a locale change
|
||||
// words it again. A node is forgotten once it has left the page.
|
||||
const worded = new Map();
|
||||
function forgetDetached() {
|
||||
for (const node of worded.keys())
|
||||
if (!node.isConnected) worded.delete(node);
|
||||
let destroyed = false;
|
||||
// The cards in the root, in the order they were shown: the current one and
|
||||
// any still leaving. Their text is kept as the calls that word it, so a
|
||||
// locale change words it again.
|
||||
const cards = [];
|
||||
|
||||
/** Render the cards and the YouTube preload into the root, synchronously. */
|
||||
function render() {
|
||||
if (destroyed) return;
|
||||
const warmFrame = warmRecord?.frame;
|
||||
renderVue(
|
||||
h(BhoteKoshiMediaCards, {
|
||||
warm: warmFrame
|
||||
? {
|
||||
key: warmRecord.key,
|
||||
src: warmFrame.src,
|
||||
title: warmFrame.title(),
|
||||
}
|
||||
: null,
|
||||
cards: cards.map((current) => ({
|
||||
id: current.id,
|
||||
provider: current.source.provider,
|
||||
portrait: current.portrait,
|
||||
entering: current.entering,
|
||||
leaving: current.leaving,
|
||||
unavailable: current.unavailable,
|
||||
label: current.label(),
|
||||
title: current.title(),
|
||||
status: current.status(),
|
||||
url: current.source.url,
|
||||
frame: current.frame && {
|
||||
src: current.frame.src,
|
||||
title: current.frame.title(),
|
||||
onLoad: current.frame.onLoad,
|
||||
},
|
||||
bind: current.bind,
|
||||
})),
|
||||
}),
|
||||
root,
|
||||
);
|
||||
}
|
||||
function word(node, property, text) {
|
||||
let properties = worded.get(node);
|
||||
if (!properties) worded.set(node, (properties = new Map()));
|
||||
properties.set(property, text);
|
||||
writeWord(node, property, text());
|
||||
}
|
||||
function writeWord(node, property, value) {
|
||||
if (property === 'textContent' || property === 'title')
|
||||
node[property] = value;
|
||||
else node.setAttribute(property, value);
|
||||
}
|
||||
const offLocale = onLocaleChange(() => {
|
||||
forgetDetached();
|
||||
for (const [node, properties] of worded)
|
||||
for (const [property, text] of properties)
|
||||
writeWord(node, property, text());
|
||||
});
|
||||
const offLocale = onLocaleChange(render);
|
||||
const leavingRecords = new Set();
|
||||
|
||||
/** Word a card's status and draw it. */
|
||||
function setStatus(current, text) {
|
||||
current.status = text;
|
||||
render();
|
||||
}
|
||||
/** Mark a card unavailable or not and draw it. */
|
||||
function setUnavailable(current, unavailable) {
|
||||
current.unavailable = unavailable;
|
||||
render();
|
||||
}
|
||||
|
||||
const prefersReducedMotion = () =>
|
||||
globalRef.matchMedia?.('(prefers-reduced-motion: reduce)')?.matches ===
|
||||
true;
|
||||
@@ -848,9 +880,10 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
if (current.exitTimer != null) globalRef.clearTimeout?.(current.exitTimer);
|
||||
destroyFacebookSession(current.facebookSession);
|
||||
current.youtubeSession?.destroy();
|
||||
current.card?.remove?.();
|
||||
const at = cards.indexOf(current);
|
||||
if (at !== -1) cards.splice(at, 1);
|
||||
leavingRecords.delete(current);
|
||||
forgetDetached();
|
||||
render();
|
||||
}
|
||||
|
||||
const position = () => {
|
||||
@@ -866,7 +899,8 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
current.enterFrame = scheduleFrame(() => {
|
||||
current.enterFrame = null;
|
||||
if (token !== generation || record !== current) return;
|
||||
current.card.classList.add('is-entering');
|
||||
current.entering = true;
|
||||
render();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -888,9 +922,10 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
removeRecord(current);
|
||||
return;
|
||||
}
|
||||
current.card.classList.remove('is-entering');
|
||||
current.card.classList.add('is-leaving');
|
||||
current.entering = false;
|
||||
current.leaving = true;
|
||||
leavingRecords.add(current);
|
||||
render();
|
||||
current.exitTimer =
|
||||
globalRef.setTimeout?.(
|
||||
() => removeRecord(current),
|
||||
@@ -900,11 +935,10 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
|
||||
function clearWarm(key = null) {
|
||||
if (!warmRecord || (key && warmRecord.key !== key)) return;
|
||||
if (warmRecord.facebookSession)
|
||||
destroyFacebookSession(warmRecord.facebookSession);
|
||||
else warmRecord.iframe?.remove?.();
|
||||
const facebookSession = warmRecord.facebookSession;
|
||||
warmRecord = null;
|
||||
forgetDetached();
|
||||
if (facebookSession) destroyFacebookSession(facebookSession);
|
||||
else render();
|
||||
}
|
||||
|
||||
function takeWarmFacebook(key, host) {
|
||||
@@ -944,29 +978,26 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
warmRecord = { key, facebookSession };
|
||||
return true;
|
||||
}
|
||||
const iframe = documentRef.createElement('iframe');
|
||||
iframe.className = 'bhote-embedded-media-warmup';
|
||||
iframe.src = embeddedMediaFrameUrl(source, {
|
||||
autoplay: false,
|
||||
startAtSec,
|
||||
endAtSec,
|
||||
});
|
||||
word(iframe, 'title', () =>
|
||||
t('Preloading {provider} media', {
|
||||
provider: providerLabel(source.provider),
|
||||
}),
|
||||
);
|
||||
iframe.tabIndex = -1;
|
||||
iframe.setAttribute('aria-hidden', 'true');
|
||||
iframe.loading = 'eager';
|
||||
iframe.referrerPolicy = 'strict-origin-when-cross-origin';
|
||||
root.append(iframe);
|
||||
warmRecord = { key, iframe };
|
||||
warmRecord = {
|
||||
key,
|
||||
frame: {
|
||||
src: embeddedMediaFrameUrl(source, {
|
||||
autoplay: false,
|
||||
startAtSec,
|
||||
endAtSec,
|
||||
}),
|
||||
title: () =>
|
||||
t('Preloading {provider} media', {
|
||||
provider: providerLabel(source.provider),
|
||||
}),
|
||||
},
|
||||
};
|
||||
render();
|
||||
return true;
|
||||
}
|
||||
|
||||
async function mountX(current, token) {
|
||||
word(current.status, 'textContent', () => t('LOADING X POST…'));
|
||||
setStatus(current, () => t('LOADING X POST…'));
|
||||
try {
|
||||
const api = await xLoader(documentRef, globalRef);
|
||||
if (token !== generation || record !== current) return;
|
||||
@@ -986,14 +1017,12 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
throw new Error(
|
||||
msg('The X post is unavailable or cannot be embedded.'),
|
||||
);
|
||||
word(current.status, 'textContent', () =>
|
||||
t('X POST · PROVIDER CONTROLS'),
|
||||
);
|
||||
setStatus(current, () => t('X POST · PROVIDER CONTROLS'));
|
||||
position();
|
||||
} catch (error) {
|
||||
if (token !== generation || record !== current) return;
|
||||
current.card.classList.add('is-unavailable');
|
||||
word(current.status, 'textContent', () =>
|
||||
current.unavailable = true;
|
||||
setStatus(current, () =>
|
||||
t('{reason} OPEN ORIGINAL.', {
|
||||
reason: t(String(error?.message || msg('X embed unavailable'))),
|
||||
}),
|
||||
@@ -1010,16 +1039,14 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
session.shouldPlay = autoplay === true;
|
||||
session.onReady = () => {
|
||||
if (token !== generation || record !== current) return;
|
||||
word(current.status, 'textContent', () =>
|
||||
t('FACEBOOK · PROVIDER CONTROLS'),
|
||||
);
|
||||
current.card.classList.remove('is-unavailable');
|
||||
current.status = () => t('FACEBOOK · PROVIDER CONTROLS');
|
||||
setUnavailable(current, false);
|
||||
position();
|
||||
};
|
||||
session.onError = (error) => {
|
||||
if (token !== generation || record !== current) return;
|
||||
current.card.classList.add('is-unavailable');
|
||||
word(current.status, 'textContent', () =>
|
||||
current.unavailable = true;
|
||||
setStatus(current, () =>
|
||||
t('{reason} OPEN ORIGINAL.', {
|
||||
reason: t(
|
||||
String(error?.message || msg('Facebook embed unavailable')),
|
||||
@@ -1030,9 +1057,7 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
};
|
||||
session.onPlaybackBlocked = () => {
|
||||
if (token !== generation || record !== current) return;
|
||||
word(current.status, 'textContent', () =>
|
||||
t('AUTOPLAY BLOCKED · USE PROVIDER CONTROLS'),
|
||||
);
|
||||
setStatus(current, () => t('AUTOPLAY BLOCKED · USE PROVIDER CONTROLS'));
|
||||
};
|
||||
if (session.ready) session.onReady();
|
||||
else if (session.error) session.onError(session.error);
|
||||
@@ -1078,104 +1103,69 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
if (warmRecord && warmRecord.key !== warmKey) clearWarm();
|
||||
hide({ preserveWarm: true });
|
||||
const token = ++generation;
|
||||
const card = documentRef.createElement('section');
|
||||
const portraitClass = isPortraitMedia(media) ? ' is-portrait' : '';
|
||||
card.className = `bhote-embedded-callout is-${source.provider}${portraitClass}`;
|
||||
card.dataset.provider = source.provider;
|
||||
word(card, 'aria-label', () =>
|
||||
t('Embedded {provider} media for {source}', {
|
||||
provider: providerLabel(source.provider),
|
||||
source: observation?.title
|
||||
? bhoteEventText(observation.title)
|
||||
: t('witness source'),
|
||||
}),
|
||||
);
|
||||
const leader = documentRef.createElementNS(
|
||||
'http://www.w3.org/2000/svg',
|
||||
'svg',
|
||||
);
|
||||
leader.classList.add('bhote-embedded-callout-leader');
|
||||
leader.setAttribute('aria-hidden', 'true');
|
||||
leader.innerHTML =
|
||||
'<path pathLength="1" fill="none" stroke="rgba(147,213,228,.72)" stroke-width="1.5"/><circle r="4" fill="#6be8ff" stroke="#ffffff" stroke-width="1.5"/>';
|
||||
const player = documentRef.createElement('div');
|
||||
player.className = 'bhote-embedded-callout-player ui-scroll';
|
||||
const footer = documentRef.createElement('footer');
|
||||
const title = documentRef.createElement('strong');
|
||||
word(title, 'textContent', () =>
|
||||
bhoteEventText(
|
||||
observation?.shortTitle || observation?.title || t('Witness source'),
|
||||
).toUpperCase(),
|
||||
);
|
||||
const status = documentRef.createElement('small');
|
||||
status.setAttribute('role', 'status');
|
||||
word(status, 'textContent', () =>
|
||||
t('{provider} · EMBEDDED SOURCE', {
|
||||
provider: providerLabel(source.provider),
|
||||
}),
|
||||
);
|
||||
const link = documentRef.createElement('a');
|
||||
link.href = source.url;
|
||||
link.target = '_blank';
|
||||
link.rel = 'noopener noreferrer';
|
||||
word(link, 'textContent', () => t('OPEN ORIGINAL ↗'));
|
||||
footer.append(title, status, link);
|
||||
const body = documentRef.createElement('div');
|
||||
body.className = 'bhote-embedded-callout-body';
|
||||
// The card takes taps (its container and leader do not); Stories keeps that.
|
||||
body.setAttribute('data-overlay-input', '');
|
||||
body.append(player, footer);
|
||||
card.append(leader, body);
|
||||
root.append(card);
|
||||
record = {
|
||||
const providerName = () => providerLabel(source.provider);
|
||||
const sourceName = () =>
|
||||
observation?.title
|
||||
? bhoteEventText(observation.title)
|
||||
: t('witness source');
|
||||
const current = {
|
||||
id: ++_cardSequence,
|
||||
key,
|
||||
source,
|
||||
anchor,
|
||||
card,
|
||||
leader,
|
||||
player,
|
||||
status,
|
||||
portrait: isPortraitMedia(media),
|
||||
entering: false,
|
||||
leaving: false,
|
||||
unavailable: false,
|
||||
label: () =>
|
||||
t('Embedded {provider} media for {source}', {
|
||||
provider: providerName(),
|
||||
source: sourceName(),
|
||||
}),
|
||||
title: () =>
|
||||
bhoteEventText(
|
||||
observation?.shortTitle || observation?.title || t('Witness source'),
|
||||
).toUpperCase(),
|
||||
status: () =>
|
||||
t('{provider} · EMBEDDED SOURCE', { provider: providerName() }),
|
||||
frame: null,
|
||||
frameElement: null,
|
||||
card: null,
|
||||
leader: null,
|
||||
player: null,
|
||||
entered: false,
|
||||
};
|
||||
|
||||
if (source.provider === 'x') {
|
||||
void mountX(record, token);
|
||||
} else if (source.provider === 'facebook') {
|
||||
bindFacebook(record, token, warmKey, autoplay);
|
||||
} else {
|
||||
const iframe = documentRef.createElement('iframe');
|
||||
iframe.src = embeddedMediaFrameUrl(source, {
|
||||
autoplay,
|
||||
startAtSec,
|
||||
endAtSec,
|
||||
});
|
||||
const controlled =
|
||||
// The elements the card's view draws: placed after each frame, filled by
|
||||
// the provider.
|
||||
current.bind = (name, element) => {
|
||||
if (element) current[name] = element;
|
||||
};
|
||||
let controlled = false;
|
||||
if (source.provider === 'youtube') {
|
||||
const url = new URL(
|
||||
embeddedMediaFrameUrl(source, {
|
||||
autoplay,
|
||||
startAtSec,
|
||||
endAtSec,
|
||||
}),
|
||||
);
|
||||
controlled =
|
||||
autoplay &&
|
||||
Number.isFinite(Number(endAtSec)) &&
|
||||
Number(endAtSec) > startAtSec;
|
||||
if (controlled) {
|
||||
const url = new URL(iframe.src);
|
||||
url.searchParams.set('enablejsapi', '1');
|
||||
if (globalRef.location?.origin)
|
||||
url.searchParams.set('origin', globalRef.location.origin);
|
||||
iframe.src = url.href;
|
||||
}
|
||||
word(iframe, 'title', () =>
|
||||
t('{provider} media: {source}', {
|
||||
provider: providerLabel(source.provider),
|
||||
source: observation?.title
|
||||
? bhoteEventText(observation.title)
|
||||
: t('witness source'),
|
||||
}),
|
||||
);
|
||||
iframe.allow =
|
||||
'autoplay; encrypted-media; picture-in-picture; fullscreen';
|
||||
iframe.allowFullscreen = true;
|
||||
iframe.loading = 'eager';
|
||||
iframe.referrerPolicy = 'strict-origin-when-cross-origin';
|
||||
iframe.addEventListener?.(
|
||||
'load',
|
||||
() => {
|
||||
current.frame = {
|
||||
src: url.href,
|
||||
title: () =>
|
||||
t('{provider} media: {source}', {
|
||||
provider: providerName(),
|
||||
source: sourceName(),
|
||||
}),
|
||||
onLoad: () => {
|
||||
if (token !== generation || record?.key !== key) return;
|
||||
clearWarm(warmKey);
|
||||
if (
|
||||
@@ -1184,38 +1174,41 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
record.youtubeSession.getState().phase,
|
||||
)
|
||||
) {
|
||||
word(status, 'textContent', () =>
|
||||
t('{provider} · PROVIDER CONTROLS', {
|
||||
provider: providerLabel(source.provider),
|
||||
}),
|
||||
setStatus(current, () =>
|
||||
t('{provider} · PROVIDER CONTROLS', { provider: providerName() }),
|
||||
);
|
||||
}
|
||||
position();
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
player.append(iframe);
|
||||
if (controlled) {
|
||||
const current = record;
|
||||
current.youtubeSession = createTrimmedYouTubePlayback({
|
||||
iframe,
|
||||
endAtSec: Number(endAtSec),
|
||||
startAtSec,
|
||||
globalRef,
|
||||
loadApi: () => youtubeLoader(documentRef, globalRef),
|
||||
isCurrent: () => token === generation && record === current,
|
||||
onStatus: (phase) => {
|
||||
if (phase !== 'completed')
|
||||
word(status, 'textContent', () =>
|
||||
t('YOUTUBE {state} · OPEN ORIGINAL', {
|
||||
state: YOUTUBE_PHASE_LABELS[phase]
|
||||
? t(YOUTUBE_PHASE_LABELS[phase])
|
||||
: phase.toUpperCase(),
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
record = current;
|
||||
cards.push(current);
|
||||
render();
|
||||
|
||||
if (source.provider === 'x') {
|
||||
void mountX(current, token);
|
||||
} else if (source.provider === 'facebook') {
|
||||
bindFacebook(current, token, warmKey, autoplay);
|
||||
} else if (controlled) {
|
||||
current.youtubeSession = createTrimmedYouTubePlayback({
|
||||
iframe: current.frameElement,
|
||||
endAtSec: Number(endAtSec),
|
||||
startAtSec,
|
||||
globalRef,
|
||||
loadApi: () => youtubeLoader(documentRef, globalRef),
|
||||
isCurrent: () => token === generation && record === current,
|
||||
onStatus: (phase) => {
|
||||
if (phase !== 'completed')
|
||||
setStatus(current, () =>
|
||||
t('YOUTUBE {state} · OPEN ORIGINAL', {
|
||||
state: YOUTUBE_PHASE_LABELS[phase]
|
||||
? t(YOUTUBE_PHASE_LABELS[phase])
|
||||
: phase.toUpperCase(),
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
position();
|
||||
return true;
|
||||
@@ -1223,11 +1216,12 @@ export function createBhoteKoshiEmbeddedMedia({
|
||||
|
||||
function destroy() {
|
||||
offLocale();
|
||||
worded.clear();
|
||||
hide({ immediate: true });
|
||||
for (const current of [...leavingRecords]) removeRecord(current);
|
||||
clearWarm();
|
||||
removePostRender?.();
|
||||
destroyed = true;
|
||||
renderVue(null, root);
|
||||
root.remove();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user