/** * 2007-2020 PrestaShop * * NOTICE OF LICENSE * * @author Innovadeluxe SL * @copyright 2020 Innovadeluxe SL * @license INNOVADELUXE */ class IdxrvalidatinguserFront{ constructor(){ this.config = typeof IdxrvalidatinguserFrontConfig == 'object' ? IdxrvalidatinguserFrontConfig : {}; IdxrvalidatinguserFront.SELF = this; } init(){ this.otherElements(); this.otherDinamicElements(); this.alertBeforeRegister(); this.initFilesAlerts(); } ajax(endpoint = '', tipo = 'get', datos = {}, datatype= 'json'){ let promise = new Promise((resolve, reject)=>{ $.ajax({ type: tipo, data: datos, dataType: datatype, url: endpoint, success: function(response){ resolve(response); }, error: function(xhr, ajaxOptions, thrownError) { reject(thrownError); } }); }); return promise; } otherElements(){ if(this.config.es17){ $(`#${this.config.idFormRegister}`).attr("enctype", "multipart/form-data"); }else{ $( document ).ajaxComplete(function( event, xhr, settings ) { if(settings.data === undefined || typeof settings.data.indexOf !== 'function' || settings.data.indexOf("controller=authentication&SubmitCreate") !== 0){ return; } setTimeout(()=>{ $("#account-creation_form").attr("enctype", "multipart/form-data"); }, 1000); }); $("#account-creation_form").attr("enctype", "multipart/form-data"); } if($('#idxrvalidatinguser-content').length){ $('#idxrvalidatinguser-content').closest('form').attr("enctype", "multipart/form-data"); } const self = this; if(parseInt(this.config.showupload)){ $("#accountb2b").change(function() { $(".form-b2b").slideToggle(); }); $(document).on('change', "[name='b2b_attachment']", function () { var ext = this.value.match(/\.([^\.]+)$/)[1]; switch(ext) { case 'pdf': case 'doc': case 'docx': case 'xls': case 'xlsx': case 'txt': case 'zip': case 'rar': case 'jpg': case 'jpeg': case 'png': case 'gif': document.getElementById("uploadFile").value = this.value; break; default: alert(self.config.notAllowedFileTranslation); this.value=''; } }); } $(document).on('submit', `#${this.config.idFormRegister}`, function(e){ if(!self.config.isOpc){ self.initPopupNewsletter(e); } }) if (this.config.hide_vat_and_company){ if ($('input.form-control[name="company"]').length > 0){ $('input.form-control[name="company"]').parent().parent().hide(); } if ($('input.form-control[name="vat_number"]').length > 0){ $('input.form-control[name="vat_number"]').parent().parent().hide(); } } } otherDinamicElements(){ const self = this; $(document).on('change','input[name="id_customertype"]', function(){ if(self.config.es17) { $("[name='b2b_attachment']").parent().parent().toggle(); }else{ $("#content_uploadBtn").toggle(); } $('#alertprivate').toggle(); $('#alertdoc').toggle(); }); $(document).on('keyup','input[name="siret"]',function(e) { e.preventDefault(); if(parseInt(self.config.isb2bauto)!=0) { if($('input[name="siret"]').val()) { if(self.config.es17) { $("[name='b2b_attachment']").parent().parent().show(); $('#alertprivate').show(); $('#alertdoc').show(); }else{ $("[name='b2b_attachment']").parent().show(); if(!$('input[name="id_customertype"]').prop("checked")){ $('input[name="id_customertype"]').click(); } } } else { if(self.config.es17) { $("[name='b2b_attachment']").parent().parent().toggle(); $('#alertprivate').toggle(); $('#alertdoc').toggle(); }else{ $("[name='b2b_attachment']").parent().toggle(); $('input[name="id_customertype"]').click(); } } } }); if(self.config.es17){ $(document).on('change','[name="id_country"]', function(e){ let inputsForm = { ...self.getFormInputValue()}; let datos = {action: 'GetStates', id_country: $(e.target).val(), inputsForm: JSON.stringify(inputsForm) , idxb2b: true}; self.ajax(self.config.ajaxDisplayAddresForm, 'post', datos) .then(function(res){ if (res.result == 'ok') { self.removeFormInputs(); $('[name="email"]').closest('div.form-group.row').before(res.form); } else if (res.result == 'ko') { alert('error: ajax change country'); } }) .catch(function(err){ console.warn(err); }) }); } this.config.fieldsDependens.forEach(element => { let selfElement = element; $(document).on('change',`#idxrvalidatinguser-input-${parseInt(selfElement.id_dependent)}`, function(){ let elementDependent = $(`#idxrvalidatinguser-input-${selfElement.id_option}`); if(!$(`#idxrvalidatinguser-input-${selfElement.id_dependent}`).prop('checked')) { elementDependent.closest('.idxfm.form-group').hide(); self.resetInputValue(elementDependent); }else{ elementDependent.closest('.idxfm.form-group').show(); } }); }); this.createBtnWholesaleform(); } getFormInputValue(){ let inputsForm = []; let exclude_form = ['id_gender', 'email','password', 'birthday', 'optin', 'newsletter', 'psgdpr', 'b2b_attachment', 'id_customertype', 'customer_privacy', 'mobileotp', 'GenerateOTP']; $('div.form-group.row div input').each( (index, element)=> { if( !exclude_form.includes($(element).prop('name')) ){ inputsForm[$(element).prop('name')] = $(element).val(); } }) return inputsForm; } removeFormInputs(){ if($('[id="ajax-register-section"]:visible').length > 0){ $('[id="ajax-register-section"]:visible').remove(); }else{ let exclude_form = ['id_gender', 'email','password', 'birthday', 'optin', 'newsletter', 'psgdpr', 'b2b_attachment', 'id_customertype', 'customer_privacy', 'mobileotp', 'GenerateOTP']; $('div.form-group.row div input').each( (index, element)=> { if( !exclude_form.includes($(element).prop('name')) ){ $(element).closest('div.form-group.row').remove(); } }); $('div.form-group.row div select').each( (index, element)=> { $(element).closest('div.form-group.row').remove(); }); } } resetInputValue(e) { switch (e.prop('type')) { case 'checkbox-group': case 'checkbox': if(e.prop('checked')){ e.click(); } break; default: e.val(''); break; } } alertBeforeRegister(){ const self = this; if(self.config.es17){ // Alert file attachment $("[name='b2b_attachment']").parent().parent().before(`
`); $("[name='b2b_attachment']").parent().before(``); // Inputfile professional let allowretailer = $('input[name="id_customertype"]'); if( (allowretailer.prop('type')==="checkbox" && !allowretailer.prop('checked')) || !allowretailer.length){ $("[name='b2b_attachment']").parent().closest('div.form-group').hide(); $('#alertprivate').hide(); } if( (allowretailer.prop('type')!=="checkbox" && allowretailer.length) || (parseInt(self.config.is_b2bForm)) || (allowretailer.prop('type') ==="checkbox" && allowretailer.prop('checked')) ){ $('#alertdoc').show(); $("[name='b2b_attachment']").parent().closest('div.form-group').show(); } } } decodeEntities(encodedString) { var textArea = document.createElement('textarea'); textArea.innerHTML = encodedString; return textArea.value; } initPopupNewsletter(e){ const self = this; return new Promise((resolve, reject)=>{ if(parseInt(self.config.enableNewsletterPrivacy) && ( !$('input[name="optin"]').prop('checked') && !$('#newsletter').prop('checked') && !$('input[name="newsletter"]').prop('checked'))){ if(e !== undefined){ e.preventDefault(); } let $modal = $('#idxrvalidatinguser-popup-privacidad'); $modal.removeClass('hidden'); if(!self.config.modalOn){ self.config.modalOn = 1; resolve(self.initModal($modal)); } } resolve(true); }); } initModal($modal){ return new Promise((resolve, reject)=>{ const self = this; let form = $('#submitAccount'); $('.idxrmodal-close-header').on('click',function(e){ $modal.addClass('hidden'); $('.idxrvalidatinguser-backdrop').addClass('hidden'); resolve(false); }) $('.idxrmodal-continue').on('click',function(e){ if(!self.config.es17){ e.preventDefault(); } self.config.enableNewsletterPrivacy = 0; $modal.addClass('hidden'); $('.idxrvalidatinguser-backdrop').addClass('hidden'); if(!self.config.es17 && !self.config.isOpc){ form.get(0).click(); } resolve(false); }) $('.idxrmodal-newsletter').on('click',function(e){ if(!self.config.es17){ e.preventDefault(); } $modal.addClass('hidden'); $('.idxrvalidatinguser-backdrop').addClass('hidden'); if(self.config.isOpc){ $('#register_form_content input[name="newsletter"]').prop('checked', true); }else{ $('input[name="newsletter"]').prop('checked', true); $('#newsletter').prop('checked', true); } if(!self.config.es17 && !self.config.isOpc){ form.get(0).click(); } resolve(true); }) }) } createBtnWholesaleform(){ const self = this; if(parseInt(self.config.wholesaleForm) && !self.config.es17) { var htmlAppend = ``; $('#create-account_form .form_content.clearfix div.submit').addClass('standard-rg'); $('#create-account_form .form_content.clearfix div.submit').after('' + htmlAppend); } } initFilesAlerts(){ let self = this; $('input[type="file"][name^="idxrvalidatinguser-input-"]').each(function() { $(this).on('change', function() { self.checkExtension($(this)) }); }); } checkExtension(input_file){ let self = this; let name = input_file.attr('name'); let id_campo = name.charAt(name.length - 1); let filePath = input_file.val(); let fileExtension = filePath.split('.').pop(); let datos = {action: 'checkExtension', id_campo: id_campo, extension: fileExtension}; this.ajax(this.config.ajaxDisplayAddresForm, 'post', datos) .then(function(res){ if (!res.isValidExtension) { $(input_file).val(''); alert(self.config.notAllowedFileTranslation); } }) .catch(function(err){ $(input_file).val(''); console.warn(err); }) } } $(function() { try{ let handler; if(typeof IdxrvalidatinguserFrontOverride == 'function'){ handler = new IdxrvalidatinguserFrontOverride(); }else{ handler = new IdxrvalidatinguserFront(); } handler.init(); }catch(e){ console.log(e); } });