51 linhas
2,1 KiB
JavaScript
51 linhas
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.mozoutStatusStatus;
|
|
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('.mozout_status__banner');
|
|
var list = box.querySelector('.mozout_status__list');
|
|
if (!banner || !list) { return; }
|
|
banner.className = 'mozout_status__banner is-' + d.overall;
|
|
banner.querySelector('strong').textContent = d.title;
|
|
banner.querySelector('.mozout_status__time').textContent = d.time;
|
|
var ul = el('ul', 'mozout_status__list');
|
|
d.items.forEach(function (it) {
|
|
var li = el('li', 'mozout_status__item is-' + it.state);
|
|
var dot = el('span', 'mozout_status__dot');
|
|
dot.setAttribute('aria-hidden', 'true');
|
|
li.appendChild(dot);
|
|
li.appendChild(el('span', 'mozout_status__name', it.label));
|
|
li.appendChild(el('span', 'mozout_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('.mozout-status-box[data-refresh]').forEach(setup); }
|
|
if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); }
|
|
})();
|