feat(viewer): Refactor truncate viewer API integration and update modal handling

This commit is contained in:
Ross
2026-05-17 11:04:18 +01:00
parent e368d0c7d1
commit b6716c9ae1
+11 -23
View File
@@ -249,7 +249,7 @@
<div class="card h-100">
<div class="card-header fw-semibold">Viewer / Compare Preview</div>
<div class="card-body p-2 d-flex flex-column gap-2">
<div id="truncate-modal-viewer" class="dicom-viewer-root w-100"
<div id="truncate_viewer" class="dicom-viewer-root w-100"
style="height: 320px; background: #222;"
data-images="{{ image_url_array_and_count.0 }}"
data-auto-cache-stack="false">
@@ -381,6 +381,7 @@
<script>
$(document).ready(function () {
const apiKey = "root"
const truncateApiKey = "truncate_viewer";
const seriesPk = {{ series.pk }};
const totalImages = {{ image_url_array_and_count.1 }};
const viewerHeightKey = `series_viewer_height_${seriesPk}`;
@@ -455,30 +456,14 @@
}, 200);
});
function resolveViewerApi(baseName) {
const candidates = [
`${baseName}_${apiKey}`,
`${baseName}_root`,
`${baseName}_truncate_modal_viewer`,
`${baseName}_truncate-modal-viewer`,
];
for (const name of candidates) {
if (typeof window[name] === 'function') {
return window[name];
}
}
// Fallback: first matching function name from global scope.
const dyn = Object.keys(window).find(
(k) => k.startsWith(`${baseName}_`) && typeof window[k] === 'function'
);
return dyn ? window[dyn] : null;
function getTruncateViewerApi(baseName) {
const fn = window[`${baseName}_${truncateApiKey}`];
return typeof fn === 'function' ? fn : null;
}
document.getElementById('truncate-set-lower-btn')?.addEventListener('click', (e) => {
e.preventDefault();
const getStackPosition = resolveViewerApi('getCurrentStackPosition');
const getStackPosition = getTruncateViewerApi('getCurrentStackPosition');
if (getStackPosition) {
const current = getStackPosition(0);
const pos = Number.isFinite(current) ? current + 1 : 1;
@@ -489,7 +474,7 @@
document.getElementById('truncate-set-upper-btn')?.addEventListener('click', (e) => {
e.preventDefault();
const getStackPosition = resolveViewerApi('getCurrentStackPosition');
const getStackPosition = getTruncateViewerApi('getCurrentStackPosition');
if (getStackPosition) {
const current = getStackPosition(0);
const pos = Number.isFinite(current) ? current + 1 : 1;
@@ -511,13 +496,16 @@
return;
}
const truncateFn = resolveViewerApi('truncateStack');
const truncateFn = getTruncateViewerApi('truncateStack');
if (truncateFn) {
const ok = truncateFn(lower, upper);
if (ok) {
document.getElementById('truncate-modal-output').innerHTML = `Preview shows images ${lower + 1} to ${upper + 1} (${upper - lower + 1} total).`;
document.getElementById('truncate-modal-output').classList.remove('d-none');
}
} else {
document.getElementById('truncate-modal-output').innerHTML = 'Truncate viewer API not ready yet. Reopen the modal and try again.';
document.getElementById('truncate-modal-output').classList.remove('d-none');
}
});
}