var SPMaskBehaviorFlyer = function (val) { return val.replace(/\D/g, '').length === 11 ? '(00) 00000-0000' : '(00) 0000-00009'; }; var spOptionsFlyer = { onKeyPress: function (val, e, field, options) { field.mask(SPMaskBehaviorFlyer.apply({}, arguments), options); } }; var form = { texts: [], inputs: [], cargo:{ 0:{ nome:'Assistente', }, 1:{ nome:'Gerente', } }, address:1, key:0, id_model:0, appendForm:'#produto-form-personalizar .panel-body', buttonHandler:'#pagina-produto #produto-resumo button.confirm', init: function(){ self = this; if ($(self.buttonHandler).length > 1){ self.buttonHandler = '#produto-resumo > div > div.col-7.button--paddingLeft > button' } $.ajax({ url: "https://editor.print-one.com.br/api/modelo/"+this.id_model+"/layers/?key="+this.key, type: "GET", dataType:'json', beforeSend: function () { }, success: function (objJSON) { self.texts = objJSON.layers.texts self.configForm(); self.createContainer(); self.createForm(); }, error: function (objXHR, txtStatus, txtErro) { console.error('erro', objXHR); }, complete: function (xhr) { } }); }, configForm: function () { for (let key = 0; key < this.texts.length; key++) { const element = this.texts[key].name; const elementID = this.texts[key].name.replace(' ', '').toLowerCase(); // const mask = element.toLowerCase().indexOf('telefone') != -1 || element.toLowerCase().indexOf('celular') != -1 ? true : false; const maxlength = element.toLowerCase().indexOf('nome') != -1 ? 45 : false; let type = element.toLowerCase().indexOf('endereco') != -1 ? 'select' : 'text'; type = element.toLowerCase().indexOf('cargo') != -1 ? 'select' : 'text'; const label = element.toLowerCase().indexOf('endereco') != -1 ? 'Endereço' : this.capitalize(element.replace(/\#/g,'')); let value = element.toLowerCase().indexOf('endereco') != -1 ? this.endereco : ''; value = element.toLowerCase().indexOf('cargo') != -1 ? this.cargo : ''; if (element.toLowerCase().indexOf('endereco4') == -1 & element.toLowerCase().indexOf('endereco2') == -1 & element.toLowerCase().indexOf('endereco3') == -1){ if (this.findArray(elementID)){ this.inputs.push({ name:elementID, label: label, match:"#"+elementID+"#", value:value, required: false, id: elementID, type: type, // mask: mask, maxlength, maxlength }); } } } }, save:function(){ self = this; var validate = true; for (var i in self.inputs){ if(self.inputs[i].required && $('#'+self.inputs[i].id).val() == ''){ alert('Campo '+self.inputs[i].label+' obrigatório'); $('#'+self.inputs[i].id).focus(); validate = false; break; }else{ localStorage.setItem($('#'+self.inputs[i].id).data('match'),$('#'+self.inputs[i].id).val()); } } if(validate){ $(self.buttonHandler).click(); } }, createContainer:function(){ $container = $('

Preencha os dados corretamente

'); $container.insertAfter('#produto-descricao-reduzida'); }, capitalize: function (string) { string = string[0].toUpperCase() + string.slice(1); //tem número na string? Ex: Telefone1, Telefone2... hasNumber = string.match(/[0-9]/); if(hasNumber){ //Separa para ficar Telefone 1, Telefone 2, Telefone 3 string = string.replace(hasNumber[0], ' ' + hasNumber[0]); } return string; }, findArray: function (string) { //Verifica se já existe um campo com o nome igual, se tiver não deixa criar o input if (this.inputs.filter(p => p.name == string).length > 0){ return false; }else{ return true; } }, createForm:function(){ self = this; if(!self.inputs.length){ console.error('Nenhum input definido'); }else{ for (var i in self.inputs){ $(self.appendForm).append(self.getInput(self.inputs[i])); } $button = $('
'); $button.css({ 'position':'absolute', 'top':0, 'left':0, 'right':0, 'bottom':0, 'z-index':1, 'cursor':'pointer', 'width': $(self.buttonHandler).width() + 15 }).click(function(event) { event.preventDefault(); form.save(); }); $(self.buttonHandler).css({ 'position':'relative' }).parent().css({ 'position':'relative' }).append($button) } }, getInput:function(input, key){ $field = $('
'); var $input; if(input.type == 'text'){ $input = $(''); } if(input.type == 'select'){ $input = $(''); for (var i in input.value) { $input.append(''); } } $input.attr({ 'name':input.name, 'id':input.id }).data({ 'match':input.match, 'required':input.required, }) if(!!localStorage.getItem(input.match)){ $input.val(localStorage.getItem(input.match)); } if(input.maxlength){ $input.attr({ 'maxlength':input.maxlength, }).data({ 'maxlength':input.maxlength }) } var $label = $(''); $label.attr({ 'for':input.id }).text(input.label); var $span; if(input.maxlength){ $span = $(''+($input.val().length)+'/'+input.maxlength+''); $input.on('input',function(){ max = $(this).data('maxlength'); current = $(this).val().length; $(this).next('span').text(current+'/'+max); }) } // if(input.mask !== false){ // $input.mask(SPMaskBehaviorFlyer, spOptionsFlyer); // } $field.append($label); $field.append($input); $field.append($span); return $field; } } var WhatsApp = { caracterSeparadorDeTelefones: '/', WhatsAppFlag: 'WhatsApp', telefones:[], $footerLocation: false, $headerLocation: false, init:function(opcoes){ if (typeof opcoes !== 'undefined') { this.caracterSeparadorDeTelefones = (typeof opcoes.caracterSeparadorDeTelefones !== 'undefined') ? opcoes.caracterSeparadorDeTelefones : this.caracterSeparadorDeTelefones; this.WhatsAppFlag = (typeof opcoes.WhatsAppFlag !== 'undefined') ? opcoes.WhatsAppFlag : this.WhatsAppFlag; this.telefones = (typeof opcoes.telefones !== 'undefined') ? opcoes.telefones : this.telefones; } this.setTelefones(); this.createTopoElement(); this.createRodapeElement(); }, getTelefones:function(){ var $telefoneHeaderDiv = $('#upper-header .user-actions.clearfix.phone-store'); var $telefoneFooterDiv = $('#footer .col-items .contact-item'); var $telefoneHeader = $telefoneHeaderDiv.find('a:contains("Tel.:")'); var $telefoneFooter = $telefoneFooterDiv.find('a:contains("Tel.:")'); if ($telefoneHeader.length > 0 && $telefoneFooter.length > 0 ) { this.$headerLocation = $telefoneHeaderDiv; this.$footerLocation = $telefoneFooterDiv; return $telefoneFooter.text(); } else if($telefoneFooter.length > 0) { this.$footerLocation = $telefoneFooterDiv; return $telefoneFooter.text(); } else if($telefoneHeader.length) { this.$headerLocation = $telefoneHeaderDiv; return $telefoneHeader.text(); } else { return false; } }, setTelefones:function(){ var self = this; var isWhatsApp = function(telefone){ return telefone.indexOf(self.WhatsAppFlag) > -1; }; var removeWhatsAppFlag = function(telefone){ return telefone.replace(self.WhatsAppFlag,'').trim(); }; if (this.telefones.length <= 0) { var telefones = this.getTelefones(); if (typeof telefones !== 'undefined' ) { telefones = telefones.replace('Tel.:','').trim(); var telefonesArray = telefones.split(this.caracterSeparadorDeTelefones); var telefonesLength = telefonesArray.length; if (telefonesLength > 0) { for (var i = 0; i < telefonesLength; i++) { var telefone = telefonesArray[i]; var telefoneOBJ = {numero:'',href:'',cssClass:'',style:'',target:'',texto:'',isWhatsApp:false}; if (isWhatsApp(telefone)) { telefone = removeWhatsAppFlag(telefone); var href = telefone.match(/[0-9]/g); if (href[0] != 5 || href[1] != 5) { href = '55' + href.join('').toString(); } else { href = href.join(''); } telefoneOBJ = { numero:telefone, href:'href="https://api.whatsapp.com/send?phone=' + href + '"', cssClass:'class="phone-link whatsapp-link"', style:"", target:'target="_blank"', texto: telefone, isWhatsApp:true }; } else { telefoneOBJ = { numero:telefone, href:'href="#"', cssClass:'class="phone-link phone-link-normal"', style:'style="cursor: default; text-decoration: none;"', target:'target="_blank"', texto: telefone, isWhatsApp:false }; } this.telefones.push(telefoneOBJ); } } } } }, removeClickEvent:function($elem){ $elem.on('click', function(event) { event.preventDefault(); }); return $elem; }, createTopoElement:function(){ var self = this; var $location = self.$headerLocation; if ($location.length > 0 & self.telefones.length > 0) { $location.empty(); var elements = []; $.each(self.telefones, function(index, val) { var $a = $('' + val.texto + ''); $a = (val.isWhatsApp === false) ? self.removeClickEvent($a) : $a; elements.push($a); }); $location.append(elements); } }, createRodapeElement:function(){ var $ul = $('