Files
noname-shuying/mengsan/ui/relic-strip.js
T

126 lines
4.8 KiB
JavaScript
Raw Normal View History

2026-10-13 18:10:42 +08:00
export function mountRelicStrip(root, getRelics, open, createIcon,
2026-11-22 17:36:42 +08:00
document, window, getCounter = () => null) {
2026-10-13 18:10:42 +08:00
const strip = document.createElement("div");
strip.className = "mengsan-hand-relics-shuying";
2026-10-29 15:21:11 +08:00
strip.setAttribute("aria-label", "持有遗物,点击查看列表");
2026-10-13 18:10:42 +08:00
const before = root.style.getPropertyValue("--mengsan-relic-space");
const priority = root.style.getPropertyPriority("--mengsan-relic-space");
2026-11-22 17:36:42 +08:00
const counters = [];
2026-10-13 18:10:42 +08:00
let pending = null, timer = null, signature = null, disposed = false;
const cancel = () => {
if (timer !== null) window.clearTimeout(timer);
pending?.button.classList.remove("is-pressed");
timer = null;
pending = null;
};
const stop = event => event.stopPropagation();
const down = event => {
stop(event);
cancel();
const button = event.target.closest?.("button");
if (!button || !strip.contains(button) || event.button !== 0) return;
pending = { button, id: event.pointerId,
x: event.clientX, y: event.clientY };
button.classList.add("is-pressed");
timer = window.setTimeout(() => {
const relic = getRelics().find(item => item.id === button.dataset.relicId);
cancel();
if (!disposed && relic) open(relic);
}, 500);
};
const move = event => {
if (event.pointerId !== pending?.id) return;
if (Math.hypot(event.clientX - pending.x,
event.clientY - pending.y) > 8) cancel();
};
const up = event => {
if (event.pointerId === pending?.id) cancel();
};
const click = event => {
stop(event);
event.preventDefault();
const button = event.target.closest?.("button");
const relic = getRelics().find(item => item.id === button?.dataset.relicId);
if (relic) open(relic);
};
const context = event => { stop(event); event.preventDefault(); };
const listeners = [
[strip, "pointerdown", down], [strip, "pointermove", stop],
[strip, "pointerup", stop], [strip, "click", click],
[strip, "keydown", stop], [strip, "keyup", stop],
[strip, "contextmenu", context], [strip, "scroll", cancel],
[strip, "wheel", stop], [strip, "touchstart", stop],
[strip, "touchmove", stop], [strip, "touchend", stop],
[window, "pointermove", move, true],
[window, "pointerup", up, true],
[window, "pointercancel", up, true], [window, "blur", cancel],
];
for (const args of listeners) args[0].addEventListener(...args.slice(1));
root.appendChild(strip);
2026-11-22 17:36:42 +08:00
const refreshCounters = () => {
if (disposed) return;
for (const { relic, button, badge } of counters) {
const { value, label } = getCounter(relic);
const text = String(value);
if (text === badge.textContent) continue;
badge.textContent = text;
button.setAttribute("aria-label", `${relic.name},${label},点击查看遗物列表`);
}
};
2026-10-13 18:10:42 +08:00
const refresh = () => {
if (disposed) return;
const relics = getRelics();
const next = JSON.stringify(relics.map(item => [item.id, item.image, item.name]));
2026-11-22 17:36:42 +08:00
if (next === signature) {
refreshCounters();
return;
}
2026-10-13 18:10:42 +08:00
signature = next;
cancel();
strip.replaceChildren();
2026-11-22 17:36:42 +08:00
counters.length = 0;
2026-10-13 18:10:42 +08:00
strip.hidden = relics.length === 0;
root.style.setProperty("--mengsan-relic-space", relics.length ? "88px" : "0px");
for (const relic of relics) {
const button = document.createElement("button");
button.type = "button";
button.dataset.relicId = relic.id;
2026-10-29 15:21:11 +08:00
button.setAttribute("aria-label", `${relic.name},点击查看遗物列表`);
2026-10-13 18:10:42 +08:00
button.setAttribute("aria-haspopup", "dialog");
const fallback = document.createElement("span");
fallback.textContent = relic.name;
const icon = createIcon(relic.image);
if (icon) {
fallback.hidden = true;
icon.addEventListener("error", () => { fallback.hidden = false; },
{ once: true });
button.appendChild(icon);
}
button.appendChild(fallback);
2026-11-22 17:36:42 +08:00
if (getCounter(relic) !== null) {
const badge = document.createElement("span");
badge.className = "mengsan-relic-counter-shuying";
badge.setAttribute("aria-hidden", "true");
button.appendChild(badge);
counters.push({ relic, button, badge });
}
2026-10-13 18:10:42 +08:00
strip.appendChild(button);
}
2026-11-22 17:36:42 +08:00
refreshCounters();
2026-10-13 18:10:42 +08:00
};
refresh();
2026-11-22 17:36:42 +08:00
return { refresh, refreshCounters, dispose() {
2026-10-13 18:10:42 +08:00
if (disposed) return;
disposed = true;
cancel();
2026-11-22 17:36:42 +08:00
counters.length = 0;
2026-10-13 18:10:42 +08:00
for (const args of listeners) args[0].removeEventListener(...args.slice(1));
strip.remove();
const value = root.style.getPropertyValue("--mengsan-relic-space");
if (value === "88px" || value === "0px") {
if (before) root.style.setProperty("--mengsan-relic-space", before, priority);
else root.style.removeProperty("--mengsan-relic-space");
}
} };
}