- Every open page in the member area asks live.php every 20 s while it is visible (assets/js/live.js). The counts at the menu items and the own Lumipunkte are replaced in place; header.php and live.php compute them with the same functions (nav_badges(), nav_points() in includes/live_updates.php, links marked with data-badge). - Dashboard, gallery, picture page, profile, member list, calendar, shop, shop orders, Lumipunkte, warehouse, premades, bug reports, moderation, user management and wiki get a fingerprint of the data they show (count, highest id, checksum of the shown columns - no "last active"). When it changes, the page reloads - but only while nobody is typing, has a form started, a dialog, lightbox or menu open or clicked in the last 8 s; otherwise a notice "Es gibt Neues auf dieser Seite" with a reload button, and it reloads once things are quiet. At most 3 automatic reloads per minute. - Tools and forms (map, gear planner, edit pages) never reload on their own; the raid page keeps its own live updates. The dashboard notices new raids and signups through the raid cache (now also stores a fingerprint of dates, slots and signups); premades ask the bot with a 3 s timeout (new timeout parameter for premades_api_request/fetch_premades). - README and patch notes updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
8.2 KiB
JavaScript
192 lines
8.2 KiB
JavaScript
/**
|
|
* Live-Aktualisierung im Mitgliederbereich: Solange die Seite zu sehen ist, fragt sie alle 20 Sekunden
|
|
* bei live.php nach (includes/live_updates.php).
|
|
* - Die Zahlen an den Menuepunkten und der eigene Punktestand werden direkt ersetzt.
|
|
* - Hat sich der Inhalt der Seite geaendert (Fingerabdruck in data-live-fp), laedt sie neu - aber nur,
|
|
* wenn gerade niemand tippt, etwas ausfuellt, ein Fenster offen hat oder in den letzten Sekunden
|
|
* etwas angeklickt hat. Sonst erscheint unten ein Hinweis mit "Aktualisieren".
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var INTERVAL = 20000; // Abstand der Nachfragen
|
|
var QUIET = 8000; // so lange nach der letzten Eingabe gilt die Seite als ruhig
|
|
var RETRY = 4000; // Wartet ein Neuladen, so oft nachsehen, ob es jetzt passt
|
|
|
|
var body = document.body;
|
|
var path = body.getAttribute('data-live-path') || location.pathname;
|
|
var fingerprint = body.getAttribute('data-live-fp') || '';
|
|
var timer = 0;
|
|
var retryTimer = 0;
|
|
var lastPoll = 0;
|
|
var inFlight = false;
|
|
var lastInput = Date.now();
|
|
var pending = false;
|
|
var stopped = false;
|
|
var notice = null;
|
|
|
|
// Auf- oder zugeklappte Bereiche gingen beim Neuladen verloren - Ausgangszustand merken
|
|
var details = Array.prototype.map.call(document.querySelectorAll('details'), function (el) {
|
|
return { el: el, open: el.open };
|
|
});
|
|
|
|
['pointerdown', 'keydown', 'wheel', 'touchstart', 'input'].forEach(function (type) {
|
|
window.addEventListener(type, function () { lastInput = Date.now(); }, { capture: true, passive: true });
|
|
});
|
|
|
|
// --- Darf die Seite gerade neu laden? -------------------------------------------------
|
|
|
|
function fieldChanged(field) {
|
|
if (field.closest('[data-live-ignore]')) return false;
|
|
if (field.tagName === 'SELECT') {
|
|
var fallback = field.multiple ? -1 : 0;
|
|
for (var i = 0; i < field.options.length; i++) {
|
|
var option = field.options[i];
|
|
if (field.multiple && option.selected !== option.defaultSelected) return true;
|
|
if (!field.multiple && option.defaultSelected) fallback = i;
|
|
}
|
|
return !field.multiple && field.selectedIndex !== fallback;
|
|
}
|
|
switch (field.type) {
|
|
case 'hidden': case 'submit': case 'button': case 'reset': case 'image':
|
|
return false;
|
|
case 'checkbox': case 'radio':
|
|
return field.checked !== field.defaultChecked;
|
|
case 'file':
|
|
return !!(field.files && field.files.length);
|
|
default:
|
|
return field.value !== field.defaultValue;
|
|
}
|
|
}
|
|
|
|
function busy() {
|
|
if (Date.now() - lastInput < QUIET) return true;
|
|
var active = document.activeElement;
|
|
if (active && active.matches && active.matches('textarea, select, [contenteditable], input:not([type=checkbox]):not([type=radio]):not([type=button]):not([type=submit])')) return true;
|
|
// Offene Fenster, Rueckfragen, Grossansicht, Einfuehrung, Menue-Bearbeitung, aufgeklapptes Handy-Menue
|
|
if (document.querySelector('.modal-overlay:not([hidden]), .lightbox:not([hidden]), .tour-root, #success-popup, dialog[open], .sidebar-nav.is-editing, .sidebar.open')) return true;
|
|
if (body.classList.contains('lumina-dialog-open')) return true;
|
|
if (Array.prototype.some.call(document.querySelectorAll('input, textarea, select'), fieldChanged)) return true;
|
|
if (details.some(function (d) { return d.el.isConnected && d.el.open !== d.open; })) return true;
|
|
var selection = window.getSelection ? String(window.getSelection()) : '';
|
|
return selection.trim() !== '';
|
|
}
|
|
|
|
// Bremse: hoechstens 3 automatische Neuladungen pro Minute (falls sich etwas staendig aendert)
|
|
function reloadAllowed() {
|
|
try {
|
|
var now = Date.now();
|
|
var recent = JSON.parse(sessionStorage.getItem('lumina-live-reloads') || '[]').filter(function (t) { return now - t < 60000; });
|
|
if (recent.length >= 3) return false;
|
|
recent.push(now);
|
|
sessionStorage.setItem('lumina-live-reloads', JSON.stringify(recent));
|
|
} catch (e) {
|
|
// ohne Speicher eben ohne Bremse
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function showNotice() {
|
|
if (notice) return;
|
|
notice = document.createElement('div');
|
|
notice.className = 'live-notice';
|
|
notice.setAttribute('role', 'status');
|
|
notice.innerHTML = '<span>Es gibt Neues auf dieser Seite.</span>' +
|
|
'<button type="button" class="button button-small">Aktualisieren</button>';
|
|
notice.querySelector('button').addEventListener('click', function () { window.location.reload(); });
|
|
body.appendChild(notice);
|
|
}
|
|
|
|
function applyPending() {
|
|
window.clearTimeout(retryTimer);
|
|
retryTimer = 0;
|
|
if (!pending || document.hidden) return;
|
|
if (!busy() && reloadAllowed()) {
|
|
window.location.reload();
|
|
return;
|
|
}
|
|
showNotice();
|
|
retryTimer = window.setTimeout(applyPending, RETRY);
|
|
}
|
|
|
|
// --- Zahlen im Menue -----------------------------------------------------------------
|
|
|
|
function updateBadges(badges) {
|
|
Array.prototype.forEach.call(document.querySelectorAll('[data-badge]'), function (link) {
|
|
var want = badges[link.getAttribute('data-badge')];
|
|
var badge = link.querySelector('.sidebar-badge');
|
|
if (!want) {
|
|
if (badge) badge.remove();
|
|
return;
|
|
}
|
|
if (!badge) {
|
|
badge = document.createElement('span');
|
|
link.appendChild(badge);
|
|
}
|
|
badge.className = want['class'];
|
|
badge.title = want.title;
|
|
badge.textContent = want.text;
|
|
});
|
|
}
|
|
|
|
function updatePoints(points) {
|
|
var el = document.querySelector('[data-live-points]');
|
|
if (el && typeof points === 'number') el.textContent = points.toLocaleString('de-DE');
|
|
}
|
|
|
|
// --- Nachfragen ------------------------------------------------------------------------
|
|
|
|
function schedule(ms) {
|
|
window.clearTimeout(timer);
|
|
timer = window.setTimeout(poll, ms);
|
|
}
|
|
|
|
function poll() {
|
|
timer = 0;
|
|
if (stopped || inFlight || document.hidden) return;
|
|
lastPoll = Date.now();
|
|
inFlight = true;
|
|
var url = '/live.php?path=' + encodeURIComponent(path) + '&q=' + encodeURIComponent(location.search.replace(/^\?/, ''));
|
|
fetch(url, { credentials: 'same-origin', cache: 'no-store', headers: { Accept: 'application/json' } })
|
|
.then(function (res) {
|
|
inFlight = false;
|
|
if (res.status === 401) {
|
|
stopped = true; // abgemeldet - beim naechsten Klick fuehrt die Seite zur Anmeldung
|
|
return null;
|
|
}
|
|
if (res.status === 503) {
|
|
window.location.reload(); // Wartungsmodus: die Seite zeigt dann die Wartungsseite
|
|
return null;
|
|
}
|
|
if (!res.ok) throw new Error('HTTP ' + res.status);
|
|
return res.json();
|
|
})
|
|
.then(function (data) {
|
|
if (!data || stopped) return;
|
|
if (data.badges) updateBadges(data.badges);
|
|
updatePoints(data.points);
|
|
if (data.fp) {
|
|
if (!fingerprint) {
|
|
fingerprint = data.fp;
|
|
} else if (data.fp !== fingerprint) {
|
|
pending = true;
|
|
applyPending();
|
|
}
|
|
}
|
|
schedule(INTERVAL);
|
|
})
|
|
.catch(function () {
|
|
inFlight = false;
|
|
schedule(INTERVAL * 3); // Netz oder Server gerade weg - seltener nachfragen
|
|
});
|
|
}
|
|
|
|
// Wieder sichtbar (Tab gewechselt, Handy entsperrt): gleich nachsehen statt auf den Takt zu warten
|
|
document.addEventListener('visibilitychange', function () {
|
|
if (document.hidden || stopped) return;
|
|
if (pending) applyPending();
|
|
if (Date.now() - lastPoll > 5000) schedule(0);
|
|
});
|
|
|
|
schedule(INTERVAL);
|
|
})();
|