From: Cyperghost Date: Tue, 2 Jul 2024 08:52:01 +0000 (+0200) Subject: Add helper function `fileInitializationFailed` X-Git-Tag: 6.1.0_Alpha_1~41^2~11 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=2d429fda761a4b875ac761755c53378a526cfe4f;p=GitHub%2FWoltLab%2FWCF.git Add helper function `fileInitializationFailed` --- diff --git a/ts/WoltLabSuite/Core/Component/Attachment/Entry.ts b/ts/WoltLabSuite/Core/Component/Attachment/Entry.ts index d0d5eb0248..6afc50c64d 100644 --- a/ts/WoltLabSuite/Core/Component/Attachment/Entry.ts +++ b/ts/WoltLabSuite/Core/Component/Attachment/Entry.ts @@ -5,7 +5,11 @@ import DomChangeListener from "WoltLabSuite/Core/Dom/Change/Listener"; import { dispatchToCkeditor } from "../Ckeditor/Event"; import { deleteFile } from "WoltLabSuite/Core/Api/Files/DeleteFile"; import { getPhrase } from "WoltLabSuite/Core/Language"; -import { removeUploadProgress, trackUploadProgress } from "WoltLabSuite/Core/Component/File/File"; +import { + fileInitializationFailed, + removeUploadProgress, + trackUploadProgress, +} from "WoltLabSuite/Core/Component/File/File"; type FileProcessorData = { attachmentID: number; @@ -150,45 +154,6 @@ function getInsertButton(attachmentId: number, url: string, editor: HTMLElement) return button; } -function fileInitializationFailed(element: HTMLElement, file: WoltlabCoreFileElement, reason: unknown): void { - if (reason instanceof Error) { - throw reason; - } - - if (file.apiError === undefined) { - return; - } - - let errorMessage: string; - - const validationError = file.apiError.getValidationError(); - if (validationError !== undefined) { - switch (validationError.param) { - case "preflight": - errorMessage = getPhrase(`wcf.upload.error.${validationError.code}`); - break; - - default: - errorMessage = "Unrecognized error type: " + JSON.stringify(validationError); - break; - } - } else { - errorMessage = `Unexpected server error: [${file.apiError.type}] ${file.apiError.message}`; - } - - markElementAsErroneous(element, errorMessage); -} - -function markElementAsErroneous(element: HTMLElement, errorMessage: string): void { - element.classList.add("fileList__item--error"); - - const errorElement = document.createElement("div"); - errorElement.classList.add("attachemnt__item__errorMessage"); - errorElement.textContent = errorMessage; - - element.append(errorElement); -} - export function createAttachmentFromFile(file: WoltlabCoreFileElement, editor: HTMLElement) { const element = document.createElement("li"); element.classList.add("fileList__item", "attachment__item"); diff --git a/ts/WoltLabSuite/Core/Component/File/File.ts b/ts/WoltLabSuite/Core/Component/File/File.ts index d975623357..74063f1ae6 100644 --- a/ts/WoltLabSuite/Core/Component/File/File.ts +++ b/ts/WoltLabSuite/Core/Component/File/File.ts @@ -1,4 +1,5 @@ import WoltlabCoreFileElement from "WoltLabSuite/Core/Component/File/woltlab-core-file"; +import { getPhrase } from "WoltLabSuite/Core/Language"; export function trackUploadProgress(element: HTMLElement, file: WoltlabCoreFileElement): void { const progress = document.createElement("progress"); @@ -31,3 +32,42 @@ export function removeUploadProgress(element: HTMLElement): void { element.classList.remove("fileProcessor__item--uploading"); element.querySelector(".fileList__item__progress")?.remove(); } + +export function fileInitializationFailed(element: HTMLElement, file: WoltlabCoreFileElement, reason: unknown): void { + if (reason instanceof Error) { + throw reason; + } + + if (file.apiError === undefined) { + return; + } + + let errorMessage: string; + + const validationError = file.apiError.getValidationError(); + if (validationError !== undefined) { + switch (validationError.param) { + case "preflight": + errorMessage = getPhrase(`wcf.upload.error.${validationError.code}`); + break; + + default: + errorMessage = "Unrecognized error type: " + JSON.stringify(validationError); + break; + } + } else { + errorMessage = `Unexpected server error: [${file.apiError.type}] ${file.apiError.message}`; + } + + markElementAsErroneous(element, errorMessage); +} + +function markElementAsErroneous(element: HTMLElement, errorMessage: string): void { + element.classList.add("fileList__item--error"); + + const errorElement = document.createElement("div"); + errorElement.classList.add("attachemnt__item__errorMessage"); + errorElement.textContent = errorMessage; + + element.append(errorElement); +} diff --git a/ts/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.ts b/ts/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.ts index faf8d7838d..0784e14da2 100644 --- a/ts/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.ts +++ b/ts/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.ts @@ -10,7 +10,11 @@ import { getPhrase } from "WoltLabSuite/Core/Language"; import { deleteFile } from "WoltLabSuite/Core/Api/Files/DeleteFile"; import { formatFilesize } from "WoltLabSuite/Core/FileUtil"; import DomChangeListener from "WoltLabSuite/Core/Dom/Change/Listener"; -import { removeUploadProgress, trackUploadProgress } from "WoltLabSuite/Core/Component/File/File"; +import { + fileInitializationFailed, + removeUploadProgress, + trackUploadProgress, +} from "WoltLabSuite/Core/Component/File/File"; const _data = new Map(); @@ -107,36 +111,9 @@ export class FileProcessor { } #markElementUploadHasFailed(container: HTMLElement, element: WoltlabCoreFileElement, reason: unknown): void { - if (reason instanceof Error) { - throw reason; - } - if (element.apiError === undefined) { - return; - } - let errorMessage: string; - - const validationError = element.apiError.getValidationError(); - if (validationError !== undefined) { - switch (validationError.param) { - case "preflight": - errorMessage = getPhrase(`wcf.upload.error.${validationError.code}`); - break; - - default: - errorMessage = "Unrecognized error type: " + JSON.stringify(validationError); - break; - } - } else { - errorMessage = `Unexpected server error: [${element.apiError.type}] ${element.apiError.message}`; - } + fileInitializationFailed(container, element, reason); container.classList.add("innerError"); - - const errorElement = document.createElement("div"); - errorElement.classList.add(this.classPrefix + "item__errorMessage"); - errorElement.textContent = errorMessage; - - element.append(errorElement); } protected addDeleteButton(element: WoltlabCoreFileElement, buttons: HTMLUListElement): void { diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Attachment/Entry.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Attachment/Entry.js index b8ec9fd3a3..fa387f2fa1 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Attachment/Entry.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Attachment/Entry.js @@ -110,37 +110,6 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/FileUtil", "WoltLabSui }); return button; } - function fileInitializationFailed(element, file, reason) { - if (reason instanceof Error) { - throw reason; - } - if (file.apiError === undefined) { - return; - } - let errorMessage; - const validationError = file.apiError.getValidationError(); - if (validationError !== undefined) { - switch (validationError.param) { - case "preflight": - errorMessage = (0, Language_1.getPhrase)(`wcf.upload.error.${validationError.code}`); - break; - default: - errorMessage = "Unrecognized error type: " + JSON.stringify(validationError); - break; - } - } - else { - errorMessage = `Unexpected server error: [${file.apiError.type}] ${file.apiError.message}`; - } - markElementAsErroneous(element, errorMessage); - } - function markElementAsErroneous(element, errorMessage) { - element.classList.add("fileList__item--error"); - const errorElement = document.createElement("div"); - errorElement.classList.add("attachemnt__item__errorMessage"); - errorElement.textContent = errorMessage; - element.append(errorElement); - } function createAttachmentFromFile(file, editor) { const element = document.createElement("li"); element.classList.add("fileList__item", "attachment__item"); @@ -159,7 +128,7 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/FileUtil", "WoltLabSui fileInitializationCompleted(element, file, editor); }) .catch((reason) => { - fileInitializationFailed(element, file, reason); + (0, File_1.fileInitializationFailed)(element, file, reason); }) .finally(() => { (0, File_1.removeUploadProgress)(element); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/File/File.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/File/File.js index 7c95d78b4d..9830d2e06a 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/File/File.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/File/File.js @@ -1,7 +1,7 @@ -define(["require", "exports"], function (require, exports) { +define(["require", "exports", "WoltLabSuite/Core/Language"], function (require, exports, Language_1) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); - exports.removeUploadProgress = exports.trackUploadProgress = void 0; + exports.fileInitializationFailed = exports.removeUploadProgress = exports.trackUploadProgress = void 0; function trackUploadProgress(element, file) { const progress = document.createElement("progress"); progress.classList.add("fileList__item__progress__bar"); @@ -29,4 +29,36 @@ define(["require", "exports"], function (require, exports) { element.querySelector(".fileList__item__progress")?.remove(); } exports.removeUploadProgress = removeUploadProgress; + function fileInitializationFailed(element, file, reason) { + if (reason instanceof Error) { + throw reason; + } + if (file.apiError === undefined) { + return; + } + let errorMessage; + const validationError = file.apiError.getValidationError(); + if (validationError !== undefined) { + switch (validationError.param) { + case "preflight": + errorMessage = (0, Language_1.getPhrase)(`wcf.upload.error.${validationError.code}`); + break; + default: + errorMessage = "Unrecognized error type: " + JSON.stringify(validationError); + break; + } + } + else { + errorMessage = `Unexpected server error: [${file.apiError.type}] ${file.apiError.message}`; + } + markElementAsErroneous(element, errorMessage); + } + exports.fileInitializationFailed = fileInitializationFailed; + function markElementAsErroneous(element, errorMessage) { + element.classList.add("fileList__item--error"); + const errorElement = document.createElement("div"); + errorElement.classList.add("attachemnt__item__errorMessage"); + errorElement.textContent = errorMessage; + element.append(errorElement); + } }); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.js index 91a8df9ed2..00e1fd1011 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.js @@ -77,32 +77,8 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/Language", "WoltLabSui container.append(buttons); } #markElementUploadHasFailed(container, element, reason) { - if (reason instanceof Error) { - throw reason; - } - if (element.apiError === undefined) { - return; - } - let errorMessage; - const validationError = element.apiError.getValidationError(); - if (validationError !== undefined) { - switch (validationError.param) { - case "preflight": - errorMessage = (0, Language_1.getPhrase)(`wcf.upload.error.${validationError.code}`); - break; - default: - errorMessage = "Unrecognized error type: " + JSON.stringify(validationError); - break; - } - } - else { - errorMessage = `Unexpected server error: [${element.apiError.type}] ${element.apiError.message}`; - } + (0, File_1.fileInitializationFailed)(container, element, reason); container.classList.add("innerError"); - const errorElement = document.createElement("div"); - errorElement.classList.add(this.classPrefix + "item__errorMessage"); - errorElement.textContent = errorMessage; - element.append(errorElement); } addDeleteButton(element, buttons) { const deleteButton = document.createElement("button");