Интерфейс панели был полностью перерисован
Новый дизайн в стиле коммерческих продуктов, рассчитанный на обычного пользователя: - светлая и тёмная темы (следует системной), шрифт Inter и иконки Lucide вшиты в панель — работает без интернета; - общий каркас base.html и компоненты _ui.html вместо 14 независимых страниц; адаптивная вёрстка для телефона; - простой язык: «Работает / Выключена», «Автозапуск», «Доступ из интернета» вместо running, autostart, DNAT; технические данные убраны во вкладку «Для специалистов»; - главная: загрузка процессора, памяти и хранилища, карточки машин с главным действием и меню остальных, приветственный сценарий для первой машины; - мастер создания машины из трёх шагов с готовыми конфигурациями; - страница машины с вкладками, переключателем автозапуска и порядком загрузки кнопками; - «Сеть и доступ»: открытие SSH, сайта, RDP и т. д. из списка, ручные IP машин, понятная диагностика; - загрузка образов перетаскиванием с прогрессом и оставшимся временем; - подтверждения опасных действий в диалоге и уведомления; - раздел «Помощь»; - web/presenters.py переводит вывод virsh и /proc в данные для страниц; tests/dev_server.py запускает панель с имитацией virsh. Также: исправлено определение модели CPU на x86 («207» вместо названия процессора), история версии 0.10.0 и документация обновлены под Ubuntu 26.04 LTS, в README добавлены скриншоты. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019LEUXMcdZo7b87mjkUsPgg
Этот коммит содержится в:
+460
-302
@@ -1,319 +1,477 @@
|
||||
/* Virtuality panel UI behaviour. No dependencies. */
|
||||
(() => {
|
||||
const app = {
|
||||
refreshTimers: [],
|
||||
'use strict';
|
||||
|
||||
const $ = (selector, root = document) => root.querySelector(selector);
|
||||
const $$ = (selector, root = document) => Array.from(root.querySelectorAll(selector));
|
||||
const ICONS = '/static/icons.svg';
|
||||
const icon = (name, cls = 'icon') => `<svg class="${cls}" aria-hidden="true"><use href="${ICONS}#i-${name}"></use></svg>`;
|
||||
const escapeHtml = (value) => String(value ?? '').replace(/[&<>"']/g, (ch) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[ch]));
|
||||
|
||||
const VM_STATES = {
|
||||
'running': ['Работает', 'success'], 'idle': ['Работает', 'success'], 'blocked': ['Работает', 'success'],
|
||||
'paused': ['Приостановлена', 'warning'], 'pmsuspended': ['Спящий режим', 'warning'],
|
||||
'in shutdown': ['Выключается', 'warning'], 'shut off': ['Выключена', 'neutral'], 'shutoff': ['Выключена', 'neutral'],
|
||||
'crashed': ['Сбой', 'danger'], 'dying': ['Останавливается', 'warning'],
|
||||
};
|
||||
const OP_STATES = { success: ['Готово', 'success'], error: ['Ошибка', 'danger'], running: ['Выполняется', 'info'], queued: ['В очереди', 'neutral'] };
|
||||
const vmState = (state) => VM_STATES[String(state || '').toLowerCase()] || [state || 'Неизвестно', 'neutral'];
|
||||
const opState = (status) => OP_STATES[String(status || '').toLowerCase()] || [status || '—', 'neutral'];
|
||||
const badge = ([label, tone]) => `<span class="dot"></span>${escapeHtml(label)}`;
|
||||
|
||||
function qs(selector, root = document) { return root.querySelector(selector); }
|
||||
function qsa(selector, root = document) { return Array.from(root.querySelectorAll(selector)); }
|
||||
|
||||
function ensureThemeStyles() {
|
||||
if (qs('link[data-virtuality-themes="1"]')) return;
|
||||
const link = document.createElement('link');
|
||||
link.rel = 'stylesheet';
|
||||
link.href = '/static/themes.css';
|
||||
link.dataset.virtualityThemes = '1';
|
||||
document.head.appendChild(link);
|
||||
function bytes(value) {
|
||||
if (!value) return '0 Б';
|
||||
const units = ['Б', 'КБ', 'МБ', 'ГБ', 'ТБ'];
|
||||
let n = value; let i = 0;
|
||||
while (n >= 1024 && i < units.length - 1) { n /= 1024; i += 1; }
|
||||
return `${n.toFixed(n >= 10 || i === 0 ? 0 : 1)} ${units[i]}`;
|
||||
}
|
||||
|
||||
function getTheme() {
|
||||
return localStorage.getItem('virtualityTheme') || 'neon';
|
||||
}
|
||||
|
||||
function applyTheme(theme) {
|
||||
const selected = theme === 'macos' ? 'macos' : 'neon';
|
||||
document.documentElement.dataset.theme = selected;
|
||||
localStorage.setItem('virtualityTheme', selected);
|
||||
qsa('[data-theme-select]').forEach((btn) => {
|
||||
btn.classList.toggle('is-active', btn.dataset.themeSelect === selected);
|
||||
/* ---------------------------------------------------- theme */
|
||||
function applyTheme(mode) {
|
||||
const dark = mode === 'dark' || (mode === 'system' && matchMedia('(prefers-color-scheme: dark)').matches);
|
||||
document.documentElement.dataset.theme = dark ? 'dark' : 'light';
|
||||
$$('[data-theme-toggle]').forEach((btn) => {
|
||||
btn.innerHTML = icon(dark ? 'sun' : 'moon');
|
||||
btn.title = dark ? 'Светлая тема' : 'Тёмная тема';
|
||||
});
|
||||
}
|
||||
|
||||
function wireThemeSwitcher() {
|
||||
ensureThemeStyles();
|
||||
const nav = qs('.v-nav');
|
||||
if (!nav || qs('.theme-switcher')) {
|
||||
applyTheme(getTheme());
|
||||
return;
|
||||
}
|
||||
const switcher = document.createElement('div');
|
||||
switcher.className = 'theme-switcher';
|
||||
switcher.innerHTML = '<div class="theme-switcher-label">Тема</div><div class="theme-buttons"><button type="button" class="theme-button" data-theme-select="neon">Neon</button><button type="button" class="theme-button" data-theme-select="macos">MacOS</button></div>';
|
||||
nav.appendChild(switcher);
|
||||
qsa('[data-theme-select]', switcher).forEach((btn) => {
|
||||
btn.addEventListener('click', () => applyTheme(btn.dataset.themeSelect));
|
||||
});
|
||||
applyTheme(getTheme());
|
||||
function currentMode() { try { return localStorage.getItem('virtualityTheme') || 'system'; } catch (_) { return 'system'; } }
|
||||
function wireTheme() {
|
||||
applyTheme(currentMode());
|
||||
$$('[data-theme-toggle]').forEach((btn) => btn.addEventListener('click', () => {
|
||||
const next = document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark';
|
||||
try { localStorage.setItem('virtualityTheme', next); } catch (_) {}
|
||||
applyTheme(next);
|
||||
}));
|
||||
matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { if (currentMode() === 'system') applyTheme('system'); });
|
||||
}
|
||||
|
||||
function activeSidebar(pathname = window.location.pathname) {
|
||||
qsa('.v-nav a').forEach((item) => {
|
||||
const url = new URL(item.href, window.location.href);
|
||||
const path = url.pathname;
|
||||
const active = path === '/' ? pathname === '/' : pathname.startsWith(path);
|
||||
item.classList.toggle('active', active);
|
||||
});
|
||||
qsa('.v-nav-group').forEach((group) => {
|
||||
const hasActive = qsa('a.active', group).length > 0;
|
||||
group.classList.toggle('open', hasActive);
|
||||
});
|
||||
}
|
||||
|
||||
async function refreshFragments() {
|
||||
const fragments = qsa('[data-refresh-url]');
|
||||
for (const fragment of fragments) {
|
||||
const url = fragment.dataset.refreshUrl;
|
||||
if (!url) continue;
|
||||
try {
|
||||
const response = await fetch(url, { cache: 'no-store', headers: { 'X-Requested-With': 'XMLHttpRequest' }});
|
||||
const payload = await response.json();
|
||||
if (!payload.ok) continue;
|
||||
if (payload.html) fragment.innerHTML = payload.html;
|
||||
if (payload.text) fragment.textContent = payload.text;
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
function startAutoRefresh(root = document) {
|
||||
const interval = Number(qs('[data-auto-refresh]', root)?.dataset.autoRefresh || 0);
|
||||
if (interval > 0) {
|
||||
app.refreshTimers.forEach((timer) => clearInterval(timer));
|
||||
app.refreshTimers = [];
|
||||
const timer = setInterval(refreshFragments, interval * 1000);
|
||||
app.refreshTimers.push(timer);
|
||||
refreshFragments();
|
||||
}
|
||||
}
|
||||
|
||||
function wireNavGroups(root = document) {
|
||||
qsa('.v-nav-group-title', root).forEach((title) => {
|
||||
if (title.dataset.groupWired === '1') return;
|
||||
title.dataset.groupWired = '1';
|
||||
title.addEventListener('click', () => title.closest('.v-nav-group')?.classList.toggle('open'));
|
||||
});
|
||||
}
|
||||
|
||||
function wireMobileMenu() {
|
||||
const sidebar = qs('.v-sidebar');
|
||||
if (!sidebar || qs('.mobile-menu-toggle')) return;
|
||||
const btn = document.createElement('button');
|
||||
btn.className = 'mobile-menu-toggle';
|
||||
btn.type = 'button';
|
||||
btn.textContent = '☰ Меню';
|
||||
btn.addEventListener('click', () => sidebar.classList.toggle('open'));
|
||||
document.body.appendChild(btn);
|
||||
}
|
||||
|
||||
function initPanel() {
|
||||
wireThemeSwitcher();
|
||||
wireNavGroups();
|
||||
wireMobileMenu();
|
||||
activeSidebar();
|
||||
startAutoRefresh();
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', initPanel);
|
||||
applyTheme(getTheme());
|
||||
})();
|
||||
|
||||
/* VM visual cards from raw virsh output */
|
||||
(() => {
|
||||
function qs(selector, root = document) { return root.querySelector(selector); }
|
||||
function qsa(selector, root = document) { return Array.from(root.querySelectorAll(selector)); }
|
||||
function escapeHtml(value) {
|
||||
return String(value ?? '').replace(/[&<>"]/g, (ch) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[ch]));
|
||||
}
|
||||
function rawPreBySummary(summaryText) {
|
||||
const details = qsa('.raw-details').find((item) => (qs('summary', item)?.textContent || '').includes(summaryText));
|
||||
return qs('pre', details)?.textContent.trim() || '';
|
||||
}
|
||||
function parseKeyValues(raw) {
|
||||
const data = {};
|
||||
raw.split('\n').forEach((line) => {
|
||||
const idx = line.indexOf(':');
|
||||
if (idx < 0) return;
|
||||
const key = line.slice(0, idx).trim().toLowerCase();
|
||||
const value = line.slice(idx + 1).trim();
|
||||
if (key) data[key] = value;
|
||||
});
|
||||
return data;
|
||||
}
|
||||
function sizeToGiB(value) {
|
||||
const raw = String(value || '').trim();
|
||||
const n = Number(raw.replace(',', '.').replace(/[^0-9.]/g, ''));
|
||||
if (!Number.isFinite(n) || n <= 0) return 0;
|
||||
if (/kib|kb/i.test(raw)) return n / 1024 / 1024;
|
||||
if (/mib|mb/i.test(raw)) return n / 1024;
|
||||
if (/gib|gb/i.test(raw)) return n;
|
||||
return n / 1024 / 1024;
|
||||
}
|
||||
function renderInfo() {
|
||||
const box = qs('#vm-info-visual');
|
||||
if (!box) return;
|
||||
const raw = rawPreBySummary('dominfo');
|
||||
const data = parseKeyValues(raw);
|
||||
const cpu = data['cpu(s)'] || data['cpu'] || '—';
|
||||
const state = data.state || 'unknown';
|
||||
const osType = data['os type'] || data['os'] || '—';
|
||||
const maxMem = sizeToGiB(data['max memory']);
|
||||
const usedMem = sizeToGiB(data['used memory']);
|
||||
const memPct = maxMem > 0 ? Math.max(0, Math.min(100, Math.round((usedMem / maxMem) * 100))) : 0;
|
||||
const cpuPct = Math.max(8, Math.min(100, Number(cpu) ? Number(cpu) * 12 : 18));
|
||||
const autostartText = (qsa('.top-meta .status').find((item) => item.textContent.toLowerCase().includes('autostart'))?.textContent || 'autostart: —').replace(/^autostart:\s*/i, '');
|
||||
box.innerHTML = `
|
||||
<div class="metric-strip">
|
||||
<div class="metric-tile"><span>Состояние</span><strong>${escapeHtml(state)}</strong></div>
|
||||
<div class="metric-tile"><span>CPU</span><strong>${escapeHtml(cpu)}</strong></div>
|
||||
<div class="metric-tile"><span>Тип ОС</span><strong>${escapeHtml(osType)}</strong></div>
|
||||
</div>
|
||||
<div class="chart-row">
|
||||
<div class="donut" style="--value:${memPct};--donut-color:var(--accent)"><div class="donut-value">${memPct}%</div></div>
|
||||
<div class="bar-list">
|
||||
<div class="bar-item"><div class="bar-head"><span>Память</span><b>${usedMem ? usedMem.toFixed(1) : '—'} / ${maxMem ? maxMem.toFixed(1) : '—'} GB</b></div><div class="mini-bar"><span style="--value:${memPct}%"></span></div></div>
|
||||
<div class="bar-item"><div class="bar-head"><span>vCPU capacity</span><b>${escapeHtml(cpu)} vCPU</b></div><div class="mini-bar"><span style="--value:${cpuPct}%"></span></div></div>
|
||||
<div class="bar-item"><div class="bar-head"><span>Автозапуск</span><b>${escapeHtml(autostartText)}</b></div><div class="mini-bar"><span style="--value:${/enable|включ/i.test(autostartText) ? 100 : 18}%"></span></div></div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
function parseDisks(raw) {
|
||||
const rows = [];
|
||||
raw.split('\n').map((line) => line.trim()).filter(Boolean).forEach((line) => {
|
||||
if (/^(target|type|[-\s]+$)/i.test(line)) return;
|
||||
const parts = line.split(/\s+/);
|
||||
if (parts.length >= 4 && /^(file|block|dir|network|volume)$/i.test(parts[0])) {
|
||||
rows.push({ type: parts[0], device: parts[1], target: parts[2], source: parts.slice(3).join(' ') });
|
||||
} else if (parts.length >= 2) {
|
||||
rows.push({ type: 'file', device: 'disk', target: parts[0], source: parts.slice(1).join(' ') });
|
||||
}
|
||||
});
|
||||
return rows;
|
||||
}
|
||||
function renderDisks() {
|
||||
const box = qs('#vm-disk-visual');
|
||||
if (!box) return;
|
||||
const rows = parseDisks(rawPreBySummary('domblklist'));
|
||||
if (!rows.length) {
|
||||
box.innerHTML = '<div class="visual-empty">Диски не найдены в выводе libvirt.</div>';
|
||||
return;
|
||||
}
|
||||
box.innerHTML = `<div class="visual-list">${rows.map((row) => `
|
||||
<div class="visual-item">
|
||||
<div class="visual-icon">▣</div>
|
||||
<div><b>${escapeHtml(row.target)}</b><small>${escapeHtml(row.device)} · ${escapeHtml(row.type)}<br>${escapeHtml(row.source)}</small></div>
|
||||
<span class="status ok">disk</span>
|
||||
</div>`).join('')}</div>`;
|
||||
}
|
||||
function parseInterfaces(raw) {
|
||||
const rows = [];
|
||||
raw.split('\n').map((line) => line.trim()).filter(Boolean).forEach((line) => {
|
||||
if (/^(interface|[-\s]+$)/i.test(line)) return;
|
||||
const parts = line.split(/\s+/);
|
||||
if (parts.length >= 5) rows.push({ iface: parts[0], type: parts[1], source: parts[2], model: parts[3], mac: parts[4] });
|
||||
else if (parts.length >= 2) rows.push({ iface: parts[0], type: parts[1] || '—', source: parts[2] || '—', model: parts[3] || '—', mac: parts[4] || '—' });
|
||||
});
|
||||
return rows;
|
||||
}
|
||||
function renderNetwork() {
|
||||
const box = qs('#vm-network-visual');
|
||||
if (!box) return;
|
||||
const rows = parseInterfaces(rawPreBySummary('domiflist'));
|
||||
if (!rows.length) {
|
||||
box.innerHTML = '<div class="visual-empty">Сетевые интерфейсы не найдены в выводе libvirt.</div>';
|
||||
return;
|
||||
}
|
||||
box.innerHTML = `<div class="visual-list">${rows.map((row) => `
|
||||
<div class="visual-item">
|
||||
<div class="visual-icon">⇄</div>
|
||||
<div><b>${escapeHtml(row.iface)}</b><small>${escapeHtml(row.type)} · ${escapeHtml(row.source)} · ${escapeHtml(row.model)}<br>${escapeHtml(row.mac)}</small></div>
|
||||
<span class="status ok">link</span>
|
||||
</div>`).join('')}</div>`;
|
||||
}
|
||||
function renderVmVisuals() {
|
||||
if (!qs('#vm-info-visual') && !qs('#vm-disk-visual') && !qs('#vm-network-visual')) return;
|
||||
renderInfo();
|
||||
renderDisks();
|
||||
renderNetwork();
|
||||
}
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', renderVmVisuals); else renderVmVisuals();
|
||||
})();
|
||||
|
||||
/* Virtuality live status + toast layer */
|
||||
(() => {
|
||||
if (window.VirtualityLiveStatus) return;
|
||||
window.VirtualityLiveStatus = true;
|
||||
function qs(s, r = document) { return r.querySelector(s); }
|
||||
function qsa(s, r = document) { return Array.from(r.querySelectorAll(s)); }
|
||||
function toast(message, type = 'ok') {
|
||||
/* ---------------------------------------------------- toasts */
|
||||
function toast(message, tone = 'success') {
|
||||
if (!message) return;
|
||||
let wrap = qs('#v-toast-wrap');
|
||||
if (!wrap) { wrap = document.createElement('div'); wrap.id = 'v-toast-wrap'; document.body.appendChild(wrap); }
|
||||
let wrap = $('#toasts');
|
||||
if (!wrap) { wrap = document.createElement('div'); wrap.id = 'toasts'; document.body.appendChild(wrap); }
|
||||
const icons = { success: 'circle-check', warning: 'triangle-alert', danger: 'circle-x', info: 'info' };
|
||||
const item = document.createElement('div');
|
||||
item.className = 'v-toast ' + type;
|
||||
item.textContent = message;
|
||||
item.className = `toast ${tone}`;
|
||||
item.setAttribute('role', 'status');
|
||||
item.innerHTML = `${icon(icons[tone] || 'info')}<div>${escapeHtml(message)}</div>`;
|
||||
wrap.appendChild(item);
|
||||
setTimeout(() => item.classList.add('show'), 20);
|
||||
setTimeout(() => { item.classList.remove('show'); setTimeout(() => item.remove(), 260); }, 3600);
|
||||
requestAnimationFrame(() => item.classList.add('show'));
|
||||
setTimeout(() => { item.classList.remove('show'); setTimeout(() => item.remove(), 250); }, 4200);
|
||||
}
|
||||
function vmNameFromRow(row) {
|
||||
const link = qs('a[href^="/vm/"]', row);
|
||||
if (!link) return '';
|
||||
try { const p = new URL(link.href, location.href).pathname.split('/').filter(Boolean); return p[0] === 'vm' ? decodeURIComponent(p[1] || '') : ''; } catch (_) { return ''; }
|
||||
}
|
||||
function stateClass(state) { const v = String(state || '').toLowerCase(); return v.includes('running') ? 'ok' : v.includes('shut') ? 'err' : 'warn'; }
|
||||
function updateDashboardRows(vms) {
|
||||
if (!Array.isArray(vms)) return;
|
||||
const map = new Map(vms.map((vm) => [vm.name, vm]));
|
||||
qsa('table.vm-table tbody tr').forEach((row) => {
|
||||
const name = vmNameFromRow(row);
|
||||
if (!name || !map.has(name)) return;
|
||||
const vm = map.get(name);
|
||||
const cells = qsa('td', row);
|
||||
if (cells[0]) cells[0].textContent = vm.id || '-';
|
||||
if (cells[2] && vm.ip && vm.ip !== '—' && vm.ip !== 'not available') cells[2].textContent = vm.ip;
|
||||
if (cells[3]) {
|
||||
let badge = qs('.status', cells[3]);
|
||||
if (!badge) { badge = document.createElement('span'); cells[3].textContent = ''; cells[3].appendChild(badge); }
|
||||
badge.className = 'status ' + (vm.state_css || stateClass(vm.state));
|
||||
badge.textContent = vm.state || 'unknown';
|
||||
}
|
||||
if (cells[4] && typeof vm.autostart_enabled !== 'undefined') {
|
||||
const button = qs('button.status-button', cells[4]);
|
||||
if (button) {
|
||||
button.textContent = vm.autostart_label || (vm.autostart_enabled ? 'enabled' : 'disabled');
|
||||
button.className = 'status status-button ' + (vm.autostart_enabled ? 'ok' : 'err');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
function updateDetailHeader(vms) {
|
||||
const title = qs('.v-title');
|
||||
if (!title || !Array.isArray(vms)) return;
|
||||
const vm = vms.find((item) => item.name === title.textContent.trim());
|
||||
if (!vm) return;
|
||||
let badge = qs('#vm-live-badge');
|
||||
if (!badge) { badge = document.createElement('span'); badge.id = 'vm-live-badge'; title.insertAdjacentElement('afterend', badge); }
|
||||
badge.className = 'status live-badge ' + (vm.state_css || stateClass(vm.state));
|
||||
badge.textContent = vm.state || 'unknown';
|
||||
}
|
||||
async function refreshLiveStatus() {
|
||||
if (document.hidden || !qs('.v-main')) return;
|
||||
window.vToast = toast;
|
||||
function flashToast() {
|
||||
try {
|
||||
const r = await fetch('/live/status', { cache: 'no-store', headers: { 'Accept': 'application/json' }});
|
||||
if (!r.ok) return;
|
||||
const p = await r.json();
|
||||
if (!p.ok) return;
|
||||
updateDashboardRows(p.vms);
|
||||
updateDetailHeader(p.vms);
|
||||
const raw = sessionStorage.getItem('virtualityToast');
|
||||
if (!raw) return;
|
||||
sessionStorage.removeItem('virtualityToast');
|
||||
const data = JSON.parse(raw);
|
||||
toast(data.message, data.tone);
|
||||
} catch (_) {}
|
||||
}
|
||||
document.addEventListener('submit', (event) => {
|
||||
const form = event.target;
|
||||
if (!form || !form.action) return;
|
||||
const parts = new URL(form.action, location.href).pathname.split('/').filter(Boolean);
|
||||
if (parts[0] !== 'vm' || parts.length < 3) return;
|
||||
const labels = { start: 'Команда запуска VM отправлена', shutdown: 'Команда мягкого выключения VM отправлена', reboot: 'Команда перезагрузки VM отправлена', destroy: 'Команда принудительного выключения VM отправлена', autostart: 'Автозапуск VM включается', 'autostart-disable': 'Автозапуск VM отключается', delete: 'Удаление VM запущено' };
|
||||
const action = parts[2];
|
||||
if (labels[action]) { const type = action === 'destroy' || action === 'delete' ? 'warn' : 'ok'; sessionStorage.setItem('virtualityToast', labels[action]); toast(labels[action], type); setTimeout(refreshLiveStatus, 900); setTimeout(refreshLiveStatus, 2500); }
|
||||
}, true);
|
||||
function boot() { const m = sessionStorage.getItem('virtualityToast'); if (m) { sessionStorage.removeItem('virtualityToast'); toast(m, 'ok'); } refreshLiveStatus(); setInterval(refreshLiveStatus, 5000); }
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot); else boot();
|
||||
|
||||
/* ---------------------------------------------------- navigation & menus */
|
||||
function wireNav() {
|
||||
$$('[data-nav-toggle]').forEach((btn) => btn.addEventListener('click', () => document.body.classList.toggle('nav-open')));
|
||||
document.addEventListener('click', (event) => {
|
||||
if (document.body.classList.contains('nav-open') && !event.target.closest('.sidebar, [data-nav-toggle]')) document.body.classList.remove('nav-open');
|
||||
$$('details.menu[open]').forEach((menu) => { if (!menu.contains(event.target)) menu.removeAttribute('open'); });
|
||||
});
|
||||
document.addEventListener('keydown', (event) => {
|
||||
if (event.key !== 'Escape') return;
|
||||
document.body.classList.remove('nav-open');
|
||||
$$('details.menu[open]').forEach((menu) => menu.removeAttribute('open'));
|
||||
});
|
||||
$$('details.menu').forEach((menu) => menu.addEventListener('toggle', () => {
|
||||
if (menu.open) $$('details.menu[open]').forEach((other) => { if (other !== menu) other.removeAttribute('open'); });
|
||||
}));
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- forms: confirm, busy, toast */
|
||||
function confirmDialog(form) {
|
||||
return new Promise((resolve) => {
|
||||
const dialog = $('#confirm-dialog');
|
||||
if (!dialog || typeof dialog.showModal !== 'function') { resolve(window.confirm(form.dataset.confirm)); return; }
|
||||
const danger = form.dataset.confirmTone !== 'brand';
|
||||
$('[data-confirm-title]', dialog).textContent = form.dataset.confirmTitle || 'Подтвердите действие';
|
||||
$('[data-confirm-text]', dialog).textContent = form.dataset.confirm;
|
||||
const ok = $('[data-confirm-ok]', dialog);
|
||||
ok.textContent = form.dataset.confirmButton || 'Продолжить';
|
||||
ok.className = `btn ${danger ? 'btn-danger' : 'btn-primary'}`;
|
||||
$('.dialog-icon', dialog).className = `dialog-icon ${danger ? '' : 'brand'}`;
|
||||
const done = (value) => { dialog.close(); ok.removeEventListener('click', onOk); dialog.removeEventListener('close', onClose); resolve(value); };
|
||||
const onOk = () => done(true);
|
||||
const onClose = () => resolve(false);
|
||||
ok.addEventListener('click', onOk);
|
||||
dialog.addEventListener('close', onClose, { once: true });
|
||||
dialog.showModal();
|
||||
});
|
||||
}
|
||||
function markBusy(form, submitter) {
|
||||
const button = submitter || $('button[type=submit], button:not([type])', form);
|
||||
if (!button || form.dataset.noBusy !== undefined) return;
|
||||
button.disabled = true;
|
||||
if (!button.classList.contains('switch') && !button.classList.contains('menu-item')) {
|
||||
button.dataset.label = button.innerHTML;
|
||||
button.innerHTML = `<span class="spinner"></span>${escapeHtml(button.dataset.busy || button.textContent.trim())}`;
|
||||
}
|
||||
}
|
||||
function wireForms() {
|
||||
document.addEventListener('submit', async (event) => {
|
||||
const form = event.target;
|
||||
if (!(form instanceof HTMLFormElement) || form.dataset.uploader !== undefined) return;
|
||||
if (form.dataset.confirm && form.dataset.confirmed !== '1') {
|
||||
event.preventDefault();
|
||||
const submitter = event.submitter;
|
||||
if (await confirmDialog(form)) {
|
||||
form.dataset.confirmed = '1';
|
||||
if (form.dataset.toast) sessionStorage.setItem('virtualityToast', JSON.stringify({ message: form.dataset.toast, tone: form.dataset.toastTone || 'success' }));
|
||||
markBusy(form, submitter);
|
||||
form.submit();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (form.dataset.toast) sessionStorage.setItem('virtualityToast', JSON.stringify({ message: form.dataset.toast, tone: form.dataset.toastTone || 'success' }));
|
||||
const submitter = event.submitter;
|
||||
setTimeout(() => markBusy(form, submitter), 0);
|
||||
});
|
||||
window.addEventListener('pageshow', (event) => {
|
||||
if (!event.persisted) return;
|
||||
$$('button[data-label]').forEach((btn) => { btn.innerHTML = btn.dataset.label; btn.disabled = false; });
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- tabs */
|
||||
function wireTabs() {
|
||||
$$('[data-tabs]').forEach((tabs) => {
|
||||
const buttons = $$('[data-tab]', tabs);
|
||||
const select = (id, push) => {
|
||||
buttons.forEach((btn) => btn.setAttribute('aria-selected', String(btn.dataset.tab === id)));
|
||||
$$('[data-tab-panel]').forEach((panel) => { panel.hidden = panel.dataset.tabPanel !== id; });
|
||||
if (push) history.replaceState(null, '', `#${id}`);
|
||||
};
|
||||
buttons.forEach((btn) => btn.addEventListener('click', () => select(btn.dataset.tab, true)));
|
||||
const initial = location.hash.slice(1);
|
||||
select(buttons.some((btn) => btn.dataset.tab === initial) ? initial : buttons[0]?.dataset.tab, false);
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- copy */
|
||||
function wireCopy() {
|
||||
document.addEventListener('click', async (event) => {
|
||||
const btn = event.target.closest('[data-copy]');
|
||||
if (!btn) return;
|
||||
try { await navigator.clipboard.writeText(btn.dataset.copy); toast('Скопировано'); } catch (_) { toast('Не удалось скопировать', 'warning'); }
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- live VM status */
|
||||
function applyVmState(vm) {
|
||||
$$(`[data-live-vm="${CSS.escape(vm.name)}"]`).forEach((root) => {
|
||||
const [label, tone] = vmState(vm.state);
|
||||
$$('[data-live="badge"]', root).forEach((el) => { el.className = `badge ${tone}${el.classList.contains('lg') ? ' lg' : ''}`; el.innerHTML = badge([label, tone]); });
|
||||
$$('[data-live="dot"]', root).forEach((el) => { el.className = `status-dot ${tone}`; });
|
||||
$$('[data-live="ip"]', root).forEach((el) => { if (vm.ip && vm.ip !== '—') el.textContent = vm.ip; });
|
||||
const wasRunning = root.dataset.running === '1';
|
||||
const nowRunning = tone === 'success';
|
||||
if (root.dataset.reloadOnChange !== undefined && wasRunning !== nowRunning && !document.querySelector('details.menu[open]')) location.reload();
|
||||
});
|
||||
}
|
||||
async function refreshLive() {
|
||||
if (document.hidden) return;
|
||||
try {
|
||||
const response = await fetch('/live/status', { cache: 'no-store', headers: { Accept: 'application/json' } });
|
||||
if (!response.ok) return;
|
||||
const payload = await response.json();
|
||||
if (!payload.ok) return;
|
||||
payload.vms.forEach(applyVmState);
|
||||
const running = payload.vms.filter((vm) => vmState(vm.state)[1] === 'success').length;
|
||||
$$('[data-live-count="running"]').forEach((el) => { el.textContent = running; });
|
||||
} catch (_) {}
|
||||
}
|
||||
function wireLive() {
|
||||
if (!$('[data-live-vm]')) return;
|
||||
setInterval(refreshLive, 5000);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- uploads */
|
||||
function wireUploader(form) {
|
||||
const input = $('input[type=file]', form);
|
||||
const zone = $('.dropzone', form);
|
||||
const fileBox = $('[data-upload-file]', form);
|
||||
const nameEl = $('[data-upload-name]', form);
|
||||
const sizeEl = $('[data-upload-size]', form);
|
||||
const bar = $('[data-upload-progress] > span', form);
|
||||
const progress = $('[data-upload-progress]', form);
|
||||
const statusEl = $('[data-upload-status]', form);
|
||||
const statsEl = $('[data-upload-stats]', form);
|
||||
const errorEl = $('[data-upload-error]', form);
|
||||
const submit = $('[data-upload-submit]', form);
|
||||
const cancel = $('[data-upload-cancel]', form);
|
||||
const accept = (form.dataset.accept || '').split(',').map((item) => item.trim().toLowerCase()).filter(Boolean);
|
||||
let xhr = null;
|
||||
|
||||
const valid = (file) => file && (!accept.length || accept.some((ext) => file.name.toLowerCase().endsWith(ext)));
|
||||
const setError = (message) => { errorEl.hidden = !message; $('[data-upload-error-text]', errorEl).textContent = message || ''; };
|
||||
const setActive = (active) => {
|
||||
window.VirtualityUploadActive = active;
|
||||
submit.hidden = active; cancel.hidden = !active; input.disabled = active;
|
||||
zone.hidden = active || Boolean(input.files[0]);
|
||||
};
|
||||
const showFile = () => {
|
||||
const file = input.files[0];
|
||||
setError('');
|
||||
if (!file) { fileBox.hidden = true; zone.hidden = false; submit.disabled = true; return; }
|
||||
if (!valid(file)) {
|
||||
setError(`Файл «${file.name}» не подходит. Допустимые форматы: ${accept.join(', ')}`);
|
||||
input.value = ''; fileBox.hidden = true; zone.hidden = false; submit.disabled = true; return;
|
||||
}
|
||||
nameEl.textContent = file.name; sizeEl.textContent = bytes(file.size);
|
||||
fileBox.hidden = false; zone.hidden = true; progress.hidden = true; statsEl.textContent = '';
|
||||
statusEl.textContent = 'Готов к загрузке'; submit.disabled = false;
|
||||
};
|
||||
|
||||
input.addEventListener('change', showFile);
|
||||
['dragenter', 'dragover'].forEach((type) => zone.addEventListener(type, (e) => { e.preventDefault(); zone.classList.add('dragover'); }));
|
||||
['dragleave', 'drop'].forEach((type) => zone.addEventListener(type, () => zone.classList.remove('dragover')));
|
||||
zone.addEventListener('drop', (e) => { e.preventDefault(); if (e.dataTransfer.files.length) { input.files = e.dataTransfer.files; showFile(); } });
|
||||
$('[data-upload-clear]', form)?.addEventListener('click', () => { input.value = ''; showFile(); });
|
||||
cancel.addEventListener('click', () => xhr && xhr.abort());
|
||||
window.addEventListener('beforeunload', (event) => { if (window.VirtualityUploadActive) { event.preventDefault(); event.returnValue = ''; } });
|
||||
|
||||
async function pollOperation(id) {
|
||||
statusEl.textContent = 'Файл загружен. Готовим образ…';
|
||||
bar.style.width = '0%';
|
||||
for (;;) {
|
||||
await new Promise((r) => setTimeout(r, 1200));
|
||||
try {
|
||||
const response = await fetch(`/api/operations/${id}`, { cache: 'no-store' });
|
||||
const data = await response.json();
|
||||
const op = data.operation || {};
|
||||
bar.style.width = `${op.progress || 0}%`;
|
||||
statusEl.textContent = `Конвертация в формат qcow2 — ${op.progress || 0}%`;
|
||||
if (op.status === 'success') return true;
|
||||
if (op.status === 'error') { setError(op.message || 'Ошибка конвертации'); return false; }
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
form.addEventListener('submit', (event) => {
|
||||
event.preventDefault();
|
||||
const file = input.files[0];
|
||||
if (!valid(file)) { setError('Выберите файл'); return; }
|
||||
const started = Date.now();
|
||||
xhr = new XMLHttpRequest();
|
||||
setActive(true); progress.hidden = false; progress.classList.remove('success', 'danger');
|
||||
statusEl.textContent = 'Загрузка на сервер…';
|
||||
xhr.upload.onprogress = (e) => {
|
||||
if (!e.lengthComputable) return;
|
||||
const pct = Math.round((e.loaded / e.total) * 100);
|
||||
const speed = e.loaded / Math.max(1, (Date.now() - started) / 1000);
|
||||
const left = speed > 0 ? Math.round((e.total - e.loaded) / speed) : 0;
|
||||
bar.style.width = `${pct}%`;
|
||||
statsEl.textContent = `${bytes(e.loaded)} из ${bytes(e.total)} · ${bytes(speed)}/с${left > 2 ? ` · осталось ~${left > 90 ? Math.round(left / 60) + ' мин' : left + ' с'}` : ''}`;
|
||||
statusEl.textContent = pct >= 100 ? 'Сохраняем файл на сервере…' : `Загрузка на сервер — ${pct}%`;
|
||||
};
|
||||
xhr.onload = async () => {
|
||||
if (xhr.status >= 200 && xhr.status < 400) {
|
||||
let payload = null;
|
||||
try { payload = JSON.parse(xhr.responseText); } catch (_) {}
|
||||
if (payload && payload.mode === 'converting' && payload.operation_id) {
|
||||
const ok = await pollOperation(payload.operation_id);
|
||||
window.VirtualityUploadActive = false;
|
||||
if (!ok) { setActive(false); progress.classList.add('danger'); return; }
|
||||
}
|
||||
window.VirtualityUploadActive = false;
|
||||
progress.classList.add('success'); bar.style.width = '100%'; statusEl.textContent = 'Готово';
|
||||
sessionStorage.setItem('virtualityToast', JSON.stringify({ message: `Файл ${file.name} загружен`, tone: 'success' }));
|
||||
location.href = form.dataset.redirect || location.pathname;
|
||||
} else {
|
||||
setActive(false); progress.classList.add('danger');
|
||||
const doc = new DOMParser().parseFromString(xhr.responseText || '', 'text/html');
|
||||
setError(doc.querySelector('[data-error-message]')?.textContent.trim() || `Сервер ответил ошибкой (${xhr.status})`);
|
||||
statusEl.textContent = 'Загрузка не удалась';
|
||||
}
|
||||
};
|
||||
xhr.onerror = () => { setActive(false); setError('Связь с сервером прервалась. Попробуйте ещё раз.'); };
|
||||
xhr.onabort = () => { setActive(false); statusEl.textContent = 'Загрузка отменена'; bar.style.width = '0%'; statsEl.textContent = ''; };
|
||||
xhr.open('POST', form.action);
|
||||
xhr.setRequestHeader('Accept', 'application/json');
|
||||
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
||||
xhr.send(new FormData(form));
|
||||
});
|
||||
showFile();
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- operation page */
|
||||
function wireOperation() {
|
||||
const root = $('[data-operation-id]');
|
||||
if (!root) return;
|
||||
const id = root.dataset.operationId;
|
||||
const statusEl = $('[data-op="status"]', root);
|
||||
const bar = $('[data-op="bar"]', root);
|
||||
const progress = $('[data-op="progress"]', root);
|
||||
const pctEl = $('[data-op="pct"]', root);
|
||||
const messageEl = $('[data-op="message"]', root);
|
||||
const logEl = $('[data-op="log"]');
|
||||
let shown = Number(root.dataset.progress || 0);
|
||||
let status = root.dataset.status;
|
||||
const done = () => status === 'success' || status === 'error';
|
||||
const render = () => {
|
||||
bar.style.width = `${Math.round(shown)}%`;
|
||||
pctEl.textContent = `${Math.round(shown)}%`;
|
||||
progress.classList.toggle('success', status === 'success');
|
||||
progress.classList.toggle('danger', status === 'error');
|
||||
};
|
||||
const tick = setInterval(() => {
|
||||
if (done()) return;
|
||||
shown = Math.min(status === 'queued' ? 10 : 95, shown + (shown > 80 ? 0.15 : 0.5));
|
||||
render();
|
||||
}, 1000);
|
||||
const poll = async () => {
|
||||
try {
|
||||
const response = await fetch(`/api/operations/${id}`, { cache: 'no-store' });
|
||||
const data = await response.json();
|
||||
if (!data.ok) return;
|
||||
const op = data.operation;
|
||||
const wasDone = done();
|
||||
status = op.status;
|
||||
shown = status === 'success' ? 100 : Math.max(shown, Number(op.progress || 0));
|
||||
const [label, tone] = opState(status);
|
||||
statusEl.className = `badge lg ${tone}`; statusEl.innerHTML = badge([label, tone]);
|
||||
messageEl.textContent = op.message || '';
|
||||
if (logEl) { const atBottom = logEl.scrollHeight - logEl.scrollTop - logEl.clientHeight < 40; logEl.textContent = op.log_tail || ''; if (atBottom) logEl.scrollTop = logEl.scrollHeight; }
|
||||
render();
|
||||
if (done()) { clearInterval(timer); clearInterval(tick); if (!wasDone) { toast(status === 'success' ? 'Операция завершена' : 'Операция завершилась с ошибкой', status === 'success' ? 'success' : 'danger'); $$('[data-op-done]').forEach((el) => { el.hidden = false; }); } }
|
||||
} catch (_) {}
|
||||
};
|
||||
const timer = setInterval(poll, 1500);
|
||||
render(); poll();
|
||||
if (logEl) logEl.scrollTop = logEl.scrollHeight;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- logs & update pollers */
|
||||
function wireLogs() {
|
||||
const box = $('[data-log-source]');
|
||||
if (!box) return;
|
||||
const refresh = async () => {
|
||||
if (document.hidden || !$('[data-log-live]')?.checked) return;
|
||||
try {
|
||||
const response = await fetch(`/api/logs?source=${encodeURIComponent(box.dataset.logSource)}&lines=${encodeURIComponent(box.dataset.logLines)}`, { cache: 'no-store' });
|
||||
const data = await response.json();
|
||||
if (!data.ok) return;
|
||||
const atBottom = box.scrollHeight - box.scrollTop - box.clientHeight < 40;
|
||||
box.textContent = data.log.content || 'Журнал пуст';
|
||||
if (atBottom) box.scrollTop = box.scrollHeight;
|
||||
} catch (_) {}
|
||||
};
|
||||
box.scrollTop = box.scrollHeight;
|
||||
setInterval(refresh, 4000);
|
||||
}
|
||||
function wireUpdate() {
|
||||
const logBox = $('[data-update-log]');
|
||||
if (!logBox) return;
|
||||
const stateEl = $('[data-update-state]');
|
||||
const poll = async () => {
|
||||
try {
|
||||
const response = await fetch('/update/status', { cache: 'no-store' });
|
||||
const data = await response.json();
|
||||
if (!data.ok) return;
|
||||
logBox.textContent = data.log_tail || 'Журнал пока пуст.';
|
||||
const state = data.state || {};
|
||||
if (stateEl) stateEl.textContent = state.message || '';
|
||||
if (state.status === 'running') { $$('[data-update-running]').forEach((el) => { el.hidden = false; }); }
|
||||
} catch (_) {}
|
||||
};
|
||||
poll();
|
||||
setInterval(poll, 2500);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- VM wizard */
|
||||
function wireWizard() {
|
||||
const form = $('form[data-wizard]');
|
||||
if (!form) return;
|
||||
const field = (name) => form.elements[name];
|
||||
const value = (name) => { const el = field(name); if (!el) return ''; if (el instanceof RadioNodeList) return el.value; return el.value; };
|
||||
const setSummary = (key, text) => $$(`[data-summary="${key}"]`).forEach((el) => { el.textContent = text || '—'; });
|
||||
const optionText = (select) => select && select.selectedIndex >= 0 ? select.options[select.selectedIndex].dataset.label || select.options[select.selectedIndex].text : '';
|
||||
|
||||
function sync() {
|
||||
const source = value('source_type');
|
||||
$$('[data-show-when]', form).forEach((el) => {
|
||||
const [key, expected] = el.dataset.showWhen.split('=');
|
||||
const visible = value(key) === expected;
|
||||
el.hidden = !visible;
|
||||
$$('input, select', el).forEach((input) => { input.disabled = !visible; });
|
||||
});
|
||||
$$('[data-visible-when]', form).forEach((el) => {
|
||||
const [key, expected] = el.dataset.visibleWhen.split('=');
|
||||
el.hidden = value(key) !== expected;
|
||||
});
|
||||
const preset = value('preset');
|
||||
const custom = preset === 'custom';
|
||||
const presetInput = $(`input[name=preset][value="${preset}"]`, form);
|
||||
if (!custom && presetInput) {
|
||||
field('vcpus').value = presetInput.dataset.vcpus;
|
||||
field('memory').value = presetInput.dataset.memory;
|
||||
field('disk_size').value = presetInput.dataset.disk;
|
||||
}
|
||||
$$('[data-custom-resources] input', form).forEach((input) => { input.readOnly = !custom; });
|
||||
const memory = Number(value('memory')) || 0;
|
||||
setSummary('name', value('name'));
|
||||
setSummary('source', source === 'disk_image' ? optionText(field('disk_image_path')) : optionText(field('iso_path')));
|
||||
setSummary('cpu', `${value('vcpus')} ${Number(value('vcpus')) === 1 ? 'ядро' : 'ядра'}`);
|
||||
setSummary('memory', memory >= 1024 ? `${+(memory / 1024).toFixed(1)} ГБ` : `${memory} МБ`);
|
||||
setSummary('disk', source === 'disk_image' ? 'из образа' : `${value('disk_size')} ГБ`);
|
||||
setSummary('network', value('network_mode') === 'bridge' ? 'Локальная сеть' : 'Автоматически (NAT)');
|
||||
const hasSource = source === 'disk_image' ? form.dataset.hasDisks === '1' : form.dataset.hasIsos === '1';
|
||||
$$('[data-create-button]').forEach((btn) => { btn.disabled = !hasSource || !value('name'); });
|
||||
$$('[data-missing-source]').forEach((el) => { el.hidden = hasSource || el.dataset.missingSource !== source; });
|
||||
}
|
||||
form.addEventListener('input', sync);
|
||||
form.addEventListener('change', sync);
|
||||
sync();
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- boot order list */
|
||||
function wireBootOrder() {
|
||||
const list = $('[data-boot-list]');
|
||||
if (!list) return;
|
||||
const input = $('[data-boot-value]');
|
||||
const orders = { cdrom_disk: ['cdrom', 'hd', 'network'], disk_cdrom: ['hd', 'cdrom', 'network'], network_disk: ['network', 'hd', 'cdrom'], disk: ['hd', 'cdrom', 'network'], auto: ['hd', 'cdrom', 'network'] };
|
||||
(orders[list.dataset.current] || orders.auto).forEach((device) => { const node = $(`[data-device="${device}"]`, list); if (node) list.appendChild(node); });
|
||||
const update = () => {
|
||||
const devices = $$('[data-device]', list).map((item) => item.dataset.device);
|
||||
const [first, second] = devices;
|
||||
input.value = first === 'cdrom' && second === 'hd' ? 'cdrom_disk' : first === 'hd' && second === 'cdrom' ? 'disk_cdrom' : first === 'network' && second === 'hd' ? 'network_disk' : first === 'hd' ? 'disk' : 'auto';
|
||||
$$('[data-device]', list).forEach((item, index) => { $('[data-position]', item).textContent = index + 1; });
|
||||
};
|
||||
list.addEventListener('click', (event) => {
|
||||
const btn = event.target.closest('[data-move]');
|
||||
if (!btn) return;
|
||||
const item = btn.closest('[data-device]');
|
||||
if (btn.dataset.move === 'up' && item.previousElementSibling) list.insertBefore(item, item.previousElementSibling);
|
||||
if (btn.dataset.move === 'down' && item.nextElementSibling) list.insertBefore(item.nextElementSibling, item);
|
||||
update();
|
||||
});
|
||||
update();
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
wireTheme();
|
||||
wireNav();
|
||||
wireForms();
|
||||
wireTabs();
|
||||
wireCopy();
|
||||
wireLive();
|
||||
$$('form[data-uploader]').forEach(wireUploader);
|
||||
wireOperation();
|
||||
wireLogs();
|
||||
wireUpdate();
|
||||
wireWizard();
|
||||
wireBootOrder();
|
||||
flashToast();
|
||||
});
|
||||
})();
|
||||
|
||||
Ссылка в новой задаче
Block a user