340 lines
10 KiB
JavaScript
340 lines
10 KiB
JavaScript
/**
|
|
* Unified HUD Floating Card Focus & Always-On-Top Stack Manager.
|
|
*
|
|
* Rules:
|
|
* 1. User interaction (`source: "user"`, e.g. click / pointerdown / focus / manual open)
|
|
* immediately promotes that card to the top of the dynamic card band (`z-index: 52..135`).
|
|
* 2. System autonomous activation (`source: "system"`, e.g. Stage Narrative next act,
|
|
* Cruise next event, or Spotlight News story rotation) promotes the newly activated
|
|
* card(s) to the top ONLY when the user is not actively pressing/dragging and has
|
|
* been idle past the brief interaction grace window.
|
|
* 3. Always-On-Top pinned surfaces (`pinned: true`, e.g. `#stage-narrative-bar`,
|
|
* `.compute-evidence`, or user-pinned `#situational-copilot`) permanently reside in
|
|
* the pinned band (`z-index: 156..178`), above all dynamic cards and below global
|
|
* toolbars/modals (`200+`).
|
|
*/
|
|
|
|
export const DYNAMIC_Z_BASE = 52;
|
|
export const DYNAMIC_Z_MAX = 135;
|
|
export const HOVER_PREVIEW_Z = 138;
|
|
export const PINNED_Z_BASE = 156;
|
|
|
|
export const DEFAULT_STAGE_GRACE_MS = 1200;
|
|
export const DEFAULT_AMBIENT_GRACE_MS = 3500;
|
|
|
|
const DYNAMIC_CARD_SELECTOR = [
|
|
".compute-card",
|
|
".news-region-card",
|
|
"#info-panel",
|
|
"#news-hud-panel",
|
|
"#situational-copilot",
|
|
".compute-picker",
|
|
"#media-panel",
|
|
].join(", ");
|
|
|
|
function isElementLike(el) {
|
|
return Boolean(el && typeof el === "object" && el.style && el.classList);
|
|
}
|
|
|
|
export function createHudFocusStack({
|
|
nowFn = () => (typeof performance !== "undefined" ? performance.now() : Date.now()),
|
|
stageGraceMs = DEFAULT_STAGE_GRACE_MS,
|
|
ambientGraceMs = DEFAULT_AMBIENT_GRACE_MS,
|
|
} = {}) {
|
|
const dynamicOrder = [];
|
|
const pinnedOrder = [];
|
|
const pinnedPriority = new Map();
|
|
const assignedZ = new Map();
|
|
const boundElements = new WeakSet();
|
|
const activePointers = new Set();
|
|
|
|
let lastUserFocusAt = -Infinity;
|
|
let globalAttachedDoc = null;
|
|
let detachGlobal = null;
|
|
|
|
function removeFromArray(arr, item) {
|
|
const idx = arr.indexOf(item);
|
|
if (idx !== -1) arr.splice(idx, 1);
|
|
}
|
|
|
|
function syncStyles() {
|
|
assignedZ.clear();
|
|
|
|
// Trim any detached DOM nodes if isConnected is explicitly false
|
|
for (let i = dynamicOrder.length - 1; i >= 0; i -= 1) {
|
|
if (dynamicOrder[i]?.isConnected === false) {
|
|
dynamicOrder.splice(i, 1);
|
|
}
|
|
}
|
|
for (let i = pinnedOrder.length - 1; i >= 0; i -= 1) {
|
|
if (pinnedOrder[i]?.isConnected === false) {
|
|
pinnedPriority.delete(pinnedOrder[i]);
|
|
pinnedOrder.splice(i, 1);
|
|
}
|
|
}
|
|
|
|
const count = dynamicOrder.length;
|
|
const step = count > (DYNAMIC_Z_MAX - DYNAMIC_Z_BASE)
|
|
? (DYNAMIC_Z_MAX - DYNAMIC_Z_BASE) / Math.max(1, count)
|
|
: 1;
|
|
const topDynamic = count > 0 ? dynamicOrder[count - 1] : null;
|
|
|
|
dynamicOrder.forEach((el, index) => {
|
|
const z = Math.min(DYNAMIC_Z_MAX, Math.round(DYNAMIC_Z_BASE + index * step));
|
|
assignedZ.set(el, z);
|
|
const isSpotlightOrFloating =
|
|
!el.classList.contains("compute-card") && !el.classList.contains("news-region-card")
|
|
? true
|
|
: el.classList.contains("is-spotlight") || el.classList.contains("is-hover-expanded");
|
|
if (isSpotlightOrFloating) {
|
|
el.style.zIndex = String(z);
|
|
}
|
|
const isTop = el === topDynamic;
|
|
el.classList.toggle("is-hud-focused", isTop);
|
|
el.classList.toggle("is-callout-front", isTop);
|
|
el.classList.remove("is-hud-pinned");
|
|
if (el.dataset) {
|
|
el.dataset.hudPinned = "false";
|
|
}
|
|
});
|
|
|
|
// Sort pinned surfaces stably by priority then insertion/activation order
|
|
const sortedPinned = [...pinnedOrder].sort(
|
|
(a, b) => (pinnedPriority.get(a) || 0) - (pinnedPriority.get(b) || 0),
|
|
);
|
|
sortedPinned.forEach((el, index) => {
|
|
const prio = pinnedPriority.get(el) || 0;
|
|
const z = PINNED_Z_BASE + prio * 4 + index;
|
|
assignedZ.set(el, z);
|
|
el.style.zIndex = String(z);
|
|
el.classList.add("is-hud-pinned");
|
|
el.classList.remove("is-callout-front");
|
|
if (el.dataset) {
|
|
el.dataset.hudPinned = "true";
|
|
}
|
|
});
|
|
}
|
|
|
|
function bindElementInteractions(element) {
|
|
if (!isElementLike(element) || typeof element.addEventListener !== "function") return;
|
|
if (boundElements.has(element)) return;
|
|
boundElements.add(element);
|
|
|
|
element.addEventListener(
|
|
"pointerdown",
|
|
(event) => {
|
|
if (event?.pointerId != null) {
|
|
activePointers.add(event.pointerId);
|
|
}
|
|
focus(element, { source: "user" });
|
|
},
|
|
{ capture: true },
|
|
);
|
|
|
|
element.addEventListener(
|
|
"focusin",
|
|
() => {
|
|
focus(element, { source: "user" });
|
|
},
|
|
{ capture: true },
|
|
);
|
|
}
|
|
|
|
function register(element, { pinned = false, priority = 0 } = {}) {
|
|
if (!isElementLike(element)) return element;
|
|
bindElementInteractions(element);
|
|
if (pinned) {
|
|
removeFromArray(dynamicOrder, element);
|
|
if (!pinnedOrder.includes(element)) {
|
|
pinnedOrder.push(element);
|
|
}
|
|
pinnedPriority.set(element, priority);
|
|
} else if (!pinnedOrder.includes(element) && !dynamicOrder.includes(element)) {
|
|
dynamicOrder.push(element);
|
|
}
|
|
syncStyles();
|
|
return element;
|
|
}
|
|
|
|
function setPinned(element, pinned, { priority = 0 } = {}) {
|
|
if (!isElementLike(element)) return false;
|
|
bindElementInteractions(element);
|
|
if (pinned) {
|
|
removeFromArray(dynamicOrder, element);
|
|
removeFromArray(pinnedOrder, element);
|
|
pinnedOrder.push(element);
|
|
pinnedPriority.set(element, priority);
|
|
} else {
|
|
removeFromArray(pinnedOrder, element);
|
|
pinnedPriority.delete(element);
|
|
removeFromArray(dynamicOrder, element);
|
|
dynamicOrder.push(element);
|
|
}
|
|
syncStyles();
|
|
return Boolean(pinned);
|
|
}
|
|
|
|
function isPinned(element) {
|
|
return pinnedOrder.includes(element);
|
|
}
|
|
|
|
function canSystemAcquireFocus(now, kind = "stage_act") {
|
|
if (activePointers.size > 0) return false;
|
|
const grace = kind === "ambient_tick" ? ambientGraceMs : stageGraceMs;
|
|
if (Number.isFinite(lastUserFocusAt) && now - lastUserFocusAt < grace) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function focus(targetOrTargets, { source = "user", kind = "stage_act", now = nowFn() } = {}) {
|
|
const targets = (Array.isArray(targetOrTargets) ? targetOrTargets : [targetOrTargets])
|
|
.filter(isElementLike);
|
|
if (!targets.length) return false;
|
|
|
|
targets.forEach((el) => bindElementInteractions(el));
|
|
|
|
if (source === "user") {
|
|
lastUserFocusAt = now;
|
|
for (const el of targets) {
|
|
if (isPinned(el)) {
|
|
removeFromArray(pinnedOrder, el);
|
|
pinnedOrder.push(el);
|
|
} else {
|
|
removeFromArray(dynamicOrder, el);
|
|
dynamicOrder.push(el);
|
|
}
|
|
}
|
|
syncStyles();
|
|
return true;
|
|
}
|
|
|
|
// System autonomous activation
|
|
if (!canSystemAcquireFocus(now, kind)) {
|
|
// Ensure unregistered targets still get baseline z-index without overtaking user's active card
|
|
let added = false;
|
|
for (const el of targets) {
|
|
if (!isPinned(el) && !dynamicOrder.includes(el)) {
|
|
dynamicOrder.unshift(el);
|
|
added = true;
|
|
}
|
|
}
|
|
if (added) syncStyles();
|
|
return false;
|
|
}
|
|
|
|
for (const el of targets) {
|
|
if (isPinned(el)) continue;
|
|
removeFromArray(dynamicOrder, el);
|
|
dynamicOrder.push(el);
|
|
}
|
|
syncStyles();
|
|
return true;
|
|
}
|
|
|
|
function getCardZIndex(element, { isSpotlight = true, isHoverExpanded = false, isFront = true } = {}) {
|
|
if (!isElementLike(element)) return isFront ? "30" : "8";
|
|
if (isPinned(element)) {
|
|
return String(assignedZ.get(element) || PINNED_Z_BASE);
|
|
}
|
|
if (isHoverExpanded && !isSpotlight) {
|
|
return String(HOVER_PREVIEW_Z);
|
|
}
|
|
if (isSpotlight) {
|
|
if (!dynamicOrder.includes(element)) {
|
|
bindElementInteractions(element);
|
|
dynamicOrder.push(element);
|
|
syncStyles();
|
|
}
|
|
return String(assignedZ.get(element) || DYNAMIC_Z_BASE);
|
|
}
|
|
return isFront ? "30" : "8";
|
|
}
|
|
|
|
function notifyPointerRelease(pointerId = null) {
|
|
if (pointerId == null) {
|
|
activePointers.clear();
|
|
} else {
|
|
activePointers.delete(pointerId);
|
|
}
|
|
}
|
|
|
|
function attachGlobalListeners(doc = typeof document !== "undefined" ? document : null) {
|
|
if (!doc || typeof doc.addEventListener !== "function") return;
|
|
if (globalAttachedDoc === doc) return;
|
|
detachGlobal?.();
|
|
globalAttachedDoc = doc;
|
|
|
|
const onPointerDown = (event) => {
|
|
if (event?.pointerId != null) {
|
|
activePointers.add(event.pointerId);
|
|
}
|
|
const target = event?.target;
|
|
if (!target || typeof target.closest !== "function") return;
|
|
const card = target.closest(DYNAMIC_CARD_SELECTOR);
|
|
if (!card) return;
|
|
// Non-spotlight 42px capsules that aren't hover-expanded still record user timestamp
|
|
focus(card, { source: "user" });
|
|
};
|
|
|
|
const onPointerEnd = (event) => {
|
|
if (event?.pointerId != null) {
|
|
activePointers.delete(event.pointerId);
|
|
} else {
|
|
activePointers.clear();
|
|
}
|
|
};
|
|
|
|
const onBlur = () => {
|
|
activePointers.clear();
|
|
};
|
|
|
|
doc.addEventListener("pointerdown", onPointerDown, { capture: true });
|
|
doc.addEventListener("pointerup", onPointerEnd, { capture: true });
|
|
doc.addEventListener("pointercancel", onPointerEnd, { capture: true });
|
|
if (typeof window !== "undefined") {
|
|
window.addEventListener("blur", onBlur);
|
|
}
|
|
|
|
detachGlobal = () => {
|
|
doc.removeEventListener("pointerdown", onPointerDown, { capture: true });
|
|
doc.removeEventListener("pointerup", onPointerEnd, { capture: true });
|
|
doc.removeEventListener("pointercancel", onPointerEnd, { capture: true });
|
|
if (typeof window !== "undefined") {
|
|
window.removeEventListener("blur", onBlur);
|
|
}
|
|
globalAttachedDoc = null;
|
|
detachGlobal = null;
|
|
};
|
|
}
|
|
|
|
return {
|
|
register,
|
|
setPinned,
|
|
isPinned,
|
|
focus,
|
|
getCardZIndex,
|
|
notifyPointerRelease,
|
|
attachGlobalListeners,
|
|
getTopDynamic() {
|
|
return dynamicOrder.length ? dynamicOrder[dynamicOrder.length - 1] : null;
|
|
},
|
|
getZIndex(element) {
|
|
return assignedZ.get(element) ?? null;
|
|
},
|
|
reset() {
|
|
dynamicOrder.length = 0;
|
|
pinnedOrder.length = 0;
|
|
pinnedPriority.clear();
|
|
assignedZ.clear();
|
|
activePointers.clear();
|
|
lastUserFocusAt = -Infinity;
|
|
},
|
|
};
|
|
}
|
|
|
|
export const sharedHudFocusStack = createHudFocusStack();
|
|
if (typeof document !== "undefined") {
|
|
sharedHudFocusStack.attachGlobalListeners(document);
|
|
}
|