hbox-docs/docs/js/sitio.js

34 lines
1.6 KiB
JavaScript

/* Sitio Drogheda: detalles que necesitan datos del servidor en todas las páginas.
- Junto a «Pendientes» en el menú lateral, cuántos hay abiertos (de /api/tareas/resumen).
Si el servidor no responde, el menú queda igual. */
(function () {
function cuentaPendientes() {
var ligas = [].slice.call(document.querySelectorAll('.md-nav--primary a.md-nav__link')).filter(function (a) {
try { return /\/tableros\/pendientes\/?$/.test(new URL(a.href, location.href).pathname); } catch (e) { return false; }
});
if (!ligas.length) return;
fetch('/api/tareas/resumen', { cache: 'no-store', credentials: 'same-origin' })
.then(function (r) { return r.ok ? r.json() : null; })
.then(function (j) {
if (!j || !j.ok || !j.por_pista) return;
var abiertos = 0;
Object.keys(j.por_pista).forEach(function (p) {
var e = j.por_pista[p] || {};
abiertos += (e['por-hacer'] || 0) + (e['en-curso'] || 0) + (e['bloqueado'] || 0);
});
var vencidos = (j.vencidas || []).length;
ligas.forEach(function (a) {
if (a.querySelector('.hg-cuenta')) return;
var s = document.createElement('span');
s.className = 'hg-cuenta' + (vencidos ? ' hg-cuenta--vencidos' : '');
s.textContent = abiertos;
s.title = abiertos + ' abiertos' + (vencidos ? ', ' + vencidos + ' vencido' + (vencidos > 1 ? 's' : '') : '');
a.appendChild(s);
});
})
.catch(function () {});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', cuentaPendientes);
else cuentaPendientes();
})();