Files
safekat/httpdocs/assets/js/safekat/pages/presupuestoCliente/disenioInterior.js
2024-11-07 20:24:40 +01:00

775 lines
34 KiB
JavaScript

class DisenioInterior {
constructor(domItem, wizardForm, validatorStepper) {
this.domItem = domItem;
this.wizardStep = wizardForm.querySelector('#interior-libro');
this.validatorStepper = validatorStepper;
this.allowNext = true;
this.disenioInterior = this.domItem.find(".disenio-interior");
this.divPapelInterior = this.domItem.find("#divPapelInterior");
this.divPapelInteriorColor = this.domItem.find("#divPapelInteriorColor");
this.divGramajeInterior = this.domItem.find("#divGramajeInterior");
this.papelInterior = this.domItem.find(".papel-interior");
this.negroEstandar = this.domItem.find("#negroEstandar");
this.negroPremium = this.domItem.find("#negroPremium");
this.colorEstandar = this.domItem.find("#colorEstandar");
this.colorPremium = this.domItem.find("#colorPremium");
this.offsetBlanco = this.domItem.find("#offsetBlanco");
this.offsetAhuesado = this.domItem.find("#offsetAhuesado");
this.offsetAhuesadoVolumen = this.domItem.find("#offsetAhuesadoVolumen");
this.estucadoMate = this.domItem.find("#estucadoMate");
this.gramaje = this.domItem.find(".check-interior-gramaje");
this.gramaje70 = this.domItem.find("#interiorGramaje70");
this.gramaje80 = this.domItem.find("#interiorGramaje80");
this.gramaje90 = this.domItem.find("#interiorGramaje90");
this.gramaje100 = this.domItem.find("#interiorGramaje100");
this.gramaje115 = this.domItem.find("#interiorGramaje115");
this.gramaje120 = this.domItem.find("#interiorGramaje120");
this.gramaje135 = this.domItem.find("#interiorGramaje135");
this.gramaje150 = this.domItem.find("#interiorGramaje150");
this.gramaje170 = this.domItem.find("#interiorGramaje170");
this.interiorColor = this.domItem.find(".interior-color");
this.disenioInterior_color = this.domItem.find(".disenio-interior-color");
this.papelInterior_color = this.domItem.find(".papel-interior-color");
this.colorEstandar_color = this.domItem.find("#colorEstandarColor");
this.colorPremium_color = this.domItem.find("#colorPremiumColor");
this.offsetBlanco_color = this.domItem.find("#offsetBlancoColor");
this.offsetAhuesado_color = this.domItem.find("#offsetAhuesadoColor");
this.offsetAhuesadoVolumen_color = this.domItem.find("#offsetAhuesadoVolumenColor");
this.estucadoMate_color = this.domItem.find("#estucadoMateColor");
this.gramaje_color = this.domItem.find(".check-interior-color-gramaje");
this.gramaje70_color = this.domItem.find("#interiorGramaje70Color");
this.gramaje80_color = this.domItem.find("#interiorGramaje80Color");
this.gramaje90_color = this.domItem.find("#interiorGramaje90Color");
this.gramaje100_color = this.domItem.find("#interiorGramaje100Color");
this.gramaje115_color = this.domItem.find("#interiorGramaje115Color");
this.gramaje120_color = this.domItem.find("#interiorGramaje120Color");
this.gramaje135_color = this.domItem.find("#interiorGramaje135Color");
this.gramaje150_color = this.domItem.find("#interiorGramaje150Color");
this.gramaje170_color = this.domItem.find("#interiorGramaje170Color");
this.checksGramaje = $('.gramaje-interior');
this.initValidation();
// Creamos un nuevo observador que detecta cambios en los atributos
this.observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
const targetElement = mutation.target;
if (targetElement.id.includes("Color"))
this.#handleUpdateGramajeColor();
else
this.#handleUpdateGramaje();
}
});
});
}
init() {
const self = this;
// Eventos
this.disenioInterior.on('click', this.#handleDisenioInterior.bind(this));
this.papelInterior.on('click', this.#handlePapelInterior.bind(this));
this.disenioInterior_color.on('click', this.#handleDisenioInterior.bind(this));
this.papelInterior_color.on('click', this.#handlePapelInterior.bind(this));
// Observadores
this.observer.observe(this.negroEstandar[0], { attributes: true });
this.observer.observe(this.negroPremium[0], { attributes: true });
this.observer.observe(this.colorEstandar[0], { attributes: true });
this.observer.observe(this.colorPremium[0], { attributes: true });
this.observer.observe(this.offsetBlanco[0], { attributes: true });
this.observer.observe(this.offsetAhuesado[0], { attributes: true });
this.observer.observe(this.offsetAhuesadoVolumen[0], { attributes: true });
this.observer.observe(this.estucadoMate[0], { attributes: true });
this.observer.observe(this.colorEstandar_color[0], { attributes: true });
this.observer.observe(this.colorPremium_color[0], { attributes: true });
this.observer.observe(this.offsetBlanco_color[0], { attributes: true });
this.observer.observe(this.offsetAhuesado_color[0], { attributes: true });
this.observer.observe(this.offsetAhuesadoVolumen_color[0], { attributes: true });
this.observer.observe(this.estucadoMate_color[0], { attributes: true });
this.checksGramaje.each(function () {
const customOptionEL = $(this);
customOptionEL.on('click', function () {
self.#handleClickGramaje(customOptionEL);
});
});
}
cargarDatos(datos, papelInteriorDiferente) {
if (papelInteriorDiferente) {
if (datos.negro) {
if (datos.negro.tipo.includes("Premium")) {
this.negroPremium.addClass('selected');
}
else {
this.negroEstandar.addClass('selected');
}
this.divPapelInterior.find(`[cod="${datos.negro.papel.code}"]`).addClass('selected');
setTimeout(function () {
$(`#divGramajeInterior .gramaje-interior input[data-value="${datos.negro.gramaje}"]`).trigger('click');
}, 0);
}
if (datos.color) {
if (datos.color.tipo.includes("Premium")) {
this.colorPremium_color.addClass('selected');
}
else {
this.colorEstandar_color.addClass('selected');
}
this.divPapelInteriorColor.find(`[cod="${datos.color.papel.code}"]`).addClass('selected');
setTimeout(function () {
$(`#divGramajeInteriorColor .gramaje-interior-color input[data-value="${datos.color.gramaje}"]`).trigger('click');
}, 0);
}
}
else {
if (datos.color) {
if (datos.color.tipo.includes("Premium")) {
this.colorPremium.addClass('selected');
}
else {
this.colorEstandar.addClass('selected');
}
this.divPapelInterior.find(`[cod="${datos.color.papel.code}"]`).addClass('selected');
setTimeout(function () {
$(`#divGramajeInterior .gramaje-interior input[data-value="${datos.color.gramaje}"]`).trigger('click');
}, 0);
}
else {
if (datos.negro.tipo.includes("Premium")) {
this.negroPremium.addClass('selected');
}
else {
this.negroEstandar.addClass('selected');
}
// mismo papel y gramaje
this.divPapelInterior.find(`[cod="${datos.negro.papel.code}"]`).addClass('selected');
setTimeout(function () {
$(`#divGramajeInterior .gramaje-interior input[data-value="${datos.negro.gramaje}"]`).trigger('click');
}, 0);
}
}
}
initValidation() {
const stepper = this.validatorStepper;
this.formValidation = FormValidation.formValidation(this.wizardStep, {
fields: {
div_impresion_interior: {
validators: {
callback: {
callback: function (input) {
const divImpresionInterior = $('#divImpresionInterior'); // Selecciona el div
divImpresionInterior.find('.fv-plugins-message-container').remove();
if ($('.disenio-interior.selected').length > 0) {
return true;
}
else {
divImpresionInterior.append(`
<div class="fv-plugins-message-container invalid-feedback">
<div data-field="div_impresion_interior" data-validator="callback" style="margin-top: 50px;">
${window.translations["validation"].disenio_interior}
</div>
</div>
`);
}
return false;
},
}
}
},
div_papel_interior: {
validators: {
callback: {
callback: function (input) {
const divPapelInterior = $('#divPapelInterior'); // Selecciona el div
divPapelInterior.find('.fv-plugins-message-container').remove();
if ($('.papel-interior.selected').length > 0) {
return true;
}
else {
divPapelInterior.append(`
<div class="fv-plugins-message-container invalid-feedback">
<div data-field="div_impresion_interior" data-validator="callback" style="margin-top: 50px;">
${window.translations["validation"].papel_interior}
</div>
</div>
`);
}
return false;
},
}
}
},
div_gramaje_interior: {
validators: {
callback: {
callback: function (input) {
const divGramajeInterior = $('#divGramajeInterior'); // Selecciona el div
if ($("#divGramajeInterior").hasClass("d-none")) return true;
divGramajeInterior.find('.fv-plugins-message-container').remove();
if ($('.check-interior-gramaje:checked').length > 0) {
return true;
}
else {
divGramajeInterior.append(`
<div class="fv-plugins-message-container invalid-feedback">
<div data-field="div_impresion_interior" data-validator="callback" style="margin-top: 50px;">
${window.translations["validation"].gramaje_interior}
</div>
</div>
`);
}
return false;
},
}
}
},
div_impresion_interior_color: {
validators: {
callback: {
callback: function (input) {
const divImpresionInterior = $('#divImpresionInteriorColor'); // Selecciona el div
if (divImpresionInterior.hasClass("d-none")) return true;
divImpresionInterior.find('.fv-plugins-message-container').remove();
if ($('.disenio-interior-color.selected').length > 0) {
return true;
}
else {
divImpresionInterior.append(`
<div class="fv-plugins-message-container invalid-feedback">
<div data-field="div_impresion_interior_color" data-validator="callback" style="margin-top: 50px;">
${window.translations["validation"].disenio_interior}
</div>
</div>
`);
}
return false;
},
}
}
},
div_papel_interior_color: {
validators: {
callback: {
callback: function (input) {
const divPapelInterior = $('#divPapelInteriorColor'); // Selecciona el div
if (divPapelInterior.hasClass("d-none")) return true;
divPapelInterior.find('.fv-plugins-message-container').remove();
if ($('.papel-interior.selected').length > 0) {
return true;
}
else {
divPapelInterior.append(`
<div class="fv-plugins-message-container invalid-feedback">
<div data-field="div_impresion_interior" data-validator="callback" style="margin-top: 50px;">
${window.translations["validation"].papel_interior}
</div>
</div>
`);
}
return false;
},
}
}
},
div_gramaje_interior_color: {
validators: {
callback: {
callback: function (input) {
const divGramajeInterior = $('#divGramajeInteriorColor'); // Selecciona el div
if ($("#divGramajeInterior").hasClass("d-none")) return true;
divGramajeInterior.find('.fv-plugins-message-container').remove();
if ($('.check-interior-gramaje:checked').length > 0) {
return true;
}
else {
divGramajeInterior.append(`
<div class="fv-plugins-message-container invalid-feedback">
<div data-field="div_impresion_interior" data-validator="callback" style="margin-top: 50px;">
${window.translations["validation"].gramaje_interior}
</div>
</div>
`);
}
return false;
},
}
}
},
},
plugins: {
trigger: new FormValidation.plugins.Trigger(),
bootstrap5: new FormValidation.plugins.Bootstrap5({
// Use this for enabling/changing valid/invalid class
// eleInvalidClass: '',
eleValidClass: '',
rowSelector: function (field, ele) {
// field is the field name
// ele is the field element
switch (field) {
case 'div_impresion_interior':
case 'div_papel_interior':
case 'div_gramaje_interior':
case 'div_impresion_interior_color':
case 'div_papel_interior_color':
case 'div_gramaje_interior_color':
return '.col-sm-10';
default:
return '.col-sm-3';
}
}
}),
autoFocus: new FormValidation.plugins.AutoFocus(),
submitButton: new FormValidation.plugins.SubmitButton()
}
}).on('core.form.valid', () => {
if (this.allowNext)
stepper.next();
});
}
getIsHq() {
try {
if (this.interiorColor.filter('.d-none').length > 0) {
return this.disenioInterior.filter('.selected').attr('id').includes('Premium');
}
else if (this.disenioInterior_color.filter('.selected').length > 0) {
return {
negro: this.disenioInterior.filter('.selected').attr('id').includes('Premium'),
color: this.disenioInterior_color.filter('.selected').attr('id').includes('Premium')
}
}
return null;
} catch (e) {
return null;
}
}
getPapel(forResumen = false) {
try {
if (this.papelInterior.filter('.selected').length > 0) {
if (this.papelInterior_color.filter('.selected').length > 0) {
if (forResumen) {
return {
negro: $(this.papelInterior.filter('.selected').find('.form-label')).text(),
color: $(this.papelInterior_color.filter('.selected').find('.form-label')).text(),
}
}
else {
return {
negro: this.papelInterior.filter('.selected').attr('cod'),
color: this.papelInterior_color.filter('.selected').attr('cod')
}
}
}
if (this.interiorColor.filter('.d-none').length > 0) {
if (forResumen) {
return $(this.papelInterior.filter('.selected').find('.form-label')).text();
}
else {
return this.papelInterior.filter('.selected').attr('cod')
}
}
else
return null;
}
return null;
}
catch (e) {
return null;
}
}
getGramaje() {
try {
if (this.interiorColor.filter('.d-none').length == 0) {
let values = {
negro: this.gramaje.filter(':checked').attr('data-value'),
color: this.gramaje_color.filter(':checked').attr('data-value')
}
if (values.negro && values.color) {
return values;
}
else return null;
}
else {
return this.gramaje.filter(':checked').attr('data-value');
}
} catch (e) {
return null;
}
}
#handleClickGramaje(customOption) {
const el = customOption[0];
if (el.checked) {
// If custom option element is radio, remove checked from the siblings (closest `.row`)
if (el.type === 'radio') {
const customRadioOptionList = [].slice.call(el.closest('.row').querySelectorAll('.custom-option'))
customRadioOptionList.map(function (customRadioOptionEL) {
customRadioOptionEL.closest('.custom-option').classList.remove('checked')
})
}
el.closest('.custom-option').classList.add('checked')
} else {
el.closest('.custom-option').classList.remove('checked')
}
}
#handleDisenioInterior(event) {
// Accede al ID del elemento que disparó el evento
const element = $(event.target);
let class2Find = '.disenio-interior';
if (element[0].closest('.image-container').id.includes('Color'))
class2Find = '.disenio-interior-color';
let containers = element.closest(class2Find).parent().find(class2Find);
for (let container of containers) {
if (container != element.closest(class2Find)[0]) {
$(container).removeClass('selected');
$(container).find('.image-presupuesto').removeClass('selected');
}
}
element.closest(class2Find).toggleClass('selected');
element.closest('.image-presupuesto').toggleClass('selected');
// Para recalcular el presupuesto
element.trigger('change');
}
#handlePapelInterior(event) {
// Accede al ID del elemento que disparó el evento
const element = $(event.target);
let class2Find = '.papel-interior';
if (element[0].closest('.image-container').id.includes('Color'))
class2Find = '.papel-interior-color';
let containers = element.closest(class2Find).parent().find(class2Find);
for (let container of containers) {
if (container != element.closest(class2Find)[0]) {
$(container).removeClass('selected');
$(container).find('.image-presupuesto').removeClass('selected');
}
}
element.closest(class2Find).toggleClass('selected');
element.closest('.image-presupuesto').toggleClass('selected');
if ($('#fresado').hasClass('selected')) {
if (this.estucadoMate.hasClass('selected') || this.estucadoMate_color.hasClass('selected')) {
$('#tapaDuraLomoRedondo').addClass('d-none');
}
else {
$('#tapaDuraLomoRedondo').removeClass('d-none');
}
}
// Para recalcular el presupuesto
element.trigger('change');
}
#handleUpdateGramaje() {
let showGramaje = false;
$(".check-interior-gramaje ").prop("checked", false);
$(".check-interior-color-gramaje ").prop("checked", false);
if (this.negroEstandar.hasClass("selected") || this.colorEstandar.hasClass("selected")) {
if (this.offsetBlanco.hasClass("selected")) {
this.gramaje70.addClass("d-none");
this.gramaje80.removeClass("d-none");
this.gramaje90.removeClass("d-none");
this.gramaje100.addClass("d-none");
this.gramaje115.addClass("d-none");
this.gramaje120.removeClass("d-none");
this.gramaje135.addClass("d-none");
this.gramaje150.addClass("d-none");
this.gramaje170.addClass("d-none");
showGramaje = true;
}
else if (this.offsetAhuesado.hasClass("selected")) {
this.gramaje70.addClass("d-none");
this.gramaje80.removeClass("d-none");
this.gramaje90.removeClass("d-none");
this.gramaje100.addClass("d-none");
this.gramaje115.addClass("d-none");
this.gramaje120.addClass("d-none");
this.gramaje135.addClass("d-none");
this.gramaje150.addClass("d-none");
this.gramaje170.addClass("d-none");
showGramaje = true;
}
else if (this.offsetAhuesadoVolumen.hasClass("selected")) {
this.gramaje70.removeClass("d-none");
this.gramaje80.addClass("d-none");
this.gramaje90.addClass("d-none");
this.gramaje100.addClass("d-none");
this.gramaje115.addClass("d-none");
this.gramaje120.addClass("d-none");
this.gramaje135.addClass("d-none");
this.gramaje150.addClass("d-none");
this.gramaje170.addClass("d-none");
showGramaje = true;
}
else if (this.estucadoMate.hasClass("selected")) {
this.gramaje70.addClass("d-none");
this.gramaje80.addClass("d-none");
this.gramaje90.removeClass("d-none");
this.gramaje100.addClass("d-none");
this.gramaje115.addClass("d-none");
this.gramaje120.removeClass("d-none");
this.gramaje135.addClass("d-none");
this.gramaje150.addClass("d-none");
this.gramaje170.addClass("d-none");
showGramaje = true;
}
}
else if (this.negroPremium.hasClass("selected") || this.colorPremium.hasClass("selected")) {
if (this.offsetBlanco.hasClass("selected")) {
this.gramaje70.addClass("d-none");
this.gramaje80.removeClass("d-none");
this.gramaje90.removeClass("d-none");
this.gramaje100.removeClass("d-none");
this.gramaje115.addClass("d-none");
this.gramaje120.addClass("d-none");
this.gramaje135.addClass("d-none");
this.gramaje150.removeClass("d-none");
this.gramaje170.removeClass("d-none");
showGramaje = true;
}
else if (this.offsetAhuesado.hasClass("selected")) {
this.gramaje70.addClass("d-none");
this.gramaje80.removeClass("d-none");
this.gramaje90.removeClass("d-none");
this.gramaje100.removeClass("d-none");
this.gramaje115.addClass("d-none");
this.gramaje120.addClass("d-none");
this.gramaje135.addClass("d-none");
this.gramaje150.addClass("d-none");
this.gramaje170.addClass("d-none");
showGramaje = true;
}
else if (this.offsetAhuesadoVolumen.hasClass("selected")) {
this.gramaje70.addClass("d-none");
this.gramaje80.addClass("d-none");
this.gramaje90.addClass("d-none");
this.gramaje100.addClass("d-none");
this.gramaje115.addClass("d-none");
this.gramaje120.addClass("d-none");
this.gramaje135.addClass("d-none");
this.gramaje150.addClass("d-none");
this.gramaje170.addClass("d-none");
showGramaje = true;
}
else if (this.estucadoMate.hasClass("selected")) {
this.gramaje70.addClass("d-none");
this.gramaje80.addClass("d-none");
this.gramaje90.removeClass("d-none");
this.gramaje100.addClass("d-none");
this.gramaje115.removeClass("d-none");
this.gramaje120.addClass("d-none");
this.gramaje135.removeClass("d-none");
this.gramaje150.removeClass("d-none");
this.gramaje170.removeClass("d-none");
showGramaje = true;
}
}
if ($("#divGramajeInterior").hasClass("d-none") && showGramaje) {
$("#divGramajeInterior").removeClass("d-none");
}
else if (!showGramaje) {
$("#divGramajeInterior").addClass("d-none");
}
}
#handleUpdateGramajeColor() {
let showGramaje = false;
$(".check-interior-color-gramaje ").prop("checked", false);
if (this.colorEstandar_color.hasClass("selected")) {
if (this.offsetBlanco_color.hasClass("selected")) {
this.gramaje70_color.addClass("d-none");
this.gramaje80_color.removeClass("d-none");
this.gramaje90_color.removeClass("d-none");
this.gramaje100_color.addClass("d-none");
this.gramaje115_color.addClass("d-none");
this.gramaje120_color.removeClass("d-none");
this.gramaje135_color.addClass("d-none");
this.gramaje150_color.addClass("d-none");
this.gramaje170_color.addClass("d-none");
showGramaje = true;
}
else if (this.offsetAhuesado_color.hasClass("selected")) {
this.gramaje70_color.addClass("d-none");
this.gramaje80_color.removeClass("d-none");
this.gramaje90_color.removeClass("d-none");
this.gramaje100_color.addClass("d-none");
this.gramaje115_color.addClass("d-none");
this.gramaje120_color.addClass("d-none");
this.gramaje135_color.addClass("d-none");
this.gramaje150_color.addClass("d-none");
this.gramaje170_color.addClass("d-none");
showGramaje = true;
}
else if (this.offsetAhuesadoVolumen_color.hasClass("selected")) {
this.gramaje70_color.removeClass("d-none");
this.gramaje80_color.addClass("d-none");
this.gramaje90_color.addClass("d-none");
this.gramaje100_color.addClass("d-none");
this.gramaje115_color.addClass("d-none");
this.gramaje120_color.addClass("d-none");
this.gramaje135_color.addClass("d-none");
this.gramaje150_color.addClass("d-none");
this.gramaje170_color.addClass("d-none");
showGramaje = true;
}
else if (this.estucadoMate_color.hasClass("selected")) {
this.gramaje70_color.addClass("d-none");
this.gramaje80_color.addClass("d-none");
this.gramaje90_color.removeClass("d-none");
this.gramaje100_color.addClass("d-none");
this.gramaje115_color.addClass("d-none");
this.gramaje120_color.removeClass("d-none");
this.gramaje135_color.addClass("d-none");
this.gramaje150_color.addClass("d-none");
this.gramaje170_color.addClass("d-none");
showGramaje = true;
}
}
else if (this.colorPremium_color.hasClass("selected")) {
if (this.offsetBlanco_color.hasClass("selected")) {
this.gramaje70_color.addClass("d-none");
this.gramaje80_color.removeClass("d-none");
this.gramaje90_color.removeClass("d-none");
this.gramaje100_color.removeClass("d-none");
this.gramaje115_color.addClass("d-none");
this.gramaje120_color.addClass("d-none");
this.gramaje135_color.addClass("d-none");
this.gramaje150_color.removeClass("d-none");
this.gramaje170_color.removeClass("d-none");
showGramaje = true;
}
else if (this.offsetAhuesado_color.hasClass("selected")) {
this.gramaje70_color.addClass("d-none");
this.gramaje80_color.removeClass("d-none");
this.gramaje90_color.removeClass("d-none");
this.gramaje100_color.removeClass("d-none");
this.gramaje115_color.addClass("d-none");
this.gramaje120_color.addClass("d-none");
this.gramaje135_color.addClass("d-none");
this.gramaje150_color.addClass("d-none");
this.gramaje170_color.addClass("d-none");
showGramaje = true;
}
else if (this.offsetAhuesadoVolumen_color.hasClass("selected")) {
this.gramaje70_color.addClass("d-none");
this.gramaje80_color.addClass("d-none");
this.gramaje90_color.addClass("d-none");
this.gramaje100_color.addClass("d-none");
this.gramaje115_color.addClass("d-none");
this.gramaje120_color.addClass("d-none");
this.gramaje135_color.addClass("d-none");
this.gramaje150_color.addClass("d-none");
this.gramaje170_color.addClass("d-none");
showGramaje = true;
}
else if (this.estucadoMate_color.hasClass("selected")) {
this.gramaje70_color.addClass("d-none");
this.gramaje80_color.addClass("d-none");
this.gramaje90_color.removeClass("d-none");
this.gramaje100_color.addClass("d-none");
this.gramaje115_color.removeClass("d-none");
this.gramaje120_color.addClass("d-none");
this.gramaje135_color.removeClass("d-none");
this.gramaje150_color.removeClass("d-none");
this.gramaje170_color.removeClass("d-none");
showGramaje = true;
}
}
if ($("#divGramajeInteriorColor").hasClass("d-none") && showGramaje) {
$("#divGramajeInteriorColor").removeClass("d-none");
}
else if (!showGramaje) {
$("#divGramajeInteriorColor").addClass("d-none");
}
}
}
export default DisenioInterior;