Files
Site-Web/autre/lsblk/new/js/ui.js
T
2026-11-20 19:53:02 +01:00

120 lines
9.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const $ = id => document.getElementById(id);
const esc = s => String(s).replace(/[&<>"]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[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] ? ` <small class="dim">pool ${S.pools[p.pool].profile === '?' ? '' : S.pools[p.pool].profile + ' '}×${S.pools[p.pool].members.length}</small>` : '';
const mountHtml = p => p.fstype === 'swap' ? '[SWAP]'
: host(p).mounts.map(m => esc(m.mp) + (m.subvol ? ` <small class="dim">(subvol=${esc(m.subvol)})</small>` : '')).join('<br>');
function renderTree() {
const R = rows();
$('tree').innerHTML = !R.length ? '<span class="dim">rien à afficher — ajoute un disque ou importe un lsblk</span>' :
'<div class="row head"><span>NAME</span><span>MAJ:MIN</span><span>RM</span><span>SIZE</span><span>RO</span><span>TYPE</span><span>MOUNTPOINTS</span></div>' +
R.map(r => `<div class="row ${r.d.id === S.sel ? 'sel' : ''}" data-act="sel" data-id="${r.d.id}"><span class="n-${r.d.kind}">${esc(r.name)}</span>` +
(r.cont ? '<span></span>'.repeat(5)
: `<span class="dim">${r.d.majmin}</span><span class="dim">${r.d.rm}</span><span>${fmtLsblk(r.d.size)}</span><span class="dim">${r.d.ro}</span><span class="or">${typeOf(r.d)}</span>`) +
`<span class="${r.mount === '[SWAP]' ? 'sw' : 'mt'}">${esc(r.mount)}</span></div>`).join('');
}
function renderDetail() {
const d = sel();
if (!d) { $('detail').innerHTML = '<span class="dim">clique un disque ou une partition</span>'; return; }
if (d.kind === 'raid') {
$('detail').innerHTML = `<div class="info"><b>${esc(d.name)}</b> · RAID ${d.level} · ${fmtIEC(d.size)} · ${d.majmin} · ${fsName(d)} · <span class="mt">${mountHtml(d)}</span></div>` +
'<table><tr><th>Membre</th><th>Taille</th></tr>' + d.members.map(m => `<tr data-act="sel" data-id="${m}"><td>${esc(S.devs[m].name)}</td><td>${fmtIEC(S.devs[m].size)}</td></tr>`).join('') + '</table>';
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 => `<div class="seg ${p.id === S.sel ? 'on' : ''}" data-act="sel" data-id="${p.id}" style="flex:${p.size} 1 0;background:${colorOf(p)}" title="${esc(p.name)} · ${fsName(p)} · ${fmtIEC(p.size)}">${pct(p.size) > 7 ? esc(p.name) : ''}</div>`).join('') +
(fr > 0 ? `<div class="seg free" style="flex:${fr} 1 0" title="non alloué · ${fmtIEC(fr)}">${pct(fr) > 7 ? 'libre' : ''}</div>` : '');
const fss = [...new Set(parts.map(p => p.fstype))];
const legend = fss.map(f => `<span><i style="background:${COL[f] || COL['?']}"></i>${fsLabel(f)}</span>`).join('') + (fr > 0 ? '<span><i style="background:#34374a"></i>non alloué</span>' : '');
$('detail').innerHTML =
`<div class="info"><b>${esc(dk.name)}</b> · ${fmtIEC(dk.size)} · ${dk.majmin} · ${dk.children.length} partition(s) · <b>${fmtIEC(fr)}</b> libre</div>` +
`<div class="gauge">${gauge}</div><div class="legend">${legend}</div>` +
(parts.length ? '<table><tr><th>Partition</th><th>FS</th><th>Montages</th><th>Étiquette</th><th>Taille</th><th>Utilisé</th></tr>' +
parts.map(p => `<tr class="${p.id === S.sel ? 'sel' : ''}" data-act="sel" data-id="${p.id}"><td>${esc(p.name)}</td><td><span class="dot" style="background:${colorOf(p)}"></span>${fsName(p)}${poolTag(p)}</td><td class="mt">${mountHtml(p)}</td><td>${esc(p.label)}</td><td>${fmtIEC(p.size)}</td><td>${p.used != null ? fmtIEC(p.used) : '—'}</td></tr>`).join('') + '</table>' : '');
}
function renderDevPanel() {
const d = sel();
if (!d) { $('devPanel').innerHTML = '<p class="hint">Aucune sélection.</p>'; return; }
const pool = d.pool && S.pools[d.pool], h = host(d);
const canFs = !pool && !(d.kind === 'disk' && d.children.length);
$('devPanel').innerHTML = `<div class="info"><b>${esc(d.name)}</b> · ${fmtIEC(d.size)} · ${fsName(d)}</div>` +
(pool ? `<p class="hint">Pool btrfs <b>${esc(pool.label || '(sans étiquette)')}</b> · profil ${pool.profile === '?' ? 'inconnu' : pool.profile} · ${pool.members.length} périphériques${pool.usable != null ? ' · utilisable ≈ ' + fmtIEC(pool.usable) : ''}<br>Membres : ${pool.members.map(m => esc(S.devs[m].name)).join(', ')}<br>Montages portés par <b>${esc(h.name)}</b>.</p><button class="ghost" data-act="unpool">Dissoudre le pool</button>` : '') +
(canFs ? `<label>Système de fichiers</label><select id="fsSel">${['', ...FS].map(f => `<option value="${f}" ${f === d.fstype ? 'selected' : ''}>${f || '— non formaté —'}</option>`).join('')}</select>
<label>Étiquette</label><input id="lblIn" value="${esc(d.label)}"><button data-act="fmt">Formater (efface les montages)</button>` : pool ? '' : '<p class="hint">Disque partitionné : sélectionne une partition pour la formater/monter.</p>') +
(h.fstype && h.fstype !== 'swap' ? `<label>Points de montage (${h.mounts.length})</label>` +
h.mounts.map((m, i) => `<div class="mnt"><span>${esc(m.mp)}${m.subvol ? ` <small class="dim">(subvol=${esc(m.subvol)})</small>` : ''}</span><button data-act="umount" data-i="${i}">✕</button></div>`).join('') +
`<div class="r2" style="grid-template-columns:1fr 1fr;margin-top:6px"><input id="mpIn" placeholder="/home"><input id="svIn" placeholder="sous-volume (@home)"></div><button data-act="mount">+ Ajouter un montage</button>` : '') +
'<button class="ghost" data-act="del">Supprimer</button>';
}
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 => `<label class="chk"><input type="checkbox" class="rchk" value="${x.id}">${esc(x.name)} · ${fmtIEC(x.size)}</label>`).join('') : '<p class="hint">aucun périphérique libre (non monté, hors RAID)</p>';
}
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 => `<option value="${f}">${f || '— non formaté —'}</option>`).join('');
load(); render();