| 1 |
jQuery(document).ready(function($) { |
| 2 |
//console.log('Script loaded'); // Confirm script is loading |
| 3 |
|
| 4 |
// --- Tab Navigation and Toggles --- |
| 5 |
|
| 6 |
$('.mxchat-nav-tab').on('click', function(e) { |
| 7 |
e.preventDefault(); |
| 8 |
$('.mxchat-nav-tab').removeClass('mxchat-nav-tab-active'); |
| 9 |
$(this).addClass('mxchat-nav-tab-active'); |
| 10 |
$('.mxchat-tab-content').removeClass('active').hide(); |
| 11 |
var activeTab = $(this).attr('href'); |
| 12 |
$(activeTab).addClass('active').show(); |
| 13 |
}); |
| 14 |
|
| 15 |
// Activate the first tab by default |
| 16 |
$('.mxchat-nav-tab-active').trigger('click'); |
| 17 |
|
| 18 |
// Toggle visibility of various API keys |
| 19 |
function toggleVisibility(selector) { |
| 20 |
$(selector).on('click', function() { |
| 21 |
var inputField = $(this).prev('input'); |
| 22 |
if (inputField.attr('type') === 'password') { |
| 23 |
inputField.attr('type', 'text'); |
| 24 |
$(this).text('Hide'); |
| 25 |
} else { |
| 26 |
inputField.attr('type', 'password'); |
| 27 |
$(this).text('Show'); |
| 28 |
} |
| 29 |
}); |
| 30 |
} |
| 31 |
toggleVisibility('#toggleApiKeyVisibility'); |
| 32 |
toggleVisibility('#toggleWooCommerceSecretVisibility'); |
| 33 |
toggleVisibility('#toggleLoopsApiKeyVisibility'); |
| 34 |
toggleVisibility('#toggleXaiApiKeyVisibility'); |
| 35 |
toggleVisibility('#toggleClaudeApiKeyVisibility'); |
| 36 |
toggleVisibility('#toggleBraveApiKeyVisibility'); |
| 37 |
|
| 38 |
// --- Add Intent Form Submission --- |
| 39 |
|
| 40 |
$('#mxchat-add-intent-form').on('submit', function(event) { |
| 41 |
$('#mxchat-intent-loading').show(); |
| 42 |
$('#mxchat-intent-loading-text').show(); |
| 43 |
$(this).find('button[type="submit"]').hide(); // Hide the submit button to prevent multiple clicks |
| 44 |
}); |
| 45 |
|
| 46 |
// --- Inline Edit Functionality --- |
| 47 |
|
| 48 |
$('.edit-button').on('click', function() { |
| 49 |
var row = $(this).closest('tr'); |
| 50 |
row.find('.content-view, .url-view').hide(); |
| 51 |
row.find('.content-edit, .url-edit').show(); |
| 52 |
row.find('.edit-button').hide(); |
| 53 |
row.find('.save-button').show(); |
| 54 |
}); |
| 55 |
|
| 56 |
$('.save-button').on('click', function() { |
| 57 |
var button = $(this); |
| 58 |
var row = button.closest('tr'); |
| 59 |
var id = button.data('id'); |
| 60 |
var newContent = row.find('.content-edit').val(); |
| 61 |
var newUrl = row.find('.url-edit').val(); |
| 62 |
|
| 63 |
// Send an AJAX request to save the changes |
| 64 |
$.ajax({ |
| 65 |
url: ajaxurl, // ajaxurl is automatically available in WP admin |
| 66 |
type: 'POST', |
| 67 |
data: { |
| 68 |
action: 'mxchat_save_inline_prompt', |
| 69 |
id: id, |
| 70 |
article_content: newContent, |
| 71 |
article_url: newUrl, |
| 72 |
_ajax_nonce: mxchatInlineEdit.nonce // Use localized nonce |
| 73 |
}, |
| 74 |
success: function(response) { |
| 75 |
if (response.success) { |
| 76 |
row.find('.content-view').html(newContent.replace(/\n/g, "<br>")); |
| 77 |
row.find('.url-view a').attr('href', newUrl).text(newUrl); |
| 78 |
row.find('.content-edit, .url-edit').hide(); |
| 79 |
row.find('.content-view, .url-view').show(); |
| 80 |
row.find('.save-button').hide(); |
| 81 |
row.find('.edit-button').show(); |
| 82 |
} else { |
| 83 |
alert('Error saving content.'); |
| 84 |
} |
| 85 |
}, |
| 86 |
error: function() { |
| 87 |
alert('An error occurred.'); |
| 88 |
} |
| 89 |
}); |
| 90 |
}); |
| 91 |
|
| 92 |
// --- Activation Script --- |
| 93 |
|
| 94 |
// Select activation-related elements |
| 95 |
var form = $('#mxchat-activation-form'); |
| 96 |
var spinner = $('#mxchat-activation-spinner'); |
| 97 |
var submitButton = $('#activate_license_button'); |
| 98 |
var licenseStatus = $('#mxchat-license-status'); |
| 99 |
|
| 100 |
// Ensure essential elements exist before running activation-specific code |
| 101 |
if (form.length && licenseStatus.length && submitButton.length) { |
| 102 |
//console.log('Activation elements detected, running activation-specific code.'); |
| 103 |
|
| 104 |
// Function to handle the response from the activation AJAX request |
| 105 |
function handleActivationResponse(response) { |
| 106 |
spinner.hide(); // Hide the spinner |
| 107 |
|
| 108 |
if (response.success) { |
| 109 |
// Update UI on successful activation |
| 110 |
licenseStatus.text('Active'); |
| 111 |
licenseStatus.removeClass('inactive').addClass('active'); |
| 112 |
form.hide(); // Hide the activation form after successful activation |
| 113 |
} else { |
| 114 |
licenseStatus.text('Inactive'); |
| 115 |
alert(response.data || 'Activation failed. Please check your input.'); |
| 116 |
submitButton.prop('disabled', false); // Re-enable button on failure |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// Event listener for form submission to activate the license |
| 121 |
form.on('submit', function(event) { |
| 122 |
event.preventDefault(); // Prevent the default form submission |
| 123 |
//console.log("Activating license..."); |
| 124 |
|
| 125 |
// Show spinner and disable the submit button to prevent multiple submissions |
| 126 |
spinner.show(); |
| 127 |
submitButton.prop('disabled', true); |
| 128 |
|
| 129 |
// Gather form data |
| 130 |
var formData = { |
| 131 |
action: 'mxchat_activate_license', |
| 132 |
mxchat_pro_email: $('#mxchat_pro_email').val(), |
| 133 |
mxchat_activation_key: $('#mxchat_activation_key').val(), |
| 134 |
security: mxchatAdmin.nonce // Ensure this is localized in PHP |
| 135 |
}; |
| 136 |
|
| 137 |
// Send the AJAX request using jQuery |
| 138 |
$.post(mxchatAdmin.ajax_url, formData, function(response) { |
| 139 |
//console.log("Activation response received:", response); |
| 140 |
handleActivationResponse(response); |
| 141 |
}).fail(function() { |
| 142 |
alert('Server error. Please try again.'); |
| 143 |
spinner.hide(); |
| 144 |
submitButton.prop('disabled', false); |
| 145 |
}); |
| 146 |
}); |
| 147 |
} else { |
| 148 |
//console.log('Activation elements not found; skipping activation-specific code.'); |
| 149 |
} |
| 150 |
}); |
| 151 |
|