(function(){
'use strict';
function ready(fn){
if(document.readyState!=='loading'){
fn();
}else{
document.addEventListener('DOMContentLoaded',fn);
}
}
ready(function(){
document
.querySelectorAll('[data-tribuna-eleicoes="1"]')
.forEach(init);
});
function init(root){
if(!window.TribunaEleicoes)return;
var scope='br';
var turno='1';
var busy=false;
var expanded={};
var requestId=0;
var pollTimer=null;
var grid=root.querySelector('.tre-grid');
var apuracao=root.querySelector('.tre-apuracao');
var status=root.querySelector('.tre-status-text');
var dot=root.querySelector('.tre-status-dot');
var updated=root.querySelector('.tre-updated');
var tabs=root.querySelectorAll('.tre-local-tabs button');
var uf=root.querySelector('.tre-uf-select');
var mun=root.querySelector('.tre-municipio-select');
var inicio=root.querySelector('.tre-inicio');
var share=root.querySelector('.tre-compartilhar');
var urlParams=new URLSearchParams(window.location.search);
var urlScope=urlParams.get('escopo');
var urlTurno=urlParams.get('turno');
var urlUf=urlParams.get('uf');
var urlMunicipio=urlParams.get('municipio');
/*
* ==========================================================
* ESCOPO
* ==========================================================
*/
function setScope(value){
scope=value;
tabs.forEach(function(button){
button.classList.toggle(
'is-active',
button.dataset.scope===scope
);
});
if(scope==='br'){
if(uf)uf.disabled=true;
if(mun)mun.disabled=true;
}
if(scope==='uf'){
if(uf)uf.disabled=false;
if(mun)mun.disabled=true;
}
if(scope==='municipio'){
if(uf)uf.disabled=false;
if(mun)mun.disabled=false;
}
}
function setTurn(value){
turno=String(value);
root.querySelectorAll('.tre-turno button').forEach(function(button){
button.classList.toggle(
'is-active',
button.dataset.turn===turno
);
});
}
function ufExiste(value){
if(!uf || !value)return false;
return Array.prototype.some.call(
uf.options,
function(option){
return String(option.value).toUpperCase()===
String(value).toUpperCase();
}
);
}
/*
* ==========================================================
* MENSAGENS
* ==========================================================
*/
function mensagem(texto,tipo){
if(!grid)return;
grid.innerHTML=
'
'+
esc(texto)+
'
';
}
/*
* ==========================================================
* MUNICÍPIOS
*
* Só consulta quando o usuário entra em MUNICÍPIO.
* Não carrega a lista na abertura da página.
* ==========================================================
*/
function loadMunicipios(state,preferred,autoLoad){
if(!mun || !state)return;
var id=++requestId;
mun.disabled=true;
mun.innerHTML=
'';
var endpoint=
window.TribunaEleicoes.municipiosEndpoint+
'?uf='+encodeURIComponent(state);
fetch(endpoint,{
credentials:'same-origin',
cache:'default'
})
.then(function(response){
if(!response.ok){
throw new Error('HTTP '+response.status);
}
return response.json();
})
.then(function(data){
if(id!==requestId)return;
var list=
Array.isArray(data.municipios)
?data.municipios
:[];
mun.innerHTML='';
var first=document.createElement('option');
first.value='';
first.textContent='Selecione um município';
mun.appendChild(first);
list.forEach(function(item){
var option=document.createElement('option');
option.value=String(item.codigo);
option.textContent=item.nome;
mun.appendChild(option);
});
if(preferred){
var existe=list.some(function(item){
return String(item.codigo)===String(preferred);
});
if(existe){
mun.value=String(preferred);
}
}
mun.disabled=(scope!=='municipio');
if(
autoLoad &&
scope==='municipio' &&
mun.value
){
load(false);
}
if(
scope==='municipio' &&
!mun.value
){
mensagem(
'Selecione um município para consultar os resultados.'
);
}
})
.catch(function(){
if(id!==requestId)return;
mun.innerHTML=
'';
mun.disabled=(scope!=='municipio');
if(autoLoad){
mensagem(
'Não foi possível carregar os municípios agora.',
'error'
);
}
});
}
/*
* ==========================================================
* ABAS
* ==========================================================
*/
tabs.forEach(function(button){
button.addEventListener('click',function(){
expanded={};
var novoScope=button.dataset.scope;
/*
* BRASIL
*/
if(novoScope==='br'){
setScope('br');
load(false);
return;
}
/*
* ESTADO
*/
if(novoScope==='uf'){
setScope('uf');
/*
* BA é apenas fallback caso o seletor esteja vazio.
* Não é carregado nenhum resultado de BA
* enquanto o usuário não estiver na aba Estado.
*/
if(!ufExiste(uf.value)){
var primeiraUF=null;
if(uf && uf.options.length){
for(
var i=0;
i"']/g,
function(match){
return {
'&':'&',
'<':'<',
'>':'>',
'"':'"',
"'":'''
}[match];
}
);
}
/*
* ==========================================================
* APURAÇÃO
* ==========================================================
*/
function trackingBox(tracking,title){
if(!tracking)return '';
var percentual=
tracking.percentual==null
?'—'
:pct(tracking.percentual);
var secoes=
tracking.secoes==null
?'—'
:money(tracking.secoes);
var total=
tracking.secoes_total==null
?'—'
:money(tracking.secoes_total);
var progress=
tracking.percentual==null
?0
:Math.max(
0,
Math.min(
100,
Number(tracking.percentual)
)
);
return (
'
'+
''+
esc(title)+
''+
''+
''+percentual+' dos votos totalizados'+
''+
''+
(
secoes!=='—' &&
total!=='—'
?secoes+' de '+total+
' seções totalizadas'
:'Totalização em atualização pelo TSE'
)+
''+
'