/** * @fileoverview Manages search functionality for the room interface. * This file handles: * - Quick search input processing * - Debounced search execution * - File filtering based on search terms * - Search state management * - Clear search functionality */ /** * @class SearchManager * @classdesc Manages search operations including input handling, debounced search execution, * and file filtering based on search criteria. */ export class SearchManager { /** * Creates a new SearchManager instance. * @param {RoomManager} roomManager - The parent RoomManager instance */ constructor(roomManager) { this.roomManager = roomManager; this.searchInput = document.getElementById('quickSearchInput'); this.clearSearchBtn = document.getElementById('clearSearchBtn'); this.isSearching = false; } /** * Initializes the search functionality. * Sets up event listeners for search input and clear button. * Creates a debounced search function to prevent excessive searches. */ initialize() { if (!this.searchInput) return; // Create debounced search function const debouncedSearch = this.debounce((searchTerm) => { if (searchTerm) { this.isSearching = true; this.performSearch(searchTerm); this.clearSearchBtn.style.display = 'block'; } else { this.isSearching = false; this.roomManager.fileManager.fetchFiles(); // Reset to show all files this.clearSearchBtn.style.display = 'none'; } }, 300); // Add input event listener this.searchInput.addEventListener('input', (e) => { const searchTerm = e.target.value.trim(); debouncedSearch(searchTerm); }); // Add clear search button functionality if (this.clearSearchBtn) { this.clearSearchBtn.addEventListener('click', () => { this.searchInput.value = ''; this.isSearching = false; this.roomManager.fileManager.fetchFiles(); // Reset to show all files this.clearSearchBtn.style.display = 'none'; }); } } /** * Performs the search operation using the server-side search endpoint. * @param {string} searchTerm - The term to search for */ performSearch(searchTerm) { // Fetch all files from the server for searching fetch(`/api/rooms/${this.roomManager.roomId}/search?query=${encodeURIComponent(searchTerm)}`) .then(response => response.json()) .then(files => { // Modify the file objects to show full paths in search results const modifiedFiles = files.map(file => ({ ...file, displayName: this.isSearching ? file.full_path : file.name })); this.roomManager.viewManager.renderFiles(modifiedFiles); }) .catch(error => { console.error('Error performing search:', error); }); } /** * Creates a debounced version of a function. * Delays the execution of the function until after a specified wait time * has elapsed since the last time it was invoked. * @param {Function} func - The function to debounce * @param {number} wait - The number of milliseconds to delay * @returns {Function} The debounced function */ debounce(func, wait) { let timeout; return function executedFunction(...args) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; } }