-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Download progress indicator #598 #1677
Draft
verheyenkoen
wants to merge
7
commits into
dev
Choose a base branch
from
enhancement/export-progress-indicator
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
57e0ae8
Fixed Bootstrap toast implementation (show/hide via JS, without remov…
verheyenkoen 1cd4959
Upgraded bootstrap.native and refactored popover script
verheyenkoen 69462b1
Added modal error test
verheyenkoen 38585d4
Extracted and fixed file upload modal error template
verheyenkoen 33c0311
Added download progress indicator #598
verheyenkoen f08c5f0
Improved FlashMessage code (removed helper method and incorporated al…
verheyenkoen 04da661
Added possibility to define download file name via download-attribute…
verheyenkoen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,10 @@ | ||
#flash-messages { | ||
position: absolute; | ||
top: 2rem; | ||
top: 12rem; | ||
right: 2rem; | ||
display: flex; | ||
flex-direction: column-reverse; | ||
gap: 2rem; | ||
background-color: transparent; | ||
pointer-events: none; | ||
|
||
.toast { | ||
margin: 10rem 0.3rem 1.3rem 0; | ||
|
||
&.fade { | ||
opacity: 1; | ||
display: block; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,8 @@ | |
display: inline-block; | ||
opacity: 1; | ||
} | ||
|
||
.toast .spinner-border { | ||
display: inline-block; | ||
opacity: 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import showFlashMessage from "./flash_message"; | ||
|
||
export default function fileDownload(el) { | ||
el.querySelectorAll("a[download]").forEach((anchor) => { | ||
anchor.addEventListener("click", async (e) => { | ||
e.preventDefault(); | ||
|
||
const flashMessage = showFlashMessage({ | ||
isLoading: true, | ||
text: "Preparing download...", | ||
isDismissible: false, | ||
toastOptions: { | ||
autohide: false, | ||
}, | ||
}); | ||
|
||
try { | ||
const response = await fetch(anchor.href); | ||
if (!response.ok) { | ||
throw new Error( | ||
"An unexpected error occurred while downloading the file. Please try again.", | ||
); | ||
} | ||
|
||
const filename = extractFileName(response); | ||
const blob = await response.blob(); | ||
|
||
const dummyAnchor = document.createElement("a"); | ||
dummyAnchor.href = URL.createObjectURL(blob); | ||
dummyAnchor.setAttribute("class", "link-primary text-nowrap"); | ||
dummyAnchor.setAttribute("download", filename); | ||
dummyAnchor.textContent = filename; | ||
|
||
flashMessage?.setLevel("success"); | ||
flashMessage?.setIsLoading(false); | ||
flashMessage?.setText("Download ready: " + dummyAnchor.outerHTML); | ||
flashMessage?.setIsDismissible(true); | ||
|
||
// Trigger download (save-as window or auto-download, depending on browser settings) | ||
dummyAnchor.click(); | ||
} catch (error) { | ||
if (flashMessage) { | ||
flashMessage.hide(); | ||
} | ||
|
||
showFlashMessage({ | ||
level: "error", | ||
text: error.message, | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function extractFileName(response: Response) { | ||
const FILENAME_REGEX = /filename\*?=(UTF-8'')?/; | ||
const contentDispositionHeader = response.headers.get("content-disposition"); | ||
|
||
if (contentDispositionHeader) { | ||
const fileNamePart = contentDispositionHeader | ||
.split(";") | ||
.find((n) => n.match(FILENAME_REGEX)); | ||
|
||
if (fileNamePart) { | ||
const fileName = fileNamePart.replace(FILENAME_REGEX, "").trim(); | ||
return decodeURIComponent(fileName); | ||
} | ||
} | ||
|
||
return ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { Toast, type ToastOptions } from "bootstrap.native"; | ||
|
||
const levelMap = { | ||
success: "success", | ||
info: "primary", | ||
warning: "warning", | ||
error: "error", | ||
} as const; | ||
|
||
type Level = keyof typeof levelMap; | ||
|
||
type FlashMessageOptions = { | ||
level?: Level; | ||
isLoading?: boolean; | ||
title?: string; | ||
text: string; | ||
isDismissible?: boolean; | ||
toastOptions?: Partial<ToastOptions>; | ||
}; | ||
|
||
export default function showFlashMessage({ | ||
level, | ||
isLoading = false, | ||
title, | ||
text, | ||
isDismissible = true, | ||
toastOptions, | ||
}: FlashMessageOptions) { | ||
let flashMessages = document.querySelector("#flash-messages"); | ||
if (flashMessages) { | ||
/* | ||
* Expects somewhere in the document .. | ||
* | ||
* <template id="template-flash-message"></template> | ||
* | ||
* .. a template that encapsulates the toast | ||
* */ | ||
const templateFlashMessage = document.querySelector<HTMLTemplateElement>( | ||
"template#template-flash-message", | ||
); | ||
|
||
if (templateFlashMessage) { | ||
const toastEl = ( | ||
templateFlashMessage.content.cloneNode(true) as HTMLElement | ||
).querySelector<HTMLElement>(".toast"); | ||
|
||
if (toastEl) { | ||
const flashMessage = new FlashMessage(toastEl); | ||
|
||
flashMessage.setLevel(level); | ||
flashMessage.setIsLoading(isLoading); | ||
flashMessage.setTitle(title); | ||
flashMessage.setText(text); | ||
flashMessage.setIsDismissible(isDismissible); | ||
|
||
flashMessages.appendChild(toastEl); | ||
|
||
flashMessage.show(toastOptions); | ||
|
||
return flashMessage; | ||
} | ||
} | ||
} | ||
} | ||
|
||
class FlashMessage { | ||
#toastEl: HTMLElement; | ||
#toast: Toast | null; | ||
|
||
constructor(readonly toastEl: HTMLElement) { | ||
this.#toastEl = toastEl; | ||
} | ||
|
||
setLevel(level: Level | undefined) { | ||
this.#toastEl.querySelectorAll(".toast-body i.if").forEach((el) => { | ||
el.classList.add("d-none"); | ||
}); | ||
|
||
if (level && Object.keys(levelMap).includes(level)) { | ||
this.#toastEl | ||
.querySelector(`.if--${levelMap[level]}`) | ||
?.classList.remove("d-none"); | ||
} | ||
} | ||
|
||
setIsLoading(isLoading: boolean) { | ||
this.#toastEl | ||
.querySelector(".spinner-border") | ||
?.classList.toggle("d-none", !isLoading); | ||
} | ||
|
||
setTitle(title: string | undefined) { | ||
const titleEl = this.#toastEl.querySelector(".alert-title"); | ||
if (titleEl) { | ||
if (title) { | ||
titleEl.classList.remove("d-none"); | ||
titleEl.textContent = title; | ||
} else { | ||
titleEl.classList.add("d-none"); | ||
} | ||
} | ||
} | ||
|
||
setText(text: string) { | ||
const textEl = this.#toastEl.querySelector(".toast-text"); | ||
if (textEl) { | ||
textEl.innerHTML = text; | ||
} | ||
} | ||
|
||
setIsDismissible(isDismissible: boolean) { | ||
const btnClose = this.#toastEl.querySelector(".btn-close"); | ||
if (btnClose) { | ||
btnClose.classList.toggle("d-none", !isDismissible); | ||
} | ||
} | ||
|
||
setAutohide(autohide: boolean, delay = 5000) { | ||
if (this.#toast) { | ||
this.#toast.options.autohide = autohide; | ||
this.#toast.options.delay = delay; | ||
this.#toast.show(); | ||
} | ||
} | ||
|
||
show(toastOptions: Partial<ToastOptions> = {}) { | ||
this.#toast = | ||
Toast.getInstance(this.#toastEl) ?? | ||
new Toast(this.#toastEl, toastOptions); | ||
|
||
this.#toast.show(); | ||
|
||
this.#toastEl.addEventListener("hidden.bs.toast", () => { | ||
this.#toastEl.remove(); | ||
}); | ||
} | ||
|
||
hide() { | ||
if (this.#toast) { | ||
this.#toast.hide(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,43 @@ | ||
import { Modal } from "bootstrap.native"; | ||
|
||
export default function (error) { | ||
let modals = document.querySelector("#modals"); | ||
if (modals) { | ||
/* | ||
* Expects somewhere in the document .. | ||
* | ||
* <template id="template-modal-error"></template> | ||
* | ||
* .. a template that encapsulates the modal body | ||
* */ | ||
const templateModalError = document.querySelector( | ||
"template#template-modal-error", | ||
); | ||
|
||
if (!modals) return; | ||
if (templateModalError) { | ||
const modalEl = templateModalError.content | ||
.cloneNode(true) | ||
.querySelector(".modal"); | ||
|
||
/* | ||
* Expects somewhere in the document .. | ||
* | ||
* <template class="template-modal-error"></template> | ||
* | ||
* .. a template that encapsulates the modal body | ||
* */ | ||
let templateModalError = document.querySelector( | ||
"template.template-modal-error", | ||
); | ||
modalEl.querySelector(".msg").textContent = error; | ||
|
||
if (!templateModalError) return; | ||
modals.innerHTML = ""; | ||
modals.appendChild(modalEl); | ||
|
||
let modal = templateModalError.content.cloneNode(true); | ||
initModal(modalEl); | ||
} | ||
} | ||
} | ||
|
||
// modal-close not triggered for dynamically added modals | ||
modal.querySelector(".modal-close").addEventListener("click", function () { | ||
modals.innerHTML = ""; | ||
function initModal(modalEl) { | ||
const modal = new Modal(modalEl, { | ||
backdrop: "static", | ||
keyboard: false, | ||
}); | ||
|
||
modal.querySelector(".msg").textContent = error; | ||
modal.show(); | ||
|
||
modals.innerHTML = ""; | ||
modals.appendChild(modal); | ||
modalEl.addEventListener("hidden.bs.modal", function () { | ||
modalEl.remove(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,19 @@ | ||
import htmx from "htmx.org/dist/htmx.esm.js"; | ||
import { Popover } from "bootstrap.native"; | ||
import Popover from "bootstrap.native/popover"; | ||
|
||
export default function () { | ||
let addEvents = function (rootEl) { | ||
rootEl | ||
.querySelectorAll("[data-bs-toggle=popover-custom]") | ||
.forEach(function (el) { | ||
let container = document.querySelector(el.dataset.popoverContent); | ||
let content = container.querySelector(".popover-body"); | ||
let heading = container.querySelector(".popover-heading"); | ||
let title = ""; | ||
if (heading) { | ||
title = heading.innerHTML; | ||
} | ||
new Popover(el, { | ||
content: content.innerHTML, | ||
title: title, | ||
delay: 1000, | ||
}); | ||
}); | ||
}; | ||
htmx.onLoad((rootEl) => { | ||
rootEl.querySelectorAll("[data-bs-toggle=popover-custom]").forEach((el) => { | ||
const container = document.querySelector(el.dataset.popoverContent); | ||
const content = container.querySelector(".popover-body"); | ||
const heading = container.querySelector(".popover-heading"); | ||
const title = heading?.innerHTML ?? ""; | ||
|
||
htmx.onLoad(addEvents); | ||
new Popover(el, { | ||
content: content.innerHTML, | ||
title, | ||
delay: 1000, | ||
}); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a hack that's used widespread. The click on the
<a>
tag is prevented at the top of this function so we can intervene and display the toast first. Then the file is downloaded via thefetch()
API. When ready, we create a dummy anchor tag using a data URL with the downloaded file. At the end, theclick()
method is triggered on that which will actually present the download to the browser. The dummy anchor is also included in the updated toast, but that is not necessary. Just an alternate way to get the file, in case you canceled the save-as-dialog.Tested in Chrome, Firefox, Safari and Edge.