// DOM elements const singleModeBtn = document.getElementById('single-mode-btn'); const batchModeBtn = document.getElementById('batch-mode-btn'); const keywordModeBtn = document.getElementById("keyword-mode-btn"); const indexerModeBtn = document.getElementById("indexer-mode-btn") const singleInput = document.querySelector('.single-input'); const batchInput = document.querySelector('.batch-input'); const keywordSearchInput = document.querySelector(".keyword-input"); const indexerButtons = document.querySelector(".indexer-buttons") const docIdInput = document.getElementById('doc-id'); const batchIdsInput = document.getElementById('batch-ids'); const keywordInput = document.getElementById("keywords"); const searchBtn = document.getElementById('search-btn'); const batchSearchBtn = document.getElementById('batch-search-btn'); const keywordSearchBtn = document.getElementById("keyword-search-btn"); const loader = document.getElementById('loader'); const resultsContainer = document.getElementById('results-container'); const resultsList = document.getElementById('results-list'); const resultsStats = document.getElementById('results-stats'); const errorMessage = document.getElementById('error-message'); // Search mode toggle singleModeBtn.addEventListener('click', () => { singleModeBtn.classList.add('active'); keywordModeBtn.classList.remove("active"); batchModeBtn.classList.remove('active'); indexerModeBtn.classList.remove("active"); singleInput.style.display = 'block'; batchInput.style.display = 'none'; keywordSearchInput.style.display = "none"; indexerButtons.style.display = "none"; }); batchModeBtn.addEventListener('click', () => { batchModeBtn.classList.add('active'); keywordModeBtn.classList.remove("active"); singleModeBtn.classList.remove('active'); indexerModeBtn.classList.remove("active"); batchInput.style.display = 'block'; keywordSearchInput.style.display = "none"; indexerButtons.style.display = "none"; singleInput.style.display = 'none'; }); keywordModeBtn.addEventListener('click', () => { keywordModeBtn.classList.add("active"); singleModeBtn.classList.remove('active'); batchModeBtn.classList.remove("active"); indexerModeBtn.classList.remove("active"); singleInput.style.display = "none"; batchInput.style.display = "none"; indexerButtons.style.display = "none"; keywordSearchInput.style.display = "block"; }) indexerModeBtn.addEventListener('click', ()=>{ keywordModeBtn.classList.remove("active"); singleModeBtn.classList.remove('active'); batchModeBtn.classList.remove("active"); indexerModeBtn.classList.add("active"); singleInput.style.display = "none"; batchInput.style.display = "none"; indexerButtons.style.display = "block"; keywordSearchInput.style.display = "none"; }) keywordSearchBtn.addEventListener("click", async ()=>{ const keywords = keywordInput.value.trim(); if (!keywords) { showError("Please enter at least one keyword"); return; } showLoader(); hideError(); try{ const response = await fetch("/search-spec", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ keywords }) }); const data = await response.json(); if (response.ok){ displayKeywordResults(data); } else { showError('Error processing batch request'); } } catch (error) { showError('Error connecting to the server. Please check if the API is running.'); console.error('Error:', error); } finally { hideLoader(); } }) // Single document search searchBtn.addEventListener('click', async () => { const docId = docIdInput.value.trim(); if (!docId) { showError('Please enter a document ID'); return; } showLoader(); hideError(); try { const response = await fetch(`/find`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ doc_id: docId, release: null }) }); const data = await response.json(); if (response.ok) { displaySingleResult(data); } else { displaySingleNotFound(docId, data.detail); } } catch (error) { showError('Error connecting to the server. Please check if the API is running.'); console.error('Error:', error); } finally { hideLoader(); } }); // Batch document search batchSearchBtn.addEventListener('click', async () => { const batchText = batchIdsInput.value.trim(); if (!batchText) { showError('Please enter at least one document ID'); return; } const docIds = batchText.split('\n') .map(id => id.trim()) .filter(id => id !== ''); if (docIds.length === 0) { showError('Please enter at least one valid document ID'); return; } showLoader(); hideError(); try { const response = await fetch(`/batch`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ doc_ids: docIds }) }); const data = await response.json(); if (response.ok) { displayBatchResults(data); } else { showError('Error processing batch request'); } } catch (error) { showError('Error connecting to the server. Please check if the API is running.'); console.error('Error:', error); } finally { hideLoader(); } }); // Display single result function displaySingleResult(data) { resultsList.innerHTML = ''; const resultItem = document.createElement('div'); resultItem.className = 'result-item'; resultItem.innerHTML = `
Title: ${spec.title}
Type: ${spec.type}
Release: ${spec.release}
Version: ${spec.version}
WG: ${spec.working_group}