45 lines
1.9 KiB
JavaScript
45 lines
1.9 KiB
JavaScript
window.rustProxyOverviewSamples = (() => {
|
|
const allowed = new Set([60, 120, 300, 600]);
|
|
const storageKey = "rustproxy.overview.liveChartSamples";
|
|
const factory = typeof ADMIN_DASHBOARD_DEFAULTS === "object" ? Number(ADMIN_DASHBOARD_DEFAULTS.overview_samples) : 60;
|
|
let limit = allowed.has(factory) ? factory : 60;
|
|
try {
|
|
const stored = Number(localStorage.getItem(storageKey));
|
|
if (allowed.has(stored)) limit = stored;
|
|
} catch { /* Private browsing or storage policy: selection still works for this visit. */ }
|
|
const callbacks = new Set();
|
|
const getLimit = () => limit;
|
|
const trim = array => {
|
|
if (Array.isArray(array) && array.length > limit) array.splice(0, array.length - limit);
|
|
return array;
|
|
};
|
|
function sync() {
|
|
const select = document.getElementById("overview-live-samples");
|
|
if (select) select.value = String(limit);
|
|
}
|
|
function setLimit(value, persist = false) {
|
|
const count = Number(value);
|
|
if (!allowed.has(count)) return false;
|
|
const changed = count !== limit;
|
|
limit = count;
|
|
if (persist) {
|
|
try {
|
|
localStorage.setItem(storageKey, String(count));
|
|
} catch {
|
|
if (typeof toast === "function") toast(t("overviewSamplesStorageUnavailable"), true);
|
|
}
|
|
}
|
|
if (changed) for (const callback of callbacks) callback(limit);
|
|
sync();
|
|
return true;
|
|
}
|
|
function init() {
|
|
const select = document.getElementById("overview-live-samples");
|
|
select?.addEventListener("change", () => setLimit(select.value, true));
|
|
sync();
|
|
}
|
|
if (document.readyState !== "complete") document.addEventListener("DOMContentLoaded", init, { once: true });
|
|
else queueMicrotask(init);
|
|
return { getLimit, trim, setLimit, onChange: callback => callbacks.add(callback) };
|
|
})();
|