Files
ComfyUI-Lora-Manager/static/js/utils/infiniteScroll.js
Will Miao 04545c5706 Implement lazy loading and infinite scroll features in core application
- Added lazy loading for images and initialized infinite scroll in the AppCore class to enhance performance across various pages.
- Updated LoraPageManager and RecipeManager to utilize the new initializePageFeatures method for common UI features.
- Enhanced infinite scroll functionality to dynamically load more content based on the page type, improving user experience.
- Refactored recipes.html to trigger the import modal through the ModalManager for better modal handling.
2025-03-19 17:04:58 +08:00

58 lines
1.8 KiB
JavaScript

import { state } from '../state/index.js';
import { loadMoreLoras } from '../api/loraApi.js';
import { debounce } from './debounce.js';
export function initializeInfiniteScroll(pageType = 'loras') {
if (state.observer) {
state.observer.disconnect();
}
// Determine the load more function and grid ID based on page type
let loadMoreFunction;
let gridId;
switch (pageType) {
case 'recipes':
loadMoreFunction = window.recipeManager?.loadMoreRecipes || (() => console.warn('loadMoreRecipes not found'));
gridId = 'recipeGrid';
break;
case 'checkpoints':
loadMoreFunction = window.checkpointManager?.loadMoreCheckpoints || (() => console.warn('loadMoreCheckpoints not found'));
gridId = 'checkpointGrid';
break;
case 'loras':
default:
loadMoreFunction = loadMoreLoras;
gridId = 'loraGrid';
break;
}
const debouncedLoadMore = debounce(loadMoreFunction, 200);
state.observer = new IntersectionObserver(
(entries) => {
const target = entries[0];
if (target.isIntersecting && !state.isLoading && state.hasMore) {
debouncedLoadMore();
}
},
{ threshold: 0.1 }
);
const grid = document.getElementById(gridId);
if (!grid) {
console.warn(`Grid with ID "${gridId}" not found for infinite scroll`);
return;
}
const existingSentinel = document.getElementById('scroll-sentinel');
if (existingSentinel) {
state.observer.observe(existingSentinel);
} else {
const sentinel = document.createElement('div');
sentinel.id = 'scroll-sentinel';
sentinel.style.height = '10px';
grid.appendChild(sentinel);
state.observer.observe(sentinel);
}
}