(() => {
const CONVERSATION = [
{
a: "Can I register at the venue, or do I need to have registered online?",
b: "Walk-in registration is open today! Just bring a valid photo ID and we'll get you set up in minutes.",
},
{
a: "What if I don't have a confirmation email?",
b: "No problem — we can look you up by name at the counter.",
},
{
a: "Where's the registration counter?",
b: "North Atrium, ground floor — straight ahead from the main entrance, past the coffee stand.",
},
{
a: "How far is that from the main entrance?",
b: "About a 2-minute walk — signs are posted the whole way.",
},
{
a: "Is there parking nearby?",
b: "Yes, Lot B is right outside — free for attendees.",
},
{
a: "Perfect, thank you!",
b: "Anytime! Enjoy the event 🎉",
},
];
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
function initChatDemo() {
const widget = document.querySelector(".chat-widget");
if (!widget || !window.gsap) return;
const messages = widget.querySelector(".chat-messages");
const textarea = widget.querySelector(".chat-input-bar textarea");
const sendBtn = widget.querySelector(".send-btn");
const status = widget.querySelector(".chat-header .status");
if (!messages || !textarea || !sendBtn || !status) return;
// Normalize avatar defs ids
const defs = widget.querySelector(".avatar-defs");
let avatarId = "demo";
if (defs) {
const groups = defs.querySelectorAll("g[id]");
groups.forEach((g) => {
if (g.id.startsWith("avatar-a")) g.id = "avatar-a-demo";
if (g.id.startsWith("avatar-b")) g.id = "avatar-b-demo";
});
}
let stopped = false;
const timelines = [];
const track = (tl) => {
timelines.push(tl);
tl.eventCallback("onComplete", () => {
const i = timelines.indexOf(tl);
if (i >= 0) timelines.splice(i, 1);
});
return tl;
};
const scrollBottom = () => {
messages.scrollTop = messages.scrollHeight;
};
const avatarSvg = (who) =>
``;
const resizeInput = () => {
textarea.style.height = "auto";
textarea.style.height = `${textarea.scrollHeight}px`;
};
const typeIntoInput = (text, speed = 45) =>
new Promise((resolve) => {
textarea.value = "";
resizeInput();
let i = 0;
const id = setInterval(() => {
if (stopped) {
clearInterval(id);
resolve();
return;
}
textarea.value += text[i];
resizeInput();
i += 1;
if (i >= text.length) {
clearInterval(id);
resolve();
}
}, speed);
});
const addMessage = (who, text) => {
const row = document.createElement("div");
row.className = `msg-row from-${who}`;
row.innerHTML = `${avatarSvg(who)}