-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchi.js
95 lines (81 loc) · 3.4 KB
/
chi.js
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
class ChatInterface {
constructor(options) {
this.container = options.container || document.body;
this.headerText = options.headerText || "CHI";
this.subHeaderText = options.subHeaderText || "Your Chat Interface";
this.onMessageSend = options.onMessageSend; // Custom callback function
this.init();
}
init() {
this.createChatContainer();
this.setupEvents();
}
createChatContainer() {
this.chatContainer = document.createElement('div');
this.chatContainer.className = 'chat-container';
this.header = document.createElement('div');
this.header.className = 'header';
this.header.innerHTML = `<h1>${this.headerText}</h1><p>${this.subHeaderText}</p>`;
this.chatContainer.appendChild(this.header);
this.messages = document.createElement('div');
this.messages.className = 'messages';
this.messages.id = 'messages';
this.chatContainer.appendChild(this.messages);
this.input = document.createElement('input');
this.input.type = 'text';
this.input.placeholder = 'Type a message...';
this.chatContainer.appendChild(this.input);
this.button = document.createElement('button');
this.button.textContent = 'Send';
this.chatContainer.appendChild(this.button);
this.container.appendChild(this.chatContainer);
}
setupEvents() {
this.button.addEventListener('click', () => this.sendMessage());
this.input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.sendMessage();
}
});
}
sendMessage() {
const messageText = this.input.value.trim();
if (messageText) {
this.displayMessage(messageText, 'user');
this.input.value = '';
// Show loading spinner
const loadingSpinner = document.createElement('div');
loadingSpinner.className = 'spinner';
this.displayMessage(loadingSpinner.outerHTML, 'bot');
if (this.onMessageSend) {
this.onMessageSend(messageText).then(responseText => {
// Remove loading message
this.removeLastBotMessage();
this.displayMessage(responseText, 'bot');
}).catch(error => {
console.error('Error in onMessageSend:', error);
this.removeLastBotMessage();
this.displayMessage("There was an error processing your request.", 'bot');
});
} else {
this.removeLastBotMessage();
this.displayMessage("This is a default response.", 'bot');
}
}
}
displayMessage(text, sender) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${sender}`;
messageDiv.innerHTML = text; // Use innerHTML to allow HTML content
this.messages.appendChild(messageDiv);
this.messages.scrollTop = this.messages.scrollHeight; // Auto scroll
}
removeLastBotMessage() {
const botMessages = this.messages.getElementsByClassName('bot');
if (botMessages.length > 0) {
this.messages.removeChild(botMessages[botMessages.length - 1]);
}
}
}
// Expose the ChatInterface class to the global scope
window.ChatInterface = ChatInterface;