Spaces:
Sleeping
Sleeping
File size: 9,249 Bytes
214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 15e9574 214c544 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | /** Unlimited-depth tree library UI + admin management. */
let treeData = null;
let imageTypes = [];
const librarySidebar = {
containerId: 'lib-tree',
searchId: 'lib-tree-search',
allBtnId: 'btn-tree-all',
getSelectedNode: () => window.ddaState?.selectedNode,
onNodeSelect: (node) => {
window.ddaState.setNode(node);
window.ddaState.refreshImages();
syncUploadNodeSelect();
},
onClearNode: () => {
window.ddaState.clearNode();
window.ddaState.refreshImages();
syncUploadNodeSelect();
},
};
let compareSidebar = null;
function isAdmin() {
return window.ddaState?.userRole === 'admin';
}
function getSidebars() {
const list = [librarySidebar];
if (compareSidebar) list.push(compareSidebar);
return list;
}
function renderTreeNodes(nodes, sidebar, depth = 0) {
if (!nodes || !nodes.length) return '';
const filter = (document.getElementById(sidebar.searchId)?.value || '').toLowerCase();
const sel = sidebar.getSelectedNode?.();
return nodes.map((node) => {
const name = node.name || node.nodeName;
const children = node.children || [];
const matches = !filter ||
name.toLowerCase().includes(filter) ||
(node.nodePath || '').toLowerCase().includes(filter) ||
children.some((c) => JSON.stringify(c).toLowerCase().includes(filter));
if (!matches && filter) return '';
const active = sel?.id === node.id;
const hasKids = children.length > 0;
const count = node.imageCount ? ` <span class="dim">(${node.imageCount})</span>` : '';
const indent = depth > 0 ? ` style="padding-left:${depth * 0.5}rem"` : '';
if (hasKids) {
return `
<details class="dda-tree-node-wrap" open>
<summary class="dda-tree-node-summary ${active ? 'active' : ''}" data-node-id="${node.id}">
<button type="button" class="dda-tree-node-btn ${active ? 'active' : ''}" data-node-id="${node.id}"
data-node-path="${escapeHtml(node.nodePath || name)}">${escapeHtml(name)}${count}</button>
</summary>
<div class="dda-tree-children">${renderTreeNodes(children, sidebar, depth + 1)}</div>
</details>`;
}
return `
<button type="button" class="dda-tree-node-btn dda-tree-leaf ${active ? 'active' : ''}" data-node-id="${node.id}"
data-node-path="${escapeHtml(node.nodePath || name)}"${indent}>${escapeHtml(name)}${count}</button>`;
}).join('');
}
function renderTreeSidebar(sidebar, tree, types) {
const el = document.getElementById(sidebar.containerId);
if (!el) return;
if (types) imageTypes = types;
const nodes = tree?.tree || tree || [];
const sel = sidebar.getSelectedNode?.();
const allActive = !sel?.id;
const allBtnId = sidebar.allBtnId || `btn-tree-all-${sidebar.containerId}`;
let html = `<button type="button" class="dda-tree-all ${allActive ? 'active' : ''}" id="${allBtnId}">All images</button>`;
html += renderTreeNodes(nodes, sidebar);
if (!nodes.length) {
html += '<p class="dim">No nodes yet. Create folders on disk or use Manage to add zones.</p>';
}
el.innerHTML = html;
document.getElementById(allBtnId)?.addEventListener('click', () => {
sidebar.onClearNode?.();
renderAllTrees(treeData, imageTypes);
});
el.querySelectorAll('.dda-tree-node-btn').forEach((btn) => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
sidebar.onNodeSelect?.({
id: parseInt(btn.dataset.nodeId, 10),
path: btn.dataset.nodePath,
});
renderAllTrees(treeData, imageTypes);
});
});
}
function renderAllTrees(tree, types) {
if (tree) treeData = tree;
getSidebars().forEach((sidebar) => renderTreeSidebar(sidebar, treeData, types));
}
function renderTree(tree, types) {
renderAllTrees(tree, types);
}
function registerCompareTreeSidebar(config) {
compareSidebar = config;
if (treeData) renderAllTrees(treeData, imageTypes);
}
document.getElementById('lib-tree-search')?.addEventListener('input', () => {
if (treeData) renderAllTrees(treeData, imageTypes);
});
document.getElementById('compare-tree-search')?.addEventListener('input', () => {
if (treeData) renderAllTrees(treeData, imageTypes);
});
async function loadTree() {
const data = await ddaApi('GET', '/api/dda/tree');
renderAllTrees(data, data.imageTypes);
populateNodeSelects(data.tree);
if (typeof populateManageNodeSelect === 'function') populateManageNodeSelect();
return data;
}
window.loadTree = loadTree;
window.renderTree = renderTree;
window.renderAllTrees = renderAllTrees;
window.registerCompareTreeSidebar = registerCompareTreeSidebar;
function flattenNodes(nodes, out = []) {
(nodes || []).forEach((n) => {
out.push(n);
flattenNodes(n.children, out);
});
return out;
}
function populateNodeSelects(tree) {
const flat = flattenNodes(tree || []);
const opts = '<option value="">— Select node —</option>' +
flat.map((n) => `<option value="${n.id}">${escapeHtml(n.nodePath || n.name)}</option>`).join('');
['upload-node', 'manage-parent', 'move-parent', 'add-child-parent'].forEach((id) => {
const el = document.getElementById(id);
if (el) el.innerHTML = id === 'manage-parent' ? '<option value="">— Root —</option>' + flat.map((n) =>
`<option value="${n.id}">${escapeHtml(n.nodePath || n.name)}</option>`).join('') : opts;
});
const typeSel = document.getElementById('upload-image-type');
if (typeSel && imageTypes.length) {
typeSel.innerHTML = imageTypes.map((t) => `<option value="${t}">${t}</option>`).join('');
}
syncUploadNodeSelect();
}
function syncUploadNodeSelect() {
const sel = document.getElementById('upload-node');
const node = window.ddaState?.selectedNode;
if (sel && node?.id) sel.value = String(node.id);
}
/* Manage modal */
function openManage() {
document.getElementById('dda-manage-modal')?.classList.remove('hidden');
if (treeData) populateNodeSelects(treeData.tree || treeData);
}
function closeManage() {
document.getElementById('dda-manage-modal')?.classList.add('hidden');
}
document.getElementById('btn-manage-library')?.addEventListener('click', openManage);
document.getElementById('dda-manage-close')?.addEventListener('click', closeManage);
document.getElementById('btn-add-node')?.addEventListener('click', async () => {
const parentVal = document.getElementById('add-child-parent')?.value;
const name = document.getElementById('add-node-name')?.value?.trim();
const type = document.getElementById('add-node-type')?.value || 'Folder';
if (!name) return showDdaError('Enter a node name.');
const body = { node_name: name, node_type: type, parent_id: parentVal ? parseInt(parentVal, 10) : null };
try {
await ddaApi('POST', '/api/dda/tree/nodes', { body: JSON.stringify(body) });
document.getElementById('add-node-name').value = '';
showDdaSuccess('Node created.');
await window.ddaState.rescan();
} catch (err) {
showDdaError(err.message);
}
});
document.getElementById('btn-rename-node')?.addEventListener('click', async () => {
const id = parseInt(document.getElementById('manage-node-select')?.value || '0', 10);
const name = document.getElementById('rename-node-name')?.value?.trim();
if (!id || !name) return showDdaError('Select a node and enter a new name.');
try {
await ddaApi('PUT', `/api/dda/tree/nodes/${id}/rename`, { body: JSON.stringify({ node_name: name }) });
showDdaSuccess('Node renamed.');
await window.ddaState.rescan();
} catch (err) {
showDdaError(err.message);
}
});
document.getElementById('btn-delete-node')?.addEventListener('click', async () => {
const id = parseInt(document.getElementById('manage-node-select')?.value || '0', 10);
if (!id || !confirm('Delete this node? Check delete files if removing images.')) return;
const deleteFiles = document.getElementById('delete-files-check')?.checked;
try {
await ddaApi('DELETE', `/api/dda/tree/nodes/${id}`, { body: JSON.stringify({ delete_files: !!deleteFiles }) });
showDdaSuccess('Node deleted.');
await window.ddaState.rescan();
} catch (err) {
showDdaError(err.message);
}
});
document.getElementById('btn-move-node')?.addEventListener('click', async () => {
const id = parseInt(document.getElementById('manage-node-select')?.value || '0', 10);
const parentVal = document.getElementById('move-parent')?.value;
if (!id) return showDdaError('Select a node to move.');
try {
await ddaApi('POST', `/api/dda/tree/nodes/${id}/move`, {
body: JSON.stringify({ parent_id: parentVal ? parseInt(parentVal, 10) : null }),
});
showDdaSuccess('Node moved.');
await window.ddaState.rescan();
} catch (err) {
showDdaError(err.message);
}
});
function populateManageNodeSelect() {
if (!treeData) return;
const flat = flattenNodes(treeData.tree || treeData || []);
const sel = document.getElementById('manage-node-select');
if (sel) sel.innerHTML = flat.map((n) =>
`<option value="${n.id}">${escapeHtml(n.nodePath || n.name)}</option>`).join('');
}
document.getElementById('dda-manage-modal')?.addEventListener('click', (e) => {
if (e.target.id === 'dda-manage-modal') closeManage();
});
window.populateManageNodeSelect = populateManageNodeSelect;
|