// Lightweight Terminal Panel singleton
(() => {
class TerminalPanelImpl {
constructor() {
this.container = null; // external container passed by DrawerComponent
this.panelEl = null;
this.logEl = null;
this.inputEl = null;
this.socket = null;
this.connectedIp = null;
this.isOpen = false;
this.resizeHandler = null;
}
open(container, nodeIp) {
try {
this.container = container;
if (!this.container) return;
if (!this.panelEl) {
this._buildPanel();
}
// Reset any leftover inline positioning so CSS centering applies
if (this.container) {
this.container.style.right = '';
this.container.style.left = '';
this.container.style.top = '';
this.container.style.bottom = '';
this.container.style.width = '';
}
// Show panel with animation
requestAnimationFrame(() => {
this.panelEl.classList.add('visible');
this.isOpen = true;
this.inputEl && this.inputEl.focus();
});
// Connect websocket
if (nodeIp) {
this._connect(nodeIp);
}
} catch (err) {
console.error('TerminalPanel.open error:', err);
}
}
close() {
try {
if (!this.isOpen) return;
this.panelEl && this.panelEl.classList.remove('visible');
this.isOpen = false;
if (this.socket) {
try { this.socket.close(); } catch (_) {}
this.socket = null;
}
} catch (err) {
console.error('TerminalPanel.close error:', err);
}
}
_buildPanel() {
// Ensure container baseline positioning
this.container.classList.add('terminal-panel-container');
// Create panel DOM
this.panelEl = document.createElement('div');
this.panelEl.className = 'terminal-panel';
this.panelEl.innerHTML = `
`;
this.container.appendChild(this.panelEl);
this.logEl = this.panelEl.querySelector('.terminal-log');
this.inputEl = this.panelEl.querySelector('.terminal-input');
const sendBtn = this.panelEl.querySelector('.terminal-send-btn');
const closeBtn = this.panelEl.querySelector('.terminal-close-btn');
const clearBtn = this.panelEl.querySelector('.terminal-clear-btn');
const sendHandler = () => {
const value = (this.inputEl && this.inputEl.value) || '';
if (!value) return;
this._send(value);
this.inputEl.value = '';
};
if (sendBtn) sendBtn.addEventListener('click', (e) => { e.stopPropagation(); sendHandler(); });
if (this.inputEl) this.inputEl.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
sendHandler();
}
});
if (closeBtn) closeBtn.addEventListener('click', (e) => { e.stopPropagation(); this.close(); });
if (clearBtn) clearBtn.addEventListener('click', (e) => { e.stopPropagation(); this._clear(); });
}
_connect(nodeIp) {
try {
const titleEl = this.panelEl.querySelector('.terminal-title');
if (titleEl) titleEl.textContent = `Terminal — ${nodeIp}`;
// Close previous socket if switching node
if (this.socket) {
try { this.socket.close(); } catch (_) {}
this.socket = null;
}
const protocol = (window.location && window.location.protocol === 'https:') ? 'wss' : 'ws';
const url = `${protocol}://${nodeIp}/ws`;
this.connectedIp = nodeIp;
this._appendLine(`[connecting] ${url}`);
const ws = new WebSocket(url);
this.socket = ws;
ws.addEventListener('open', () => {
this._appendLine('[open] WebSocket connection established');
});
ws.addEventListener('message', (evt) => {
let dataStr = typeof evt.data === 'string' ? evt.data : '[binary]';
// Decode any HTML entities so JSON isn't shown as " etc.
dataStr = this._decodeHtmlEntities(dataStr);
// Try to pretty-print JSON if applicable
const trimmed = dataStr.trim();
if ((trimmed.startsWith('{') && trimmed.endsWith('}')) || (trimmed.startsWith('[') && trimmed.endsWith(']'))) {
try {
const obj = JSON.parse(trimmed);
const pretty = JSON.stringify(obj, null, 2);
this._appendLine(pretty);
return;
} catch (_) {
// fall through if not valid JSON
}
}
this._appendLine(dataStr);
});
ws.addEventListener('error', (evt) => {
this._appendLine('[error] WebSocket error');
});
ws.addEventListener('close', () => {
this._appendLine('[close] WebSocket connection closed');
});
} catch (err) {
console.error('TerminalPanel._connect error:', err);
this._appendLine(`[error] ${err.message || err}`);
}
}
_send(text) {
try {
if (!this.socket || this.socket.readyState !== 1) {
this._appendLine('[warn] Socket not open');
return;
}
this.socket.send(text);
this._appendLine(`> ${text}`);
} catch (err) {
this._appendLine(`[error] ${err.message || err}`);
}
}
_clear() {
if (this.logEl) this.logEl.textContent = '';
}
_appendLine(line) {
if (!this.logEl) return;
const timestamp = new Date().toLocaleTimeString();
this.logEl.textContent += `[${timestamp}] ${line}\n`;
// Auto-scroll to bottom
const bodyEl = this.panelEl.querySelector('.terminal-body');
if (bodyEl) bodyEl.scrollTop = bodyEl.scrollHeight;
}
_decodeHtmlEntities(text) {
try {
const div = document.createElement('div');
div.innerHTML = text;
return div.textContent || div.innerText || '';
} catch (_) {
return text;
}
}
}
// Expose singleton API
window.TerminalPanel = window.TerminalPanel || new TerminalPanelImpl();
})();