| 1 | /** |
| 2 | * Modal picker for editor dimensions: list + create. |
| 3 | * options: { id, label, disabled?, hint? }[] |
| 4 | */ |
| 5 | export function initDimensionPickerModal ({ |
| 6 | onSelect, |
| 7 | onCreate |
| 8 | }) |
| 9 | { |
| 10 | const openBtn = document.getElementById("open-dimension-picker"); |
| 11 | const currentEl = document.getElementById("dimension-current"); |
| 12 | const backdrop = document.getElementById("dimension-picker-backdrop"); |
| 13 | const modal = document.getElementById("dimension-picker-modal"); |
| 14 | const listEl = document.getElementById("dimension-picker-list"); |
| 15 | const closeBtn = document.getElementById("dimension-picker-close"); |
| 16 | const createBtn = document.getElementById("dimension-picker-create"); |
| 17 | const createIdInput = document.getElementById("dimension-picker-create-id"); |
| 18 | const createNameInput = document.getElementById("dimension-picker-create-name"); |
| 19 | |
| 20 | /** @type {{ id: string, label: string, disabled?: boolean, hint?: string }[]} */ |
| 21 | let options = []; |
| 22 | let selectedId = null; |
| 23 | let open = false; |
| 24 | |
| 25 | function displayLabel (opt) |
| 26 | { |
| 27 | if (!opt) |
| 28 | { return selectedId ?? "—"; } |
| 29 | return opt.label && opt.label !== opt.id ? `${opt.label} (${opt.id})` : opt.id; |
| 30 | } |
| 31 | |
| 32 | function syncCurrentLabel () |
| 33 | { |
| 34 | if (!currentEl) |
| 35 | { return; } |
| 36 | const opt = options.find((entry) => entry.id === selectedId) ?? null; |
| 37 | currentEl.textContent = displayLabel(opt); |
| 38 | } |
| 39 | |
| 40 | function setOpen (visible) |
| 41 | { |
| 42 | open = visible; |
| 43 | if (backdrop) |
| 44 | { backdrop.hidden = !visible; } |
| 45 | if (modal) |
| 46 | { modal.hidden = !visible; } |
| 47 | if (visible) |
| 48 | { |
| 49 | renderList(); |
| 50 | createIdInput?.focus(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | function renderList () |
| 55 | { |
| 56 | if (!listEl) |
| 57 | { return; } |
| 58 | listEl.replaceChildren(); |
| 59 | if (!options.length) |
| 60 | { |
| 61 | const empty = document.createElement("p"); |
| 62 | empty.className = "hint"; |
| 63 | empty.textContent = "No dimensions yet."; |
| 64 | listEl.append(empty); |
| 65 | return; |
| 66 | } |
| 67 | for (const opt of options) |
| 68 | { |
| 69 | const btn = document.createElement("button"); |
| 70 | btn.type = "button"; |
| 71 | btn.className = "dimension-picker-option"; |
| 72 | if (opt.id === selectedId) |
| 73 | { btn.classList.add("active"); } |
| 74 | if (opt.disabled) |
| 75 | { btn.classList.add("soft-deleted"); } |
| 76 | const title = document.createElement("strong"); |
| 77 | title.textContent = opt.label && opt.label !== opt.id ? opt.label : opt.id; |
| 78 | btn.append(title); |
| 79 | if (opt.label && opt.label !== opt.id) |
| 80 | { |
| 81 | const idLine = document.createElement("span"); |
| 82 | idLine.className = "dimension-picker-option-id"; |
| 83 | idLine.textContent = opt.id; |
| 84 | btn.append(idLine); |
| 85 | } |
| 86 | if (opt.hint) |
| 87 | { |
| 88 | const hint = document.createElement("span"); |
| 89 | hint.className = "dimension-picker-option-hint"; |
| 90 | hint.textContent = opt.hint; |
| 91 | btn.append(hint); |
| 92 | } |
| 93 | btn.addEventListener("click", () => |
| 94 | { |
| 95 | setOpen(false); |
| 96 | onSelect?.(opt.id); |
| 97 | }); |
| 98 | listEl.append(btn); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | function submitCreate () |
| 103 | { |
| 104 | const rawId = createIdInput?.value?.trim() ?? ""; |
| 105 | if (!rawId) |
| 106 | { |
| 107 | createIdInput?.focus(); |
| 108 | return; |
| 109 | } |
| 110 | const displayName = createNameInput?.value?.trim() || rawId; |
| 111 | if (createIdInput) |
| 112 | { createIdInput.value = ""; } |
| 113 | if (createNameInput) |
| 114 | { createNameInput.value = ""; } |
| 115 | setOpen(false); |
| 116 | onCreate?.(rawId, displayName); |
| 117 | } |
| 118 | |
| 119 | openBtn?.addEventListener("click", () => setOpen(true)); |
| 120 | closeBtn?.addEventListener("click", () => setOpen(false)); |
| 121 | backdrop?.addEventListener("click", () => setOpen(false)); |
| 122 | createBtn?.addEventListener("click", () => submitCreate()); |
| 123 | createIdInput?.addEventListener("keydown", (event) => |
| 124 | { |
| 125 | if (event.key === "Enter") |
| 126 | { |
| 127 | event.preventDefault(); |
| 128 | submitCreate(); |
| 129 | } |
| 130 | }); |
| 131 | createNameInput?.addEventListener("keydown", (event) => |
| 132 | { |
| 133 | if (event.key === "Enter") |
| 134 | { |
| 135 | event.preventDefault(); |
| 136 | submitCreate(); |
| 137 | } |
| 138 | }); |
| 139 | |
| 140 | return { |
| 141 | isOpen: () => open, |
| 142 | setOptions (nextOptions) |
| 143 | { |
| 144 | options = Array.isArray(nextOptions) ? nextOptions.slice() : []; |
| 145 | if (selectedId && !options.some((opt) => opt.id === selectedId)) |
| 146 | { selectedId = options[0]?.id ?? null; } |
| 147 | syncCurrentLabel(); |
| 148 | if (open) |
| 149 | { renderList(); } |
| 150 | }, |
| 151 | setValue (id) |
| 152 | { |
| 153 | selectedId = id; |
| 154 | syncCurrentLabel(); |
| 155 | if (open) |
| 156 | { renderList(); } |
| 157 | }, |
| 158 | getValue () |
| 159 | { |
| 160 | return selectedId; |
| 161 | }, |
| 162 | close () |
| 163 | { |
| 164 | setOpen(false); |
| 165 | } |
| 166 | }; |
| 167 | } |