File size: 11,507 Bytes
9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 9e1507e e6e0e94 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | // Native reading interface for the authored inspection passage. Facts come only
// from validated published records; the scene is accompaniment, not a gate.
export function createFigureReader({ world, motionState, onReadIndex, onMotion, onSelect, sceneState = () => ({ theme: 'sea' }), onTheme = () => {} }) {
const byId = id => document.getElementById(id);
const dialog = byId('figure-passage');
let item = null;
let stageIndex = 0;
let invoker = null;
let returning = false;
let returnedAction = null;
let textureReady = false;
let graphicsUnavailable = false;
const visited = new Map();
const stageUrl = stage => new URL(stage.src, document.baseURI).href;
const instruments = { sea: 'Sea / optical telescope', orbital: 'Space / scanning gantry', woodland: 'Nature / specimen cabinet' };
const collectionNames = { work: 'projects', research: 'research', journal: 'journal entries', journey: 'journey chapters', news: 'news' };
function syncTheme() {
const theme = sceneState().theme;
byId('passage-instrument-label').textContent = instruments[theme] || instruments.sea;
for (const button of byId('passage-worlds').querySelectorAll('button')) {
button.setAttribute('aria-pressed', String(button.dataset.inspectionTheme === theme));
button.disabled = returning;
}
}
function finishReturn() {
if (!item) return;
const entryButton = invoker?.dataset.entry ? document.querySelector(`#entries button[data-entry="${invoker.dataset.entry}"]`) : null;
const landmark = invoker?.dataset.destination ? document.querySelector(`.world-label[data-destination="${invoker.dataset.destination}"]`) : null;
const target = invoker?.isConnected && invoker.getClientRects().length ? invoker : entryButton || (landmark?.getClientRects().length ? landmark : null) || byId('inspect-figure');
const action = returnedAction;
item = null;
returning = false;
returnedAction = null;
invoker = null;
document.body.dataset.inspection = 'false';
if (dialog.open) dialog.close();
if (action) action();
else target.focus({ preventScroll: true });
}
function syncMotion() {
const { paused, reduced, ready: sceneReady } = motionState();
const ready = sceneReady && !graphicsUnavailable;
const button = byId('passage-motion');
button.hidden = !ready;
button.disabled = reduced;
button.textContent = reduced ? 'Reduced motion' : paused ? 'Resume motion' : 'Pause motion';
button.setAttribute('aria-pressed', String(paused || reduced));
dialog.dataset.motion = paused || reduced ? 'static' : 'moving';
byId('passage-fallback').hidden = ready && textureReady;
}
function renderRecord() {
byId('passage-title').textContent = item.title;
byId('passage-origin').textContent = `${item.status === 'example' ? 'Illustrative demo · ' : ''}${item.kind}${item.status && item.status !== 'example' ? ` / ${item.status}` : ''}`;
byId('passage-summary').textContent = item.summary;
byId('passage-body').textContent = item.body;
byId('passage-caption').textContent = item.figure.caption;
const links = item.links.map(link => {
const anchor = document.createElement('a');
anchor.textContent = `${link.label} ↗`;
anchor.href = link.url;
anchor.target = '_blank'; anchor.rel = 'noopener noreferrer';
return anchor;
});
byId('passage-links').replaceChildren(...links);
byId('passage-index').textContent = `Browse all ${collectionNames[item.collection] || item.collection} ↗`;
byId('passage-stages').replaceChildren(...item.figure.stages.map((stage, index) => {
const button = document.createElement('button');
button.type = 'button';
button.dataset.stage = String(index);
button.setAttribute('aria-label', `Inspect view ${index + 1}: ${stage.label}`);
const number = document.createElement('span');
number.className = 'passage-view-number';
number.textContent = String(index + 1).padStart(2, '0');
button.append(number, document.createTextNode(stage.label));
button.addEventListener('click', () => selectStage(index));
return button;
}));
}
function renderStage() {
const stage = item.figure.stages[stageIndex];
const seen = visited.get(item.id);
byId('passage-stage-number').textContent = `${String(stageIndex + 1).padStart(2, '0')} / ${String(item.figure.stages.length).padStart(2, '0')}`;
byId('passage-stage-label').textContent = stage.label;
byId('passage-stage-description').textContent = stage.description;
byId('passage-reader-stage').textContent = `${stage.label}: ${stage.description}`;
byId('passage-fallback').src = stageUrl(stage);
byId('passage-fallback').alt = `${item.figure.alt} — ${stage.label}`;
byId('passage-flat-image').src = stageUrl(stage);
byId('passage-flat-image').alt = `${item.figure.alt} — ${stage.label}`;
byId('passage-source').href = stageUrl(stage);
byId('passage-progress').textContent = seen.size === item.figure.stages.length ? 'Every view explored. Return whenever you like.' : 'Explore the supplied views';
for (const button of byId('passage-stages').querySelectorAll('button')) {
const index = Number(button.dataset.stage);
button.setAttribute('aria-pressed', String(index === stageIndex));
button.classList.toggle('is-visited', seen.has(item.figure.stages[index].id));
}
byId('passage-status').textContent = `View ${stageIndex + 1} of ${item.figure.stages.length}: ${stage.label}.`;
}
function selectStage(index, { repaint = true } = {}) {
if (!item || returning || !Number.isInteger(index) || index < 0 || index >= item.figure.stages.length) return;
stageIndex = index;
textureReady = false;
if (!visited.has(item.id)) visited.set(item.id, new Set());
visited.get(item.id).add(item.figure.stages[index].id);
renderStage();
if (repaint) world.setInspectionStage({ imageUrl: stageUrl(item.figure.stages[index]), stage: index });
}
function open(record, source = document.activeElement) {
if (!record?.published || !record.figure || returning || typeof dialog.showModal !== 'function') return false;
item = record;
textureReady = false;
graphicsUnavailable = !motionState().ready;
invoker = source;
returning = false;
returnedAction = null;
byId('passage-flat-image').closest('details').open = false;
renderRecord();
syncTheme();
dialog.querySelector('.passage-reader').scrollTop = 0;
selectStage(0, { repaint: false });
onSelect(record.collection);
syncMotion();
// Preserve the inline camera snapshot before ResizeObserver handles the
// fullscreen frame; no await is allowed between these two operations.
document.body.dataset.inspection = 'true';
world.setInspection({ active: true, imageUrl: stageUrl(record.figure.stages[0]), stage: 0, collection: record.collection });
if (!dialog.open) dialog.showModal();
byId('passage-title').focus({ preventScroll: true });
// Reset after modal layout and focus as well: changing to the phone's
// stacked layout can otherwise preserve a previous scroll anchor.
dialog.querySelector('.passage-reader').scrollTop = 0;
return true;
}
function close(afterReturn = null) {
if (!item) { afterReturn?.(); return; }
if (afterReturn) returnedAction = afterReturn;
if (dialog.open) { dialog.close(); return; }
if (returning) return;
returning = true;
syncTheme();
world.setInspection({ active: false });
if (!motionState().ready || graphicsUnavailable) finishReturn();
}
dialog.addEventListener('close', () => close());
byId('passage-close').addEventListener('click', () => close());
byId('passage-index').addEventListener('click', () => {
const collection = item?.collection;
close(() => onReadIndex(collection));
});
byId('passage-motion').addEventListener('click', () => { onMotion(); syncMotion(); });
for (const button of byId('passage-worlds').querySelectorAll('button')) {
button.addEventListener('click', () => {
if (!item || returning) return;
onTheme(button.dataset.inspectionTheme);
syncTheme();
byId('passage-status').textContent = `${instruments[sceneState().theme]}. Same entry, view ${stageIndex + 1} of ${item.figure.stages.length}.`;
});
}
dialog.addEventListener('keydown', event => {
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || !item || returning) return;
if (['INPUT', 'TEXTAREA', 'SELECT'].includes(event.target.tagName?.toUpperCase()) || event.target.isContentEditable) return;
const button = event.target.closest('button[data-stage]');
if (!['ArrowRight', 'ArrowLeft', 'Home', 'End'].includes(event.key)) return;
if (!button && ['Home', 'End'].includes(event.key)) return;
event.preventDefault();
const current = button ? Number(button.dataset.stage) : stageIndex;
const next = event.key === 'Home' ? 0 : event.key === 'End' ? item.figure.stages.length - 1
: (current + (event.key === 'ArrowRight' ? 1 : -1) + item.figure.stages.length) % item.figure.stages.length;
selectStage(next);
byId('passage-stages').querySelector(`[data-stage="${next}"]`).focus({ preventScroll: true });
});
for (const id of ['passage-flat-image', 'passage-fallback']) {
byId(id).addEventListener('error', () => {
if (item) byId('passage-status').textContent = 'The figure image could not load. The entry and caption are still available; check the figure source.';
});
}
return {
open, close, syncMotion, syncTheme,
get active() { return Boolean(item); },
onSceneState({ phase }) {
dialog.dataset.phase = phase;
if (phase === 'unavailable') {
graphicsUnavailable = true;
textureReady = false;
syncMotion();
byId('passage-status').textContent = '3D is unavailable. The figure and entry remain open in the flat reading view.';
}
if (phase === 'closed') finishReturn();
},
onSceneImage({ stage, ready, error }) {
if (!item || returning || stage !== stageIndex) return;
textureReady = ready;
syncMotion();
byId('passage-status').textContent = error
? 'The 3D figure could not load. Use the flat figure and readable entry; check the source image.'
: ready ? `View ${stageIndex + 1} of ${item.figure.stages.length}: ${item.figure.stages[stageIndex].label}.`
: 'Loading the figure image. The entry is ready to read.';
},
refresh(records) {
if (!item || returning) return;
const next = records.find(record => record.id === item.id && record.published && record.figure);
if (!next) { close(); return; }
if (next.collection !== item.collection) {
close(() => onReadIndex(next.collection));
return;
}
const previousStage = item.figure.stages[stageIndex].id;
const stageHadFocus = Boolean(document.activeElement.closest?.('#passage-stages button'));
item = next;
const seen = visited.get(item.id) || new Set();
visited.set(item.id, new Set([...seen].filter(id => item.figure.stages.some(stage => stage.id === id))));
renderRecord();
selectStage(Math.max(0, item.figure.stages.findIndex(stage => stage.id === previousStage)));
if (stageHadFocus) byId('passage-stages').querySelector(`[data-stage="${stageIndex}"]`).focus({ preventScroll: true });
},
};
}
|