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' :
'
| Membre | Taille |
|---|---|
| ${esc(S.devs[m].name)} | ${fmtIEC(S.devs[m].size)} |
| Partition | FS | Montages | Étiquette | Taille | Utilisé |
|---|---|---|---|---|---|
| ${esc(p.name)} | ${fsName(p)}${poolTag(p)} | ${mountHtml(p)} | ${esc(p.label)} | ${fmtIEC(p.size)} | ${p.used != null ? fmtIEC(p.used) : '—'} |
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 = `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)}.
Disque partitionné : sélectionne une partition pour la formater/monter.
') + (h.fstype && h.fstype !== 'swap' ? `` + h.mounts.map((m, i) => `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();