diff --git a/js/ui.js b/js/ui.js index 40067727..979e5f21 100644 --- a/js/ui.js +++ b/js/ui.js @@ -1,6 +1,6 @@ let seenTheDarkness = false, shownDaylightBanner = false, LOCATIONS, locationID = undefined; const startTime = performance.now(); -let labels = [], DELETE_HISTORY = []; +let LABELS = [], DELETE_HISTORY = []; const STATE = { mode: 'analyse', @@ -1623,7 +1623,7 @@ function generateBirdList(store, rows) { function generateBirdOptionList({ store, rows, selected }) { let listHTML = ''; if (store === 'allSpecies') { - let sortedList = labels.map(label => label.split('_')[1]); + let sortedList = LABELS.map(label => label.split('_')[1]); sortedList.sort((a, b) => a.localeCompare(b)); // Check if we have prepared this before const all = document.getElementById('allSpecies'); @@ -2137,8 +2137,8 @@ locale.addEventListener('change', async ()=> { if (! response.ok) throw new Error('Network response was not ok'); return response.text(); }).then(filecontents => { - const labels = filecontents.trim().split('\n'); - worker.postMessage({action: 'update-locale', locale: labels}) + LABELS = filecontents.trim().split('\n'); + worker.postMessage({action: 'update-locale', locale: LABELS}) }).catch(error =>{ console.error('There was a problem fetching the label file:', error); }) @@ -2621,7 +2621,7 @@ function displayWarmUpMessage() { function onModelReady(args) { modelReady = true; - labels = args.labels; + LABELS = args.labels; sampleRate = args.sampleRate; warmupText.classList.add('d-none'); if (fileLoaded) { @@ -2954,10 +2954,12 @@ async function renderResult({ if (index <= 1) { if (selection) { const selectionTable = document.getElementById('selectionResultTableBody'); - selectionTable.innerHTML = ''; + selectionTable.textContent = ''; } else { showElement(['resultTableContainer', 'resultsHead'], false); + const resultTable = document.getElementById('resultTableBody'); + resultTable.textContent = '' } } else if (!isFromDB && index % (config.limit + 1) === 0) { addPagination(index, 0) @@ -4038,9 +4040,9 @@ audioDownmix.addEventListener('change', (e) => { }); function getSnameFromCname(cname) { - for (let i = 0; i < labels.length; i++) { - if (labels[i].includes(cname)) { - return labels[i].split('_')[0]; + for (let i = 0; i < LABELS.length; i++) { + if (LABELS[i].includes(cname)) { + return LABELS[i].split('_')[0]; } } return ; // Substring not found in any item diff --git a/js/worker.js b/js/worker.js index dfe03f8a..dbd4ba22 100644 --- a/js/worker.js +++ b/js/worker.js @@ -128,6 +128,8 @@ async function loadDB(path) { await diskDB.runAsync('VACUUM'); await diskDB.runAsync('PRAGMA foreign_keys = ON'); const { count } = await diskDB.getAsync('SELECT COUNT(*) as count FROM records') + const labelList = await diskDB.allAsync('SELECT sname, cname FROM species'); + LABELS = labelList.map(obj => `${obj.sname}_${obj.cname}`); if (count) { UI.postMessage({ event: 'diskDB-has-records' }) } @@ -146,7 +148,7 @@ async function loadDB(path) { // await Promise.all([species, files]); console.log("Opened and cleaned disk db " + file) } - return true + return LABELS } @@ -1925,13 +1927,14 @@ let SEEN_LABELS = false, SEEN_MODEL_READY = false; async function parseMessage(e) { const response = e.data; switch (response['message']) { - case "labels": {t0 = Date.now(); -LABELS = response["labels"]; -if ( !SEEN_LABELS) { - SEEN_LABELS = true; - await loadDB(appPath); - memoryDB || await createDB(); -} + case "labels": { + t0 = Date.now(); + if ( !SEEN_LABELS) { + LABELS = response["labels"]; + SEEN_LABELS = true; + LABELS = await loadDB(appPath); + memoryDB || await createDB(); + } break; } case "model-ready": {if ( !SEEN_MODEL_READY) { @@ -2801,10 +2804,20 @@ async function onUpdateLocale(labels){ await diskDB.runAsync('BEGIN'); for (let i = 0; i < labels.length; i++){ const [sname, cname] = labels[i].split('_'); - diskDB.runAsync('UPDATE species SET cname = ? WHERE sname = ?', cname, sname); + await diskDB.runAsync('UPDATE species SET cname = ? WHERE sname = ?', cname, sname); } await diskDB.runAsync('END'); - await createDB(); + await memoryDB.runAsync('BEGIN'); + for (let i = 0; i < labels.length; i++){ + const [sname, cname] = labels[i].split('_'); + await memoryDB.runAsync('UPDATE species SET cname = ? WHERE sname = ?', cname, sname); + } + await memoryDB.runAsync('END'); + // //reset index to flush results table + // index = 0; + await getResults() + await getSummary(); + } }