/* This script is responsible for fetching new results on the same page when the user scrolls to the bottom of the page. */ document.addEventListener("DOMContentLoaded", function () { const templateData = document.getElementById('template-data'); let page = parseInt(templateData.getAttribute('data-page')) || 1; const query = templateData.getAttribute('data-query') || ''; let searchType = templateData.getAttribute('data-type') || 'text'; // Default to 'text' if not provided let loading = false; let hasMoreResults = true; const loadingIndicator = document.getElementById('message-bottom-right'); let loadingTimeout; function showLoadingMessage() { loadingIndicator.classList.add('visible'); } function hideLoadingMessage() { loadingIndicator.classList.remove('visible'); } function loadResults(newPage) { if (loading || !hasMoreResults || hasEndOrNoResultsMessage()) return; loading = true; loadingTimeout = setTimeout(() => showLoadingMessage(), 150); fetch(`/search?q=${encodeURIComponent(query)}&t=${encodeURIComponent(searchType)}&p=${newPage}`) .then(response => { if (!response.ok) throw new Error('Network response was not ok'); return response.text(); }) .then(data => { clearTimeout(loadingTimeout); hideLoadingMessage(); const parser = new DOMParser(); const doc = parser.parseFromString(data, 'text/html'); const newResults = doc.querySelectorAll('#results > *'); const resultsContainer = document.getElementById('results'); if (newResults.length === 0) { hasMoreResults = false; loading = false; return; } newResults.forEach(el => resultsContainer.appendChild(el)); page = newPage; checkIfMoreResultsNeeded(); loading = false; }) .catch(error => { clearTimeout(loadingTimeout); hideLoadingMessage(); console.error('Error loading results:', error); hasMoreResults = false; loading = false; }); } function checkIfMoreResultsNeeded() { if (!hasMoreResults || hasEndOrNoResultsMessage()) return; if (document.body.scrollHeight <= window.innerHeight) { loadResults(page + 1); } } function hasEndOrNoResultsMessage() { return !!document.querySelector('.no-results-found'); } window.addEventListener('scroll', () => { if (window.innerHeight + window.scrollY >= document.body.offsetHeight) { loadResults(page + 1); } }); // Ensure the correct search button has the active class at load const buttons = document.querySelectorAll('.search-container-results-btn button'); if (buttons.length === 0) { console.error("No search buttons found"); } else { buttons.forEach(btn => { btn.addEventListener('click', function () { const activeElement = document.querySelector('.search-container-results-btn .search-active'); if (activeElement) { activeElement.classList.remove('search-active'); } this.classList.add('search-active'); // Update search type when button is clicked searchType = this.getAttribute('value'); }); }); // Ensure one button is active on page load const initialActiveElement = document.querySelector('.search-container-results-btn .search-active'); if (!initialActiveElement) { buttons[0].classList.add('search-active'); searchType = buttons[0].getAttribute('value'); // Set default search type } } // Check if more results are needed right after the initial content is loaded checkIfMoreResultsNeeded(); });