PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.3.4
MxChat – AI Chatbot & Content Generation for WordPress v2.3.4
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | js/chat-script.js +2391 -1558 2.0.32.3.4 View file →
@@ -1,1558 +1,2391 @@
1 -jQuery(document).ready(function($) {
2 -//console.log('mxchatChat object:', mxchatChat);
3 -//console.log('Link Target Toggle Value:', mxchatChat.link_target_toggle);
4 -// Add these variables at the top of your chat-script.js file
5 - const toolbarIconColor = mxchatChat.toolbar_icon_color || '#212121';
6 -
7 - // Initialize color settings
8 - var userMessageBgColor = mxchatChat.user_message_bg_color;
9 - var userMessageFontColor = mxchatChat.user_message_font_color;
10 - var botMessageBgColor = mxchatChat.bot_message_bg_color;
11 - var botMessageFontColor = mxchatChat.bot_message_font_color;
12 - // Add live agent message colors
13 - var liveAgentMessageBgColor = mxchatChat.live_agent_message_bg_color;
14 - var liveAgentMessageFontColor = mxchatChat.live_agent_message_font_color;
15 -
16 -
17 - var linkTarget = mxchatChat.link_target_toggle === 'on' ? '_blank' : '_self';
18 - let lastSeenMessageId = '';
19 - let notificationCheckInterval;
20 - let notificationBadge;
21 - // Initialize session ID
22 - var sessionId = getChatSession();
23 -
24 - let pollingInterval; // Variable to store the interval ID
25 - let processedMessageIds = new Set(); // Add this at the top with your other variables
26 -//console.log('Live Agent BG Color:', liveAgentMessageBgColor);
27 -//console.log('Live Agent Font Color:', liveAgentMessageFontColor);
28 - let activePdfFile = null;
29 - let activeWordFile = null;
30 -
31 -
32 -// Function to create and append notification badge
33 -// Function to create and append notification badge
34 -function createNotificationBadge() {
35 - console.log("Creating notification badge...");
36 - const chatButton = document.getElementById('floating-chatbot-button');
37 - console.log("Chat button found:", !!chatButton);
38 -
39 - if (!chatButton) return;
40 -
41 - // Remove any existing badge first
42 - const existingBadge = chatButton.querySelector('.chat-notification-badge');
43 - if (existingBadge) {
44 - console.log("Removing existing badge");
45 - existingBadge.remove();
46 - }
47 -
48 - notificationBadge = document.createElement('div');
49 - notificationBadge.className = 'chat-notification-badge';
50 - notificationBadge.style.cssText = `
51 - display: none;
52 - position: absolute;
53 - top: -5px;
54 - right: -5px;
55 - background-color: red;
56 - color: white;
57 - border-radius: 50%;
58 - padding: 4px 8px;
59 - font-size: 12px;
60 - font-weight: bold;
61 - z-index: 10001;
62 - `;
63 - chatButton.style.position = 'relative';
64 - chatButton.appendChild(notificationBadge);
65 -
66 - console.log("Notification badge created and appended:", {
67 - exists: !!notificationBadge,
68 - parent: notificationBadge?.parentNode?.id,
69 - display: notificationBadge?.style?.display
70 - });
71 -}
72 -
73 -// Function to check for new messages
74 -function checkForNewMessages() {
75 - const sessionId = getChatSession();
76 - const chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
77 -
78 - if (!chatPersistenceEnabled) return;
79 -
80 - $.ajax({
81 - url: mxchatChat.ajax_url,
82 - type: 'POST',
83 - data: {
84 - action: 'mxchat_check_new_messages',
85 - session_id: sessionId,
86 - last_seen_id: lastSeenMessageId,
87 - nonce: mxchatChat.nonce
88 - },
89 - success: function(response) {
90 - if (response.success && response.data.hasNewMessages) {
91 - showNotification();
92 - }
93 - }
94 - });
95 -}
96 -
97 -// Function to show notification
98 -function showNotification() {
99 - const badge = document.getElementById('chat-notification-badge');
100 - if (badge && $('#floating-chatbot').hasClass('hidden')) {
101 - badge.style.display = 'block';
102 - badge.textContent = '1';
103 - }
104 -}
105 -
106 -function hideNotification() {
107 - const badge = document.getElementById('chat-notification-badge');
108 - if (badge) {
109 - badge.style.display = 'none';
110 - }
111 -}
112 -// Function to start notification checking
113 -function startNotificationChecking() {
114 - const chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
115 - if (!chatPersistenceEnabled) return;
116 -
117 - createNotificationBadge();
118 - notificationCheckInterval = setInterval(checkForNewMessages, 30000); // Check every 30 seconds
119 -}
120 -
121 -// Function to stop notification checking
122 -function stopNotificationChecking() {
123 - if (notificationCheckInterval) {
124 - clearInterval(notificationCheckInterval);
125 - }
126 -}
127 -
128 -
129 -
130 -
131 -function getChatSession() {
132 - var sessionId = getCookie('mxchat_session_id');
133 - //console.log("Session ID retrieved from cookie: ", sessionId);
134 -
135 - if (!sessionId) {
136 - sessionId = generateSessionId();
137 - //console.log("Generated new session ID: ", sessionId);
138 - setChatSession(sessionId);
139 - }
140 -
141 - //console.log("Final session ID: ", sessionId);
142 - return sessionId;
143 -}
144 -
145 -function setChatSession(sessionId) {
146 - // Set the cookie with a 24-hour expiration (86400 seconds)
147 - document.cookie = "mxchat_session_id=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
148 -}
149 -
150 -// Get cookie value by name
151 -function getCookie(name) {
152 - let value = "; " + document.cookie;
153 - let parts = value.split("; " + name + "=");
154 - if (parts.length == 2) return parts.pop().split(";").shift();
155 -}
156 -
157 -// Generate a new session ID
158 -function generateSessionId() {
159 - return 'mxchat_chat_' + Math.random().toString(36).substr(2, 9);
160 -}
161 -
162 -// Function to send the message to the chatbot (backend)
163 -function sendMessageToChatbot(message) {
164 - var sessionId = getChatSession(); // Reuse the session ID logic
165 -
166 - // Hide the popular questions section
167 - $('#mxchat-popular-questions').hide();
168 -
169 - // Show thinking indicator (no need to append the user's message again)
170 - appendThinkingMessage();
171 - scrollToBottom();
172 -
173 - //console.log("Sending message to chatbot:", message); // Log the message
174 - //console.log("Session ID:", sessionId); // Log the session ID
175 -
176 - // Call the chatbot using the same call logic as sendMessage
177 - callMxChat(message, function(response) {
178 - // ** Ensure temporary thinking message is removed before adding new response **
179 - $('.temporary-message').remove();
180 -
181 - // Replace thinking indicator with actual response
182 - replaceLastMessage("bot", response);
183 - });
184 -}
185 -
186 -
187 -
188 -
189 -function sendMessage() {
190 - var message = $('#chat-input').val(); // Get value from textarea
191 - if (message) {
192 - appendMessage("user", message); // Append user's message
193 - $('#chat-input').val(''); // Clear the textarea
194 - $('#chat-input').css('height', 'auto'); // Reset height after clearing content
195 -
196 - // Hide the popular questions section
197 - $('#mxchat-popular-questions').hide();
198 -
199 - // Show typing indicator
200 - appendThinkingMessage();
201 - scrollToBottom();
202 -
203 - callMxChat(message, function(response) {
204 - // Replace typing indicator with actual response
205 - replaceLastMessage("bot", response);
206 - });
207 - }
208 -}
209 -
210 -
211 -
212 - // Function to append a thinking message with animation
213 - function appendThinkingMessage() {
214 - // Remove any existing thinking dots first
215 - $('.thinking-dots').remove();
216 -
217 - // Retrieve the bot message font color and background color
218 - var botMessageFontColor = mxchatChat.bot_message_font_color;
219 - var botMessageBgColor = mxchatChat.bot_message_bg_color;
220 -
221 -
222 - var thinkingHtml = '<div class="thinking-dots-container">' +
223 - '<div class="thinking-dots">' +
224 - '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
225 - '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
226 - '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
227 - '</div>' +
228 - '</div>';
229 -
230 - // Append the thinking dots to the chat container (or within the temporary message div)
231 - $("#chat-box").append('<div class="bot-message temporary-message" style="background-color: ' + botMessageBgColor + ';">' + thinkingHtml + '</div>');
232 - scrollToBottom();
233 - }
234 -
235 - // Trigger send button click when "Enter" key is pressed in the textarea
236 - $('#chat-input').keypress(function(e) {
237 - if (e.which == 13 && !e.shiftKey) { // Check if "Enter" is pressed without Shift
238 - e.preventDefault(); // Prevent default "Enter" behavior
239 - $('#send-button').click(); // Trigger send button click
240 - }
241 - });
242 -
243 - // Handle send button click
244 - $('#send-button').click(function() {
245 - sendMessage();
246 - });
247 -
248 - // Handle click on popular questions
249 - $('.mxchat-popular-question').on('click', function () {
250 - var question = $(this).text(); // Get the text of the clicked question
251 -
252 - // Append the question as if the user typed it
253 - appendMessage("user", question);
254 -
255 - // Send the question to the server (backend)
256 - sendMessageToChatbot(question);
257 - });
258 -
259 -
260 -// Use the linkTarget in your linkify function
261 -function linkify(inputText) {
262 - // Check for already linked URLs and skip them
263 - // We use negative lookaheads to skip anything already in an <a> tag
264 - var markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s]+)\)/g;
265 - var replacedText = inputText.replace(markdownLinkPattern, '<a href="$2" target="' + linkTarget + '">$1</a>');
266 -
267 - // Replace standalone URLs not already in an <a> tag
268 - var urlPattern = /(^|[^">])(https?:\/\/[^\s<]+)/gim;
269 - replacedText = replacedText.replace(urlPattern, '$1<a href="$2" target="' + linkTarget + '">$2</a>');
270 -
271 - // Replace "www." prefixed URLs not already in an <a> tag
272 - var wwwPattern = /(^|[^">])(www\.[\S]+(\b|$))(?![^<]*<\/a>)/gim;
273 - replacedText = replacedText.replace(wwwPattern, '$1<a href="http://$2" target="' + linkTarget + '">$2</a>');
274 -
275 - return replacedText;
276 -}
277 -
278 -
279 -function scrollElementToTop(element) {
280 - var chatBox = $('#chat-box');
281 - var elementTop = element.position().top + chatBox.scrollTop();
282 - chatBox.animate({ scrollTop: elementTop }, 500);
283 -}
284 -
285 -
286 -// Optimized scrollToBottom function for instant scrolling
287 -function scrollToBottom(instant = false) {
288 - var chatBox = $('#chat-box');
289 - if (instant) {
290 - // Instantly set the scroll position to the bottom
291 - chatBox.scrollTop(chatBox.prop("scrollHeight"));
292 - } else {
293 - // Use requestAnimationFrame for smoother scrolling if needed
294 - let start = null;
295 - const scrollHeight = chatBox.prop("scrollHeight");
296 - const initialScroll = chatBox.scrollTop();
297 - const distance = scrollHeight - initialScroll;
298 - const duration = 500; // Duration in ms
299 -
300 - function smoothScroll(timestamp) {
301 - if (!start) start = timestamp;
302 - const progress = timestamp - start;
303 - const currentScroll = initialScroll + (distance * (progress / duration));
304 - chatBox.scrollTop(currentScroll);
305 -
306 - if (progress < duration) {
307 - requestAnimationFrame(smoothScroll);
308 - } else {
309 - chatBox.scrollTop(scrollHeight); // Ensure it's exactly at the bottom
310 - }
311 - }
312 -
313 - requestAnimationFrame(smoothScroll);
314 - }
315 -}
316 -
317 -
318 - // Function to format text with **bold** inside double asterisks
319 - function formatBoldText(text) {
320 - return text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
321 - }
322 -
323 - // Function to convert newline characters to HTML line breaks and handle paragraph spacing
324 -function convertNewlinesToBreaks(text) {
325 - // Split the text into paragraphs (marked by double newlines or multiple <br> tags)
326 - const paragraphs = text.split(/(?:\n\n|\<br\>\s*\<br\>)/g);
327 -
328 - // Wrap each paragraph in <p> tags
329 - return paragraphs
330 - .map(para => `<p>${para.trim()}</p>`)
331 - .join('');
332 -}
333 - // Copy to clipboard function
334 - // Function to copy text to clipboard
335 - function copyToClipboard(text) {
336 - var tempInput = $('<input>');
337 - $('body').append(tempInput);
338 - tempInput.val(text).select();
339 - document.execCommand('copy');
340 - tempInput.remove();
341 - }
342 -
343 -
344 -function updateChatModeIndicator(mode) {
345 - const indicator = document.getElementById('chat-mode-indicator');
346 - if (indicator) {
347 - indicator.textContent = mode === 'agent' ? 'Live Agent' : 'AI Agent';
348 - }
349 -
350 - // Start or stop polling based on mode
351 - if (mode === 'agent') {
352 - startPolling();
353 - } else {
354 - stopPolling();
355 - }
356 -}
357 -
358 -function callMxChat(message, callback) {
359 - $.ajax({
360 - url: mxchatChat.ajax_url,
361 - type: 'POST',
362 - dataType: 'json',
363 - data: {
364 - action: 'mxchat_handle_chat_request',
365 - message: message,
366 - session_id: getChatSession(),
367 - nonce: mxchatChat.nonce
368 - },
369 - success: function(response) {
370 - // Existing chat mode check
371 - if (response.chat_mode) {
372 - updateChatModeIndicator(response.chat_mode);
373 - }
374 - else if (response.fallbackResponse && response.fallbackResponse.chat_mode) {
375 - updateChatModeIndicator(response.fallbackResponse.chat_mode);
376 - }
377 -
378 - // Add PDF filename handling
379 - if (response.data && response.data.filename) {
380 - showActivePdf(response.data.filename);
381 - activePdfFile = response.data.filename;
382 - }
383 -
384 - // Add redirect check here
385 - if (response.redirect_url) {
386 - let responseText = response.text || '';
387 - if (responseText) {
388 - replaceLastMessage("bot", responseText);
389 - }
390 - setTimeout(() => {
391 - window.location.href = response.redirect_url;
392 - }, 1500);
393 - return;
394 - }
395 -
396 - // Check for live agent response
397 - if (response.success && response.data && response.data.status === 'waiting_for_agent') {
398 - updateChatModeIndicator('agent');
399 - return;
400 - }
401 -
402 - // Handle other responses
403 - let responseText = response.text || '';
404 - let responseHtml = response.html || '';
405 - let responseMessage = response.message || '';
406 -
407 - if (responseText === 'You are now chatting with the AI chatbot.') {
408 - updateChatModeIndicator('ai');
409 - }
410 -
411 - // Handle the message and show notification if chat is hidden
412 - if (responseText || responseHtml || responseMessage) {
413 - // Update the messages as before
414 - if (responseText && responseHtml) {
415 - replaceLastMessage("bot", responseText, responseHtml);
416 - } else if (responseText) {
417 - replaceLastMessage("bot", responseText);
418 - } else if (responseHtml) {
419 - replaceLastMessage("bot", "", responseHtml);
420 - } else if (responseMessage) {
421 - replaceLastMessage("bot", responseMessage);
422 - }
423 -
424 - // Check if chat is hidden and show notification
425 - if ($('#floating-chatbot').hasClass('hidden')) {
426 - const badge = $('#chat-notification-badge');
427 - if (badge.length) {
428 - badge.show();
429 - }
430 - }
431 - } else {
432 - console.error("Unexpected response format:", response);
433 - replaceLastMessage("bot", "I'm sorry, something went wrong.");
434 - }
435 -
436 - if (response.message_id) {
437 - lastSeenMessageId = response.message_id;
438 - }
439 - },
440 - error: function(xhr, status, error) {
441 - replaceLastMessage("bot", "An unexpected error occurred.");
442 - }
443 - });
444 -}
445 -
446 -function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false) {
447 - //console.log("Appending message. Sender:", sender, "Content:", messageText);
448 -
449 - try {
450 - // Determine styles based on sender type
451 - let messageClass, bgColor, fontColor;
452 -
453 - if (sender === "user") {
454 - messageClass = "user-message";
455 - bgColor = userMessageBgColor;
456 - fontColor = userMessageFontColor;
457 - } else if (sender === "agent") {
458 - messageClass = "agent-message";
459 - bgColor = liveAgentMessageBgColor;
460 - fontColor = liveAgentMessageFontColor;
461 - } else {
462 - messageClass = "bot-message";
463 - bgColor = botMessageBgColor;
464 - fontColor = botMessageFontColor;
465 - }
466 -
467 - const messageDiv = $('<div>')
468 - .addClass(messageClass)
469 - .css({
470 - 'background': bgColor,
471 - 'color': fontColor,
472 - });
473 -
474 - // Add CSS for paragraphs
475 - messageDiv.css({
476 - 'margin-bottom': '1em'
477 - });
478 -
479 - // Format and process the message content
480 - let fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(messageText))));
481 -
482 - // Add images if provided
483 - if (images && images.length > 0) {
484 - fullMessage += '<div class="image-gallery">';
485 - images.forEach(img => {
486 - fullMessage += `
487 - <div style="margin-bottom: 10px;">
488 - <strong>${img.title}</strong><br>
489 - <a href="${img.image_url}" target="_blank">
490 - <img src="${img.thumbnail_url}" alt="${img.title}" style="max-width: 100px; height: auto; margin: 5px;" />
491 - </a>
492 - </div>`;
493 - });
494 - fullMessage += '</div>';
495 - }
496 -
497 - // Append HTML content if provided
498 - if (messageHtml) {
499 - fullMessage += '<br><br>' + messageHtml;
500 - }
501 -
502 - messageDiv.html(fullMessage);
503 -
504 - // Add a class for temporary messages if needed
505 - if (isTemporary) {
506 - messageDiv.addClass('temporary-message');
507 - }
508 -
509 - // Append the message to the chat box
510 - messageDiv.hide().appendTo('#chat-box').fadeIn(300, function () {
511 - if (sender === "bot") {
512 - // After bot's message is displayed, scroll the last user message to the top
513 - const lastUserMessage = $('#chat-box').find('.user-message').last();
514 - if (lastUserMessage.length) {
515 - scrollElementToTop(lastUserMessage);
516 - }
517 - }
518 - });
519 -
520 - if (messageText.id) {
521 - lastSeenMessageId = messageText.id;
522 - hideNotification();
523 - }
524 - } catch (error) {
525 - console.error("Error rendering message with images:", error);
526 - }
527 -}
528 -
529 -
530 -function replaceLastMessage(sender, responseText, responseHtml = '', images = []) {
531 - var messageClass = sender === "user" ? "user-message" : sender === "agent" ? "agent-message" : "bot-message";
532 - var lastMessageDiv = $('#chat-box').find('.bot-message.temporary-message, .agent-message.temporary-message').last();
533 -
534 - // Determine styles
535 - let bgColor, fontColor;
536 - if (sender === "user") {
537 - bgColor = userMessageBgColor;
538 - fontColor = userMessageFontColor;
539 - } else if (sender === "agent") {
540 - bgColor = liveAgentMessageBgColor;
541 - fontColor = liveAgentMessageFontColor;
542 - } else {
543 - bgColor = botMessageBgColor;
544 - fontColor = botMessageFontColor;
545 - }
546 -
547 - var fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(responseText))));
548 - if (responseHtml) {
549 - fullMessage += '<br><br>' + responseHtml;
550 - }
551 -
552 - if (images.length > 0) {
553 - fullMessage += '<div class="image-gallery">';
554 - images.forEach(img => {
555 - fullMessage += `
556 - <div style="margin-bottom: 10px;">
557 - <strong>${img.title}</strong><br>
558 - <a href="${img.image_url}" target="_blank">
559 - <img src="${img.thumbnail_url}" alt="${img.title}" style="max-width: 100px; height: auto; margin: 5px;" />
560 - </a>
561 - </div>`;
562 - });
563 - fullMessage += '</div>';
564 - }
565 -
566 - if (lastMessageDiv.length) {
567 - lastMessageDiv.fadeOut(200, function() {
568 - $(this)
569 - .html(fullMessage)
570 - .removeClass('bot-message user-message')
571 - .addClass(messageClass)
572 - .css({
573 - 'background-color': bgColor,
574 - 'color': fontColor,
575 - })
576 - .removeClass('temporary-message')
577 - .fadeIn(200, function() {
578 - if (sender === "bot" || sender === "agent") {
579 - const lastUserMessage = $('#chat-box').find('.user-message').last();
580 - if (lastUserMessage.length) {
581 - scrollElementToTop(lastUserMessage);
582 - }
583 - // Show notification if chat is hidden
584 - if ($('#floating-chatbot').hasClass('hidden')) {
585 - showNotification();
586 - }
587 - }
588 - });
589 - });
590 - } else {
591 - appendMessage(sender, responseText, responseHtml, images);
592 - }
593 -}
594 -
595 -
596 -function startPolling() {
597 - // Clear any existing interval first
598 - stopPolling();
599 - // Start new polling interval
600 - pollingInterval = setInterval(checkForAgentMessages, 5000);
601 - //console.log("Started agent message polling");
602 -}
603 -
604 -function stopPolling() {
605 - if (pollingInterval) {
606 - clearInterval(pollingInterval);
607 - pollingInterval = null;
608 - //console.log("Stopped agent message polling");
609 - }
610 -}
611 -
612 -
613 -// Update your checkForAgentMessages function
614 -function checkForAgentMessages() {
615 - const sessionId = getChatSession();
616 - $.ajax({
617 - url: mxchatChat.ajax_url,
618 - type: 'POST',
619 - dataType: 'json',
620 - data: {
621 - action: 'mxchat_fetch_new_messages',
622 - session_id: sessionId,
623 - last_seen_id: lastSeenMessageId,
624 - nonce: mxchatChat.nonce
625 - },
626 - success: function (response) {
627 - if (response.success && response.data?.new_messages) {
628 - let hasNewMessage = false;
629 -
630 - response.data.new_messages.forEach(function (message) {
631 - if (message.role === "agent" && !processedMessageIds.has(message.id)) {
632 - hasNewMessage = true;
633 - replaceLastMessage("agent", message.content);
634 - lastSeenMessageId = message.id;
635 - processedMessageIds.add(message.id);
636 - }
637 - });
638 -
639 - if (hasNewMessage && $('#floating-chatbot').hasClass('hidden')) {
640 - showNotification();
641 - }
642 -
643 - scrollToBottom(true);
644 - }
645 - },
646 - error: function (xhr, status, error) {
647 - console.error("Polling error:", xhr, status, error);
648 - }
649 - });
650 -}
651 -function loadChatHistory() {
652 - var sessionId = getChatSession();
653 - var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
654 -
655 - if (chatPersistenceEnabled && sessionId) {
656 - $.ajax({
657 - url: mxchatChat.ajax_url,
658 - type: 'POST',
659 - dataType: 'json',
660 - data: {
661 - action: 'mxchat_fetch_conversation_history',
662 - session_id: sessionId
663 - },
664 - success: function(response) {
665 - if (response.success && response.data && Array.isArray(response.data.conversation)) {
666 -
667 -
668 - var $chatBox = $('#chat-box');
669 - var $fragment = $(document.createDocumentFragment());
670 - let highestMessageId = lastSeenMessageId;
671 -
672 - if (response.data.chat_mode) {
673 - updateChatModeIndicator(response.data.chat_mode);
674 - }
675 -
676 - $.each(response.data.conversation, function(index, message) {
677 - // Skip agent messages if persistence is off
678 - if (!chatPersistenceEnabled && message.role === 'agent') {
679 - return;
680 - }
681 -
682 - var messageClass, messageBgColor, messageFontColor;
683 -
684 - switch (message.role) {
685 - case 'user':
686 - messageClass = 'user-message';
687 - messageBgColor = userMessageBgColor;
688 - messageFontColor = userMessageFontColor;
689 - break;
690 - case 'agent':
691 - messageClass = 'agent-message';
692 - messageBgColor = liveAgentMessageBgColor;
693 - messageFontColor = liveAgentMessageFontColor;
694 - break;
695 - default:
696 - messageClass = 'bot-message';
697 - messageBgColor = botMessageBgColor;
698 - messageFontColor = botMessageFontColor;
699 - break;
700 - }
701 -
702 - var messageElement = $('<div>').addClass(messageClass)
703 - .css({
704 - 'background': messageBgColor,
705 - 'color': messageFontColor
706 - });
707 -
708 - var content = message.content;
709 - content = content.replace(/\\'/g, "'").replace(/\\"/g, '"');
710 - content = decodeHTMLEntities(content);
711 -
712 - if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) {
713 - messageElement.html(content);
714 - } else {
715 - var formattedContent = linkify(
716 - formatBoldText(
717 - convertNewlinesToBreaks(formatCodeBlocks(content))
718 - )
719 - );
720 - messageElement.html(formattedContent);
721 - }
722 -
723 - $fragment.append(messageElement);
724 -
725 - // In loadChatHistory, change this part:
726 - if (message.id) {
727 - highestMessageId = Math.max(highestMessageId, message.id);
728 - processedMessageIds.add(message.id); // Add all message IDs to processed set
729 - }
730 - });
731 -
732 - $chatBox.append($fragment);
733 - scrollToBottom(true);
734 -
735 - if (response.data.conversation.length > 0) {
736 - $('#mxchat-popular-questions').hide();
737 - }
738 -
739 - // Update lastSeenMessageId after history loads
740 - lastSeenMessageId = highestMessageId;
741 -
742 - // Only update chat mode if persistence is enabled
743 - if (chatPersistenceEnabled && response.data.conversation.length > 0) {
744 - var lastMessage = response.data.conversation[response.data.conversation.length - 1];
745 - if (lastMessage.role === 'agent') {
746 - updateChatModeIndicator('agent');
747 - }
748 - }
749 - } else {
750 - console.warn("No conversation history found.");
751 - }
752 - },
753 - error: function(xhr, status, error) {
754 - console.error("Error loading chat history:", status, error);
755 - appendMessage("bot", "Unable to load chat history.");
756 - }
757 - });
758 - } else {
759 - console.warn("Chat persistence is disabled or no session ID found. Not loading history.");
760 - }
761 -}
762 -
763 -// Function to decode HTML entities
764 -function decodeHTMLEntities(text) {
765 - var textArea = document.createElement('textarea');
766 - textArea.innerHTML = text;
767 - return textArea.value;
768 -}
769 -
770 -
771 -// Update formatCodeBlocks function
772 -function formatCodeBlocks(text) {
773 - // First handle raw PHP tags
774 - text = text.replace(/(<\?php[\s\S]*?\?>)/g, (match) => {
775 - return `<pre><code class="language-php">${escapeHtml(match)}</code></pre>`;
776 - });
777 -
778 - // Then handle code blocks with backticks
779 - text = text.replace(/```php5?\n([\s\S]+?)```/gi, (match, code) => {
780 - return `<pre><code class="language-php">${escapeHtml(code)}</code></pre>`;
781 - });
782 -
783 - return text;
784 -}
785 -
786 -// Update escapeHtml function to preserve existing code blocks
787 -function escapeHtml(unsafe) {
788 - // First check if it's already a code block
789 - if (unsafe.includes('<pre><code') || unsafe.includes('</code></pre>')) {
790 - return unsafe;
791 - }
792 -
793 - return unsafe
794 - .replace(/&/g, "&amp;")
795 - .replace(/</g, "&lt;")
796 - .replace(/>/g, "&gt;")
797 - .replace(/"/g, "&quot;")
798 - .replace(/'/g, "&#039;");
799 -}
800 -// Utility function to escape HTML
801 -function escapeHtml(unsafe) {
802 - return unsafe
803 - .replace(/&/g, "&amp;")
804 - .replace(/</g, "&lt;")
805 - .replace(/>/g, "&gt;")
806 - .replace(/"/g, "&quot;")
807 - .replace(/'/g, "&#039;");
808 -}
809 -
810 -
811 -
812 -
813 -// Function to convert newlines, skipping preformatted text
814 -function convertNewlinesToBreaks(text) {
815 - // Split while preserving code blocks
816 - return text.split(/(<pre\b[^>]*>[\s\S]*?<\/pre>)/g).map(part => {
817 - if (part.startsWith('<pre')) return part;
818 - return part.replace(/(^|[^>])\n/g, '$1<br>');
819 - }).join('');
820 -}
821 -
822 -
823 -
824 -
825 - // Helper function to check if a string is an image HTML
826 - function isImageHtml(str) {
827 - return str.startsWith('<img') && str.endsWith('>');
828 - }
829 -
830 - // Function to remove thinking dots
831 - function removeThinkingDots() {
832 - $('.thinking-dots').closest('.temporary-message').remove();
833 - }
834 -
835 - function isMobile() {
836 - // This can be a simple check, or more sophisticated detection of mobile devices
837 - return window.innerWidth <= 768; // Example threshold for mobile devices
838 - }
839 -
840 - function disableScroll() {
841 - if (isMobile()) {
842 - $('body').css('overflow', 'hidden');
843 - }
844 - }
845 -
846 - function enableScroll() {
847 - if (isMobile()) {
848 - $('body').css('overflow', '');
849 - }
850 - }
851 -
852 -// Pre-chat dismissal check function (wrapped in a function for reuse)
853 - function checkPreChatDismissal() {
854 - $.ajax({
855 - url: mxchatChat.ajax_url,
856 - type: 'POST',
857 - data: {
858 - action: 'mxchat_check_pre_chat_message_status',
859 - _ajax_nonce: mxchatChat.nonce
860 - },
861 - success: function(response) {
862 - if (response.success && !response.data.dismissed) {
863 - $('#pre-chat-message').fadeIn(250);
864 - } else {
865 - $('#pre-chat-message').hide();
866 - }
867 - },
868 - error: function() {
869 - console.error('Failed to check pre-chat message dismissal status.');
870 - }
871 - });
872 - }
873 -
874 - // Function to show the chatbot widget
875 -function showChatWidget() {
876 - // First ensure display is set
877 - $('#floating-chatbot-button').css('display', 'flex');
878 - // Then handle the fade
879 - $('#floating-chatbot-button').fadeTo(500, 1);
880 - // Force visibility
881 - $('#floating-chatbot-button').removeClass('hidden');
882 - //console.log('Showing widget');
883 -}
884 -
885 -// Function to hide the chatbot widget
886 -function hideChatWidget() {
887 - $('#floating-chatbot-button').css('display', 'none');
888 - $('#floating-chatbot-button').addClass('hidden');
889 - //console.log('Hiding widget');
890 -}
891 -
892 -function initializeChatVisibility() {
893 - //console.log('Initializing chat visibility');
894 - const complianzEnabled = mxchatChat.complianz_toggle === 'on' ||
895 - mxchatChat.complianz_toggle === '1' ||
896 - mxchatChat.complianz_toggle === 1;
897 -
898 - if (complianzEnabled && typeof cmplz_has_consent === "function" && typeof complianz !== 'undefined') {
899 - // Initial check
900 - checkConsentAndShowChat();
901 -
902 - // Listen for consent changes
903 - $(document).on('cmplz_status_change', function(event) {
904 - //console.log('Status change detected');
905 - checkConsentAndShowChat();
906 - });
907 - } else {
908 - // If Complianz is not enabled, always show
909 - $('#floating-chatbot-button')
910 - .css('display', 'flex')
911 - .removeClass('hidden no-consent')
912 - .fadeTo(500, 1);
913 -
914 - // Also check pre-chat message when Complianz is not enabled
915 - checkPreChatDismissal();
916 - }
917 -}
918 -
919 -
920 -
921 -function checkConsentAndShowChat() {
922 - var consentStatus = cmplz_has_consent('marketing');
923 - var consentType = complianz.consenttype;
924 -
925 - //console.log('Checking consent:', {status: consentStatus,type: consentType});
926 -
927 - let $widget = $('#floating-chatbot-button');
928 - let $chatbot = $('#floating-chatbot');
929 - let $preChat = $('#pre-chat-message');
930 -
931 - if (consentStatus === true) {
932 - //console.log('Consent granted - showing widget');
933 - $widget
934 - .removeClass('no-consent')
935 - .css('display', 'flex')
936 - .removeClass('hidden')
937 - .fadeTo(500, 1);
938 - $chatbot.removeClass('no-consent');
939 -
940 - // Show pre-chat message if not dismissed
941 - checkPreChatDismissal();
942 - } else {
943 - //console.log('No consent - hiding widget');
944 - $widget
945 - .addClass('no-consent')
946 - .fadeTo(500, 0, function() {
947 - $(this)
948 - .css('display', 'none')
949 - .addClass('hidden');
950 - });
951 - $chatbot.addClass('no-consent');
952 -
953 - // Hide pre-chat message when no consent
954 - $preChat.hide();
955 - }
956 -}
957 -
958 - // Function to dismiss pre-chat message for 24 hours
959 - function handlePreChatDismissal() {
960 - $('#pre-chat-message').fadeOut(200);
961 - $.ajax({
962 - url: mxchatChat.ajax_url,
963 - type: 'POST',
964 - data: {
965 - action: 'mxchat_dismiss_pre_chat_message',
966 - _ajax_nonce: mxchatChat.nonce
967 - },
968 - success: function() {
969 - $('#pre-chat-message').hide();
970 - },
971 - error: function() {
972 - console.error('Failed to dismiss pre-chat message.');
973 - }
974 - });
975 - }
976 -
977 - // Handle pre-chat message dismissal on button click
978 - $(document).on('click', '.close-pre-chat-message', function(e) {
979 - e.stopPropagation();
980 - handlePreChatDismissal();
981 - });
982 -
983 - // Toggle chatbot visibility on floating button click
984 - $(document).on('click', '#floating-chatbot-button', function() {
985 - var chatbot = $('#floating-chatbot');
986 - if (chatbot.hasClass('hidden')) {
987 - chatbot.removeClass('hidden').addClass('visible');
988 - $(this).addClass('hidden');
989 - $('#chat-notification-badge').hide(); // Hide notification when opening chat
990 - disableScroll();
991 - $('#pre-chat-message').fadeOut(250);
992 - } else {
993 - chatbot.removeClass('visible').addClass('hidden');
994 - $(this).removeClass('hidden');
995 - enableScroll();
996 - checkPreChatDismissal();
997 - }
998 - });
999 -
1000 - $(document).on('click', '#exit-chat-button', function() {
1001 - $('#floating-chatbot').addClass('hidden').removeClass('visible');
1002 - $('#floating-chatbot-button').removeClass('hidden');
1003 - enableScroll();
1004 - });
1005 -
1006 - // Close pre-chat message on click
1007 - $(document).on('click', '.close-pre-chat-message', function(e) {
1008 - e.stopPropagation(); // Prevent triggering the parent .pre-chat-message click
1009 - $('#pre-chat-message').fadeOut(200, function() {
1010 - $(this).remove();
1011 - });
1012 - });
1013 -
1014 - // Open chatbot when pre-chat message is clicked
1015 - $(document).on('click', '#pre-chat-message', function() {
1016 - var chatbot = $('#floating-chatbot');
1017 - if (chatbot.hasClass('hidden')) {
1018 - chatbot.removeClass('hidden').addClass('visible');
1019 - $('#floating-chatbot-button').addClass('hidden');
1020 - $('#pre-chat-message').fadeOut(250); // Hide pre-chat message
1021 - disableScroll(); // Disable scroll when chatbot opens
1022 - }
1023 - });
1024 -
1025 - // If the chatbot is initially hidden, ensure the button is visible
1026 - if ($('#floating-chatbot').hasClass('hidden')) {
1027 - $('#floating-chatbot-button').removeClass('hidden');
1028 - }
1029 -
1030 - function setFullHeight() {
1031 - var vh = $(window).innerHeight() * 0.01;
1032 - $(':root').css('--vh', vh + 'px');
1033 - }
1034 -
1035 - // Set the height when the page loads
1036 -
1037 -
1038 - // Set the height on resize and orientation change events
1039 - $(window).on('resize orientationchange', function() {
1040 - setFullHeight();
1041 - });
1042 -
1043 -
1044 - // Now handle the close button to dismiss the pre-chat message for 24 hours
1045 - var closeButton = document.querySelector('.close-pre-chat-message');
1046 - if (closeButton) {
1047 - closeButton.addEventListener('click', function() {
1048 - $('#pre-chat-message').fadeOut(200); // Hide the message
1049 -
1050 - // Send an AJAX request to set the transient flag for 24 hours
1051 - $.ajax({
1052 - url: mxchatChat.ajax_url,
1053 - type: 'POST',
1054 - data: {
1055 - action: 'mxchat_dismiss_pre_chat_message',
1056 - _ajax_nonce: mxchatChat.nonce
1057 - },
1058 - success: function() {
1059 - //console.log('Pre-chat message dismissed for 24 hours.');
1060 -
1061 - // Ensure the message is hidden after dismissal
1062 - $('#pre-chat-message').hide();
1063 - },
1064 - error: function() {
1065 - //console.error('Failed to dismiss pre-chat message.');
1066 - }
1067 - });
1068 - });
1069 - }
1070 -
1071 -
1072 -
1073 -
1074 -// Event listener for Add to Cart button
1075 -$(document).on('click', '.mxchat-add-to-cart-button', function() {
1076 - var productId = $(this).data('product-id');
1077 - // Add a special prefix to indicate this is from button
1078 - appendMessage("user", "add to cart");
1079 - sendMessageToChatbot("!addtocart"); // Special command to indicate button click
1080 -});
1081 -
1082 -
1083 -if (document.getElementById('pdf-upload-btn')) {
1084 - document.getElementById('pdf-upload-btn').addEventListener('click', function() {
1085 - document.getElementById('pdf-upload').click();
1086 - });
1087 -}
1088 -
1089 -if (document.getElementById('word-upload-btn')) {
1090 - document.getElementById('word-upload-btn').addEventListener('click', function() {
1091 - document.getElementById('word-upload').click();
1092 - });
1093 -}
1094 -
1095 -// PDF file input change handler
1096 -document.getElementById('pdf-upload').addEventListener('change', async function(e) {
1097 - const file = e.target.files[0];
1098 -
1099 - if (!file || file.type !== 'application/pdf') {
1100 - alert('Please select a valid PDF file.');
1101 - return;
1102 - }
1103 -
1104 - if (!sessionId) {
1105 - console.error('No session ID found');
1106 - alert('Error: No session ID found');
1107 - return;
1108 - }
1109 -
1110 - if (!mxchatChat || !mxchatChat.ajax_url || !mxchatChat.nonce) {
1111 - console.error('mxchatChat not properly configured:', mxchatChat);
1112 - alert('Error: Ajax configuration missing');
1113 - return;
1114 - }
1115 -
1116 - // Disable buttons and show loading state
1117 - const uploadBtn = document.getElementById('pdf-upload-btn');
1118 - const sendBtn = document.getElementById('send-button');
1119 - const originalBtnContent = uploadBtn.innerHTML;
1120 -
1121 - try {
1122 - const formData = new FormData();
1123 - formData.append('action', 'mxchat_upload_pdf');
1124 - formData.append('pdf_file', file);
1125 - formData.append('session_id', sessionId);
1126 - formData.append('nonce', mxchatChat.nonce);
1127 -
1128 - uploadBtn.disabled = true;
1129 - sendBtn.disabled = true;
1130 - uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
1131 - <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
1132 - </svg>`;
1133 -
1134 - const response = await fetch(mxchatChat.ajax_url, {
1135 - method: 'POST',
1136 - body: formData
1137 - });
1138 -
1139 - const data = await response.json();
1140 -
1141 - if (data.success) {
1142 - // Hide popular questions if they exist
1143 - const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
1144 - if (popularQuestionsContainer) {
1145 - popularQuestionsContainer.style.display = 'none';
1146 - }
1147 -
1148 - // Show the active PDF name
1149 - showActivePdf(data.data.filename);
1150 -
1151 - appendMessage('bot', data.data.message);
1152 - scrollToBottom();
1153 - activePdfFile = data.data.filename;
1154 - } else {
1155 - console.error('Upload failed:', data.data);
1156 - alert('Failed to upload PDF. Please try again.');
1157 - }
1158 - } catch (error) {
1159 - console.error('Upload error:', error);
1160 - alert('Error uploading file. Please try again.');
1161 - } finally {
1162 - uploadBtn.disabled = false;
1163 - sendBtn.disabled = false;
1164 - uploadBtn.innerHTML = originalBtnContent;
1165 - this.value = ''; // Reset file input
1166 - }
1167 -});
1168 -
1169 -// Word file input change handler
1170 -document.getElementById('word-upload').addEventListener('change', async function(e) {
1171 - const file = e.target.files[0];
1172 -
1173 - if (!file || file.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
1174 - alert('Please select a valid Word document (.docx).');
1175 - return;
1176 - }
1177 -
1178 - if (!sessionId) {
1179 - console.error('No session ID found');
1180 - alert('Error: No session ID found');
1181 - return;
1182 - }
1183 -
1184 - // Disable buttons and show loading state
1185 - const uploadBtn = document.getElementById('word-upload-btn');
1186 - const sendBtn = document.getElementById('send-button');
1187 - const originalBtnContent = uploadBtn.innerHTML;
1188 -
1189 - try {
1190 - const formData = new FormData();
1191 - formData.append('action', 'mxchat_upload_word');
1192 - formData.append('word_file', file);
1193 - formData.append('session_id', sessionId);
1194 - formData.append('nonce', mxchatChat.nonce);
1195 -
1196 - uploadBtn.disabled = true;
1197 - sendBtn.disabled = true;
1198 - uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
1199 - <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
1200 - </svg>`;
1201 -
1202 - const response = await fetch(mxchatChat.ajax_url, {
1203 - method: 'POST',
1204 - body: formData
1205 - });
1206 -
1207 - const data = await response.json();
1208 -
1209 - if (data.success) {
1210 - // Hide popular questions if they exist
1211 - const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
1212 - if (popularQuestionsContainer) {
1213 - popularQuestionsContainer.style.display = 'none';
1214 - }
1215 -
1216 - // Show the active Word document name
1217 - showActiveWord(data.data.filename);
1218 -
1219 - appendMessage('bot', data.data.message);
1220 - scrollToBottom();
1221 - activeWordFile = data.data.filename;
1222 - } else {
1223 - console.error('Upload failed:', data.data);
1224 - alert('Failed to upload Word document. Please try again.');
1225 - }
1226 - } catch (error) {
1227 - console.error('Upload error:', error);
1228 - alert('Error uploading file. Please try again.');
1229 - } finally {
1230 - uploadBtn.disabled = false;
1231 - sendBtn.disabled = false;
1232 - uploadBtn.innerHTML = originalBtnContent;
1233 - this.value = ''; // Reset file input
1234 - }
1235 -});
1236 -
1237 -// Function to show active PDF name in toolbar
1238 -function showActivePdf(filename) {
1239 - const container = document.getElementById('active-pdf-container');
1240 - const nameElement = document.getElementById('active-pdf-name');
1241 -
1242 - if (!container || !nameElement) {
1243 - console.error('PDF container elements not found');
1244 - return;
1245 - }
1246 -
1247 - nameElement.textContent = filename;
1248 - container.style.display = 'flex';
1249 -}
1250 -
1251 -// Function to show active Word document name in toolbar
1252 -function showActiveWord(filename) {
1253 - const container = document.getElementById('active-word-container');
1254 - const nameElement = document.getElementById('active-word-name');
1255 -
1256 - if (!container || !nameElement) {
1257 - console.error('Word document container elements not found');
1258 - return;
1259 - }
1260 -
1261 - nameElement.textContent = filename;
1262 - container.style.display = 'flex';
1263 -}
1264 -
1265 -// Function to remove active PDF
1266 -function removeActivePdf() {
1267 - const container = document.getElementById('active-pdf-container');
1268 - const nameElement = document.getElementById('active-pdf-name');
1269 -
1270 - if (!container || !nameElement || !activePdfFile) return;
1271 -
1272 - fetch(mxchatChat.ajax_url, {
1273 - method: 'POST',
1274 - headers: {
1275 - 'Content-Type': 'application/x-www-form-urlencoded',
1276 - },
1277 - body: new URLSearchParams({
1278 - 'action': 'mxchat_remove_pdf',
1279 - 'session_id': sessionId,
1280 - 'nonce': mxchatChat.nonce
1281 - })
1282 - })
1283 - .then(response => response.json())
1284 - .then(data => {
1285 - if (data.success) {
1286 - container.style.display = 'none';
1287 - nameElement.textContent = '';
1288 - activePdfFile = null;
1289 - appendMessage('bot', 'PDF removed.');
1290 - }
1291 - })
1292 - .catch(error => {
1293 - console.error('Error removing PDF:', error);
1294 - });
1295 -}
1296 -
1297 -// Function to remove active Word document
1298 -function removeActiveWord() {
1299 - const container = document.getElementById('active-word-container');
1300 - const nameElement = document.getElementById('active-word-name');
1301 -
1302 - if (!container || !nameElement || !activeWordFile) return;
1303 -
1304 - fetch(mxchatChat.ajax_url, {
1305 - method: 'POST',
1306 - headers: {
1307 - 'Content-Type': 'application/x-www-form-urlencoded',
1308 - },
1309 - body: new URLSearchParams({
1310 - 'action': 'mxchat_remove_word',
1311 - 'session_id': sessionId,
1312 - 'nonce': mxchatChat.nonce
1313 - })
1314 - })
1315 - .then(response => response.json())
1316 - .then(data => {
1317 - if (data.success) {
1318 - container.style.display = 'none';
1319 - nameElement.textContent = '';
1320 - activeWordFile = null;
1321 - appendMessage('bot', 'Word document removed.');
1322 - }
1323 - })
1324 - .catch(error => {
1325 - console.error('Error removing Word document:', error);
1326 - });
1327 -}
1328 -
1329 -// Add remove button click handlers
1330 -document.getElementById('remove-pdf-btn')?.addEventListener('click', function(e) {
1331 - e.preventDefault();
1332 - e.stopPropagation();
1333 - removeActivePdf();
1334 -});
1335 -
1336 -document.getElementById('remove-word-btn')?.addEventListener('click', function(e) {
1337 - e.preventDefault();
1338 - e.stopPropagation();
1339 - removeActiveWord();
1340 -});
1341 -
1342 -// Check initial document status
1343 -function checkInitialDocumentStatus() {
1344 - if (!sessionId) return;
1345 -
1346 - // Check PDF status
1347 - fetch(mxchatChat.ajax_url, {
1348 - method: 'POST',
1349 - headers: {
1350 - 'Content-Type': 'application/x-www-form-urlencoded',
1351 - },
1352 - body: new URLSearchParams({
1353 - 'action': 'mxchat_check_pdf_status',
1354 - 'session_id': sessionId,
1355 - 'nonce': mxchatChat.nonce
1356 - })
1357 - })
1358 - .then(response => response.json())
1359 - .then(data => {
1360 - if (data.success && data.data.filename) {
1361 - showActivePdf(data.data.filename);
1362 - activePdfFile = data.data.filename;
1363 - }
1364 - })
1365 - .catch(error => {
1366 - console.error('Error checking PDF status:', error);
1367 - });
1368 -
1369 - // Check Word document status
1370 - fetch(mxchatChat.ajax_url, {
1371 - method: 'POST',
1372 - headers: {
1373 - 'Content-Type': 'application/x-www-form-urlencoded',
1374 - },
1375 - body: new URLSearchParams({
1376 - 'action': 'mxchat_check_word_status',
1377 - 'session_id': sessionId,
1378 - 'nonce': mxchatChat.nonce
1379 - })
1380 - })
1381 - .then(response => response.json())
1382 - .then(data => {
1383 - if (data.success && data.data.filename) {
1384 - showActiveWord(data.data.filename);
1385 - activeWordFile = data.data.filename;
1386 - }
1387 - })
1388 - .catch(error => {
1389 - console.error('Error checking Word document status:', error);
1390 - });
1391 -}
1392 -
1393 -// Apply toolbar settings
1394 -if (mxchatChat.chat_toolbar_toggle === 'on') {
1395 - $('.chat-toolbar').show();
1396 -} else {
1397 - $('.chat-toolbar').hide();
1398 -}
1399 -
1400 -// Initialize on page load
1401 -document.addEventListener('DOMContentLoaded', function() {
1402 - checkInitialDocumentStatus();
1403 -});
1404 -
1405 -// Style all toolbar elements
1406 -const toolbarElements = [
1407 - '#mxchat-chatbot .toolbar-btn svg',
1408 - '#mxchat-chatbot .active-pdf-name',
1409 - '#mxchat-chatbot .active-word-name',
1410 - '#mxchat-chatbot .remove-pdf-btn svg',
1411 - '#mxchat-chatbot .remove-word-btn svg'
1412 -];
1413 -$(toolbarElements.join(', ')).css({
1414 - 'fill': toolbarIconColor,
1415 - 'color': toolbarIconColor
1416 -});
1417 -
1418 -
1419 -
1420 -// Ensure essential elements are defined
1421 -const emailForm = document.getElementById('email-collection-form');
1422 -const emailBlocker = document.getElementById('email-blocker');
1423 -const chatbotWrapper = document.getElementById('chat-container');
1424 -
1425 -if (emailForm && emailBlocker && chatbotWrapper) {
1426 - // Check if email exists for the current session
1427 - function checkSessionAndEmail() {
1428 - const sessionId = getChatSession();
1429 - //console.log("[DEBUG JS] checkSessionAndEmail -> sessionId:", sessionId);
1430 -
1431 - fetch(mxchatChat.ajax_url, {
1432 - method: 'POST',
1433 - headers: {
1434 - 'Content-Type': 'application/x-www-form-urlencoded',
1435 - },
1436 - body: new URLSearchParams({
1437 - action: 'mxchat_check_email_provided',
1438 - session_id: sessionId,
1439 - nonce: mxchatChat.nonce,
1440 - }),
1441 - })
1442 - .then((response) => response.json())
1443 - .then((data) => {
1444 - //console.log("[DEBUG JS] mxchat_check_email_provided response:", data);
1445 -
1446 - if (data.success) {
1447 - if (data.data.logged_in) {
1448 - //console.log("[DEBUG JS] User is logged in. Hiding email form.");
1449 - emailBlocker.style.display = 'none';
1450 - chatbotWrapper.style.display = 'flex';
1451 - } else if (data.data.email) {
1452 - //console.log("[DEBUG JS] Email found for session. Hiding email form.");
1453 - emailBlocker.style.display = 'none';
1454 - chatbotWrapper.style.display = 'flex';
1455 - } else {
1456 - //console.log("[DEBUG JS] No email provided. Showing email form.");
1457 - emailBlocker.style.display = 'flex';
1458 - chatbotWrapper.style.display = 'none';
1459 - }
1460 - } else {
1461 - //console.log("[DEBUG JS] Error or no data received. Showing email form.");
1462 - emailBlocker.style.display = 'flex';
1463 - chatbotWrapper.style.display = 'none';
1464 - }
1465 - })
1466 - .catch((error) => {
1467 - // console.error("[DEBUG JS] Fetch error -> forcing email form visible:", error);
1468 - emailBlocker.style.display = 'flex';
1469 - chatbotWrapper.style.display = 'none';
1470 - });
1471 -}
1472 -
1473 -
1474 -
1475 - // Handle email form submission
1476 - emailForm.addEventListener('submit', function (event) {
1477 - event.preventDefault();
1478 - const userEmail = document.getElementById('user-email').value;
1479 - const sessionId = getChatSession();
1480 -
1481 - if (userEmail) {
1482 - fetch(mxchatChat.ajax_url, {
1483 - method: 'POST',
1484 - headers: {
1485 - 'Content-Type': 'application/x-www-form-urlencoded',
1486 - },
1487 - body: new URLSearchParams({
1488 - action: 'mxchat_handle_save_email_and_response',
1489 - email: userEmail,
1490 - session_id: sessionId,
1491 - nonce: mxchatChat.nonce,
1492 - }),
1493 - })
1494 - .then((response) => response.json())
1495 - .then((data) => {
1496 - //console.log('Backend response:', data);
1497 - if (data.success) {
1498 - //console.log('Email saved successfully:', userEmail);
1499 - emailBlocker.style.display = 'none';
1500 - chatbotWrapper.style.display = 'flex';
1501 -
1502 - // Optionally handle bot response
1503 - if (data.message) {
1504 - appendMessage('bot', data.message);
1505 - scrollToBottom();
1506 - }
1507 - } else {
1508 - console.error('Error saving email:', data.message || 'Unknown error');
1509 - }
1510 - })
1511 - .catch((error) => {
1512 - console.error('AJAX error:', error);
1513 - });
1514 - }
1515 - });
1516 -
1517 - // Check session and email status on page load
1518 - checkSessionAndEmail();
1519 -} else {
1520 - console.error('Essential elements for email handling are missing.');
1521 -}
1522 -
1523 -
1524 -// Initialize when document is ready
1525 -$(document).ready(function() {
1526 - setFullHeight();
1527 - initializeChatVisibility();
1528 - loadChatHistory();
1529 -
1530 -});
1531 -
1532 -});
1533 -
1534 -// Event listener for copy button
1535 -document.addEventListener("click", (e) => {
1536 - if (e.target.classList.contains("mxchat-copy-button")) {
1537 - const copyButton = e.target;
1538 - const codeBlock = copyButton
1539 - .closest(".mxchat-code-block-container")
1540 - .querySelector(".mxchat-code-block code");
1541 -
1542 - if (codeBlock) {
1543 - // Preserve formatting using innerText
1544 - navigator.clipboard.writeText(codeBlock.innerText).then(() => {
1545 - copyButton.textContent = "Copied!";
1546 - copyButton.setAttribute("aria-label", "Copied to clipboard");
1547 -
1548 - setTimeout(() => {
1549 - copyButton.textContent = "Copy";
1550 - copyButton.setAttribute("aria-label", "Copy to clipboard");
1551 - }, 2000);
1552 - });
1553 - }
1554 - }
1555 -});
1556 -
1557 -
1558 -
1 +jQuery(document).ready(function($) {
2 +
3 + // ====================================
4 + // GLOBAL VARIABLES & CONFIGURATION
5 + // ====================================
6 + const toolbarIconColor = mxchatChat.toolbar_icon_color || '#212121';
7 +
8 + // Initialize color settings
9 + var userMessageBgColor = mxchatChat.user_message_bg_color;
10 + var userMessageFontColor = mxchatChat.user_message_font_color;
11 + var botMessageBgColor = mxchatChat.bot_message_bg_color;
12 + var botMessageFontColor = mxchatChat.bot_message_font_color;
13 + var liveAgentMessageBgColor = mxchatChat.live_agent_message_bg_color;
14 + var liveAgentMessageFontColor = mxchatChat.live_agent_message_font_color;
15 +
16 + var linkTarget = mxchatChat.link_target_toggle === 'on' ? '_blank' : '_self';
17 + let lastSeenMessageId = '';
18 + let notificationCheckInterval;
19 + let notificationBadge;
20 + var sessionId = getChatSession();
21 + let pollingInterval;
22 + let processedMessageIds = new Set();
23 + let activePdfFile = null;
24 + let activeWordFile = null;
25 +
26 +
27 + // ====================================
28 + // SESSION MANAGEMENT
29 + // ====================================
30 +
31 + function getChatSession() {
32 + var sessionId = getCookie('mxchat_session_id');
33 + //console.log("Session ID retrieved from cookie: ", sessionId);
34 +
35 + if (!sessionId) {
36 + sessionId = generateSessionId();
37 + //console.log("Generated new session ID: ", sessionId);
38 + setChatSession(sessionId);
39 + }
40 +
41 + //console.log("Final session ID: ", sessionId);
42 + return sessionId;
43 + }
44 +
45 + function setChatSession(sessionId) {
46 + // Set the cookie with a 24-hour expiration (86400 seconds)
47 + document.cookie = "mxchat_session_id=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
48 + }
49 +
50 + function getCookie(name) {
51 + let value = "; " + document.cookie;
52 + let parts = value.split("; " + name + "=");
53 + if (parts.length == 2) return parts.pop().split(";").shift();
54 + }
55 +
56 + function generateSessionId() {
57 + return 'mxchat_chat_' + Math.random().toString(36).substr(2, 9);
58 + }
59 +
60 +// ====================================
61 +// CONTEXTUAL AWARENESS FUNCTIONALITY
62 +// ====================================
63 +
64 +function getPageContext() {
65 + // Check if contextual awareness is enabled
66 + if (mxchatChat.contextual_awareness_toggle !== 'on') {
67 + return null;
68 + }
69 +
70 + // Get page URL
71 + const pageUrl = window.location.href;
72 +
73 + // Get page title
74 + const pageTitle = document.title || '';
75 +
76 + // Get main content from the page
77 + let pageContent = '';
78 +
79 + // Try to get content from common content areas
80 + const contentSelectors = [
81 + 'main',
82 + '[role="main"]',
83 + '.content',
84 + '.main-content',
85 + '.post-content',
86 + '.entry-content',
87 + '.page-content',
88 + 'article',
89 + '#content',
90 + '#main'
91 + ];
92 +
93 + let contentElement = null;
94 + for (const selector of contentSelectors) {
95 + contentElement = document.querySelector(selector);
96 + if (contentElement) {
97 + break;
98 + }
99 + }
100 +
101 + // If no specific content area found, use body but exclude header, footer, nav, sidebar
102 + if (!contentElement) {
103 + contentElement = document.body;
104 + }
105 +
106 + if (contentElement) {
107 + // Clone the element to avoid modifying the original
108 + const clone = contentElement.cloneNode(true);
109 +
110 + // Remove unwanted elements
111 + const unwantedSelectors = [
112 + 'header',
113 + 'footer',
114 + 'nav',
115 + '.navigation',
116 + '.sidebar',
117 + '.widget',
118 + '.menu',
119 + 'script',
120 + 'style',
121 + '.comments',
122 + '#comments',
123 + '.breadcrumb',
124 + '.breadcrumbs',
125 + '#floating-chatbot',
126 + '#floating-chatbot-button',
127 + '.mxchat',
128 + '[class*="chat"]',
129 + '[id*="chat"]'
130 + ];
131 +
132 + unwantedSelectors.forEach(selector => {
133 + const elements = clone.querySelectorAll(selector);
134 + elements.forEach(el => el.remove());
135 + });
136 +
137 + // NEW: Extract MxChat context data attributes before getting text content
138 + const contextData = [];
139 + clone.querySelectorAll('[data-mxchat-context]').forEach(el => {
140 + const contextValue = el.dataset.mxchatContext;
141 + if (contextValue && contextValue.trim()) {
142 + contextData.push(contextValue);
143 + }
144 + });
145 +
146 + // Get text content and clean it up
147 + pageContent = clone.textContent || clone.innerText || '';
148 +
149 + // Add context data to page content if any were found
150 + if (contextData.length > 0) {
151 + pageContent += '\n\nAdditional Context:\n' + contextData.join('\n');
152 + }
153 +
154 + // Clean up whitespace and limit length
155 + pageContent = pageContent
156 + .replace(/\s+/g, ' ')
157 + .trim()
158 + .substring(0, 3000); // Limit to 3000 characters to avoid token limits
159 + }
160 +
161 + // Only return context if we have meaningful content
162 + if (!pageContent || pageContent.length < 50) {
163 + return null;
164 + }
165 +
166 + return {
167 + url: pageUrl,
168 + title: pageTitle,
169 + content: pageContent
170 + };
171 +}
172 +
173 +// ====================================
174 +// CORE CHAT FUNCTIONALITY
175 +// ====================================
176 +// Update your existing sendMessage function
177 +function sendMessage() {
178 + var message = $('#chat-input').val();
179 +
180 + // ADD PROMPT HOOK HERE
181 + if (typeof customMxChatFilter === 'function') {
182 + message = customMxChatFilter(message, "prompt");
183 + }
184 +
185 + if (message) {
186 + appendMessage("user", message);
187 + $('#chat-input').val('');
188 + $('#chat-input').css('height', 'auto');
189 +
190 + if (hasQuickQuestions()) {
191 + collapseQuickQuestions();
192 + }
193 + appendThinkingMessage();
194 + scrollToBottom();
195 +
196 + const currentModel = mxchatChat.model || 'gpt-4o';
197 +
198 + // Check if streaming is enabled AND supported for this model
199 + if (shouldUseStreaming(currentModel)) {
200 + callMxChatStream(message, function(response) {
201 + $('.bot-message.temporary-message').removeClass('temporary-message');
202 + });
203 + } else {
204 + callMxChat(message, function(response) {
205 + replaceLastMessage("bot", response);
206 + });
207 + }
208 + }
209 +}
210 +
211 +// Update your existing sendMessageToChatbot function
212 +function sendMessageToChatbot(message) {
213 + // ADD PROMPT HOOK HERE
214 + if (typeof customMxChatFilter === 'function') {
215 + message = customMxChatFilter(message, "prompt");
216 + }
217 +
218 + var sessionId = getChatSession();
219 +
220 + if (hasQuickQuestions()) {
221 + collapseQuickQuestions();
222 + }
223 + appendThinkingMessage();
224 + scrollToBottom();
225 +
226 + const currentModel = mxchatChat.model || 'gpt-4o';
227 +
228 + // Check if streaming is enabled AND supported for this model
229 + if (shouldUseStreaming(currentModel)) {
230 + callMxChatStream(message, function(response) {
231 + $('.bot-message.temporary-message').removeClass('temporary-message');
232 + });
233 + } else {
234 + callMxChat(message, function(response) {
235 + $('.temporary-message').remove();
236 + replaceLastMessage("bot", response);
237 + });
238 + }
239 +}
240 +
241 +// Updated shouldUseStreaming function with debugging
242 +function shouldUseStreaming(model) {
243 + // Check if streaming is enabled in settings (using your toggle naming pattern)
244 + const streamingEnabled = mxchatChat.enable_streaming_toggle === 'on';
245 +
246 + // Check if model supports streaming
247 + const streamingSupported = isStreamingSupported(model);
248 +
249 +
250 + // Only use streaming if both enabled and supported
251 + return streamingEnabled && streamingSupported;
252 +}
253 +
254 +function callMxChat(message, callback) {
255 + // Get page context if contextual awareness is enabled
256 + const pageContext = getPageContext();
257 +
258 + // Prepare AJAX data
259 + const ajaxData = {
260 + action: 'mxchat_handle_chat_request',
261 + message: message,
262 + session_id: getChatSession(),
263 + nonce: mxchatChat.nonce
264 + };
265 +
266 + // Add page context if available
267 + if (pageContext) {
268 + ajaxData.page_context = JSON.stringify(pageContext);
269 + }
270 +
271 + // CHECK FOR VISION FLAGS AND ADD THEM
272 + if (window.mxchatVisionProcessed) {
273 + ajaxData.vision_processed = true;
274 + ajaxData.original_user_message = window.mxchatOriginalMessage || message;
275 + ajaxData.vision_images_count = window.mxchatVisionImagesCount || 0;
276 + // Clear the flags after use
277 + window.mxchatVisionProcessed = false;
278 + window.mxchatOriginalMessage = null;
279 + window.mxchatVisionImagesCount = 0;
280 + }
281 +
282 + $.ajax({
283 + url: mxchatChat.ajax_url,
284 + type: 'POST',
285 + dataType: 'json',
286 + data: ajaxData,
287 + success: function(response) {
288 + // Log the full response for debugging
289 + //console.log("API Response:", response);
290 +
291 + // First check if this is a successful response by looking for text, html, or message fields
292 + // This preserves compatibility with your server response format
293 + if (response.text !== undefined || response.html !== undefined || response.message !== undefined ||
294 + (response.success === true && response.data && response.data.status === 'waiting_for_agent')) {
295 +
296 + // Handle successful response - this is your original success handling code
297 +
298 + // Existing chat mode check
299 + if (response.chat_mode) {
300 + updateChatModeIndicator(response.chat_mode);
301 + }
302 + else if (response.fallbackResponse && response.fallbackResponse.chat_mode) {
303 + updateChatModeIndicator(response.fallbackResponse.chat_mode);
304 + }
305 +
306 + // Add PDF filename handling
307 + if (response.data && response.data.filename) {
308 + showActivePdf(response.data.filename);
309 + activePdfFile = response.data.filename;
310 + }
311 +
312 + // Add redirect check here
313 + if (response.redirect_url) {
314 + let responseText = response.text || '';
315 + if (responseText) {
316 + replaceLastMessage("bot", responseText);
317 + }
318 + setTimeout(() => {
319 + window.location.href = response.redirect_url;
320 + }, 1500);
321 + return;
322 + }
323 +
324 + // Check for live agent response
325 + if (response.success && response.data && response.data.status === 'waiting_for_agent') {
326 + updateChatModeIndicator('agent');
327 + return;
328 + }
329 +
330 + // Handle other responses
331 + let responseText = response.text || '';
332 + let responseHtml = response.html || '';
333 + let responseMessage = response.message || '';
334 +
335 + if (responseText === 'You are now chatting with the AI chatbot.') {
336 + updateChatModeIndicator('ai');
337 + }
338 +
339 + // Handle the message and show notification if chat is hidden
340 + if (responseText || responseHtml || responseMessage) {
341 +
342 + // ADD RESPONSE HOOKS HERE - BEFORE DISPLAYING
343 + if (responseText && typeof customMxChatFilter === 'function') {
344 + responseText = customMxChatFilter(responseText, "response");
345 + }
346 + if (responseMessage && typeof customMxChatFilter === 'function') {
347 + responseMessage = customMxChatFilter(responseMessage, "response");
348 + }
349 +
350 + // Update the messages as before
351 + if (responseText && responseHtml) {
352 + replaceLastMessage("bot", responseText, responseHtml);
353 + } else if (responseText) {
354 + replaceLastMessage("bot", responseText);
355 + } else if (responseHtml) {
356 + replaceLastMessage("bot", "", responseHtml);
357 + } else if (responseMessage) {
358 + replaceLastMessage("bot", responseMessage);
359 + }
360 +
361 + // Check if chat is hidden and show notification
362 + if ($('#floating-chatbot').hasClass('hidden')) {
363 + const badge = $('#chat-notification-badge');
364 + if (badge.length) {
365 + badge.show();
366 + }
367 + }
368 + } else {
369 + ////console.error("Unexpected response format:", response);
370 + replaceLastMessage("bot", "I received an empty response. Please try again or contact support if this persists.");
371 + }
372 +
373 + if (response.message_id) {
374 + lastSeenMessageId = response.message_id;
375 + }
376 +
377 + return;
378 + }
379 +
380 + // If we got here, it's likely an error response
381 + // Now we can check for error conditions with our robust error handling
382 +
383 + let errorMessage = "";
384 + let errorCode = "";
385 +
386 + // Check various possible error locations in the response
387 + if (response.data && response.data.error_message) {
388 + errorMessage = response.data.error_message;
389 + errorCode = response.data.error_code || "";
390 + } else if (response.error_message) {
391 + errorMessage = response.error_message;
392 + errorCode = response.error_code || "";
393 + } else if (response.message) {
394 + errorMessage = response.message;
395 + } else if (typeof response.data === 'string') {
396 + errorMessage = response.data;
397 + } else if (!response.success) {
398 + // Explicit check for success: false without other error info
399 + errorMessage = "An error occurred. Please try again or contact support.";
400 + } else {
401 + // Fallback for any other unexpected response format
402 + errorMessage = "Unexpected response received. Please try again or contact support.";
403 + }
404 +
405 + // Log the error with code for debugging
406 + //console.log("Response data:", response.data);
407 + ////console.error("API Error:", errorMessage, "Code:", errorCode);
408 +
409 + // Format user-friendly error message
410 + let displayMessage = errorMessage;
411 +
412 + // Customize message for admin users
413 + if (mxchatChat.is_admin) {
414 + // For admin users, show more technical details including error code
415 + displayMessage = errorMessage + (errorCode ? " (Error code: " + errorCode + ")" : "");
416 + }
417 +
418 + replaceLastMessage("bot", displayMessage);
419 + },
420 + error: function(xhr, status, error) {
421 + //console.error("AJAX Error:", status, error);
422 + //console.log("Response Text:", xhr.responseText);
423 +
424 + let errorMessage = "An unexpected error occurred.";
425 +
426 + // Try to parse the response if it's JSON
427 + try {
428 + const responseJson = JSON.parse(xhr.responseText);
429 + //console.log("Parsed error response:", responseJson);
430 +
431 + if (responseJson.data && responseJson.data.error_message) {
432 + errorMessage = responseJson.data.error_message;
433 + } else if (responseJson.message) {
434 + errorMessage = responseJson.message;
435 + }
436 + } catch (e) {
437 + // Not JSON or parsing failed, use HTTP status based messages
438 + if (xhr.status === 0) {
439 + errorMessage = "Network error: Please check your internet connection.";
440 + } else if (xhr.status === 403) {
441 + errorMessage = "Access denied: Your session may have expired. Please refresh the page.";
442 + } else if (xhr.status === 404) {
443 + errorMessage = "API endpoint not found. Please contact support.";
444 + } else if (xhr.status === 429) {
445 + errorMessage = "Too many requests. Please try again in a moment.";
446 + } else if (xhr.status >= 500) {
447 + errorMessage = "Server error: The server encountered an issue. Please try again later.";
448 + }
449 + }
450 +
451 + replaceLastMessage("bot", errorMessage);
452 + }
453 + });
454 +}
455 +
456 +function callMxChatStream(message, callback) {
457 + //console.log("Using streaming for message:", message);
458 +
459 + const currentModel = mxchatChat.model || 'gpt-4o';
460 + if (!isStreamingSupported(currentModel)) {
461 + //console.log("Streaming not supported, falling back to regular call");
462 + callMxChat(message, callback);
463 + return;
464 + }
465 +
466 + // Get page context if contextual awareness is enabled
467 + const pageContext = getPageContext();
468 +
469 + const formData = new FormData();
470 + formData.append('action', 'mxchat_stream_chat');
471 + formData.append('message', message);
472 + formData.append('session_id', getChatSession());
473 + formData.append('nonce', mxchatChat.nonce);
474 +
475 + // Add page context if available
476 + if (pageContext) {
477 + formData.append('page_context', JSON.stringify(pageContext));
478 + }
479 +
480 + // CHECK FOR VISION FLAGS AND ADD THEM
481 + if (window.mxchatVisionProcessed) {
482 + formData.append('vision_processed', 'true');
483 + formData.append('original_user_message', window.mxchatOriginalMessage || message);
484 + formData.append('vision_images_count', window.mxchatVisionImagesCount || '0');
485 + // Clear the flags after use
486 + window.mxchatVisionProcessed = false;
487 + window.mxchatOriginalMessage = null;
488 + window.mxchatVisionImagesCount = 0;
489 + }
490 +
491 + let accumulatedContent = '';
492 + let testingDataReceived = false;
493 +
494 + fetch(mxchatChat.ajax_url, {
495 + method: 'POST',
496 + body: formData,
497 + credentials: 'same-origin'
498 + })
499 + .then(response => {
500 + //console.log("Streaming response received:", response);
501 +
502 + if (!response.ok) {
503 + throw new Error('Network response was not ok');
504 + }
505 +
506 + // Check if response is JSON instead of streaming
507 + const contentType = response.headers.get('content-type');
508 + if (contentType && contentType.includes('application/json')) {
509 + //console.log("Received JSON response instead of stream, handling as regular response");
510 + return response.json().then(data => {
511 + // Handle as regular JSON response
512 + $('.bot-message.temporary-message').remove();
513 +
514 + // FIXED: Always check for testing panel, not just in testing mode
515 + if (window.mxchatTestPanelInstance && data.testing_data) {
516 + //console.log('Testing data found in streaming JSON response:', data.testing_data);
517 + window.mxchatTestPanelInstance.handleTestingData(data.testing_data);
518 + }
519 +
520 + // Handle different response formats (including intent responses)
521 + if (data.text || data.html || data.message) {
522 +
523 + // ADD RESPONSE HOOKS HERE - FOR STREAMING JSON RESPONSES
524 + if (data.text && typeof customMxChatFilter === 'function') {
525 + data.text = customMxChatFilter(data.text, "response");
526 + }
527 + if (data.message && typeof customMxChatFilter === 'function') {
528 + data.message = customMxChatFilter(data.message, "response");
529 + }
530 +
531 + if (data.text && data.html) {
532 + replaceLastMessage("bot", data.text, data.html);
533 + } else if (data.text) {
534 + replaceLastMessage("bot", data.text);
535 + } else if (data.html) {
536 + replaceLastMessage("bot", "", data.html);
537 + } else if (data.message) {
538 + replaceLastMessage("bot", data.message);
539 + }
540 + }
541 +
542 + // Handle other response properties
543 + if (data.chat_mode) {
544 + updateChatModeIndicator(data.chat_mode);
545 + }
546 +
547 + if (data.data && data.data.filename) {
548 + showActivePdf(data.data.filename);
549 + activePdfFile = data.data.filename;
550 + }
551 +
552 + if (callback) {
553 + callback(data.text || data.message || '');
554 + }
555 + });
556 + }
557 +
558 + // NEW: Check if it's event-stream but might contain JSON (Cloudflare issue)
559 + if (contentType && contentType.includes('text/event-stream')) {
560 + // First, try to read the entire response to check if it's actually JSON
561 + return response.text().then(fullText => {
562 + // Check if the response looks like a single JSON object instead of SSE format
563 + const trimmedText = fullText.trim();
564 + if (trimmedText.startsWith('{') && trimmedText.endsWith('}') && !trimmedText.includes('data: ')) {
565 + //console.log("Detected JSON in event-stream wrapper, parsing as JSON fallback");
566 + try {
567 + const data = JSON.parse(trimmedText);
568 +
569 + // Handle as regular JSON response
570 + $('.bot-message.temporary-message').remove();
571 +
572 + // Check for testing panel data
573 + if (window.mxchatTestPanelInstance && data.testing_data) {
574 + window.mxchatTestPanelInstance.handleTestingData(data.testing_data);
575 + }
576 +
577 + // Handle response content
578 + if (data.text || data.html || data.message) {
579 + if (data.text && typeof customMxChatFilter === 'function') {
580 + data.text = customMxChatFilter(data.text, "response");
581 + }
582 + if (data.message && typeof customMxChatFilter === 'function') {
583 + data.message = customMxChatFilter(data.message, "response");
584 + }
585 +
586 + if (data.text && data.html) {
587 + replaceLastMessage("bot", data.text, data.html);
588 + } else if (data.text) {
589 + replaceLastMessage("bot", data.text);
590 + } else if (data.html) {
591 + replaceLastMessage("bot", "", data.html);
592 + } else if (data.message) {
593 + replaceLastMessage("bot", data.message);
594 + }
595 + }
596 +
597 + // Handle other response properties
598 + if (data.chat_mode) {
599 + updateChatModeIndicator(data.chat_mode);
600 + }
601 +
602 + if (data.data && data.data.filename) {
603 + showActivePdf(data.data.filename);
604 + activePdfFile = data.data.filename;
605 + }
606 +
607 + if (callback) {
608 + callback(data.text || data.message || '');
609 + }
610 +
611 + return; // Exit early, we handled it as JSON
612 + } catch (e) {
613 + //console.log("Failed to parse as JSON, continuing with streaming logic");
614 + }
615 + }
616 +
617 + // If we get here, it's actual streaming data, process it normally
618 + const lines = fullText.split('\n');
619 + let buffer = '';
620 +
621 + for (const line of lines) {
622 + if (line.startsWith('data: ')) {
623 + const data = line.substring(6);
624 +
625 + if (data === '[DONE]') {
626 + //console.log("Received [DONE] signal");
627 + if (callback) {
628 + callback(accumulatedContent);
629 + }
630 + return;
631 + }
632 +
633 + try {
634 + const json = JSON.parse(data);
635 +
636 + // Handle testing data
637 + if (json.testing_data && !testingDataReceived) {
638 + //console.log('Testing data received in stream:', json.testing_data);
639 + if (window.mxchatTestPanelInstance) {
640 + window.mxchatTestPanelInstance.handleTestingData(json.testing_data);
641 + testingDataReceived = true;
642 + }
643 + }
644 + // Handle content streaming
645 + else if (json.content) {
646 + accumulatedContent += json.content;
647 + updateStreamingMessage(accumulatedContent);
648 + }
649 + // Handle errors
650 + else if (json.error) {
651 + //console.error("Streaming error:", json.error);
652 + replaceLastMessage("bot", "Error: " + json.error);
653 + return;
654 + }
655 + } catch (e) {
656 + //console.error('Error parsing SSE data:', e, 'Data:', data);
657 + }
658 + }
659 + }
660 +
661 + if (callback) {
662 + callback(accumulatedContent);
663 + }
664 + });
665 + }
666 +
667 + // If we get here, something unexpected happened, fall back to regular processing
668 + //console.log("Unexpected content type, falling back to text processing");
669 + return response.text().then(text => {
670 + //console.log("Raw response text:", text);
671 + // Try to extract any meaningful content and display it
672 + replaceLastMessage("bot", "Received response but couldn't process streaming format. Please try again.");
673 + });
674 + })
675 + .catch(error => {
676 + //console.error('Streaming error:', error);
677 + // Fall back to regular chat on any error
678 + callMxChat(message, callback);
679 + });
680 +}
681 +
682 +
683 +// Function to update message during streaming
684 +function updateStreamingMessage(content) {
685 + // ADD RESPONSE HOOK FOR REAL-TIME STREAMING
686 + if (typeof customMxChatFilter === 'function') {
687 + content = customMxChatFilter(content, "response");
688 + }
689 +
690 + const formattedContent = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(content))));
691 +
692 + // Find the temporary message
693 + const tempMessage = $('.bot-message.temporary-message').last();
694 +
695 + if (tempMessage.length) {
696 + // Update existing message
697 + tempMessage.html(formattedContent);
698 + } else {
699 + // Create new temporary message if it doesn't exist
700 + appendMessage("bot", content, '', [], true);
701 + }
702 +}
703 +
704 +// UPGRADE: Function to check if streaming is supported for the current model
705 +function isStreamingSupported(model) {
706 + if (!model) return false;
707 +
708 + //console.log("Checking streaming support for model:", model); // Debug log
709 +
710 + // Get the model prefix
711 + const modelPrefix = model.split('-')[0].toLowerCase();
712 +
713 + //console.log("Model prefix:", modelPrefix); // Debug log
714 +
715 + // Support streaming for OpenAI, Claude, and Grok models
716 + const isSupported = modelPrefix === 'gpt' || modelPrefix === 'o1' || modelPrefix === 'claude' || modelPrefix === 'grok';
717 +
718 + //console.log("Streaming supported:", isSupported); // Debug log
719 +
720 + return isSupported;
721 +}
722 +
723 +// Update the event handlers to use the correct function names
724 +$('#send-button').off('click').on('click', function() {
725 + sendMessage(); // Use the updated sendMessage function
726 +});
727 +
728 +// Override enter key handler
729 +$('#chat-input').off('keypress').on('keypress', function(e) {
730 + if (e.which == 13 && !e.shiftKey) {
731 + e.preventDefault();
732 + sendMessage(); // Use the updated sendMessage function
733 + }
734 +});
735 +
736 +
737 + function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false) {
738 + try {
739 + // Determine styles based on sender type
740 + let messageClass, bgColor, fontColor;
741 +
742 + if (sender === "user") {
743 + messageClass = "user-message";
744 + bgColor = userMessageBgColor;
745 + fontColor = userMessageFontColor;
746 + // Only sanitize user input
747 + messageText = sanitizeUserInput(messageText);
748 + } else if (sender === "agent") {
749 + messageClass = "agent-message";
750 + bgColor = liveAgentMessageBgColor;
751 + fontColor = liveAgentMessageFontColor;
752 + } else {
753 + messageClass = "bot-message";
754 + bgColor = botMessageBgColor;
755 + fontColor = botMessageFontColor;
756 + }
757 +
758 + const messageDiv = $('<div>')
759 + .addClass(messageClass)
760 + .attr('dir', 'auto') // Add dir="auto" for automatic text direction
761 + .css({
762 + 'background': bgColor,
763 + 'color': fontColor,
764 + 'margin-bottom': '1em'
765 + });
766 +
767 + // Process the message content based on sender
768 + let fullMessage;
769 + if (sender === "user") {
770 + // For user messages, apply linkify after sanitization
771 + fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(messageText))));
772 + } else {
773 + // For bot/agent messages, preserve HTML
774 + fullMessage = messageText;
775 + }
776 +
777 + // Add images if provided
778 + if (images && images.length > 0) {
779 + fullMessage += '<div class="image-gallery" dir="auto">'; // Add dir="auto" to image gallery
780 + images.forEach(img => {
781 + // Ensure image URLs and titles are properly escaped
782 + const safeTitle = sanitizeUserInput(img.title);
783 + const safeUrl = encodeURI(img.image_url);
784 + const safeThumbnail = encodeURI(img.thumbnail_url);
785 +
786 + fullMessage += `
787 + <div style="margin-bottom: 10px;">
788 + <strong>${safeTitle}</strong><br>
789 + <a href="${safeUrl}" target="_blank">
790 + <img src="${safeThumbnail}" alt="${safeTitle}" style="max-width: 100px; height: auto; margin: 5px;" />
791 + </a>
792 + </div>`;
793 + });
794 + fullMessage += '</div>';
795 + }
796 +
797 + // Append HTML content if provided
798 + if (messageHtml && sender !== "user") {
799 + fullMessage += '<br><br>' + messageHtml;
800 + }
801 +
802 + messageDiv.html(fullMessage);
803 +
804 + if (isTemporary) {
805 + messageDiv.addClass('temporary-message');
806 + }
807 +
808 + messageDiv.hide().appendTo('#chat-box').fadeIn(300, function() {
809 + if (sender === "bot") {
810 + const lastUserMessage = $('#chat-box').find('.user-message').last();
811 + if (lastUserMessage.length) {
812 + scrollElementToTop(lastUserMessage);
813 + }
814 + }
815 + });
816 +
817 + if (messageText.id) {
818 + lastSeenMessageId = messageText.id;
819 + hideNotification();
820 + }
821 + } catch (error) {
822 + //console.error("Error rendering message:", error);
823 + }
824 + }
825 +
826 + function replaceLastMessage(sender, responseText, responseHtml = '', images = []) {
827 + var messageClass = sender === "user" ? "user-message" : sender === "agent" ? "agent-message" : "bot-message";
828 + var lastMessageDiv = $('#chat-box').find('.bot-message.temporary-message, .agent-message.temporary-message').last();
829 +
830 + // Determine styles
831 + let bgColor, fontColor;
832 + if (sender === "user") {
833 + bgColor = userMessageBgColor;
834 + fontColor = userMessageFontColor;
835 + } else if (sender === "agent") {
836 + bgColor = liveAgentMessageBgColor;
837 + fontColor = liveAgentMessageFontColor;
838 + } else {
839 + bgColor = botMessageBgColor;
840 + fontColor = botMessageFontColor;
841 + }
842 +
843 + var fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(responseText))));
844 + if (responseHtml) {
845 + fullMessage += '<br><br>' + responseHtml;
846 + }
847 +
848 + if (images.length > 0) {
849 + fullMessage += '<div class="image-gallery" dir="auto">'; // Add dir="auto" to image gallery
850 + images.forEach(img => {
851 + fullMessage += `
852 + <div style="margin-bottom: 10px;">
853 + <strong>${img.title}</strong><br>
854 + <a href="${img.image_url}" target="_blank">
855 + <img src="${img.thumbnail_url}" alt="${img.title}" style="max-width: 100px; height: auto; margin: 5px;" />
856 + </a>
857 + </div>`;
858 + });
859 + fullMessage += '</div>';
860 + }
861 +
862 + if (lastMessageDiv.length) {
863 + lastMessageDiv.fadeOut(200, function() {
864 + $(this)
865 + .html(fullMessage)
866 + .removeClass('bot-message user-message')
867 + .addClass(messageClass)
868 + .attr('dir', 'auto') // Add dir="auto" for automatic text direction
869 + .css({
870 + 'background-color': bgColor,
871 + 'color': fontColor,
872 + })
873 + .removeClass('temporary-message')
874 + .fadeIn(200, function() {
875 + if (sender === "bot" || sender === "agent") {
876 + const lastUserMessage = $('#chat-box').find('.user-message').last();
877 + if (lastUserMessage.length) {
878 + scrollElementToTop(lastUserMessage);
879 + }
880 + // Show notification if chat is hidden
881 + if ($('#floating-chatbot').hasClass('hidden')) {
882 + showNotification();
883 + }
884 + }
885 + });
886 + });
887 + } else {
888 + appendMessage(sender, responseText, responseHtml, images);
889 + }
890 + }
891 +
892 + function appendThinkingMessage() {
893 + // Remove any existing thinking dots first
894 + $('.thinking-dots').remove();
895 +
896 + // Retrieve the bot message font color and background color
897 + var botMessageFontColor = mxchatChat.bot_message_font_color;
898 + var botMessageBgColor = mxchatChat.bot_message_bg_color;
899 +
900 +
901 + var thinkingHtml = '<div class="thinking-dots-container">' +
902 + '<div class="thinking-dots">' +
903 + '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
904 + '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
905 + '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
906 + '</div>' +
907 + '</div>';
908 +
909 + // Append the thinking dots to the chat container (or within the temporary message div)
910 + $("#chat-box").append('<div class="bot-message temporary-message" style="background-color: ' + botMessageBgColor + ';">' + thinkingHtml + '</div>');
911 + scrollToBottom();
912 + }
913 +
914 + function removeThinkingDots() {
915 + $('.thinking-dots').closest('.temporary-message').remove();
916 + }
917 +
918 +
919 + // ====================================
920 + // TEXT FORMATTING & PROCESSING
921 + // ====================================
922 +
923 +
924 + function linkify(inputText) {
925 + if (!inputText) return '';
926 +
927 + // Process markdown headers
928 + let processedText = formatMarkdownHeaders(inputText);
929 +
930 + // Process markdown links
931 + const markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s]+)\)/g;
932 + processedText = processedText.replace(markdownLinkPattern, (match, text, url) => {
933 + const safeUrl = encodeURI(url);
934 + const safeText = sanitizeUserInput(text);
935 + return `<a href="${safeUrl}" target="${linkTarget}">${safeText}</a>`;
936 + });
937 +
938 + // Process phone numbers (tel:)
939 + const phonePattern = /\[([^\]]+)\]\((tel:[\d+]+)\)/g;
940 + processedText = processedText.replace(phonePattern, (match, text, phone) => {
941 + const safePhone = encodeURI(phone);
942 + const safeText = sanitizeUserInput(text);
943 + return `<a href="${safePhone}">${safeText}</a>`;
944 + });
945 +
946 + // Process standalone URLs
947 + const urlPattern = /(^|[^">])(https?:\/\/[^\s<]+)/gim;
948 + processedText = processedText.replace(urlPattern, (match, prefix, url) => {
949 + const safeUrl = encodeURI(url);
950 + return `${prefix}<a href="${safeUrl}" target="${linkTarget}">${url}</a>`;
951 + });
952 +
953 + // Process www. URLs
954 + const wwwPattern = /(^|[^">])(www\.[\S]+(\b|$))(?![^<]*<\/a>)/gim;
955 + processedText = processedText.replace(wwwPattern, (match, prefix, url) => {
956 + const safeUrl = encodeURI(`http://${url}`);
957 + return `${prefix}<a href="${safeUrl}" target="${linkTarget}">${url}</a>`;
958 + });
959 +
960 + // Add this after your phone pattern
961 + const mailtoPattern = /\[([^\]]+)\]\((mailto:[^\)]+)\)/g;
962 + processedText = processedText.replace(mailtoPattern, (match, text, mailto) => {
963 + const safeMailto = encodeURI(mailto);
964 + const safeText = sanitizeUserInput(text);
965 + return `<a href="${safeMailto}">${safeText}</a>`;
966 + });
967 +
968 + return processedText;
969 + }
970 +
971 + function formatMarkdownHeaders(text) {
972 + // Handle h1 to h6 headers
973 + return text.replace(/^(#{1,6})\s(.+)$/gm, function(match, hashes, content) {
974 + const level = hashes.length;
975 + return `<h${level} class="chat-heading">${content}</h${level}>`;
976 + });
977 + }
978 +
979 + function formatBoldText(text) {
980 + return text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
981 + }
982 +
983 + function convertNewlinesToBreaks(text) {
984 + // Split the text into paragraphs (marked by double newlines or multiple <br> tags)
985 + const paragraphs = text.split(/(?:\n\n|\<br\>\s*\<br\>)/g);
986 +
987 + // Wrap each paragraph in <p> tags
988 + return paragraphs
989 + .map(para => `<p>${para.trim()}</p>`)
990 + .join('');
991 + }
992 +
993 + function formatCodeBlocks(text) {
994 + // First handle raw PHP tags
995 + text = text.replace(/(<\?php[\s\S]*?\?>)/g, (match) => {
996 + return `<pre><code class="language-php">${escapeHtml(match)}</code></pre>`;
997 + });
998 +
999 + // Then handle code blocks with backticks
1000 + text = text.replace(/```php5?\n([\s\S]+?)```/gi, (match, code) => {
1001 + return `<pre><code class="language-php">${escapeHtml(code)}</code></pre>`;
1002 + });
1003 +
1004 + return text;
1005 + }
1006 +
1007 + function sanitizeUserInput(text) {
1008 + const div = document.createElement('div');
1009 + div.textContent = text;
1010 + return div.innerHTML;
1011 + }
1012 +
1013 +
1014 + function escapeHtml(unsafe) {
1015 + // First check if it's already a code block
1016 + if (unsafe.includes('<pre><code') || unsafe.includes('</code></pre>')) {
1017 + return unsafe;
1018 + }
1019 +
1020 + return unsafe
1021 + .replace(/&/g, "&amp;")
1022 + .replace(/</g, "&lt;")
1023 + .replace(/>/g, "&gt;")
1024 + .replace(/"/g, "&quot;")
1025 + .replace(/'/g, "&#039;");
1026 + }
1027 +
1028 + function decodeHTMLEntities(text) {
1029 + var textArea = document.createElement('textarea');
1030 + textArea.innerHTML = text;
1031 + return textArea.value;
1032 + }
1033 +
1034 +
1035 + // ====================================
1036 + // UI & SCROLLING CONTROLS
1037 + // ====================================
1038 +
1039 + function scrollToBottom(instant = false) {
1040 + var chatBox = $('#chat-box');
1041 + if (instant) {
1042 + // Instantly set the scroll position to the bottom
1043 + chatBox.scrollTop(chatBox.prop("scrollHeight"));
1044 + } else {
1045 + // Use requestAnimationFrame for smoother scrolling if needed
1046 + let start = null;
1047 + const scrollHeight = chatBox.prop("scrollHeight");
1048 + const initialScroll = chatBox.scrollTop();
1049 + const distance = scrollHeight - initialScroll;
1050 + const duration = 500; // Duration in ms
1051 +
1052 + function smoothScroll(timestamp) {
1053 + if (!start) start = timestamp;
1054 + const progress = timestamp - start;
1055 + const currentScroll = initialScroll + (distance * (progress / duration));
1056 + chatBox.scrollTop(currentScroll);
1057 +
1058 + if (progress < duration) {
1059 + requestAnimationFrame(smoothScroll);
1060 + } else {
1061 + chatBox.scrollTop(scrollHeight); // Ensure it's exactly at the bottom
1062 + }
1063 + }
1064 +
1065 + requestAnimationFrame(smoothScroll);
1066 + }
1067 + }
1068 +
1069 + function scrollElementToTop(element) {
1070 + var chatBox = $('#chat-box');
1071 + var elementTop = element.position().top + chatBox.scrollTop();
1072 + chatBox.animate({ scrollTop: elementTop }, 500);
1073 + }
1074 +
1075 + function showChatWidget() {
1076 + // First ensure display is set
1077 + $('#floating-chatbot-button').css('display', 'flex');
1078 + // Then handle the fade
1079 + $('#floating-chatbot-button').fadeTo(500, 1);
1080 + // Force visibility
1081 + $('#floating-chatbot-button').removeClass('hidden');
1082 + //console.log('Showing widget');
1083 + }
1084 +
1085 + function hideChatWidget() {
1086 + $('#floating-chatbot-button').css('display', 'none');
1087 + $('#floating-chatbot-button').addClass('hidden');
1088 + //console.log('Hiding widget');
1089 + }
1090 +
1091 + function disableScroll() {
1092 + if (isMobile()) {
1093 + $('body').css('overflow', 'hidden');
1094 + }
1095 + }
1096 +
1097 + function enableScroll() {
1098 + if (isMobile()) {
1099 + $('body').css('overflow', '');
1100 + }
1101 + }
1102 +
1103 + function isMobile() {
1104 + // This can be a simple check, or more sophisticated detection of mobile devices
1105 + return window.innerWidth <= 768; // Example threshold for mobile devices
1106 + }
1107 +
1108 + function setFullHeight() {
1109 + var vh = $(window).innerHeight() * 0.01;
1110 + $(':root').css('--vh', vh + 'px');
1111 + }
1112 +
1113 +
1114 + // ====================================
1115 + // NOTIFICATION SYSTEM
1116 + // ====================================
1117 +
1118 + function createNotificationBadge() {
1119 + //console.log("Creating notification badge...");
1120 + const chatButton = document.getElementById('floating-chatbot-button');
1121 + //console.log("Chat button found:", !!chatButton);
1122 +
1123 + if (!chatButton) return;
1124 +
1125 + // Remove any existing badge first
1126 + const existingBadge = chatButton.querySelector('.chat-notification-badge');
1127 + if (existingBadge) {
1128 + //console.log("Removing existing badge");
1129 + existingBadge.remove();
1130 + }
1131 +
1132 + notificationBadge = document.createElement('div');
1133 + notificationBadge.className = 'chat-notification-badge';
1134 + notificationBadge.style.cssText = `
1135 + display: none;
1136 + position: absolute;
1137 + top: -5px;
1138 + right: -5px;
1139 + background-color: red;
1140 + color: white;
1141 + border-radius: 50%;
1142 + padding: 4px 8px;
1143 + font-size: 12px;
1144 + font-weight: bold;
1145 + z-index: 10001;
1146 + `;
1147 + chatButton.style.position = 'relative';
1148 + chatButton.appendChild(notificationBadge);
1149 +
1150 + }
1151 +
1152 + function showNotification() {
1153 + const badge = document.getElementById('chat-notification-badge');
1154 + if (badge && $('#floating-chatbot').hasClass('hidden')) {
1155 + badge.style.display = 'block';
1156 + badge.textContent = '1';
1157 + }
1158 + }
1159 +
1160 + function hideNotification() {
1161 + const badge = document.getElementById('chat-notification-badge');
1162 + if (badge) {
1163 + badge.style.display = 'none';
1164 + }
1165 + }
1166 +
1167 + function startNotificationChecking() {
1168 + const chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
1169 + if (!chatPersistenceEnabled) return;
1170 +
1171 + createNotificationBadge();
1172 + notificationCheckInterval = setInterval(checkForNewMessages, 30000); // Check every 30 seconds
1173 + }
1174 +
1175 + function stopNotificationChecking() {
1176 + if (notificationCheckInterval) {
1177 + clearInterval(notificationCheckInterval);
1178 + }
1179 + }
1180 +
1181 + function checkForNewMessages() {
1182 + const sessionId = getChatSession();
1183 + const chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
1184 +
1185 + if (!chatPersistenceEnabled) return;
1186 +
1187 + $.ajax({
1188 + url: mxchatChat.ajax_url,
1189 + type: 'POST',
1190 + data: {
1191 + action: 'mxchat_check_new_messages',
1192 + session_id: sessionId,
1193 + last_seen_id: lastSeenMessageId,
1194 + nonce: mxchatChat.nonce
1195 + },
1196 + success: function(response) {
1197 + if (response.success && response.data.hasNewMessages) {
1198 + showNotification();
1199 + }
1200 + }
1201 + });
1202 + }
1203 +
1204 +
1205 + // ====================================
1206 + // LIVE AGENT FUNCTIONALITY
1207 + // ====================================
1208 +
1209 + function updateChatModeIndicator(mode) {
1210 + const indicator = document.getElementById('chat-mode-indicator');
1211 + if (indicator) {
1212 + // For Live Agent, keep as is; for AI mode, use the customized text
1213 + if (mode === 'agent') {
1214 + indicator.textContent = 'Live Agent';
1215 + } else {
1216 + // Get the custom AI agent text from a data attribute we'll add to the element
1217 + const customAiText = indicator.getAttribute('data-ai-text') || 'AI Agent';
1218 + indicator.textContent = customAiText;
1219 + }
1220 + }
1221 + // Start or stop polling based on mode
1222 + if (mode === 'agent') {
1223 + startPolling();
1224 + } else {
1225 + stopPolling();
1226 + }
1227 + }
1228 +
1229 + function startPolling() {
1230 + // Clear any existing interval first
1231 + stopPolling();
1232 + // Start new polling interval
1233 + pollingInterval = setInterval(checkForAgentMessages, 5000);
1234 + //console.log("Started agent message polling");
1235 + }
1236 +
1237 + function stopPolling() {
1238 + if (pollingInterval) {
1239 + clearInterval(pollingInterval);
1240 + pollingInterval = null;
1241 + //console.log("Stopped agent message polling");
1242 + }
1243 + }
1244 +
1245 +function checkForAgentMessages() {
1246 + const sessionId = getChatSession();
1247 + $.ajax({
1248 + url: mxchatChat.ajax_url,
1249 + type: 'POST',
1250 + dataType: 'json',
1251 + data: {
1252 + action: 'mxchat_fetch_new_messages',
1253 + session_id: sessionId,
1254 + last_seen_id: lastSeenMessageId,
1255 + persistence_enabled: 'true', // Add this too
1256 + nonce: mxchatChat.nonce
1257 + },
1258 + success: function (response) {
1259 + if (response.success && response.data?.new_messages) {
1260 + let hasNewMessage = false;
1261 +
1262 + response.data.new_messages.forEach(function (message) {
1263 + if (message.role === "agent" && !processedMessageIds.has(message.id)) {
1264 + hasNewMessage = true;
1265 + // CHANGE THIS LINE:
1266 + appendMessage("agent", message.content); // Instead of replaceLastMessage
1267 + lastSeenMessageId = message.id;
1268 + processedMessageIds.add(message.id);
1269 + }
1270 + });
1271 +
1272 + if (hasNewMessage && $('#floating-chatbot').hasClass('hidden')) {
1273 + showNotification();
1274 + }
1275 +
1276 + scrollToBottom(true);
1277 + }
1278 + },
1279 + error: function (xhr, status, error) {
1280 + //console.error("Polling error:", xhr, status, error);
1281 + }
1282 + });
1283 +}
1284 +
1285 + // ====================================
1286 + // CHAT HISTORY & PERSISTENCE
1287 + // ====================================
1288 +
1289 + function loadChatHistory() {
1290 + var sessionId = getChatSession();
1291 + var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
1292 +
1293 + if (chatPersistenceEnabled && sessionId) {
1294 + $.ajax({
1295 + url: mxchatChat.ajax_url,
1296 + type: 'POST',
1297 + dataType: 'json',
1298 + data: {
1299 + action: 'mxchat_fetch_conversation_history',
1300 + session_id: sessionId
1301 + },
1302 + success: function(response) {
1303 + if (response.success && response.data && Array.isArray(response.data.conversation)) {
1304 +
1305 +
1306 + var $chatBox = $('#chat-box');
1307 + var $fragment = $(document.createDocumentFragment());
1308 + let highestMessageId = lastSeenMessageId;
1309 +
1310 + if (response.data.chat_mode) {
1311 + updateChatModeIndicator(response.data.chat_mode);
1312 + }
1313 +
1314 + $.each(response.data.conversation, function(index, message) {
1315 + // Skip agent messages if persistence is off
1316 + if (!chatPersistenceEnabled && message.role === 'agent') {
1317 + return;
1318 + }
1319 +
1320 + var messageClass, messageBgColor, messageFontColor;
1321 +
1322 + switch (message.role) {
1323 + case 'user':
1324 + messageClass = 'user-message';
1325 + messageBgColor = userMessageBgColor;
1326 + messageFontColor = userMessageFontColor;
1327 + break;
1328 + case 'agent':
1329 + messageClass = 'agent-message';
1330 + messageBgColor = liveAgentMessageBgColor;
1331 + messageFontColor = liveAgentMessageFontColor;
1332 + break;
1333 + default:
1334 + messageClass = 'bot-message';
1335 + messageBgColor = botMessageBgColor;
1336 + messageFontColor = botMessageFontColor;
1337 + break;
1338 + }
1339 +
1340 + var messageElement = $('<div>').addClass(messageClass)
1341 + .css({
1342 + 'background': messageBgColor,
1343 + 'color': messageFontColor
1344 + });
1345 +
1346 + var content = message.content;
1347 + content = content.replace(/\\'/g, "'").replace(/\\"/g, '"');
1348 + content = decodeHTMLEntities(content);
1349 +
1350 + if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) {
1351 + messageElement.html(content);
1352 + } else {
1353 + var formattedContent = linkify(
1354 + formatBoldText(
1355 + convertNewlinesToBreaks(formatCodeBlocks(content))
1356 + )
1357 + );
1358 + messageElement.html(formattedContent);
1359 + }
1360 +
1361 + $fragment.append(messageElement);
1362 +
1363 + // In loadChatHistory, change this part:
1364 + if (message.id) {
1365 + highestMessageId = Math.max(highestMessageId, message.id);
1366 + processedMessageIds.add(message.id); // Add all message IDs to processed set
1367 + }
1368 + });
1369 +
1370 + $chatBox.append($fragment);
1371 + scrollToBottom(true);
1372 +
1373 + if (response.data.conversation.length > 0 && hasQuickQuestions()) {
1374 + collapseQuickQuestions();
1375 + }
1376 +
1377 + // Update lastSeenMessageId after history loads
1378 + lastSeenMessageId = highestMessageId;
1379 +
1380 + // Only update chat mode if persistence is enabled
1381 + if (chatPersistenceEnabled && response.data.conversation.length > 0) {
1382 + var lastMessage = response.data.conversation[response.data.conversation.length - 1];
1383 + if (lastMessage.role === 'agent') {
1384 + updateChatModeIndicator('agent');
1385 + }
1386 + }
1387 + } else {
1388 + console.warn("No conversation history found.");
1389 + }
1390 + },
1391 + error: function(xhr, status, error) {
1392 + //console.error("Error loading chat history:", status, error);
1393 + appendMessage("bot", "Unable to load chat history.");
1394 + }
1395 + });
1396 + } else {
1397 + console.warn("Chat persistence is disabled or no session ID found. Not loading history.");
1398 + }
1399 + }
1400 +
1401 +
1402 + // ====================================
1403 + // FILE UPLOAD FUNCTIONALITY
1404 + // ====================================
1405 +
1406 + function addSafeEventListener(elementId, eventType, handler) {
1407 + const element = document.getElementById(elementId);
1408 + if (element) {
1409 + element.addEventListener(eventType, handler);
1410 + }
1411 + }
1412 +
1413 + function showActivePdf(filename) {
1414 + const container = document.getElementById('active-pdf-container');
1415 + const nameElement = document.getElementById('active-pdf-name');
1416 +
1417 + if (!container || !nameElement) {
1418 + //console.error('PDF container elements not found');
1419 + return;
1420 + }
1421 +
1422 + nameElement.textContent = filename;
1423 + container.style.display = 'flex';
1424 + }
1425 +
1426 + function showActiveWord(filename) {
1427 + const container = document.getElementById('active-word-container');
1428 + const nameElement = document.getElementById('active-word-name');
1429 +
1430 + if (!container || !nameElement) {
1431 + //console.error('Word document container elements not found');
1432 + return;
1433 + }
1434 +
1435 + nameElement.textContent = filename;
1436 + container.style.display = 'flex';
1437 + }
1438 +
1439 + function removeActivePdf() {
1440 + const container = document.getElementById('active-pdf-container');
1441 + const nameElement = document.getElementById('active-pdf-name');
1442 +
1443 + if (!container || !nameElement || !activePdfFile) return;
1444 +
1445 + fetch(mxchatChat.ajax_url, {
1446 + method: 'POST',
1447 + headers: {
1448 + 'Content-Type': 'application/x-www-form-urlencoded',
1449 + },
1450 + body: new URLSearchParams({
1451 + 'action': 'mxchat_remove_pdf',
1452 + 'session_id': sessionId,
1453 + 'nonce': mxchatChat.nonce
1454 + })
1455 + })
1456 + .then(response => response.json())
1457 + .then(data => {
1458 + if (data.success) {
1459 + container.style.display = 'none';
1460 + nameElement.textContent = '';
1461 + activePdfFile = null;
1462 + appendMessage('bot', 'PDF removed.');
1463 + }
1464 + })
1465 + .catch(error => {
1466 + //console.error('Error removing PDF:', error);
1467 + });
1468 + }
1469 +
1470 + function removeActiveWord() {
1471 + const container = document.getElementById('active-word-container');
1472 + const nameElement = document.getElementById('active-word-name');
1473 +
1474 + if (!container || !nameElement || !activeWordFile) return;
1475 +
1476 + fetch(mxchatChat.ajax_url, {
1477 + method: 'POST',
1478 + headers: {
1479 + 'Content-Type': 'application/x-www-form-urlencoded',
1480 + },
1481 + body: new URLSearchParams({
1482 + 'action': 'mxchat_remove_word',
1483 + 'session_id': sessionId,
1484 + 'nonce': mxchatChat.nonce
1485 + })
1486 + })
1487 + .then(response => response.json())
1488 + .then(data => {
1489 + if (data.success) {
1490 + container.style.display = 'none';
1491 + nameElement.textContent = '';
1492 + activeWordFile = null;
1493 + appendMessage('bot', 'Word document removed.');
1494 + }
1495 + })
1496 + .catch(error => {
1497 + //console.error('Error removing Word document:', error);
1498 + });
1499 + }
1500 +
1501 + // ====================================
1502 + // CONSENT & COMPLIANCE (GDPR)
1503 + // ====================================
1504 +
1505 + function initializeChatVisibility() {
1506 + //console.log('Initializing chat visibility');
1507 + const complianzEnabled = mxchatChat.complianz_toggle === 'on' ||
1508 + mxchatChat.complianz_toggle === '1' ||
1509 + mxchatChat.complianz_toggle === 1;
1510 +
1511 + if (complianzEnabled && typeof cmplz_has_consent === "function" && typeof complianz !== 'undefined') {
1512 + // Initial check
1513 + checkConsentAndShowChat();
1514 +
1515 + // Listen for consent changes
1516 + $(document).on('cmplz_status_change', function(event) {
1517 + //console.log('Status change detected');
1518 + checkConsentAndShowChat();
1519 + });
1520 + } else {
1521 + // If Complianz is not enabled, always show
1522 + $('#floating-chatbot-button')
1523 + .css('display', 'flex')
1524 + .removeClass('hidden no-consent')
1525 + .fadeTo(500, 1);
1526 +
1527 + // Also check pre-chat message when Complianz is not enabled
1528 + checkPreChatDismissal();
1529 + }
1530 + }
1531 +
1532 +
1533 + function checkConsentAndShowChat() {
1534 + var consentStatus = cmplz_has_consent('marketing');
1535 + var consentType = complianz.consenttype;
1536 +
1537 + //console.log('Checking consent:', {status: consentStatus,type: consentType});
1538 +
1539 + let $widget = $('#floating-chatbot-button');
1540 + let $chatbot = $('#floating-chatbot');
1541 + let $preChat = $('#pre-chat-message');
1542 +
1543 + if (consentStatus === true) {
1544 + //console.log('Consent granted - showing widget');
1545 + $widget
1546 + .removeClass('no-consent')
1547 + .css('display', 'flex')
1548 + .removeClass('hidden')
1549 + .fadeTo(500, 1);
1550 + $chatbot.removeClass('no-consent');
1551 +
1552 + // Show pre-chat message if not dismissed
1553 + checkPreChatDismissal();
1554 + } else {
1555 + //console.log('No consent - hiding widget');
1556 + $widget
1557 + .addClass('no-consent')
1558 + .fadeTo(500, 0, function() {
1559 + $(this)
1560 + .css('display', 'none')
1561 + .addClass('hidden');
1562 + });
1563 + $chatbot.addClass('no-consent');
1564 +
1565 + // Hide pre-chat message when no consent
1566 + $preChat.hide();
1567 + }
1568 + }
1569 +
1570 +
1571 + // ====================================
1572 + // PRE-CHAT MESSAGE HANDLING
1573 + // ====================================
1574 +
1575 + function checkPreChatDismissal() {
1576 + $.ajax({
1577 + url: mxchatChat.ajax_url,
1578 + type: 'POST',
1579 + data: {
1580 + action: 'mxchat_check_pre_chat_message_status',
1581 + _ajax_nonce: mxchatChat.nonce
1582 + },
1583 + success: function(response) {
1584 + if (response.success && !response.data.dismissed) {
1585 + $('#pre-chat-message').fadeIn(250);
1586 + } else {
1587 + $('#pre-chat-message').hide();
1588 + }
1589 + },
1590 + error: function() {
1591 + //console.error('Failed to check pre-chat message dismissal status.');
1592 + }
1593 + });
1594 + }
1595 +
1596 + function handlePreChatDismissal() {
1597 + $('#pre-chat-message').fadeOut(200);
1598 + $.ajax({
1599 + url: mxchatChat.ajax_url,
1600 + type: 'POST',
1601 + data: {
1602 + action: 'mxchat_dismiss_pre_chat_message',
1603 + _ajax_nonce: mxchatChat.nonce
1604 + },
1605 + success: function() {
1606 + $('#pre-chat-message').hide();
1607 + },
1608 + error: function() {
1609 + //console.error('Failed to dismiss pre-chat message.');
1610 + }
1611 + });
1612 + }
1613 +
1614 +
1615 + // ====================================
1616 + // UTILITY FUNCTIONS
1617 + // ====================================
1618 +
1619 + function copyToClipboard(text) {
1620 + var tempInput = $('<input>');
1621 + $('body').append(tempInput);
1622 + tempInput.val(text).select();
1623 + document.execCommand('copy');
1624 + tempInput.remove();
1625 + }
1626 +
1627 +
1628 + function isImageHtml(str) {
1629 + return str.startsWith('<img') && str.endsWith('>');
1630 + }
1631 +
1632 +
1633 + // ====================================
1634 + // EVENT HANDLERS & INITIALIZATION
1635 + // ====================================
1636 +
1637 +$(document).on('click', '.mxchat-popular-question', function () {
1638 + var question = $(this).text();
1639 +
1640 + // Append the question as if the user typed it
1641 + appendMessage("user", question);
1642 +
1643 + // Only collapse if there are questions
1644 + if (hasQuickQuestions()) {
1645 + collapseQuickQuestions();
1646 + }
1647 +
1648 + // Send the question to the server
1649 + sendMessageToChatbot(question);
1650 +});
1651 +
1652 +$(document).on('click', '.questions-toggle-btn', function(e) {
1653 + e.preventDefault();
1654 + e.stopPropagation();
1655 + expandQuickQuestions();
1656 +});
1657 +
1658 +$(document).on('click', '.questions-collapse-btn', function(e) {
1659 + e.preventDefault();
1660 + e.stopPropagation();
1661 + collapseQuickQuestions();
1662 +});
1663 +
1664 + // Chatbot visibility toggle handlers
1665 + $(document).on('click', '#floating-chatbot-button', function() {
1666 + var chatbot = $('#floating-chatbot');
1667 + if (chatbot.hasClass('hidden')) {
1668 + chatbot.removeClass('hidden').addClass('visible');
1669 + $(this).addClass('hidden');
1670 + $('#chat-notification-badge').hide(); // Hide notification when opening chat
1671 + disableScroll();
1672 + $('#pre-chat-message').fadeOut(250);
1673 + } else {
1674 + chatbot.removeClass('visible').addClass('hidden');
1675 + $(this).removeClass('hidden');
1676 + enableScroll();
1677 + checkPreChatDismissal();
1678 + }
1679 + });
1680 +
1681 + $(document).on('click', '#exit-chat-button', function() {
1682 + $('#floating-chatbot').addClass('hidden').removeClass('visible');
1683 + $('#floating-chatbot-button').removeClass('hidden');
1684 + enableScroll();
1685 + });
1686 +
1687 + $(document).on('click', '.close-pre-chat-message', function(e) {
1688 + e.stopPropagation(); // Prevent triggering the parent .pre-chat-message click
1689 + $('#pre-chat-message').fadeOut(200, function() {
1690 + $(this).remove();
1691 + });
1692 + });
1693 +
1694 + // Add to Cart button handler
1695 + $(document).on('click', '.mxchat-add-to-cart-button', function() {
1696 + var productId = $(this).data('product-id');
1697 +
1698 + // Get the button text instead of hardcoded "add to cart"
1699 + var buttonText = $(this).text() || "add to cart";
1700 +
1701 + // Add a special prefix to indicate this is from button
1702 + appendMessage("user", buttonText);
1703 + sendMessageToChatbot("!addtocart"); // Special command to indicate button click
1704 + });
1705 +
1706 + // PDF upload button handlers
1707 + if (document.getElementById('pdf-upload-btn')) {
1708 + document.getElementById('pdf-upload-btn').addEventListener('click', function() {
1709 + document.getElementById('pdf-upload').click();
1710 + });
1711 + }
1712 +
1713 + // Word upload button handlers
1714 + if (document.getElementById('word-upload-btn')) {
1715 + document.getElementById('word-upload-btn').addEventListener('click', function() {
1716 + document.getElementById('word-upload').click();
1717 + });
1718 + }
1719 +
1720 + // PDF file input change handler
1721 + addSafeEventListener('pdf-upload', 'change', async function(e) {
1722 + const file = e.target.files[0];
1723 +
1724 + if (!file || file.type !== 'application/pdf') {
1725 + alert('Please select a valid PDF file.');
1726 + return;
1727 + }
1728 +
1729 + if (!sessionId) {
1730 + //console.error('No session ID found');
1731 + alert('Error: No session ID found');
1732 + return;
1733 + }
1734 +
1735 + if (!mxchatChat || !mxchatChat.ajax_url || !mxchatChat.nonce) {
1736 + //console.error('mxchatChat not properly configured:', mxchatChat);
1737 + alert('Error: Ajax configuration missing');
1738 + return;
1739 + }
1740 +
1741 + // Disable buttons and show loading state
1742 + const uploadBtn = document.getElementById('pdf-upload-btn');
1743 + const sendBtn = document.getElementById('send-button');
1744 + const originalBtnContent = uploadBtn.innerHTML;
1745 +
1746 + try {
1747 + const formData = new FormData();
1748 + formData.append('action', 'mxchat_upload_pdf');
1749 + formData.append('pdf_file', file);
1750 + formData.append('session_id', sessionId);
1751 + formData.append('nonce', mxchatChat.nonce);
1752 +
1753 + uploadBtn.disabled = true;
1754 + sendBtn.disabled = true;
1755 + uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
1756 + <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
1757 + </svg>`;
1758 +
1759 + const response = await fetch(mxchatChat.ajax_url, {
1760 + method: 'POST',
1761 + body: formData
1762 + });
1763 +
1764 + const data = await response.json();
1765 +
1766 + if (data.success) {
1767 + // Hide popular questions if they exist
1768 + const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
1769 + if (hasQuickQuestions()) {
1770 + collapseQuickQuestions();
1771 + }
1772 +
1773 + // Show the active PDF name
1774 + showActivePdf(data.data.filename);
1775 +
1776 + appendMessage('bot', data.data.message);
1777 + scrollToBottom();
1778 + activePdfFile = data.data.filename;
1779 + } else {
1780 + //console.error('Upload failed:', data.data);
1781 + alert('Failed to upload PDF. Please try again.');
1782 + }
1783 + } catch (error) {
1784 + //console.error('Upload error:', error);
1785 + alert('Error uploading file. Please try again.');
1786 + } finally {
1787 + uploadBtn.disabled = false;
1788 + sendBtn.disabled = false;
1789 + uploadBtn.innerHTML = originalBtnContent;
1790 + this.value = ''; // Reset file input
1791 + }
1792 + });
1793 +
1794 + // Word file input change handler
1795 + addSafeEventListener('word-upload', 'change', async function(e) {
1796 + const file = e.target.files[0];
1797 +
1798 + if (!file || file.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
1799 + alert('Please select a valid Word document (.docx).');
1800 + return;
1801 + }
1802 +
1803 + if (!sessionId) {
1804 + //console.error('No session ID found');
1805 + alert('Error: No session ID found');
1806 + return;
1807 + }
1808 +
1809 + // Disable buttons and show loading state
1810 + const uploadBtn = document.getElementById('word-upload-btn');
1811 + const sendBtn = document.getElementById('send-button');
1812 + const originalBtnContent = uploadBtn.innerHTML;
1813 +
1814 + try {
1815 + const formData = new FormData();
1816 + formData.append('action', 'mxchat_upload_word');
1817 + formData.append('word_file', file);
1818 + formData.append('session_id', sessionId);
1819 + formData.append('nonce', mxchatChat.nonce);
1820 +
1821 + uploadBtn.disabled = true;
1822 + sendBtn.disabled = true;
1823 + uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
1824 + <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
1825 + </svg>`;
1826 +
1827 + const response = await fetch(mxchatChat.ajax_url, {
1828 + method: 'POST',
1829 + body: formData
1830 + });
1831 +
1832 + const data = await response.json();
1833 +
1834 + if (data.success) {
1835 + // Hide popular questions if they exist
1836 + const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
1837 + if (hasQuickQuestions()) {
1838 + collapseQuickQuestions();
1839 + }
1840 +
1841 + // Show the active Word document name
1842 + showActiveWord(data.data.filename);
1843 +
1844 + appendMessage('bot', data.data.message);
1845 + scrollToBottom();
1846 + activeWordFile = data.data.filename;
1847 + } else {
1848 + //console.error('Upload failed:', data.data);
1849 + alert('Failed to upload Word document. Please try again.');
1850 + }
1851 + } catch (error) {
1852 + //console.error('Upload error:', error);
1853 + alert('Error uploading file. Please try again.');
1854 + } finally {
1855 + uploadBtn.disabled = false;
1856 + sendBtn.disabled = false;
1857 + uploadBtn.innerHTML = originalBtnContent;
1858 + this.value = ''; // Reset file input
1859 + }
1860 + });
1861 +
1862 + // Remove button click handlers
1863 + document.getElementById('remove-pdf-btn')?.addEventListener('click', function(e) {
1864 + e.preventDefault();
1865 + e.stopPropagation();
1866 + removeActivePdf();
1867 + });
1868 +
1869 + document.getElementById('remove-word-btn')?.addEventListener('click', function(e) {
1870 + e.preventDefault();
1871 + e.stopPropagation();
1872 + removeActiveWord();
1873 + });
1874 +
1875 + // Window resize handlers
1876 + $(window).on('resize orientationchange', function() {
1877 + setFullHeight();
1878 + });
1879 +
1880 +
1881 + // ====================================
1882 + // TOOLBAR & STYLING SETUP
1883 + // ====================================
1884 +
1885 + // Apply toolbar settings
1886 + if (mxchatChat.chat_toolbar_toggle === 'on') {
1887 + $('.chat-toolbar').show();
1888 + } else {
1889 + $('.chat-toolbar').hide();
1890 + }
1891 +
1892 + // Apply toolbar icon colors
1893 + const toolbarElements = [
1894 + '#mxchat-chatbot .toolbar-btn svg',
1895 + '#mxchat-chatbot .active-pdf-name',
1896 + '#mxchat-chatbot .active-word-name',
1897 + '#mxchat-chatbot .remove-pdf-btn svg',
1898 + '#mxchat-chatbot .remove-word-btn svg',
1899 + '#mxchat-chatbot .toolbar-perplexity svg'
1900 + ];
1901 +
1902 + toolbarElements.forEach(selector => {
1903 + $(selector).css({
1904 + 'fill': toolbarIconColor,
1905 + 'stroke': toolbarIconColor,
1906 + 'color': toolbarIconColor
1907 + });
1908 + });
1909 +
1910 +
1911 +// ====================================
1912 +// IMPROVED EMAIL COLLECTION SETUP
1913 +// ====================================
1914 +
1915 +// Email collection form setup and handlers
1916 +const emailForm = document.getElementById('email-collection-form');
1917 +const emailBlocker = document.getElementById('email-blocker');
1918 +const chatbotWrapper = document.getElementById('chat-container');
1919 +
1920 +if (emailForm && emailBlocker && chatbotWrapper) {
1921 + // Add loading state management
1922 + let isSubmitting = false;
1923 +
1924 + // Check if email exists for the current session
1925 + function checkSessionAndEmail() {
1926 + const sessionId = getChatSession();
1927 +
1928 + // Add timeout to prevent hanging
1929 + const controller = new AbortController();
1930 + const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout
1931 +
1932 + fetch(mxchatChat.ajax_url, {
1933 + method: 'POST',
1934 + headers: {
1935 + 'Content-Type': 'application/x-www-form-urlencoded',
1936 + },
1937 + body: new URLSearchParams({
1938 + action: 'mxchat_check_email_provided',
1939 + session_id: sessionId,
1940 + nonce: mxchatChat.nonce,
1941 + }),
1942 + signal: controller.signal
1943 + })
1944 + .then((response) => {
1945 + clearTimeout(timeoutId);
1946 + if (!response.ok) {
1947 + throw new Error(`HTTP error! status: ${response.status}`);
1948 + }
1949 + return response.json();
1950 + })
1951 + .then((data) => {
1952 + if (data.success) {
1953 + if (data.data.logged_in || data.data.email) {
1954 + showChatContainer();
1955 + } else {
1956 + showEmailForm();
1957 + }
1958 + } else {
1959 + // On error, default to showing email form
1960 + showEmailForm();
1961 + }
1962 + })
1963 + .catch((error) => {
1964 + clearTimeout(timeoutId);
1965 + console.warn('Email check failed, defaulting to email form:', error);
1966 + showEmailForm();
1967 + });
1968 + }
1969 +
1970 + // Optimized UI transition functions
1971 + function showEmailForm() {
1972 + emailBlocker.style.display = 'flex';
1973 + chatbotWrapper.style.display = 'none';
1974 + }
1975 +
1976 + function showChatContainer() {
1977 + // Show chat immediately without delay
1978 + emailBlocker.style.display = 'none';
1979 + chatbotWrapper.style.display = 'flex';
1980 +
1981 + // Load chat history only after showing chat container
1982 + if (typeof loadChatHistory === 'function') {
1983 + loadChatHistory();
1984 + }
1985 + }
1986 +
1987 + // Enhanced email validation
1988 + function isValidEmail(email) {
1989 + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
1990 + return emailRegex.test(email.trim()) && email.length <= 254; // RFC 5321 limit
1991 + }
1992 +
1993 + // Show loading state with spinner
1994 + function setSubmissionState(loading) {
1995 + const submitButton = document.getElementById('email-submit-button');
1996 + const emailInput = document.getElementById('user-email');
1997 +
1998 + if (loading) {
1999 + isSubmitting = true;
2000 + submitButton.disabled = true;
2001 + emailInput.disabled = true;
2002 +
2003 + // Store original content and add spinner
2004 + if (!submitButton.getAttribute('data-original-html')) {
2005 + submitButton.setAttribute('data-original-html', submitButton.innerHTML);
2006 + }
2007 +
2008 + // Add loading spinner while keeping original text
2009 + const originalText = submitButton.textContent;
2010 + submitButton.innerHTML = `
2011 + <svg class="email-spinner" style="width: 16px; height: 16px; margin-right: 8px; animation: spin 1s linear infinite;" viewBox="0 0 24 24">
2012 + <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" fill="none" stroke-dasharray="31.416" stroke-dashoffset="31.416">
2013 + <animate attributeName="stroke-dasharray" dur="2s" values="0 31.416;15.708 15.708;0 31.416" repeatCount="indefinite"/>
2014 + <animate attributeName="stroke-dashoffset" dur="2s" values="0;-15.708;-31.416" repeatCount="indefinite"/>
2015 + </circle>
2016 + </svg>
2017 + ${originalText}
2018 + `;
2019 +
2020 + submitButton.style.opacity = '0.8';
2021 + } else {
2022 + isSubmitting = false;
2023 + submitButton.disabled = false;
2024 + emailInput.disabled = false;
2025 +
2026 + // Restore original content
2027 + const originalHtml = submitButton.getAttribute('data-original-html');
2028 + if (originalHtml) {
2029 + submitButton.innerHTML = originalHtml;
2030 + }
2031 +
2032 + submitButton.style.opacity = '1';
2033 + }
2034 + }
2035 +
2036 + // Add CSS for spinner animation if not already present
2037 + if (!document.getElementById('email-spinner-styles')) {
2038 + const style = document.createElement('style');
2039 + style.id = 'email-spinner-styles';
2040 + style.textContent = `
2041 + @keyframes spin {
2042 + from { transform: rotate(0deg); }
2043 + to { transform: rotate(360deg); }
2044 + }
2045 + .email-spinner {
2046 + display: inline-block;
2047 + vertical-align: middle;
2048 + }
2049 + `;
2050 + document.head.appendChild(style);
2051 + }
2052 +
2053 + // Handle email form submission with improved error handling
2054 + emailForm.addEventListener('submit', function (event) {
2055 + event.preventDefault();
2056 +
2057 + // Prevent double submission
2058 + if (isSubmitting) {
2059 + return;
2060 + }
2061 +
2062 + const userEmail = document.getElementById('user-email').value.trim();
2063 + const sessionId = getChatSession();
2064 +
2065 + // Validate email before submission
2066 + if (!userEmail) {
2067 + showEmailError('Please enter your email address.');
2068 + return;
2069 + }
2070 +
2071 + if (!isValidEmail(userEmail)) {
2072 + showEmailError('Please enter a valid email address.');
2073 + return;
2074 + }
2075 +
2076 + // Clear any existing errors
2077 + clearEmailError();
2078 + setSubmissionState(true);
2079 +
2080 + // Add timeout for submission
2081 + const controller = new AbortController();
2082 + const timeoutId = setTimeout(() => {
2083 + controller.abort();
2084 + setSubmissionState(false);
2085 + showEmailError('Request timed out. Please try again.');
2086 + }, 15000); // 15 second timeout
2087 +
2088 + fetch(mxchatChat.ajax_url, {
2089 + method: 'POST',
2090 + headers: {
2091 + 'Content-Type': 'application/x-www-form-urlencoded',
2092 + },
2093 + body: new URLSearchParams({
2094 + action: 'mxchat_handle_save_email_and_response',
2095 + email: userEmail,
2096 + session_id: sessionId,
2097 + nonce: mxchatChat.nonce,
2098 + }),
2099 + signal: controller.signal
2100 + })
2101 + .then((response) => {
2102 + clearTimeout(timeoutId);
2103 + if (!response.ok) {
2104 + throw new Error(`HTTP error! status: ${response.status}`);
2105 + }
2106 + return response.json();
2107 + })
2108 + .then((data) => {
2109 + setSubmissionState(false);
2110 +
2111 + if (data.success) {
2112 + // Show chat immediately
2113 + showChatContainer();
2114 +
2115 + // Handle bot response if provided
2116 + if (data.message && typeof appendMessage === 'function') {
2117 + setTimeout(() => {
2118 + appendMessage('bot', data.message);
2119 + if (typeof scrollToBottom === 'function') {
2120 + scrollToBottom();
2121 + }
2122 + }, 100);
2123 + }
2124 + } else {
2125 + showEmailError(data.message || 'Failed to save email. Please try again.');
2126 + }
2127 + })
2128 + .catch((error) => {
2129 + clearTimeout(timeoutId);
2130 + setSubmissionState(false);
2131 +
2132 + if (error.name === 'AbortError') {
2133 + showEmailError('Request timed out. Please try again.');
2134 + } else {
2135 + console.error('Email submission error:', error);
2136 + showEmailError('An error occurred. Please try again.');
2137 + }
2138 + });
2139 + });
2140 +
2141 + // Real-time email validation
2142 + const emailInput = document.getElementById('user-email');
2143 + if (emailInput) {
2144 + let validationTimeout;
2145 +
2146 + emailInput.addEventListener('input', function() {
2147 + // Clear previous validation timeout
2148 + if (validationTimeout) {
2149 + clearTimeout(validationTimeout);
2150 + }
2151 +
2152 + // Debounce validation
2153 + validationTimeout = setTimeout(() => {
2154 + const email = this.value.trim();
2155 + clearEmailError();
2156 +
2157 + if (email && !isValidEmail(email)) {
2158 + showEmailError('Please enter a valid email address.');
2159 + }
2160 + }, 500);
2161 + });
2162 +
2163 + // Handle Enter key
2164 + emailInput.addEventListener('keypress', function(e) {
2165 + if (e.key === 'Enter' && !isSubmitting) {
2166 + emailForm.dispatchEvent(new Event('submit'));
2167 + }
2168 + });
2169 + }
2170 +
2171 + // Error display functions
2172 + function showEmailError(message) {
2173 + clearEmailError();
2174 +
2175 + const errorDiv = document.createElement('div');
2176 + errorDiv.className = 'email-error';
2177 + errorDiv.style.cssText = `
2178 + color: #e74c3c;
2179 + font-size: 12px;
2180 + margin-top: 8px;
2181 + padding: 4px 0;
2182 + animation: fadeInError 0.3s ease;
2183 + `;
2184 + errorDiv.textContent = message;
2185 +
2186 + // Add CSS animation if not already present
2187 + if (!document.getElementById('email-error-styles')) {
2188 + const style = document.createElement('style');
2189 + style.id = 'email-error-styles';
2190 + style.textContent = `
2191 + @keyframes fadeInError {
2192 + from { opacity: 0; transform: translateY(-5px); }
2193 + to { opacity: 1; transform: translateY(0); }
2194 + }
2195 + .email-input-shake {
2196 + animation: shake 0.5s ease-in-out;
2197 + }
2198 + @keyframes shake {
2199 + 0%, 100% { transform: translateX(0); }
2200 + 25% { transform: translateX(-5px); }
2201 + 75% { transform: translateX(5px); }
2202 + }
2203 + `;
2204 + document.head.appendChild(style);
2205 + }
2206 +
2207 + emailForm.appendChild(errorDiv);
2208 +
2209 + // Add shake animation to input
2210 + if (emailInput) {
2211 + emailInput.classList.add('email-input-shake');
2212 + setTimeout(() => {
2213 + emailInput.classList.remove('email-input-shake');
2214 + }, 500);
2215 + }
2216 + }
2217 +
2218 + function clearEmailError() {
2219 + const existingErrors = emailForm.querySelectorAll('.email-error');
2220 + existingErrors.forEach(error => error.remove());
2221 + }
2222 +
2223 + // Initialize email check with delay to prevent race conditions
2224 + setTimeout(checkSessionAndEmail, 100);
2225 +
2226 +} else if (mxchatChat.email_collection_enabled) {
2227 + console.error('Essential elements for email handling are missing:', {
2228 + emailForm: !!emailForm,
2229 + emailBlocker: !!emailBlocker,
2230 + chatbotWrapper: !!chatbotWrapper
2231 + });
2232 +}
2233 +
2234 +
2235 + // Open chatbot when pre-chat message is clicked
2236 + $(document).on('click', '#pre-chat-message', function() {
2237 + var chatbot = $('#floating-chatbot');
2238 + if (chatbot.hasClass('hidden')) {
2239 + chatbot.removeClass('hidden').addClass('visible');
2240 + $('#floating-chatbot-button').addClass('hidden');
2241 + $('#pre-chat-message').fadeOut(250); // Hide pre-chat message
2242 + disableScroll(); // Disable scroll when chatbot opens
2243 + }
2244 + });
2245 +
2246 + var closeButton = document.querySelector('.close-pre-chat-message');
2247 + if (closeButton) {
2248 + closeButton.addEventListener('click', function() {
2249 + $('#pre-chat-message').fadeOut(200); // Hide the message
2250 +
2251 + // Send an AJAX request to set the transient flag for 24 hours
2252 + $.ajax({
2253 + url: mxchatChat.ajax_url,
2254 + type: 'POST',
2255 + data: {
2256 + action: 'mxchat_dismiss_pre_chat_message',
2257 + _ajax_nonce: mxchatChat.nonce
2258 + },
2259 + success: function() {
2260 + //console.log('Pre-chat message dismissed for 24 hours.');
2261 +
2262 + // Ensure the message is hidden after dismissal
2263 + $('#pre-chat-message').hide();
2264 + },
2265 + error: function() {
2266 + ////console.error('Failed to dismiss pre-chat message.');
2267 + }
2268 + });
2269 + });
2270 + }
2271 +
2272 +
2273 +function hasQuickQuestions() {
2274 + const questionButtons = document.querySelectorAll('#mxchat-popular-questions .mxchat-popular-question');
2275 + return questionButtons.length > 0;
2276 +}
2277 +
2278 +function collapseQuickQuestions() {
2279 + const questionsContainer = document.getElementById('mxchat-popular-questions');
2280 + if (questionsContainer && hasQuickQuestions()) {
2281 + questionsContainer.classList.add('collapsed');
2282 + questionsContainer.classList.add('has-been-collapsed');
2283 + try {
2284 + sessionStorage.setItem('mxchat_questions_collapsed', 'true');
2285 + sessionStorage.setItem('mxchat_questions_has_been_collapsed', 'true');
2286 + } catch (e) {
2287 + // Ignore if sessionStorage is not available
2288 + }
2289 + }
2290 +}
2291 +
2292 +function expandQuickQuestions() {
2293 + const questionsContainer = document.getElementById('mxchat-popular-questions');
2294 + if (questionsContainer && hasQuickQuestions()) {
2295 + questionsContainer.classList.remove('collapsed');
2296 + try {
2297 + sessionStorage.setItem('mxchat_questions_collapsed', 'false');
2298 + } catch (e) {
2299 + // Ignore if sessionStorage is not available
2300 + }
2301 + }
2302 +}
2303 +
2304 +function checkQuickQuestionsState() {
2305 + if (!hasQuickQuestions()) {
2306 + return; // Don't do anything if no questions exist
2307 + }
2308 +
2309 + try {
2310 + const isCollapsed = sessionStorage.getItem('mxchat_questions_collapsed');
2311 + const hasBeenCollapsed = sessionStorage.getItem('mxchat_questions_has_been_collapsed');
2312 +
2313 + const questionsContainer = document.getElementById('mxchat-popular-questions');
2314 + if (questionsContainer) {
2315 + if (hasBeenCollapsed === 'true') {
2316 + questionsContainer.classList.add('has-been-collapsed');
2317 + }
2318 + if (isCollapsed === 'true') {
2319 + questionsContainer.classList.add('collapsed');
2320 + }
2321 + }
2322 + } catch (e) {
2323 + // Ignore if sessionStorage is not available
2324 + }
2325 +}
2326 +
2327 +
2328 +
2329 +
2330 +// ====================================
2331 +// MAIN INITIALIZATION
2332 +// ====================================
2333 +
2334 +if ($('#floating-chatbot').hasClass('hidden')) {
2335 + $('#floating-chatbot-button').removeClass('hidden');
2336 +}
2337 +// Initialize when document is ready
2338 +setFullHeight();
2339 +initializeChatVisibility();
2340 +loadChatHistory();
2341 +
2342 +// Make functions globally available for add-ons
2343 +window.hasQuickQuestions = hasQuickQuestions;
2344 +window.collapseQuickQuestions = collapseQuickQuestions;
2345 +window.appendMessage = appendMessage;
2346 +window.appendThinkingMessage = appendThinkingMessage;
2347 +window.scrollToBottom = scrollToBottom;
2348 +window.scrollElementToTop = scrollElementToTop;
2349 +window.replaceLastMessage = replaceLastMessage;
2350 +window.callMxChat = callMxChat;
2351 +window.callMxChatStream = callMxChatStream;
2352 +window.shouldUseStreaming = shouldUseStreaming;
2353 +window.getChatSession = getChatSession;
2354 +window.getPageContext = getPageContext;
2355 +window.updateStreamingMessage = updateStreamingMessage;
2356 +
2357 +}); // End of jQuery ready
2358 +
2359 +
2360 +// ====================================
2361 +// GLOBAL EVENT LISTENERS (Outside jQuery)
2362 +// ====================================
2363 +
2364 +// Event listener for copy button (code blocks)
2365 +document.addEventListener("click", (e) => {
2366 + if (e.target.classList.contains("mxchat-copy-button")) {
2367 + const copyButton = e.target;
2368 + const codeBlock = copyButton
2369 + .closest(".mxchat-code-block-container")
2370 + .querySelector(".mxchat-code-block code");
2371 +
2372 + if (codeBlock) {
2373 + // Preserve formatting using innerText
2374 + navigator.clipboard.writeText(codeBlock.innerText).then(() => {
2375 + copyButton.textContent = "Copied!";
2376 + copyButton.setAttribute("aria-label", "Copied to clipboard");
2377 +
2378 + setTimeout(() => {
2379 + copyButton.textContent = "Copy";
2380 + copyButton.setAttribute("aria-label", "Copy to clipboard");
2381 + }, 2000);
2382 + });
2383 + }
2384 + }
2385 +});
2386 +
2387 +
2388 +
2389 +
2390 +
2391 +