| @@ -1,23 +1,6 @@ | ||
| 1 | 1 | jQuery(document).ready(function($) { |
| 2 | 2 | |
| 3 | - // Nonce refresh is deferred until first user interaction (ensureSession) | |
| 4 | - // to avoid admin-ajax calls on passive page loads. | |
| 5 | - var nonceRefreshed = false; | |
| 6 | - function refreshNonceIfNeeded(callback) { | |
| 7 | - if (nonceRefreshed || typeof mxchatChat === 'undefined' || !mxchatChat.ajax_url) { | |
| 8 | - if (callback) callback(); | |
| 9 | - return; | |
| 10 | - } | |
| 11 | - nonceRefreshed = true; | |
| 12 | - $.post(mxchatChat.ajax_url, { action: 'mxchat_refresh_nonce' }, function(res) { | |
| 13 | - if (res && res.success && res.data && res.data.nonce) { | |
| 14 | - mxchatChat.nonce = res.data.nonce; | |
| 15 | - } | |
| 16 | - if (callback) callback(); | |
| 17 | - }); | |
| 18 | - } | |
| 19 | - | |
| 20 | 3 | // ==================================== |
| 21 | 4 | // MULTI-INSTANCE MANAGEMENT SYSTEM |
| 22 | 5 | // ==================================== |
| 23 | 6 | |
| @@ -27,15 +10,11 @@ | ||
| 27 | 10 | |
| 28 | 11 | // Initialize an instance for a bot |
| 29 | 12 | init: function(botId) { |
| 30 | 13 | if (!this.instances[botId]) { |
| 31 | - // When persistence is OFF, track when this session started | |
| 32 | - // so the AI only sees messages from this page load | |
| 33 | - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on'; | |
| 34 | - | |
| 35 | 14 | this.instances[botId] = { |
| 36 | 15 | botId: botId, |
| 37 | - sessionId: null, | |
| 16 | + sessionId: this.getChatSession(botId), | |
| 38 | 17 | lastSeenMessageId: '', |
| 39 | 18 | notificationCheckInterval: null, |
| 40 | 19 | pollingInterval: null, |
| 41 | 20 | processedMessageIds: new Set(), |
| @@ -41,11 +20,9 @@ | ||
| 41 | 20 | processedMessageIds: new Set(), |
| 42 | 21 | activePdfFile: null, |
| 43 | 22 | activeWordFile: null, |
| 44 | 23 | chatHistoryLoaded: false, |
| 45 | - isStreaming: false, | |
| 46 | - // Fresh context timestamp - only used when persistence is OFF | |
| 47 | - sessionStartTimestamp: chatPersistenceEnabled ? 0 : Date.now() | |
| 24 | + isStreaming: false | |
| 48 | 25 | }; |
| 49 | 26 | } |
| 50 | 27 | return this.instances[botId]; |
| 51 | 28 | }, |
| @@ -60,77 +37,23 @@ | ||
| 60 | 37 | return Object.keys(this.instances); |
| 61 | 38 | }, |
| 62 | 39 | |
| 63 | 40 | // Session management per bot |
| 64 | - // Returns existing session ID from cookie or localStorage (with in-memory fallback), | |
| 65 | - // or null if none exists. Does NOT create a new session — use ensureSession() for that. | |
| 66 | 41 | getChatSession: function(botId) { |
| 67 | 42 | var cookieName = 'mxchat_session_id_' + botId; |
| 68 | - var storageKey = 'mxchat_session_id_' + botId; | |
| 69 | 43 | var sessionId = getCookie(cookieName); |
| 70 | 44 | |
| 71 | - // Fallback to localStorage if cookie is missing (e.g. cleared by browser/consent) | |
| 72 | 45 | if (!sessionId) { |
| 73 | - try { sessionId = localStorage.getItem(storageKey); } catch (e) {} | |
| 46 | + sessionId = generateSessionId(); | |
| 47 | + this.setChatSession(botId, sessionId); | |
| 74 | 48 | } |
| 75 | 49 | |
| 76 | - // Fallback to in-memory instance when cookie AND localStorage are both blocked | |
| 77 | - // (Safari ITP, strict tracking prevention, cross-origin iframes with partitioned | |
| 78 | - // storage). Without this, ensureSession() can generate and store an ID that | |
| 79 | - // getChatSession() then can't read back, causing null session_ids on send. | |
| 80 | - if (!sessionId && this.instances[botId] && this.instances[botId].sessionId) { | |
| 81 | - sessionId = this.instances[botId].sessionId; | |
| 82 | - } | |
| 83 | - | |
| 84 | - // Guard against stored sentinel values that indicate earlier broken writes. | |
| 85 | - if (sessionId === 'null' || sessionId === 'undefined') { | |
| 86 | - sessionId = null; | |
| 87 | - } | |
| 88 | - | |
| 89 | - // Re-sync cookie from localStorage if cookie was lost | |
| 90 | - if (sessionId && !getCookie(cookieName)) { | |
| 91 | - document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax"; | |
| 92 | - } | |
| 93 | - | |
| 94 | - return sessionId || null; | |
| 50 | + return sessionId; | |
| 95 | 51 | }, |
| 96 | 52 | |
| 97 | - // Lazy session initializer — called on first user interaction | |
| 98 | - ensureSession: function(botId) { | |
| 99 | - botId = botId || 'default'; | |
| 100 | - var instance = this.instances[botId] || this.init(botId); | |
| 101 | - | |
| 102 | - if (instance.sessionId) { | |
| 103 | - return instance.sessionId; | |
| 104 | - } | |
| 105 | - | |
| 106 | - // Check for existing session from cookie or localStorage | |
| 107 | - var existingSession = this.getChatSession(botId); | |
| 108 | - | |
| 109 | - if (existingSession) { | |
| 110 | - instance.sessionId = existingSession; | |
| 111 | - } else { | |
| 112 | - // Brand new session | |
| 113 | - var newId = generateSessionId(); | |
| 114 | - this.setChatSession(botId, newId); | |
| 115 | - instance.sessionId = newId; | |
| 116 | - } | |
| 117 | - | |
| 118 | - // Now that we have a session, do the deferred work | |
| 119 | - refreshNonceIfNeeded(); | |
| 120 | - trackOriginatingPage(); | |
| 121 | - | |
| 122 | - // Note: loadChatHistory is handled by showChatContainerForBot with loader UI, | |
| 123 | - // so we do NOT call it here to avoid a race condition. | |
| 124 | - | |
| 125 | - return instance.sessionId; | |
| 126 | - }, | |
| 127 | - | |
| 128 | 53 | setChatSession: function(botId, sessionId) { |
| 129 | 54 | var cookieName = 'mxchat_session_id_' + botId; |
| 130 | - var storageKey = 'mxchat_session_id_' + botId; | |
| 131 | 55 | document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax"; |
| 132 | - try { localStorage.setItem(storageKey, sessionId); } catch (e) {} | |
| 133 | 56 | if (this.instances[botId]) { |
| 134 | 57 | this.instances[botId].sessionId = sessionId; |
| 135 | 58 | } |
| 136 | 59 | }, |
| @@ -135,10 +58,8 @@ | ||
| 135 | 58 | } |
| 136 | 59 | }, |
| 137 | 60 | |
| 138 | 61 | resetChatSession: function(botId) { |
| 139 | - // Clear old session from localStorage before setting new one | |
| 140 | - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {} | |
| 141 | 62 | var newSessionId = generateSessionId(); |
| 142 | 63 | this.setChatSession(botId, newSessionId); |
| 143 | 64 | var $chatBox = getElement(botId, 'chat-box'); |
| 144 | 65 | if ($chatBox.length) { |
| @@ -147,20 +68,8 @@ | ||
| 147 | 68 | if (this.instances[botId]) { |
| 148 | 69 | this.instances[botId].chatHistoryLoaded = false; |
| 149 | 70 | this.instances[botId].processedMessageIds = new Set(); |
| 150 | 71 | } |
| 151 | - }, | |
| 152 | - | |
| 153 | - // Silent reset — new session ID without clearing the chat UI | |
| 154 | - // Used when IP changes mid-conversation so the user doesn't see messages vanish | |
| 155 | - silentResetSession: function(botId) { | |
| 156 | - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {} | |
| 157 | - var newSessionId = generateSessionId(); | |
| 158 | - this.setChatSession(botId, newSessionId); | |
| 159 | - if (this.instances[botId]) { | |
| 160 | - this.instances[botId].sessionId = newSessionId; | |
| 161 | - } | |
| 162 | - return newSessionId; | |
| 163 | 72 | } |
| 164 | 73 | }; |
| 165 | 74 | |
| 166 | 75 | // ==================================== |
| @@ -465,9 +374,8 @@ | ||
| 465 | 374 | |
| 466 | 375 | // Update your existing sendMessage function |
| 467 | 376 | function sendMessage(botId) { |
| 468 | 377 | botId = botId || 'default'; |
| 469 | - MxChatInstances.ensureSession(botId); | |
| 470 | 378 | var $chatInput = getElement(botId, 'chat-input'); |
| 471 | 379 | var message = $chatInput.val(); |
| 472 | 380 | |
| 473 | 381 | // ADD PROMPT HOOK HERE |
| @@ -475,14 +383,10 @@ | ||
| 475 | 383 | message = customMxChatFilter(message, "prompt"); |
| 476 | 384 | } |
| 477 | 385 | |
| 478 | 386 | if (message) { |
| 479 | - // Don't disable input in live agent mode - let users chat freely | |
| 480 | - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator'); | |
| 481 | - var isAgentMode = modeIndicator && modeIndicator.textContent === 'Live Agent'; | |
| 482 | - if (!isAgentMode) { | |
| 483 | - disableChatInput(botId); | |
| 484 | - } | |
| 387 | + // Disable input while waiting for response | |
| 388 | + disableChatInput(botId); | |
| 485 | 389 | |
| 486 | 390 | appendMessage("user", message, '', [], false, botId); |
| 487 | 391 | $chatInput.val(''); |
| 488 | 392 | $chatInput.css('height', 'auto'); |
| @@ -492,9 +396,9 @@ | ||
| 492 | 396 | } |
| 493 | 397 | appendThinkingMessage(botId); |
| 494 | 398 | scrollToBottom(botId); |
| 495 | 399 | |
| 496 | - const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest'; | |
| 400 | + const currentModel = mxchatChat.model || 'gpt-4o'; | |
| 497 | 401 | |
| 498 | 402 | // Check if streaming is enabled AND supported for this model |
| 499 | 403 | if (shouldUseStreaming(currentModel)) { |
| 500 | 404 | callMxChatStream(message, function(response) { |
| @@ -510,9 +414,8 @@ | ||
| 510 | 414 | |
| 511 | 415 | // Update your existing sendMessageToChatbot function |
| 512 | 416 | function sendMessageToChatbot(message, botId) { |
| 513 | 417 | botId = botId || 'default'; |
| 514 | - MxChatInstances.ensureSession(botId); | |
| 515 | 418 | |
| 516 | 419 | // ADD PROMPT HOOK HERE |
| 517 | 420 | if (typeof customMxChatFilter === 'function') { |
| 518 | 421 | message = customMxChatFilter(message, "prompt"); |
| @@ -517,14 +420,10 @@ | ||
| 517 | 420 | if (typeof customMxChatFilter === 'function') { |
| 518 | 421 | message = customMxChatFilter(message, "prompt"); |
| 519 | 422 | } |
| 520 | 423 | |
| 521 | - // Don't disable input in live agent mode - let users chat freely | |
| 522 | - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator'); | |
| 523 | - var isAgentMode = modeIndicator && modeIndicator.textContent === 'Live Agent'; | |
| 524 | - if (!isAgentMode) { | |
| 525 | - disableChatInput(botId); | |
| 526 | - } | |
| 424 | + // Disable input while waiting for response | |
| 425 | + disableChatInput(botId); | |
| 527 | 426 | |
| 528 | 427 | var sessionId = getChatSession(botId); |
| 529 | 428 | |
| 530 | 429 | if (hasQuickQuestions(botId)) { |
| @@ -532,9 +431,9 @@ | ||
| 532 | 431 | } |
| 533 | 432 | appendThinkingMessage(botId); |
| 534 | 433 | scrollToBottom(botId); |
| 535 | 434 | |
| 536 | - const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest'; | |
| 435 | + const currentModel = mxchatChat.model || 'gpt-4o'; | |
| 537 | 436 | |
| 538 | 437 | // Check if streaming is enabled AND supported for this model |
| 539 | 438 | if (shouldUseStreaming(currentModel)) { |
| 540 | 439 | callMxChatStream(message, function(response) { |
| @@ -609,32 +508,17 @@ | ||
| 609 | 508 | |
| 610 | 509 | // Get page context if contextual awareness is enabled |
| 611 | 510 | const pageContext = getPageContext(); |
| 612 | 511 | |
| 613 | - // Get instance for session start timestamp (used when persistence is OFF) | |
| 614 | - var instance = MxChatInstances.get(botId); | |
| 615 | - | |
| 616 | - // Guarantee a non-null session_id before the AJAX leaves. ensureSession() is idempotent | |
| 617 | - // and returns the guaranteed-present session id from the in-memory instance even when | |
| 618 | - // cookie/localStorage writes are silently blocked by the browser. | |
| 619 | - var sessionId = MxChatInstances.ensureSession(botId); | |
| 620 | - if (!sessionId || sessionId === 'null' || sessionId === 'undefined') { | |
| 621 | - // Last-resort generation to ensure we never POST a null marker. | |
| 622 | - sessionId = generateSessionId(); | |
| 623 | - MxChatInstances.setChatSession(botId, sessionId); | |
| 624 | - } | |
| 625 | - | |
| 626 | 512 | // Prepare AJAX data |
| 627 | 513 | const ajaxData = { |
| 628 | 514 | action: 'mxchat_handle_chat_request', |
| 629 | 515 | message: message, |
| 630 | - session_id: sessionId, | |
| 516 | + session_id: getChatSession(botId), | |
| 631 | 517 | nonce: mxchatChat.nonce, |
| 632 | 518 | current_page_url: window.location.href, |
| 633 | 519 | current_page_title: document.title, |
| 634 | - bot_id: botId, | |
| 635 | - // Pass session start timestamp so AI context matches what user sees | |
| 636 | - session_start_timestamp: instance.sessionStartTimestamp || 0 | |
| 520 | + bot_id: botId | |
| 637 | 521 | }; |
| 638 | 522 | |
| 639 | 523 | // Add page context if available |
| 640 | 524 | if (pageContext) { |
| @@ -690,16 +574,23 @@ | ||
| 690 | 574 | errorMessage = "An error occurred. Please try again or contact support."; |
| 691 | 575 | } |
| 692 | 576 | |
| 693 | 577 | // Handle session reset action (IP changed, session expired, etc.) |
| 694 | - // Silent reset — keep chat UI intact, just get a new session and retry | |
| 695 | 578 | if (response.data && response.data.action === 'reset_session') { |
| 696 | - MxChatInstances.silentResetSession(botId); | |
| 697 | - // Re-send the original message with the new session (user message is already displayed) | |
| 579 | + // Clear the old session and generate a new one | |
| 580 | + resetChatSession(botId); | |
| 581 | + // Remove the temporary loading message | |
| 582 | + getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove(); | |
| 583 | + // Re-send the original message with the new session | |
| 698 | 584 | var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message'); |
| 699 | 585 | if (originalMessage) { |
| 700 | 586 | getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null); |
| 701 | - var currentModel = mxchatChat.model || 'gpt-5.1-chat-latest'; | |
| 587 | + // Re-add the user message and thinking indicator | |
| 588 | + appendMessage("user", originalMessage, '', [], false, botId); | |
| 589 | + appendThinkingMessage(botId); | |
| 590 | + scrollToBottom(botId); | |
| 591 | + // Determine whether to use streaming | |
| 592 | + const currentModel = mxchatChat.model || 'gpt-4o'; | |
| 702 | 593 | if (shouldUseStreaming(currentModel)) { |
| 703 | 594 | callMxChatStream(originalMessage, function(response) { |
| 704 | 595 | getElement(botId, 'chat-box').find('.bot-message.temporary-message').removeClass('temporary-message'); |
| 705 | 596 | }, botId); |
| @@ -756,11 +647,9 @@ | ||
| 756 | 647 | } |
| 757 | 648 | |
| 758 | 649 | // Check for live agent response |
| 759 | 650 | if (response.success && response.data && response.data.status === 'waiting_for_agent') { |
| 760 | - removeThinkingDots(botId); | |
| 761 | 651 | updateChatModeIndicator('agent', botId); |
| 762 | - enableChatInput(botId); | |
| 763 | 652 | return; |
| 764 | 653 | } |
| 765 | 654 | |
| 766 | 655 | // Handle the message and show notification if chat is hidden |
| @@ -793,13 +682,9 @@ | ||
| 793 | 682 | $badge.show(); |
| 794 | 683 | } |
| 795 | 684 | } |
| 796 | 685 | } else { |
| 797 | - var emptyMsg = "I received an empty response. Please try again or contact support if this persists."; | |
| 798 | - if (response.vectorstore_error) { | |
| 799 | - emptyMsg = "I received an empty response. Debug info: " + response.vectorstore_error; | |
| 800 | - } | |
| 801 | - replaceLastMessage("bot", emptyMsg, '', [], botId); | |
| 686 | + replaceLastMessage("bot", "I received an empty response. Please try again or contact support if this persists.", '', [], botId); | |
| 802 | 687 | } |
| 803 | 688 | |
| 804 | 689 | if (response.message_id) { |
| 805 | 690 | var instance = MxChatInstances.get(botId); |
| @@ -849,9 +734,9 @@ | ||
| 849 | 734 | |
| 850 | 735 | // Store the message in case we need to retry after session reset |
| 851 | 736 | getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', message); |
| 852 | 737 | |
| 853 | - const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest'; | |
| 738 | + const currentModel = mxchatChat.model || 'gpt-4o'; | |
| 854 | 739 | if (!isStreamingSupported(currentModel)) { |
| 855 | 740 | callMxChat(message, callback, botId); |
| 856 | 741 | return; |
| 857 | 742 | } |
| @@ -858,32 +743,17 @@ | ||
| 858 | 743 | |
| 859 | 744 | // Get page context if contextual awareness is enabled |
| 860 | 745 | const pageContext = getPageContext(); |
| 861 | 746 | |
| 862 | - // Get instance for session start timestamp (used when persistence is OFF) | |
| 863 | - var instance = MxChatInstances.get(botId); | |
| 864 | - | |
| 865 | - // Guarantee a non-null session_id before the fetch. FormData.append() stringifies any | |
| 866 | - // non-string value via String(), so passing `null` would POST the literal string "null" | |
| 867 | - // and land in the transcripts table as a ghost session. ensureSession() always returns | |
| 868 | - // a real string even when cookies/localStorage are blocked. | |
| 869 | - var streamSessionId = MxChatInstances.ensureSession(botId); | |
| 870 | - if (!streamSessionId || streamSessionId === 'null' || streamSessionId === 'undefined') { | |
| 871 | - streamSessionId = generateSessionId(); | |
| 872 | - MxChatInstances.setChatSession(botId, streamSessionId); | |
| 873 | - } | |
| 874 | - | |
| 875 | 747 | const formData = new FormData(); |
| 876 | 748 | formData.append('action', 'mxchat_stream_chat'); |
| 877 | 749 | formData.append('message', message); |
| 878 | - formData.append('session_id', streamSessionId); | |
| 750 | + formData.append('session_id', getChatSession(botId)); | |
| 879 | 751 | formData.append('nonce', mxchatChat.nonce); |
| 880 | 752 | formData.append('current_page_url', window.location.href); |
| 881 | 753 | formData.append('current_page_title', document.title); |
| 882 | 754 | formData.append('bot_id', botId); |
| 883 | - // Pass session start timestamp so AI context matches what user sees | |
| 884 | - formData.append('session_start_timestamp', instance.sessionStartTimestamp || 0); | |
| 885 | - | |
| 755 | + | |
| 886 | 756 | // Add page context if available |
| 887 | 757 | if (pageContext) { |
| 888 | 758 | formData.append('page_context', JSON.stringify(pageContext)); |
| 889 | 759 | } |
| @@ -973,19 +843,8 @@ | ||
| 973 | 843 | }); |
| 974 | 844 | return; |
| 975 | 845 | } |
| 976 | 846 | |
| 977 | - // Re-enable chat input when stream ends with content | |
| 978 | - enableChatInput(botId); | |
| 979 | - | |
| 980 | - // Scroll the user's last message to the top now that the | |
| 981 | - // bot's full reply has rendered (gives max reading room). | |
| 982 | - var $chatBoxDone = getElement(botId, 'chat-box'); | |
| 983 | - var $lastUserMsgDone = $chatBoxDone.find('.user-message').last(); | |
| 984 | - if ($lastUserMsgDone.length) { | |
| 985 | - scrollElementToTop($lastUserMsgDone, botId); | |
| 986 | - } | |
| 987 | - | |
| 988 | 847 | if (callback) { |
| 989 | 848 | callback(accumulatedContent); |
| 990 | 849 | } |
| 991 | 850 | return; |
| @@ -1008,16 +867,8 @@ | ||
| 1008 | 867 | |
| 1009 | 868 | // Re-enable chat input after streaming completes |
| 1010 | 869 | enableChatInput(botId); |
| 1011 | 870 | |
| 1012 | - // Scroll the user's last message to the top now | |
| 1013 | - // that the bot's full reply has rendered. | |
| 1014 | - var $chatBoxStreamDone = getElement(botId, 'chat-box'); | |
| 1015 | - var $lastUserMsgStreamDone = $chatBoxStreamDone.find('.user-message').last(); | |
| 1016 | - if ($lastUserMsgStreamDone.length) { | |
| 1017 | - scrollElementToTop($lastUserMsgStreamDone, botId); | |
| 1018 | - } | |
| 1019 | - | |
| 1020 | 871 | if (callback) { |
| 1021 | 872 | callback(accumulatedContent); |
| 1022 | 873 | } |
| 1023 | 874 | return; |
| @@ -1136,16 +987,21 @@ | ||
| 1136 | 987 | errorMessage = "An error occurred. Please try again or contact support."; |
| 1137 | 988 | } |
| 1138 | 989 | |
| 1139 | 990 | // Handle session reset action (IP changed, session expired, etc.) |
| 1140 | - // Silent reset — keep chat UI intact, just get a new session and retry | |
| 1141 | 991 | if (data.data && data.data.action === 'reset_session') { |
| 1142 | - MxChatInstances.silentResetSession(botId); | |
| 1143 | - // Re-send the original message with the new session (user message is already displayed) | |
| 992 | + // Clear the old session and generate a new one | |
| 993 | + resetChatSession(botId); | |
| 994 | + // Re-send the original message with the new session | |
| 1144 | 995 | var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message'); |
| 1145 | 996 | if (originalMessage) { |
| 1146 | 997 | getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null); |
| 1147 | - var currentModel = mxchatChat.model || 'gpt-5.1-chat-latest'; | |
| 998 | + // Re-add the user message and thinking indicator | |
| 999 | + appendMessage("user", originalMessage, '', [], false, botId); | |
| 1000 | + appendThinkingMessage(botId); | |
| 1001 | + scrollToBottom(botId); | |
| 1002 | + // Determine whether to use streaming | |
| 1003 | + const currentModel = mxchatChat.model || 'gpt-4o'; | |
| 1148 | 1004 | if (shouldUseStreaming(currentModel)) { |
| 1149 | 1005 | callMxChatStream(originalMessage, callback, botId); |
| 1150 | 1006 | } else { |
| 1151 | 1007 | callMxChat(originalMessage, callback, botId); |
| @@ -1167,22 +1023,8 @@ | ||
| 1167 | 1023 | } |
| 1168 | 1024 | return; // Exit early for errors |
| 1169 | 1025 | } |
| 1170 | 1026 | |
| 1171 | - // Check for live agent response | |
| 1172 | - if (data.success && data.data && data.data.status === 'waiting_for_agent') { | |
| 1173 | - removeThinkingDots(botId); | |
| 1174 | - // Also remove any leftover bot-message that lost its temporary-message class | |
| 1175 | - var $chatBox = getElement(botId, 'chat-box'); | |
| 1176 | - $chatBox.find('.bot-message .thinking-dots').closest('.bot-message').remove(); | |
| 1177 | - updateChatModeIndicator('agent', botId); | |
| 1178 | - enableChatInput(botId); | |
| 1179 | - if (callback) { | |
| 1180 | - callback(''); | |
| 1181 | - } | |
| 1182 | - return; | |
| 1183 | - } | |
| 1184 | - | |
| 1185 | 1027 | // Handle different response formats |
| 1186 | 1028 | if (data.text || data.html || data.message) { |
| 1187 | 1029 | |
| 1188 | 1030 | // Apply response hooks |
| @@ -1227,15 +1069,19 @@ | ||
| 1227 | 1069 | } |
| 1228 | 1070 | |
| 1229 | 1071 | // Enhanced updateChatModeIndicator function for immediate DOM updates |
| 1230 | 1072 | function updateChatModeIndicator(mode, botId) { |
| 1073 | + console.log('[MxChat] updateChatModeIndicator called with mode:', mode, 'botId:', botId); | |
| 1231 | 1074 | botId = botId || 'default'; |
| 1232 | 1075 | const indicator = getElementDOM(botId, 'chat-mode-indicator'); |
| 1076 | + console.log('[MxChat] chat-mode-indicator element found:', !!indicator); | |
| 1233 | 1077 | if (indicator) { |
| 1234 | 1078 | const oldText = indicator.textContent; |
| 1079 | + console.log('[MxChat] Current indicator text:', oldText, '-> changing to mode:', mode); | |
| 1235 | 1080 | |
| 1236 | 1081 | if (mode === 'agent') { |
| 1237 | 1082 | indicator.textContent = 'Live Agent'; |
| 1083 | + console.log('[MxChat] Mode is agent, calling startPolling...'); | |
| 1238 | 1084 | startPolling(botId); |
| 1239 | 1085 | } else { |
| 1240 | 1086 | // Everything else is AI mode |
| 1241 | 1087 | const customAiText = indicator.getAttribute('data-ai-text') || 'AI Agent'; |
| @@ -1306,12 +1152,9 @@ | ||
| 1306 | 1152 | // Update the event handlers to use the correct function names (using event delegation) |
| 1307 | 1153 | // Use class-based selectors for multi-instance support |
| 1308 | 1154 | $(document).on('click', '.send-button', function() { |
| 1309 | 1155 | var botId = getBotIdFromElement(this); |
| 1310 | - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator'); | |
| 1311 | - if (!(modeIndicator && modeIndicator.textContent === 'Live Agent')) { | |
| 1312 | - disableChatInput(botId); | |
| 1313 | - } | |
| 1156 | + disableChatInput(botId); | |
| 1314 | 1157 | sendMessage(botId); |
| 1315 | 1158 | }); |
| 1316 | 1159 | |
| 1317 | 1160 | // Override enter key handler (using event delegation) |
| @@ -1318,256 +1161,14 @@ | ||
| 1318 | 1161 | $(document).on('keypress', '.chat-input', function(e) { |
| 1319 | 1162 | if (e.which == 13 && !e.shiftKey) { |
| 1320 | 1163 | e.preventDefault(); |
| 1321 | 1164 | var botId = getBotIdFromElement(this); |
| 1322 | - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator'); | |
| 1323 | - if (!(modeIndicator && modeIndicator.textContent === 'Live Agent')) { | |
| 1324 | - disableChatInput(botId); | |
| 1325 | - } | |
| 1165 | + disableChatInput(botId); | |
| 1326 | 1166 | sendMessage(botId); |
| 1327 | 1167 | } |
| 1328 | 1168 | }); |
| 1329 | 1169 | |
| 1330 | 1170 | |
| 1331 | -// Tags the chat-box transcript so the print stylesheet can target it, | |
| 1332 | -// and lazily initializes the header overflow menu for this bot if the | |
| 1333 | -// markup is present but not yet wired (covers dynamically-rendered widgets). | |
| 1334 | -// Idempotent; safe to call on every appended message. | |
| 1335 | -function mxchatEnsurePrintRoot(botId) { | |
| 1336 | - var $chatBox = getElement(botId, 'chat-box'); | |
| 1337 | - if (!$chatBox || !$chatBox.length) return; | |
| 1338 | - $chatBox.addClass('mxchat-conversation-print-root'); | |
| 1339 | - if (!$chatBox.attr('data-print-title')) { | |
| 1340 | - var nowStr = new Date().toLocaleString(); | |
| 1341 | - var headerTitle = ((typeof mxchatChat !== 'undefined' && mxchatChat.print_header_title) || 'Chat transcript') + ' — ' + nowStr; | |
| 1342 | - $chatBox.attr('data-print-title', headerTitle); | |
| 1343 | - } | |
| 1344 | - if (typeof mxchatInitHeaderMenu === 'function') { | |
| 1345 | - mxchatInitHeaderMenu(botId); | |
| 1346 | - } | |
| 1347 | -} | |
| 1348 | - | |
| 1349 | -// Builds the list of overflow-menu items for a given bot. | |
| 1350 | -// Adding a future item is one push to this array — do NOT hardcode "only download." | |
| 1351 | -function mxchatGetHeaderMenuItems(botId) { | |
| 1352 | - var items = []; | |
| 1353 | - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {}; | |
| 1354 | - | |
| 1355 | - // The `print_button_*` keys still gate this item for back-compat with | |
| 1356 | - // existing user options. The action is now a transcript download, not print. | |
| 1357 | - if (settings.print_button_enabled === 'on') { | |
| 1358 | - items.push({ | |
| 1359 | - id: 'download-transcript', | |
| 1360 | - label: settings.print_button_label || 'Download Transcript', | |
| 1361 | - icon: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>', | |
| 1362 | - action: function() { | |
| 1363 | - mxchatDownloadTranscript(botId); | |
| 1364 | - } | |
| 1365 | - }); | |
| 1366 | - } | |
| 1367 | - | |
| 1368 | - return items; | |
| 1369 | -} | |
| 1370 | - | |
| 1371 | -// Builds a clean markdown transcript of the current conversation and triggers | |
| 1372 | -// a file download. Used by the "Download Transcript" menu item. | |
| 1373 | -function mxchatDownloadTranscript(botId) { | |
| 1374 | - var $chatBox = getElement(botId, 'chat-box'); | |
| 1375 | - if (!$chatBox || !$chatBox.length) return; | |
| 1376 | - | |
| 1377 | - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {}; | |
| 1378 | - var headerTitle = settings.print_header_title || 'Chat transcript'; | |
| 1379 | - var now = new Date(); | |
| 1380 | - var stamp = now.toLocaleString(); | |
| 1381 | - | |
| 1382 | - var lines = []; | |
| 1383 | - lines.push('# ' + headerTitle); | |
| 1384 | - lines.push(''); | |
| 1385 | - lines.push('Exported: ' + stamp); | |
| 1386 | - lines.push(''); | |
| 1387 | - lines.push('---'); | |
| 1388 | - lines.push(''); | |
| 1389 | - | |
| 1390 | - $chatBox.find('.user-message, .bot-message, .agent-message').each(function() { | |
| 1391 | - var $msg = $(this); | |
| 1392 | - // Skip thinking placeholders and any in-flight temporary messages. | |
| 1393 | - if ($msg.find('.thinking-dots').length) return; | |
| 1394 | - if ($msg.hasClass('temporary-message')) return; | |
| 1395 | - | |
| 1396 | - var sender; | |
| 1397 | - if ($msg.hasClass('user-message')) sender = 'User'; | |
| 1398 | - else if ($msg.hasClass('agent-message')) sender = 'Live Agent'; | |
| 1399 | - else sender = 'AI Agent'; | |
| 1400 | - | |
| 1401 | - // Strip interactive UI from the cloned message so we get the conversation text. | |
| 1402 | - var $clone = $msg.clone(); | |
| 1403 | - $clone.find('.copy-button, .message-toolbar, .mxchat-copy, button, script, style').remove(); | |
| 1404 | - var text = $clone.text().replace(/ /g, ' ').replace(/[ \t]+\n/g, '\n').replace(/\n{3,}/g, '\n\n').trim(); | |
| 1405 | - if (!text) return; | |
| 1406 | - | |
| 1407 | - lines.push('**' + sender + '**'); | |
| 1408 | - lines.push(''); | |
| 1409 | - lines.push(text); | |
| 1410 | - lines.push(''); | |
| 1411 | - }); | |
| 1412 | - | |
| 1413 | - var content = lines.join('\n'); | |
| 1414 | - var iso = now.toISOString().replace(/[:.]/g, '-').slice(0, 19); | |
| 1415 | - var fname = 'mxchat-transcript-' + iso + '.md'; | |
| 1416 | - var blob = new Blob([content], { type: 'text/markdown;charset=utf-8' }); | |
| 1417 | - var url = URL.createObjectURL(blob); | |
| 1418 | - var a = document.createElement('a'); | |
| 1419 | - a.href = url; | |
| 1420 | - a.download = fname; | |
| 1421 | - a.style.display = 'none'; | |
| 1422 | - document.body.appendChild(a); | |
| 1423 | - a.click(); | |
| 1424 | - setTimeout(function() { | |
| 1425 | - if (a.parentNode) a.parentNode.removeChild(a); | |
| 1426 | - URL.revokeObjectURL(url); | |
| 1427 | - }, 100); | |
| 1428 | -} | |
| 1429 | - | |
| 1430 | -// Reads the bot bubble's actual computed bg+fg and writes them as CSS vars | |
| 1431 | -// on the menu wrap, so the dropdown matches whatever paints the bubble — | |
| 1432 | -// saved options, AI theme CSS, or the mxchat-theme add-on. | |
| 1433 | -function mxchatSyncMenuColors(botId, $wrap) { | |
| 1434 | - if (!$wrap || !$wrap.length) return; | |
| 1435 | - var $bot = $wrap.closest('.mxchat-chatbot-wrapper').find('.bot-message').not('.temporary-message').first(); | |
| 1436 | - if (!$bot.length) return; | |
| 1437 | - var cs = window.getComputedStyle($bot[0]); | |
| 1438 | - if (cs.backgroundColor && cs.backgroundColor !== 'rgba(0, 0, 0, 0)' && cs.backgroundColor !== 'transparent') { | |
| 1439 | - $wrap[0].style.setProperty('--mxchat-menu-bg', cs.backgroundColor); | |
| 1440 | - } | |
| 1441 | - // Bot text color usually lives on a child div, not .bot-message itself. | |
| 1442 | - var $textChild = $bot.find('[style*="color"]').first(); | |
| 1443 | - var fg = ($textChild.length ? window.getComputedStyle($textChild[0]).color : cs.color); | |
| 1444 | - if (fg) $wrap[0].style.setProperty('--mxchat-menu-fg', fg); | |
| 1445 | -} | |
| 1446 | - | |
| 1447 | -// One-time per-widget init: renders menu items, wires open/close, | |
| 1448 | -// outside-click, Escape, and arrow-key navigation. If no items, hides the trigger. | |
| 1449 | -function mxchatInitHeaderMenu(botId) { | |
| 1450 | - var $wrap = $('.mxchat-header-menu-wrap[data-bot-id="' + botId + '"]').first(); | |
| 1451 | - if (!$wrap.length || $wrap.data('mxchatMenuReady')) return; | |
| 1452 | - | |
| 1453 | - var $trigger = $wrap.find('.mxchat-menu-trigger'); | |
| 1454 | - var $menu = $wrap.find('.mxchat-header-menu'); | |
| 1455 | - var items = mxchatGetHeaderMenuItems(botId); | |
| 1456 | - | |
| 1457 | - // Initial color sync — covers normal page load. | |
| 1458 | - mxchatSyncMenuColors(botId, $wrap); | |
| 1459 | - | |
| 1460 | - if (!items.length) { | |
| 1461 | - $trigger.hide(); | |
| 1462 | - $menu.hide(); | |
| 1463 | - $wrap.data('mxchatMenuReady', true); | |
| 1464 | - return; | |
| 1465 | - } | |
| 1466 | - | |
| 1467 | - // Build the menu items. | |
| 1468 | - $menu.empty(); | |
| 1469 | - items.forEach(function(item, idx) { | |
| 1470 | - var $btn = $('<button>', { | |
| 1471 | - type: 'button', | |
| 1472 | - 'class': 'mxchat-menu-item', | |
| 1473 | - 'role': 'menuitem', | |
| 1474 | - 'tabindex': '-1', | |
| 1475 | - 'data-menu-id': item.id, | |
| 1476 | - html: '<span class="mxchat-menu-item-icon">' + item.icon + '</span>' + | |
| 1477 | - '<span class="mxchat-menu-item-label"></span>' | |
| 1478 | - }); | |
| 1479 | - $btn.find('.mxchat-menu-item-label').text(item.label); | |
| 1480 | - $btn.on('click', function(e) { | |
| 1481 | - e.preventDefault(); | |
| 1482 | - e.stopPropagation(); | |
| 1483 | - closeMenu(); | |
| 1484 | - try { item.action(); } catch (err) { /* no-op */ } | |
| 1485 | - }); | |
| 1486 | - $menu.append($btn); | |
| 1487 | - }); | |
| 1488 | - | |
| 1489 | - function openMenu() { | |
| 1490 | - // Re-sync each open in case the active theme changed since init. | |
| 1491 | - mxchatSyncMenuColors(botId, $wrap); | |
| 1492 | - $menu.prop('hidden', false).attr('aria-hidden', 'false').addClass('is-open'); | |
| 1493 | - $trigger.attr('aria-expanded', 'true'); | |
| 1494 | - // Focus the first item for keyboard users | |
| 1495 | - setTimeout(function() { | |
| 1496 | - $menu.find('.mxchat-menu-item').first().attr('tabindex', '0').trigger('focus'); | |
| 1497 | - }, 0); | |
| 1498 | - } | |
| 1499 | - function closeMenu(returnFocus) { | |
| 1500 | - $menu.prop('hidden', true).attr('aria-hidden', 'true').removeClass('is-open'); | |
| 1501 | - $trigger.attr('aria-expanded', 'false'); | |
| 1502 | - $menu.find('.mxchat-menu-item').attr('tabindex', '-1'); | |
| 1503 | - if (returnFocus) $trigger.trigger('focus'); | |
| 1504 | - } | |
| 1505 | - | |
| 1506 | - // Toggle on trigger click — stop propagation so the .chatbot-top-bar | |
| 1507 | - // click-to-collapse handler does not fire. | |
| 1508 | - $trigger.on('click', function(e) { | |
| 1509 | - e.preventDefault(); | |
| 1510 | - e.stopPropagation(); | |
| 1511 | - if ($menu.hasClass('is-open')) closeMenu(); | |
| 1512 | - else openMenu(); | |
| 1513 | - }); | |
| 1514 | - | |
| 1515 | - // Don't let clicks inside the menu bubble to the top-bar collapse handler. | |
| 1516 | - $menu.on('click', function(e) { | |
| 1517 | - e.stopPropagation(); | |
| 1518 | - }); | |
| 1519 | - | |
| 1520 | - // Outside click closes the menu. | |
| 1521 | - $(document).on('click.mxchatMenu-' + botId, function(e) { | |
| 1522 | - if (!$menu.hasClass('is-open')) return; | |
| 1523 | - if ($wrap.has(e.target).length || $wrap.is(e.target)) return; | |
| 1524 | - closeMenu(); | |
| 1525 | - }); | |
| 1526 | - | |
| 1527 | - // Keyboard: Escape closes and returns focus; arrow keys move focus; Enter activates. | |
| 1528 | - $menu.on('keydown', '.mxchat-menu-item', function(e) { | |
| 1529 | - var $items = $menu.find('.mxchat-menu-item'); | |
| 1530 | - var idx = $items.index(this); | |
| 1531 | - if (e.key === 'Escape') { | |
| 1532 | - e.preventDefault(); | |
| 1533 | - closeMenu(true); | |
| 1534 | - } else if (e.key === 'ArrowDown') { | |
| 1535 | - e.preventDefault(); | |
| 1536 | - var $next = $items.eq((idx + 1) % $items.length); | |
| 1537 | - $items.attr('tabindex', '-1'); | |
| 1538 | - $next.attr('tabindex', '0').trigger('focus'); | |
| 1539 | - } else if (e.key === 'ArrowUp') { | |
| 1540 | - e.preventDefault(); | |
| 1541 | - var $prev = $items.eq((idx - 1 + $items.length) % $items.length); | |
| 1542 | - $items.attr('tabindex', '-1'); | |
| 1543 | - $prev.attr('tabindex', '0').trigger('focus'); | |
| 1544 | - } else if (e.key === 'Enter' || e.key === ' ') { | |
| 1545 | - e.preventDefault(); | |
| 1546 | - $(this).trigger('click'); | |
| 1547 | - } | |
| 1548 | - }); | |
| 1549 | - $trigger.on('keydown', function(e) { | |
| 1550 | - if (e.key === 'Escape' && $menu.hasClass('is-open')) { | |
| 1551 | - e.preventDefault(); | |
| 1552 | - closeMenu(true); | |
| 1553 | - } else if ((e.key === 'ArrowDown' || e.key === 'Enter' || e.key === ' ') && !$menu.hasClass('is-open')) { | |
| 1554 | - e.preventDefault(); | |
| 1555 | - openMenu(); | |
| 1556 | - } | |
| 1557 | - }); | |
| 1558 | - | |
| 1559 | - $wrap.data('mxchatMenuReady', true); | |
| 1560 | -} | |
| 1561 | - | |
| 1562 | -// Initialize header menus for every rendered widget on DOM ready. | |
| 1563 | -$(function() { | |
| 1564 | - $('.mxchat-header-menu-wrap').each(function() { | |
| 1565 | - var botId = $(this).data('bot-id'); | |
| 1566 | - if (botId) mxchatInitHeaderMenu(botId); | |
| 1567 | - }); | |
| 1568 | -}); | |
| 1569 | - | |
| 1570 | 1171 | function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false, botId = 'default') { |
| 1571 | 1172 | try { |
| 1572 | 1173 | // Determine styles based on sender type |
| 1573 | 1174 | let messageClass, bgColor, fontColor; |
| @@ -1605,12 +1206,17 @@ | ||
| 1605 | 1206 | 'margin-bottom': '1em' |
| 1606 | 1207 | }); |
| 1607 | 1208 | } |
| 1608 | 1209 | |
| 1609 | - // Process the message content - always run linkify to convert markdown | |
| 1610 | - // links and format text. linkify() handles existing HTML safely via | |
| 1611 | - // negative lookaheads that skip URLs already inside <a> tags. | |
| 1612 | - let fullMessage = linkify(messageText); | |
| 1210 | + // Process the message content based on sender | |
| 1211 | + let fullMessage; | |
| 1212 | + if (sender === "user") { | |
| 1213 | + // For user messages, apply linkify after sanitization | |
| 1214 | + fullMessage = linkify(messageText); | |
| 1215 | + } else { | |
| 1216 | + // For bot/agent messages, preserve HTML | |
| 1217 | + fullMessage = messageText; | |
| 1218 | + } | |
| 1613 | 1219 | |
| 1614 | 1220 | // Add images if provided |
| 1615 | 1221 | if (images && images.length > 0) { |
| 1616 | 1222 | fullMessage += '<div class="image-gallery" dir="auto">'; |
| @@ -1659,12 +1265,8 @@ | ||
| 1659 | 1265 | if (lastUserMessage.length) { |
| 1660 | 1266 | scrollElementToTop(lastUserMessage, botId); |
| 1661 | 1267 | } |
| 1662 | 1268 | } |
| 1663 | - | |
| 1664 | - if ((sender === "bot" || sender === "agent") && !isTemporary) { | |
| 1665 | - mxchatEnsurePrintRoot(botId); | |
| 1666 | - } | |
| 1667 | 1269 | }); |
| 1668 | 1270 | |
| 1669 | 1271 | if (messageText.id) { |
| 1670 | 1272 | var instance = MxChatInstances.get(botId); |
| @@ -1749,12 +1351,26 @@ | ||
| 1749 | 1351 | bgColor = botMessageBgColor; |
| 1750 | 1352 | fontColor = botMessageFontColor; |
| 1751 | 1353 | } |
| 1752 | 1354 | |
| 1753 | - // Always run linkify to convert markdown links and format text. | |
| 1754 | - // linkify() already handles existing HTML (its URL patterns use negative lookaheads | |
| 1755 | - // to avoid double-processing URLs that are already inside <a> tags). | |
| 1756 | - var fullMessage = linkify(responseText); | |
| 1355 | + // FIXED: Only linkify if response doesn't already contain HTML links or tags | |
| 1356 | + // This prevents double-processing of URLs that are already formatted as HTML | |
| 1357 | + var fullMessage; | |
| 1358 | + if (sender === "user") { | |
| 1359 | + // Always linkify user messages (they're plain text) | |
| 1360 | + fullMessage = linkify(responseText); | |
| 1361 | + } else { | |
| 1362 | + // For bot/agent messages, check if HTML already exists | |
| 1363 | + if (responseText.includes('<a href=') || responseText.includes('</a>') || | |
| 1364 | + responseText.includes('<img') || responseText.includes('<div') || | |
| 1365 | + responseText.includes('<p>') || responseText.includes('<br>')) { | |
| 1366 | + // Response already has HTML, don't process it | |
| 1367 | + fullMessage = responseText; | |
| 1368 | + } else { | |
| 1369 | + // Plain text response, apply linkify | |
| 1370 | + fullMessage = linkify(responseText); | |
| 1371 | + } | |
| 1372 | + } | |
| 1757 | 1373 | |
| 1758 | 1374 | if (responseHtml) { |
| 1759 | 1375 | // Only add line breaks if there's actual text content before the HTML |
| 1760 | 1376 | if (fullMessage && fullMessage.trim()) { |
| @@ -1811,12 +1427,8 @@ | ||
| 1811 | 1427 | } |
| 1812 | 1428 | |
| 1813 | 1429 | // Re-enable chat input after response is displayed |
| 1814 | 1430 | enableChatInput(botId); |
| 1815 | - | |
| 1816 | - if (sender === "bot" || sender === "agent") { | |
| 1817 | - mxchatEnsurePrintRoot(botId); | |
| 1818 | - } | |
| 1819 | 1431 | } else { |
| 1820 | 1432 | appendMessage(sender, responseText, responseHtml, images, false, botId); |
| 1821 | 1433 | // Re-enable chat input after response is displayed |
| 1822 | 1434 | enableChatInput(botId); |
| @@ -1825,15 +1437,8 @@ | ||
| 1825 | 1437 | |
| 1826 | 1438 | |
| 1827 | 1439 | function appendThinkingMessage(botId) { |
| 1828 | 1440 | botId = botId || 'default'; |
| 1829 | - | |
| 1830 | - // Don't show thinking dots in live agent mode - message is just forwarded to a human | |
| 1831 | - var indicator = getElementDOM(botId, 'chat-mode-indicator'); | |
| 1832 | - if (indicator && indicator.textContent === 'Live Agent') { | |
| 1833 | - return; | |
| 1834 | - } | |
| 1835 | - | |
| 1836 | 1441 | var $chatBox = getElement(botId, 'chat-box'); |
| 1837 | 1442 | |
| 1838 | 1443 | // Remove any existing thinking dots in this bot's chat first |
| 1839 | 1444 | $chatBox.find('.thinking-dots').remove(); |
| @@ -1855,9 +1460,9 @@ | ||
| 1855 | 1460 | '</div>' + |
| 1856 | 1461 | '</div>'; |
| 1857 | 1462 | |
| 1858 | 1463 | // Append the thinking dots to this bot's chat container - skip inline colors if AI theme is active |
| 1859 | - var messageStyle = skipColors ? '' : ' style="background-color: ' + botMessageBgColor + '; color: ' + botMessageFontColor + ';"'; | |
| 1464 | + var messageStyle = skipColors ? '' : ' style="background-color: ' + botMessageBgColor + ';"'; | |
| 1860 | 1465 | $chatBox.append('<div class="bot-message temporary-message"' + messageStyle + '>' + thinkingHtml + '</div>'); |
| 1861 | 1466 | scrollToBottom(botId); |
| 1862 | 1467 | } |
| 1863 | 1468 | |
| @@ -1863,11 +1468,9 @@ | ||
| 1863 | 1468 | |
| 1864 | 1469 | function removeThinkingDots(botId) { |
| 1865 | 1470 | botId = botId || 'default'; |
| 1866 | 1471 | var $chatBox = getElement(botId, 'chat-box'); |
| 1867 | - // Remove by temporary-message class first, then fall back to any bot-message containing thinking dots | |
| 1868 | 1472 | $chatBox.find('.thinking-dots').closest('.temporary-message').remove(); |
| 1869 | - $chatBox.find('.bot-message .thinking-dots').closest('.bot-message').remove(); | |
| 1870 | 1473 | } |
| 1871 | 1474 | |
| 1872 | 1475 | // ==================================== |
| 1873 | 1476 | // TEXT FORMATTING & PROCESSING |
| @@ -1901,12 +1504,9 @@ | ||
| 1901 | 1504 | processedText = formatTextStyling(processedText); |
| 1902 | 1505 | |
| 1903 | 1506 | // Process code blocks BEFORE processing links |
| 1904 | 1507 | processedText = formatCodeBlocks(processedText); |
| 1905 | - | |
| 1906 | - // Process markdown tables BEFORE converting newlines to paragraphs | |
| 1907 | - processedText = formatMarkdownTables(processedText); | |
| 1908 | - | |
| 1508 | + | |
| 1909 | 1509 | // NOW convert to paragraphs |
| 1910 | 1510 | processedText = convertNewlinesToBreaks(processedText); |
| 1911 | 1511 | |
| 1912 | 1512 | // IMPORTANT: Handle citation-style brackets FIRST [URL] |
| @@ -1919,63 +1519,37 @@ | ||
| 1919 | 1519 | // Return as a proper link without the brackets |
| 1920 | 1520 | return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`; |
| 1921 | 1521 | }); |
| 1922 | 1522 | |
| 1923 | - // Process markdown links: [text](url) and [](url) | |
| 1924 | - // Uses balanced parenthesis matching to handle URLs containing parens | |
| 1925 | - // (e.g. PDF filenames with dates like (2025-08-28).pdf) | |
| 1926 | - processedText = (function(input) { | |
| 1927 | - var result = ''; | |
| 1928 | - var i = 0; | |
| 1929 | - while (i < input.length) { | |
| 1930 | - // Look for [ at current position | |
| 1931 | - if (input[i] === '[') { | |
| 1932 | - // Find closing ] | |
| 1933 | - var closeBracket = input.indexOf(']', i + 1); | |
| 1934 | - if (closeBracket === -1 || closeBracket + 1 >= input.length || input[closeBracket + 1] !== '(') { | |
| 1935 | - result += input[i]; | |
| 1936 | - i++; | |
| 1937 | - continue; | |
| 1938 | - } | |
| 1939 | - var linkText = input.substring(i + 1, closeBracket); | |
| 1940 | - // Check if URL starts with http | |
| 1941 | - var urlStart = closeBracket + 2; | |
| 1942 | - if (!input.substring(urlStart).match(/^https?:\/\//)) { | |
| 1943 | - result += input[i]; | |
| 1944 | - i++; | |
| 1945 | - continue; | |
| 1946 | - } | |
| 1947 | - // Find balanced closing paren | |
| 1948 | - var depth = 1; | |
| 1949 | - var j = urlStart; | |
| 1950 | - while (j < input.length && depth > 0) { | |
| 1951 | - if (input[j] === '(') depth++; | |
| 1952 | - else if (input[j] === ')') depth--; | |
| 1953 | - if (depth > 0) j++; | |
| 1954 | - } | |
| 1955 | - if (depth !== 0) { | |
| 1956 | - result += input[i]; | |
| 1957 | - i++; | |
| 1958 | - continue; | |
| 1959 | - } | |
| 1960 | - var url = input.substring(urlStart, j); | |
| 1961 | - var cleanUrl = url.replace(/[\].,;!?]+$/, ''); | |
| 1962 | - var encodedUrl = safeEncodeUrl(cleanUrl); | |
| 1963 | - if (!linkText || !linkText.trim()) { | |
| 1964 | - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + cleanUrl + '</a>'; | |
| 1965 | - } else { | |
| 1966 | - var safeText = sanitizeUserInput(linkText); | |
| 1967 | - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + safeText + '</a>'; | |
| 1968 | - } | |
| 1969 | - i = j + 1; // Skip past the closing ) | |
| 1970 | - } else { | |
| 1971 | - result += input[i]; | |
| 1972 | - i++; | |
| 1973 | - } | |
| 1523 | + // Process proper markdown links with text: [text](url) | |
| 1524 | + // This MUST have non-empty text in the first brackets | |
| 1525 | + const markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g; | |
| 1526 | + processedText = processedText.replace(markdownLinkPattern, (match, text, url) => { | |
| 1527 | + // Make sure we have actual text (not just whitespace) | |
| 1528 | + if (!text || !text.trim()) { | |
| 1529 | + // If no text, treat the URL as the text | |
| 1530 | + let cleanUrl = url.replace(/[.,;!?]+$/, ''); | |
| 1531 | + const safeUrl = safeEncodeUrl(cleanUrl); | |
| 1532 | + return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`; | |
| 1974 | 1533 | } |
| 1975 | - return result; | |
| 1976 | - })(processedText); | |
| 1534 | + | |
| 1535 | + // Clean the URL | |
| 1536 | + let cleanUrl = url.replace(/[\].,;!?]+$/, ''); | |
| 1537 | + const safeUrl = safeEncodeUrl(cleanUrl); | |
| 1538 | + const safeText = sanitizeUserInput(text); | |
| 1539 | + return `<a href="${safeUrl}" target="${linkTarget}">${safeText}</a>`; | |
| 1540 | + }); | |
| 1977 | 1541 | |
| 1542 | + // Handle empty markdown links: [](url) | |
| 1543 | + // This is a specific case where there's no text | |
| 1544 | + const emptyMarkdownPattern = /\[\]\((https?:\/\/[^\s)]+)\)/g; | |
| 1545 | + processedText = processedText.replace(emptyMarkdownPattern, (match, url) => { | |
| 1546 | + let cleanUrl = url.replace(/[.,;!?]+$/, ''); | |
| 1547 | + const safeUrl = safeEncodeUrl(cleanUrl); | |
| 1548 | + // Use the URL itself as the link text | |
| 1549 | + return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`; | |
| 1550 | + }); | |
| 1551 | + | |
| 1978 | 1552 | // Process phone numbers: [text](tel:number) |
| 1979 | 1553 | const phonePattern = /\[([^\]]+)\]\((tel:[\d+\-\s()]+)\)/g; |
| 1980 | 1554 | processedText = processedText.replace(phonePattern, (match, text, phone) => { |
| 1981 | 1555 | const safePhone = safeEncodeUrl(phone); |
| @@ -2127,78 +1701,9 @@ | ||
| 2127 | 1701 | }); |
| 2128 | 1702 | |
| 2129 | 1703 | return text; |
| 2130 | 1704 | } |
| 2131 | - | |
| 2132 | - function formatMarkdownTables(text) { | |
| 2133 | - var lines = text.split('\n'); | |
| 2134 | - var result = []; | |
| 2135 | - var i = 0; | |
| 2136 | - | |
| 2137 | - while (i < lines.length) { | |
| 2138 | - // Check for a table: current line has pipes AND next line is a separator row | |
| 2139 | - if (i + 1 < lines.length && | |
| 2140 | - lines[i].indexOf('|') !== -1 && | |
| 2141 | - /^\s*\|?[\s\-:]+(\|[\s\-:]+)+\|?\s*$/.test(lines[i + 1])) { | |
| 2142 | - | |
| 2143 | - var tableLines = []; | |
| 2144 | - var headerLine = lines[i]; | |
| 2145 | - var separatorLine = lines[i + 1]; | |
| 2146 | - tableLines.push(headerLine); | |
| 2147 | - tableLines.push(separatorLine); | |
| 2148 | - | |
| 2149 | - // Collect remaining table rows | |
| 2150 | - var j = i + 2; | |
| 2151 | - while (j < lines.length && lines[j].indexOf('|') !== -1 && lines[j].trim() !== '') { | |
| 2152 | - tableLines.push(lines[j]); | |
| 2153 | - j++; | |
| 2154 | - } | |
| 2155 | - | |
| 2156 | - // Parse alignment from separator row | |
| 2157 | - var sepCells = separatorLine.split('|').filter(function(c) { return c.trim() !== ''; }); | |
| 2158 | - var alignments = sepCells.map(function(cell) { | |
| 2159 | - var trimmed = cell.trim(); | |
| 2160 | - if (trimmed.charAt(0) === ':' && trimmed.charAt(trimmed.length - 1) === ':') return 'center'; | |
| 2161 | - if (trimmed.charAt(trimmed.length - 1) === ':') return 'right'; | |
| 2162 | - return 'left'; | |
| 2163 | - }); | |
| 2164 | - | |
| 2165 | - // Build HTML table | |
| 2166 | - var html = '<div class="mxchat-table-wrapper"><table class="mxchat-table">'; | |
| 2167 | - | |
| 2168 | - // Header row | |
| 2169 | - var headerCells = tableLines[0].split('|').filter(function(c) { return c.trim() !== ''; }); | |
| 2170 | - html += '<thead><tr>'; | |
| 2171 | - headerCells.forEach(function(cell, idx) { | |
| 2172 | - var align = alignments[idx] || 'left'; | |
| 2173 | - html += '<th style="text-align:' + align + '">' + cell.trim() + '</th>'; | |
| 2174 | - }); | |
| 2175 | - html += '</tr></thead>'; | |
| 2176 | - | |
| 2177 | - // Body rows | |
| 2178 | - html += '<tbody>'; | |
| 2179 | - for (var r = 2; r < tableLines.length; r++) { | |
| 2180 | - var rowCells = tableLines[r].split('|').filter(function(c) { return c.trim() !== ''; }); | |
| 2181 | - html += '<tr>'; | |
| 2182 | - rowCells.forEach(function(cell, idx) { | |
| 2183 | - var align = alignments[idx] || 'left'; | |
| 2184 | - html += '<td style="text-align:' + align + '">' + cell.trim() + '</td>'; | |
| 2185 | - }); | |
| 2186 | - html += '</tr>'; | |
| 2187 | - } | |
| 2188 | - html += '</tbody></table></div>'; | |
| 2189 | - | |
| 2190 | - result.push(html); | |
| 2191 | - i = j; | |
| 2192 | - } else { | |
| 2193 | - result.push(lines[i]); | |
| 2194 | - i++; | |
| 2195 | - } | |
| 2196 | - } | |
| 2197 | - | |
| 2198 | - return result.join('\n'); | |
| 2199 | - } | |
| 2200 | - | |
| 1705 | + | |
| 2201 | 1706 | function sanitizeUserInput(text) { |
| 2202 | 1707 | const div = document.createElement('div'); |
| 2203 | 1708 | div.textContent = text; |
| 2204 | 1709 | return div.innerHTML; |
| @@ -2269,14 +1774,13 @@ | ||
| 2269 | 1774 | requestAnimationFrame(smoothScroll); |
| 2270 | 1775 | } |
| 2271 | 1776 | } |
| 2272 | 1777 | |
| 2273 | - function scrollElementToTop(element, botId, topOffset) { | |
| 1778 | + function scrollElementToTop(element, botId) { | |
| 2274 | 1779 | botId = botId || 'default'; |
| 2275 | - topOffset = (typeof topOffset === 'number') ? topOffset : 2; | |
| 2276 | 1780 | var chatBox = getElement(botId, 'chat-box'); |
| 2277 | 1781 | var elementTop = element.position().top + chatBox.scrollTop(); |
| 2278 | - chatBox.animate({ scrollTop: Math.max(0, elementTop - topOffset) }, 500); | |
| 1782 | + chatBox.animate({ scrollTop: elementTop }, 500); | |
| 2279 | 1783 | } |
| 2280 | 1784 | |
| 2281 | 1785 | function showChatWidget(botId) { |
| 2282 | 1786 | botId = botId || 'default'; |
| @@ -2420,12 +1924,15 @@ | ||
| 2420 | 1924 | // LIVE AGENT FUNCTIONALITY |
| 2421 | 1925 | // ==================================== |
| 2422 | 1926 | |
| 2423 | 1927 | function startPolling(botId) { |
| 1928 | + console.log('[MxChat] startPolling called for botId:', botId); | |
| 2424 | 1929 | botId = botId || 'default'; |
| 2425 | 1930 | var instance = MxChatInstances.get(botId); |
| 2426 | 1931 | // Clear any existing interval first |
| 2427 | 1932 | stopPolling(botId); |
| 1933 | + // Start new polling interval | |
| 1934 | + console.log('[MxChat] Starting polling interval (5s) for botId:', botId); | |
| 2428 | 1935 | instance.pollingInterval = setInterval(function() { |
| 2429 | 1936 | checkForAgentMessages(botId); |
| 2430 | 1937 | }, 5000); |
| 2431 | 1938 | } |
| @@ -2430,17 +1937,20 @@ | ||
| 2430 | 1937 | }, 5000); |
| 2431 | 1938 | } |
| 2432 | 1939 | |
| 2433 | 1940 | function stopPolling(botId) { |
| 1941 | + console.log('[MxChat] stopPolling called for botId:', botId); | |
| 2434 | 1942 | botId = botId || 'default'; |
| 2435 | 1943 | var instance = MxChatInstances.get(botId); |
| 2436 | 1944 | if (instance.pollingInterval) { |
| 2437 | 1945 | clearInterval(instance.pollingInterval); |
| 2438 | 1946 | instance.pollingInterval = null; |
| 1947 | + console.log('[MxChat] Polling stopped for botId:', botId); | |
| 2439 | 1948 | } |
| 2440 | 1949 | } |
| 2441 | 1950 | |
| 2442 | 1951 | function checkForAgentMessages(botId) { |
| 1952 | + console.log('[MxChat] checkForAgentMessages called for botId:', botId); | |
| 2443 | 1953 | botId = botId || 'default'; |
| 2444 | 1954 | var instance = MxChatInstances.get(botId); |
| 2445 | 1955 | const sessionId = getChatSession(botId); |
| 2446 | 1956 | $.ajax({ |
| @@ -2466,12 +1976,8 @@ | ||
| 2466 | 1976 | instance.processedMessageIds.add(message.id); |
| 2467 | 1977 | } |
| 2468 | 1978 | }); |
| 2469 | 1979 | |
| 2470 | - if (hasNewMessage) { | |
| 2471 | - enableChatInput(botId); | |
| 2472 | - } | |
| 2473 | - | |
| 2474 | 1980 | var $floatingChatbot = getElement(botId, 'floating-chatbot'); |
| 2475 | 1981 | if (hasNewMessage && $floatingChatbot.hasClass('hidden')) { |
| 2476 | 1982 | showNotification(botId); |
| 2477 | 1983 | } |
| @@ -2477,13 +1983,8 @@ | ||
| 2477 | 1983 | } |
| 2478 | 1984 | |
| 2479 | 1985 | scrollToBottom(botId, true); |
| 2480 | 1986 | } |
| 2481 | - | |
| 2482 | - // Handle chat mode transitions (e.g. agent ended chat via !endchat) | |
| 2483 | - if (response.success && response.data?.chat_mode) { | |
| 2484 | - updateChatModeIndicator(response.data.chat_mode, botId); | |
| 2485 | - } | |
| 2486 | 1987 | }, |
| 2487 | 1988 | error: function (xhr, status, error) { |
| 2488 | 1989 | // Polling error - silently continue |
| 2489 | 1990 | } |
| @@ -2493,29 +1994,20 @@ | ||
| 2493 | 1994 | // ==================================== |
| 2494 | 1995 | // CHAT HISTORY & PERSISTENCE |
| 2495 | 1996 | // ==================================== |
| 2496 | 1997 | |
| 2497 | -function loadChatHistory(botId, onComplete) { | |
| 1998 | +function loadChatHistory(botId) { | |
| 2498 | 1999 | botId = botId || 'default'; |
| 2499 | 2000 | var instance = MxChatInstances.get(botId); |
| 2500 | 2001 | |
| 2501 | 2002 | // Prevent duplicate loading |
| 2502 | 2003 | if (instance.chatHistoryLoaded) { |
| 2503 | - if (onComplete) onComplete(); | |
| 2504 | 2004 | return; |
| 2505 | 2005 | } |
| 2506 | 2006 | |
| 2507 | - // Use getChatSession which returns null if no session exists (does NOT create one) | |
| 2508 | 2007 | var sessionId = getChatSession(botId); |
| 2509 | 2008 | var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on'; |
| 2510 | 2009 | |
| 2511 | - // No session yet — nothing to load. History will load after first message via ensureSession. | |
| 2512 | - if (!sessionId) { | |
| 2513 | - instance.chatHistoryLoaded = true; | |
| 2514 | - if (onComplete) onComplete(); | |
| 2515 | - return; | |
| 2516 | - } | |
| 2517 | - | |
| 2518 | 2010 | if (chatPersistenceEnabled && sessionId) { |
| 2519 | 2011 | $.ajax({ |
| 2520 | 2012 | url: mxchatChat.ajax_url, |
| 2521 | 2013 | type: 'POST', |
| @@ -2526,12 +2018,11 @@ | ||
| 2526 | 2018 | }, |
| 2527 | 2019 | success: function(response) { |
| 2528 | 2020 | // Handle session reset (IP changed while user was away) |
| 2529 | 2021 | if (response.success === false && response.data && response.data.action === 'reset_session') { |
| 2530 | - // Silent reset — new session but don't clear UI | |
| 2531 | - MxChatInstances.silentResetSession(botId); | |
| 2022 | + // Silently reset session - user will start fresh | |
| 2023 | + resetChatSession(botId); | |
| 2532 | 2024 | instance.chatHistoryLoaded = true; // Prevent retry loop |
| 2533 | - if (onComplete) onComplete(); | |
| 2534 | 2025 | return; |
| 2535 | 2026 | } |
| 2536 | 2027 | |
| 2537 | 2028 | // Check if the response indicates success |
| @@ -2587,19 +2078,9 @@ | ||
| 2587 | 2078 | var content = message.content; |
| 2588 | 2079 | content = content.replace(/\\'/g, "'").replace(/\\"/g, '"'); |
| 2589 | 2080 | content = decodeHTMLEntities(content); |
| 2590 | 2081 | |
| 2591 | - // Skip linkify for messages containing structured HTML | |
| 2592 | - // (forms, product cards, galleries, etc.) to avoid | |
| 2593 | - // markdown formatting corrupting HTML attributes | |
| 2594 | - // (e.g. underscores in name="field_name" becoming <em> tags) | |
| 2595 | - if (content.includes("mxchat-product-card") || | |
| 2596 | - content.includes("mxchat-image-gallery") || | |
| 2597 | - content.includes("mxchat-featured-products") || | |
| 2598 | - content.includes("<form") || | |
| 2599 | - content.includes("<input") || | |
| 2600 | - content.includes("<select") || | |
| 2601 | - content.includes("<textarea")) { | |
| 2082 | + if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) { | |
| 2602 | 2083 | messageElement.html(content); |
| 2603 | 2084 | } else { |
| 2604 | 2085 | var formattedContent = linkify(content); |
| 2605 | 2086 | messageElement.html(formattedContent); |
| @@ -2639,17 +2120,13 @@ | ||
| 2639 | 2120 | instance.chatHistoryLoaded = true; |
| 2640 | 2121 | } |
| 2641 | 2122 | } |
| 2642 | 2123 | } |
| 2643 | - if (onComplete) onComplete(); | |
| 2644 | 2124 | }, |
| 2645 | 2125 | error: function(xhr, status, error) { |
| 2646 | 2126 | // Error loading chat history - silently continue |
| 2647 | - if (onComplete) onComplete(); | |
| 2648 | 2127 | } |
| 2649 | 2128 | }); |
| 2650 | - } else { | |
| 2651 | - if (onComplete) onComplete(); | |
| 2652 | 2129 | } |
| 2653 | 2130 | } |
| 2654 | 2131 | |
| 2655 | 2132 | |
| @@ -2825,35 +2302,45 @@ | ||
| 2825 | 2302 | // ==================================== |
| 2826 | 2303 | |
| 2827 | 2304 | function checkPreChatDismissal(botId) { |
| 2828 | 2305 | botId = botId || 'default'; |
| 2829 | - try { | |
| 2830 | - var dismissedAt = localStorage.getItem('mxchat_pre_chat_dismissed_' + botId); | |
| 2831 | - if (dismissedAt) { | |
| 2832 | - // Re-show after 24 hours | |
| 2833 | - var elapsed = Date.now() - parseInt(dismissedAt, 10); | |
| 2834 | - if (elapsed < 86400000) { | |
| 2306 | + $.ajax({ | |
| 2307 | + url: mxchatChat.ajax_url, | |
| 2308 | + type: 'POST', | |
| 2309 | + data: { | |
| 2310 | + action: 'mxchat_check_pre_chat_message_status', | |
| 2311 | + _ajax_nonce: mxchatChat.nonce | |
| 2312 | + }, | |
| 2313 | + success: function(response) { | |
| 2314 | + if (response.success && !response.data.dismissed) { | |
| 2315 | + getElement(botId, 'pre-chat-message').fadeIn(250); | |
| 2316 | + } else { | |
| 2835 | 2317 | getElement(botId, 'pre-chat-message').hide(); |
| 2836 | - return; | |
| 2837 | 2318 | } |
| 2838 | - // Expired — clear and show again | |
| 2839 | - localStorage.removeItem('mxchat_pre_chat_dismissed_' + botId); | |
| 2319 | + }, | |
| 2320 | + error: function() { | |
| 2321 | + // Error checking pre-chat dismissal - silently continue | |
| 2840 | 2322 | } |
| 2841 | - getElement(botId, 'pre-chat-message').fadeIn(250); | |
| 2842 | - } catch (e) { | |
| 2843 | - // localStorage unavailable — show the message | |
| 2844 | - getElement(botId, 'pre-chat-message').fadeIn(250); | |
| 2845 | - } | |
| 2323 | + }); | |
| 2846 | 2324 | } |
| 2847 | 2325 | |
| 2848 | 2326 | function handlePreChatDismissal(botId) { |
| 2849 | 2327 | botId = botId || 'default'; |
| 2850 | 2328 | getElement(botId, 'pre-chat-message').fadeOut(200); |
| 2851 | - try { | |
| 2852 | - localStorage.setItem('mxchat_pre_chat_dismissed_' + botId, String(Date.now())); | |
| 2853 | - } catch (e) { | |
| 2854 | - // localStorage unavailable — dismissal won't persist | |
| 2855 | - } | |
| 2329 | + $.ajax({ | |
| 2330 | + url: mxchatChat.ajax_url, | |
| 2331 | + type: 'POST', | |
| 2332 | + data: { | |
| 2333 | + action: 'mxchat_dismiss_pre_chat_message', | |
| 2334 | + _ajax_nonce: mxchatChat.nonce | |
| 2335 | + }, | |
| 2336 | + success: function() { | |
| 2337 | + $('#pre-chat-message').hide(); | |
| 2338 | + }, | |
| 2339 | + error: function() { | |
| 2340 | + // Error dismissing pre-chat message - silently continue | |
| 2341 | + } | |
| 2342 | + }); | |
| 2856 | 2343 | } |
| 2857 | 2344 | |
| 2858 | 2345 | |
| 2859 | 2346 | // ==================================== |
| @@ -2920,26 +2407,8 @@ | ||
| 2920 | 2407 | $(this).addClass('hidden'); |
| 2921 | 2408 | $badge.hide(); // Hide notification when opening chat |
| 2922 | 2409 | disableScroll(); |
| 2923 | 2410 | $preChat.fadeOut(250); |
| 2924 | - | |
| 2925 | - // Load chat history for returning visitors (persistence) | |
| 2926 | - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on'; | |
| 2927 | - if (chatPersistenceEnabled) { | |
| 2928 | - MxChatInstances.ensureSession(botId); | |
| 2929 | - } | |
| 2930 | - | |
| 2931 | - // Deferred email check — only on first widget open | |
| 2932 | - var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 2933 | - var instance = MxChatInstances.get(botId); | |
| 2934 | - if (emailBlocker && !instance.emailCheckDone) { | |
| 2935 | - instance.emailCheckDone = true; | |
| 2936 | - resolveEmailState(botId); | |
| 2937 | - } else if (!emailBlocker) { | |
| 2938 | - // No email collection — still route through showChatContainerForBot | |
| 2939 | - // so the loader is shown while chat history loads | |
| 2940 | - showChatContainerForBot(botId); | |
| 2941 | - } | |
| 2942 | 2411 | } else { |
| 2943 | 2412 | $chatbot.removeClass('visible').addClass('hidden'); |
| 2944 | 2413 | $(this).removeClass('hidden'); |
| 2945 | 2414 | enableScroll(); |
| @@ -2957,9 +2426,11 @@ | ||
| 2957 | 2426 | |
| 2958 | 2427 | $(document).on('click', '.close-pre-chat-message', function(e) { |
| 2959 | 2428 | e.stopPropagation(); // Prevent triggering the parent .pre-chat-message click |
| 2960 | 2429 | var botId = getBotIdFromElement(this); |
| 2961 | - handlePreChatDismissal(botId); | |
| 2430 | + getElement(botId, 'pre-chat-message').fadeOut(200, function() { | |
| 2431 | + $(this).remove(); | |
| 2432 | + }); | |
| 2962 | 2433 | }); |
| 2963 | 2434 | |
| 2964 | 2435 | |
| 2965 | 2436 | // PDF upload button handlers - use class selector |
| @@ -3160,437 +2631,380 @@ | ||
| 3160 | 2631 | }); |
| 3161 | 2632 | |
| 3162 | 2633 | |
| 3163 | 2634 | // ==================================== |
| 3164 | -// INIT LOADER & CHAT CONTAINER HELPERS | |
| 2635 | +// EMAIL COLLECTION SETUP - FIXED VERSION | |
| 3165 | 2636 | // ==================================== |
| 3166 | -// These must be outside the email collection block so they're always available | |
| 3167 | -// (used by persistence loading even when email collection is off) | |
| 3168 | - | |
| 3169 | -function showInitLoader(botId) { | |
| 3170 | - var loader = getElementDOM(botId, 'mxchat-init-loader'); | |
| 3171 | - if (loader) loader.style.display = 'flex'; | |
| 3172 | -} | |
| 3173 | - | |
| 3174 | -function hideInitLoader(botId) { | |
| 3175 | - var loader = getElementDOM(botId, 'mxchat-init-loader'); | |
| 3176 | - if (loader) loader.style.display = 'none'; | |
| 3177 | -} | |
| 3178 | - | |
| 3179 | -function showEmailFormForBot(botId) { | |
| 3180 | - hideInitLoader(botId); | |
| 3181 | - var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 3182 | - var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 3183 | - if (emailBlocker) emailBlocker.style.display = 'flex'; | |
| 3184 | - if (chatContainer) chatContainer.style.display = 'none'; | |
| 3185 | -} | |
| 3186 | - | |
| 3187 | -function showChatContainerForBot(botId) { | |
| 3188 | - var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 3189 | - var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 3190 | - if (emailBlocker) emailBlocker.style.display = 'none'; | |
| 3191 | - | |
| 3192 | - var instance = MxChatInstances.get(botId); | |
| 3193 | - var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on'; | |
| 3194 | - | |
| 3195 | - // If persistence is on and history hasn't loaded yet, show loader | |
| 3196 | - // while history loads to prevent flash of empty chat | |
| 3197 | - if (chatPersistenceEnabled && !instance.chatHistoryLoaded) { | |
| 3198 | - if (chatContainer) chatContainer.style.display = 'none'; | |
| 3199 | - showInitLoader(botId); | |
| 3200 | - loadChatHistory(botId, function() { | |
| 3201 | - hideInitLoader(botId); | |
| 3202 | - if (chatContainer) chatContainer.style.display = 'flex'; | |
| 3203 | - scrollToBottom(botId, true); | |
| 3204 | - }); | |
| 3205 | - } else { | |
| 3206 | - hideInitLoader(botId); | |
| 3207 | - if (chatContainer) chatContainer.style.display = 'flex'; | |
| 3208 | - if (typeof loadChatHistory === 'function') { | |
| 3209 | - loadChatHistory(botId); | |
| 3210 | - } | |
| 3211 | - } | |
| 3212 | -} | |
| 3213 | - | |
| 3214 | -// ==================================== | |
| 3215 | -// EMAIL COLLECTION SETUP - MULTI-INSTANCE VERSION | |
| 3216 | -// ==================================== | |
| 3217 | 2637 | // Only run email collection setup if it's enabled |
| 3218 | 2638 | if (mxchatChat && mxchatChat.email_collection_enabled === 'on') { |
| 2639 | + // Email collection form setup and handlers | |
| 2640 | + const emailForm = document.getElementById('email-collection-form'); | |
| 2641 | + const emailBlocker = document.getElementById('email-blocker'); | |
| 2642 | + const chatbotWrapper = document.getElementById('chat-container'); | |
| 3219 | 2643 | |
| 3220 | - // Track submitting state per bot | |
| 3221 | - const emailSubmittingState = {}; | |
| 2644 | + if (emailForm && emailBlocker && chatbotWrapper) { | |
| 2645 | + | |
| 2646 | + // Add loading state management | |
| 2647 | + let isSubmitting = false; | |
| 2648 | + | |
| 2649 | + // Optimized UI transition functions | |
| 2650 | + function showEmailForm() { | |
| 2651 | + emailBlocker.style.display = 'flex'; | |
| 2652 | + chatbotWrapper.style.display = 'none'; | |
| 2653 | + } | |
| 3222 | 2654 | |
| 3223 | - // Add CSS animations for email form (once globally) | |
| 3224 | - if (!document.getElementById('email-error-styles')) { | |
| 3225 | - const style = document.createElement('style'); | |
| 3226 | - style.id = 'email-error-styles'; | |
| 3227 | - style.textContent = ` | |
| 3228 | - @keyframes fadeInError { | |
| 3229 | - from { opacity: 0; transform: translateY(-5px); } | |
| 3230 | - to { opacity: 1; transform: translateY(0); } | |
| 2655 | + function showChatContainer() { | |
| 2656 | + // Show chat immediately without delay | |
| 2657 | + emailBlocker.style.display = 'none'; | |
| 2658 | + chatbotWrapper.style.display = 'flex'; | |
| 2659 | + | |
| 2660 | + // Load chat history only after showing chat container | |
| 2661 | + if (typeof loadChatHistory === 'function') { | |
| 2662 | + loadChatHistory(); | |
| 3231 | 2663 | } |
| 3232 | - .email-input-shake { | |
| 3233 | - animation: shake 0.5s ease-in-out; | |
| 3234 | - } | |
| 3235 | - @keyframes shake { | |
| 3236 | - 0%, 100% { transform: translateX(0); } | |
| 3237 | - 25% { transform: translateX(-5px); } | |
| 3238 | - 75% { transform: translateX(5px); } | |
| 3239 | - } | |
| 3240 | - @keyframes spin { | |
| 3241 | - from { transform: rotate(0deg); } | |
| 3242 | - to { transform: rotate(360deg); } | |
| 3243 | - } | |
| 3244 | - .email-spinner { | |
| 3245 | - display: inline-block; | |
| 3246 | - vertical-align: middle; | |
| 3247 | - } | |
| 3248 | - `; | |
| 3249 | - document.head.appendChild(style); | |
| 3250 | - } | |
| 2664 | + } | |
| 3251 | 2665 | |
| 3252 | - function isValidEmailAddress(email) { | |
| 3253 | - const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | |
| 3254 | - return emailRegex.test(email.trim()) && email.length <= 254; | |
| 3255 | - } | |
| 2666 | + // Enhanced email validation | |
| 2667 | + function isValidEmail(email) { | |
| 2668 | + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | |
| 2669 | + return emailRegex.test(email.trim()) && email.length <= 254; // RFC 5321 limit | |
| 2670 | + } | |
| 3256 | 2671 | |
| 3257 | - function isValidNameInput(name) { | |
| 3258 | - return name && name.trim().length >= 2 && name.trim().length <= 100; | |
| 3259 | - } | |
| 2672 | + // Enhanced name validation | |
| 2673 | + function isValidName(name) { | |
| 2674 | + return name && name.trim().length >= 2 && name.trim().length <= 100; | |
| 2675 | + } | |
| 3260 | 2676 | |
| 3261 | - /** | |
| 3262 | - * Replace {visitor_name} placeholder in intro message with actual visitor name | |
| 3263 | - * @param {string} botId - The bot instance ID | |
| 3264 | - * @param {string} visitorName - The visitor's name to insert | |
| 3265 | - */ | |
| 3266 | - function replaceVisitorNamePlaceholder(botId, visitorName) { | |
| 3267 | - var chatBox = getElementDOM(botId, 'chat-box'); | |
| 3268 | - if (!chatBox) return; | |
| 3269 | - | |
| 3270 | - // Find the first bot message (intro message) | |
| 3271 | - var introMessage = chatBox.querySelector('.bot-message'); | |
| 3272 | - if (!introMessage) return; | |
| 3273 | - | |
| 3274 | - var messageContent = introMessage.querySelector('div[dir="auto"]'); | |
| 3275 | - if (!messageContent) return; | |
| 3276 | - | |
| 3277 | - var html = messageContent.innerHTML; | |
| 3278 | - | |
| 3279 | - // Replace {visitor_name} placeholder (case-insensitive) | |
| 3280 | - if (visitorName && visitorName.trim()) { | |
| 3281 | - // Escape HTML to prevent XSS | |
| 3282 | - var safeName = $('<div>').text(visitorName.trim()).html(); | |
| 3283 | - html = html.replace(/\{visitor_name\}/gi, safeName); | |
| 3284 | - } else { | |
| 3285 | - // Remove placeholder and clean up spacing if no name provided | |
| 3286 | - html = html.replace(/\{visitor_name\}/gi, ''); | |
| 3287 | - // Clean up any double spaces that might result | |
| 3288 | - html = html.replace(/\s{2,}/g, ' ').trim(); | |
| 2677 | + // Show loading state with spinner | |
| 2678 | + function setSubmissionState(loading) { | |
| 2679 | + const submitButton = document.getElementById('email-submit-button'); | |
| 2680 | + const emailInput = document.getElementById('user-email'); | |
| 2681 | + const nameInput = document.getElementById('user-name'); | |
| 2682 | + | |
| 2683 | + if (loading) { | |
| 2684 | + isSubmitting = true; | |
| 2685 | + if (submitButton) submitButton.disabled = true; | |
| 2686 | + if (emailInput) emailInput.disabled = true; | |
| 2687 | + if (nameInput) nameInput.disabled = true; | |
| 2688 | + | |
| 2689 | + // Store original content and add spinner | |
| 2690 | + if (submitButton && !submitButton.getAttribute('data-original-html')) { | |
| 2691 | + submitButton.setAttribute('data-original-html', submitButton.innerHTML); | |
| 2692 | + | |
| 2693 | + // Add loading spinner while keeping original text | |
| 2694 | + const originalText = submitButton.textContent; | |
| 2695 | + submitButton.innerHTML = ` | |
| 2696 | + <svg class="email-spinner" style="width: 16px; height: 16px; margin-right: 8px; animation: spin 1s linear infinite;" viewBox="0 0 24 24"> | |
| 2697 | + <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" fill="none" stroke-dasharray="31.416" stroke-dashoffset="31.416"> | |
| 2698 | + <animate attributeName="stroke-dasharray" dur="2s" values="0 31.416;15.708 15.708;0 31.416" repeatCount="indefinite"/> | |
| 2699 | + <animate attributeName="stroke-dashoffset" dur="2s" values="0;-15.708;-31.416" repeatCount="indefinite"/> | |
| 2700 | + </circle> | |
| 2701 | + </svg> | |
| 2702 | + ${originalText} | |
| 2703 | + `; | |
| 2704 | + | |
| 2705 | + submitButton.style.opacity = '0.8'; | |
| 2706 | + } | |
| 2707 | + } else { | |
| 2708 | + isSubmitting = false; | |
| 2709 | + if (submitButton) submitButton.disabled = false; | |
| 2710 | + if (emailInput) emailInput.disabled = false; | |
| 2711 | + if (nameInput) nameInput.disabled = false; | |
| 2712 | + | |
| 2713 | + // Restore original content | |
| 2714 | + if (submitButton) { | |
| 2715 | + const originalHtml = submitButton.getAttribute('data-original-html'); | |
| 2716 | + if (originalHtml) { | |
| 2717 | + submitButton.innerHTML = originalHtml; | |
| 2718 | + } | |
| 2719 | + submitButton.style.opacity = '1'; | |
| 2720 | + } | |
| 2721 | + } | |
| 3289 | 2722 | } |
| 3290 | 2723 | |
| 3291 | - messageContent.innerHTML = html; | |
| 3292 | - } | |
| 3293 | - | |
| 3294 | - function setEmailSubmissionState(botId, loading) { | |
| 3295 | - var submitButton = getElementDOM(botId, 'email-submit-button'); | |
| 3296 | - var emailInput = getElementDOM(botId, 'user-email'); | |
| 3297 | - var nameInput = getElementDOM(botId, 'user-name'); | |
| 3298 | - | |
| 3299 | - if (loading) { | |
| 3300 | - emailSubmittingState[botId] = true; | |
| 3301 | - if (submitButton) submitButton.disabled = true; | |
| 3302 | - if (emailInput) emailInput.disabled = true; | |
| 3303 | - if (nameInput) nameInput.disabled = true; | |
| 3304 | - | |
| 3305 | - if (submitButton && !submitButton.getAttribute('data-original-html')) { | |
| 3306 | - submitButton.setAttribute('data-original-html', submitButton.innerHTML); | |
| 3307 | - const originalText = submitButton.textContent; | |
| 3308 | - submitButton.innerHTML = ` | |
| 3309 | - <svg class="email-spinner" style="width: 16px; height: 16px; margin-right: 8px; animation: spin 1s linear infinite;" viewBox="0 0 24 24"> | |
| 3310 | - <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" fill="none" stroke-dasharray="31.416" stroke-dashoffset="31.416"> | |
| 3311 | - <animate attributeName="stroke-dasharray" dur="2s" values="0 31.416;15.708 15.708;0 31.416" repeatCount="indefinite"/> | |
| 3312 | - <animate attributeName="stroke-dashoffset" dur="2s" values="0;-15.708;-31.416" repeatCount="indefinite"/> | |
| 3313 | - </circle> | |
| 3314 | - </svg> | |
| 3315 | - ${originalText} | |
| 2724 | + // Error display functions | |
| 2725 | + function showEmailError(message) { | |
| 2726 | + clearEmailError(); | |
| 2727 | + | |
| 2728 | + const errorDiv = document.createElement('div'); | |
| 2729 | + errorDiv.className = 'email-error'; | |
| 2730 | + errorDiv.style.cssText = ` | |
| 2731 | + color: #e74c3c; | |
| 2732 | + font-size: 12px; | |
| 2733 | + margin-top: 8px; | |
| 2734 | + padding: 4px 0; | |
| 2735 | + animation: fadeInError 0.3s ease; | |
| 2736 | + `; | |
| 2737 | + errorDiv.textContent = message; | |
| 2738 | + | |
| 2739 | + // Add CSS animation if not already present | |
| 2740 | + if (!document.getElementById('email-error-styles')) { | |
| 2741 | + const style = document.createElement('style'); | |
| 2742 | + style.id = 'email-error-styles'; | |
| 2743 | + style.textContent = ` | |
| 2744 | + @keyframes fadeInError { | |
| 2745 | + from { opacity: 0; transform: translateY(-5px); } | |
| 2746 | + to { opacity: 1; transform: translateY(0); } | |
| 2747 | + } | |
| 2748 | + .email-input-shake { | |
| 2749 | + animation: shake 0.5s ease-in-out; | |
| 2750 | + } | |
| 2751 | + @keyframes shake { | |
| 2752 | + 0%, 100% { transform: translateX(0); } | |
| 2753 | + 25% { transform: translateX(-5px); } | |
| 2754 | + 75% { transform: translateX(5px); } | |
| 2755 | + } | |
| 2756 | + @keyframes spin { | |
| 2757 | + from { transform: rotate(0deg); } | |
| 2758 | + to { transform: rotate(360deg); } | |
| 2759 | + } | |
| 2760 | + .email-spinner { | |
| 2761 | + display: inline-block; | |
| 2762 | + vertical-align: middle; | |
| 2763 | + } | |
| 3316 | 2764 | `; |
| 3317 | - submitButton.style.opacity = '0.8'; | |
| 2765 | + document.head.appendChild(style); | |
| 3318 | 2766 | } |
| 3319 | - } else { | |
| 3320 | - emailSubmittingState[botId] = false; | |
| 3321 | - if (submitButton) submitButton.disabled = false; | |
| 3322 | - if (emailInput) emailInput.disabled = false; | |
| 3323 | - if (nameInput) nameInput.disabled = false; | |
| 3324 | - | |
| 3325 | - if (submitButton) { | |
| 3326 | - const originalHtml = submitButton.getAttribute('data-original-html'); | |
| 3327 | - if (originalHtml) { | |
| 3328 | - submitButton.innerHTML = originalHtml; | |
| 3329 | - } | |
| 3330 | - submitButton.style.opacity = '1'; | |
| 2767 | + | |
| 2768 | + emailForm.appendChild(errorDiv); | |
| 2769 | + | |
| 2770 | + // Add shake animation to inputs | |
| 2771 | + const emailInput = document.getElementById('user-email'); | |
| 2772 | + const nameInput = document.getElementById('user-name'); | |
| 2773 | + | |
| 2774 | + if (emailInput) { | |
| 2775 | + emailInput.classList.add('email-input-shake'); | |
| 2776 | + setTimeout(() => { | |
| 2777 | + emailInput.classList.remove('email-input-shake'); | |
| 2778 | + }, 500); | |
| 3331 | 2779 | } |
| 2780 | + | |
| 2781 | + if (nameInput) { | |
| 2782 | + nameInput.classList.add('email-input-shake'); | |
| 2783 | + setTimeout(() => { | |
| 2784 | + nameInput.classList.remove('email-input-shake'); | |
| 2785 | + }, 500); | |
| 2786 | + } | |
| 3332 | 2787 | } |
| 3333 | - } | |
| 3334 | 2788 | |
| 3335 | - function showEmailError(botId, message) { | |
| 3336 | - clearEmailError(botId); | |
| 3337 | - | |
| 3338 | - var emailForm = getElementDOM(botId, 'email-collection-form'); | |
| 3339 | - if (!emailForm) return; | |
| 3340 | - | |
| 3341 | - const errorDiv = document.createElement('div'); | |
| 3342 | - errorDiv.className = 'email-error'; | |
| 3343 | - errorDiv.style.cssText = ` | |
| 3344 | - color: #e74c3c; | |
| 3345 | - font-size: 12px; | |
| 3346 | - margin-top: 8px; | |
| 3347 | - padding: 4px 0; | |
| 3348 | - animation: fadeInError 0.3s ease; | |
| 3349 | - `; | |
| 3350 | - errorDiv.textContent = message; | |
| 3351 | - emailForm.appendChild(errorDiv); | |
| 3352 | - | |
| 3353 | - // Add shake animation to inputs | |
| 3354 | - var emailInput = getElementDOM(botId, 'user-email'); | |
| 3355 | - var nameInput = getElementDOM(botId, 'user-name'); | |
| 3356 | - | |
| 3357 | - if (emailInput) { | |
| 3358 | - emailInput.classList.add('email-input-shake'); | |
| 3359 | - setTimeout(() => emailInput.classList.remove('email-input-shake'), 500); | |
| 3360 | - } | |
| 3361 | - if (nameInput) { | |
| 3362 | - nameInput.classList.add('email-input-shake'); | |
| 3363 | - setTimeout(() => nameInput.classList.remove('email-input-shake'), 500); | |
| 3364 | - } | |
| 3365 | - } | |
| 3366 | - | |
| 3367 | - function clearEmailError(botId) { | |
| 3368 | - var emailForm = getElementDOM(botId, 'email-collection-form'); | |
| 3369 | - if (emailForm) { | |
| 2789 | + function clearEmailError() { | |
| 3370 | 2790 | const existingErrors = emailForm.querySelectorAll('.email-error'); |
| 3371 | 2791 | existingErrors.forEach(error => error.remove()); |
| 3372 | 2792 | } |
| 3373 | - } | |
| 3374 | 2793 | |
| 3375 | - // Resolve email state using server-side data when available, AJAX fallback otherwise | |
| 3376 | - function resolveEmailState(botId) { | |
| 3377 | - if (mxchatChat.skip_email_check && mxchatChat.initial_email_state) { | |
| 3378 | - if (mxchatChat.initial_email_state.show_email_form) { | |
| 3379 | - showEmailFormForBot(botId); | |
| 3380 | - } else { | |
| 3381 | - showChatContainerForBot(botId); | |
| 3382 | - } | |
| 3383 | - } else { | |
| 3384 | - checkSessionAndEmailForBot(botId); | |
| 3385 | - } | |
| 3386 | - } | |
| 2794 | + // MAIN FORM SUBMIT HANDLER | |
| 2795 | + // Remove any existing event listeners first | |
| 2796 | + emailForm.removeEventListener('submit', handleFormSubmit); | |
| 3387 | 2797 | |
| 3388 | - function checkSessionAndEmailForBot(botId) { | |
| 3389 | - const sessionId = MxChatInstances.ensureSession(botId); | |
| 2798 | + // Add the form submit handler | |
| 2799 | + emailForm.addEventListener('submit', handleFormSubmit); | |
| 3390 | 2800 | |
| 3391 | - // Hide both panels while we check — show loader instead | |
| 3392 | - var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 3393 | - var chatContainer = getElementDOM(botId, 'chat-container'); | |
| 3394 | - if (emailBlocker) emailBlocker.style.display = 'none'; | |
| 3395 | - if (chatContainer) chatContainer.style.display = 'none'; | |
| 3396 | - showInitLoader(botId); | |
| 2801 | + function handleFormSubmit(event) { | |
| 2802 | + event.preventDefault(); | |
| 2803 | + event.stopPropagation(); | |
| 3397 | 2804 | |
| 3398 | - fetch(mxchatChat.ajax_url, { | |
| 3399 | - method: 'POST', | |
| 3400 | - headers: { | |
| 3401 | - 'Content-Type': 'application/x-www-form-urlencoded', | |
| 3402 | - }, | |
| 3403 | - body: new URLSearchParams({ | |
| 3404 | - action: 'mxchat_check_email_provided', | |
| 3405 | - session_id: sessionId, | |
| 3406 | - nonce: mxchatChat.nonce, | |
| 3407 | - }) | |
| 3408 | - }) | |
| 3409 | - .then((response) => { | |
| 3410 | - if (!response.ok) { | |
| 3411 | - throw new Error(`HTTP error! status: ${response.status}`); | |
| 2805 | + // Prevent double submission | |
| 2806 | + if (isSubmitting) { | |
| 2807 | + return false; | |
| 3412 | 2808 | } |
| 3413 | - return response.json(); | |
| 3414 | - }) | |
| 3415 | - .then((data) => { | |
| 3416 | - if (data.success) { | |
| 3417 | - if (data.data.logged_in || data.data.email) { | |
| 3418 | - showChatContainerForBot(botId); | |
| 3419 | - } else { | |
| 3420 | - showEmailFormForBot(botId); | |
| 3421 | - } | |
| 3422 | - } else { | |
| 3423 | - showEmailFormForBot(botId); | |
| 3424 | - } | |
| 3425 | - }) | |
| 3426 | - .catch((error) => { | |
| 3427 | - showEmailFormForBot(botId); | |
| 3428 | - }); | |
| 3429 | - } | |
| 3430 | 2809 | |
| 3431 | - // Event delegation for email form submission | |
| 3432 | - $(document).on('submit', '.email-collection-form', function(e) { | |
| 3433 | - e.preventDefault(); | |
| 3434 | - e.stopPropagation(); | |
| 2810 | + const userEmail = document.getElementById('user-email').value.trim(); | |
| 2811 | + const nameInput = document.getElementById('user-name'); | |
| 2812 | + const userName = nameInput ? nameInput.value.trim() : ''; | |
| 2813 | + const sessionId = getChatSession(); | |
| 3435 | 2814 | |
| 3436 | - var botId = getBotIdFromElement(this); | |
| 2815 | + // Validate email before submission | |
| 2816 | + if (!userEmail) { | |
| 2817 | + showEmailError('Please enter your email address.'); | |
| 2818 | + return false; | |
| 2819 | + } | |
| 3437 | 2820 | |
| 3438 | - // Prevent double submission | |
| 3439 | - if (emailSubmittingState[botId]) { | |
| 3440 | - return false; | |
| 3441 | - } | |
| 2821 | + if (!isValidEmail(userEmail)) { | |
| 2822 | + showEmailError('Please enter a valid email address.'); | |
| 2823 | + return false; | |
| 2824 | + } | |
| 3442 | 2825 | |
| 3443 | - var emailInput = getElementDOM(botId, 'user-email'); | |
| 3444 | - var nameInput = getElementDOM(botId, 'user-name'); | |
| 3445 | - var userEmail = emailInput ? emailInput.value.trim() : ''; | |
| 3446 | - var userName = nameInput ? nameInput.value.trim() : ''; | |
| 3447 | - var sessionId = MxChatInstances.ensureSession(botId); | |
| 2826 | + // Validate name if field exists | |
| 2827 | + if (nameInput && !isValidName(userName)) { | |
| 2828 | + showEmailError('Please enter a valid name (2-100 characters).'); | |
| 2829 | + return false; | |
| 2830 | + } | |
| 3448 | 2831 | |
| 3449 | - // Validate email | |
| 3450 | - if (!userEmail) { | |
| 3451 | - showEmailError(botId, 'Please enter your email address.'); | |
| 3452 | - return false; | |
| 3453 | - } | |
| 2832 | + // Clear any existing errors | |
| 2833 | + clearEmailError(); | |
| 2834 | + setSubmissionState(true); | |
| 3454 | 2835 | |
| 3455 | - if (!isValidEmailAddress(userEmail)) { | |
| 3456 | - showEmailError(botId, 'Please enter a valid email address.'); | |
| 3457 | - return false; | |
| 3458 | - } | |
| 2836 | + // Prepare form data with optional name | |
| 2837 | + const formData = new URLSearchParams({ | |
| 2838 | + action: 'mxchat_handle_save_email_and_response', | |
| 2839 | + email: userEmail, | |
| 2840 | + session_id: sessionId, | |
| 2841 | + nonce: mxchatChat.nonce, | |
| 2842 | + }); | |
| 3459 | 2843 | |
| 3460 | - // Validate name if field exists and has content | |
| 3461 | - if (nameInput && userName && !isValidNameInput(userName)) { | |
| 3462 | - showEmailError(botId, 'Please enter a valid name (2-100 characters).'); | |
| 3463 | - return false; | |
| 3464 | - } | |
| 2844 | + // Add name to form data if provided | |
| 2845 | + if (userName) { | |
| 2846 | + formData.append('name', userName); | |
| 2847 | + } | |
| 3465 | 2848 | |
| 3466 | - clearEmailError(botId); | |
| 3467 | - setEmailSubmissionState(botId, true); | |
| 2849 | + fetch(mxchatChat.ajax_url, { | |
| 2850 | + method: 'POST', | |
| 2851 | + headers: { | |
| 2852 | + 'Content-Type': 'application/x-www-form-urlencoded', | |
| 2853 | + }, | |
| 2854 | + body: formData | |
| 2855 | + }) | |
| 2856 | + .then((response) => { | |
| 2857 | + if (!response.ok) { | |
| 2858 | + throw new Error(`HTTP error! status: ${response.status}`); | |
| 2859 | + } | |
| 2860 | + return response.json(); | |
| 2861 | + }) | |
| 2862 | + .then((data) => { | |
| 2863 | + setSubmissionState(false); | |
| 3468 | 2864 | |
| 3469 | - // Prepare form data | |
| 3470 | - const formData = new URLSearchParams({ | |
| 3471 | - action: 'mxchat_handle_save_email_and_response', | |
| 3472 | - email: userEmail, | |
| 3473 | - session_id: sessionId, | |
| 3474 | - nonce: mxchatChat.nonce, | |
| 3475 | - }); | |
| 2865 | + if (data.success) { | |
| 2866 | + // Show chat immediately | |
| 2867 | + showChatContainer(); | |
| 3476 | 2868 | |
| 3477 | - if (userName) { | |
| 3478 | - formData.append('name', userName); | |
| 2869 | + // Handle bot response if provided | |
| 2870 | + if (data.message && typeof appendMessage === 'function') { | |
| 2871 | + setTimeout(() => { | |
| 2872 | + appendMessage('bot', data.message); | |
| 2873 | + if (typeof scrollToBottom === 'function') { | |
| 2874 | + scrollToBottom(); | |
| 2875 | + } | |
| 2876 | + }, 100); | |
| 2877 | + } | |
| 2878 | + } else { | |
| 2879 | + showEmailError(data.message || 'Failed to save email. Please try again.'); | |
| 2880 | + } | |
| 2881 | + }) | |
| 2882 | + .catch((error) => { | |
| 2883 | + setSubmissionState(false); | |
| 2884 | + showEmailError('An error occurred. Please try again.'); | |
| 2885 | + }); | |
| 2886 | + | |
| 2887 | + return false; // Extra prevention | |
| 3479 | 2888 | } |
| 3480 | 2889 | |
| 3481 | - fetch(mxchatChat.ajax_url, { | |
| 3482 | - method: 'POST', | |
| 3483 | - headers: { | |
| 3484 | - 'Content-Type': 'application/x-www-form-urlencoded', | |
| 3485 | - }, | |
| 3486 | - body: formData | |
| 3487 | - }) | |
| 3488 | - .then((response) => { | |
| 3489 | - if (!response.ok) { | |
| 3490 | - throw new Error(`HTTP error! status: ${response.status}`); | |
| 3491 | - } | |
| 3492 | - return response.json(); | |
| 3493 | - }) | |
| 3494 | - .then((data) => { | |
| 3495 | - setEmailSubmissionState(botId, false); | |
| 2890 | + // Real-time email validation | |
| 2891 | + const emailInput = document.getElementById('user-email'); | |
| 2892 | + if (emailInput) { | |
| 2893 | + let validationTimeout; | |
| 2894 | + | |
| 2895 | + emailInput.addEventListener('input', function() { | |
| 2896 | + // Clear previous validation timeout | |
| 2897 | + if (validationTimeout) { | |
| 2898 | + clearTimeout(validationTimeout); | |
| 2899 | + } | |
| 2900 | + | |
| 2901 | + // Debounce validation | |
| 2902 | + validationTimeout = setTimeout(() => { | |
| 2903 | + const email = this.value.trim(); | |
| 2904 | + clearEmailError(); | |
| 2905 | + | |
| 2906 | + if (email && !isValidEmail(email)) { | |
| 2907 | + showEmailError('Please enter a valid email address.'); | |
| 2908 | + } | |
| 2909 | + }, 500); | |
| 2910 | + }); | |
| 3496 | 2911 | |
| 3497 | - if (data.success) { | |
| 3498 | - showChatContainerForBot(botId); | |
| 2912 | + // Handle Enter key | |
| 2913 | + emailInput.addEventListener('keypress', function(e) { | |
| 2914 | + if (e.key === 'Enter' && !isSubmitting) { | |
| 2915 | + e.preventDefault(); | |
| 2916 | + emailForm.dispatchEvent(new Event('submit')); | |
| 2917 | + } | |
| 2918 | + }); | |
| 2919 | + } | |
| 3499 | 2920 | |
| 3500 | - // Replace {visitor_name} placeholder in intro message with actual name | |
| 3501 | - if (userName) { | |
| 3502 | - replaceVisitorNamePlaceholder(botId, userName); | |
| 3503 | - } else { | |
| 3504 | - // Remove placeholder if no name provided | |
| 3505 | - replaceVisitorNamePlaceholder(botId, ''); | |
| 2921 | + // Real-time name validation | |
| 2922 | + const nameInput = document.getElementById('user-name'); | |
| 2923 | + if (nameInput) { | |
| 2924 | + let nameValidationTimeout; | |
| 2925 | + | |
| 2926 | + nameInput.addEventListener('input', function() { | |
| 2927 | + // Clear previous validation timeout | |
| 2928 | + if (nameValidationTimeout) { | |
| 2929 | + clearTimeout(nameValidationTimeout); | |
| 3506 | 2930 | } |
| 2931 | + | |
| 2932 | + // Debounce validation | |
| 2933 | + nameValidationTimeout = setTimeout(() => { | |
| 2934 | + const name = this.value.trim(); | |
| 2935 | + clearEmailError(); | |
| 2936 | + | |
| 2937 | + if (name && !isValidName(name)) { | |
| 2938 | + showEmailError('Name must be between 2 and 100 characters.'); | |
| 2939 | + } | |
| 2940 | + }, 500); | |
| 2941 | + }); | |
| 3507 | 2942 | |
| 3508 | - if (data.message && typeof appendMessage === 'function') { | |
| 3509 | - setTimeout(() => { | |
| 3510 | - appendMessage('bot', data.message, '', [], false, botId); | |
| 3511 | - if (typeof scrollToBottom === 'function') { | |
| 3512 | - scrollToBottom(botId); | |
| 3513 | - } | |
| 3514 | - }, 100); | |
| 2943 | + // Handle Enter key | |
| 2944 | + nameInput.addEventListener('keypress', function(e) { | |
| 2945 | + if (e.key === 'Enter' && !isSubmitting) { | |
| 2946 | + e.preventDefault(); | |
| 2947 | + emailForm.dispatchEvent(new Event('submit')); | |
| 3515 | 2948 | } |
| 2949 | + }); | |
| 2950 | + } | |
| 2951 | + | |
| 2952 | + // Initial state check | |
| 2953 | + if (mxchatChat.skip_email_check && mxchatChat.initial_email_state) { | |
| 2954 | + const emailState = mxchatChat.initial_email_state; | |
| 2955 | + if (emailState.show_email_form) { | |
| 2956 | + showEmailForm(); | |
| 3516 | 2957 | } else { |
| 3517 | - showEmailError(botId, data.message || 'Failed to save email. Please try again.'); | |
| 2958 | + showChatContainer(); | |
| 3518 | 2959 | } |
| 3519 | - }) | |
| 3520 | - .catch((error) => { | |
| 3521 | - setEmailSubmissionState(botId, false); | |
| 3522 | - showEmailError(botId, 'An error occurred. Please try again.'); | |
| 3523 | - }); | |
| 3524 | - | |
| 3525 | - return false; | |
| 3526 | - }); | |
| 3527 | - | |
| 3528 | - // Real-time email validation using event delegation | |
| 3529 | - $(document).on('input', '.mxchat-email-input', function() { | |
| 3530 | - var botId = getBotIdFromElement(this); | |
| 3531 | - var $input = $(this); | |
| 3532 | - | |
| 3533 | - // Clear previous timeout | |
| 3534 | - clearTimeout($input.data('validationTimeout')); | |
| 3535 | - | |
| 3536 | - // Debounce validation | |
| 3537 | - var timeout = setTimeout(() => { | |
| 3538 | - var email = this.value.trim(); | |
| 3539 | - clearEmailError(botId); | |
| 3540 | - | |
| 3541 | - if (email && !isValidEmailAddress(email)) { | |
| 3542 | - showEmailError(botId, 'Please enter a valid email address.'); | |
| 3543 | - } | |
| 3544 | - }, 500); | |
| 3545 | - | |
| 3546 | - $input.data('validationTimeout', timeout); | |
| 3547 | - }); | |
| 3548 | - | |
| 3549 | - // Handle Enter key in email input | |
| 3550 | - $(document).on('keypress', '.mxchat-email-input', function(e) { | |
| 3551 | - if (e.key === 'Enter') { | |
| 3552 | - e.preventDefault(); | |
| 3553 | - var botId = getBotIdFromElement(this); | |
| 3554 | - if (!emailSubmittingState[botId]) { | |
| 3555 | - $(this).closest('.email-collection-form').submit(); | |
| 3556 | - } | |
| 2960 | + } else { | |
| 2961 | + // Check email status via AJAX | |
| 2962 | + setTimeout(checkSessionAndEmail, 100); | |
| 3557 | 2963 | } |
| 3558 | - }); | |
| 3559 | 2964 | |
| 3560 | - // Handle Enter key in name input | |
| 3561 | - $(document).on('keypress', '.mxchat-name-input', function(e) { | |
| 3562 | - if (e.key === 'Enter') { | |
| 3563 | - e.preventDefault(); | |
| 3564 | - var botId = getBotIdFromElement(this); | |
| 3565 | - if (!emailSubmittingState[botId]) { | |
| 3566 | - $(this).closest('.email-collection-form').submit(); | |
| 3567 | - } | |
| 2965 | + // Check if email exists for the current session | |
| 2966 | + function checkSessionAndEmail() { | |
| 2967 | + const sessionId = getChatSession(); | |
| 2968 | + | |
| 2969 | + fetch(mxchatChat.ajax_url, { | |
| 2970 | + method: 'POST', | |
| 2971 | + headers: { | |
| 2972 | + 'Content-Type': 'application/x-www-form-urlencoded', | |
| 2973 | + }, | |
| 2974 | + body: new URLSearchParams({ | |
| 2975 | + action: 'mxchat_check_email_provided', | |
| 2976 | + session_id: sessionId, | |
| 2977 | + nonce: mxchatChat.nonce, | |
| 2978 | + }) | |
| 2979 | + }) | |
| 2980 | + .then((response) => { | |
| 2981 | + if (!response.ok) { | |
| 2982 | + throw new Error(`HTTP error! status: ${response.status}`); | |
| 2983 | + } | |
| 2984 | + return response.json(); | |
| 2985 | + }) | |
| 2986 | + .then((data) => { | |
| 2987 | + if (data.success) { | |
| 2988 | + if (data.data.logged_in || data.data.email) { | |
| 2989 | + showChatContainer(); | |
| 2990 | + } else { | |
| 2991 | + showEmailForm(); | |
| 2992 | + } | |
| 2993 | + } else { | |
| 2994 | + // On error, default to showing email form | |
| 2995 | + showEmailForm(); | |
| 2996 | + } | |
| 2997 | + }) | |
| 2998 | + .catch((error) => { | |
| 2999 | + // Email check failed - default to email form | |
| 3000 | + showEmailForm(); | |
| 3001 | + }); | |
| 3568 | 3002 | } |
| 3569 | - }); | |
| 3570 | 3003 | |
| 3571 | - // Initialize email check for all bot instances | |
| 3572 | - // For floating bots: defer until widget is opened (zero passive AJAX) | |
| 3573 | - // For embedded bots: check immediately since the form is visible | |
| 3574 | - $('.mxchat-chatbot-wrapper').each(function() { | |
| 3575 | - var botId = $(this).data('bot-id') || 'default'; | |
| 3576 | - var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 3577 | - | |
| 3578 | - if (emailBlocker) { | |
| 3579 | - if (isEmbeddedBot(botId)) { | |
| 3580 | - // Embedded bots are always visible — check now | |
| 3581 | - resolveEmailState(botId); | |
| 3582 | - } | |
| 3583 | - // Floating bots: handled in the widget open handler | |
| 3584 | - } else if (isEmbeddedBot(botId)) { | |
| 3585 | - // Embedded bot, no email collection — load history with loader | |
| 3586 | - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on'; | |
| 3587 | - if (chatPersistenceEnabled) { | |
| 3588 | - MxChatInstances.ensureSession(botId); | |
| 3589 | - showChatContainerForBot(botId); | |
| 3590 | - } | |
| 3591 | - } | |
| 3592 | - }); | |
| 3004 | + } else { | |
| 3005 | + // Email collection is enabled but essential elements are missing - silently continue | |
| 3006 | + } | |
| 3593 | 3007 | } |
| 3594 | 3008 | |
| 3595 | 3009 | // Open chatbot when pre-chat message is clicked - use class selector for multi-instance |
| 3596 | 3010 | $(document).on('click', '.pre-chat-message', function() { |
| @@ -3598,32 +3012,39 @@ | ||
| 3598 | 3012 | var $chatbot = getElement(botId, 'floating-chatbot'); |
| 3599 | 3013 | if ($chatbot.hasClass('hidden')) { |
| 3600 | 3014 | $chatbot.removeClass('hidden').addClass('visible'); |
| 3601 | 3015 | getElement(botId, 'floating-chatbot-button').addClass('hidden'); |
| 3602 | - handlePreChatDismissal(botId); | |
| 3016 | + $(this).fadeOut(250); // Hide pre-chat message | |
| 3603 | 3017 | disableScroll(); // Disable scroll when chatbot opens |
| 3018 | + } | |
| 3019 | + }); | |
| 3604 | 3020 | |
| 3605 | - // Load chat history for returning visitors (persistence) | |
| 3606 | - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on'; | |
| 3607 | - if (chatPersistenceEnabled) { | |
| 3608 | - MxChatInstances.ensureSession(botId); | |
| 3609 | - } | |
| 3021 | + // Dismiss pre-chat message via close button - handled by event delegation above at line ~2376 | |
| 3022 | + // This is a fallback for legacy support | |
| 3023 | + $(document).on('click', '.close-pre-chat-message', function() { | |
| 3024 | + var botId = getBotIdFromElement(this); | |
| 3025 | + var $preChat = getElement(botId, 'pre-chat-message'); | |
| 3026 | + $preChat.fadeOut(200); // Hide the message | |
| 3610 | 3027 | |
| 3611 | - // Deferred email check — only on first widget open | |
| 3612 | - var emailBlocker = getElementDOM(botId, 'email-blocker'); | |
| 3613 | - var instance = MxChatInstances.get(botId); | |
| 3614 | - if (emailBlocker && !instance.emailCheckDone) { | |
| 3615 | - instance.emailCheckDone = true; | |
| 3616 | - resolveEmailState(botId); | |
| 3617 | - } else if (!emailBlocker) { | |
| 3618 | - showChatContainerForBot(botId); | |
| 3028 | + // Send an AJAX request to set the transient flag for 24 hours | |
| 3029 | + $.ajax({ | |
| 3030 | + url: mxchatChat.ajax_url, | |
| 3031 | + type: 'POST', | |
| 3032 | + data: { | |
| 3033 | + action: 'mxchat_dismiss_pre_chat_message', | |
| 3034 | + _ajax_nonce: mxchatChat.nonce | |
| 3035 | + }, | |
| 3036 | + success: function() { | |
| 3037 | + // Ensure the message is hidden after dismissal | |
| 3038 | + $preChat.hide(); | |
| 3039 | + }, | |
| 3040 | + error: function() { | |
| 3041 | + // Error dismissing pre-chat message - silently continue | |
| 3619 | 3042 | } |
| 3620 | - } | |
| 3043 | + }); | |
| 3621 | 3044 | }); |
| 3622 | 3045 | |
| 3623 | - // Legacy duplicate close handler removed — handled by single event delegation above | |
| 3624 | 3046 | |
| 3625 | - | |
| 3626 | 3047 | function hasQuickQuestions(botId) { |
| 3627 | 3048 | botId = botId || 'default'; |
| 3628 | 3049 | var questionsContainer = getElementDOM(botId, 'mxchat-popular-questions'); |
| 3629 | 3050 | if (!questionsContainer) return false; |
| @@ -3759,11 +3180,18 @@ | ||
| 3759 | 3180 | }); |
| 3760 | 3181 | |
| 3761 | 3182 | // Initialize when document is ready |
| 3762 | 3183 | setFullHeight(); |
| 3184 | + trackOriginatingPage(); | |
| 3763 | 3185 | |
| 3764 | - // Note: trackOriginatingPage() and loadChatHistory() are now deferred | |
| 3765 | - // until the user's first interaction via MxChatInstances.ensureSession() | |
| 3186 | + // Only load chat history if email collection is disabled | |
| 3187 | + if (mxchatChat.email_collection_enabled !== 'on') { | |
| 3188 | + // Load history for all instances | |
| 3189 | + $('.mxchat-chatbot-wrapper').each(function() { | |
| 3190 | + var botId = $(this).data('bot-id') || 'default'; | |
| 3191 | + loadChatHistory(botId); | |
| 3192 | + }); | |
| 3193 | + } | |
| 3766 | 3194 | |
| 3767 | 3195 | // Initialize chat visibility for all instances |
| 3768 | 3196 | $('.mxchat-chatbot-wrapper').each(function() { |
| 3769 | 3197 | var botId = $(this).data('bot-id') || 'default'; |