208 lines
7 KiB
JavaScript
208 lines
7 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;
|
|
}
|
|
|
|
const MSG_STYLE_KEY = 'oscar.msgStyle';
|
|
export function getMessageStyle() {
|
|
const v = localStorage.getItem(MSG_STYLE_KEY);
|
|
return v === 'classic' ? 'classic' : 'bubbles';
|
|
}
|
|
|
|
const NAME_COLORS = [
|
|
'#6ee7ff', '#ff7ab6', '#7CFF6B', '#ffd166', '#c792ea',
|
|
'#ff9f43', '#4dd0e1', '#f78fb3', '#a8e063', '#82aaff',
|
|
'#f6c177', '#ff6b6b', '#5ad1a3', '#d4a5ff', '#f9f871',
|
|
];
|
|
|
|
function nameColor(name) {
|
|
let hash = 0;
|
|
const s = String(name || 'unknown').toLowerCase();
|
|
for (let i = 0; i < s.length; i++) hash = ((hash << 5) - hash) + s.charCodeAt(i);
|
|
return NAME_COLORS[Math.abs(hash) % NAME_COLORS.length];
|
|
}
|
|
export function setMessageStyle(style) {
|
|
const v = style === 'classic' ? 'classic' : 'bubbles';
|
|
localStorage.setItem(MSG_STYLE_KEY, v);
|
|
applyMessageStyle();
|
|
const btn = document.getElementById('styleToggleBtn');
|
|
if (btn) btn.textContent = v === 'classic' ? 'Classic' : 'Bubbles';
|
|
}
|
|
export function applyMessageStyle() {
|
|
const box = document.getElementById('messages');
|
|
if (!box) return;
|
|
const style = getMessageStyle();
|
|
box.classList.toggle('style-bubbles', style === 'bubbles');
|
|
box.classList.toggle('style-classic', style === 'classic');
|
|
}
|
|
|
|
export function showLogin() {
|
|
el('loginView').classList.remove('hidden');
|
|
el('chatView').classList.add('hidden');
|
|
el('logoutBtn').classList.add('hidden');
|
|
el('tocStatus').classList.add('hidden');
|
|
const styleBtn = document.getElementById('styleToggleBtn');
|
|
if (styleBtn) styleBtn.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');
|
|
const styleBtn = document.getElementById('styleToggleBtn');
|
|
if (styleBtn) styleBtn.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');
|
|
applyMessageStyle();
|
|
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() : '';
|
|
const from = msg.from || 'unknown';
|
|
const style = getMessageStyle();
|
|
if (style === 'classic') {
|
|
const timeSpan = document.createElement('span');
|
|
timeSpan.textContent = `[${when || ''}] `;
|
|
const nameSpan = document.createElement('span');
|
|
nameSpan.textContent = `<${from}>`;
|
|
nameSpan.style.color = nameColor(from);
|
|
meta.append(timeSpan, nameSpan, document.createTextNode(' '));
|
|
} else {
|
|
meta.textContent = `${from}${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');
|
|
const card = document.getElementById('invitesCard');
|
|
if (!list) return;
|
|
clear(list);
|
|
const items = invites || [];
|
|
if (card) {
|
|
card.classList.toggle('hidden', items.length === 0);
|
|
card.classList.toggle('invite-attention', items.length > 0);
|
|
}
|
|
for (const inv of items) {
|
|
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');
|
|
}
|