Files
dtf-system/web/site-steps.js
Cauê Faleiros f58b0701b4
All checks were successful
Build and deploy / Validate source (push) Successful in 10s
Build and deploy / Integration suite on a real stack (push) Successful in 8m36s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m13s
fix: a Site script the network dropped is said at once, with a reload, and reported (DTF-WEB-1W)
On an Android phone site-upload.js never arrived, so recusa() was the page's
#recusa element and the product did not open. The last script now checks each
module, shows "Uma parte do site não carregou" with Atualizar, and tells
Sentry which file is missing.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-26 15:43:30 -03:00

169 lines
8.3 KiB
JavaScript

/* Site DTF — moving between the Site's pages (addresses in site-pages.js), the
order progress bar, the product card prices and the phone total bar. It only
switches which page is shown and reads the page's state (the open product,
the cart, the checkout status); orders, prices and files stay with the other
site-*.js files. */
// The Site is twenty scripts loaded in turn: one the phone's network dropped
// leaves its functions missing, and the page breaks at the first click
// (Sentry DTF-WEB-1W, site-upload.js on Android). Said to the customer at
// once, with a reload, and Sentry hears which file did not arrive.
(function(){
const modulos={'site-pages.js':typeof paginaDe,'site-config.js':typeof precoNaMetragem,
'site-videos.js':typeof videoElement,'site-modes.js':typeof escolheProduto,'site-upload.js':typeof recusa,
'site-sheet.js':typeof passoDe,'site-pdf.js':typeof testaWorker,'site-quality.js':typeof perguntaMista,
'packing.js':typeof dilatar,'site-packing.js':typeof carregarImagem,'site-cart.js':typeof artesDoItem,
'site-flow.js':typeof guardaItem,'upload.js':typeof uploadSlot};
const faltando=Object.keys(modulos).filter(nome=>modulos[nome]!=='function');
if(!faltando.length) return;
window.dtfObservability?.uiError?.('O site carregou sem '+faltando.join(', ')+'.','site');
const aviso=document.createElement('div');
aviso.setAttribute('role','alert');
aviso.style.cssText='position:fixed;left:12px;right:12px;bottom:12px;z-index:9999;padding:14px 16px;border-radius:10px;'+
'background:#1f1f1f;color:#fff;font:15px/1.4 system-ui,sans-serif;display:flex;gap:12px;align-items:center;justify-content:space-between';
const texto=document.createElement('span');
texto.textContent='Uma parte do site não carregou, provavelmente a conexão oscilou. Atualize a página para continuar.';
const botao=document.createElement('button');
botao.type='button'; botao.textContent='Atualizar';
botao.style.cssText='flex:none;padding:8px 14px;border:0;border-radius:8px;background:#F28C1E;color:#111;font-weight:700';
botao.addEventListener('click',()=>location.reload());
aviso.append(texto,botao);
document.body.append(aviso);
})();
// Every price on the page (top chip, home example, product cards, price table)
// comes from the same table the checkout uses, so they cannot drift.
document.querySelectorAll('[data-preco]').forEach(el=>{ el.textContent=rs(PRECO[el.dataset.preco]); });
const TITULO_INICIO=document.title;
function tituloPagina(){
const p=paginaDe(location.pathname);
if(p.pagina==='carrinho') return 'Carrinho · DTF Dropstar';
if(p.pagina==='pagamento') return 'Pagamento · DTF Dropstar';
if(p.pagina==='pix') return 'Pagamento com PIX · DTF Dropstar';
if(p.pagina==='produto') return MODOS[p.modo].tit+' · DTF Dropstar';
return TITULO_INICIO;
}
// Shows the page for the current address; `ancora` scrolls to a section,
// otherwise the page opens at `y` (the saved position on Back and Forward).
function mostraPagina(ancora,y,suave){
const p=paginaDe(location.pathname);
if(p.pagina==='produto' && modo!==p.modo) abrir(p.modo);
document.documentElement.dataset.rota=p.pagina;
$('cards').style.display='';
document.title=tituloPagina();
window.dispatchEvent(new Event('dtf-page-changed'));
const el=ancora && document.getElementById(ancora);
if(el) el.scrollIntoView({behavior:suave?'smooth':'auto',block:'start'});
else window.scrollTo(0,y||0);
}
function vaiPara(url){
const u=new URL(url,location.href);
const ancora=u.hash.slice(1)||null;
if(u.pathname+u.search===location.pathname+location.search){
mostraPagina(ancora,0,true);
return;
}
try{ history.replaceState({...(history.state||{}),y:scrollY},''); }catch(e){}
history.pushState({y:0},'',u.pathname+u.search+u.hash);
mostraPagina(ancora,0,false);
}
(function(){
if('scrollRestoration' in history) history.scrollRestoration='manual';
addEventListener('popstate',e=>mostraPagina(e.state?null:location.hash.slice(1)||null,e.state?.y));
// Links to the Site's own pages move inside this document; everything else
// (the account portal, the store, e-mail) is a normal link.
document.addEventListener('click',e=>{
const a=e.target.closest && e.target.closest('a[href]');
if(!a || e.defaultPrevented || e.button || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || a.target) return;
const u=new URL(a.href,location.href);
if(u.origin!==location.origin) return;
const caminho=u.pathname.replace(/\/+$/,'')||'/';
if(caminho!=='/' && caminho!=='/index.html' && paginaDe(caminho).pagina==='inicio') return;
e.preventDefault(); vaiPara(u.pathname+u.search+u.hash);
});
const passos=$('passos'), barra=$('barraM'), vazio=$('carrVazio');
const foco=$('foco'), carr=$('carr'), status=$('checkoutStatus'), acoes=$('checkoutActions');
const rota=()=>document.documentElement.dataset.rota;
const temCheckout=()=>!!(status.textContent||'').trim() || acoes.children.length>0;
// The store header is sticky; the progress bar and the side panels sit below it.
function alturas(){
const topo=document.querySelector('.topo')?.offsetHeight||0;
document.documentElement.style.setProperty('--topo',topo+'px');
document.documentElement.style.setProperty('--topo-passos',(topo+(passos.hidden?0:passos.offsetHeight)+16)+'px');
}
function naTela(el){
const r=el.getBoundingClientRect();
return r.height>0 && r.top<innerHeight && r.bottom>0;
}
// Where the phone bar's button leads: the add-to-cart button on a product
// page, the order summary in the cart.
function alvoBarra(){
if(rota()==='produto' && itemAtual) return $('bAdd');
if(rota()==='carrinho' && (pedido.length || itemAtual) && !temCheckout())
return document.querySelector('.resumo');
return null;
}
let pendente=false;
function pinta(){
pendente=false;
const r=rota();
// With nothing in it, the cart page says so instead of showing an empty form.
const semItens = !pedido.length && !itemAtual;
const carrinhoVazio = r==='carrinho' && semItens && !temCheckout();
document.documentElement.toggleAttribute('data-sem-itens', semItens);
vazio.hidden = !carrinhoVazio;
passos.hidden = r==='inicio' || carrinhoVazio;
alturas();
if(!passos.hidden){
const atual = r==='produto' ? 1 : r==='pagamento' || r==='pix' ? 3 : 2;
passos.querySelectorAll('[data-passo]').forEach(a=>{
const n=Number(a.dataset.passo);
a.classList.toggle('atual',n===atual); a.classList.toggle('feito',n<atual);
const marca=a.querySelector('span');
if(marca) marca.innerHTML = n<atual ? icone('ok') : String(n);
if(n===atual) a.setAttribute('aria-current','step'); else a.removeAttribute('aria-current');
});
passos.querySelector('[data-passo="1"]').href =
modo && foco.classList.contains('on') ? PAGINAS[modo] : '/#envio';
}
const alvo=alvoBarra();
const mostrar=!!alvo && !naTela(alvo);
barra.hidden=!mostrar;
if(mostrar){
$('barraMTot').textContent=$('carrTot').textContent;
$('barraMSub').textContent= r==='produto' ? 'Total até agora' : 'Seu pedido';
}
}
function agenda(){ if(!pendente){ pendente=true; requestAnimationFrame(pinta); } }
const obs=new MutationObserver(agenda);
obs.observe(foco,{attributes:true,attributeFilter:['class']});
obs.observe(carr,{attributes:true,attributeFilter:['class']});
obs.observe($('prev'),{attributes:true,attributeFilter:['class']});
obs.observe(status,{childList:true,characterData:true,subtree:true});
obs.observe(acoes,{childList:true});
obs.observe($('carrTot'),{childList:true,characterData:true,subtree:true});
addEventListener('scroll',agenda,{passive:true});
addEventListener('resize',agenda);
addEventListener('dtf-cart-changed',agenda);
addEventListener('dtf-page-changed',agenda);
// Payment is a step only once the order has been sent.
passos.querySelector('[data-passo="3"]').addEventListener('click',e=>{
let enviado=false;
try{ enviado=!!localStorage.getItem('dtf-quote'); }catch(_){}
if(!enviado){ e.preventDefault(); e.stopPropagation(); }
},true);
$('barraMBtn').addEventListener('click',e=>{
e.preventDefault();
alvoBarra()?.scrollIntoView({behavior:'smooth',block:'center'});
});
try{ history.replaceState({...(history.state||{}),y:0},''); }catch(e){}
mostraPagina(location.hash.slice(1)||null,0);
})();