From 64436ee2b325b197499b8931533f68b6dfd91950 Mon Sep 17 00:00:00 2001 From: Miodec Date: Sun, 14 Dec 2025 20:32:03 +0100 Subject: [PATCH] impr(dom utils): rename ondocumentready, add onwindowload !nuf --- frontend/src/ts/controllers/ad-controller.ts | 4 ++-- frontend/src/ts/pages/account-settings.ts | 10 +++------- frontend/src/ts/ready.ts | 4 ++-- frontend/src/ts/utils/dom.ts | 19 +++++++++++++++++-- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/frontend/src/ts/controllers/ad-controller.ts b/frontend/src/ts/controllers/ad-controller.ts index a612a81f0..ca66690dd 100644 --- a/frontend/src/ts/controllers/ad-controller.ts +++ b/frontend/src/ts/controllers/ad-controller.ts @@ -7,7 +7,7 @@ import Config from "../config"; import * as TestState from "../test/test-state"; import * as EG from "./eg-ad-controller"; import * as PW from "./pw-ad-controller"; -import { onDocumentReady, qs } from "../utils/dom"; +import { onDOMReady, qs } from "../utils/dom"; const breakpoint = 900; let widerThanBreakpoint = true; @@ -317,7 +317,7 @@ BannerEvent.subscribe(() => { updateVerticalMargin(); }); -onDocumentReady(() => { +onDOMReady(() => { updateBreakpoint(true); updateBreakpoint2(); }); diff --git a/frontend/src/ts/pages/account-settings.ts b/frontend/src/ts/pages/account-settings.ts index 0fad24100..14f3149d3 100644 --- a/frontend/src/ts/pages/account-settings.ts +++ b/frontend/src/ts/pages/account-settings.ts @@ -12,7 +12,7 @@ import * as BlockedUserTable from "../elements/account-settings/blocked-user-tab import * as Notifications from "../elements/notifications"; import { z } from "zod"; import * as AuthEvent from "../observables/auth-event"; -import { qs, qsr, onDocumentReady } from "../utils/dom"; +import { qs, qsr, onWindowLoad } from "../utils/dom"; const pageElement = qsr(".page.pageAccountSettings"); @@ -242,10 +242,6 @@ export const page = new PageWithUrlParams({ }, }); -onDocumentReady(() => { - setTimeout(() => { - //band aid fix for now, we need to delay saving the skeleton - // to allow the click listeners to be registered first - Skeleton.save("pageAccountSettings"); - }, 0); +onWindowLoad(() => { + Skeleton.save("pageAccountSettings"); }); diff --git a/frontend/src/ts/ready.ts b/frontend/src/ts/ready.ts index d0ca342bc..20f523cdf 100644 --- a/frontend/src/ts/ready.ts +++ b/frontend/src/ts/ready.ts @@ -10,9 +10,9 @@ import { getActiveFunboxesWithFunction } from "./test/funbox/list"; import { configLoadPromise } from "./config"; import { authPromise } from "./firebase"; import { animate } from "animejs"; -import { onDocumentReady, qs } from "./utils/dom"; +import { onDOMReady, qs } from "./utils/dom"; -onDocumentReady(async () => { +onDOMReady(async () => { await configLoadPromise; await authPromise; diff --git a/frontend/src/ts/utils/dom.ts b/frontend/src/ts/utils/dom.ts index 467a7eef2..7955d0378 100644 --- a/frontend/src/ts/utils/dom.ts +++ b/frontend/src/ts/utils/dom.ts @@ -52,10 +52,11 @@ export function qsr( } /** - * Execute a callback function when the document is fully loaded. + * Execute a callback function when the DOM is fully loaded. If you need to wait + * for all resources (images, stylesheets, scripts, etc.) to load, use `onWindowLoad` instead. * If the document is already loaded, the callback is executed immediately. */ -export function onDocumentReady(callback: () => void): void { +export function onDOMReady(callback: () => void): void { if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", callback); } else { @@ -63,6 +64,20 @@ export function onDocumentReady(callback: () => void): void { } } +/** + * Execute a callback function when the window 'load' event fires, which occurs + * after the entire page (including all dependent resources such as images, + * stylesheets, and scripts) has fully loaded. + * If the window is already loaded, the callback is executed immediately. + */ +export function onWindowLoad(callback: () => void): void { + if (document.readyState === "complete") { + callback(); + } else { + window.addEventListener("load", callback); + } +} + /** * Creates an ElementWithUtils wrapping a newly created element. * @param tagName The tag name of the element to create.