wordpress-status-plugin/interweb-ao-status/assets/status.js
2026-12-01 14:09:12 +02:00

51 lines
2.1 KiB
JavaScript

/* Service status: refreshes the box from this WordPress, which asks the API with the key. */
(function () {
'use strict';
var cfg = window.interwebAoStatusStatus;
if (!cfg) { return; }
function el(tag, cls, text) {
var n = document.createElement(tag);
if (cls) { n.className = cls; }
if (text) { n.textContent = text; }
return n;
}
function paint(box, d) {
var banner = box.querySelector('.interweb_ao_status__banner');
var list = box.querySelector('.interweb_ao_status__list');
if (!banner || !list) { return; }
banner.className = 'interweb_ao_status__banner is-' + d.overall;
banner.querySelector('strong').textContent = d.title;
banner.querySelector('.interweb_ao_status__time').textContent = d.time;
var ul = el('ul', 'interweb_ao_status__list');
d.items.forEach(function (it) {
var li = el('li', 'interweb_ao_status__item is-' + it.state);
var dot = el('span', 'interweb_ao_status__dot');
dot.setAttribute('aria-hidden', 'true');
li.appendChild(dot);
li.appendChild(el('span', 'interweb_ao_status__name', it.label));
li.appendChild(el('span', 'interweb_ao_status__state', it.text));
ul.appendChild(li);
});
list.parentNode.replaceChild(ul, list);
}
function setup(box) {
var every = parseInt(box.getAttribute('data-refresh'), 10) || 0;
if (every < 30) { return; }
var url = cfg.url + (cfg.url.indexOf('?') > -1 ? '&' : '?') + 'lang=' + cfg.lang + '&show=' + encodeURIComponent(box.getAttribute('data-show') || '');
function tick() {
if (document.hidden) { return; }
fetch(url, { credentials: 'same-origin' })
.then(function (r) { return r.ok ? r.json() : null; })
.then(function (d) { if (d && d.ok) { paint(box, d); } })
.catch(function () {});
}
setInterval(tick, every * 1000);
document.addEventListener('visibilitychange', function () { if (!document.hidden) { tick(); } });
}
function init() { document.querySelectorAll('.interweb-ao-status-box[data-refresh]').forEach(setup); }
if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); }
})();