2023-02-16 09:59:35 +01:00
|
|
|
import invariant from 'tiny-invariant';
|
|
|
|
|
2019-02-13 14:16:22 +01:00
|
|
|
const PENDING_CLASS = 'direct-upload--pending';
|
|
|
|
const ERROR_CLASS = 'direct-upload--error';
|
|
|
|
const COMPLETE_CLASS = 'direct-upload--complete';
|
|
|
|
|
|
|
|
/**
|
|
|
|
ProgressBar is and utility class responsible for
|
|
|
|
rendering upload progress bar. It is used to handle
|
|
|
|
direct-upload form ujs events but also in the
|
|
|
|
Uploader delegate used with uploads on json api.
|
2020-09-03 14:52:42 +02:00
|
|
|
|
|
|
|
As the associated DOM element may disappear for some
|
|
|
|
reason (a dynamic React list, an element being removed
|
|
|
|
and recreated again later, etc.), this class doesn't
|
|
|
|
raise any error if the associated DOM element cannot
|
|
|
|
be found.
|
2019-02-13 14:16:22 +01:00
|
|
|
*/
|
|
|
|
export default class ProgressBar {
|
2022-04-07 17:48:20 +02:00
|
|
|
static init(input: HTMLInputElement, id: string, file: File) {
|
2019-02-13 14:16:22 +01:00
|
|
|
clearErrors(input);
|
|
|
|
const html = this.render(id, file.name);
|
2023-02-16 09:59:35 +01:00
|
|
|
input.before(html);
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
static start(id: string) {
|
2019-02-13 14:16:22 +01:00
|
|
|
const element = getDirectUploadElement(id);
|
2020-09-03 14:52:42 +02:00
|
|
|
if (element) {
|
|
|
|
element.classList.remove(PENDING_CLASS);
|
2021-03-09 18:56:57 +01:00
|
|
|
element.focus();
|
2020-09-03 14:52:42 +02:00
|
|
|
}
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
static progress(id: string, progress: number) {
|
2019-02-13 14:16:22 +01:00
|
|
|
const element = getDirectUploadProgressElement(id);
|
2020-09-03 14:52:42 +02:00
|
|
|
if (element) {
|
|
|
|
element.style.width = `${progress}%`;
|
2022-04-07 17:48:20 +02:00
|
|
|
element.setAttribute('aria-valuenow', `${progress}`);
|
2020-09-03 14:52:42 +02:00
|
|
|
}
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
static error(id: string, error: string) {
|
2019-02-13 14:16:22 +01:00
|
|
|
const element = getDirectUploadElement(id);
|
2020-09-03 14:52:42 +02:00
|
|
|
if (element) {
|
|
|
|
element.classList.add(ERROR_CLASS);
|
|
|
|
element.setAttribute('title', error);
|
|
|
|
}
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
static end(id: string) {
|
2019-02-13 14:16:22 +01:00
|
|
|
const element = getDirectUploadElement(id);
|
2020-09-03 14:52:42 +02:00
|
|
|
if (element) {
|
|
|
|
element.classList.add(COMPLETE_CLASS);
|
|
|
|
}
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
static render(id: string, filename: string) {
|
2023-02-16 09:59:35 +01:00
|
|
|
const template = document.querySelector<HTMLTemplateElement>(
|
|
|
|
'#progress-bar-template'
|
|
|
|
);
|
|
|
|
invariant(template, 'Missing progress-bar-template');
|
|
|
|
const fragment = template.content.cloneNode(true) as DocumentFragment;
|
|
|
|
const container = fragment.querySelector<HTMLDivElement>('.direct-upload');
|
|
|
|
invariant(container, 'Missing .direct-upload element in template');
|
|
|
|
const slot = container.querySelector<HTMLSlotElement>(
|
|
|
|
'slot[name="filename"]'
|
|
|
|
);
|
|
|
|
invariant(slot, 'Missing "filename" slot in template');
|
|
|
|
|
|
|
|
container.id = `direct-upload-${id}`;
|
|
|
|
container.dataset.directUploadId = id;
|
|
|
|
container.classList.add(PENDING_CLASS);
|
|
|
|
slot.replaceWith(document.createTextNode(filename));
|
|
|
|
|
|
|
|
return container;
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
id: string;
|
|
|
|
|
|
|
|
constructor(input: HTMLInputElement, id: string, file: File) {
|
|
|
|
ProgressBar.init(input, id, file);
|
2019-02-13 14:16:22 +01:00
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2022-04-07 17:48:20 +02:00
|
|
|
ProgressBar.start(this.id);
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
progress(progress: number) {
|
|
|
|
ProgressBar.progress(this.id, progress);
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
error(error: string) {
|
|
|
|
ProgressBar.error(this.id, error);
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
2022-04-07 17:48:20 +02:00
|
|
|
ProgressBar.end(this.id);
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
const element = getDirectUploadElement(this.id);
|
2022-04-07 17:48:20 +02:00
|
|
|
element?.remove();
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
function clearErrors(input: HTMLInputElement) {
|
|
|
|
const errorElements =
|
|
|
|
input.parentElement?.querySelectorAll(`.${ERROR_CLASS}`) ?? [];
|
|
|
|
for (const element of errorElements) {
|
2019-02-13 14:16:22 +01:00
|
|
|
element.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
function getDirectUploadElement(id: string) {
|
|
|
|
return document.querySelector<HTMLDivElement>(`#direct-upload-${id}`);
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
function getDirectUploadProgressElement(id: string) {
|
|
|
|
return document.querySelector<HTMLDivElement>(
|
2019-02-13 14:16:22 +01:00
|
|
|
`#direct-upload-${id} .direct-upload__progress`
|
|
|
|
);
|
|
|
|
}
|