diff --git a/public/js/index.js b/public/js/index.js index dc01b20..3e4a901 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -10,6 +10,8 @@ const moduleField = document.getElementById('moduleField'); const moduleNames = new Map(); const moduleAttachments = new Map(); let moduleSearchTimer = null; +let moduleSearchSeq = 0; +let moduleOptionsKey = ''; const toast = document.getElementById('toast'); const camVideo = document.getElementById('camVideo'); const camCanvas = document.getElementById('camCanvas'); @@ -248,8 +250,20 @@ if (params.get('nameInput')) { field.innerHTML = `
${escName(studentName)}
`; } +function rememberModules(list) { + list.forEach(m => { + moduleNames.set(m.name, m.id); + moduleAttachments.set(m.name.toLowerCase(), m.has_attachments === true); + }); +} + function renderModuleOptions(list) { - list.forEach(m => { moduleNames.set(m.name, m.id); moduleAttachments.set(m.name.toLowerCase(), m.has_attachments === true); }); + rememberModules(list); + const value = (moduleInput.value || '').trim().toLowerCase(); + if (value && list.some(m => String(m.name).trim().toLowerCase() === value)) return; + const key = list.map(m => m.name).join('\n'); + if (key === moduleOptionsKey) return; + moduleOptionsKey = key; moduleInputList.innerHTML = ''; list.forEach(m => { const opt = document.createElement('option'); @@ -298,7 +312,7 @@ async function resolveModuleId(name) { const cached = findModuleId(n); if (cached) return cached; const list = await fetchModules(n, 5); - if (list) renderModuleOptions(list); + if (list) rememberModules(list); const lower = n.toLowerCase(); const exact = (list || []).find(m => m.name.toLowerCase() === lower); return exact ? exact.id : null; @@ -307,9 +321,10 @@ async function resolveModuleId(name) { (async () => { await initDateTime(); try { + const seq = moduleSearchSeq; const list = await fetchModules('', MODULE_LIST_LIMIT); if (list) { - renderModuleOptions(list); + if (seq === moduleSearchSeq) renderModuleOptions(list); if (list.length) { moduleInput.required = true; moduleField.style.display = ''; @@ -547,17 +562,25 @@ moduleInput.addEventListener('input', () => { updateAttachHint(); clearTimeout(moduleSearchTimer); const q = moduleInput.value.trim(); + const seq = ++moduleSearchSeq; moduleSearchTimer = setTimeout(async () => { const list = await fetchModules(q, q ? 20 : MODULE_LIST_LIMIT); + if (seq !== moduleSearchSeq) return; if (list) renderModuleOptions(list); updateAttachHint(); }, 250); }); +moduleInput.addEventListener('change', () => { + clearTimeout(moduleSearchTimer); + moduleSearchSeq++; + updateAttachHint(); +}); moduleInput.addEventListener('focus', async () => { - if (!moduleInput.value.trim()) { - const list = await fetchModules('', MODULE_LIST_LIMIT); - if (list) renderModuleOptions(list); - } + if (moduleInput.value.trim()) return; + const seq = ++moduleSearchSeq; + const list = await fetchModules('', MODULE_LIST_LIMIT); + if (seq !== moduleSearchSeq) return; + if (list) renderModuleOptions(list); }); (async () => {