| 1 | import { listAllItemPickerEntries, itemDefinition } from "/shared/definitions/items.js"; |
| 2 | import { |
| 3 | formatGiftLetterBody, |
| 4 | QUESTWARD_GIFT_AUTHOR, |
| 5 | QUESTWARD_GIFT_ORG |
| 6 | } from "/shared/courier/giftLetters.js"; |
| 7 | import { formatLetterSignature } from "/shared/courier/systemLetters.js"; |
| 8 | |
| 9 | export function initGiftsModal ({ socket, MessageType }) |
| 10 | { |
| 11 | const backdrop = document.getElementById("gifts-backdrop"); |
| 12 | const modal = document.getElementById("gifts-modal"); |
| 13 | const resultsEl = document.getElementById("gifts-results"); |
| 14 | const detailEl = document.getElementById("gifts-detail"); |
| 15 | const statusEl = document.getElementById("gifts-status"); |
| 16 | const searchInput = document.getElementById("gifts-search-input"); |
| 17 | const searchBtn = document.getElementById("gifts-search-btn"); |
| 18 | let open = false; |
| 19 | /** @type {any[]} */ |
| 20 | let characters = []; |
| 21 | /** @type {any|null} */ |
| 22 | let selected = null; |
| 23 | /** @type {Array<{ itemId: number, quantity: number, displayName: string }>} */ |
| 24 | let giftItems = []; |
| 25 | let messageText = ""; |
| 26 | /** @type {null | { resultsEl: HTMLElement, inputEl: HTMLInputElement }} */ |
| 27 | let itemPickContext = null; |
| 28 | |
| 29 | function show (visible) |
| 30 | { |
| 31 | open = visible; |
| 32 | backdrop.hidden = !visible; |
| 33 | modal.hidden = !visible; |
| 34 | if (!visible) |
| 35 | { |
| 36 | closeInlineItemSearch(); |
| 37 | return; |
| 38 | } |
| 39 | render(); |
| 40 | } |
| 41 | |
| 42 | function setStatus (text, isError = false) |
| 43 | { |
| 44 | if (!statusEl) |
| 45 | { return; } |
| 46 | statusEl.textContent = text || ""; |
| 47 | statusEl.classList.toggle("error", !!isError); |
| 48 | } |
| 49 | |
| 50 | function closeInlineItemSearch () |
| 51 | { |
| 52 | if (itemPickContext?.resultsEl) |
| 53 | { itemPickContext.resultsEl.replaceChildren(); } |
| 54 | itemPickContext = null; |
| 55 | } |
| 56 | |
| 57 | function addGiftItem (item) |
| 58 | { |
| 59 | if (!item?.itemId) |
| 60 | { return; } |
| 61 | const existing = giftItems.find((row) => row.itemId === item.itemId); |
| 62 | if (existing) |
| 63 | { |
| 64 | existing.quantity = Math.min(100000, (existing.quantity | 0) + 1); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | giftItems.push({ |
| 69 | itemId: item.itemId, |
| 70 | quantity: 1, |
| 71 | displayName: item.displayName |
| 72 | || itemDefinition(item.itemId)?.displayName |
| 73 | || `Item ${item.itemId}` |
| 74 | }); |
| 75 | } |
| 76 | closeInlineItemSearch(); |
| 77 | renderDetail(); |
| 78 | } |
| 79 | |
| 80 | function filterItemMatches (query) |
| 81 | { |
| 82 | const q = String(query ?? "").trim().toLowerCase(); |
| 83 | const taken = new Set(giftItems.map((row) => row.itemId)); |
| 84 | return listAllItemPickerEntries().filter((item) => |
| 85 | { |
| 86 | if (taken.has(item.itemId)) |
| 87 | { return false; } |
| 88 | if (!q) |
| 89 | { return true; } |
| 90 | return item.displayName.toLowerCase().includes(q) |
| 91 | || item.key.toLowerCase().includes(q) |
| 92 | || String(item.itemId).includes(q); |
| 93 | }).slice(0, 40); |
| 94 | } |
| 95 | |
| 96 | function renderInlineItemSearch (query) |
| 97 | { |
| 98 | if (!itemPickContext) |
| 99 | { return; } |
| 100 | const { resultsEl: pickResults } = itemPickContext; |
| 101 | pickResults.replaceChildren(); |
| 102 | const matches = filterItemMatches(query); |
| 103 | if (!matches.length) |
| 104 | { |
| 105 | pickResults.textContent = "No matching items."; |
| 106 | return; |
| 107 | } |
| 108 | for (const item of matches) |
| 109 | { |
| 110 | const row = document.createElement("button"); |
| 111 | row.type = "button"; |
| 112 | row.className = "shop-item-search-row"; |
| 113 | const name = document.createElement("span"); |
| 114 | name.className = "shop-item-search-row-name"; |
| 115 | name.textContent = item.displayName; |
| 116 | const key = document.createElement("span"); |
| 117 | key.className = "shop-item-search-row-key"; |
| 118 | key.textContent = `${item.key} (#${item.itemId})`; |
| 119 | row.append(name, key); |
| 120 | row.addEventListener("click", () => addGiftItem(item)); |
| 121 | pickResults.append(row); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | function characterLabel (row) |
| 126 | { |
| 127 | const name = `${row.firstName ?? ""} ${row.lastName ?? ""}`.trim(); |
| 128 | const account = row.accountUsername ? ` · @${row.accountUsername}` : ""; |
| 129 | return `${name}${account}`; |
| 130 | } |
| 131 | |
| 132 | function renderResults () |
| 133 | { |
| 134 | resultsEl.replaceChildren(); |
| 135 | if (!characters.length) |
| 136 | { |
| 137 | resultsEl.textContent = "No characters found."; |
| 138 | return; |
| 139 | } |
| 140 | for (const row of characters) |
| 141 | { |
| 142 | const btn = document.createElement("button"); |
| 143 | btn.type = "button"; |
| 144 | btn.className = selected?.characterId === row.characterId ? "active" : ""; |
| 145 | btn.textContent = characterLabel(row); |
| 146 | btn.addEventListener("click", () => |
| 147 | { |
| 148 | selected = row; |
| 149 | giftItems = []; |
| 150 | messageText = ""; |
| 151 | closeInlineItemSearch(); |
| 152 | render(); |
| 153 | }); |
| 154 | resultsEl.append(btn); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | function renderPreview () |
| 159 | { |
| 160 | const preview = detailEl.querySelector("#gifts-letter-preview"); |
| 161 | const signature = detailEl.querySelector("#gifts-letter-signature"); |
| 162 | if (!preview || !selected) |
| 163 | { return; } |
| 164 | const displayName = `${selected.firstName} ${selected.lastName}`.trim(); |
| 165 | preview.textContent = formatGiftLetterBody({ |
| 166 | characterDisplayName: displayName, |
| 167 | message: messageText, |
| 168 | items: giftItems |
| 169 | }); |
| 170 | if (signature) |
| 171 | { |
| 172 | signature.textContent = formatLetterSignature({ |
| 173 | authorDisplayName: QUESTWARD_GIFT_AUTHOR, |
| 174 | authorOrganization: QUESTWARD_GIFT_ORG |
| 175 | }); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | function renderDetail () |
| 180 | { |
| 181 | detailEl.replaceChildren(); |
| 182 | if (!selected) |
| 183 | { |
| 184 | detailEl.textContent = "Search for a character, then select one to compose a gift."; |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | const heading = document.createElement("h3"); |
| 189 | heading.textContent = `Gift to ${characterLabel(selected)}`; |
| 190 | detailEl.append(heading); |
| 191 | |
| 192 | const messageLabel = document.createElement("label"); |
| 193 | messageLabel.className = "gifts-message-label"; |
| 194 | messageLabel.textContent = "Message"; |
| 195 | const messageArea = document.createElement("textarea"); |
| 196 | messageArea.id = "gifts-message"; |
| 197 | messageArea.rows = 5; |
| 198 | messageArea.placeholder = "Write the personal note included in the letter…"; |
| 199 | messageArea.value = messageText; |
| 200 | messageArea.addEventListener("input", () => |
| 201 | { |
| 202 | messageText = messageArea.value; |
| 203 | renderPreview(); |
| 204 | }); |
| 205 | messageLabel.append(messageArea); |
| 206 | detailEl.append(messageLabel); |
| 207 | |
| 208 | const itemsHeader = document.createElement("div"); |
| 209 | itemsHeader.className = "gifts-items-header"; |
| 210 | const itemsTitle = document.createElement("strong"); |
| 211 | itemsTitle.textContent = "Items"; |
| 212 | itemsHeader.append(itemsTitle); |
| 213 | detailEl.append(itemsHeader); |
| 214 | |
| 215 | const itemSearch = document.createElement("input"); |
| 216 | itemSearch.type = "search"; |
| 217 | itemSearch.placeholder = "Search items to add…"; |
| 218 | itemSearch.autocomplete = "off"; |
| 219 | const itemResults = document.createElement("div"); |
| 220 | itemResults.className = "gifts-item-search-results"; |
| 221 | detailEl.append(itemSearch, itemResults); |
| 222 | |
| 223 | itemPickContext = { inputEl: itemSearch, resultsEl: itemResults }; |
| 224 | itemSearch.addEventListener("input", () => renderInlineItemSearch(itemSearch.value)); |
| 225 | itemSearch.addEventListener("search", () => renderInlineItemSearch(itemSearch.value)); |
| 226 | itemSearch.addEventListener("keydown", (event) => |
| 227 | { |
| 228 | if (event.key !== "Enter") |
| 229 | { return; } |
| 230 | event.preventDefault(); |
| 231 | event.stopPropagation(); |
| 232 | const matches = filterItemMatches(itemSearch.value); |
| 233 | if (matches[0]) |
| 234 | { addGiftItem(matches[0]); } |
| 235 | }); |
| 236 | renderInlineItemSearch(itemSearch.value); |
| 237 | |
| 238 | const list = document.createElement("div"); |
| 239 | list.className = "gifts-item-list"; |
| 240 | if (!giftItems.length) |
| 241 | { |
| 242 | const empty = document.createElement("p"); |
| 243 | empty.className = "hint"; |
| 244 | empty.textContent = "No items yet — search above and click a result (or press Enter)."; |
| 245 | list.append(empty); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | for (let index = 0; index < giftItems.length; index++) |
| 250 | { |
| 251 | const entry = giftItems[index]; |
| 252 | const row = document.createElement("div"); |
| 253 | row.className = "gifts-item-row"; |
| 254 | const name = document.createElement("span"); |
| 255 | name.textContent = entry.displayName |
| 256 | || itemDefinition(entry.itemId)?.displayName |
| 257 | || `Item ${entry.itemId}`; |
| 258 | const qty = document.createElement("input"); |
| 259 | qty.type = "number"; |
| 260 | qty.min = "1"; |
| 261 | qty.max = "100000"; |
| 262 | qty.value = String(entry.quantity); |
| 263 | qty.addEventListener("change", () => |
| 264 | { |
| 265 | entry.quantity = Math.max(1, Math.min(100000, qty.valueAsNumber || 1)); |
| 266 | qty.value = String(entry.quantity); |
| 267 | renderPreview(); |
| 268 | }); |
| 269 | const remove = document.createElement("button"); |
| 270 | remove.type = "button"; |
| 271 | remove.textContent = "Remove"; |
| 272 | remove.addEventListener("click", () => |
| 273 | { |
| 274 | giftItems.splice(index, 1); |
| 275 | renderDetail(); |
| 276 | }); |
| 277 | row.append(name, qty, remove); |
| 278 | list.append(row); |
| 279 | } |
| 280 | } |
| 281 | detailEl.append(list); |
| 282 | |
| 283 | const previewWrap = document.createElement("div"); |
| 284 | previewWrap.className = "gifts-preview-wrap"; |
| 285 | const previewTitle = document.createElement("strong"); |
| 286 | previewTitle.textContent = "Letter preview"; |
| 287 | const preview = document.createElement("pre"); |
| 288 | preview.id = "gifts-letter-preview"; |
| 289 | preview.className = "gifts-letter-preview"; |
| 290 | const signature = document.createElement("pre"); |
| 291 | signature.id = "gifts-letter-signature"; |
| 292 | signature.className = "gifts-letter-signature"; |
| 293 | previewWrap.append(previewTitle, preview, signature); |
| 294 | detailEl.append(previewWrap); |
| 295 | |
| 296 | const sendBtn = document.createElement("button"); |
| 297 | sendBtn.type = "button"; |
| 298 | sendBtn.id = "gifts-send"; |
| 299 | sendBtn.textContent = "Send Gift"; |
| 300 | sendBtn.addEventListener("click", () => |
| 301 | { |
| 302 | if (!selected) |
| 303 | { return; } |
| 304 | setStatus("Sending gift…"); |
| 305 | socket.send({ |
| 306 | type: MessageType.editorSendGift, |
| 307 | characterId: selected.characterId, |
| 308 | message: messageText, |
| 309 | items: giftItems.map((row) => ({ |
| 310 | itemId: row.itemId, |
| 311 | quantity: row.quantity |
| 312 | })) |
| 313 | }); |
| 314 | }); |
| 315 | detailEl.append(sendBtn); |
| 316 | renderPreview(); |
| 317 | } |
| 318 | |
| 319 | function render () |
| 320 | { |
| 321 | renderResults(); |
| 322 | renderDetail(); |
| 323 | } |
| 324 | |
| 325 | function runSearch () |
| 326 | { |
| 327 | const query = searchInput.value.trim(); |
| 328 | if (!query) |
| 329 | { |
| 330 | setStatus("Enter a character or account name.", true); |
| 331 | return; |
| 332 | } |
| 333 | setStatus("Searching…"); |
| 334 | socket.send({ type: MessageType.editorSearchGiftCharacters, query }); |
| 335 | } |
| 336 | |
| 337 | document.getElementById("open-gifts").addEventListener("click", () => |
| 338 | { |
| 339 | setStatus(""); |
| 340 | show(true); |
| 341 | }); |
| 342 | document.getElementById("gifts-close").addEventListener("click", () => show(false)); |
| 343 | backdrop.addEventListener("click", () => show(false)); |
| 344 | searchBtn.addEventListener("click", runSearch); |
| 345 | searchInput.addEventListener("keydown", (event) => |
| 346 | { |
| 347 | if (event.key === "Enter") |
| 348 | { |
| 349 | event.preventDefault(); |
| 350 | runSearch(); |
| 351 | } |
| 352 | }); |
| 353 | |
| 354 | return { |
| 355 | isOpen: () => open, |
| 356 | handleError (message) |
| 357 | { |
| 358 | setStatus(message || "Gift failed.", true); |
| 359 | }, |
| 360 | handleMessage (message) |
| 361 | { |
| 362 | if (message.type === MessageType.editorGiftCharacters) |
| 363 | { |
| 364 | characters = message.characters ?? []; |
| 365 | if (selected && !characters.some((row) => row.characterId === selected.characterId)) |
| 366 | { selected = null; } |
| 367 | setStatus(characters.length ? `${characters.length} character(s)` : "No matches."); |
| 368 | render(); |
| 369 | return; |
| 370 | } |
| 371 | if (message.type === MessageType.editorGiftSent) |
| 372 | { |
| 373 | const name = `${message.firstName ?? ""} ${message.lastName ?? ""}`.trim(); |
| 374 | setStatus(`Gift sent to ${name}.`); |
| 375 | giftItems = []; |
| 376 | messageText = ""; |
| 377 | renderDetail(); |
| 378 | } |
| 379 | } |
| 380 | }; |
| 381 | } |