| 1 |
function mxchatOpenEditModal(intentId, phrases) { |
| 2 |
const modal = document.getElementById('mxchat-edit-modal'); |
| 3 |
if (!modal) return; |
| 4 |
const intentIdField = document.getElementById('edit_intent_id'); |
| 5 |
const phrasesField = document.getElementById('edit_phrases'); |
| 6 |
|
| 7 |
intentIdField.value = intentId; |
| 8 |
phrasesField.value = phrases; |
| 9 |
modal.style.display = 'block'; |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
jQuery(document).ready(function($) { |
| 16 |
//console.log('Script loaded'); // Confirm script is loading |
| 17 |
|
| 18 |
// --- Tab Navigation and Toggles --- |
| 19 |
|
| 20 |
$('.mxchat-nav-tab').on('click', function(e) { |
| 21 |
e.preventDefault(); |
| 22 |
$('.mxchat-nav-tab').removeClass('mxchat-nav-tab-active'); |
| 23 |
$(this).addClass('mxchat-nav-tab-active'); |
| 24 |
$('.mxchat-tab-content').removeClass('active').hide(); |
| 25 |
var activeTab = $(this).attr('href'); |
| 26 |
$(activeTab).addClass('active').show(); |
| 27 |
}); |
| 28 |
|
| 29 |
// Activate the first tab by default |
| 30 |
$('.mxchat-nav-tab-active').trigger('click'); |
| 31 |
|
| 32 |
|
| 33 |
// Attach click event to dynamically call the function |
| 34 |
$(document).on('click', '.mxchat-edit-button', function() { |
| 35 |
const intentId = $(this).data('intent-id'); |
| 36 |
const phrases = $(this).data('phrases'); |
| 37 |
mxchatOpenEditModal(intentId, phrases); |
| 38 |
}); |
| 39 |
|
| 40 |
// Toggle visibility of various API keys |
| 41 |
function toggleVisibility(selector) { |
| 42 |
$(selector).on('click', function() { |
| 43 |
var inputField = $(this).prev('input'); |
| 44 |
if (inputField.attr('type') === 'password') { |
| 45 |
inputField.attr('type', 'text'); |
| 46 |
$(this).text('Hide'); |
| 47 |
} else { |
| 48 |
inputField.attr('type', 'password'); |
| 49 |
$(this).text('Show'); |
| 50 |
} |
| 51 |
}); |
| 52 |
} |
| 53 |
toggleVisibility('#toggleApiKeyVisibility'); |
| 54 |
toggleVisibility('#toggleWooCommerceSecretVisibility'); |
| 55 |
toggleVisibility('#toggleLoopsApiKeyVisibility'); |
| 56 |
toggleVisibility('#toggleXaiApiKeyVisibility'); |
| 57 |
toggleVisibility('#toggleClaudeApiKeyVisibility'); |
| 58 |
toggleVisibility('#toggleBraveApiKeyVisibility'); |
| 59 |
toggleVisibility('#toggleWebhookUrlVisibility'); |
| 60 |
toggleVisibility('#toggleSecretKeyVisibility'); |
| 61 |
toggleVisibility('#toggleBotTokenVisibility'); |
| 62 |
|
| 63 |
// --- Add Intent Form Submission --- |
| 64 |
|
| 65 |
$('#mxchat-add-intent-form').on('submit', function(event) { |
| 66 |
$('#mxchat-intent-loading').show(); |
| 67 |
$('#mxchat-intent-loading-text').show(); |
| 68 |
$(this).find('button[type="submit"]').hide(); // Hide the submit button to prevent multiple clicks |
| 69 |
}); |
| 70 |
|
| 71 |
// --- Inline Edit Functionality --- |
| 72 |
|
| 73 |
$('.edit-button').on('click', function() { |
| 74 |
var row = $(this).closest('tr'); |
| 75 |
row.find('.content-view, .url-view').hide(); |
| 76 |
row.find('.content-edit, .url-edit').show(); |
| 77 |
row.find('.edit-button').hide(); |
| 78 |
row.find('.save-button').show(); |
| 79 |
}); |
| 80 |
|
| 81 |
$('.save-button').on('click', function() { |
| 82 |
var button = $(this); |
| 83 |
var row = button.closest('tr'); |
| 84 |
var id = button.data('id'); |
| 85 |
var newContent = row.find('.content-edit').val(); |
| 86 |
var newUrl = row.find('.url-edit').val(); |
| 87 |
|
| 88 |
// Send an AJAX request to save the changes |
| 89 |
$.ajax({ |
| 90 |
url: ajaxurl, // ajaxurl is automatically available in WP admin |
| 91 |
type: 'POST', |
| 92 |
data: { |
| 93 |
action: 'mxchat_save_inline_prompt', |
| 94 |
id: id, |
| 95 |
article_content: newContent, |
| 96 |
article_url: newUrl, |
| 97 |
_ajax_nonce: mxchatInlineEdit.nonce // Use localized nonce |
| 98 |
}, |
| 99 |
success: function(response) { |
| 100 |
if (response.success) { |
| 101 |
row.find('.content-view').html(newContent.replace(/\n/g, "<br>")); |
| 102 |
row.find('.url-view a').attr('href', newUrl).text(newUrl); |
| 103 |
row.find('.content-edit, .url-edit').hide(); |
| 104 |
row.find('.content-view, .url-view').show(); |
| 105 |
row.find('.save-button').hide(); |
| 106 |
row.find('.edit-button').show(); |
| 107 |
} else { |
| 108 |
alert('Error saving content.'); |
| 109 |
} |
| 110 |
}, |
| 111 |
error: function() { |
| 112 |
alert('An error occurred.'); |
| 113 |
} |
| 114 |
}); |
| 115 |
}); |
| 116 |
|
| 117 |
// --- Activation Script --- |
| 118 |
|
| 119 |
// Select activation-related elements |
| 120 |
var form = $('#mxchat-activation-form'); |
| 121 |
var spinner = $('#mxchat-activation-spinner'); |
| 122 |
var submitButton = $('#activate_license_button'); |
| 123 |
var licenseStatus = $('#mxchat-license-status'); |
| 124 |
|
| 125 |
// Ensure essential elements exist before running activation-specific code |
| 126 |
if (form.length && licenseStatus.length && submitButton.length) { |
| 127 |
//console.log('Activation elements detected, running activation-specific code.'); |
| 128 |
|
| 129 |
// Function to handle the response from the activation AJAX request |
| 130 |
function handleActivationResponse(response) { |
| 131 |
spinner.hide(); // Hide the spinner |
| 132 |
|
| 133 |
if (response.success) { |
| 134 |
// Update UI on successful activation |
| 135 |
licenseStatus.text('Active'); |
| 136 |
licenseStatus.removeClass('inactive').addClass('active'); |
| 137 |
form.hide(); // Hide the activation form after successful activation |
| 138 |
} else { |
| 139 |
licenseStatus.text('Inactive'); |
| 140 |
alert(response.data || 'Activation failed. Please check your input.'); |
| 141 |
submitButton.prop('disabled', false); // Re-enable button on failure |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Event listener for form submission to activate the license |
| 146 |
form.on('submit', function(event) { |
| 147 |
event.preventDefault(); // Prevent the default form submission |
| 148 |
//console.log("Activating license..."); |
| 149 |
|
| 150 |
// Show spinner and disable the submit button to prevent multiple submissions |
| 151 |
spinner.show(); |
| 152 |
submitButton.prop('disabled', true); |
| 153 |
|
| 154 |
// Gather form data |
| 155 |
var formData = { |
| 156 |
action: 'mxchat_activate_license', |
| 157 |
mxchat_pro_email: $('#mxchat_pro_email').val(), |
| 158 |
mxchat_activation_key: $('#mxchat_activation_key').val(), |
| 159 |
security: mxchatAdmin.nonce // Ensure this is localized in PHP |
| 160 |
}; |
| 161 |
|
| 162 |
// Send the AJAX request using jQuery |
| 163 |
$.post(mxchatAdmin.ajax_url, formData, function(response) { |
| 164 |
//console.log("Activation response received:", response); |
| 165 |
handleActivationResponse(response); |
| 166 |
}).fail(function() { |
| 167 |
alert('Server error. Please try again.'); |
| 168 |
spinner.hide(); |
| 169 |
submitButton.prop('disabled', false); |
| 170 |
}); |
| 171 |
}); |
| 172 |
} else { |
| 173 |
//console.log('Activation elements not found; skipping activation-specific code.'); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
// Add new question |
| 180 |
$('.mxchat-add-question').on('click', function () { |
| 181 |
const container = $('#mxchat-additional-questions-container'); |
| 182 |
const questionCount = container.find('.mxchat-question-row').length + 4; |
| 183 |
|
| 184 |
const newQuestion = ` |
| 185 |
<div class="mxchat-question-row"> |
| 186 |
<input type="text" name="mxchat_options[additional_popular_questions][]" placeholder="Enter Additional Popular Question ${questionCount}" /> |
| 187 |
<button type="button" class="button mxchat-remove-question" aria-label="Remove question">Remove</button> |
| 188 |
</div> |
| 189 |
`; |
| 190 |
container.append(newQuestion); |
| 191 |
}); |
| 192 |
|
| 193 |
// Remove a question |
| 194 |
$(document).on('click', '.mxchat-remove-question', function () { |
| 195 |
$(this).closest('.mxchat-question-row').remove(); |
| 196 |
}); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
const statusToggle = document.getElementById('live_agent_status'); |
| 201 |
const statusText = statusToggle?.parentElement.nextElementSibling?.querySelector('.status-text'); |
| 202 |
|
| 203 |
if (statusToggle && statusText) { |
| 204 |
statusToggle.addEventListener('change', function() { |
| 205 |
statusText.textContent = this.checked ? 'Online' : 'Offline'; |
| 206 |
}); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
}); |
| 212 |
|