From 42a4c69c7fcc1a32aec4fbdc90bf1574b6d2b173 Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Wed, 18 Dec 2024 10:31:14 +0100 Subject: [PATCH] Add a callback that is triggered when the value of a FileProcessorFormField is changed. --- .../Builder/Field/Controller/FileProcessor.ts | 32 +++++++++++++++++++ .../Builder/Field/Controller/FileProcessor.js | 25 +++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/ts/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.ts b/ts/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.ts index 18e541f2b6..b618c51c0c 100644 --- a/ts/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.ts +++ b/ts/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.ts @@ -20,6 +20,7 @@ import { innerError } from "WoltLabSuite/Core/Dom/Util"; type FileId = string; const fileProcessors = new Map(); +const callbacks = new Map) => void)[]>(); export interface ExtraButton { title: string; @@ -197,6 +198,8 @@ export class FileProcessor { const result = await deleteFile(element.fileId!); if (result.ok) { this.#unregisterFile(element); + + notifyValueChange(this.#fieldId, this.values); } else { let container: HTMLElement = element; if (!this.#useBigPreview) { @@ -357,6 +360,8 @@ export class FileProcessor { container.append(input); this.addButtons(container, element); + + notifyValueChange(this.#fieldId, this.values); } get values(): undefined | number | Set { @@ -395,3 +400,30 @@ export function getValues(fieldId: string): undefined | number | Set { return field.values; } + +/** + * Registers a callback that will be called when the value of the field changes. + * + * @since 6.2 + */ +export function registerCallback(fieldId: string, callback: (values: undefined | number | Set) => void): void { + if (!callbacks.has(fieldId)) { + callbacks.set(fieldId, []); + } + + callbacks.get(fieldId)!.push(callback); +} + +/** + * @since 6.2 + */ +export function unregisterCallback( + fieldId: string, + callback: (values: undefined | number | Set) => void, +): void { + callbacks.set(fieldId, callbacks.get(fieldId)?.filter((registeredCallback) => registeredCallback !== callback) ?? []); +} + +function notifyValueChange(fieldId: string, values: undefined | number | Set): void { + callbacks.get(fieldId)?.forEach((callback) => callback(values)); +} 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 a9badcb066..e9ce9108d5 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 @@ -9,8 +9,11 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/Language", "WoltLabSui Object.defineProperty(exports, "__esModule", { value: true }); exports.FileProcessor = void 0; exports.getValues = getValues; + exports.registerCallback = registerCallback; + exports.unregisterCallback = unregisterCallback; Listener_1 = tslib_1.__importDefault(Listener_1); const fileProcessors = new Map(); + const callbacks = new Map(); class FileProcessor { #container; #uploadButton; @@ -145,6 +148,7 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/Language", "WoltLabSui const result = await (0, DeleteFile_1.deleteFile)(element.fileId); if (result.ok) { this.#unregisterFile(element); + notifyValueChange(this.#fieldId, this.values); } else { let container = element; @@ -282,6 +286,7 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/Language", "WoltLabSui input.value = element.fileId.toString(); container.append(input); this.addButtons(container, element); + notifyValueChange(this.#fieldId, this.values); } get values() { if (this.#singleFileUpload) { @@ -311,4 +316,24 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/Language", "WoltLabSui } return field.values; } + /** + * Registers a callback that will be called when the value of the field changes. + * + * @since 6.2 + */ + function registerCallback(fieldId, callback) { + if (!callbacks.has(fieldId)) { + callbacks.set(fieldId, []); + } + callbacks.get(fieldId).push(callback); + } + /** + * @since 6.2 + */ + function unregisterCallback(fieldId, callback) { + callbacks.set(fieldId, callbacks.get(fieldId)?.filter((registeredCallback) => registeredCallback !== callback) ?? []); + } + function notifyValueChange(fieldId, values) { + callbacks.get(fieldId)?.forEach((callback) => callback(values)); + } }); -- 2.20.1