153 lines
4.9 KiB
JavaScript
153 lines
4.9 KiB
JavaScript
// public/js/ui.js - all DOM updates. Show message text with textContent, never innerHTML.
|
|
|
|
const el = (id) => document.getElementById(id);
|
|
|
|
function clear(node) {
|
|
while (node.firstChild) node.removeChild(node.firstChild);
|
|
}
|
|
|
|
function button(label, onClick, { active = false } = {}) {
|
|
const b = document.createElement('button');
|
|
b.type = 'button';
|
|
b.textContent = label;
|
|
if (active) b.classList.add('active');
|
|
b.addEventListener('click', onClick);
|
|
return b;
|
|
}
|
|
|
|
export function showLogin() {
|
|
el('loginView').classList.remove('hidden');
|
|
el('chatView').classList.add('hidden');
|
|
el('logoutBtn').classList.add('hidden');
|
|
el('tocStatus').classList.add('hidden');
|
|
el('me').textContent = '';
|
|
}
|
|
|
|
export function showChat(screenName) {
|
|
el('loginView').classList.add('hidden');
|
|
el('chatView').classList.remove('hidden');
|
|
el('logoutBtn').classList.remove('hidden');
|
|
el('tocStatus').classList.remove('hidden');
|
|
el('me').textContent = `Signed in as ${screenName}`;
|
|
}
|
|
|
|
export function setLoginError(msg) {
|
|
el('loginError').textContent = msg || '';
|
|
}
|
|
|
|
export function setChatError(msg) {
|
|
el('chatError').textContent = msg || '';
|
|
}
|
|
|
|
export function showBroadcast(text) {
|
|
const banner = el('broadcastBanner');
|
|
banner.textContent = `Broadcast: ${text}`;
|
|
banner.classList.remove('hidden');
|
|
}
|
|
|
|
export function renderConversations(state) {
|
|
const list = el('conversations');
|
|
clear(list);
|
|
for (const conv of Object.values(state.conversations)) {
|
|
const li = document.createElement('li');
|
|
const label = conv.type === 'chat' ? `# ${conv.name}` : conv.name;
|
|
li.appendChild(button(label, () => state.selectConversation(conv.key), {
|
|
active: state.active === conv.key,
|
|
}));
|
|
list.appendChild(li);
|
|
}
|
|
}
|
|
|
|
export function renderMessages(state) {
|
|
const conv = state.conversations[state.active];
|
|
el('chatTitle').textContent = conv ? (conv.type === 'chat' ? `# ${conv.name}` : conv.name) : 'No conversation selected';
|
|
el('chatMeta').textContent = conv ? (conv.type === 'chat' ? 'Chat room' : 'Direct message') : '';
|
|
|
|
const box = el('messages');
|
|
clear(box);
|
|
if (!conv) return;
|
|
for (const msg of conv.messages) {
|
|
const item = document.createElement('div');
|
|
item.className = 'msg' + (msg.mine ? ' me' : '');
|
|
|
|
const meta = document.createElement('div');
|
|
meta.className = 'meta';
|
|
const when = msg.time ? new Date(msg.time).toLocaleTimeString() : '';
|
|
meta.textContent = `${msg.from || 'unknown'}${when ? ' · ' + when : ''}`;
|
|
|
|
const body = document.createElement('div');
|
|
body.className = 'body';
|
|
body.textContent = msg.text || ''; // XSS-safe
|
|
|
|
item.append(meta, body);
|
|
box.appendChild(item);
|
|
}
|
|
box.scrollTop = box.scrollHeight;
|
|
const input = el('messageInput');
|
|
if (input) input.focus();
|
|
}
|
|
|
|
export function renderUserResults(users, onOpen) {
|
|
const list = el('userResults');
|
|
clear(list);
|
|
for (const name of users) {
|
|
const li = document.createElement('li');
|
|
li.appendChild(button(name, () => onOpen(name)));
|
|
list.appendChild(li);
|
|
}
|
|
}
|
|
|
|
export function renderBuddies(buddies, onOpen) {
|
|
const list = el('buddies');
|
|
clear(list);
|
|
for (const b of buddies) {
|
|
const li = document.createElement('li');
|
|
const name = typeof b === 'string'
|
|
? b
|
|
: (b.aimId || b.displayId || b.screenName || b.name || JSON.stringify(b));
|
|
const state = b && typeof b === 'object' && b.state ? ` (${b.state})` : '';
|
|
li.appendChild(button(`${name}${state}`, () => onOpen(name)));
|
|
list.appendChild(li);
|
|
}
|
|
}
|
|
|
|
export function renderInvites(invites, onAccept, onIgnore) {
|
|
const list = document.getElementById('invites');
|
|
if (!list) return;
|
|
clear(list);
|
|
for (const inv of invites || []) {
|
|
const li = document.createElement('li');
|
|
li.className = 'invite';
|
|
const title = document.createElement('div');
|
|
title.textContent = `${inv.from} invited you to "${inv.roomName}"`;
|
|
const msg = document.createElement('div');
|
|
msg.className = 'meta';
|
|
msg.textContent = inv.message || '';
|
|
const actions = document.createElement('div');
|
|
actions.className = 'invite-actions';
|
|
const join = document.createElement('button');
|
|
join.type = 'button';
|
|
join.textContent = 'Join';
|
|
join.addEventListener('click', () => onAccept && onAccept(inv.roomId));
|
|
const ignore = document.createElement('button');
|
|
ignore.type = 'button';
|
|
ignore.textContent = 'Ignore';
|
|
ignore.addEventListener('click', () => onIgnore && onIgnore(inv.roomId));
|
|
actions.append(join, ignore);
|
|
li.append(title, msg, actions);
|
|
list.appendChild(li);
|
|
}
|
|
}
|
|
|
|
export function setTocStatus(connected, detail = '') {
|
|
const wrap = document.getElementById('tocStatus');
|
|
if (!wrap) return;
|
|
const dot = wrap.querySelector('.dot');
|
|
if (dot) {
|
|
dot.classList.toggle('dot-on', !!connected);
|
|
dot.classList.toggle('dot-off', !connected);
|
|
}
|
|
const label = connected ? 'TOC connected' : 'TOC disconnected';
|
|
wrap.title = detail ? `${label}: ${detail}` : label;
|
|
wrap.classList.remove('hidden');
|
|
}
|