Spaces:
Paused
Paused
Download public/client.js from av4x03/server-admin: direct link, hf CLI and curl.
- Browser
- Download file 24.7 kB
-
https://huggingface.co/spaces/av4x03/server-admin/resolve/main/public/client.js
- Command line
-
hf download hf://spaces/av4x03/server-admin/public/client.js
-
curl -L -o client.js https://huggingface.co/spaces/av4x03/server-admin/resolve/main/public/client.js
24.7 kB
| // server-admin/public/client.js | |
| const fitAddon = new FitAddon.FitAddon(); | |
| const term = new Terminal({ | |
| theme: { | |
| background: 'transparent', | |
| foreground: '#e6eef2', | |
| cursor: '#00d084', | |
| selection: 'rgba(0, 208, 132, 0.3)', | |
| black: '#0b0b0c', red: '#ff7b7b', green: '#7ef3b2', yellow: '#ffd27a', | |
| blue: '#75d1ff', magenta: '#c792ea', cyan: '#89ddff', white: '#e6eef2', | |
| brightBlack: '#8b8f92', brightRed: '#ff9e9e', brightGreen: '#a1f8cd', | |
| brightYellow: '#ffe5a8', brightBlue: '#a4e1ff', brightMagenta: '#e4b9ff', | |
| brightCyan: '#b8edff', brightWhite: '#ffffff', | |
| }, | |
| fontSize: 14, | |
| fontFamily: 'Menlo, "DejaVu Sans Mono", Consolas, "Lucida Console", monospace', | |
| cursorBlink: true, cursorStyle: 'block', allowTransparency: true, | |
| windowsMode: false, scrollback: 1000, | |
| }); | |
| term.loadAddon(fitAddon); | |
| term.open(document.getElementById('terminal')); | |
| fitAddon.fit(); | |
| function resizeTerminal() { | |
| setTimeout(() => { | |
| try { fitAddon.fit(); } catch (e) { } | |
| if (currentSocket && activeTerminalSessionId) { | |
| currentSocket.emit('resize', { sessionId: activeTerminalSessionId, cols: term.cols, rows: term.rows }); | |
| } | |
| }, 0); | |
| } | |
| window.addEventListener('resize', resizeTerminal); | |
| const ICONS = ['fas fa-server', 'fas fa-database', 'fas fa-network-wired', 'fas fa-laptop-code']; | |
| const SERVER_STORAGE_KEY = 'admin-servers-list'; | |
| const LOCAL_SERVICES = [ | |
| { | |
| uid: 'internal-system', | |
| name: 'System Status', | |
| description: 'Giám sát tài nguyên admin', | |
| isLocal: true, | |
| icon: 'fas fa-tachometer-alt' | |
| }, | |
| { | |
| uid: 'internal-uptime', | |
| name: 'Uptime Monitor', | |
| description: 'Giám sát trạng thái website', | |
| isLocal: true, | |
| icon: 'fas fa-heartbeat' | |
| } | |
| ]; | |
| const HARDCODED_SERVERS = [ | |
| { | |
| uid: 'hardcoded-1', name: 'Terminal-v1', url: 'https://av4x03-server-terminal-v1.hf.space', | |
| description: 'Server-Terminal 🚀', | |
| isHardcoded: true | |
| }, | |
| { | |
| uid: 'hardcoded-2', name: 'Terminal-v2', url: 'https://av4x04-server-terminal-v2.hf.space', | |
| description: 'Server-Terminal 🚀', | |
| isHardcoded: true | |
| }, | |
| { | |
| uid: 'hardcoded-3', name: 'Terminal-v3', url: 'https://av4x04-server-terminal-v3.hf.space', | |
| description: 'Server-Terminal 🚀', | |
| isHardcoded: true | |
| } | |
| ]; | |
| let adminSocket; // Socket for admin panel features like Uptime | |
| let currentSocket = null; | |
| let activeServerUid = null; | |
| let allServices = []; // Combined list of local services and remote servers | |
| // Expose để hàm openPort() trong HTML có thể đọc | |
| Object.defineProperty(window, '_activeServerUid', { get: () => activeServerUid }); | |
| Object.defineProperty(window, '_allServices', { get: () => allServices }); | |
| let userServers = []; | |
| const resettingServers = {}; // State to track resetting servers: { [uid]: endTime } | |
| // Terminal multi-session state | |
| let terminalSessions = new Map(); | |
| let activeTerminalSessionId = null; | |
| // UI Elements | |
| const statusText = document.getElementById('status-text'); | |
| const serverListContainer = document.getElementById('server-list'); | |
| const modalOverlay = document.getElementById('modal-overlay'); | |
| const serverForm = document.getElementById('server-form'); | |
| const terminalLoader = document.getElementById('terminal-loader'); | |
| const loaderAscii = document.getElementById('loader-ascii'); | |
| const loaderText = document.getElementById('loader-text'); | |
| const terminalTabsContainer = document.getElementById('terminal-tabs-container'); | |
| // Views | |
| const allViews = document.querySelectorAll('.view-container'); | |
| const terminalView = document.getElementById('terminal-view'); | |
| const uptimeView = document.getElementById('uptime-view'); | |
| const systemView = document.getElementById('system-view'); | |
| function switchToView(viewId) { | |
| allViews.forEach(view => { | |
| view.style.display = view.id === viewId ? 'flex' : 'none'; | |
| }); | |
| } | |
| function loadServers() { | |
| const storedUserServers = localStorage.getItem(SERVER_STORAGE_KEY); | |
| try { | |
| userServers = storedUserServers ? JSON.parse(storedUserServers) : []; | |
| } catch (e) { | |
| console.error("Error parsing user servers from localStorage", e); | |
| userServers = []; | |
| } | |
| allServices = [...LOCAL_SERVICES, ...HARDCODED_SERVERS, ...userServers]; | |
| } | |
| function saveServers() { | |
| localStorage.setItem(SERVER_STORAGE_KEY, JSON.stringify(userServers)); | |
| } | |
| function renderServerList() { | |
| serverListContainer.innerHTML = ''; | |
| allServices.forEach((service, index) => { | |
| const iconClass = service.icon || ICONS[index % ICONS.length]; | |
| const serverElement = document.createElement('div'); | |
| serverElement.className = 'tab-item'; | |
| if (service.uid === activeServerUid) serverElement.classList.add('active'); | |
| serverElement.dataset.uid = service.uid; | |
| let actionsHtml = !service.isLocal ? ` | |
| <div class="tab-actions"> | |
| <button class="options-btn" title="Tùy chọn"><i class="fas fa-ellipsis-v"></i></button> | |
| <div class="options-menu"> | |
| <a href="#" class="reset-btn"><i class="fas fa-sync-alt"></i> Reset</a> | |
| ${!service.isHardcoded ? `<a href="#" class="delete-btn delete"><i class="fas fa-trash-alt"></i> Xóa</a>` : ''} | |
| </div> | |
| </div>` : ''; | |
| serverElement.innerHTML = ` | |
| <div class="icon-circle"><i class="${iconClass}"></i></div> | |
| <div class="tab-meta"> | |
| <div class="tab-name">${service.name}</div> | |
| <div class="tab-sub">${service.description || service.url}</div> | |
| </div> | |
| ${actionsHtml}`; | |
| serverElement.addEventListener('click', (e) => { | |
| if (!e.target.closest('.tab-actions')) selectServer(service); | |
| }); | |
| if (!service.isLocal) { | |
| const optionsBtn = serverElement.querySelector('.options-btn'); | |
| optionsBtn.addEventListener('click', e => { | |
| e.stopPropagation(); | |
| document.querySelectorAll('.options-menu.show').forEach(m => m.classList.remove('show')); | |
| optionsBtn.nextElementSibling.classList.toggle('show'); | |
| }); | |
| serverElement.querySelector('.reset-btn').addEventListener('click', e => { | |
| e.stopPropagation(); | |
| handleReset(service); | |
| }); | |
| if (!service.isHardcoded) { | |
| serverElement.querySelector('.delete-btn').addEventListener('click', e => { | |
| e.stopPropagation(); | |
| handleDelete(service); | |
| }); | |
| } | |
| } | |
| serverListContainer.appendChild(serverElement); | |
| }); | |
| } | |
| function selectServer(service) { | |
| if (activeServerUid === service.uid) return; | |
| activeServerUid = service.uid; | |
| if (currentSocket) currentSocket.disconnect(); | |
| currentSocket = null; | |
| terminalSessions.clear(); | |
| activeTerminalSessionId = null; | |
| renderTerminalTabs(); | |
| renderServerList(); | |
| if (service.isLocal) { | |
| terminalLoader.classList.add('hidden'); | |
| if (service.uid === 'internal-uptime') { | |
| switchToView('uptime-view'); | |
| statusText.textContent = 'Uptime Monitor'; | |
| initializeUptimeMonitor(); | |
| } else if (service.uid === 'internal-system') { | |
| switchToView('system-view'); | |
| statusText.textContent = 'System Status'; | |
| if (adminSocket && adminSocket.connected) { | |
| adminSocket.emit('system:subscribe'); | |
| } | |
| } | |
| } else { | |
| switchToView('terminal-view'); | |
| connectToTerminalServer(service); | |
| } | |
| } | |
| // Global Port Opener (From previous turn, still applies) | |
| window.openPort = function () { | |
| const portInput = document.getElementById('port-input'); | |
| const port = portInput.value.trim(); | |
| if (!port) { | |
| alert('Vui lòng nhập số cổng (VD: 3000).'); | |
| return; | |
| } | |
| const service = allServices.find(s => s.uid === activeServerUid); | |
| if (service && !service.isLocal && service.url) { | |
| window.open(`${service.url.replace(/\/+$/, '')}/p/${port}/`, '_blank'); | |
| } else { | |
| alert('Vui lòng chọn một Server Terminal để mở cổng.'); | |
| } | |
| }; | |
| function showConnectionAnimation(server) { | |
| term.reset(); | |
| terminalLoader.classList.remove('hidden'); | |
| loaderAscii.textContent = '\n(>_<)\n\n'; | |
| loaderText.textContent = `Đang kết nối đến ${server.name}...`; | |
| } | |
| function showResettingOverlay(server, duration) { | |
| if (currentSocket && currentSocket.connected) currentSocket.disconnect(); | |
| currentSocket = null; | |
| term.reset(); | |
| switchToView('terminal-view'); | |
| terminalLoader.classList.remove('hidden'); | |
| loaderAscii.textContent = '\n(>_<)\n\n'; | |
| loaderText.textContent = 'Server đang khởi động lại. Chờ 3 phút...'; | |
| statusText.textContent = `Đang reset: ${server.name}`; | |
| document.getElementById('terminal-title').textContent = server.name; | |
| setTimeout(() => { | |
| delete resettingServers[server.uid]; | |
| if (activeServerUid === server.uid) selectServer(server); | |
| }, duration); | |
| } | |
| function connectToTerminalServer(server) { | |
| const resetEndTime = resettingServers[server.uid]; | |
| if (resetEndTime && Date.now() < resetEndTime) { | |
| showResettingOverlay(server, resetEndTime - Date.now()); | |
| return; | |
| } | |
| statusText.textContent = `Đang kết nối...`; | |
| document.getElementById('terminal-title').textContent = server.name; | |
| showConnectionAnimation(server); | |
| currentSocket = io(server.url, { transports: ['websocket'] }); | |
| currentSocket.on('connect', () => { | |
| terminalLoader.classList.add('hidden'); | |
| statusText.textContent = `Đã kết nối: ${server.name}`; | |
| if (activeTerminalSessionId) { | |
| currentSocket.emit('switch-session', activeTerminalSessionId); | |
| } | |
| term.focus(); | |
| }); | |
| currentSocket.on('disconnect', () => { | |
| if (activeServerUid === server.uid) { | |
| statusText.textContent = 'Mất kết nối'; | |
| term.write('\r\n\x1b[31m⚠️ Mất kết nối với server.\x1b[0m\r\n'); | |
| } | |
| }); | |
| currentSocket.on('output', data => term.write(data)); | |
| currentSocket.on('history', history => { | |
| terminalLoader.classList.add('hidden'); | |
| term.reset(); | |
| term.write(history); | |
| }); | |
| currentSocket.on('sessions-list', (sessionList) => { | |
| terminalSessions.clear(); | |
| sessionList.forEach(s => terminalSessions.set(s.id, s)); | |
| if (terminalSessions.size > 0 && !terminalSessions.has(activeTerminalSessionId)) { | |
| const firstSessionId = terminalSessions.keys().next().value; | |
| switchTerminalSession(firstSessionId); | |
| } else { | |
| renderTerminalTabs(); | |
| } | |
| }); | |
| currentSocket.on('session-created', (session) => { | |
| terminalSessions.set(session.id, session); | |
| renderTerminalTabs(); | |
| switchTerminalSession(session.id); | |
| }); | |
| currentSocket.on('session-closed', ({ id }) => { | |
| const wasActive = (id === activeTerminalSessionId); | |
| if (terminalSessions.has(id)) { | |
| terminalSessions.delete(id); | |
| if (wasActive && terminalSessions.size > 0) { | |
| switchTerminalSession(terminalSessions.keys().next().value); | |
| } else if (terminalSessions.size === 0) { | |
| activeTerminalSessionId = null; | |
| term.reset(); | |
| renderTerminalTabs(); | |
| } else { | |
| renderTerminalTabs(); | |
| } | |
| } | |
| }); | |
| } | |
| function renderTerminalTabs() { | |
| terminalTabsContainer.innerHTML = ''; | |
| if (allServices.find(s => s.uid === activeServerUid)?.isLocal) return; | |
| terminalSessions.forEach(session => { | |
| const tab = document.createElement('button'); | |
| tab.className = 'tab-btn'; | |
| tab.dataset.sessionId = session.id; | |
| tab.textContent = session.name; | |
| if (session.id === activeTerminalSessionId) tab.classList.add('active'); | |
| const closeBtn = document.createElement('i'); | |
| closeBtn.className = 'fas fa-times close-tab-btn'; | |
| if (terminalSessions.size > 1) { | |
| tab.appendChild(closeBtn); | |
| closeBtn.onclick = e => { | |
| e.stopPropagation(); | |
| if (confirm(`Bạn có chắc muốn đóng "${session.name}"?`)) { | |
| currentSocket.emit('close-session', session.id); | |
| } | |
| }; | |
| } | |
| tab.onclick = () => switchTerminalSession(session.id); | |
| terminalTabsContainer.appendChild(tab); | |
| }); | |
| const addBtn = document.createElement('button'); | |
| addBtn.className = 'add-tab-btn'; | |
| addBtn.title = 'Phiên mới'; | |
| addBtn.textContent = '+'; | |
| addBtn.onclick = () => currentSocket.emit('create-session'); | |
| terminalTabsContainer.appendChild(addBtn); | |
| } | |
| function switchTerminalSession(sessionId) { | |
| if (!terminalSessions.has(sessionId) || sessionId === activeTerminalSessionId) return; | |
| activeTerminalSessionId = sessionId; | |
| currentSocket.emit('switch-session', sessionId); | |
| term.reset(); | |
| renderTerminalTabs(); | |
| resizeTerminal(); | |
| term.focus(); | |
| } | |
| async function handleReset(server) { | |
| if (!server.deployHookUrl) { | |
| alert(`Server '${server.name}' chưa được cấu hình Deploy Hook URL.`); | |
| return; | |
| } | |
| if (activeServerUid !== server.uid) selectServer(server); | |
| term.write(`\r\n\x1b[33m[Reset] Đang gửi lệnh reset đến '${server.name}'...\x1b[0m\r\n`); | |
| try { | |
| await fetch(server.deployHookUrl, { method: 'POST', mode: 'no-cors' }); | |
| term.write(`\r\n\x1b[32m[Reset] Tín hiệu đã được gửi. Server sẽ khởi động lại.\x1b[0m\r\n`); | |
| const RESET_DURATION = 180000; // 3 minutes | |
| resettingServers[server.uid] = Date.now() + RESET_DURATION; | |
| if (activeServerUid === server.uid) showResettingOverlay(server, RESET_DURATION); | |
| } catch (error) { | |
| term.write(`\r\n\x1b[31m[Lỗi Reset] Không thể gửi lệnh: ${error.message}\x1b[0m\r\n`); | |
| } | |
| } | |
| function handleDelete(serverToDelete) { | |
| if (serverToDelete.isHardcoded || !confirm(`Bạn có chắc muốn xóa '${serverToDelete.name}'?`)) return; | |
| userServers = userServers.filter(s => s.uid !== serverToDelete.uid); | |
| saveServers(); | |
| loadServers(); | |
| if (activeServerUid === serverToDelete.uid) { | |
| selectServer(allServices[0]); | |
| } | |
| renderServerList(); | |
| } | |
| function showModal() { | |
| serverForm.reset(); | |
| document.getElementById('server-id').value = ''; | |
| document.getElementById('modal-title').innerHTML = 'Thêm Server Mới <i class="fas fa-plus-circle"></i>'; | |
| document.getElementById('modal-overlay').classList.add('show'); | |
| } | |
| function hideModal() { | |
| document.getElementById('modal-overlay').classList.remove('show'); | |
| } | |
| function handleFormSubmit(event) { | |
| event.preventDefault(); | |
| userServers.push({ | |
| uid: 'server_' + Date.now(), | |
| name: document.getElementById('server-name').value, | |
| url: document.getElementById('server-url').value, | |
| description: document.getElementById('server-description').value, | |
| deployHookUrl: document.getElementById('server-deploy-hook').value, | |
| }); | |
| saveServers(); | |
| loadServers(); | |
| renderServerList(); | |
| hideModal(); | |
| } | |
| // --- UPTIME MONITOR LOGIC (CLIENT-SIDE) --- | |
| const uptimeModalOverlay = document.getElementById('uptime-modal-overlay'); | |
| const uptimeForm = document.getElementById('uptime-form'); | |
| const uptimeViewContent = document.getElementById('uptime-view-content'); | |
| function initializeUptimeMonitor() { | |
| if (adminSocket && adminSocket.connected) { | |
| uptimeViewContent.innerHTML = `<div class="empty-uptime"><i class="fas fa-spinner fa-spin"></i><p>Đang tải dữ liệu...</p></div>`; | |
| adminSocket.emit('uptime:subscribe'); | |
| } else { | |
| uptimeViewContent.innerHTML = `<div class="empty-uptime"><i class="fas fa-exclamation-triangle"></i><p>Không thể kết nối server admin.</p></div>`; | |
| } | |
| } | |
| function renderUptimeList(state) { | |
| uptimeViewContent.innerHTML = ''; | |
| if (!state.sites || state.sites.length === 0) { | |
| uptimeViewContent.innerHTML = `<div class="empty-uptime"><i class="fas fa-satellite-dish"></i><p>Chưa có website nào được theo dõi.</p></div>`; | |
| return; | |
| } | |
| state.sites.forEach(site => { | |
| const card = createUptimeCard(site); | |
| uptimeViewContent.appendChild(card); | |
| if (state.statuses[site.uid]) updateUptimeCard(site.uid, state.statuses[site.uid]); | |
| }); | |
| } | |
| function createUptimeCard(site) { | |
| const card = document.createElement('div'); | |
| card.className = 'uptime-card'; | |
| card.dataset.uid = site.uid; | |
| const deleteBtnHtml = !site.isHardcoded ? `<button class="btn ghost danger uptime-delete-btn" style="padding: 4px 8px; font-size: 12px;">Xóa</button>` : ''; | |
| card.innerHTML = ` | |
| <div class="uptime-header"> | |
| <div class="uptime-title">${site.name} ${site.isHardcoded ? '<i class="fas fa-lock" style="font-size: 10px; color: var(--muted);"></i>' : ''}</div> | |
| <div class="uptime-status pending">Pending...</div> | |
| </div> | |
| <div class="uptime-url">${site.url}</div> | |
| <div class="uptime-meta"> | |
| <span>Response: <span class="uptime-response">- ms</span></span> | |
| ${deleteBtnHtml} | |
| </div>`; | |
| if (!site.isHardcoded) { | |
| card.querySelector('.uptime-delete-btn').addEventListener('click', e => { | |
| e.stopPropagation(); | |
| deleteUptimeSite(site.uid); | |
| }); | |
| } | |
| return card; | |
| } | |
| function updateUptimeCard(uid, statusData) { | |
| const card = document.querySelector(`.uptime-card[data-uid="${uid}"]`); | |
| if (!card) return; | |
| const statusEl = card.querySelector('.uptime-status'); | |
| const responseEl = card.querySelector('.uptime-response'); | |
| statusEl.classList.remove('up', 'down', 'pending'); | |
| switch (statusData.status) { | |
| case 'up': | |
| statusEl.classList.add('up'); statusEl.textContent = 'Up'; | |
| responseEl.textContent = `${statusData.responseTime} ms`; break; | |
| case 'down': | |
| statusEl.classList.add('down'); statusEl.textContent = 'Down'; | |
| responseEl.textContent = 'N/A'; break; | |
| default: | |
| statusEl.classList.add('pending'); statusEl.textContent = 'Checking...'; | |
| responseEl.textContent = '- ms'; break; | |
| } | |
| } | |
| function showUptimeModal() { uptimeForm.reset(); uptimeModalOverlay.classList.add('show'); } | |
| function hideUptimeModal() { uptimeModalOverlay.classList.remove('show'); } | |
| function handleUptimeFormSubmit(e) { | |
| e.preventDefault(); | |
| adminSocket.emit('uptime:add_site', { | |
| name: document.getElementById('uptime-name').value, | |
| url: document.getElementById('uptime-url').value, | |
| }); | |
| hideUptimeModal(); | |
| } | |
| function deleteUptimeSite(uid) { | |
| if (!confirm('Bạn có chắc muốn ngừng theo dõi website này?')) return; | |
| adminSocket.emit('uptime:delete_site', uid); | |
| } | |
| // --- SYSTEM STATUS LOGIC (CLIENT-SIDE) --- | |
| let cpuHistory = []; | |
| const MAX_HISTORY_POINTS = 40; // 40 points * 1.5s interval = 60s of history | |
| const cpuChartPath = document.getElementById('cpu-chart-path'); | |
| const cpuCoresContainer = document.getElementById('cpu-cores-container'); | |
| function updateSystemStats(stats) { | |
| // Update CPU | |
| document.getElementById('cpu-usage-text').textContent = `${stats.cpu.toFixed(1)}%`; | |
| // Update CPU Chart | |
| cpuHistory.push(stats.cpu); | |
| if (cpuHistory.length > MAX_HISTORY_POINTS) { | |
| cpuHistory.shift(); | |
| } | |
| const pathData = cpuHistory.map((p, i) => { | |
| const x = (i / (MAX_HISTORY_POINTS - 1)) * 100; | |
| const y = 30 - (p / 100) * 30; // Invert Y-axis | |
| return `${x},${y}`; | |
| }).join(' L '); | |
| cpuChartPath.setAttribute('d', `M ${pathData} L 100,30 L 0,30 Z`); | |
| // Update RAM | |
| const ramUsedGb = stats.ram.used / (1024 ** 3); | |
| const ramTotalGb = stats.ram.total / (1024 ** 3); | |
| document.getElementById('ram-usage-text').textContent = `${ramUsedGb.toFixed(2)} GB`; | |
| document.getElementById('ram-total-text').textContent = `${ramTotalGb.toFixed(2)} GB Total`; | |
| document.getElementById('ram-progress-bar').style.width = `${stats.ram.percent}%`; | |
| // Update System Info (only once or if changed) | |
| if (document.getElementById('info-hostname').textContent === '--') { | |
| document.getElementById('info-hostname').textContent = stats.info.hostname; | |
| document.getElementById('info-os').textContent = `${stats.info.platform} ${stats.info.release}`; | |
| document.getElementById('info-node').textContent = stats.info.nodeVersion; | |
| } | |
| const uptime = new Date(stats.info.uptime * 1000).toISOString().substr(11, 8); | |
| document.getElementById('info-uptime').textContent = uptime; | |
| // Update CPU Cores | |
| if (cpuCoresContainer.childElementCount !== stats.cpus.length) { | |
| cpuCoresContainer.innerHTML = ''; | |
| stats.cpus.forEach((core, i) => { | |
| const coreEl = document.createElement('div'); | |
| coreEl.className = 'cpu-core-item'; | |
| coreEl.innerHTML = ` | |
| <span class="label">Core ${i}</span> | |
| <div class="progress-bar"> | |
| <div id="core-bar-${i}" class="progress-bar-inner" style="width: ${core.toFixed(1)}%;"></div> | |
| </div> | |
| `; | |
| cpuCoresContainer.appendChild(coreEl); | |
| }); | |
| } else { | |
| stats.cpus.forEach((core, i) => { | |
| const bar = document.getElementById(`core-bar-${i}`); | |
| if (bar) bar.style.width = `${core.toFixed(1)}%`; | |
| }); | |
| } | |
| } | |
| function toggleFullscreen() { | |
| if (!document.fullscreenElement) document.documentElement.requestFullscreen(); | |
| else if (document.exitFullscreen) document.exitFullscreen(); | |
| } | |
| function initializeDashboard() { | |
| adminSocket = io(); | |
| adminSocket.on('connect', () => { | |
| const activeService = allServices.find(s => s.uid === activeServerUid); | |
| if (activeService?.isLocal) { | |
| if (activeService.uid === 'internal-uptime') initializeUptimeMonitor(); | |
| if (activeService.uid === 'internal-system') adminSocket.emit('system:subscribe'); | |
| } | |
| }); | |
| // Uptime listeners | |
| adminSocket.on('uptime:full_list', renderUptimeList); | |
| adminSocket.on('uptime:site_added', (newSite) => { | |
| const emptyState = uptimeViewContent.querySelector('.empty-uptime'); | |
| if (emptyState) emptyState.remove(); | |
| uptimeViewContent.appendChild(createUptimeCard(newSite)); | |
| }); | |
| adminSocket.on('uptime:site_removed', (uid) => { | |
| document.querySelector(`.uptime-card[data-uid="${uid}"]`)?.remove(); | |
| if (uptimeViewContent.childElementCount === 0) renderUptimeList({ sites: [], statuses: {} }); | |
| }); | |
| adminSocket.on('uptime:update', (updateData) => updateUptimeCard(updateData.uid, updateData)); | |
| // System status listener | |
| adminSocket.on('system-stats', updateSystemStats); | |
| loadServers(); | |
| renderServerList(); | |
| if (allServices.length > 0) { | |
| selectServer(allServices[0]); | |
| } else { | |
| statusText.textContent = 'No Services'; | |
| terminalLoader.classList.remove('hidden'); | |
| loaderAscii.textContent = '(>_<)'; | |
| loaderText.textContent = 'Không có dịch vụ nào.'; | |
| } | |
| // Event Listeners | |
| document.getElementById('add-server-btn').addEventListener('click', showModal); | |
| document.getElementById('cancel-btn').addEventListener('click', hideModal); | |
| serverForm.addEventListener('submit', handleFormSubmit); | |
| document.getElementById('add-uptime-site-btn').addEventListener('click', showUptimeModal); | |
| document.getElementById('uptime-cancel-btn').addEventListener('click', hideUptimeModal); | |
| uptimeForm.addEventListener('submit', handleUptimeFormSubmit); | |
| document.addEventListener('click', (e) => { | |
| if (!e.target.closest('.options-btn')) { | |
| document.querySelectorAll('.options-menu.show').forEach(m => m.classList.remove('show')); | |
| } | |
| if (e.target === document.getElementById('modal-overlay')) hideModal(); | |
| if (e.target === document.getElementById('uptime-modal-overlay')) hideUptimeModal(); | |
| }); | |
| term.onData(data => { | |
| if (currentSocket && activeTerminalSessionId) { | |
| currentSocket.emit('input', { sessionId: activeTerminalSessionId, data }); | |
| } | |
| }); | |
| } | |
| window.addEventListener('load', initializeDashboard); | |