98 lines
3.5 KiB
JavaScript
98 lines
3.5 KiB
JavaScript
/**
|
|
* @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');
|
|
}
|
|
|
|
/**
|
|
* 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.performSearch(searchTerm);
|
|
this.clearSearchBtn.style.display = 'block';
|
|
} else {
|
|
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.roomManager.fileManager.fetchFiles(); // Reset to show all files
|
|
this.clearSearchBtn.style.display = 'none';
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Performs the search operation on the current file list.
|
|
* Filters files based on name and type matching the search term.
|
|
* @param {string} searchTerm - The term to search for
|
|
*/
|
|
performSearch(searchTerm) {
|
|
if (!this.roomManager.fileManager.currentFiles) return;
|
|
|
|
const filteredFiles = this.roomManager.fileManager.currentFiles.filter(file => {
|
|
const searchLower = searchTerm.toLowerCase();
|
|
return file.name.toLowerCase().includes(searchLower) ||
|
|
(file.type && file.type.toLowerCase().includes(searchLower));
|
|
});
|
|
|
|
this.roomManager.viewManager.renderFiles(filteredFiles);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
};
|
|
}
|
|
}
|