-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
152 lines (134 loc) · 4.69 KB
/
app.js
File metadata and controls
152 lines (134 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* SPDX-License-Identifier: MPL-2.0 */
// mIRCat UI-only mock — no networking, no crypto. All state is in-memory.
(function () {
const $ = (sel) => document.querySelector(sel);
const $$ = (sel) => Array.from(document.querySelectorAll(sel));
const channelListEl = $('#channel-list');
const userListEl = $('#user-list');
const logEl = $('#log');
const inputEl = $('#input');
const roomNameEl = $('#room-name');
const themeToggleEl = document.querySelector('.theme-toggle');
const channels = ['#general', '#random', '#cozy-outpost'];
const users = ['alice', 'bob', 'carol', 'dave'];
const nickColors = ['nick-a','nick-b','nick-c','nick-d','nick-e','nick-f'];
const nickColorClass = (nick) => {
const idx = Math.abs(hash(nick)) % 6; // 0..5
return nickColors[idx];
};
// Simple hash for color bucketing
function hash(str) {
let h = 0;
for (let i = 0; i < str.length; i++) h = ((h << 5) - h + str.charCodeAt(i)) | 0;
return h;
}
// In-memory message store per channel
/** @type {Record<string, {time:number,nick:string,text:string}[]>} */
const store = Object.fromEntries(
channels.map((c) => [c, []])
);
// Seed a few messages in #general
push('#general', 'alice', 'hello world');
push('#general', 'bob', 'hi!');
push('#general', 'you', '/join #cozy-outpost');
renderChannels('#general');
renderUsers(users);
switchChannel('#general');
// Event: send message on Enter
$('#composer').addEventListener('submit', (e) => {
e.preventDefault();
const val = inputEl.value.trim();
if (!val) return;
const chan = currentChannel();
push(chan, 'you', val);
inputEl.value = '';
renderLog(chan);
scrollLogToBottom();
});
// Theme toggle
themeToggleEl.addEventListener('click', () => document.body.classList.toggle('light'));
themeToggleEl.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') document.body.classList.toggle('light');
});
function currentChannel() {
const active = channelListEl.querySelector('li.active');
return active?.dataset.value ?? '#general';
}
function renderChannels(active) {
channelListEl.innerHTML = '';
channels.forEach((c) => {
const li = document.createElement('li');
li.dataset.value = c;
if (c === active) li.classList.add('active');
const btn = document.createElement('button');
btn.type = 'button';
btn.textContent = c;
btn.addEventListener('click', () => switchChannel(c));
li.appendChild(btn);
channelListEl.appendChild(li);
});
}
function renderUsers(nicks) {
userListEl.innerHTML = '';
nicks.forEach((n) => {
const li = document.createElement('li');
const btn = document.createElement('button');
btn.type = 'button';
btn.innerHTML = `<span class="${nickColorClass(n)}">${escapeHtml(n)}</span>`;
li.appendChild(btn);
userListEl.appendChild(li);
});
}
function switchChannel(chan) {
// update active UI
$$('#channel-list li').forEach((li) => {
li.classList.toggle('active', li.dataset.value === chan);
});
roomNameEl.textContent = chan;
renderLog(chan);
scrollLogToBottom();
}
function renderLog(chan) {
const msgs = store[chan] ?? [];
logEl.innerHTML = '';
msgs.forEach((m) => logEl.appendChild(renderMsg(m)));
}
function renderMsg(m) {
const row = document.createElement('div');
row.className = 'msg';
const time = document.createElement('div');
const hh = new Date(m.time).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
time.className = 'time';
time.textContent = hh;
const body = document.createElement('div');
const nick = document.createElement('span');
nick.className = `nick ${nickColorClass(m.nick)}`;
nick.textContent = padNick(m.nick);
const text = document.createElement('span');
text.className = 'text';
text.textContent = m.text;
body.appendChild(nick);
body.appendChild(document.createTextNode(': '));
body.appendChild(text);
row.appendChild(time);
row.appendChild(body);
return row;
}
function padNick(n) {
// mimic fixed-width nick column feel
const max = 8; // simple pad for aesthetics
if (n.length >= max) return n.slice(0, max);
return (n + ' '.repeat(max)).slice(0, max);
}
function push(chan, nick, text) {
const arr = store[chan] || (store[chan] = []);
arr.push({ time: Date.now(), nick, text });
}
function scrollLogToBottom() {
logEl.scrollTop = logEl.scrollHeight;
}
const htmlEscapeMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
function escapeHtml(s) {
return s.replace(/[&<>"']/g, (c) => htmlEscapeMap[c]);
}
})();