import { setPage, getState } from "../src/state/hash.js"; /** * Idempotently bind jump controls inside pagination containers. * - .jump-btn click and .jump-input Enter both update the URL hash via setPage(). * - Uses input[min/max] if available; otherwise clamps to >=1. */ function bindJumpIn(container) { if (!container || container.dataset.jumpInitialized === "1") { return; } container.dataset.jumpInitialized = "1"; const input = container.querySelector(".jump-input"); const button = container.querySelector(".jump-btn"); if (!input || !button) { return; } function clamp(val) { const n = Math.floor(Number(val) || 0); const min = Number(input.getAttribute("min")) || 1; const maxAttr = input.getAttribute("max"); const max = maxAttr ? Math.max(min, Math.floor(Number(maxAttr))) : null; if (max != null) { return Math.max(min, Math.min(n, max)); } return Math.max(min, n); } function commit() { const page = clamp(input.value); if (!Number.isFinite(page) || page < 1) { return; } setPage(page); } button.addEventListener("click", commit); input.addEventListener("keydown", (e) => { if (e.key === "Enter") { commit(); } }); // Keep input in sync if hash changes externally function sync() { const { page } = getState(); if (Number.isFinite(page) && page > 0) { input.value = String(page); } } window.addEventListener("hashchange", sync); document.addEventListener("qedit:hash:updated", sync); } export function initPageJump() { const tops = [ document.getElementById("pagination-top"), document.getElementById("pagination-bottom"), ]; tops.filter(Boolean).forEach(bindJumpIn); } if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", () => initPageJump(), { once: true }); } else { initPageJump(); }