forked from linkong/planet
373 lines
14 KiB
JavaScript
373 lines
14 KiB
JavaScript
import { createConnectorPath } from "./callout-connector.js";
|
|
import { placeObjectCallout } from "./object-callout-layout.js";
|
|
|
|
const DEFAULT_CONNECTOR_READY_TIMEOUT_MS = 1200;
|
|
import { CALLOUT_DRAW_MS } from "./callout-reveal.js";
|
|
|
|
function nextAnimationFrame() {
|
|
return new Promise((resolve) => {
|
|
window.requestAnimationFrame(() => resolve());
|
|
});
|
|
}
|
|
|
|
function isCurrentContext(context) {
|
|
return !context || typeof context.isCurrent !== "function" || context.isCurrent();
|
|
}
|
|
|
|
function waitForContext(context, durationMs) {
|
|
if (context && typeof context.wait === "function") {
|
|
return context.wait(durationMs);
|
|
}
|
|
return new Promise((resolve) => {
|
|
window.setTimeout(() => resolve(isCurrentContext(context)), durationMs);
|
|
});
|
|
}
|
|
|
|
export function isTargetElementVisible(el) {
|
|
if (!el) return false;
|
|
if (!(el instanceof HTMLElement)) {
|
|
return (Number.isFinite(el.width) && el.width > 0 && Number.isFinite(el.height) && el.height > 0) ||
|
|
(Number.isFinite(el.x) && Number.isFinite(el.y));
|
|
}
|
|
if (el.hasAttribute("hidden")) return false;
|
|
const style = window.getComputedStyle ? window.getComputedStyle(el) : el.style;
|
|
if (style && style.display === "none") return false;
|
|
const rect = el.getBoundingClientRect ? el.getBoundingClientRect() : el;
|
|
return rect.width > 0 && rect.height > 0;
|
|
}
|
|
|
|
function getDefaultCardTarget() {
|
|
const isMobile = document.body.classList.contains("layout-mode-mobile");
|
|
if (isMobile) {
|
|
const mobilePopup = document.getElementById("earth-mobile-popup");
|
|
if (isTargetElementVisible(mobilePopup)) {
|
|
return mobilePopup;
|
|
}
|
|
} else {
|
|
const infoPanel = document.getElementById("info-panel");
|
|
if (isTargetElementVisible(infoPanel)) {
|
|
return infoPanel;
|
|
}
|
|
}
|
|
|
|
const mobilePopup = document.getElementById("earth-mobile-popup");
|
|
if (isTargetElementVisible(mobilePopup)) return mobilePopup;
|
|
|
|
const infoPanel = document.getElementById("info-panel");
|
|
if (isTargetElementVisible(infoPanel)) return infoPanel;
|
|
|
|
return null;
|
|
}
|
|
|
|
export class PresentationController {
|
|
constructor({
|
|
connector,
|
|
pathFactory = createConnectorPath,
|
|
requestAnimationFrame = nextAnimationFrame,
|
|
} = {}) {
|
|
this.connector = connector;
|
|
this.pathFactory = pathFactory;
|
|
this.requestAnimationFrame = requestAnimationFrame;
|
|
this.active = null;
|
|
this.timeoutId = null;
|
|
}
|
|
|
|
isActive(owner = null) {
|
|
if (!this.active) return false;
|
|
return owner ? this.active.request.owner === owner : true;
|
|
}
|
|
|
|
clearTimeout() {
|
|
if (!this.timeoutId) return;
|
|
window.clearTimeout(this.timeoutId);
|
|
this.timeoutId = null;
|
|
}
|
|
|
|
dismiss(reason = "dismiss") {
|
|
if (!this.active) return false;
|
|
const active = this.active;
|
|
this.active = null;
|
|
this.clearTimeout();
|
|
active.request.connector?.instance?.hide?.();
|
|
this.connector?.hide?.();
|
|
active.request.card?.hide?.();
|
|
active.request.onDismiss?.(reason);
|
|
return true;
|
|
}
|
|
|
|
scheduleLifetime(request) {
|
|
this.clearTimeout();
|
|
if (request.lifetime?.mode !== "timeout") return;
|
|
const timeoutMs = Number(request.lifetime.timeoutMs);
|
|
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) return;
|
|
this.timeoutId = window.setTimeout(() => {
|
|
this.dismiss("timeout");
|
|
}, timeoutMs);
|
|
}
|
|
|
|
getConnectorInstance(request) {
|
|
return request.connector?.instance || this.connector || null;
|
|
}
|
|
|
|
getConnectorPath(request) {
|
|
const connectorRequest = request.connector;
|
|
if (!connectorRequest?.enabled) return null;
|
|
const source = connectorRequest.sourceProvider?.();
|
|
if (!source) return null;
|
|
const target = connectorRequest.targetProvider?.() || getDefaultCardTarget();
|
|
if (!target) return null;
|
|
|
|
// If source is on backside, project it to the limb silhouette of the globe
|
|
let effectiveSource = source;
|
|
const globe = source.globe;
|
|
if (source.front === false && globe && Number.isFinite(globe.radius)) {
|
|
const dx = source.x - globe.x;
|
|
const dy = source.y - globe.y;
|
|
const dist = Math.hypot(dx, dy) || 1;
|
|
effectiveSource = {
|
|
...source,
|
|
x: globe.x + (dx / dist) * globe.radius,
|
|
y: globe.y + (dy / dist) * globe.radius,
|
|
};
|
|
}
|
|
|
|
const sourceIsRect =
|
|
Number.isFinite(effectiveSource.x) &&
|
|
Number.isFinite(effectiveSource.y) &&
|
|
Number.isFinite(effectiveSource.width) &&
|
|
Number.isFinite(effectiveSource.height);
|
|
const sourceAnchor = sourceIsRect
|
|
? { x: effectiveSource.x + effectiveSource.width * 0.5, y: effectiveSource.y + effectiveSource.height * 0.5 }
|
|
: effectiveSource;
|
|
const options = {
|
|
...(connectorRequest.options || {}),
|
|
...(sourceIsRect && !connectorRequest.options?.sourceRect ? { sourceRect: effectiveSource } : {}),
|
|
globe: globe || effectiveSource.globe,
|
|
};
|
|
return this.pathFactory(sourceAnchor, target, options);
|
|
}
|
|
|
|
computeIdealBalloonPosition(anchor, size, viewport, obstacles = []) {
|
|
const width = size.width || 340;
|
|
const height = size.height || 380;
|
|
const globe = anchor.globe || viewport.globe || { x: viewport.width / 2, y: viewport.height / 2, radius: 360 };
|
|
const previousPosition = Number.isFinite(size.x) && Number.isFinite(size.y) ? { x: size.x, y: size.y } : null;
|
|
return placeObjectCallout(anchor, { width, height }, { ...viewport, globe, previousPosition }, obstacles);
|
|
}
|
|
|
|
update({ animate = false } = {}) {
|
|
if (!this.active) return false;
|
|
if (!animate && this.active.phase === "preparing") return false;
|
|
const { request } = this.active;
|
|
const connector = this.getConnectorInstance(request);
|
|
if (!connector || request.connector?.enabled !== true) return false;
|
|
if (!animate && connector.isAnimating?.()) return true;
|
|
|
|
// Balloon Leash & Inertia Dynamics (气球惯性动力学):
|
|
const source = request.connector?.sourceProvider?.();
|
|
const target = request.connector?.targetProvider?.() || getDefaultCardTarget();
|
|
if (!target || !isTargetElementVisible(target)) {
|
|
connector.hide?.();
|
|
return false;
|
|
}
|
|
const isTargetDragging = Boolean(
|
|
target?.dataset?.dragged === "true" ||
|
|
target?.classList?.contains("is-dragging")
|
|
);
|
|
const isTargetUserPlaced = Boolean(
|
|
target?.dataset?.userPlaced === "true"
|
|
);
|
|
|
|
if (target instanceof HTMLElement && isTargetDragging) {
|
|
delete target.dataset.dragSyncedHemisphere;
|
|
}
|
|
|
|
if (source && target instanceof HTMLElement && !isTargetDragging) {
|
|
const globe = source.globe;
|
|
let effectiveAnchor = { x: source.x, y: source.y, globe };
|
|
if (source.front === false && globe && Number.isFinite(globe.radius)) {
|
|
const dx = source.x - globe.x;
|
|
const dy = source.y - globe.y;
|
|
const dist = Math.hypot(dx, dy) || 1;
|
|
effectiveAnchor = {
|
|
x: globe.x + (dx / dist) * globe.radius,
|
|
y: globe.y + (dy / dist) * globe.radius,
|
|
globe,
|
|
};
|
|
}
|
|
|
|
const rect = target.getBoundingClientRect();
|
|
|
|
let shouldStayPinned = isTargetUserPlaced;
|
|
if (isTargetUserPlaced) {
|
|
// User explicitly placed this card: respect user intent permanently!
|
|
// Only release user pin if anchor rotates into deep backside occlusion where line-of-sight is lost
|
|
const isDeepRear = source.front === false && (source.visibility ?? -1) < -0.35;
|
|
if (isDeepRear) {
|
|
target.dataset.userPlaced = "false";
|
|
delete target.dataset.dragSyncedHemisphere;
|
|
shouldStayPinned = false;
|
|
this._balloonVx = 0;
|
|
this._balloonVy = 0;
|
|
}
|
|
}
|
|
|
|
if (!shouldStayPinned) {
|
|
const isInteracting = (typeof document !== "undefined" && (
|
|
document.body?.classList?.contains("is-globe-dragging") ||
|
|
document.documentElement?.classList?.contains("is-globe-dragging")
|
|
)) || Boolean(request?.context?.isDragging);
|
|
const curLeft = parseFloat(target.style.left) || rect.x;
|
|
const curTop = parseFloat(target.style.top) || rect.y;
|
|
|
|
// Check if we are currently settled and resting
|
|
const lastAnchor = this.active?.lastSettledAnchor;
|
|
const anchorDist = lastAnchor
|
|
? Math.hypot(effectiveAnchor.x - lastAnchor.x, effectiveAnchor.y - lastAnchor.y)
|
|
: Infinity;
|
|
|
|
// If not interacting and settled with small anchor movement (< 35px):
|
|
// HARD SLEEP / ZERO JITTER: Keep card completely frozen in place!
|
|
const canStaySettled = !isInteracting &&
|
|
this.active?.state === "settled" &&
|
|
anchorDist < 35 &&
|
|
this.active?.settledSlot != null;
|
|
|
|
if (canStaySettled) {
|
|
target.style.left = `${this.active.settledSlot.x}px`;
|
|
target.style.top = `${this.active.settledSlot.y}px`;
|
|
this._balloonVx = 0;
|
|
this._balloonVy = 0;
|
|
} else {
|
|
const viewport = { width: window.innerWidth, height: window.innerHeight, globe };
|
|
const obstacles = [...document.querySelectorAll(".hud-panel:not(#info-panel):not(.compute-card):not(.news-region-card), .news-region-card.is-spotlight, .compute-card.is-spotlight")]
|
|
.filter(element => !element.hidden && getComputedStyle(element).visibility !== "hidden")
|
|
.map(element => { const r = element.getBoundingClientRect(); return { x: r.x, y: r.y, width: r.width, height: r.height }; })
|
|
.filter(r => r.width > 0 && r.height > 0);
|
|
|
|
const ideal = this.computeIdealBalloonPosition(effectiveAnchor, rect, viewport, obstacles);
|
|
|
|
// When first appearing (preparing/drawing connector), snap immediately to final target
|
|
// so the connector line draws directly to the final card position without any pullback!
|
|
if (animate || this.active?.phase !== "visible") {
|
|
target.style.left = `${ideal.x}px`;
|
|
target.style.top = `${ideal.y}px`;
|
|
target.style.right = "auto";
|
|
target.style.bottom = "auto";
|
|
this._balloonVx = 0;
|
|
this._balloonVy = 0;
|
|
if (this.active) {
|
|
this.active.settledSlot = { x: ideal.x, y: ideal.y };
|
|
this.active.lastSettledAnchor = { x: effectiveAnchor.x, y: effectiveAnchor.y };
|
|
this.active.state = "settled";
|
|
}
|
|
} else {
|
|
const dx = ideal.x - curLeft;
|
|
const dy = ideal.y - curTop;
|
|
const dist = Math.hypot(dx, dy);
|
|
|
|
if (dist <= 2.5) {
|
|
// Reached target slot! Settle and sleep!
|
|
target.style.left = `${ideal.x}px`;
|
|
target.style.top = `${ideal.y}px`;
|
|
this._balloonVx = 0;
|
|
this._balloonVy = 0;
|
|
if (this.active) {
|
|
this.active.settledSlot = { x: ideal.x, y: ideal.y };
|
|
this.active.lastSettledAnchor = { x: effectiveAnchor.x, y: effectiveAnchor.y };
|
|
this.active.state = "settled";
|
|
}
|
|
} else {
|
|
if (this.active) this.active.state = "seeking";
|
|
const followFactor = isInteracting ? 0.35 : 0.22;
|
|
const maxStep = isInteracting ? 70 : 35;
|
|
const stepX = Math.sign(dx) * Math.min(Math.abs(dx) * followFactor, maxStep);
|
|
const stepY = Math.sign(dy) * Math.min(Math.abs(dy) * followFactor, maxStep);
|
|
|
|
target.style.left = `${Math.round((curLeft + stepX) * 10) / 10}px`;
|
|
target.style.top = `${Math.round((curTop + stepY) * 10) / 10}px`;
|
|
this._balloonVx = stepX;
|
|
this._balloonVy = stepY;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const path = this.getConnectorPath(request);
|
|
if (!path) {
|
|
connector.hide?.();
|
|
return false;
|
|
}
|
|
const rendered = connector.render(path, { animate }) === true;
|
|
if (rendered && animate) this.active.phase = "drawing";
|
|
return rendered;
|
|
}
|
|
|
|
async waitForConnector(request, context) {
|
|
const timeoutMs =
|
|
Number(request.connector?.readyTimeoutMs) || DEFAULT_CONNECTOR_READY_TIMEOUT_MS;
|
|
const startedAt = performance.now();
|
|
let ready = false;
|
|
while (isCurrentContext(context) && this.active?.request === request) {
|
|
ready = this.update({ animate: !ready });
|
|
if (ready) break;
|
|
if (performance.now() - startedAt >= timeoutMs) break;
|
|
await this.requestAnimationFrame();
|
|
}
|
|
return ready;
|
|
}
|
|
|
|
async present(request, { context } = {}) {
|
|
if (!request?.id || !request.card || !isCurrentContext(context)) return false;
|
|
|
|
this.dismiss("replace");
|
|
this.active = { id: request.id, request, phase: "preparing" };
|
|
|
|
request.card.render?.({ reveal: false });
|
|
await this.requestAnimationFrame();
|
|
if (!isCurrentContext(context) || this.active?.request !== request) {
|
|
if (this.active?.request === request) this.dismiss("interrupted");
|
|
return false;
|
|
}
|
|
|
|
const connectorReady =
|
|
request.connector?.enabled === true
|
|
? await this.waitForConnector(request, context)
|
|
: false;
|
|
|
|
if (!isCurrentContext(context) || this.active?.request !== request) {
|
|
if (this.active?.request === request) this.dismiss("interrupted");
|
|
return false;
|
|
}
|
|
|
|
if (request.connector?.enabled && !connectorReady) {
|
|
this.dismiss("connector_unavailable");
|
|
return false;
|
|
}
|
|
if (connectorReady) {
|
|
const drawMs = Number(request.connector?.drawMs) || CALLOUT_DRAW_MS;
|
|
const connector = this.getConnectorInstance(request);
|
|
const drawCompleted = connector?.waitForDraw
|
|
? await connector.waitForDraw()
|
|
: await waitForContext(context, drawMs);
|
|
if (!drawCompleted || this.active?.request !== request) {
|
|
if (this.active?.request === request) this.dismiss("interrupted");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
this.active.phase = "visible";
|
|
request.card.render?.({ reveal: true });
|
|
await this.requestAnimationFrame();
|
|
if (!isCurrentContext(context) || this.active?.request !== request) {
|
|
if (this.active?.request === request) this.dismiss("interrupted");
|
|
return false;
|
|
}
|
|
|
|
this.update();
|
|
this.scheduleLifetime(request);
|
|
return true;
|
|
}
|
|
}
|