From 16ccc4aeb170172735105f46fae7b1743a4ba5b9 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 19 Jan 2024 18:21:00 +0200 Subject: [PATCH] Refactor finding codes --- lippukala/templates/lippukala/pos.html | 73 ++++++++++++++++---------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/lippukala/templates/lippukala/pos.html b/lippukala/templates/lippukala/pos.html index 9c30ab1..91f124b 100644 --- a/lippukala/templates/lippukala/pos.html +++ b/lippukala/templates/lippukala/pos.html @@ -69,18 +69,25 @@ } /** - * @param {Code} code + * @param {Code|null} code */ function showCode(code) { - currentlyShownId = code.id; - $("#status").innerHTML = Tee( - "
{prefix}{code}
{lit}
{prod}
{name}
{comment}
", - code, - ); - let cls = "code-unused"; - if (code.used) cls = "code-used"; - else if (code.localUsed) cls = "code-localused"; - document.body.className = cls; + const statusDiv = $("#status"); + if (code) { + currentlyShownId = code.id; + statusDiv.innerHTML = Tee( + "
{prefix}{code}
{lit}
{prod}
{name}
{comment}
", + code, + ); + let cls = "code-unused"; + if (code.used) cls = "code-used"; + else if (code.localUsed) cls = "code-localused"; + document.body.className = cls; + } else { + currentlyShownId = null; + statusDiv.innerHTML = ""; + document.body.className = ""; + } } /** @@ -112,9 +119,14 @@ }, 250); } - function search(enter) { - let nStarting = 0; - const inputCode = $("#code").value.toLowerCase(); + /** + * Find matching codes for the given input code. + * + * @param {string} inputCode + * @returns {nMatches: number, code: Code | null} + */ + function findMatchingCodes(inputCode) { + let nMatches = 0; let regexpText = `^${inputCode}`; if (/^[-a-z]+ /i.test(inputCode)) { // Cheap "fuzzy" searching ("d bu" will match "desu butler") @@ -126,7 +138,6 @@ } const searchRegexp = new RegExp(regexpText, "i"); let lastCode = null; - document.body.className = ""; for (const code of Object.values(codes)) { const prefixedCode = (code.prefix || "") + code.code; if ( @@ -137,25 +148,34 @@ searchRegexp.test(prefixedCode) || searchRegexp.test(code.lit) ) { - nStarting++; + nMatches++; lastCode = code; } } + return { nMatches, code: nMatches === 1 ? lastCode : null }; + } + + function search(enter) { const statusDiv = $("#status"); - if (nStarting === 1) { - showCode(lastCode); - if (enter) confirmUseCode(lastCode); - } else if (nStarting === 0) { + const inputCode = $("#code").value.toLowerCase().trim(); + if (!inputCode.length) { + showCode(null); + statusDiv.innerHTML = ""; + return; + } + const { nMatches, code } = findMatchingCodes(inputCode); + if (nMatches === 1) { + showCode(code); + if (enter) confirmUseCode(code); + } else if (nMatches === 0) { + showCode(null); statusDiv.innerHTML = "Koodilla ei löydy yhtään lippua. Ole hyvä ja tarkista oikeinkirjoitus ja tapahtuma!"; } else { - statusDiv.innerHTML = `... ${nStarting} ...`; + showCode(null); + statusDiv.innerHTML = `... ${nMatches} ...`; } } - function keyPress() { - search(false); - } - function formSubmit(event) { event.preventDefault(); search(true); @@ -200,9 +220,8 @@ await download(); setInterval(download, (50 + Math.random() * 20) * 1000); setInterval(syncUseQueue, 5000); - - $("#code").addEventListener("input", keyPress, true); - document.getElementById("codeform").addEventListener("submit", debounce(formSubmit, 250), true); + $("#code").addEventListener("input", () => search(false), true); + $("#codeform").addEventListener("submit", debounce(formSubmit, 250), true); };