| 1 |
jQuery(document).ready(function($) { |
| 2 |
// Function to fetch models from Google API |
| 3 |
$(document).on('click', '#qcld_gemini_fetch_models', function(e) { |
| 4 |
e.preventDefault(); |
| 5 |
var api_key = $('#qcld_gemini_api_key').val(); |
| 6 |
if (!api_key) { |
| 7 |
Swal.fire({ |
| 8 |
icon: 'error', |
| 9 |
title: 'Oops...', |
| 10 |
text: 'Please enter your API Key first!' |
| 11 |
}); |
| 12 |
return; |
| 13 |
} |
| 14 |
|
| 15 |
var $btn = $(this); |
| 16 |
var originalText = $btn.text(); |
| 17 |
$btn.text('Fetching...').prop('disabled', true); |
| 18 |
|
| 19 |
$.ajax({ |
| 20 |
url: qcld_gemini_admin_data.ajax_url, |
| 21 |
type: 'POST', |
| 22 |
data: { |
| 23 |
action: 'qcld_gemini_get_model_list', |
| 24 |
nonce: qcld_gemini_admin_data.ajax_nonce, |
| 25 |
api_key: api_key |
| 26 |
}, |
| 27 |
success: function(response) { |
| 28 |
if (response.success) { |
| 29 |
var $select = $('#qcld_gemini_model'); |
| 30 |
var currentModel = $select.val(); |
| 31 |
$select.empty(); |
| 32 |
response.data.models.forEach(function(model) { |
| 33 |
var selected = (model.id === currentModel) ? 'selected' : ''; |
| 34 |
$select.append('<option value="' + model.id + '" ' + selected + '>' + model.name + ' (' + model.id + ')</option>'); |
| 35 |
}); |
| 36 |
Swal.fire({ |
| 37 |
icon: 'success', |
| 38 |
title: 'Success', |
| 39 |
text: 'Models fetched successfully!' |
| 40 |
}); |
| 41 |
} else { |
| 42 |
Swal.fire({ |
| 43 |
icon: 'error', |
| 44 |
title: 'Error', |
| 45 |
text: response.data.msg || 'Failed to fetch models' |
| 46 |
}); |
| 47 |
} |
| 48 |
}, |
| 49 |
error: function() { |
| 50 |
Swal.fire({ |
| 51 |
icon: 'error', |
| 52 |
title: 'Error', |
| 53 |
text: 'An error occurred while fetching models.' |
| 54 |
}); |
| 55 |
}, |
| 56 |
complete: function() { |
| 57 |
$btn.text(originalText).prop('disabled', false); |
| 58 |
} |
| 59 |
}); |
| 60 |
}); |
| 61 |
|
| 62 |
// Function to fetch models from OpenRouter API |
| 63 |
|
| 64 |
var settingsGemini = document.getElementById("qcld_save_gemini_setting"); |
| 65 |
if(settingsGemini){ |
| 66 |
$('.qcl-openai').on('click', '#qcld_save_gemini_setting', function(){ |
| 67 |
if ($('#qcld_gemini_enabled').is(":checked")){ |
| 68 |
var gemini_enabled = 1; |
| 69 |
}else{ |
| 70 |
var gemini_enabled = 0; |
| 71 |
} |
| 72 |
var qcld_gemini_page_suggestion_enabled = jQuery("#qcld_gemini_page_suggestion_enabled").is(":checked") ? 1 : 0; |
| 73 |
var gemini_is_context_awareness_enabled = jQuery("#gemini_is_context_awareness_enabled").is(":checked") ? 1 : 0; |
| 74 |
var qcld_gemini_api_key = jQuery("#qcld_gemini_api_key").val(); |
| 75 |
var qcld_gemini_model = jQuery('#qcld_gemini_model').val(); |
| 76 |
var qcld_gemini_append_content = jQuery('#qcld_gemini_append_content').val(); |
| 77 |
var qcld_gemini_prepend_content = jQuery('#qcld_gemini_prepend_content').val(); |
| 78 |
var is_page_rag_enabled = jQuery("#is_page_rag_enabled_gemini").is(":checked") ? 1 : 0; |
| 79 |
var post_gemini_types = $.map($('input[name="site_gemini_search_posttypes[]"]:checked'), function(c){return c.value; }); |
| 80 |
Swal.fire({ |
| 81 |
html: '<div class="qcld-save-settings-spinner"></div>', |
| 82 |
allowOutsideClick: false, |
| 83 |
allowEscapeKey: false, |
| 84 |
showConfirmButton: false, |
| 85 |
background: 'transparent', |
| 86 |
customClass: { |
| 87 |
popup: 'qcld-swal-loading-only' |
| 88 |
} |
| 89 |
}); |
| 90 |
$.ajax({ |
| 91 |
url: qcld_gemini_admin_data.ajax_url, |
| 92 |
type:'POST', |
| 93 |
data: { |
| 94 |
action: 'qcld_gemini_settings_option', |
| 95 |
nonce: qcld_gemini_admin_data.ajax_nonce, |
| 96 |
gemini_api_key: qcld_gemini_api_key, |
| 97 |
gemini_model: qcld_gemini_model, |
| 98 |
gemini_enabled: gemini_enabled, |
| 99 |
qcld_gemini_page_suggestion_enabled: qcld_gemini_page_suggestion_enabled, |
| 100 |
gemini_is_context_awareness_enabled: gemini_is_context_awareness_enabled, |
| 101 |
qcld_gemini_append_content: qcld_gemini_append_content, |
| 102 |
qcld_gemini_prepend_content: qcld_gemini_prepend_content, |
| 103 |
openai_post_type:post_gemini_types, |
| 104 |
is_page_rag_enabled: is_page_rag_enabled |
| 105 |
}, |
| 106 |
success: function(data){ |
| 107 |
$('#result').html(data); |
| 108 |
var color = data.status === 'success' ? 'color:green;' : 'color:red;'; |
| 109 |
Swal.fire({ |
| 110 |
title: 'Your settings are saved.', |
| 111 |
html: '<p style=font-size:14px>Please clear your browser <b>cache</b> and <b>cookies</b> both and reload the front end before testing. Alternatively, you can launch a new browser window in <b>Incognito</b>/Private mode (Ctrl+Shift+N in chrome) to test.</p></b><p><span style="'+ color +'">'+ data.msg+'</span></p>', |
| 112 |
width: 450, |
| 113 |
icon: 'success', |
| 114 |
confirmButtonText: 'Got it', |
| 115 |
confirmButtonWidth: 100, |
| 116 |
confirmButtonClass: 'btn btn-lg', |
| 117 |
}).then((result) => { |
| 118 |
if (result.isConfirmed) { |
| 119 |
$.ajax({ |
| 120 |
url: qcld_gemini_admin_data.ajax_url, |
| 121 |
type: 'POST', |
| 122 |
data: { |
| 123 |
action: 'update_settings_option', |
| 124 |
nonce: qcld_gemini_admin_data.ajax_nonce, |
| 125 |
disable_ss: 1 |
| 126 |
}, |
| 127 |
|
| 128 |
}); |
| 129 |
} |
| 130 |
}); |
| 131 |
} |
| 132 |
}); |
| 133 |
}); |
| 134 |
} |
| 135 |
}); |