class MudiExperience { constructor(){ this.counterElementsDom = 0; this.color = '#e52a2b'; this.dataServer= null; this.pathName = location.pathname; this.sku = null; this.fatherContainer = null; this.device= null }; /** Conect mudiServer ✔️ */ async conectServer(){ if (!this.sku) return; const myBody = {"skus":[this.sku]}; try { /** We make the request to the MUDI server */ const request = await fetch('https://mudiview.mudi.com.co:7443/product/getProductsUrl',{ method:'POST', headers:{ "Content-type":"application/json", "tokenapi":"LXhG4UEYLjdz5fiN3AUr" }, body: JSON.stringify(myBody) }) const response = await request.json(); this.dataServer = response.data[0]; } catch (error) {console.error(`Mudi Error:\n${error}`)} }; /** Create Styles ✔️ */ createStyles(){ /** Verify element HTML */ if(document.head.querySelector('#stylesMudiGeneral'))return; const link = document.createElement('LINK'); link.setAttribute('rel','stylesheet'); link.id="stylesMudiGeneral"; link.href=`https://viewer.mudi.com.co/implementations/electroJaponesa/index.css`; document.head.appendChild(link); }; /** Crear botón 3D ✔️*/ createBtns(){ /** Verify btns */ document.body.querySelector('.btnsMudiContainer') && document.body.querySelector('.btnsMudiContainer').remove(); /** Create Fragment */ const fragment = document.createDocumentFragment(); /** Create containers */ const containerBtns = document.createElement('DIV'); containerBtns.classList.add('btnsMudiContainer'); containerBtns.appendChild(this.createTooltip()); containerBtns.innerHTML +=` `; containerBtns.querySelector('#img3DBtn').addEventListener('click',()=>{ this.createModal(); this.sendEventInteraction('3D'); }); fragment.appendChild(containerBtns) /** Add DOM */ this.fatherContainer.appendChild(fragment) }; /** Create tooltip ✔️ */ createTooltip(){ const tooltip = document.createElement('P'); tooltip.classList.add('tooltipMudi'); tooltip.innerHTML=`¡Nuevo! Descubre como se ve este producto en 3D y realidad aumentada en tu espacio`; setTimeout(()=>{ document.body.querySelector('.tooltipMudi').remove(); },9000) return tooltip; }; /** Create Modal ✔️ */ createModal(){ /** create variables */ let flagAR = false; /** We create a shell for the MUDI modal */ const modalMudi = document.createElement('DIV'); modalMudi.id=`modalMudi`; modalMudi.classList.add(`mudiModal`); modalMudi.innerHTML=`
VER EN TU ESPACIO

ESCANÉAME PARA
VER EN TU ESPACIO


Apunta el teléfono al piso.

Desplaza para visualizar.

Amplia y detalla el producto.

Toca dos veces para restablecer.

`; /** We close the MUDI modal*/ modalMudi.querySelector(`.closeModalMudi`).addEventListener('click',()=>{ document.body.querySelector('#modalMudi').remove(); }); /** Init ARExperience */ modalMudi.querySelector(`#imgARBtn`).addEventListener('click',()=>{ if(window.innerWidth>1000){ !flagAR ? ( document.body.querySelector('.containerQRMudi').style.right="0%", changeStyleBtnAR(flagAR,this.color), flagAR = !flagAR ) : ( document.body.querySelector('.containerQRMudi').style.right="-150%", changeStyleBtnAR(flagAR,this.color), flagAR = !flagAR ) } else { window.open(`${this.dataServer.URL_AR}`,"_BLANK"); } flagAR && this.sendEventInteraction('AR') }); /** Verify Style Bttn AR */ function changeStyleBtnAR(flagAR,color){ let icon = document.body.querySelectorAll('.cls-3_modal') flagAR ? ( document.body.querySelector('.cls-1_modal').style.fill=color, icon.forEach((icon)=>icon.style.fill="white"), document.body.querySelector('.cls-2_modal').style.fill="white" ) : ( document.body.querySelector('.cls-1_modal').style.fill="white", icon.forEach((icon)=>icon.style.fill=color), document.body.querySelector('.cls-2_modal').style.fill=color ) }; document.body.appendChild(modalMudi) }; /** Send Evnt Interacción ✔️ */ sendEventInteraction(eventName){ let OSdevice; if (navigator.userAgent.includes('Android')) OSdevice = 'Android'; else if (navigator.userAgent.includes('iPhone') || navigator.userAgent.includes('iPad')) OSdevice = "IOS"; else OSdevice = 'DESK'; window.dataLayer && dataLayer.push({ event: `Evento de interaccion ${eventName}`, valorMudi: 1, sku: this.skuNumber, sistemaOperativo: OSdevice }) }; /** viewer event Mudi GTM ✔️ */ sendEventViewer(){ let OSdevice; if (navigator.userAgent.includes('Android')) OSdevice = 'Android'; else if (navigator.userAgent.includes('iPhone') || navigator.userAgent.includes('iPad')) OSdevice = "IOS"; else OSdevice = 'DESK'; window.dataLayer && dataLayer.push({ event: `visualizacion_botones`, valorMudi: 1, sku: this.skuNumber, sistemaOperativo: OSdevice }) }; /** Verificación de los elementos del DOM ✔️ */ verifyDomElements(){ const container = document.querySelector('.vtex-store-components-3-x-productImagesGallerySwiperContainer'); const sku = document.querySelector('body > div.render-container.render-route-store-product > div > div.vtex-store__template.bg-base > div > div > div > div:nth-child(4) > div > div.vtex-render__container-id-product-main > div > section > div > div:nth-child(2) > div > div:nth-child(3) > div > div > div.pr0.items-stretch.vtex-flex-layout-0-x-stretchChildrenWidth.flex > span > span.vtex-product-identifier-0-x-product-identifier__value'); const mobileSKu = document.querySelector('body > div.render-container.render-route-store-product > div > div.vtex-store__template.bg-base > div > div > div > div:nth-child(4) > div > div.vtex-render__container-id-product-main > div > section > div > div:nth-child(2) > div > div:nth-child(3) > div > div > div.pr0-ns.pb0.pb0-ns.items-stretch.vtex-flex-layout-0-x-stretchChildrenWidth.flex > span > span.vtex-product-identifier-0-x-product-identifier__value'); if( !container ){ this.counterElementsDom > 5 ? ( console.warn('No se encontró el número de SKU o el contenedor para la experiencia 3D y AR') ):( this.counterElementsDom ++, setTimeout( this.verifyDomElements , 5000) ) }else{ this.fatherContainer = container; this.sku = sku ? sku.innerHTML : mobileSKu.innerHTML; !this.sku ? console.log('no tenemos todos los atributos para iniciar la experiencia 3D') : this.experienceOn(); } }; /** Verificar la ruta ✔️ */ verifyPathName(){ setInterval(()=>{ const pathName = location.pathname; const productPage = pathName.split('/') const isProductPage = productPage[productPage.length-1]=='p' this.pathName !== pathName && isProductPage && ( this.pathName = pathName, this.verifyDomElements() ) },5000); }; /** Re Desing HTML */ redesingHTML(){ const share = document.querySelector('body > div.render-container.render-route-store-product > div > div.vtex-store__template.bg-base > div > div > div > div:nth-child(4) > div > div.vtex-render__container-id-product-main > div > section > div > div:nth-child(1) > div > div:nth-child(1) > div > div.vtex-stack-layout-0-x-stackItem.vtex-stack-layout-0-x-stackItem--product.absolute.top-0.left-0.w-auto.h-auto.vtex-stack-layout-0-x-stackItem.vtex-stack-layout-0-x-stackItem--product.vtex-stack-layout-0-x-stackItem--product-share.vtex-stack-layout-0-x-stackItem--product--product-share') const container = document.querySelector('body > div.render-container.render-route-store-product > div > div.vtex-store__template.bg-base > div > div > div > div:nth-child(4) > div > div.vtex-render__container-id-product-main > div > section > div > div:nth-child(1) > div > div:nth-child(1) > div > div.vtex-stack-layout-0-x-stackItem.vtex-stack-layout-0-x-stackItem--product.absolute.top-0.left-0.w-auto.h-auto.vtex-stack-layout-0-x-stackItem.vtex-stack-layout-0-x-stackItem--product.vtex-stack-layout-0-x-stackItem--product-summary__flags.vtex-stack-layout-0-x-stackItem--product--product-summary__flags') if(share && container){ const element = share.children[0] container.appendChild(element) container.setAttribute('style','display:flex; flex-direction:column; gap:10px; z-index:1'); } }; /** verifyExperience ✔️ */ async experienceOn(){ /** Response Mudi server */ await this.conectServer(); /** verify process */ if (!this.dataServer){ document.body.querySelector('.btnsMudiContainer') && document.body.querySelector('.btnsMudiContainer').remove(); console.warn(`El sku: ${this.sku} no posee experiencias de 3D y AR`) return; } /** Re diseñamos el HTML */ this.redesingHTML(); /** Create Styles */ this.createStyles(); /** Create Buttons */ this.createBtns(); /** Enviar evento de visualización */ this.sendEventViewer(); /** VErificando URL */ this.verifyPathName(); }; }; const mudiExperience = new MudiExperience(); setTimeout(()=>{ mudiExperience.verifyDomElements(); mudiExperience.verifyPathName(); },3000)