| @@ -8,13 +8,11 @@ | ||
| 8 | 8 | const $pagination = $('.mxchat-kb-pagination'); |
| 9 | 9 | const $processButton = $('#mxchat-kb-process-selected'); |
| 10 | 10 | const $selectAll = $('#mxchat-kb-select-all'); |
| 11 | 11 | const $selectionCount = $('.mxchat-kb-selection-count'); |
| 12 | - // ACF→PDF extraction is an install-level setting (Knowledge → ACF Fields); | |
| 13 | - // the server reads the stored option — nothing to collect in this modal. | |
| 14 | - | |
| 12 | + | |
| 15 | 13 | // Filter elements |
| 16 | - const $searchInput = $('#mxchat-kb-content-search'); | |
| 14 | + const $searchInput = $('#mxchat-kb-content-search');1 | |
| 17 | 15 | const $typeFilter = $('#mxchat-kb-content-type-filter'); |
| 18 | 16 | const $statusFilter = $('#mxchat-kb-content-status-filter'); |
| 19 | 17 | const $processedFilter = $('#mxchat-kb-processed-filter'); |
| 20 | 18 | |
| @@ -39,111 +37,56 @@ | ||
| 39 | 37 | // Handle import option box clicks (for non-WordPress options) |
| 40 | 38 | $('.mxchat-import-box').on('click', function() { |
| 41 | 39 | const $box = $(this); |
| 42 | 40 | const option = $box.data('option'); |
| 43 | - | |
| 41 | + | |
| 44 | 42 | // Skip if this is the WordPress option (it has its own handler) |
| 45 | 43 | if (option === 'wordpress') { |
| 46 | 44 | return; |
| 47 | 45 | } |
| 48 | - | |
| 46 | + | |
| 49 | 47 | // Update active state |
| 50 | 48 | $('.mxchat-import-box').removeClass('active'); |
| 51 | 49 | $box.addClass('active'); |
| 52 | - | |
| 50 | + | |
| 53 | 51 | // Hide all input areas |
| 54 | - $('#mxchat-url-input-area, #mxchat-content-input-area, #mxchat-pdf-upload-area, #mxchat-document-upload-area, #mxchat-youtube-input-area').hide(); | |
| 55 | - | |
| 56 | - // Hide sitemap-specific sections (but NOT for sitemap option - let detection logic handle it) | |
| 57 | - if (option !== 'sitemap') { | |
| 58 | - $('#mxchat-detected-sitemaps, #mxchat-no-sitemaps, #mxchat-sitemaps-loading').hide(); | |
| 59 | - } | |
| 60 | - | |
| 52 | + $('#mxchat-url-input-area, #mxchat-content-input-area').hide(); | |
| 53 | + | |
| 61 | 54 | // Handle different import options |
| 62 | 55 | switch (option) { |
| 63 | - case 'pdf-url': | |
| 56 | + case 'pdf': | |
| 64 | 57 | case 'sitemap': |
| 65 | 58 | case 'url': |
| 66 | 59 | // Show URL input area with appropriate placeholder |
| 67 | 60 | $('#mxchat-url-input-area').show(); |
| 68 | 61 | $('#sitemap_url').attr('placeholder', $box.data('placeholder')); |
| 69 | - $('#import_type').val(option === 'pdf-url' ? 'pdf' : option); | |
| 70 | - | |
| 62 | + $('#import_type').val(option); | |
| 63 | + | |
| 71 | 64 | // UPDATED: Add or update bot_id hidden field for URL forms |
| 72 | 65 | updateBotIdInForm('#mxchat-url-form'); |
| 73 | - | |
| 66 | + | |
| 74 | 67 | // Update the description text based on the import type |
| 75 | 68 | let descriptionText = ''; |
| 76 | - if (option === 'pdf-url') { | |
| 69 | + if (option === 'pdf') { | |
| 77 | 70 | descriptionText = 'Import a PDF document by entering its URL above. PDFs are processed via cron job. If processing does not start, you can manually process batch 5 pages at a time.'; |
| 78 | 71 | } else if (option === 'sitemap') { |
| 79 | 72 | descriptionText = 'Enter a content-specific sub-sitemap URL, not the sitemap index. Sitemaps are processed via cron job. If processing does not start, you can manually process batch 5 pages at a time.'; |
| 80 | - // Re-show sitemap sections if they were previously loaded | |
| 81 | - const $sitemapsList = $('#mxchat-sitemaps-list'); | |
| 82 | - if ($sitemapsList.children().length > 0) { | |
| 83 | - // Sitemaps were already loaded, just show the container | |
| 84 | - $('#mxchat-detected-sitemaps').show(); | |
| 85 | - } else if ($('#mxchat-no-sitemaps').data('was-shown')) { | |
| 86 | - // No sitemaps message was shown before | |
| 87 | - $('#mxchat-no-sitemaps').show(); | |
| 88 | - } | |
| 89 | - // Note: If neither condition is true, initSitemapDetection will show loading state | |
| 90 | 73 | } else if (option === 'url') { |
| 91 | 74 | descriptionText = 'Import content from any webpage by entering its URL.'; |
| 92 | 75 | } |
| 93 | 76 | $('#url-description-text').text(descriptionText); |
| 94 | 77 | break; |
| 95 | - | |
| 78 | + | |
| 96 | 79 | case 'content': |
| 97 | 80 | // Show content input area |
| 98 | 81 | $('#mxchat-content-input-area').show(); |
| 99 | - | |
| 82 | + | |
| 100 | 83 | // UPDATED: Add or update bot_id hidden field for content forms |
| 101 | 84 | updateBotIdInForm('#mxchat-content-form'); |
| 102 | 85 | break; |
| 103 | - | |
| 104 | - case 'pdf-upload': | |
| 105 | - // Show PDF file upload area | |
| 106 | - $('#mxchat-pdf-upload-area').show(); | |
| 107 | - | |
| 108 | - // Add or update bot_id hidden field for PDF upload form | |
| 109 | - updateBotIdInForm('#mxchat-pdf-upload-form'); | |
| 110 | - break; | |
| 111 | - | |
| 112 | - case 'document-upload': | |
| 113 | - // Show document (.docx/.txt/.md) upload area | |
| 114 | - $('#mxchat-document-upload-area').show(); | |
| 115 | - | |
| 116 | - // Add or update bot_id hidden field for the document upload form | |
| 117 | - updateBotIdInForm('#mxchat-document-upload-form'); | |
| 118 | - break; | |
| 119 | - | |
| 120 | - case 'youtube': | |
| 121 | - // Show YouTube import area | |
| 122 | - $('#mxchat-youtube-input-area').show(); | |
| 123 | - | |
| 124 | - // Add or update bot_id hidden field for YouTube form | |
| 125 | - updateBotIdInForm('#mxchat-youtube-form'); | |
| 126 | - break; | |
| 127 | 86 | } |
| 128 | 87 | }); |
| 129 | 88 | |
| 130 | -// YouTube import: toggle the manual-description box with the mode radios, and | |
| 131 | -// only require the textarea when "Write my own" is selected. | |
| 132 | -$(document).on('change', 'input[name="youtube_description_mode"]', function() { | |
| 133 | - const manual = $('input[name="youtube_description_mode"]:checked').val() === 'manual'; | |
| 134 | - $('#mxchat-youtube-description-field').toggle(manual); | |
| 135 | - $('#mxchat-youtube-description').prop('required', manual); | |
| 136 | -}); | |
| 137 | - | |
| 138 | -// If the server bounced back with prefill state (auto-import found no | |
| 139 | -// transcript), reopen the YouTube form in manual mode ready to augment. | |
| 140 | -if ($('#mxchat-youtube-form').data('yt-prefill')) { | |
| 141 | - $('.mxchat-import-box[data-option="youtube"]').trigger('click'); | |
| 142 | - $('input[name="youtube_description_mode"]').trigger('change'); | |
| 143 | - $('#mxchat-youtube-description').trigger('focus'); | |
| 144 | -} | |
| 145 | - | |
| 146 | 89 | // Helper function to add/update bot_id hidden field in forms |
| 147 | 90 | function updateBotIdInForm(formSelector) { |
| 148 | 91 | const $form = $(formSelector); |
| 149 | 92 | if ($form.length === 0) return; |
| @@ -174,9 +117,9 @@ | ||
| 174 | 117 | const data = { |
| 175 | 118 | action: 'mxchat_get_content_list', |
| 176 | 119 | nonce: mxchatSelector.nonce, |
| 177 | 120 | page: currentPage, |
| 178 | - per_page: 100, | |
| 121 | + per_page: 50, | |
| 179 | 122 | search: $searchInput.val(), |
| 180 | 123 | post_type: $typeFilter.val(), |
| 181 | 124 | post_status: $statusFilter.val(), |
| 182 | 125 | processed_filter: $processedFilter.val() |
| @@ -241,22 +184,18 @@ | ||
| 241 | 184 | |
| 242 | 185 | // Render content items |
| 243 | 186 | function renderContentItems(items) { |
| 244 | 187 | let html = ''; |
| 245 | - | |
| 188 | + | |
| 246 | 189 | items.forEach(function(item) { |
| 247 | 190 | const isSelected = selectedItems.has(item.id); |
| 248 | 191 | const isProcessed = item.already_processed; |
| 249 | - const chunkCount = item.chunk_count || 0; | |
| 250 | - | |
| 251 | - // Updated badge text - include chunk count if > 1 | |
| 252 | - let badgeText = 'Not In Knowledge Base'; | |
| 253 | - if (isProcessed) { | |
| 254 | - badgeText = chunkCount > 1 ? `In Knowledge Base (${chunkCount} chunks)` : 'In Knowledge Base'; | |
| 255 | - } | |
| 192 | + | |
| 193 | + // Updated badge text | |
| 194 | + const badgeText = isProcessed ? 'In Knowledge Base' : 'Not In Knowledge Base'; | |
| 256 | 195 | const badgeClass = isProcessed ? 'mxchat-kb-processed-badge' : 'mxchat-kb-unprocessed-badge'; |
| 257 | - | |
| 258 | - | |
| 196 | + | |
| 197 | + | |
| 259 | 198 | html += ` |
| 260 | 199 | <div class="mxchat-kb-content-item ${isProcessed ? 'processed' : ''}" data-id="${item.id}"> |
| 261 | 200 | <div class="mxchat-kb-content-checkbox"> |
| 262 | 201 | <input type="checkbox" id="content-${item.id}" ${isSelected ? 'checked' : ''}> |
| @@ -276,9 +215,9 @@ | ||
| 276 | 215 | </div> |
| 277 | 216 | </div> |
| 278 | 217 | `; |
| 279 | 218 | }); |
| 280 | - | |
| 219 | + | |
| 281 | 220 | $contentList.html(html); |
| 282 | 221 | |
| 283 | 222 | // Add event listeners for checkboxes using delegation for better performance |
| 284 | 223 | $contentList.off('change', 'input[type="checkbox"]').on('change', 'input[type="checkbox"]', function() { |
| @@ -351,36 +290,18 @@ | ||
| 351 | 290 | function updateSelection() { |
| 352 | 291 | const selectedCount = selectedItems.size; |
| 353 | 292 | $selectionCount.text(selectedCount + ' ' + (selectedCount === 1 ? 'selected' : 'selected')); |
| 354 | 293 | $('.mxchat-kb-selected-count').text('(' + selectedCount + ')'); |
| 355 | - | |
| 356 | - // Show/hide clear all selections link | |
| 357 | - let $clearAllLink = $('.mxchat-kb-clear-all-selections'); | |
| 358 | - if (selectedCount > 0) { | |
| 359 | - if ($clearAllLink.length === 0) { | |
| 360 | - $clearAllLink = $('<a href="#" class="mxchat-kb-clear-all-selections" style="margin-left: 10px; font-size: 12px; color: var(--mxch-error, #dc2626);">Clear all</a>'); | |
| 361 | - $selectionCount.after($clearAllLink); | |
| 362 | - $clearAllLink.on('click', function(e) { | |
| 363 | - e.preventDefault(); | |
| 364 | - selectedItems.clear(); | |
| 365 | - $('.mxchat-kb-content-item input[type="checkbox"]').prop('checked', false); | |
| 366 | - updateSelection(); | |
| 367 | - }); | |
| 368 | - } | |
| 369 | - $clearAllLink.show(); | |
| 370 | - } else { | |
| 371 | - $clearAllLink.hide(); | |
| 372 | - } | |
| 373 | - | |
| 294 | + | |
| 374 | 295 | // Determine if any selected items are already processed |
| 375 | 296 | const hasProcessedItems = Array.from(selectedItems).some(id => { |
| 376 | 297 | const item = allItems.find(item => item.id === id); |
| 377 | 298 | return item && item.already_processed; |
| 378 | 299 | }); |
| 379 | - | |
| 300 | + | |
| 380 | 301 | if (selectedCount > 0) { |
| 381 | 302 | $processButton.prop('disabled', false); |
| 382 | - | |
| 303 | + | |
| 383 | 304 | // Update button text based on selection |
| 384 | 305 | if (hasProcessedItems && selectedCount === 1) { |
| 385 | 306 | $processButton.text('Update Selected Content (1)').addClass('update-mode'); |
| 386 | 307 | } else if (hasProcessedItems && selectedCount > 1) { |
| @@ -392,9 +313,9 @@ | ||
| 392 | 313 | $processButton.prop('disabled', true); |
| 393 | 314 | $processButton.text('Process Selected Content').removeClass('update-mode mixed-mode'); |
| 394 | 315 | $('.mxchat-kb-selected-count').text('(0)'); |
| 395 | 316 | } |
| 396 | - | |
| 317 | + | |
| 397 | 318 | updateSelectAllState(); |
| 398 | 319 | } |
| 399 | 320 | |
| 400 | 321 | // Update "Select All" checkbox state |
| @@ -482,13 +403,9 @@ | ||
| 482 | 403 | |
| 483 | 404 | // UPDATED: Get current bot_id for WordPress content processing |
| 484 | 405 | const currentBotId = $('#mxchat-bot-selector').val(); |
| 485 | 406 | |
| 486 | - // Flag to track if processing should be aborted | |
| 487 | - let abortProcessing = false; | |
| 488 | - let currentXHR = null; | |
| 489 | - | |
| 490 | - // Create a modal to show progress with stop button | |
| 407 | + // Create a modal to show progress | |
| 491 | 408 | const $progressModal = $('<div class="mxchat-kb-processing-overlay">' + |
| 492 | 409 | '<div class="mxchat-kb-processing-content">' + |
| 493 | 410 | '<h3>Processing Content</h3>' + |
| 494 | 411 | '<p class="mxchat-kb-processing-status">Processing 1 of ' + totalToProcess + '...</p>' + |
| @@ -493,48 +410,31 @@ | ||
| 493 | 410 | '<h3>Processing Content</h3>' + |
| 494 | 411 | '<p class="mxchat-kb-processing-status">Processing 1 of ' + totalToProcess + '...</p>' + |
| 495 | 412 | '<div class="mxchat-kb-progress-bar"><div class="mxchat-kb-progress-fill" style="width: 0%"></div></div>' + |
| 496 | 413 | '<p class="mxchat-kb-current-item"></p>' + |
| 497 | - '<button type="button" class="mxchat-kb-stop-processing mxch-btn mxch-btn-secondary" style="margin-top: 15px;">' + | |
| 498 | - '<span class="dashicons dashicons-controls-pause" style="margin-right: 5px;"></span>Stop Processing</button>' + | |
| 499 | 414 | '</div>' + |
| 500 | 415 | '</div>'); |
| 501 | - | |
| 416 | + | |
| 502 | 417 | $('body').append($progressModal); |
| 503 | - | |
| 504 | - // Handle stop button click | |
| 505 | - $progressModal.find('.mxchat-kb-stop-processing').on('click', function() { | |
| 506 | - abortProcessing = true; | |
| 507 | - $(this).prop('disabled', true).html('<span class="dashicons dashicons-update spin" style="margin-right: 5px;"></span>Stopping...'); | |
| 508 | - if (currentXHR) { | |
| 509 | - currentXHR.abort(); | |
| 510 | - } | |
| 511 | - }); | |
| 512 | 418 | |
| 513 | 419 | // Process posts one by one |
| 514 | 420 | function processNext(index) { |
| 515 | - // Check if processing was aborted | |
| 516 | - if (abortProcessing) { | |
| 517 | - finishProcessing(true); // Pass true to indicate abort | |
| 518 | - return; | |
| 519 | - } | |
| 520 | - | |
| 521 | 421 | if (index >= selectedPostIds.length) { |
| 522 | 422 | // All done |
| 523 | 423 | finishProcessing(); |
| 524 | 424 | return; |
| 525 | 425 | } |
| 526 | - | |
| 426 | + | |
| 527 | 427 | const postId = selectedPostIds[index]; |
| 528 | 428 | const percent = Math.round((index / totalToProcess) * 100); |
| 529 | 429 | const item = allItems.find(item => item.id === postId); |
| 530 | 430 | const isUpdate = item && item.already_processed; |
| 531 | - | |
| 431 | + | |
| 532 | 432 | // Update progress UI |
| 533 | 433 | $progressModal.find('.mxchat-kb-processing-status') |
| 534 | 434 | .text((isUpdate ? 'Updating' : 'Processing') + ' ' + (index + 1) + ' of ' + totalToProcess + '...'); |
| 535 | 435 | $progressModal.find('.mxchat-kb-progress-fill').css('width', percent + '%'); |
| 536 | - | |
| 436 | + | |
| 537 | 437 | // UPDATED: Prepare AJAX data with bot_id |
| 538 | 438 | const ajaxData = { |
| 539 | 439 | action: 'mxchat_process_selected_content', |
| 540 | 440 | nonce: mxchatSelector.nonce, |
| @@ -540,16 +440,16 @@ | ||
| 540 | 440 | nonce: mxchatSelector.nonce, |
| 541 | 441 | post_ids: [postId], |
| 542 | 442 | is_update: isUpdate |
| 543 | 443 | }; |
| 544 | - | |
| 444 | + | |
| 545 | 445 | // Add bot_id if multi-bot is active and not default |
| 546 | 446 | if (currentBotId && currentBotId !== 'default') { |
| 547 | 447 | ajaxData.bot_id = currentBotId; |
| 548 | 448 | } |
| 549 | - | |
| 550 | - // Make AJAX request for this post (store reference for potential abort) | |
| 551 | - currentXHR = $.ajax({ | |
| 449 | + | |
| 450 | + // Make AJAX request for this post | |
| 451 | + $.ajax({ | |
| 552 | 452 | url: mxchatSelector.ajaxurl, |
| 553 | 453 | method: 'POST', |
| 554 | 454 | data: ajaxData, |
| 555 | 455 | dataType: 'json', |
| @@ -568,15 +468,10 @@ | ||
| 568 | 468 | title: response.data.title || ('ID: ' + postId) |
| 569 | 469 | }); |
| 570 | 470 | } |
| 571 | 471 | |
| 572 | - let successText = 'Successfully ' + (isUpdate ? 'updated' : 'processed') + ': ' + response.data.title; | |
| 573 | - const pdfCount = parseInt(response.data.pdf_extracted_count, 10) || 0; | |
| 574 | - if (pdfCount > 0) { | |
| 575 | - const suffixTpl = (mxchatSelector.i18n && mxchatSelector.i18n.pdfExtractedSuffix) || ' (%d PDF(s) extracted)'; | |
| 576 | - successText += suffixTpl.replace('%d', pdfCount); | |
| 577 | - } | |
| 578 | - $progressModal.find('.mxchat-kb-current-item').text(successText); | |
| 472 | + $progressModal.find('.mxchat-kb-current-item') | |
| 473 | + .text('Successfully ' + (isUpdate ? 'updated' : 'processed') + ': ' + response.data.title); | |
| 579 | 474 | } else { |
| 580 | 475 | failed++; |
| 581 | 476 | results.failed.push({ |
| 582 | 477 | id: postId, |
| @@ -610,32 +505,23 @@ | ||
| 610 | 505 | }); |
| 611 | 506 | } |
| 612 | 507 | |
| 613 | 508 | // Function to finish processing and show results |
| 614 | - function finishProcessing(wasAborted) { | |
| 509 | + function finishProcessing() { | |
| 615 | 510 | // Remove progress modal |
| 616 | 511 | $progressModal.remove(); |
| 617 | - | |
| 512 | + | |
| 618 | 513 | // Determine notification type based on results |
| 619 | 514 | let notificationClass = 'success'; |
| 620 | - if (wasAborted) { | |
| 621 | - notificationClass = processed > 0 || updated > 0 ? 'warning' : 'info'; | |
| 622 | - } else if (failed > 0) { | |
| 515 | + if (failed > 0) { | |
| 623 | 516 | notificationClass = processed > 0 || updated > 0 ? 'warning' : 'error'; |
| 624 | 517 | } |
| 625 | - | |
| 518 | + | |
| 626 | 519 | // Create summary message |
| 627 | - let resultHTML = '<div class="mxchat-kb-notification ' + notificationClass + '">' + | |
| 520 | + let resultHTML = '<div class="mxchat-kb-notification ' + notificationClass + '">' + | |
| 628 | 521 | '<h4>'; |
| 629 | - | |
| 630 | - if (wasAborted) { | |
| 631 | - resultHTML += 'Processing stopped. '; | |
| 632 | - if (processed > 0 || updated > 0) { | |
| 633 | - resultHTML += 'Completed ' + (processed + updated) + ' of ' + totalToProcess + ' items before stopping'; | |
| 634 | - } else { | |
| 635 | - resultHTML += 'No items were processed before stopping'; | |
| 636 | - } | |
| 637 | - } else if (processed > 0 && updated > 0) { | |
| 522 | + | |
| 523 | + if (processed > 0 && updated > 0) { | |
| 638 | 524 | resultHTML += 'Processed ' + processed + ' new items and updated ' + updated + ' existing items'; |
| 639 | 525 | } else if (processed > 0) { |
| 640 | 526 | resultHTML += 'Processed ' + processed + ' items successfully'; |
| 641 | 527 | } else if (updated > 0) { |
| @@ -642,10 +528,10 @@ | ||
| 642 | 528 | resultHTML += 'Updated ' + updated + ' items successfully'; |
| 643 | 529 | } else { |
| 644 | 530 | resultHTML += 'No items were processed successfully'; |
| 645 | 531 | } |
| 646 | - | |
| 647 | - if (failed > 0 && !wasAborted) { | |
| 532 | + | |
| 533 | + if (failed > 0) { | |
| 648 | 534 | resultHTML += ' with ' + failed + ' failures'; |
| 649 | 535 | } |
| 650 | 536 | |
| 651 | 537 | resultHTML += '</h4>'; |
| @@ -675,20 +561,21 @@ | ||
| 675 | 561 | $button.prop('disabled', false) |
| 676 | 562 | .text('Process Selected Content') |
| 677 | 563 | .removeClass('update-mode mixed-mode'); |
| 678 | 564 | $('.mxchat-kb-selected-count').text('(0)'); |
| 679 | - | |
| 565 | + | |
| 680 | 566 | // Only reload if there were successful operations |
| 681 | 567 | if (processed > 0 || updated > 0) { |
| 682 | - // Refresh the knowledge base table with properly grouped entries | |
| 683 | - if (typeof window.refreshKnowledgeBaseTable === 'function') { | |
| 684 | - window.refreshKnowledgeBaseTable(); | |
| 685 | - } | |
| 686 | - | |
| 687 | - // Reload content list to update "already processed" status | |
| 568 | + // Reload after delay | |
| 688 | 569 | setTimeout(function() { |
| 689 | - loadContent(); | |
| 690 | - }, 1000); | |
| 570 | + // Reload the knowledge base table - if this function exists | |
| 571 | + if (typeof reloadKnowledgeBaseTable === 'function') { | |
| 572 | + reloadKnowledgeBaseTable(); | |
| 573 | + } else { | |
| 574 | + // Fallback: reload content list instead of full page reload | |
| 575 | + loadContent(); | |
| 576 | + } | |
| 577 | + }, 3000); | |
| 691 | 578 | } |
| 692 | 579 | } |
| 693 | 580 | |
| 694 | 581 | // Start processing the first post |
| @@ -698,150 +585,91 @@ | ||
| 698 | 585 | // Initialize - Set WordPress as the active option by default |
| 699 | 586 | $('.mxchat-import-box[data-option="wordpress"]').addClass('active'); |
| 700 | 587 | }); |
| 701 | 588 | |
| 702 | -// Navigation functionality for Knowledge Base page | |
| 589 | +// Tab switching functionality - Keep this separate | |
| 703 | 590 | jQuery(document).ready(function($) { |
| 704 | - | |
| 705 | - // Hook into the new navigation system using .mxch-nav-link | |
| 706 | - $(document).on('click', '.mxch-nav-link[data-target], .mxch-mobile-nav-link[data-target]', function() { | |
| 707 | - var target = $(this).data('target'); | |
| 708 | - | |
| 709 | - // Initialize Pinecone functionality when Pinecone section is activated | |
| 710 | - if (target === 'pinecone') { | |
| 711 | - setTimeout(function() { | |
| 712 | - if (typeof initPineconeFeatures === 'function') { | |
| 713 | - initPineconeFeatures(); | |
| 714 | - } | |
| 715 | - }, 100); | |
| 591 | + | |
| 592 | +// Tab switching functionality | |
| 593 | +// Modified tab switching to check for Pinecone changes | |
| 594 | +$('.mxchat-kb-tab-button').on('click', function() { | |
| 595 | + var tabId = $(this).data('tab'); | |
| 596 | + var $button = $(this); | |
| 597 | + | |
| 598 | + // Switch tabs immediately for all tabs | |
| 599 | + $('.mxchat-kb-tab-button').removeClass('active'); | |
| 600 | + $('.mxchat-kb-tab-content').removeClass('active'); | |
| 601 | + $button.addClass('active'); | |
| 602 | + $('#mxchat-kb-tab-' + tabId).addClass('active'); | |
| 603 | + | |
| 604 | + // Check if Pinecone was changed and we're going to import tab | |
| 605 | + if (tabId === 'import' && sessionStorage.getItem('mxchat_pinecone_changed') === 'true') { | |
| 606 | + sessionStorage.removeItem('mxchat_pinecone_changed'); | |
| 607 | + | |
| 608 | + // Show refresh notice | |
| 609 | + var $knowledgeCard = $('#mxchat-kb-tab-import .mxchat-card').eq(1); | |
| 610 | + if ($knowledgeCard.length > 0 && $knowledgeCard.find('.notice-warning').length === 0) { | |
| 611 | + var refreshNotice = $('<div class="notice notice-warning" style="margin: 15px 0; padding: 10px 15px;">' + | |
| 612 | + '<p style="margin: 0;">' + | |
| 613 | + '<span class="dashicons dashicons-info" style="color: #f0ad4e; margin-right: 5px;"></span>' + | |
| 614 | + 'Database settings have changed. ' + | |
| 615 | + '<a href="#" onclick="location.reload(); return false;" style="font-weight: bold;">Click here to refresh</a> to see the updated knowledge base.' + | |
| 616 | + '</p></div>'); | |
| 617 | + | |
| 618 | + $knowledgeCard.prepend(refreshNotice); | |
| 716 | 619 | } |
| 717 | - | |
| 718 | - // Initialize OpenAI Vector Store functionality when Vector Store section is activated | |
| 719 | - if (target === 'openai-vectorstore') { | |
| 720 | - setTimeout(function() { | |
| 721 | - if (typeof initVectorStoreFeatures === 'function') { | |
| 722 | - initVectorStoreFeatures(); | |
| 723 | - } | |
| 724 | - }, 100); | |
| 725 | - } | |
| 726 | - | |
| 727 | - // Check if Pinecone was changed and we're going to import section | |
| 728 | - if (target === 'import' && sessionStorage.getItem('mxchat_pinecone_changed') === 'true') { | |
| 729 | - sessionStorage.removeItem('mxchat_pinecone_changed'); | |
| 730 | - | |
| 731 | - // Show refresh notice | |
| 732 | - var $knowledgeCard = $('#import .mxch-card').eq(1); | |
| 733 | - if ($knowledgeCard.length > 0 && $knowledgeCard.find('.notice-warning').length === 0) { | |
| 734 | - var refreshNotice = $('<div class="notice notice-warning" style="margin: 15px 0; padding: 10px 15px;">' + | |
| 735 | - '<p style="margin: 0;">' + | |
| 736 | - '<span class="dashicons dashicons-info" style="color: #f0ad4e; margin-right: 5px;"></span>' + | |
| 737 | - 'Database settings have changed. ' + | |
| 738 | - '<a href="#" onclick="location.reload(); return false;" style="font-weight: bold;">Click here to refresh</a> to see the updated knowledge base.' + | |
| 739 | - '</p></div>'); | |
| 740 | - | |
| 741 | - $knowledgeCard.prepend(refreshNotice); | |
| 742 | - } | |
| 743 | - } | |
| 744 | - }); | |
| 745 | - | |
| 746 | - // Also check on page load if we're already on one of these sections | |
| 747 | - setTimeout(function() { | |
| 748 | - if ($('#pinecone').is(':visible') || $('#pinecone.active').length > 0) { | |
| 749 | - if (typeof initPineconeFeatures === 'function') { | |
| 750 | - initPineconeFeatures(); | |
| 751 | - } | |
| 752 | - } | |
| 753 | - | |
| 754 | - if ($('#openai-vectorstore').is(':visible') || $('#openai-vectorstore.active').length > 0) { | |
| 755 | - if (typeof initVectorStoreFeatures === 'function') { | |
| 756 | - initVectorStoreFeatures(); | |
| 757 | - } | |
| 758 | - } | |
| 759 | - }, 200); | |
| 620 | + } | |
| 621 | + | |
| 622 | + // Initialize Pinecone functionality when Pinecone tab is activated | |
| 623 | + if (tabId === 'pinecone') { | |
| 624 | + setTimeout(function() { | |
| 625 | + initPineconeFeatures(); | |
| 626 | + }, 100); | |
| 627 | + } | |
| 760 | 628 | }); |
| 761 | 629 | |
| 762 | -// Pinecone and Vector Store functionality - global functions | |
| 763 | -var initPineconeFeatures, initVectorStoreFeatures; | |
| 764 | 630 | |
| 765 | -(function($) { | |
| 766 | - | |
| 767 | - // Helper function to update sidebar badge when integration is toggled | |
| 768 | - function updateSidebarBadge(section, isActive) { | |
| 769 | - // Find the nav item for this section (both desktop and mobile) | |
| 770 | - var $desktopNavItem = $('.mxch-nav-item[data-section="' + section + '"] .mxch-nav-link'); | |
| 771 | - var $mobileNavItem = $('.mxch-mobile-nav-link[data-target="' + section + '"]'); | |
| 772 | - | |
| 773 | - // Remove existing badge if any | |
| 774 | - $desktopNavItem.find('.mxch-active-badge').remove(); | |
| 775 | - $mobileNavItem.find('.mxch-active-badge').remove(); | |
| 776 | - | |
| 777 | - // Add badge if active | |
| 778 | - if (isActive) { | |
| 779 | - var badgeHtml = '<span class="mxch-nav-link-badge mxch-active-badge">Active</span>'; | |
| 780 | - $desktopNavItem.append(badgeHtml); | |
| 781 | - $mobileNavItem.append(badgeHtml); | |
| 782 | - } | |
| 783 | - } | |
| 784 | - | |
| 785 | 631 | // Pinecone functionality |
| 786 | - initPineconeFeatures = function() { | |
| 787 | - // Check for either old or new section ID | |
| 788 | - if ($('#pinecone').length === 0 && $('#mxchat-kb-tab-pinecone').length === 0) { | |
| 632 | + function initPineconeFeatures() { | |
| 633 | + //console.log('Initializing Pinecone features...'); | |
| 634 | + | |
| 635 | + if ($('#mxchat-kb-tab-pinecone').length === 0) { | |
| 789 | 636 | return; |
| 790 | 637 | } |
| 791 | - | |
| 638 | + | |
| 792 | 639 | initPineconeToggle(); |
| 793 | 640 | initPineconeConnectionTest(); |
| 794 | 641 | checkPineconeCompatibility(); |
| 795 | - }; | |
| 796 | - | |
| 642 | + } | |
| 643 | + | |
| 797 | 644 | function initPineconeToggle() { |
| 798 | 645 | // Remove any existing handlers to prevent duplicates |
| 799 | - var $toggleInput = $('input[name="mxchat_pinecone_addon_options[mxchat_use_pinecone]"]'); | |
| 800 | - $toggleInput.off('change.pineconeToggle'); | |
| 801 | - | |
| 802 | - // Ensure the success notice exists in the settings div (add if not present) | |
| 803 | - // Check for both the JS-added class and any existing PHP-rendered success notice | |
| 804 | - var settingsDiv = $('.mxchat-pinecone-settings'); | |
| 805 | - if (settingsDiv.length > 0 && settingsDiv.find('.mxch-notice-success').length === 0) { | |
| 806 | - var successNotice = $('<div class="mxch-notice mxch-notice-success mxchat-pinecone-enabled-notice" style="margin-bottom: 20px; display: none;">' + | |
| 807 | - '<svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>' + | |
| 808 | - '<span>Pinecone is enabled. All new knowledge base content will be stored in Pinecone.</span>' + | |
| 809 | - '</div>'); | |
| 810 | - settingsDiv.prepend(successNotice); | |
| 811 | - } | |
| 812 | - | |
| 646 | + $('input[name="mxchat_pinecone_addon_options[mxchat_use_pinecone]"]').off('change.pineconeToggle'); | |
| 647 | + | |
| 813 | 648 | // Add the toggle handler for UI only (auto-save will handle the actual saving) |
| 814 | - $toggleInput.on('change.pineconeToggle', function() { | |
| 649 | + $('input[name="mxchat_pinecone_addon_options[mxchat_use_pinecone]"]').on('change.pineconeToggle', function() { | |
| 815 | 650 | var $checkbox = $(this); |
| 816 | 651 | var isChecked = $checkbox.is(':checked'); |
| 817 | 652 | var settingsDiv = $('.mxchat-pinecone-settings'); |
| 818 | - var enabledNotice = settingsDiv.find('.mxchat-pinecone-enabled-notice, .mxch-notice-success'); | |
| 819 | - | |
| 653 | + | |
| 654 | + //console.log('Pinecone toggle changed to:', isChecked); | |
| 655 | + | |
| 820 | 656 | // Update the UI immediately |
| 821 | 657 | if (isChecked) { |
| 822 | 658 | settingsDiv.slideDown(300); |
| 823 | - enabledNotice.slideDown(300); | |
| 824 | 659 | } else { |
| 825 | - enabledNotice.slideUp(300); | |
| 826 | 660 | settingsDiv.slideUp(300); |
| 827 | 661 | } |
| 828 | - | |
| 829 | - // Update sidebar badge for Pinecone | |
| 830 | - updateSidebarBadge('pinecone', isChecked); | |
| 831 | 662 | }); |
| 832 | - | |
| 663 | + | |
| 833 | 664 | // Set initial state based on current checkbox value |
| 834 | 665 | var currentToggle = $('input[name="mxchat_pinecone_addon_options[mxchat_use_pinecone]"]'); |
| 835 | 666 | if (currentToggle.length > 0) { |
| 836 | 667 | var settingsDiv = $('.mxchat-pinecone-settings'); |
| 837 | - var enabledNotice = settingsDiv.find('.mxchat-pinecone-enabled-notice, .mxch-notice-success'); | |
| 838 | 668 | if (currentToggle.is(':checked')) { |
| 839 | 669 | settingsDiv.show(); |
| 840 | - enabledNotice.show(); | |
| 841 | 670 | } else { |
| 842 | 671 | settingsDiv.hide(); |
| 843 | - enabledNotice.hide(); | |
| 844 | 672 | } |
| 845 | 673 | } |
| 846 | 674 | } |
| 847 | 675 | |
| @@ -938,68 +766,6 @@ | ||
| 938 | 766 | } |
| 939 | 767 | }); |
| 940 | 768 | } |
| 941 | 769 | } |
| 942 | - | |
| 943 | - // ============================================ | |
| 944 | - // OpenAI Vector Store functionality | |
| 945 | - // ============================================ | |
| 946 | - initVectorStoreFeatures = function() { | |
| 947 | - if ($('#openai-vectorstore').length === 0) { | |
| 948 | - return; | |
| 949 | - } | |
| 950 | - | |
| 951 | - initVectorStoreToggle(); | |
| 952 | - }; | |
| 953 | - | |
| 954 | - function initVectorStoreToggle() { | |
| 955 | - // Remove any existing handlers to prevent duplicates | |
| 956 | - var $toggleInput = $('input[name="mxchat_openai_vectorstore_options[mxchat_use_openai_vectorstore]"]'); | |
| 957 | - $toggleInput.off('change.vectorstoreToggle'); | |
| 958 | - | |
| 959 | - // Ensure the success notice exists in the settings div (add if not present) | |
| 960 | - // Check for both the JS-added class and any existing PHP-rendered success notice | |
| 961 | - var settingsDiv = $('.mxchat-vectorstore-settings'); | |
| 962 | - if (settingsDiv.length > 0 && settingsDiv.find('.mxch-notice-success').length === 0) { | |
| 963 | - var successNotice = $('<div class="mxch-notice mxch-notice-success mxchat-vectorstore-enabled-notice" style="margin-bottom: 20px; display: none;">' + | |
| 964 | - '<svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>' + | |
| 965 | - '<span>OpenAI Vector Store is enabled. Queries will search your Vector Store for relevant content.</span>' + | |
| 966 | - '</div>'); | |
| 967 | - settingsDiv.prepend(successNotice); | |
| 968 | - } | |
| 969 | - | |
| 970 | - // Add the toggle handler for UI only (form submit will handle the actual saving) | |
| 971 | - $toggleInput.on('change.vectorstoreToggle', function() { | |
| 972 | - var $checkbox = $(this); | |
| 973 | - var isChecked = $checkbox.is(':checked'); | |
| 974 | - var settingsDiv = $('.mxchat-vectorstore-settings'); | |
| 975 | - var enabledNotice = settingsDiv.find('.mxchat-vectorstore-enabled-notice, .mxch-notice-success'); | |
| 976 | - | |
| 977 | - // Update the UI immediately | |
| 978 | - if (isChecked) { | |
| 979 | - settingsDiv.slideDown(300); | |
| 980 | - enabledNotice.slideDown(300); | |
| 981 | - } else { | |
| 982 | - enabledNotice.slideUp(300); | |
| 983 | - settingsDiv.slideUp(300); | |
| 984 | - } | |
| 985 | - | |
| 986 | - // Update sidebar badge for OpenAI Vector Store | |
| 987 | - updateSidebarBadge('openai-vectorstore', isChecked); | |
| 988 | - }); | |
| 989 | - | |
| 990 | - // Set initial state based on current checkbox value | |
| 991 | - var currentToggle = $('input[name="mxchat_openai_vectorstore_options[mxchat_use_openai_vectorstore]"]'); | |
| 992 | - if (currentToggle.length > 0) { | |
| 993 | - var settingsDiv = $('.mxchat-vectorstore-settings'); | |
| 994 | - var enabledNotice = settingsDiv.find('.mxchat-vectorstore-enabled-notice, .mxch-notice-success'); | |
| 995 | - if (currentToggle.is(':checked')) { | |
| 996 | - settingsDiv.show(); | |
| 997 | - enabledNotice.show(); | |
| 998 | - } else { | |
| 999 | - settingsDiv.hide(); | |
| 1000 | - enabledNotice.hide(); | |
| 1001 | - } | |
| 1002 | - } | |
| 1003 | - } | |
| 1004 | - | |
| 1005 | -})(jQuery); | |
| 770 | + | |
| 771 | +}); | |