| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | jQuery(document).ready(function($) { |
| 2 | 2 | //console.log('mxchatChat object:', mxchatChat); |
| 3 | 3 | //console.log('Link Target Toggle Value:', mxchatChat.link_target_toggle); |
| 4 | -// Add these variables at the top of your chat-script.js file | |
| 4 | + | |
| 5 | 5 | const toolbarIconColor = mxchatChat.toolbar_icon_color || '#212121'; |
| 6 | 6 | |
| 7 | 7 | // Initialize color settings |
| 8 | 8 | var userMessageBgColor = mxchatChat.user_message_bg_color; |
| @@ -14,11 +14,9 @@ | ||
| 14 | 14 | var liveAgentMessageFontColor = mxchatChat.live_agent_message_font_color; |
| 15 | 15 | |
| 16 | 16 | |
| 17 | 17 | var linkTarget = mxchatChat.link_target_toggle === 'on' ? '_blank' : '_self'; |
| 18 | - let lastSeenMessageId = ''; | |
| 19 | - let notificationCheckInterval; | |
| 20 | - let notificationBadge; | |
| 18 | + var lastSeenMessageId = ''; | |
| 21 | 19 | // Initialize session ID |
| 22 | 20 | var sessionId = getChatSession(); |
| 23 | 21 | |
| 24 | 22 | let pollingInterval; // Variable to store the interval ID |
| @@ -28,107 +26,10 @@ | ||
| 28 | 26 | let activePdfFile = null; |
| 29 | 27 | let activeWordFile = null; |
| 30 | 28 | |
| 31 | 29 | |
| 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 | 30 | |
| 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 | 31 | |
| 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 | 32 | function getChatSession() { |
| 132 | 33 | var sessionId = getCookie('mxchat_session_id'); |
| 133 | 34 | //console.log("Session ID retrieved from cookie: ", sessionId); |
| 134 | 35 | |
| @@ -320,17 +221,19 @@ | ||
| 320 | 221 | return text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>'); |
| 321 | 222 | } |
| 322 | 223 | |
| 323 | 224 | // 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 | -} | |
| 225 | + function convertNewlinesToBreaks(text) { | |
| 226 | + var lines = text.split('\n'); | |
| 227 | + var formattedText = ''; | |
| 228 | + | |
| 229 | + for (var i = 0; i < lines.length; i++) { | |
| 230 | + formattedText += lines[i] + '<br>'; | |
| 231 | + } | |
| 232 | + | |
| 233 | + return formattedText; | |
| 234 | + } | |
| 235 | + | |
| 333 | 236 | // Copy to clipboard function |
| 334 | 237 | // Function to copy text to clipboard |
| 335 | 238 | function copyToClipboard(text) { |
| 336 | 239 | var tempInput = $('<input>'); |
| @@ -355,8 +258,10 @@ | ||
| 355 | 258 | } |
| 356 | 259 | } |
| 357 | 260 | |
| 358 | 261 | function callMxChat(message, callback) { |
| 262 | + //console.log("Sending message to chatbot:", message); | |
| 263 | + | |
| 359 | 264 | $.ajax({ |
| 360 | 265 | url: mxchatChat.ajax_url, |
| 361 | 266 | type: 'POST', |
| 362 | 267 | dataType: 'json', |
| @@ -366,37 +271,41 @@ | ||
| 366 | 271 | session_id: getChatSession(), |
| 367 | 272 | nonce: mxchatChat.nonce |
| 368 | 273 | }, |
| 369 | 274 | success: function(response) { |
| 370 | - // Existing chat mode check | |
| 275 | + //console.log("callMxChat response:", response); | |
| 276 | + | |
| 277 | + // Check for chat_mode in the response | |
| 371 | 278 | if (response.chat_mode) { |
| 372 | 279 | updateChatModeIndicator(response.chat_mode); |
| 373 | 280 | } |
| 281 | + // Also check in fallbackResponse if exists | |
| 374 | 282 | else if (response.fallbackResponse && response.fallbackResponse.chat_mode) { |
| 375 | 283 | updateChatModeIndicator(response.fallbackResponse.chat_mode); |
| 376 | 284 | } |
| 377 | - | |
| 378 | 285 | // Add PDF filename handling |
| 379 | 286 | if (response.data && response.data.filename) { |
| 380 | 287 | showActivePdf(response.data.filename); |
| 381 | 288 | activePdfFile = response.data.filename; |
| 382 | 289 | } |
| 383 | - | |
| 384 | - // Add redirect check here | |
| 290 | + // Add redirect check here | |
| 385 | 291 | if (response.redirect_url) { |
| 292 | + // Show the message first | |
| 386 | 293 | let responseText = response.text || ''; |
| 387 | 294 | if (responseText) { |
| 388 | 295 | replaceLastMessage("bot", responseText); |
| 389 | 296 | } |
| 297 | + // Then redirect after a short delay | |
| 390 | 298 | setTimeout(() => { |
| 391 | 299 | window.location.href = response.redirect_url; |
| 392 | 300 | }, 1500); |
| 393 | - return; | |
| 301 | + return; // Exit early since we're redirecting | |
| 394 | 302 | } |
| 395 | 303 | |
| 396 | 304 | // Check for live agent response |
| 397 | 305 | if (response.success && response.data && response.data.status === 'waiting_for_agent') { |
| 398 | 306 | updateChatModeIndicator('agent'); |
| 307 | + // Do not replace the thinking dots; just wait for the agent's actual response | |
| 399 | 308 | return; |
| 400 | 309 | } |
| 401 | 310 | |
| 402 | 311 | // Handle other responses |
| @@ -403,33 +312,31 @@ | ||
| 403 | 312 | let responseText = response.text || ''; |
| 404 | 313 | let responseHtml = response.html || ''; |
| 405 | 314 | let responseMessage = response.message || ''; |
| 406 | 315 | |
| 316 | + // Check for mode change in text response | |
| 407 | 317 | if (responseText === 'You are now chatting with the AI chatbot.') { |
| 408 | 318 | updateChatModeIndicator('ai'); |
| 409 | 319 | } |
| 410 | 320 | |
| 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 { | |
| 321 | + // For product card or chatbot responses with HTML | |
| 322 | + if (responseText && responseHtml) { | |
| 323 | + replaceLastMessage("bot", responseText, responseHtml); | |
| 324 | + } | |
| 325 | + // For regular chatbot responses with just text | |
| 326 | + else if (responseText) { | |
| 327 | + replaceLastMessage("bot", responseText); | |
| 328 | + } | |
| 329 | + // For responses with only HTML (like product cards) | |
| 330 | + else if (responseHtml) { | |
| 331 | + replaceLastMessage("bot", "", responseHtml); | |
| 332 | + } | |
| 333 | + // For legacy message format | |
| 334 | + else if (responseMessage) { | |
| 335 | + replaceLastMessage("bot", responseMessage); | |
| 336 | + } | |
| 337 | + // Fallback error case | |
| 338 | + else { | |
| 432 | 339 | console.error("Unexpected response format:", response); |
| 433 | 340 | replaceLastMessage("bot", "I'm sorry, something went wrong."); |
| 434 | 341 | } |
| 435 | 342 | |
| @@ -437,8 +344,9 @@ | ||
| 437 | 344 | lastSeenMessageId = response.message_id; |
| 438 | 345 | } |
| 439 | 346 | }, |
| 440 | 347 | error: function(xhr, status, error) { |
| 348 | + //console.log("Error communicating with the server:", xhr.status, error); | |
| 441 | 349 | replaceLastMessage("bot", "An unexpected error occurred."); |
| 442 | 350 | } |
| 443 | 351 | }); |
| 444 | 352 | } |
| @@ -463,20 +371,15 @@ | ||
| 463 | 371 | bgColor = botMessageBgColor; |
| 464 | 372 | fontColor = botMessageFontColor; |
| 465 | 373 | } |
| 466 | 374 | |
| 467 | - const messageDiv = $('<div>') | |
| 468 | - .addClass(messageClass) | |
| 469 | - .css({ | |
| 470 | - 'background': bgColor, | |
| 471 | - 'color': fontColor, | |
| 472 | - }); | |
| 375 | + const messageDiv = $('<div>') | |
| 376 | + .addClass(messageClass) | |
| 377 | + .css({ | |
| 378 | + 'background': bgColor, | |
| 379 | + 'color': fontColor, | |
| 380 | + }); | |
| 473 | 381 | |
| 474 | - // Add CSS for paragraphs | |
| 475 | - messageDiv.css({ | |
| 476 | - 'margin-bottom': '1em' | |
| 477 | - }); | |
| 478 | - | |
| 479 | 382 | // Format and process the message content |
| 480 | 383 | let fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(messageText)))); |
| 481 | 384 | |
| 482 | 385 | // Add images if provided |
| @@ -516,12 +419,12 @@ | ||
| 516 | 419 | } |
| 517 | 420 | } |
| 518 | 421 | }); |
| 519 | 422 | |
| 423 | + // Update the last seen message ID if applicable | |
| 520 | 424 | if (messageText.id) { |
| 521 | - lastSeenMessageId = messageText.id; | |
| 522 | - hideNotification(); | |
| 523 | - } | |
| 425 | + lastSeenMessageId = messageText.id; | |
| 426 | + } | |
| 524 | 427 | } catch (error) { |
| 525 | 428 | console.error("Error rendering message with images:", error); |
| 526 | 429 | } |
| 527 | 430 | } |
| @@ -574,17 +477,14 @@ | ||
| 574 | 477 | 'color': fontColor, |
| 575 | 478 | }) |
| 576 | 479 | .removeClass('temporary-message') |
| 577 | 480 | .fadeIn(200, function() { |
| 481 | + // After message is displayed, scroll last user message to top | |
| 578 | 482 | if (sender === "bot" || sender === "agent") { |
| 579 | 483 | const lastUserMessage = $('#chat-box').find('.user-message').last(); |
| 580 | 484 | if (lastUserMessage.length) { |
| 581 | 485 | scrollElementToTop(lastUserMessage); |
| 582 | 486 | } |
| 583 | - // Show notification if chat is hidden | |
| 584 | - if ($('#floating-chatbot').hasClass('hidden')) { | |
| 585 | - showNotification(); | |
| 586 | - } | |
| 587 | 487 | } |
| 588 | 488 | }); |
| 589 | 489 | }); |
| 590 | 490 | } else { |
| @@ -609,11 +509,11 @@ | ||
| 609 | 509 | } |
| 610 | 510 | } |
| 611 | 511 | |
| 612 | 512 | |
| 613 | -// Update your checkForAgentMessages function | |
| 614 | 513 | function checkForAgentMessages() { |
| 615 | 514 | const sessionId = getChatSession(); |
| 515 | + | |
| 616 | 516 | $.ajax({ |
| 617 | 517 | url: mxchatChat.ajax_url, |
| 618 | 518 | type: 'POST', |
| 619 | 519 | dataType: 'json', |
| @@ -623,24 +523,17 @@ | ||
| 623 | 523 | last_seen_id: lastSeenMessageId, |
| 624 | 524 | nonce: mxchatChat.nonce |
| 625 | 525 | }, |
| 626 | 526 | success: function (response) { |
| 527 | + //console.log("Agent messages polling response:", response); | |
| 627 | 528 | if (response.success && response.data?.new_messages) { |
| 628 | - let hasNewMessage = false; | |
| 629 | - | |
| 630 | 529 | response.data.new_messages.forEach(function (message) { |
| 631 | 530 | if (message.role === "agent" && !processedMessageIds.has(message.id)) { |
| 632 | - hasNewMessage = true; | |
| 633 | 531 | replaceLastMessage("agent", message.content); |
| 634 | 532 | lastSeenMessageId = message.id; |
| 635 | 533 | processedMessageIds.add(message.id); |
| 636 | 534 | } |
| 637 | 535 | }); |
| 638 | - | |
| 639 | - if (hasNewMessage && $('#floating-chatbot').hasClass('hidden')) { | |
| 640 | - showNotification(); | |
| 641 | - } | |
| 642 | - | |
| 643 | 536 | scrollToBottom(true); |
| 644 | 537 | } |
| 645 | 538 | }, |
| 646 | 539 | error: function (xhr, status, error) { |
| @@ -767,37 +660,29 @@ | ||
| 767 | 660 | return textArea.value; |
| 768 | 661 | } |
| 769 | 662 | |
| 770 | 663 | |
| 771 | -// Update formatCodeBlocks function | |
| 772 | 664 | 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 | - }); | |
| 665 | + // Ensure the input is a string; otherwise, convert or return empty | |
| 666 | + if (typeof text !== 'string') { | |
| 667 | + console.error("formatCodeBlocks: Input is not a string:", text); | |
| 668 | + return typeof text === 'object' && text.text ? text.text : ""; // Use .text if available, else empty | |
| 669 | + } | |
| 777 | 670 | |
| 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>`; | |
| 671 | + const codeBlockPattern = /```(\w+)?\n?([\s\S]+?)```/g; | |
| 672 | + | |
| 673 | + return text.replace(codeBlockPattern, (_, language, codeContent) => { | |
| 674 | + language = language || 'plaintext'; | |
| 675 | + | |
| 676 | + return ` | |
| 677 | + <div class="mxchat-code-block-container"> | |
| 678 | + <button class="mxchat-copy-button" aria-label="Copy to clipboard">Copy</button> | |
| 679 | + <pre class="mxchat-code-block"><code class="mxchat-language-${language}">${escapeHtml(codeContent)}</code></pre> | |
| 680 | + </div>`; | |
| 781 | 681 | }); |
| 682 | +} | |
| 782 | 683 | |
| 783 | - return text; | |
| 784 | -} | |
| 785 | 684 | |
| 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, "&") | |
| 795 | - .replace(/</g, "<") | |
| 796 | - .replace(/>/g, ">") | |
| 797 | - .replace(/"/g, """) | |
| 798 | - .replace(/'/g, "'"); | |
| 799 | -} | |
| 800 | 685 | // Utility function to escape HTML |
| 801 | 686 | function escapeHtml(unsafe) { |
| 802 | 687 | return unsafe |
| 803 | 688 | .replace(/&/g, "&") |
| @@ -811,18 +696,20 @@ | ||
| 811 | 696 | |
| 812 | 697 | |
| 813 | 698 | // Function to convert newlines, skipping preformatted text |
| 814 | 699 | 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(''); | |
| 700 | + // Regex to exclude <pre> and <code> tags from adding <br> tags | |
| 701 | + return text.replace(/(^|[^>])\n/g, '$1<br>'); | |
| 820 | 702 | } |
| 821 | 703 | |
| 822 | 704 | |
| 823 | 705 | |
| 706 | +$(document).ready(function() { | |
| 707 | + loadChatHistory(); | |
| 708 | +}); | |
| 824 | 709 | |
| 710 | + | |
| 711 | + | |
| 825 | 712 | // Helper function to check if a string is an image HTML |
| 826 | 713 | function isImageHtml(str) { |
| 827 | 714 | return str.startsWith('<img') && str.endsWith('>'); |
| 828 | 715 | } |
| @@ -848,8 +735,20 @@ | ||
| 848 | 735 | $('body').css('overflow', ''); |
| 849 | 736 | } |
| 850 | 737 | } |
| 851 | 738 | |
| 739 | + // Function to show the chatbot widget (moved outside the Complianz logic) | |
| 740 | + function showChatWidget() { | |
| 741 | + setTimeout(function() { | |
| 742 | + $('#floating-chatbot-button').css('display', 'flex').fadeTo(500, 1); | |
| 743 | + }, 250); | |
| 744 | + } | |
| 745 | + | |
| 746 | + // Function to hide the chatbot widget | |
| 747 | + function hideChatWidget() { | |
| 748 | + $('#floating-chatbot-button').css('display', 'none'); | |
| 749 | + } | |
| 750 | + | |
| 852 | 751 | // Pre-chat dismissal check function (wrapped in a function for reuse) |
| 853 | 752 | function checkPreChatDismissal() { |
| 854 | 753 | $.ajax({ |
| 855 | 754 | url: mxchatChat.ajax_url, |
| @@ -869,93 +768,9 @@ | ||
| 869 | 768 | console.error('Failed to check pre-chat message dismissal status.'); |
| 870 | 769 | } |
| 871 | 770 | }); |
| 872 | 771 | } |
| 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 | 772 | |
| 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 | 773 | // Function to dismiss pre-chat message for 24 hours |
| 959 | 774 | function handlePreChatDismissal() { |
| 960 | 775 | $('#pre-chat-message').fadeOut(200); |
| 961 | 776 | $.ajax({ |
| @@ -979,8 +794,47 @@ | ||
| 979 | 794 | e.stopPropagation(); |
| 980 | 795 | handlePreChatDismissal(); |
| 981 | 796 | }); |
| 982 | 797 | |
| 798 | + // Function for Complianz logic | |
| 799 | + var applyComplianzLogic = mxchatChat.complianz_toggle; | |
| 800 | + if (applyComplianzLogic) { | |
| 801 | + function checkConsentAndShowChat() { | |
| 802 | + var consentStatus = typeof cmplz_has_consent === "function" && cmplz_has_consent('marketing'); | |
| 803 | + var consentType = typeof complianz !== 'undefined' ? complianz.consenttype : null; | |
| 804 | + | |
| 805 | + // Show the chatbot by default | |
| 806 | + showChatWidget(); | |
| 807 | + | |
| 808 | + if (consentType === 'optin' && !consentStatus) { | |
| 809 | + // For opt-in, hide only if user explicitly denies consent | |
| 810 | + hideChatWidget(); | |
| 811 | + } else if (consentType === 'optout' && consentStatus === false) { | |
| 812 | + // For opt-out, hide only if user explicitly denies consent | |
| 813 | + hideChatWidget(); | |
| 814 | + } else { | |
| 815 | + // Keep showing the chatbot | |
| 816 | + showChatWidget(); | |
| 817 | + } | |
| 818 | + | |
| 819 | + checkPreChatDismissal(); // Ensure we check dismissal after consent is handled | |
| 820 | + } | |
| 821 | + | |
| 822 | + // Initial check when the page loads | |
| 823 | + checkConsentAndShowChat(); | |
| 824 | + | |
| 825 | + // Listen for changes in consent status | |
| 826 | + $(document).on('cmplz_status_change', function(event, category) { | |
| 827 | + if (category === 'marketing') { | |
| 828 | + checkConsentAndShowChat(); | |
| 829 | + } | |
| 830 | + }); | |
| 831 | + } else { | |
| 832 | + // If Complianz is not toggled on, always show the chatbot | |
| 833 | + showChatWidget(); | |
| 834 | + checkPreChatDismissal(); // Always check pre-chat dismissal when consent logic is not applied | |
| 835 | + } | |
| 836 | + | |
| 983 | 837 | // Toggle chatbot visibility on floating button click |
| 984 | 838 | $(document).on('click', '#floating-chatbot-button', function() { |
| 985 | 839 | var chatbot = $('#floating-chatbot'); |
| 986 | 840 | if (chatbot.hasClass('hidden')) { |
| @@ -985,15 +839,16 @@ | ||
| 985 | 839 | var chatbot = $('#floating-chatbot'); |
| 986 | 840 | if (chatbot.hasClass('hidden')) { |
| 987 | 841 | chatbot.removeClass('hidden').addClass('visible'); |
| 988 | 842 | $(this).addClass('hidden'); |
| 989 | - $('#chat-notification-badge').hide(); // Hide notification when opening chat | |
| 990 | 843 | disableScroll(); |
| 844 | + // Hide the pre-chat message without dismissing it | |
| 991 | 845 | $('#pre-chat-message').fadeOut(250); |
| 992 | 846 | } else { |
| 993 | 847 | chatbot.removeClass('visible').addClass('hidden'); |
| 994 | 848 | $(this).removeClass('hidden'); |
| 995 | 849 | enableScroll(); |
| 850 | + // Show the pre-chat message again if it hasn't been dismissed | |
| 996 | 851 | checkPreChatDismissal(); |
| 997 | 852 | } |
| 998 | 853 | }); |
| 999 | 854 | |
| @@ -1032,10 +887,12 @@ | ||
| 1032 | 887 | $(':root').css('--vh', vh + 'px'); |
| 1033 | 888 | } |
| 1034 | 889 | |
| 1035 | 890 | // Set the height when the page loads |
| 891 | + $(document).ready(function() { | |
| 892 | + setFullHeight(); | |
| 893 | + }); | |
| 1036 | 894 | |
| 1037 | - | |
| 1038 | 895 | // Set the height on resize and orientation change events |
| 1039 | 896 | $(window).on('resize orientationchange', function() { |
| 1040 | 897 | setFullHeight(); |
| 1041 | 898 | }); |
| @@ -1072,27 +929,30 @@ | ||
| 1072 | 929 | |
| 1073 | 930 | |
| 1074 | 931 | // Event listener for Add to Cart button |
| 1075 | 932 | $(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 | |
| 933 | + var productId = $(this).data('product-id'); // Get product ID from data attribute | |
| 934 | + | |
| 935 | + // Simulate user message first for proper ordering | |
| 936 | + appendMessage("user", "add to cart"); // Display the user's "add to cart" message first | |
| 937 | + | |
| 938 | + | |
| 939 | + // Use existing function to send the "add to cart" command to the chatbot | |
| 940 | + sendMessageToChatbot("add to cart"); // Triggers the chatbot response as though user typed it | |
| 1080 | 941 | }); |
| 1081 | 942 | |
| 1082 | 943 | |
| 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 | 944 | |
| 1089 | -if (document.getElementById('word-upload-btn')) { | |
| 1090 | - document.getElementById('word-upload-btn').addEventListener('click', function() { | |
| 1091 | - document.getElementById('word-upload').click(); | |
| 1092 | - }); | |
| 1093 | -} | |
| 945 | +// PDF Upload button click handler | |
| 946 | +document.getElementById('pdf-upload-btn').addEventListener('click', function() { | |
| 947 | + document.getElementById('pdf-upload').click(); | |
| 948 | +}); | |
| 1094 | 949 | |
| 950 | +// Word Upload button click handler | |
| 951 | +document.getElementById('word-upload-btn').addEventListener('click', function() { | |
| 952 | + document.getElementById('word-upload').click(); | |
| 953 | +}); | |
| 954 | + | |
| 1095 | 955 | // PDF file input change handler |
| 1096 | 956 | document.getElementById('pdf-upload').addEventListener('change', async function(e) { |
| 1097 | 957 | const file = e.target.files[0]; |
| 1098 | 958 | |
| @@ -1416,122 +1276,10 @@ | ||
| 1416 | 1276 | }); |
| 1417 | 1277 | |
| 1418 | 1278 | |
| 1419 | 1279 | |
| 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 | 1280 | }); |
| 1531 | 1281 | |
| 1532 | -}); | |
| 1533 | - | |
| 1534 | 1282 | // Event listener for copy button |
| 1535 | 1283 | document.addEventListener("click", (e) => { |
| 1536 | 1284 | if (e.target.classList.contains("mxchat-copy-button")) { |
| 1537 | 1285 | const copyButton = e.target; |
| @@ -1552,7 +1300,4 @@ | ||
| 1552 | 1300 | }); |
| 1553 | 1301 | } |
| 1554 | 1302 | } |
| 1555 | 1303 | }); |
| 1556 | - | |
| 1557 | - | |
| 1558 | - | |