Skip to content

Commit

Permalink
Refactor finding codes
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jan 19, 2024
1 parent 1f15e87 commit 16ccc4a
Showing 1 changed file with 46 additions and 27 deletions.
73 changes: 46 additions & 27 deletions lippukala/templates/lippukala/pos.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,25 @@
}

/**
* @param {Code} code
* @param {Code|null} code
*/
function showCode(code) {
currentlyShownId = code.id;
$("#status").innerHTML = Tee(
"<div class=cd><span class=pfx>{prefix}</span>{code}</div>{lit}<div class=product>{prod}</div><div class=addr>{name}</div><div class=comment>{comment}</div>",
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(
"<div class=cd><span class=pfx>{prefix}</span>{code}</div>{lit}<div class=product>{prod}</div><div class=addr>{name}</div><div class=comment>{comment}</div>",
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 = "";
}
}

/**
Expand Down Expand Up @@ -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")
Expand All @@ -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 (
Expand All @@ -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);
Expand Down Expand Up @@ -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);
};
</script>
<style>
Expand Down

0 comments on commit 16ccc4a

Please sign in to comment.