Files
pages/static/js/browser-thumbnails.js
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

76 lines
2.4 KiB
JavaScript

(() => {
document.documentElement.dataset.browserThumbnails = 'ready';
const connection = navigator.connection || {};
const concurrency = connection.saveData || /(^|-)2g$/.test(connection.effectiveType || '') ? 1 : 2;
const queue = [];
let active = 0;
let stopped = false;
const ready = image => image.closest('.browser-thumbnail')
?.classList.replace('children-preview-pending', 'children-preview-ready');
const load = image => new Promise(resolve => {
if (stopped || !image.isConnected || image.dataset.previewLoaded) {
resolve();
return;
}
image.dataset.previewLoaded = 'true';
image.loading = 'lazy';
image.decoding = 'async';
image.fetchPriority = 'low';
const done = success => {
window.clearTimeout(timeout);
if (success) ready(image);
resolve();
};
const timeout = window.setTimeout(() => done(false), 30000);
image.addEventListener('load', () => done(true), {once: true});
image.addEventListener('error', () => done(false), {once: true});
image.src = image.dataset.previewSrc;
});
const drain = () => {
if (stopped || document.visibilityState === 'hidden') return;
while (active < concurrency && queue.length) {
active++;
const image = queue.shift();
Promise.resolve().then(() => load(image)).finally(() => {
active--;
drain();
});
}
};
const enqueue = image => {
if (image.dataset.previewQueued || image.dataset.previewLoaded) return;
image.dataset.previewQueued = 'true';
queue.push(image);
};
const observer = new IntersectionObserver(entries => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
observer.unobserve(entry.target);
enqueue(entry.target);
}
drain();
}, {rootMargin: '240px 0px'});
document.querySelectorAll('.browser-thumbnail [data-preview-src]').forEach(image => {
observer.observe(image);
const rect = image.getBoundingClientRect();
if (rect.bottom >= -240 && rect.top <= innerHeight + 240) {
observer.unobserve(image);
enqueue(image);
}
});
drain();
document.addEventListener('visibilitychange', drain);
window.addEventListener('pagehide', () => {
stopped = true;
queue.length = 0;
observer.disconnect();
document.querySelectorAll('.browser-thumbnail [data-preview-loaded]').forEach(image => image.removeAttribute('src'));
}, {once: true});
})();