satdetect-dev / static /js /dda /app.js
coderuday21's picture
Cursor
Deploy latest uday app code to Hugging Face (skip research binaries).
c682b53
Raw History Blame Contribute Delete
11.4 kB
// Generic helpers (API, ddaApi, showDdaError, escapeHtml, formatBytes, etc.)
// now live in shared.js, loaded before this file — also used standalone by
// login_dda.html. Don't add page-load side effects there; this file is
// where the main SPA's init logic (initDda(), below) belongs instead.
let ddaConfig = null;
const selectedNode = { id: null, path: '' };
window.ddaState = {
get config() { return ddaConfig; },
get localCfg() { return window._localCfg; },
get selectedNode() { return { ...selectedNode }; },
get userRole() { return window._ddaUserRole || 'analyst'; },
set userRole(r) { window._ddaUserRole = r; },
setNode(n) { selectedNode.id = n.id; selectedNode.path = n.path || ''; },
clearNode() { selectedNode.id = null; selectedNode.path = ''; },
refreshImages: () => loadLibraryImages({ resetPage: true }),
rescan: () => rescanLibrary(),
};
async function rescanLibrary() {
const data = await ddaApi('POST', '/api/dda/local/rescan');
if (typeof renderAllTrees === 'function') renderAllTrees({ tree: data.tree }, []);
else if (typeof renderTree === 'function') renderTree({ tree: data.tree }, []);
if (typeof populateManageNodeSelect === 'function') populateManageNodeSelect();
await loadLibraryImages();
if (typeof loadCompareLibraryGrid === 'function') await loadCompareLibraryGrid();
return data;
}
async function initDda() {
hideDdaError();
const needsLibrary = !!document.getElementById('lib-grid');
const needsDetect = !!document.getElementById('tab-detect');
if (!needsLibrary && !needsDetect) return;
try {
const [me, cfg, localCfg] = await Promise.all([
(typeof ensureDdaMe === 'function' ? ensureDdaMe() : ddaApi('GET', '/api/me')).catch(() => null),
ddaApi('GET', '/api/dda/config'),
ddaApi('GET', '/api/dda/local/config'),
]);
window.ddaState.userRole = (me && me.role) || 'analyst';
ddaConfig = cfg;
window._localCfg = localCfg;
const pathEl = document.getElementById('lib-path-display');
if (pathEl) {
pathEl.textContent = [
'Storage root:',
localCfg.storageRoot || localCfg.writablePath || '',
'Layout: {zone}/{area}/…/Images/file.tif',
].filter(Boolean).join('\n');
}
const folderPath = document.getElementById('lib-folder-path');
if (folderPath) {
folderPath.textContent = `Storage: ${localCfg.storageRoot || ''}`;
}
const instr = document.getElementById('lib-instructions');
if (instr && localCfg.instructions) instr.textContent = localCfg.instructions;
const uploadLimit = document.getElementById('upload-limit-hint');
if (uploadLimit && localCfg.maxUploadGb) {
uploadLimit.textContent = `Select a folder, then drop files or a directory here. Max ${localCfg.maxUploadGb} GB per file.`;
}
const resHint = document.getElementById('dda-detect-res-hint');
if (resHint && localCfg.detectionMaxSide) {
resHint.textContent = `Detection runs at up to ${localCfg.detectionMaxSide}px per side.`;
}
if (needsLibrary) {
// Grid first — tree must not block first paint of images.
loadLibraryImages({ resetPage: true });
if (typeof loadTree === 'function') {
loadTree().catch(() => {
const treeEl = document.getElementById('lib-tree');
if (treeEl && !treeEl.querySelector('.dda-tree-node, .dda-tree-item')) {
treeEl.innerHTML = '<p class="dim">Could not load folders.</p>';
}
});
}
}
} catch (err) {
showDdaError(err.message || 'Failed to load library');
}
}
function selectionTitle() {
return selectedNode.path || 'All images';
}
let libGridPage = 0;
const LIB_GRID_PER_PAGE = 16;
let libGridItems = [];
let libGridTotal = 0;
function renderLibGrid() {
const grid = document.getElementById('lib-grid');
if (!grid) return;
const items = libGridItems;
if (!items.length) {
grid.innerHTML = `<p class="dim">No images in <strong>${escapeHtml(selectionTitle())}</strong>. Select a node and upload, or use Manage to create folders.</p>`;
document.getElementById('lib-grid-pagination')?.replaceChildren();
return;
}
const totalPages = Math.max(1, Math.ceil(libGridTotal / LIB_GRID_PER_PAGE));
libGridPage = Math.max(0, Math.min(libGridPage, totalPages - 1));
grid.innerHTML = items.map((img) => {
const thumb = img.thumbUrl || '';
const crumb = img.breadcrumb || img.nodePath || img.filename;
const place = [img.villageName, img.zoneName].filter(Boolean).join(', ');
const encPath = encodeURIComponent(img.path);
const capture = img.captureDate ? String(img.captureDate).slice(0, 10) : '';
const year = img.year != null ? String(img.year) : '';
return `
<div class="dda-card-img" draggable="true" data-image-path="${encPath}" data-image-id="${img.id}" title="Click to view — ${escapeHtml(img.filename)}">
<label class="dda-card-select-wrap" onclick="event.stopPropagation()">
<input type="checkbox" class="dda-card-select" data-image-id="${img.id}" />
</label>
<button type="button" class="dda-card-delete-btn" title="Delete image" aria-label="Delete image" data-requires-permission="library:delete">×</button>
${thumb ? `<img src="${thumb}" alt="" loading="lazy" />` : '<div class="meta">No preview</div>'}
<div class="meta">
<strong class="dda-card-filename">${escapeHtml(img.filename || img.imageName || '')}</strong><br/>
<span class="dim dda-crumb">${escapeHtml(crumb)}</span><br/>
${place ? `<span class="dim dda-crumb">${escapeHtml(place)}</span><br/>` : ''}
${capture ? `<span class="dda-card-capture">Capture: ${escapeHtml(capture)}</span><br/>` : ''}
<span class="dim">${year ? escapeHtml(year) + ' · ' : ''}${escapeHtml(img.imageType || '')}${img.source ? ' · ' + escapeHtml(img.source) : ''} · ${formatBytes(img.fileSizeBytes)}${img.width && img.height ? ` · ${img.width}×${img.height}` : ''}</span><br/>
<span class="dim dda-card-uploaded">Uploaded ${typeof formatDateIst === 'function' ? formatDateIst(img.uploadedOn) : (img.uploadedOn || '—')}</span>
</div>
</div>`;
}).join('');
if (typeof bindLibraryGridCards === 'function') bindLibraryGridCards(grid);
if (typeof applyPermissionGating === 'function') applyPermissionGating(window.ddaPermissions || {}, grid);
grid.querySelectorAll('.dda-card-select').forEach((cb) => {
cb.addEventListener('change', () => {
if (typeof updateLibDeleteSelectedBtn === 'function') updateLibDeleteSelectedBtn();
});
});
if (typeof updateLibDeleteSelectedBtn === 'function') updateLibDeleteSelectedBtn();
if (typeof renderPaginationControls === 'function') {
renderPaginationControls(document.getElementById('lib-grid-pagination'), libGridPage, totalPages, (p) => {
libGridPage = p;
loadLibraryImages();
});
}
}
async function loadLibraryImages(opts) {
const grid = document.getElementById('lib-grid');
const title = document.getElementById('lib-grid-title');
if (!grid) return;
if (opts && opts.resetPage) libGridPage = 0;
const q = document.getElementById('lib-filter')?.value?.trim() || '';
const params = typeof libFilterParams === 'function' ? libFilterParams() : new URLSearchParams();
if (selectedNode.id) params.set('node_id', String(selectedNode.id));
if (q) params.set('q', q);
params.set('limit', String(LIB_GRID_PER_PAGE));
params.set('offset', String(libGridPage * LIB_GRID_PER_PAGE));
if (title) title.textContent = `Images — ${selectionTitle()}`;
try {
const page = unwrapImagePage(await ddaApi('GET', '/api/dda/local/images?' + params.toString()));
window.ddaState.libraryItems = page.items;
libGridItems = page.items;
libGridTotal = page.total;
const totalPages = Math.max(1, Math.ceil((libGridTotal || 0) / LIB_GRID_PER_PAGE));
if (libGridTotal && libGridPage > totalPages - 1) {
libGridPage = totalPages - 1;
return loadLibraryImages();
}
renderLibGrid();
} catch (err) {
grid.innerHTML = `<p class="dim">Could not load images: ${err.message}</p>`;
}
}
document.getElementById('lib-filter')?.addEventListener('input', () => {
clearTimeout(window._libFilterTimer);
window._libFilterTimer = setTimeout(() => loadLibraryImages({ resetPage: true }), 300);
});
document.getElementById('btn-refresh-lib')?.addEventListener('click', async () => {
const btn = document.getElementById('btn-refresh-lib');
btn.disabled = true;
try {
const data = await rescanLibrary();
const sync = data.sync || {};
const parts = [`${data.totalImages || 0} image(s)`];
if (sync.nodesCreated) parts.push(`${sync.nodesCreated} folder(s) imported`);
if (sync.imagesIndexed) parts.push(`${sync.imagesIndexed} image(s) indexed`);
showDdaSuccess(`Library synced — ${parts.join(', ')}.`);
} catch (err) {
showDdaError(err.message);
} finally {
btn.disabled = false;
}
});
// Current-user chip + sign out (login is required to reach this page at all).
(async function initUserMenu() {
const emailEl = document.getElementById('dda-user-email');
if (!emailEl) return;
try {
const me = typeof ensureDdaMe === 'function' ? await ensureDdaMe() : await ddaApi('GET', '/api/me');
emailEl.textContent = me.email || '';
const avatarEl = document.getElementById('dda-user-avatar');
if (avatarEl) avatarEl.textContent = (me.full_name || me.email || '?').trim().charAt(0).toUpperCase();
} catch (_) {
// Not authenticated — the page-load redirect to /login should already
// have caught this; leave the chip blank rather than erroring loudly.
}
})();
// Dynamic role permissions — fetched once per page load, cached on
// window.ddaPermissions for page-specific JS (e.g. per-row Delete buttons
// rendered by library.js/reports.js) to check directly, and also applied
// declaratively to any static element carrying data-requires-permission.
(async function initPermissionGating() {
const permissions = await loadMyPermissions();
window.ddaPermissions = permissions;
applyPermissionGating(permissions);
document.dispatchEvent(new CustomEvent('dda:permissions-ready', { detail: permissions }));
})();
// Sidebar collapse/expand toggle — persisted so it stays collapsed across
// page loads, same as the LMS reference sidebar.
(function initSidebarToggle() {
const sidebar = document.querySelector('.dda-sidebar-nav');
const toggleBtn = document.getElementById('dda-sidebar-toggle');
if (!sidebar || !toggleBtn) return;
const STORAGE_KEY = 'ddaSidebarCollapsed';
const applyState = (collapsed) => {
sidebar.classList.toggle('collapsed', collapsed);
toggleBtn.setAttribute('aria-expanded', String(!collapsed));
toggleBtn.title = collapsed ? 'Expand sidebar' : 'Collapse sidebar';
};
applyState(localStorage.getItem(STORAGE_KEY) === 'true');
toggleBtn.addEventListener('click', () => {
const collapsed = !sidebar.classList.contains('collapsed');
applyState(collapsed);
localStorage.setItem(STORAGE_KEY, String(collapsed));
});
})();
document.getElementById('dda-logout-btn')?.addEventListener('click', async () => {
try {
await ddaApi('POST', '/api/auth/logout');
} catch (_) {
// Even if the request fails, still send the user to /login below.
} finally {
window.location.href = '/login';
}
});
initDda();