const $ = id => document.getElementById(id); const esc = s => String(s).replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])); const COL = { ext4: '#8be9fd', btrfs: '#bd93f9', xfs: '#ffb86c', vfat: '#ff79c6', ntfs: '#f1fa8c', zfs: '#50fa7b', swap: '#ff5555', '?': '#a0a4bd', linux_raid_member: '#c9a0ff', '': '#6272a4' }; const fsLabel = f => f === '?' ? 'inconnu' : f === 'linux_raid_member' ? 'membre RAID' : f || 'non formaté'; const colorOf = d => COL[d.fstype] || COL['?']; const fsName = d => fsLabel(d.fstype); const sel = () => S.devs[S.sel]; let err = ''; const poolTag = p => p.pool && S.pools[p.pool] ? ` pool ${S.pools[p.pool].profile === '?' ? '' : S.pools[p.pool].profile + ' '}×${S.pools[p.pool].members.length}` : ''; const mountHtml = p => p.fstype === 'swap' ? '[SWAP]' : host(p).mounts.map(m => esc(m.mp) + (m.subvol ? ` (subvol=${esc(m.subvol)})` : '')).join('
'); function renderTree() { const R = rows(); $('tree').innerHTML = !R.length ? 'rien à afficher — ajoute un disque ou importe un lsblk' : '
NAMEMAJ:MINRMSIZEROTYPEMOUNTPOINTS
' + R.map(r => `
${esc(r.name)}` + (r.cont ? ''.repeat(5) : `${r.d.majmin}${r.d.rm}${fmtLsblk(r.d.size)}${r.d.ro}${typeOf(r.d)}`) + `${esc(r.mount)}
`).join(''); } function renderDetail() { const d = sel(); if (!d) { $('detail').innerHTML = 'clique un disque ou une partition'; return; } if (d.kind === 'raid') { $('detail').innerHTML = `
${esc(d.name)} · RAID ${d.level} · ${fmtIEC(d.size)} · ${d.majmin} · ${fsName(d)} · ${mountHtml(d)}
` + '' + d.members.map(m => ``).join('') + '
MembreTaille
${esc(S.devs[m].name)}${fmtIEC(S.devs[m].size)}
'; return; } const dk = diskOf(d), fr = free(dk); const parts = dk.children.length ? dk.children.map(c => S.devs[c]) : (dk.fstype ? [dk] : []); const pct = s => s / dk.size * 100; const gauge = parts.map(p => `
${pct(p.size) > 7 ? esc(p.name) : ''}
`).join('') + (fr > 0 ? `
${pct(fr) > 7 ? 'libre' : ''}
` : ''); const fss = [...new Set(parts.map(p => p.fstype))]; const legend = fss.map(f => `${fsLabel(f)}`).join('') + (fr > 0 ? 'non alloué' : ''); $('detail').innerHTML = `
${esc(dk.name)} · ${fmtIEC(dk.size)} · ${dk.majmin} · ${dk.children.length} partition(s) · ${fmtIEC(fr)} libre
` + `
${gauge}
${legend}
` + (parts.length ? '' + parts.map(p => ``).join('') + '
PartitionFSMontagesÉtiquetteTailleUtilisé
${esc(p.name)}${fsName(p)}${poolTag(p)}${mountHtml(p)}${esc(p.label)}${fmtIEC(p.size)}${p.used != null ? fmtIEC(p.used) : '—'}
' : ''); } function renderDevPanel() { const d = sel(); if (!d) { $('devPanel').innerHTML = '

Aucune sélection.

'; return; } const pool = d.pool && S.pools[d.pool], h = host(d); const canFs = !pool && !(d.kind === 'disk' && d.children.length); $('devPanel').innerHTML = `
${esc(d.name)} · ${fmtIEC(d.size)} · ${fsName(d)}
` + (pool ? `

Pool btrfs ${esc(pool.label || '(sans étiquette)')} · profil ${pool.profile === '?' ? 'inconnu' : pool.profile} · ${pool.members.length} périphériques${pool.usable != null ? ' · utilisable ≈ ' + fmtIEC(pool.usable) : ''}
Membres : ${pool.members.map(m => esc(S.devs[m].name)).join(', ')}
Montages portés par ${esc(h.name)}.

` : '') + (canFs ? ` ` : pool ? '' : '

Disque partitionné : sélectionne une partition pour la formater/monter.

') + (h.fstype && h.fstype !== 'swap' ? `` + h.mounts.map((m, i) => `
${esc(m.mp)}${m.subvol ? ` (subvol=${esc(m.subvol)})` : ''}
`).join('') + `
` : '') + ''; } function render() { save(); const d = sel(), dk = d && diskOf(d); $('pTarget').textContent = dk ? `sur ${dk.name} — libre : ${fmtIEC(free(dk))}` : 'sélectionne un disque ou une de ses partitions'; $('err').textContent = err; renderTree(); renderDetail(); renderDevPanel(); const el = eligible(); $('raidPool').innerHTML = el.length ? el.map(x => ``).join('') : '

aucun périphérique libre (non monté, hors RAID)

'; } function download(name, text) { const a = document.createElement('a'); a.href = URL.createObjectURL(new Blob([text], { type: 'application/json' })); a.download = name; a.click(); URL.revokeObjectURL(a.href); } const ACT = { sel: t => { S.sel = t.dataset.id; }, addDisk: () => { const v = +$('dSize').value, u = $('dUnit').value; if (!(v > 0)) { err = 'Taille invalide.'; return; } const mult = $('dDec').checked ? { K: 1e3, M: 1e6, G: 1e9, T: 1e12 }[u] : U[u]; S.sel = addDisk(Math.round(v * mult), $('dMedia').value).id; }, addPart: () => { const d = sel(), dk = d && diskOf(d); if (!dk) { err = 'Sélectionne un disque.'; return; } const b = Math.round(+$('pSize').value * U[$('pUnit').value]); if (!(b > 0)) { err = 'Taille invalide.'; return; } if (b > free(dk)) { err = `Espace insuffisant (libre : ${fmtIEC(free(dk))}).`; return; } S.sel = addPart(dk, b, $('pFs').value).id; }, fmt: () => { const d = sel(); if (d && !d.children.length && !d.pool) setFs(d, $('fsSel').value, $('lblIn').value.trim()); }, mount: () => { const d = sel(); if (d) err = addMount(host(d), $('mpIn').value.trim(), $('svIn').value.trim()); }, umount: t => { const d = sel(); if (d) host(d).mounts.splice(+t.dataset.i, 1); }, del: () => { const d = sel(); if (d && confirm(`Supprimer ${d.name}${d.children.length ? ' et ses partitions' : ''} ?`)) delDev(d.id); }, raid: () => { const ids = [...document.querySelectorAll('.rchk:checked')].map(c => c.value), [k, v] = $('rLevel').value.split(':'); err = k === 'md' ? addRaid(ids, +v) : addPool(ids, v, $('rLabel').value.trim()); }, unpool: () => { const d = sel(); if (d && d.pool) dissolvePool(d.pool); }, reset: () => { if (confirm('Tout réinitialiser ?')) { S.devs = {}; S.order = []; S.pools = {}; S.sel = null; } }, imp: () => { if (!S.order.length || confirm('Remplacer la configuration actuelle ?')) err = importAny($('impTxt').value); }, exTxt: () => { $('expTxt').value = toText(); }, copy: () => { navigator.clipboard && navigator.clipboard.writeText($('expTxt').value); }, exJson: () => download('lsblk-manager.json', JSON.stringify({ devs: S.devs, order: S.order, pools: S.pools }, null, 2)), }; document.addEventListener('click', e => { const t = e.target.closest('[data-act]'); if (!t) return; err = ''; ACT[t.dataset.act](t); render(); }); $('impFile').addEventListener('change', e => { const f = e.target.files[0]; if (!f) return; f.text().then(txt => { if (!S.order.length || confirm('Remplacer la configuration actuelle ?')) { err = importAny(txt); } render(); e.target.value = ''; }); }); $('pFs').innerHTML = ['', ...FS].map(f => ``).join(''); load(); render();