e-comm-flask / static /css /script.js
abdullah9873's picture
Upload folder using huggingface_hub
3c62f53 verified
Raw History Blame Contribute Delete
22.5 kB
function initChatInterface() {
// Create main chat container
const chatContainer = document.createElement("div");
chatContainer.id = "chat-container";
chatContainer.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
width: 380px;
max-width: 90vw;
height: 500px;
max-height: 70vh;
background-color: white;
border-radius: 16px;
box-shadow: 0 5px 25px rgba(0,0,0,0.15);
z-index: 1000;
display: flex;
flex-direction: column;
font-family: 'Segoe UI', Roboto, sans-serif;
overflow: hidden;
transition: all 0.3s ease;
transform: translateY(20px);
opacity: 0;
`;
// Chat header with gradient
chatContainer.innerHTML = `
<div id="chat-header" style="
padding: 12px 16px;
background: linear-gradient( #db9c4bff 100%);
border-radius: 16px 16px 0 0;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
color: white;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
">
<div style="display: flex; align-items: center; gap: 10px;">
<div style="
width: 36px;
height: 36px;
background-color: rgba(255,255,255,0.2);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
">💬</div>
<div>
<h3 style="margin: 0; font-size: 16px; font-weight: 600;">Furniture Expert</h3>
<p style="margin: 0; font-size: 12px; opacity: 0.9;">We're online to help</p>
</div>
</div>
<div style="display: flex; gap: 8px;">
<button id="minimize-chat" style="
background: rgba(255,255,255,0.2);
border: none;
width: 28px;
height: 28px;
border-radius: 50%;
color: white;
font-size: 16px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
">−</button>
<button id="close-chat" style="
background: rgba(255,255,255,0.2);
border: none;
width: 28px;
height: 28px;
border-radius: 50%;
color: white;
font-size: 16px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
">×</button>
</div>
</div>
<div id="chat-box" style="
flex-grow: 1;
overflow-y: auto;
padding: 16px;
background: #f8f9fa;
display: flex;
flex-direction: column;
gap: 12px;
"></div>
<div id="suggestion-box" style="
padding: 8px 16px;
background: #f1f3f4;
border-top: 1px solid #e0e0e0;
display: flex;
gap: 8px;
overflow-x: auto;
"></div>
<div style="
padding: 12px 16px;
background: #fff;
border-top: 1px solid #e0e0e0;
display: flex;
gap: 8px;
">
<div style="position: relative; flex-grow: 1;">
<input type="text" id="user-input" placeholder="Ask about furniture..." style="
width: 100%;
padding: 12px 16px;
padding-right: 40px;
border: 1px solid #ddd;
border-radius: 24px;
outline: none;
font-size: 14px;
transition: border 0.2s;
">
<label for="image-upload" style="
position: absolute;
right: 12px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
color: #666;
">
📸
<input type="file" id="image-upload" accept="image/*" style="display: none">
</label>
</div>
<button id="send-button" style="
padding: 0;
width: 44px;
height: 44px;
background: #f4a742;
color: white;
border: none;
border-radius: 50%;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
">→</button>
</div>
`;
document.body.appendChild(chatContainer);
// Floating chat button with animation
const chatButton = document.createElement("button");
chatButton.id = "chat-button";
chatButton.innerHTML = `
<span>💬</span>
`;
chatButton.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
width: 60px;
height: 60px;
background: linear-gradient(135deg, #d39e00ff 0%, #f4a742 100%);
color: white;
border: none;
border-radius: 50%;
cursor: pointer;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
z-index: 999;
display: flex;
align-items: center;
justify-content: center;
font-size: 30px;
font-weight: 600;
transition: all 0.3s ease;
animation: pulse 2s infinite, float 3s ease-in-out infinite;
display: none;
`;
// Add pulse animation
const style = document.createElement("style");
style.textContent = `
@keyframes pulse {
0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(244, 167, 66, 0.7); }
70% { transform: scale(1.05); box-shadow: 0 0 0 15px rgba(244, 167, 66, 0); }
100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(244, 167, 66, 0); }
}
@keyframes float {
0% { transform: translateY(0px); }
50% { transform: translateY(-5px); }
100% { transform: translateY(0px); }
}
@keyframes popIn {
0% { transform: translateY(20px); opacity: 0; }
100% { transform: translateY(0); opacity: 1; }
}
`;
document.head.appendChild(style);
document.body.appendChild(chatButton);
// Show pop-up animation when page loads
setTimeout(() => {
chatContainer.style.animation = "popIn 0.4s ease-out forwards";
// Add welcome message
displayAssistantResponse(`
<div style="margin-bottom: 8px;">
Assalam-o-Alaikum! 👋 I'm your personal furniture assistant.
</div>
<div style="margin-bottom: 8px;">
I can help you:
<ul style="margin: 8px 0; padding-left: 20px;">
<li>Find products</li>
<li>Compare prices</li>
<li>Suggest alternatives</li>
<li>Place orders</li>
</ul>
</div>
<div>
How can I help you today?
</div>
`);
showSuggestions([
"Show sofas under 50,000",
"Best plants for home",
"New arrivals",
"Help me order",
]);
}, 1000);
// Toggle chat visibility
const chatHeader = document.getElementById("chat-header");
const minimizeBtn = document.getElementById("minimize-chat");
const closeBtn = document.getElementById("close-chat");
chatHeader.addEventListener("click", () => {
if (chatContainer.style.display === "none") {
chatContainer.style.display = "flex";
minimizeBtn.textContent = "−";
}
});
minimizeBtn.addEventListener("click", (e) => {
e.stopPropagation();
chatContainer.style.height =
chatContainer.style.height === "60px" ? "500px" : "60px";
minimizeBtn.textContent = chatContainer.style.height === "60px" ? "+" : "−";
});
closeBtn.addEventListener("click", (e) => {
e.stopPropagation();
chatContainer.style.display = "none";
chatButton.style.display = "flex";
});
chatButton.addEventListener("click", () => {
chatContainer.style.display = "flex";
chatButton.style.display = "none";
minimizeBtn.textContent = "−";
chatContainer.style.height = "500px";
});
// Add typing indicator function
window.showTypingIndicator = function () {
const chatBox = document.getElementById("chat-box");
const typingDiv = document.createElement("div");
typingDiv.id = "typing-indicator";
typingDiv.style.cssText = `
text-align: left;
margin: 10px 0;
display: flex;
align-items: center;
gap: 8px;
`;
typingDiv.innerHTML = `
<div style="
display: inline-block;
padding: 8px 12px;
background: #f5f5f5;
border-radius: 18px;
">
<div class="typing-dots">
<div></div>
<div></div>
<div></div>
</div>
</div>
`;
chatBox.appendChild(typingDiv);
chatBox.scrollTop = chatBox.scrollHeight;
// Add typing dots animation
const typingStyle = document.createElement("style");
typingStyle.textContent = `
.typing-dots {
display: flex;
gap: 4px;
}
.typing-dots div {
width: 8px;
height: 8px;
background: #666;
border-radius: 50%;
animation: typingAnimation 1.4s infinite ease-in-out;
}
.typing-dots div:nth-child(1) { animation-delay: 0s; }
.typing-dots div:nth-child(2) { animation-delay: 0.2s; }
.typing-dots div:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingAnimation {
0%, 60%, 100% { transform: translateY(0); }
30% { transform: translateY(-5px); }
}
`;
document.head.appendChild(typingStyle);
};
// Remove typing indicator function
window.hideTypingIndicator = function () {
const typingIndicator = document.getElementById("typing-indicator");
if (typingIndicator) {
typingIndicator.remove();
}
};
}
// ======= Enhanced Chat Message Handling =======
async function sendChatMessage() {
const input = document.getElementById("user-input");
const message = input.value.trim();
const imageUpload = document.getElementById("image-upload");
const chatBox = document.getElementById("chat-box");
if (imageUpload.files[0]) {
// Handle image upload
const file = imageUpload.files[0];
const reader = new FileReader();
reader.onload = async function (e) {
// Display user's image in chat
chatBox.innerHTML += `
<div style="
text-align: right;
margin: 10px 0;
align-self: flex-end;
">
<div style="
display: inline-block;
padding: 8px;
background: #e3f2fd;
border-radius: 18px;
max-width: 80%;
">
<img src="${e.target.result}" style="
max-width: 250px;
max-height: 250px;
border-radius: 8px;
display: block;
">
<div style="
font-size: 12px;
color: #666;
text-align: right;
padding-top: 4px;
">You</div>
</div>
</div>
`;
showTypingIndicator();
try {
const response = await fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ image: e.target.result }),
});
const data = await response.json();
hideTypingIndicator();
displayAssistantResponse(data.reply);
// Show suggestions if available
if (data.suggestions) {
showSuggestions(data.suggestions);
}
} catch (error) {
hideTypingIndicator();
displayAssistantResponse(
"❌ Couldn't process the image. Please try again or describe what you're looking for."
);
}
imageUpload.value = "";
chatBox.scrollTop = chatBox.scrollHeight;
};
reader.readAsDataURL(file);
} else if (message) {
// Handle text message
chatBox.innerHTML += `
<div style="
text-align: right;
margin: 10px 0;
align-self: flex-end;
">
<div style="
display: inline-block;
padding: 10px 14px;
background: #e3f2fd;
border-radius: 18px;
max-width: 80%;
word-wrap: break-word;
">
${message}
<div style="
font-size: 12px;
color: #666;
text-align: right;
padding-top: 4px;
">You</div>
</div>
</div>
`;
input.value = "";
showTypingIndicator();
try {
const response = await fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: message }),
});
const data = await response.json();
hideTypingIndicator();
displayAssistantResponse(data.reply);
// Show suggestions if available
if (data.suggestions) {
showSuggestions(data.suggestions);
}
} catch (error) {
hideTypingIndicator();
displayAssistantResponse(
"❌ Sorry, I'm having trouble responding. Please try again in a moment."
);
}
}
}
// Enhanced assistant response display
function displayAssistantResponse(response) {
const chatBox = document.getElementById("chat-box");
// Format links properly
if (typeof response === "string") {
// Convert URLs to clickable links
response = response.replace(
/(https?:\/\/[^\s]+)/g,
'<a href="$1" target="_blank" style="color:#2196F3;text-decoration:underline">$1</a>'
);
// In your chat interface code
function displayAssistantResponse(response) {
const chatBox = document.getElementById("chat-box");
const msgDiv = document.createElement("div");
msgDiv.className = "assistant-message";
msgDiv.innerHTML = `<p>${response.message || response.reply}</p>`;
if (response.link) {
const link = document.createElement("a");
link.href = response.link.includes('<a href="')
? response.link.match(/href="([^"]*)"/)[1]
: response.link;
link.innerHTML = response.link.includes('<a href="')
? response.link.match(/<a[^>]*>([^<]*)<\/a>/)[1]
: "View Details";
link.target = "_blank";
link.className = "mustard-link";
msgDiv.appendChild(link);
}
chatBox.appendChild(msgDiv);
chatBox.scrollTop = chatBox.scrollHeight;
}
// Format product listings
if (response.includes("name") && response.includes("price")) {
try {
const products = JSON.parse(response.replace(/'/g, '"'));
let html = `
<div style="
text-align: left;
margin: 10px 0;
align-self: flex-start;
">
<div style="
display: inline-block;
padding: 12px;
background: white;
border-radius: 12px;
max-width: 80%;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
">
<div style="font-weight: 600; margin-bottom: 8px;">Products matching your search:</div>
`;
products.forEach((product) => {
html += `
<div style="
margin-bottom: 12px;
padding-bottom: 12px;
border-bottom: 1px solid #eee;
display: flex;
gap: 12px;
align-items: center;
">
<div style="
width: 60px;
height: 60px;
background: #f5f5f5;
border-radius: 8px;
overflow: hidden;
">
<img src="${
product.image || "/static/images/placeholder.jpg"
}" style="
width: 100%;
height: 100%;
object-fit: cover;
">
</div>
<div>
<div style="font-weight: 500;">${product.name}</div>
<div style="font-size: 14px; color: #4CAF50;">Rs. ${product.price.toLocaleString()}</div>
<a href="/product-detail.html?id=${product.id}" style="
color: #2196F3;
font-size: 12px;
text-decoration: none;
">View Details →</a>
</div>
</div>
`;
});
html += `
<div style="margin-top: 8px; font-size: 14px;">
Need help deciding? Just ask!
</div>
</div>
</div>
`;
response = html;
} catch (e) {
console.error("Product parsing error:", e);
}
}
}
// Add timestamp
const now = new Date();
const timeString = now.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
});
chatBox.innerHTML += `
<div style="
text-align: left;
margin: 10px 0;
align-self: flex-start;
">
<div style="
display: inline-block;
padding: 12px 16px;
background: white;
border-radius: 18px;
max-width: 80%;
word-wrap: break-word;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
">
${response}
<div style="
font-size: 11px;
color: #999;
text-align: right;
padding-top: 4px;
display: flex;
align-items: center;
justify-content: flex-end;
gap: 4px;
">
<span>Assistant</span>
<span>•</span>
<span>${timeString}</span>
</div>
</div>
</div>
`;
chatBox.scrollTop = chatBox.scrollHeight;
}
// Show quick reply suggestions
function showSuggestions(suggestions) {
const suggestionBox = document.getElementById("suggestion-box");
suggestionBox.innerHTML = "";
suggestions.forEach((suggestion) => {
const button = document.createElement("button");
button.textContent = suggestion;
button.style.cssText = `
padding: 8px 12px;
background: white;
border: 1px solid #ddd;
border-radius: 16px;
font-size: 13px;
cursor: pointer;
white-space: nowrap;
transition: all 0.2s;
`;
button.addEventListener("mouseenter", () => {
button.style.background = "#f5f5f5";
});
button.addEventListener("mouseleave", () => {
button.style.background = "white";
});
button.addEventListener("click", () => {
document.getElementById("user-input").value = suggestion;
sendChatMessage();
});
suggestionBox.appendChild(button);
});
}
// ======= Event Listeners =======
document.addEventListener("DOMContentLoaded", () => {
initChatInterface();
// Handle Enter key in input
const input = document.getElementById("user-input");
input?.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
sendChatMessage();
}
});
// Handle send button click
const sendButton = document.getElementById("send-button");
sendButton?.addEventListener("click", sendChatMessage);
// Handle image upload
const imageUpload = document.getElementById("image-upload");
imageUpload?.addEventListener("change", () => {
if (imageUpload.files[0]) {
sendChatMessage();
}
});
});
// ======= Navbar Section =======
const menuIcon = document.getElementById("menuIcon");
const navLinks = document.querySelector(".nav-links");
menuIcon.addEventListener("click", () => {
navLinks.classList.toggle("active");
});
// ======= Product Cards Section =======
document.addEventListener("DOMContentLoaded", () => {
const items = {
plant: [
"/static/images/plant3.png",
"/static/images/plant4.webp",
"/static/images/plant5.png",
"/static/images/plant6.png",
],
table: [
"/static/images/table2.png",
"/static/images/table3.webp",
"/static/images/table4.webp",
"/static/images/table5.png",
],
lamp: [
"/static/images/lamp1.webp",
"/static/images/lamp2.webp",
"/static/images/lamp3.webp",
"/static/images/lamp4.png",
"/static/images/lamp5.webp",
"/static/images/lamp6.webp",
],
sofa: [
"/static/images/sofa3.webp",
"/static/images/sofa4.webp",
"/static/images/sofa5.png",
"/static/images/sofa6.png",
],
};
document.querySelectorAll(".plus-btn").forEach((button) => {
button.addEventListener("click", () => {
const item = button.dataset.item;
const card = document.getElementById(`${item}-card`);
const mainImage = button.previousElementSibling;
// Hide if already active
if (card.classList.contains("active")) {
card.innerHTML = "";
card.classList.remove("active");
return;
}
// 🔥 Apply card styling directly from JS
Object.assign(card.style, {
display: "grid",
gridTemplateColumns: "repeat(auto-fit, minmax(80px, 1fr))",
gap: "10px",
padding: "10px",
border: "2px solid #ccc",
borderRadius: "10px",
marginTop: "12px",
maxWidth: "100%",
backgroundColor: "#f5f5f5",
});
card.innerHTML = "";
items[item].forEach((imgSrc) => {
const img = document.createElement("img");
img.src = imgSrc;
img.alt = `${item} option`;
// 🧠 Apply image styles
Object.assign(img.style, {
width: "100%",
maxWidth: "80px",
height: "80px",
objectFit: "cover",
borderRadius: "8px",
cursor: "pointer",
transition: "transform 0.2s, border 0.2s",
border: "2px solid transparent",
});
img.addEventListener("mouseenter", () => {
img.style.transform = "scale(1.1)";
img.style.border = "2px solid #48c6ef";
});
img.addEventListener("mouseleave", () => {
img.style.transform = "scale(1)";
img.style.border = "2px solid transparent";
});
img.addEventListener("click", () => {
mainImage.src = imgSrc;
});
card.appendChild(img);
});
card.classList.add("active");
});
});
});