| 1 | // Interfaz HTML: árbol lateral, buscador, botones de vista, ruta y stats. |
| 2 | // createUI se llama una sola vez; setModel() reconstruye el árbol cuando se |
| 3 | // carga otra carpeta raíz. |
| 4 | import { THEMES } from './themes.js'; |
| 5 | |
| 6 | function fmtSize(b) { |
| 7 | if (b < 1024) return `${b}`; |
| 8 | if (b < 1024 * 1024) return `${(b / 1024).toFixed(1)}k`; |
| 9 | return `${(b / 1024 / 1024).toFixed(1)}M`; |
| 10 | } |
| 11 | |
| 12 | export function createUI(cb) { |
| 13 | const $ = id => document.getElementById(id); |
| 14 | const treeEl = $('tree'); |
| 15 | const pathEl = $('pathbar'); |
| 16 | const statsEl = $('stats'); |
| 17 | const resultsEl = $('search-results'); |
| 18 | const searchEl = $('search-input'); |
| 19 | |
| 20 | let model = null; |
| 21 | const fileRows = new Map(); // fileId → fila DOM |
| 22 | const nodeRows = new Map(); // nodeId → fila DOM |
| 23 | let selectedRow = null; |
| 24 | let selectedNodeRow = null; |
| 25 | |
| 26 | // ── árbol lateral (perezoso: los hijos se montan al expandir) ─────── |
| 27 | // imprescindible en monorepos: cientos de miles de filas no caben en DOM |
| 28 | const dirEntries = new Map(); // nodeId → { row, kids, depth, rendered } |
| 29 | |
| 30 | const rowFile = new WeakMap(); // fila DOM → fichero (navegación por teclado) |
| 31 | const rowNode = new WeakMap(); // fila DOM → nodo dir |
| 32 | |
| 33 | function addFileRow(container, f, depth, basement) { |
| 34 | const row = document.createElement('div'); |
| 35 | rowFile.set(row, f); |
| 36 | row.className = 'row file' + (basement ? ' basement' : ''); |
| 37 | row.style.paddingLeft = `${depth * 14 + 8}px`; |
| 38 | // stats e icono de estado git (visibles solo en modo git) |
| 39 | let gitHtml = ''; |
| 40 | if (f.git) { |
| 41 | const stats = (f.git.a || f.git.d) |
| 42 | ? `<span class="gadd">+${f.git.a}</span><span class="gdel">−${f.git.d}</span>` : ''; |
| 43 | gitHtml = `<span class="gwrap">${stats}<span class="gicon g${f.git.st}">${f.git.st}</span></span>`; |
| 44 | } |
| 45 | row.innerHTML = `<span class="name">${f.name}</span>${gitHtml}<span class="meta">${fmtSize(f.size)}</span>`; |
| 46 | row.addEventListener('click', () => cb.onSelectFile?.(f)); |
| 47 | fileRows.set(f.id, row); |
| 48 | container.appendChild(row); |
| 49 | } |
| 50 | |
| 51 | function addDirRow(container, node, depth) { |
| 52 | const row = document.createElement('div'); |
| 53 | rowNode.set(row, node); |
| 54 | const label = node.isBasement ? `${node.name} · sótano` : node.name; |
| 55 | row.className = 'row dir' + (node.isBasement ? ' basement' : ''); |
| 56 | row.style.paddingLeft = `${depth * 14 + 8}px`; |
| 57 | row.innerHTML = `<span class="caret"></span><span class="name">${label}</span><span class="meta">${node.fileIds.length + node.children.length || ''}</span>`; |
| 58 | const kids = document.createElement('div'); |
| 59 | kids.style.display = 'none'; |
| 60 | dirEntries.set(node.id, { row, kids, depth, rendered: false }); |
| 61 | nodeRows.set(node.id, row); |
| 62 | row.addEventListener('click', () => { |
| 63 | const open = row.classList.toggle('open'); |
| 64 | if (open) renderChildren(node); |
| 65 | kids.style.display = open ? '' : 'none'; |
| 66 | cb.onSelectNode?.(node); |
| 67 | }); |
| 68 | container.append(row, kids); |
| 69 | } |
| 70 | |
| 71 | function renderChildren(node) { |
| 72 | const ent = dirEntries.get(node.id); |
| 73 | if (!ent || ent.rendered) return; |
| 74 | ent.rendered = true; |
| 75 | for (const c of node.children) addDirRow(ent.kids, c, ent.depth + 1); |
| 76 | const basement = node.isBasement || node.building?.isBasement; |
| 77 | for (const fid of node.fileIds) { |
| 78 | addFileRow(ent.kids, model.files[fid], ent.depth + 1, basement); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // pliega/despliega una carpeta sin disparar la selección (teclado) |
| 83 | function toggleDir(node, open) { |
| 84 | const ent = dirEntries.get(node.id); |
| 85 | if (!ent) return; |
| 86 | if (open) renderChildren(node); |
| 87 | ent.row.classList.toggle('open', open); |
| 88 | ent.kids.style.display = open ? '' : 'none'; |
| 89 | } |
| 90 | |
| 91 | // ── navegación por teclado en el árbol ────────────────────────────── |
| 92 | treeEl.tabIndex = 0; |
| 93 | treeEl.addEventListener('pointerdown', () => treeEl.focus()); |
| 94 | let cursorRow = null; |
| 95 | |
| 96 | function setCursor(row, scroll = true) { |
| 97 | cursorRow?.classList.remove('cursor'); |
| 98 | cursorRow = row ?? null; |
| 99 | if (cursorRow) { |
| 100 | cursorRow.classList.add('cursor'); |
| 101 | if (scroll) cursorRow.scrollIntoView({ block: 'nearest' }); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | function visibleRows() { |
| 106 | return [...treeEl.querySelectorAll('.row')].filter(r => r.offsetParent !== null); |
| 107 | } |
| 108 | |
| 109 | treeEl.addEventListener('keydown', (e) => { |
| 110 | const rows = visibleRows(); |
| 111 | if (!rows.length) return; |
| 112 | const current = cursorRow && rows.includes(cursorRow) |
| 113 | ? cursorRow |
| 114 | : (selectedRow && rows.includes(selectedRow) ? selectedRow : rows[0]); |
| 115 | const idx = rows.indexOf(current); |
| 116 | const page = Math.max(3, Math.floor(treeEl.clientHeight / 24) - 2); |
| 117 | const node = rowNode.get(current); |
| 118 | const file = rowFile.get(current); |
| 119 | |
| 120 | switch (e.key) { |
| 121 | case 'ArrowDown': |
| 122 | setCursor(rows[Math.min(idx + 1, rows.length - 1)]); |
| 123 | break; |
| 124 | case 'ArrowUp': |
| 125 | setCursor(rows[Math.max(idx - 1, 0)]); |
| 126 | break; |
| 127 | case 'ArrowRight': |
| 128 | if (node) { |
| 129 | if (!current.classList.contains('open')) { toggleDir(node, true); setCursor(current); } |
| 130 | else setCursor(visibleRows()[idx + 1] ?? current); // entra al primer hijo |
| 131 | } |
| 132 | break; |
| 133 | case 'ArrowLeft': { |
| 134 | if (node && current.classList.contains('open')) { toggleDir(node, false); break; } |
| 135 | const parent = node ? node.parent : file?.node; |
| 136 | const pRow = parent && parent.role !== 'ciudad' ? nodeRows.get(parent.id) : null; |
| 137 | if (pRow) setCursor(pRow); |
| 138 | break; |
| 139 | } |
| 140 | case 'PageDown': |
| 141 | setCursor(rows[Math.min(idx + page, rows.length - 1)]); |
| 142 | break; |
| 143 | case 'PageUp': |
| 144 | setCursor(rows[Math.max(idx - page, 0)]); |
| 145 | break; |
| 146 | case 'Enter': |
| 147 | case ' ': |
| 148 | if (file) cb.onSelectFile?.(file, { fly: true }); |
| 149 | else if (node) { toggleDir(node, !current.classList.contains('open')); cb.onSelectNode?.(node); } |
| 150 | break; |
| 151 | default: |
| 152 | return; // no consumir otras teclas (F vuelo, etc.) |
| 153 | } |
| 154 | e.preventDefault(); |
| 155 | e.stopPropagation(); |
| 156 | }); |
| 157 | |
| 158 | // monta y abre la cadena de contenedores hasta `node` (incluido) |
| 159 | function ensureVisible(node) { |
| 160 | const chain = []; |
| 161 | for (let n = node; n && n.role !== 'ciudad'; n = n.parent) chain.push(n); |
| 162 | for (let i = chain.length - 1; i >= 0; i--) { |
| 163 | const ent = dirEntries.get(chain[i].id); |
| 164 | if (!ent) return; |
| 165 | renderChildren(chain[i]); |
| 166 | ent.row.classList.add('open'); |
| 167 | ent.kids.style.display = ''; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | function setModel(m) { |
| 172 | model = m; |
| 173 | fileRows.clear(); |
| 174 | nodeRows.clear(); |
| 175 | dirEntries.clear(); |
| 176 | selectedRow = null; |
| 177 | selectedNodeRow = null; |
| 178 | treeEl.innerHTML = ''; |
| 179 | for (const c of model.city.children) addDirRow(treeEl, c, 1); |
| 180 | for (const fid of model.city.fileIds) addFileRow(treeEl, model.files[fid], 1, false); |
| 181 | baseFileCount = model.files.filter(f => !f.node.building?.isBasement).length; |
| 182 | setPath(null); |
| 183 | setStats(0); |
| 184 | searchEl.value = ''; |
| 185 | hideResults(); |
| 186 | } |
| 187 | |
| 188 | function markFile(file) { |
| 189 | selectedRow?.classList.remove('selected'); |
| 190 | selectedRow = null; |
| 191 | if (file) { |
| 192 | ensureVisible(file.node); |
| 193 | selectedRow = fileRows.get(file.id) ?? null; |
| 194 | } |
| 195 | if (selectedRow) { |
| 196 | selectedRow.classList.add('selected'); |
| 197 | selectedRow.scrollIntoView({ block: 'center', behavior: 'smooth' }); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // marca una carpeta/nivel (selección sincronizada ciudad ↔ árbol) |
| 202 | function markNode(node) { |
| 203 | selectedNodeRow?.classList.remove('selected'); |
| 204 | selectedNodeRow = null; |
| 205 | if (node) { |
| 206 | ensureVisible(node); |
| 207 | selectedNodeRow = nodeRows.get(node.id) ?? null; |
| 208 | } |
| 209 | if (selectedNodeRow) { |
| 210 | selectedNodeRow.classList.add('selected'); |
| 211 | selectedNodeRow.scrollIntoView({ block: 'center', behavior: 'smooth' }); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // ── buscador (con wildcards: * dentro de segmento, ** cruza carpetas) ── |
| 216 | function termMatcher(t) { |
| 217 | if (!t.includes('*') && !t.includes('?')) { |
| 218 | return (f) => f.path.toLowerCase().includes(t); |
| 219 | } |
| 220 | const body = t |
| 221 | .replace(/[.+^${}()|[\]\\]/g, '\\$&') |
| 222 | .replace(/\*\*/g, ' |