File: //proc/self/cwd/nueva/modules/btecommercecopilot/views/js/individual-translation.js
/**
* Ecommerce copilot
*
* @author businesstech.fr <modules@businesstech.fr> - https://www.businesstech.fr/
* @copyright https://www.businesstech.fr/
* @license see file: LICENSE.txt
*
* ____ _______
* | _ \ |__ __|
* | |_) | | |
* | _ < | |
* | |_) | | |
* |____/ |_|
*/
function updateProductDescriptionContent(value, contentElementId) {
var contentElement = document.getElementById(contentElementId);
if (contentElement) {
contentElement.innerHTML = value;
}
}
document.addEventListener('DOMContentLoaded', function () {
function attachOnInputEvent() {
$('#productDescription').on('input', function() {
updateProductDescriptionContent(this.value, 'productDescriptionOverview');
});
}
attachOnInputEvent();
// Function to handle form visibility based on the current step
function handleFormVisibility(step) {
// Define an object to map step numbers to corresponding form visibility
const formVisibility = {
1: { show: "translateForm", hide: ["editTranslateForm", "savedForm"] },
2: { show: "editTranslateForm", hide: ["translateForm", "savedForm"] },
3: { show: "savedForm", hide: ["translateForm", "editTranslateForm"] }
};
// Check if the step is valid (1, 2, or 3)
if (formVisibility[step]) {
// Show the form for the current step
$(`#${formVisibility[step].show}`).removeClass('d-none');
// Hide other forms
formVisibility[step].hide.forEach(form => $(`#${form}`).addClass('d-none'));
// Always hide these elements
$("#no-valid-sub, #new-generation").addClass('d-none');
}
}
// Get the current step value and handle form visibility
const currentStep = parseInt(document.getElementById("step").value);
handleFormVisibility(currentStep);
// Attach an event listener to the #generateType element to handle changes
$("#generateType").bind("change", function () {
// Hide or show search blocks based on the selected value
switch (this.value) {
case 'long-description':
case 'short-description':
case 'meta-description':
// Show product search block, hide category and CMS search blocks
$("#category-search-block").addClass('d-none');
$("#product-search-block").removeClass('d-none');
$("#cms-search-block").addClass('d-none');
break;
case 'category-description':
case 'category-additional-description':
case 'category-meta-description':
// Show category search block, hide product and CMS search blocks
$("#product-search-block").addClass('d-none');
$("#category-search-block").removeClass('d-none');
$("#cms-search-block").addClass('d-none');
break;
case 'cms-page-content':
// Show CMS search block, hide product and category search blocks
$("#product-search-block").addClass('d-none');
$("#category-search-block").addClass('d-none');
$("#cms-search-block").removeClass('d-none');
break;
}
});
$("#translateDescription").bind("click", function (event) {
var dataToGenerateField = document.getElementById("elementId");
var canSubmit = true;
// Reset the style of the elementId field
dataToGenerateField.style.cssText += 'color:#555;background-color:#fff';
// Check if the elementId field is empty
if (document.getElementById("elementId").value == '') {
event.preventDefault();
document.getElementById("id_product_search").style.cssText += 'color:red;background-color:#f8d7da';
document.getElementById("id_product").style.cssText += 'color:red;background-color:#f8d7da';
canSubmit = false;
}
if (canSubmit === true) {
$.ajax({
type: "POST",
url: btEcop.ajaxUrl,
dataType: "json",
data: {
ajax: "1",
action: "translateDescription",
elementId: document.getElementById("elementId").value,
generateType: document.getElementById("generateType").value,
idLangFrom: document.getElementById("idLangFrom").value,
idLangTo: document.getElementById("idLangTo").value,
token: $("input[name=token]").val(),
},
beforeSend: function () {
// Show loader and hide the translation form and stepper
$("#loaderBlock").removeClass('d-none');
$("#translateForm").addClass('d-none').slideUp();
$("#stepper").addClass('d-none');
},
success: function (res) {
// Hide loader and show the translation form and stepper
$("#loaderBlock").addClass('d-none');
$("#translateForm").slideDown().removeClass('d-none');
$("#stepper").removeClass('d-none');
attachOnInputEvent();
// Handle invalid data response
if (res.hasInvalidData) {
$("#invalidData").removeClass('d-none').html(res.errorMessage);
}
// Handle generated data response
if (res.hasGeneratedData) {
// Update the step field and other hidden fields
document.getElementById("step").value = res.step;
document.getElementById("productIdToSave").value = document.getElementById("elementId").value;
document.getElementById("generateTypeToSave").value = document.getElementById("generateType").value;
document.getElementById("idLangToSave").value = document.getElementById("idLangTo").value;
// Update UI based on the current step
switch (res.step) {
case 1:
$("#translateForm").removeClass('d-none');
$("#generatedData").removeClass('d-none').html(res.successMessage);
$("#editTranslateForm, #savedForm").addClass('d-none');
$('#step1').removeClass('active').addClass('complete');
break;
case 2:
$("#translateForm").addClass('d-none');
$("#editTranslateForm").removeClass('d-none');
$('#step1').removeClass('active').addClass('complete');
$('#step2').removeClass('disable').addClass('active');
if (res.description) {
document.getElementById("productDescription").value = res.description;
$("#generatedEditData").removeClass('d-none').html(res.successMessage);
$("#productDescriptionOverview").html(res.description);
} else {
$("#generatedData, #about, #output").addClass('d-none');
$("#no-valid-sub, #new-generation").removeClass('d-none');
}
$("#savedForm").addClass('d-none');
break;
case 3:
$("#translateForm, #editTranslateForm").addClass('d-none');
$("#savedForm").removeClass('d-none');
$('#step1, #step2').removeClass('active').addClass('complete');
$('#step3').removeClass('disable').addClass('active');
break;
}
}
},
error: function (res) {
// Handle error response
if (res.hasInvalidData) {
$("#loaderBlock").addClass('d-none');
$("#translateForm").slideDown().removeClass('d-none');
$("#stepper").removeClass('d-none');
$("#invalidData").removeClass('d-none').html(res.errorMessage);
}
},
});
}
});
$("#saveProduct").bind("click", function (event) {
var canSubmit = true;
var productDescription = document.getElementById("productDescription");
// Check if the product description is empty
if (productDescription.value == '') {
event.preventDefault();
productDescription.style.cssText += 'color:red;background-color:#f8d7da';
canSubmit = false;
}
if (canSubmit === true) {
$.ajax({
type: "POST",
url: btEcop.ajaxUrl,
dataType: "json",
data: {
ajax: "1",
action: "translateSavedProductAfterPreview",
productId: document.getElementById("productIdToSave").value,
productDescription: productDescription.value,
generateType: document.getElementById("generateTypeToSave").value,
idLang: document.getElementById("idLangToSave").value,
token: $("input[name=token]").val(),
},
beforeSend: function () {
// Show loader and hide forms before sending the request
$("#loaderBlock").removeClass('d-none');
$("#editTranslateForm").addClass('d-none').slideUp();
$("#stepper").addClass('d-none');
},
success: function (res) {
// Hide loader and show forms after receiving the response
$("#loaderBlock").addClass('d-none');
$("#editTranslateForm").slideDown().removeClass('d-none');
$("#stepper").removeClass('d-none');
// Handle invalid data response
if (res.hasInvalidData) {
$("#invalidData").removeClass('d-none').html(res.errorMessage);
}
// Handle valid data response
if (res.hasGeneratedData) {
// Update the step field
document.getElementById("step").value = res.step;
// Update UI based on the current step
switch (res.step) {
case 1:
$("#translateForm").removeClass('d-none');
$("#generatedData").removeClass('d-none').html(res.successMessage);
$("#editTranslateForm, #savedForm").addClass('d-none');
$('#step1').removeClass('active').addClass('complete');
$('#step2').removeClass('disable').addClass('active');
break;
case 2:
$("#translateForm").addClass('d-none');
$("#editTranslateForm").removeClass('d-none');
$("#generatedEditData").removeClass('d-none').html(res.successMessage);
$("#savedForm").addClass('d-none');
$('#step1').removeClass('active').addClass('complete');
$('#step2').removeClass('disable').addClass('active');
break;
case 3:
$("#translateForm, #editTranslateForm").addClass('d-none');
$("#savedForm").removeClass('d-none');
$('#step1, #step2').removeClass('active').addClass('complete');
$('#step3').removeClass('disable').addClass('active');
break;
}
}
},
});
}
});
});