61 lines
2.1 KiB
JavaScript
61 lines
2.1 KiB
JavaScript
export class SearchManager {
|
|
constructor(roomManager) {
|
|
this.roomManager = roomManager;
|
|
this.searchInput = document.getElementById('quickSearchInput');
|
|
this.clearSearchBtn = document.getElementById('clearSearchBtn');
|
|
}
|
|
|
|
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';
|
|
});
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
debounce(func, wait) {
|
|
let timeout;
|
|
return function executedFunction(...args) {
|
|
const later = () => {
|
|
clearTimeout(timeout);
|
|
func(...args);
|
|
};
|
|
clearTimeout(timeout);
|
|
timeout = setTimeout(later, wait);
|
|
};
|
|
}
|
|
}
|