Add helper function `fileInitializationFailed`
authorCyperghost <olaf_schmitz_1@t-online.de>
Tue, 2 Jul 2024 08:52:01 +0000 (10:52 +0200)
committerCyperghost <olaf_schmitz_1@t-online.de>
Tue, 2 Jul 2024 08:52:01 +0000 (10:52 +0200)
ts/WoltLabSuite/Core/Component/Attachment/Entry.ts
ts/WoltLabSuite/Core/Component/File/File.ts
ts/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Component/Attachment/Entry.js
wcfsetup/install/files/js/WoltLabSuite/Core/Component/File/File.js
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.js

index d0d5eb0248ba205863cece1432087bd07f325665..6afc50c64d82e0a92660f3ed1c3815c720491078 100644 (file)
@@ -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");
index d975623357602c5729d0b0151c432fb18856c64f..74063f1ae63ff1814e41ebd36475ab2380b6e5d4 100644 (file)
@@ -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);
+}
index faf8d7838d8f9d5158337b581108c0093b25b58d..0784e14da2a8eb9802da32f47547b0acb0fa1519 100644 (file)
@@ -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<string, FileProcessor>();
 
@@ -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 {
index b8ec9fd3a3b959cf957383efcbc79275ab480004..fa387f2fa15deee44fc88a28409b92d704621cce 100644 (file)
@@ -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);
index 7c95d78b4dfe2c9f3d22863cd8f7883caea26464..9830d2e06a10b10fe3d31242737981ef7c5e3454 100644 (file)
@@ -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);
+    }
 });
index 91a8df9ed201502c31f26615a5e445ea817d314b..00e1fd10119aa9d96f36def972e1be746d7e3895 100644 (file)
@@ -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");