Files
server-manager/public/assets/js/terminal.js
Agent 88f3c1f964 refactor: extract inline CSS/JS from views into separate asset files (20+ files)
Phase A — HTML removed from PHP files:
- RateLimitMiddleware.php: heredoc moved to views/errors/429.php
- public/index.php: inline HTML replaced with View::error()
- AuthMiddleware.php: <script> redirect replaced with <meta refresh>

Phase B — Inline CSS/JS extracted from views:
- CSS: landing.css (459 lines), legal.css (shared by terms+privacy)
- JS: 17 new files extracted from ~20 view files
  - main.js (layout sidebar + notifications)
  - dashboard-chart.js, server-show.js, server-services.js
  - nginx-manager.js, database-manager.js
  - terminal.js, team-show.js, team-index.js
  - server-list.js, server-permissions.js (shared by edit+create)
  - server-ssl.js, server-processes.js, system-users.js
  - admin-users.js, audit-log.js, admin-notifications.js, admin-security.js
  - notifications.js

Total: -3264 lines from views, +288 lines in new assets
2026-06-14 08:04:01 -04:00

111 lines
3.4 KiB
JavaScript

function getCsrfToken() {
return document.querySelector('meta[name="csrf-token"]')?.content || '';
}
const terminalInput = document.getElementById('terminalInput');
const terminalOutput = document.getElementById('terminalOutput');
const connectionStatus = document.getElementById('connectionStatus');
terminalInput.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
executeCommand();
}
});
function executeCommand() {
const command = terminalInput.value.trim();
if (!command) return;
appendTerminalLine(command, 'command');
terminalInput.value = '';
terminalInput.disabled = true;
document.getElementById('executeBtn').disabled = true;
updateStatus('executing', 'Executing...');
fetch('/terminal/' + serverId + '/execute', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-CSRF-Token': getCsrfToken(),
},
body: 'command=' + encodeURIComponent(command) + '&_csrf_token=' + encodeURIComponent(getCsrfToken()),
})
.then(r => r.json())
.then(data => {
terminalInput.disabled = false;
document.getElementById('executeBtn').disabled = false;
terminalInput.focus();
if (data.success) {
if (data.output) {
appendTerminalLine(data.output, 'output');
}
if (data.stderr) {
appendTerminalLine(data.stderr, 'error');
}
updateStatus('connected', 'Connected');
} else {
appendTerminalLine(data.message || 'Command failed', 'error');
updateStatus('error', 'Error');
}
const terminalBody = document.getElementById('terminalOutput');
terminalBody.scrollTop = terminalBody.scrollHeight;
})
.catch(err => {
terminalInput.disabled = false;
document.getElementById('executeBtn').disabled = false;
appendTerminalLine('Connection error: ' + err.message, 'error');
updateStatus('error', 'Error');
});
}
function appendTerminalLine(text, type) {
const line = document.createElement('div');
line.className = 'terminal-line terminal-' + type;
if (type === 'command') {
line.innerHTML = '<span class="terminal-prompt-inline">$</span> ' + escapeHtml(text);
} else {
line.innerHTML = '<pre>' + escapeHtml(text) + '</pre>';
}
terminalOutput.appendChild(line);
terminalOutput.scrollTop = terminalOutput.scrollHeight;
}
function runQuickCmd(cmd) {
terminalInput.value = cmd;
executeCommand();
}
function clearTerminal() {
terminalOutput.innerHTML = '';
}
function clearHistory() {
if (!confirm('Clear all command history for this server?')) return;
fetch('/terminal/' + serverId + '/clear-history', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-CSRF-Token': getCsrfToken(),
},
body: '_csrf_token=' + encodeURIComponent(getCsrfToken()),
})
.then(r => r.json())
.then(data => {
showToast(data.success ? 'success' : 'error', data.message);
if (data.success) location.reload();
});
}
function updateStatus(status, text) {
const dot = connectionStatus.querySelector('.status-dot');
dot.className = 'status-dot ' + status;
connectionStatus.lastChild.textContent = ' ' + text;
}
terminalInput.focus();