-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
281 lines (227 loc) · 9.42 KB
/
script.js
File metadata and controls
281 lines (227 loc) · 9.42 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
class DualChatApp {
constructor() {
this.init();
}
init() {
// Get elements
this.input1 = document.getElementById('input1');
this.input2 = document.getElementById('input2');
this.send1 = document.getElementById('send1');
this.send2 = document.getElementById('send2');
this.messages1 = document.getElementById('messages1');
this.messages2 = document.getElementById('messages2');
// User names for identification
this.users = {
mobile1: 'Alice',
mobile2: 'Bob'
};
// Typing timeout references
this.typingTimeouts = {};
// Setup event listeners
this.setupEventListeners();
// Clear welcome messages on first interaction
this.welcomeCleared = false;
}
setupEventListeners() {
// Send button clicks
this.send1.addEventListener('click', () => this.sendMessage('mobile1'));
this.send2.addEventListener('click', () => this.sendMessage('mobile2'));
// Enter key press
this.input1.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.sendMessage('mobile1');
}
});
this.input2.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.sendMessage('mobile2');
}
});
// Typing indicators
this.input1.addEventListener('input', () => this.handleTyping('mobile1'));
this.input2.addEventListener('input', () => this.handleTyping('mobile2'));
// Auto-resize inputs
this.input1.addEventListener('input', () => this.updateSendButton('mobile1'));
this.input2.addEventListener('input', () => this.updateSendButton('mobile2'));
}
clearWelcomeMessages() {
if (!this.welcomeCleared) {
const welcomeMsg1 = this.messages1.querySelector('.welcome-message');
const welcomeMsg2 = this.messages2.querySelector('.welcome-message');
if (welcomeMsg1) welcomeMsg1.remove();
if (welcomeMsg2) welcomeMsg2.remove();
this.welcomeCleared = true;
}
}
updateSendButton(mobileId) {
const input = mobileId === 'mobile1' ? this.input1 : this.input2;
const sendBtn = mobileId === 'mobile1' ? this.send1 : this.send2;
if (input.value.trim()) {
sendBtn.style.opacity = '1';
sendBtn.style.transform = 'scale(1)';
} else {
sendBtn.style.opacity = '0.6';
}
}
handleTyping(senderMobile) {
const receiverMobile = senderMobile === 'mobile1' ? 'mobile2' : 'mobile1';
const input = senderMobile === 'mobile1' ? this.input1 : this.input2;
if (input.value.trim()) {
this.showTypingIndicator(receiverMobile);
// Clear existing timeout
if (this.typingTimeouts[senderMobile]) {
clearTimeout(this.typingTimeouts[senderMobile]);
}
// Set new timeout to hide typing indicator
this.typingTimeouts[senderMobile] = setTimeout(() => {
this.hideTypingIndicator(receiverMobile);
}, 1000);
} else {
this.hideTypingIndicator(receiverMobile);
}
}
showTypingIndicator(mobileId) {
const messagesContainer = mobileId === 'mobile1' ? this.messages1 : this.messages2;
// Remove existing typing indicator
const existingIndicator = messagesContainer.querySelector('.typing-indicator');
if (existingIndicator) {
existingIndicator.remove();
}
// Create new typing indicator
const typingDiv = document.createElement('div');
typingDiv.className = 'typing-indicator';
typingDiv.innerHTML = `
<div class="typing-dots">
<div class="typing-dot"></div>
<div class="typing-dot"></div>
<div class="typing-dot"></div>
</div>
<span style="margin-left: 8px; font-size: 12px; color: #888;">typing...</span>
`;
messagesContainer.appendChild(typingDiv);
this.scrollToBottom(messagesContainer);
}
hideTypingIndicator(mobileId) {
const messagesContainer = mobileId === 'mobile1' ? this.messages1 : this.messages2;
const typingIndicator = messagesContainer.querySelector('.typing-indicator');
if (typingIndicator) {
typingIndicator.remove();
}
}
sendMessage(senderMobile) {
const input = senderMobile === 'mobile1' ? this.input1 : this.input2;
const messageText = input.value.trim();
if (!messageText) return;
this.clearWelcomeMessages();
// Clear input
input.value = '';
this.updateSendButton(senderMobile);
// Hide typing indicator from receiver
const receiverMobile = senderMobile === 'mobile1' ? 'mobile2' : 'mobile1';
this.hideTypingIndicator(receiverMobile);
// Add message to sender's chat as "sent"
this.addMessage(senderMobile, messageText, 'sent');
// Add realistic delay before showing message on receiver's side
setTimeout(() => {
this.addMessage(receiverMobile, messageText, 'received');
}, 500 + Math.random() * 1000); // Random delay between 500-1500ms
}
addMessage(mobileId, text, type) {
const messagesContainer = mobileId === 'mobile1' ? this.messages1 : this.messages2;
const currentTime = this.getCurrentTime();
const messageDiv = document.createElement('div');
messageDiv.className = `message ${type}`;
messageDiv.innerHTML = `
<div class="message-content">${this.escapeHtml(text)}</div>
<div class="message-time">${currentTime}</div>
`;
// Add delivery status for sent messages
if (type === 'sent') {
setTimeout(() => {
const timeElement = messageDiv.querySelector('.message-time');
timeElement.classList.add('delivered');
}, 1000);
}
messagesContainer.appendChild(messageDiv);
this.scrollToBottom(messagesContainer);
// Add a subtle sound effect (you can uncomment this if you want audio)
// this.playMessageSound(type);
}
getCurrentTime() {
const now = new Date();
return now.toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
hour12: false
});
}
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
scrollToBottom(container) {
setTimeout(() => {
container.scrollTop = container.scrollHeight;
}, 100);
}
// Optional: Add sound effects
playMessageSound(type) {
// Create audio context for sound effects
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
if (type === 'sent') {
oscillator.frequency.setValueAtTime(800, audioContext.currentTime);
} else {
oscillator.frequency.setValueAtTime(600, audioContext.currentTime);
}
gainNode.gain.setValueAtTime(0, audioContext.currentTime);
gainNode.gain.linearRampToValueAtTime(0.1, audioContext.currentTime + 0.01);
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.1);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.1);
}
}
// Initialize the app when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new DualChatApp();
});
// Add some demo messages after a delay to show the interface in action
document.addEventListener('DOMContentLoaded', () => {
setTimeout(() => {
const app = new DualChatApp();
// Add a welcome interaction after 3 seconds
setTimeout(() => {
// Simulate Alice sending a message
const input1 = document.getElementById('input1');
input1.value = "Hey Bob! 👋";
app.sendMessage('mobile1');
// Bob responds after a delay
setTimeout(() => {
const input2 = document.getElementById('input2');
input2.value = "Hi Alice! How are you?";
app.sendMessage('mobile2');
}, 2000);
}, 3000);
}, 100);
});
// Utility function to add realistic message delays
function addRealisticDelay() {
return 300 + Math.random() * 1200; // 300ms to 1.5s delay
}
// Optional: Add keyboard shortcuts
document.addEventListener('keydown', (e) => {
// Ctrl/Cmd + 1 focuses on first chat
if ((e.ctrlKey || e.metaKey) && e.key === '1') {
e.preventDefault();
document.getElementById('input1').focus();
}
// Ctrl/Cmd + 2 focuses on second chat
if ((e.ctrlKey || e.metaKey) && e.key === '2') {
e.preventDefault();
document.getElementById('input2').focus();
}
});