refactor: remove unnecessary DOMContentLoaded wrappers

Since main.js has 'defer' attribute, the DOM is guaranteed to be
ready when modules execute. These wrappers are unnecessary.

dashboard.ts:
- Removed DOMContentLoaded wrapper, code runs directly
- Event delegation setup runs immediately

custom-section-builder.ts:
- Removed DOMContentLoaded wrapper
- initCustomSectionBuilder() called directly

toast.ts:
- Removed DOMContentLoaded wrapper
- initializeToastSystem() called directly at top level
- Removed dead Alpine.data registration (unused)

search.ts:
- Removed DOMContentLoaded wrapper
- initializeSearch exported for use in header

theme.ts:
- Removed DOMContentLoaded wrapper
- Functions now exported for use in header Alpine component
This commit is contained in:
2026-03-13 12:51:16 -04:00
parent 41e7445524
commit 2075077bb7
5 changed files with 64 additions and 94 deletions
+1 -1
View File
@@ -651,6 +651,6 @@ function builderEscapeHtml(text: string): string {
return div.innerHTML; return div.innerHTML;
} }
document.addEventListener("DOMContentLoaded", initCustomSectionBuilder); initCustomSectionBuilder();
export { addBookToSelection, removeBookFromSelection }; export { addBookToSelection, removeBookFromSelection };
+54 -56
View File
@@ -425,65 +425,63 @@ function initDragAndDrop(): void {
}); });
} }
document.addEventListener("DOMContentLoaded", () => { initDragAndDrop();
initDragAndDrop();
document.addEventListener("click", (e: Event) => { document.addEventListener("click", (e: Event) => {
const target = e.target as HTMLElement; const target = e.target as HTMLElement;
const actionElem = target.closest("[data-action]") as HTMLElement; const actionElem = target.closest("[data-action]") as HTMLElement;
const action = actionElem?.getAttribute("data-action"); const action = actionElem?.getAttribute("data-action");
switch (action) { switch (action) {
case "scroll-carousel": { case "scroll-carousel": {
const collectionId = const collectionId =
target.dataset.collectionId || actionElem?.dataset.collectionId; target.dataset.collectionId || actionElem?.dataset.collectionId;
const direction = parseInt( const direction = parseInt(
target.dataset.direction || actionElem?.dataset.direction || "0", target.dataset.direction || actionElem?.dataset.direction || "0",
); );
if (collectionId) scrollCarousel(collectionId, direction); if (collectionId) scrollCarousel(collectionId, direction);
break; break;
}
case "open-dashboard-settings":
openDashboardSettings();
break;
case "close-dashboard-settings":
closeDashboardSettings();
break;
case "save-dashboard-settings":
saveDashboardSettings();
break;
case "restore-system-collection": {
const colName =
actionElem?.dataset.collectionName || target.dataset.collectionName;
const colTitle =
actionElem?.dataset.collectionTitle ||
target.dataset.collectionTitle ||
"System Collection";
if (colName) restoreSystemCollection(colName, colTitle);
break;
}
case "view-book": {
const bookId = target.dataset.bookId || actionElem?.dataset.bookId;
if (bookId) viewBook(bookId);
break;
}
case "reload-page":
reloadPage();
break;
case "switch-library": {
const select = target as HTMLSelectElement;
if (select.value) switchLibrary(select.value);
break;
}
} }
});
case "open-dashboard-settings":
openDashboardSettings();
break;
case "close-dashboard-settings":
closeDashboardSettings();
break;
case "save-dashboard-settings":
saveDashboardSettings();
break;
case "restore-system-collection": {
const colName =
actionElem?.dataset.collectionName || target.dataset.collectionName;
const colTitle =
actionElem?.dataset.collectionTitle ||
target.dataset.collectionTitle ||
"System Collection";
if (colName) restoreSystemCollection(colName, colTitle);
break;
}
case "view-book": {
const bookId = target.dataset.bookId || actionElem?.dataset.bookId;
if (bookId) viewBook(bookId);
break;
}
case "reload-page":
reloadPage();
break;
case "switch-library": {
const select = target as HTMLSelectElement;
if (select.value) switchLibrary(select.value);
break;
}
}
document.addEventListener("input", (e: Event) => { document.addEventListener("input", (e: Event) => {
const target = e.target as HTMLElement; const target = e.target as HTMLElement;
+2 -3
View File
@@ -303,10 +303,9 @@ function selectLibraryAndBook(libraryId: string, bookId: string): void {
hideSearchResults(); hideSearchResults();
} }
document.addEventListener("DOMContentLoaded", initializeSearch); export { selectLibraryAndBook, initializeSearch };
export { selectLibraryAndBook };
Alpine.data("search", () => ({ Alpine.data("search", () => ({
selectLibraryAndBook, selectLibraryAndBook,
initializeSearch,
})); }));
+6 -15
View File
@@ -202,24 +202,15 @@ const updateWoodPanelingIndicators = (): void => {
}); });
}; };
// Auto-initialize when DOM is ready
if (typeof document !== "undefined") {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () => {
initializeTheme();
loadWoodPaneling();
updateWoodPanelingIndicators();
});
} else {
initializeTheme();
loadWoodPaneling();
updateWoodPanelingIndicators();
}
}
Alpine.data("theme", () => ({ Alpine.data("theme", () => ({
applyTheme,
changeTheme, changeTheme,
changeWoodPaneling, changeWoodPaneling,
initializeTheme,
loadTheme,
loadUserTheme,
loadWoodPaneling,
updateWoodPanelingIndicators,
})); }));
export { export {
+1 -19
View File
@@ -1,4 +1,3 @@
import { Alpine } from "./alpine";
// Toast notification system for backend errors // Toast notification system for backend errors
// Displays toast notifications at the top of the page // Displays toast notifications at the top of the page
@@ -216,24 +215,7 @@ const initializeToastSystem = (): void => {
setupFetchInterceptor(); setupFetchInterceptor();
}; };
// Auto-initialize when DOM is ready initializeToastSystem();
if (typeof document !== "undefined") {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initializeToastSystem);
} else {
initializeToastSystem();
}
}
// Export toast API for manual use
Alpine.data("showToast", () => ({
error: (message: string, duration?: number) =>
showToast(message, "error", duration),
success: (message: string, duration?: number) =>
showToast(message, "success", duration),
info: (message: string, duration?: number) =>
showToast(message, "info", duration),
}));
export { showToast }; export { showToast };
export type { ToastType }; export type { ToastType };