PLEAS DONT SKIP MY CODE
OTH THNGS REMIAN SME
JUT ADD COD T HAND;DE SMMALLER, UPPRE CASE SEPARTELY
ADN OFCURSE SPECIAL CHARATERS
/* =========================
Automated Chat Messages
========================= */
const messages = [
{ sender: "client", text: "Hello! How are you?" },
{ sender: "me", text: "I'm good, thank you! How can I assist you today?" },
{ sender: "client", text: "I need some information about your services." },
{ sender: "me", text: "Sure! We offer a variety of services including web development, app development, and digital marketing." },
{ sender: "client", text: "That sounds great. Can you provide more details on web development?" },
{ sender: "me", text: "Absolutely! Our web development services include building responsive websites, e-commerce platforms, and custom web applications tailored to your business needs." },
// Add more messages as needed
];
document.addEventListener('DOMContentLoaded', () => {
const emojiToggle = document.getElementById('emoji-toggle'); // Emoji button
const emojiPicker = document.getElementById('emoji-picker'); // Emoji picker div
const keyboard = document.querySelector('.keyboard'); // Keyboard container
if (!emojiToggle || !emojiPicker || !keyboard) {
console.error('Missing required elements in the DOM!');
return;
}
// Add click event to the emoji button
emojiToggle.addEventListener('click', () => {
// Hide the keyboard
keyboard.style.display = 'none';
// Show the emoji picker
emojiPicker.style.display = 'block';
console.log('Emoji picker displayed, keyboard hidden.');
});
// Optional: Hide emoji picker when clicking outside
document.addEventListener('click', (event) => {
const isClickInsidePicker = emojiPicker.contains(event.target);
const isClickOnToggle = emojiToggle.contains(event.target);
if (!isClickInsidePicker && !isClickOnToggle) {
// Hide the emoji picker
emojiPicker.style.display = 'none';
// Show the keyboard
keyboard.style.display = 'block';
console.log('Emoji picker hidden, keyboard displayed.');
}
});
});
/* =========================
DOM Content Loaded
========================= /
document.addEventListener('DOMContentLoaded', () => {
/ -------------------------
Time Handling
------------------------- */
const deviceTime = document.querySelector('.status-bar .time');
if (deviceTime) {
deviceTime.innerHTML = moment().format('h:mm A');
setInterval(() => {
deviceTime.innerHTML = moment().format('h:mm A');
}, 60000); // Update every minute for better efficiency
}
/* -------------------------
Message Handling
------------------------- */
const form = document.querySelector('.conversation-compose');
const conversation = document.querySelector('.conversation-container');
if (!conversation) {
console.error("Conversation container not found.");
return;
}
// Clear any existing messages to ensure a clean start
conversation.innerHTML = '';
if (form) {
form.addEventListener('submit', newMessage);
}
/* -------------------------
Status Handling
------------------------- */
statusSpan = document.querySelector('.user-bar .status');
if (!statusSpan) {
console.error("Status span not found.");
}
function newMessage(e) {
e.preventDefault();
const input = e.target.querySelector('textarea[name="input"]');
if (input && input.value.trim()) {
const messageText = input.value.trim();
const message = buildMessage(messageText, 'sent');
conversation.appendChild(message);
animateMessage(message);
scrollToBottom(conversation);
}
if (input) {
input.value = '';
adjustInputHeight(input);
}
}
function buildMessage(text, type = 'sent') {
const element = document.createElement('div');
element.classList.add('message', type);
// For sent messages, add double tick icons
let tickHTML = '';
if (type === 'sent') {
tickHTML = `
<span class="metadata">
<span class="time">${moment().format('h:mm A')}</span>
<span class="tick double">
<!-- Double Tick SVGs -->
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="15" fill="#92a58c">
<path d="M15.01 3.316l-.478-.372a.365.365 0 0 0-.51.063L8.666 9.88a.32.32 0 0 1-.484.032l-.358-.325a.32.32 0 0 0-.484.032l-.378.48a.418.418 0 0 0 .036.54l1.32 1.267a.32.32 0 0 0 .484-.034l6.272-8.048a.366.366 0 0 0-.064-.512zm-4.1 0l-.478-.372a.365.365 0 0 0-.51.063L4.566 9.88a.32.32 0 0 1-.484.032L1.892 7.77a.366.366 0 0 0-.516.005l-.423.433a.364.364 0 0 0 .006.514l3.255 3.185a.32.32 0 0 0 .484-.033l6.272-8.048a.365.365 0 0 0-.063-.51z"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="15" fill="#4fc3f7">
<path d="M15.01 3.316l-.478-.372a.365.365 0 0 0-.51.063L8.666 9.88a.32.32 0 0 1-.484.032l-.358-.325a.32.32 0 0 0-.484.032l-.378.48a.418.418 0 0 0 .036.54l1.32 1.267a.32.32 0 0 0 .484-.034l6.272-8.048a.366.366 0 0 0-.064-.512zm-4.1 0l-.478-.372a.365.365 0 0 0-.51.063L4.566 9.88a.32.32 0 0 1-.484.032L1.892 7.77a.366.366 0 0 0-.516.005l-.423.433a.364.364 0 0 0 .006.514l3.255 3.185a.32.32 0 0 0 .484-.033l6.272-8.048a.365.365 0 0 0-.063-.51z"/>
</svg>
</span>
</span>
`;
}
element.innerHTML = `
${sanitize(text)}
${tickHTML}
`;
return element;
}
function sanitize(str) {
const temp = document.createElement('div');
temp.textContent = str;
return temp.innerHTML;
}
function animateMessage(message) {
if (message.classList.contains('sent')) {
// No animation needed for double ticks; remove this function or keep it empty
// Alternatively, if you have animation classes, handle them here without removing 'double'
// For now, we'll keep it empty to preserve double ticks
}
}
function scrollToBottom(container) {
container.scrollTo({
top: container.scrollHeight,
behavior: 'smooth'
});
}
/* -------------------------
Keyboard Handling
------------------------- */
const inputField = document.querySelector('.input-msg');
const sendButton = document.querySelector('.send');
const shiftKeys = document.querySelectorAll('.shift');
const symKey = document.querySelector('.sym');
let isUppercase = false;
let isSymbols = false;
// Rows
const lettersRows = document.querySelectorAll('.letters-row');
const numbersRows = document.querySelectorAll('.numbers-row');
function showLetters() {
lettersRows.forEach(row => row.style.display = 'flex');
numbersRows.forEach(row => row.style.display = 'none');
}
function showNumbers() {
lettersRows.forEach(row => row.style.display = 'none');
numbersRows.forEach(row => row.style.display = 'flex');
}
// Initially show letters
showLetters();
// Toggle case function
function toggleCase() {
const letterKeys = document.querySelectorAll('.letter');
isUppercase = !isUppercase;
letterKeys.forEach(key => {
let current = key.textContent.trim();
if (current) {
key.textContent = isUppercase ? current.toUpperCase() : current.toLowerCase();
}
});
}
// Toggle symbols/numbers
if (symKey) {
symKey.addEventListener('click', () => {
isSymbols = !isSymbols;
if (isSymbols) {
showNumbers();
symKey.textContent = 'ABC';
} else {
showLetters();
symKey.textContent = '123';
}
});
}
// Shift keys
shiftKeys.forEach(shift => {
shift.addEventListener('click', () => {
toggleCase();
});
});
// Handle key clicks
const keyboard = document.querySelector('.keyboard');
if (keyboard && inputField && form) {
keyboard.addEventListener('click', (e) => {
const key = e.target.closest('.key');
if (!key) return;
const keyText = key.textContent.trim();
if (key.classList.contains('backspace')) {
inputField.value = inputField.value.slice(0, -1);
adjustInputHeight(inputField);
} else if (key.classList.contains('enter')) {
// Trigger form submission
form.dispatchEvent(new Event('submit'));
} else if (key.classList.contains('space')) {
inputField.value += ' ';
highlightVirtualKey(' ');
adjustInputHeight(inputField);
} else if (!key.classList.contains('shift') && !key.classList.contains('sym')) {
// Regular character key
inputField.value += keyText;
// Highlight the key
highlightVirtualKey(keyText);
// If shift was pressed once, return to lowercase after one key press
if (isUppercase && !isSymbols) {
toggleCase(); // revert to lowercase after a single character press
}
adjustInputHeight(inputField);
}
inputField.focus();
});
}
/* -------------------------
Automated Chat Flow
------------------------- */
// Function to display the first client message immediately
function displayInitialClientMessage(text) {
const messageType = 'received';
const message = buildMessage(text, messageType);
conversation.appendChild(message);
scrollToBottom(conversation);
}
// Function to show loading indicator for client messages
function showLoadingIndicator() {
if (statusSpan) {
statusSpan.textContent = "typing...";
statusSpan.classList.add('typing');
}
const loadingMessage = document.createElement('div');
loadingMessage.classList.add('message', 'received', 'loading');
loadingMessage.innerHTML = `
<span class="loading-dots">
<span>.</span><span>.</span><span>.</span>
</span>
<span class="metadata">
<span class="time">${moment().format('h:mm A')}</span>
</span>
`;
conversation.appendChild(loadingMessage);
scrollToBottom(conversation);
return loadingMessage;
}
// Function to replace loading indicator with actual client message
function replaceLoadingWithMessage(loadingElement, text, sender) {
if (loadingElement) {
const messageType = sender === "me" ? 'sent' : 'received';
loadingElement.classList.remove('loading');
loadingElement.classList.add(messageType);
loadingElement.innerHTML = `
${sanitize(text)}
${messageType === 'sent' ? `
<span class="metadata">
<span class="time">${moment().format('h:mm A')}</span>
<span class="tick double">
<!-- Double Tick SVGs -->
</span>
</span>
` : `
<span class="metadata">
<span class="time">${moment().format('h:mm A')}</span>
</span>
`}
`;
}
if (statusSpan && sender === "client") {
statusSpan.textContent = "online";
statusSpan.classList.remove('typing');
}
}
// Function to type a message character by character into the input field and send it
function typeAndSendMessage(text, sender, callback) {
if (sender === "client") {
// For client messages, show loading indicator first
const loadingElement = showLoadingIndicator();
// Calculate delay based on message length and typing speed
const typingSpeed = 150; // milliseconds per character (human-like)
const totalDelay = text.length * typingSpeed + 1000; // Additional delay
// After the total delay, replace loading indicator with actual message
setTimeout(() => {
replaceLoadingWithMessage(loadingElement, text, sender);
if (callback) callback();
}, totalDelay);
return;
}
if (sender === "me" && inputField && sendButton) {
inputField.value = ''; // Clear the input field before typing
adjustInputHeight(inputField);
let index = 0;
const typingSpeed = 150; // milliseconds per character (human-like)
function typeNextChar() {
if (index < text.length) {
const char = text.charAt(index);
inputField.value += char;
adjustInputHeight(inputField);
scrollToBottom(conversation);
// Highlight the corresponding virtual key
highlightVirtualKey(char);
index++;
setTimeout(typeNextChar, typingSpeed);
} else {
// Once typing is complete, trigger the send button
sendButton.click();
if (callback) callback();
}
}
typeNextChar();
}
}
// Async function to handle the chat flow
async function startChat() {
for (let i = 0; i < messages.length; i++) {
const msg = messages[i];
if (i === 0 && msg.sender === "client") {
// Display the first client message immediately without loading indicator or tick marks
displayInitialClientMessage(msg.text);
// Wait for a short delay before the next message
await delay(2000);
} else {
// For all other messages
await new Promise((resolve) => {
typeAndSendMessage(msg.text, msg.sender, resolve);
});
// Wait for a short delay before the next message
await delay(2000);
}
}
}
// Helper function to create a delay
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/* -------------------------
Helper Functions
------------------------- */
// Assign data-key attributes
assignDataKeys();
// Highlight the corresponding virtual key
function highlightVirtualKey(char) {
const normalizedChar = char.toLowerCase();
let keySelector = `.key.letter[data-key="${normalizedChar}"]`;
// Handle space separately
if (char === ' ') {
keySelector = `.key.space`;
}
const virtualKey = document.querySelector(keySelector);
if (virtualKey) {
virtualKey.classList.add('highlight');
setTimeout(() => {
virtualKey.classList.remove('highlight');
}, 150);
}
}
// Adjust input box height based on content
function adjustInputHeight(input) {
input.style.height = 'auto';
input.style.height = (input.scrollHeight) + 'px';
}
/* Highlight Toggle Function */
function toggleHighlight(keyElement) {
if (!keyElement) return;
keyElement.classList.add('highlight');
setTimeout(() => {
keyElement.classList.remove('highlight');
}, 150);
}
/* -------------------------
Start the Automated Chat
------------------------- */
startChat();
});
/* =========================
Helper Functions (Global)
========================= */
/* Assign data-key attributes */
function assignDataKeys() {
// Letters
document.querySelectorAll('.key.letter').forEach(key => {
key.setAttribute('data-key', key.textContent.trim().toLowerCase());
});
// Symbols
document.querySelectorAll('.key.symbol').forEach(key => {
key.setAttribute('data-key', key.textContent.trim());
});
// Special Keys
document.querySelectorAll('.key.space').forEach(key => {
key.setAttribute('data-key', ' ');
});
document.querySelectorAll('.key.enter').forEach(key => {
key.setAttribute('data-key', 'Enter');
});
document.querySelectorAll('.key.backspace').forEach(key => {
key.setAttribute('data-key', 'Backspace');
});
document.querySelectorAll('.key.shift').forEach(key => {
key.setAttribute('data-key', 'Shift');
});
}