From 98901ce38ec230a718030455d45ef7858ff51af6 Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Wed, 11 Jun 2025 23:45:19 +0200 Subject: [PATCH] also fixes flash messages --- scipaperloader/static/js/common.js | 55 +++++++++++++++++++----------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/scipaperloader/static/js/common.js b/scipaperloader/static/js/common.js index 2b9c8af..5f1cb45 100644 --- a/scipaperloader/static/js/common.js +++ b/scipaperloader/static/js/common.js @@ -12,7 +12,7 @@ function showFlashMessage(message, type = "success", duration = 5000) { const flashMsg = document.createElement("div"); const normalizedType = type === "error" ? "danger" : type; flashMsg.className = `flash-overlay flash-${normalizedType}`; - + // Get the appropriate icon based on type const getIcon = (messageType) => { switch (messageType) { @@ -33,37 +33,52 @@ function showFlashMessage(message, type = "success", duration = 5000) {
${getIcon(normalizedType)}
${message}
- +
`; - // Handle stacking of multiple messages - const existingMessages = document.querySelectorAll('.flash-overlay'); - existingMessages.forEach((msg, index) => { - msg.style.setProperty('--flash-index', index + 1); - }); - - // Add to page + // Add to page first document.body.appendChild(flashMsg); + // Position all messages in stack + updateFlashMessagePositions(); + // Auto dismiss setTimeout(() => { - flashMsg.classList.add("fade-out"); - setTimeout(() => { - if (flashMsg.parentNode) { - flashMsg.remove(); - // Reposition remaining messages - const remainingMessages = document.querySelectorAll('.flash-overlay'); - remainingMessages.forEach((msg, index) => { - msg.style.setProperty('--flash-index', index); - }); - } - }, 300); + removeFlashMessage(flashMsg); }, duration); return flashMsg; } +/** + * Remove a flash message and update positions + * @param {HTMLElement} flashMsg - The flash message element to remove + */ +function removeFlashMessage(flashMsg) { + if (!flashMsg || !flashMsg.parentNode) return; + + flashMsg.classList.add("fade-out"); + setTimeout(() => { + if (flashMsg.parentNode) { + flashMsg.remove(); + updateFlashMessagePositions(); + } + }, 300); +} + +/** + * Update positions of all flash messages to create a proper stack + */ +function updateFlashMessagePositions() { + const messages = document.querySelectorAll(".flash-overlay:not(.fade-out)"); + messages.forEach((msg, index) => { + const topPosition = 20 + index * 90; // 90px spacing between messages + msg.style.top = `${topPosition}px`; + msg.style.zIndex = 9999 - index; // Higher z-index for newer messages + }); +} + /** * Create a status badge HTML element * @param {string} status - The status to create a badge for