mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-27 00:11:35 +00:00
feat(dashboard): change pages without reloading the dashboard
nav.js (loaded on every signed-in page) follows links and GET forms to other dashboard pages by fetching the page and swapping in its <main>, title, page styles and page scripts; the sidebar, top bar and live WebSocket stay. History entries, back/forward (with scroll positions, also kept for a reload), deep links, #hash filters and breadcrumbs keep working. What the old page set up is taken down first: its document/window listeners, setInterval timers, Live watchers and topics, DataTables, dialogs and what it appended to <body>. Page scripts run again in order; DOMContentLoaded/load handlers they add run once they have all run. A thin bar shows while loading; a failed fetch shows an inline error with Try again / Open it normally. Falls back to a normal load for anything that isn't a signed-in dashboard page, when the account or permissions changed, and for pages with module scripts or an import map (World 3D, property view and 3D, UGC server) or data-nav="reload", and when leaving those. Unsaved-change prompts (beforeunload) are asked before swapping. Check: click around the sidebar and into accounts/characters: no white flash, the footer's live indicator stays "live", back/forward return to the same scroll position, breadcrumbs follow the path (Accounts > an account > a character). Activity Log links with #search= still filter. System Log's server picker works. Leaving Settings with a change asks first. World 3D and a property's 3D view still load (normally). Pages that poll (Server Health, Instance Load) stop polling once left (browser network tab). Test: NavRulesJs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,9 +7,10 @@
|
||||
* - moderation_counts : moderation queue sizes for the sidebar badges (GM 3+)
|
||||
*
|
||||
* Pages call Live.watchTable(name, dataTable), Live.on(name, callback) or Live.refreshPage(names, id) to react to
|
||||
* changes. Game servers report their writes as they happen (world -> master -> dashboard), and the dashboard also
|
||||
* diffs a database snapshot for writers outside the game. After a reconnect every watcher is told to resync, since
|
||||
* changes may have been missed while the socket was down.
|
||||
* changes; what a page registers lasts until nav.js swaps in another page (Live.resetPage). Game servers report their
|
||||
* writes as they happen (world -> master -> dashboard), and the dashboard also diffs a database snapshot for writers
|
||||
* outside the game. After a reconnect every watcher is told to resync, since changes may have been missed while the
|
||||
* socket was down.
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
@@ -25,6 +26,9 @@
|
||||
var actionWaiters = {}; // requestId -> callback for actions started from this tab
|
||||
var topicWatchers = {}; // extra topics a page asked for (e.g. player_positions) -> [callback]
|
||||
var wasConnected = false;
|
||||
var pageGeneration = 0; // goes up on every page change; callbacks of a page that's gone do nothing
|
||||
// The restart countdown lives across page changes, so it keeps the browser's own timer (nav.js stops the page's)
|
||||
var coreSetInterval = window.setInterval.bind(window);
|
||||
|
||||
function setIndicator(text, cls) {
|
||||
var el = document.getElementById('live-indicator');
|
||||
@@ -213,7 +217,7 @@
|
||||
banner.textContent = 'Server restart in ' + text + (restart.reason ? ': ' + restart.reason : '') + (restart.by ? ' (scheduled by ' + restart.by + ')' : '');
|
||||
}
|
||||
tick();
|
||||
restartTimer = setInterval(tick, 1000);
|
||||
restartTimer = coreSetInterval(tick, 1000);
|
||||
}
|
||||
|
||||
window.Live = {
|
||||
@@ -229,7 +233,8 @@
|
||||
},
|
||||
// Reload a DataTable (keeping its page) whenever the named table changes
|
||||
watchTable: function (table, dataTable) {
|
||||
Live.on(table, throttle(function () { dataTable.ajax.reload(null, false); }, RELOAD_THROTTLE_MS));
|
||||
var generation = pageGeneration;
|
||||
Live.on(table, throttle(function () { if (generation === pageGeneration) dataTable.ajax.reload(null, false); }, RELOAD_THROTTLE_MS));
|
||||
},
|
||||
/**
|
||||
* For server-rendered pages about one row: reload the page when that row changes in any of the given tables
|
||||
@@ -237,7 +242,11 @@
|
||||
*/
|
||||
refreshPage: function (tables, id, options) {
|
||||
options = options || {};
|
||||
var refresh = throttle(function () { if (isBusy()) showStaleBanner(); else reloadKeepingScroll(); }, PAGE_REFRESH_MIN_MS);
|
||||
var generation = pageGeneration;
|
||||
var refresh = throttle(function () {
|
||||
if (generation !== pageGeneration) return;
|
||||
if (isBusy()) showStaleBanner(); else reloadKeepingScroll();
|
||||
}, PAGE_REFRESH_MIN_MS);
|
||||
[].concat(tables).forEach(function (table) {
|
||||
Live.on(table, function (e) {
|
||||
if (e.resync || options.anyRow || String(e.id) === String(id)) refresh();
|
||||
@@ -246,6 +255,22 @@
|
||||
},
|
||||
throttle: throttle,
|
||||
onStatus: function (cb) { statusWatchers.push(cb); },
|
||||
// Forget what the page registered (nav.js, when it swaps in another page). Topics only it asked for are dropped.
|
||||
resetPage: function () {
|
||||
pageGeneration++;
|
||||
tableWatchers = {};
|
||||
statusWatchers = [];
|
||||
Object.keys(topicWatchers).forEach(function (topic) {
|
||||
if (ws && ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify({ event: 'unsubscribe', subscription: topic }));
|
||||
});
|
||||
topicWatchers = {};
|
||||
var stale = document.getElementById('live-stale');
|
||||
if (stale) stale.remove();
|
||||
},
|
||||
// The sidebar's waiting counts again (nav.js, when the menu was rebuilt)
|
||||
refreshBadges: function () {
|
||||
if (DASH.can('moderate_names')) api.get('/api/moderation/counts').then(updateBadges).catch(function () {});
|
||||
},
|
||||
/**
|
||||
* Wait for background work (a scan, a search) and fetch its result, including its data, which only the account
|
||||
* that started it can read. Resolves with {success, message, data}. Keeps checking with a growing interval
|
||||
|
||||
Reference in New Issue
Block a user