From e1bb8fee00e65da717c0d1eeda2c0973c574151e Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 6 Dec 2020 14:41:40 +0100 Subject: [PATCH] Convert `Form/Builder/Manager` to TypeScript --- .../js/WoltLabSuite/Core/Form/Builder/Data.js | 4 + .../WoltLabSuite/Core/Form/Builder/Manager.js | 115 ++++------ .../ts/WoltLabSuite/Core/Form/Builder/Data.ts | 25 +++ .../WoltLabSuite/Core/Form/Builder/Manager.js | 201 ------------------ .../WoltLabSuite/Core/Form/Builder/Manager.ts | 175 +++++++++++++++ 5 files changed, 250 insertions(+), 270 deletions(-) create mode 100644 wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Data.js create mode 100644 wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Data.ts delete mode 100644 wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.js create mode 100644 wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.ts diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Data.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Data.js new file mode 100644 index 0000000000..2ae92b6a8b --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Data.js @@ -0,0 +1,4 @@ +define(["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); +}); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Manager.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Manager.js index 8877880bd5..eef3fa8213 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Manager.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Manager.js @@ -2,57 +2,50 @@ * Manager for registered Ajax forms and its fields that can be used to retrieve the current data * of the registered forms. * - * @author Matthias Schmidt - * @copyright 2001-2020 WoltLab GmbH - * @license GNU Lesser General Public License - * @module WoltLabSuite/Core/Form/Builder/Manager - * @since 5.2 + * @author Matthias Schmidt + * @copyright 2001-2020 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Form/Builder/Manager + * @since 5.2 */ -define([ - 'Core', - 'Dictionary', - 'EventHandler', - './Field/Dependency/Manager', - './Field/Field' -], function (Core, Dictionary, EventHandler, FormBuilderFieldDependencyManager, FormBuilderField) { +define(["require", "exports", "tslib", "../../Core", "../../Event/Handler", "./Field/Field", "./Field/Dependency/Manager"], function (require, exports, tslib_1, Core, EventHandler, Field_1, Manager_1) { "use strict"; - var _fields = new Dictionary(); - var _forms = new Dictionary(); - return { + Core = tslib_1.__importStar(Core); + EventHandler = tslib_1.__importStar(EventHandler); + Field_1 = tslib_1.__importDefault(Field_1); + Manager_1 = tslib_1.__importDefault(Manager_1); + const _fields = new Map(); + const _forms = new Map(); + const FormBuilderManager = { /** * Returns a promise returning the data of the form with the given id. - * - * @param {string} formId - * @return {Promise} */ - getData: function (formId) { + getData(formId) { if (!this.hasForm(formId)) { throw new Error("Unknown form with id '" + formId + "'."); } - var promises = []; + const promises = []; _fields.get(formId).forEach(function (field) { - var fieldData = field.getData(); + const fieldData = field.getData(); if (!(fieldData instanceof Promise)) { throw new TypeError("Data for field with id '" + field.getId() + "' is no promise."); } promises.push(fieldData); }); return Promise.all(promises).then(function (promiseData) { - var data = {}; - for (var i = 0, length = promiseData.length; i < length; i++) { - data = Core.extend(data, promiseData[i]); - } + let data = {}; + promiseData.forEach((_data) => { + data = Core.extend(data, _data); + }); return data; }); }, /** - * Returns the registered form field with given id. + * Returns the registered form field with given. * - * @param {string} formId - * @return {WoltLabSuite/Core/Form/Builder/Field/Field} - * @since 5.2.3 + * @since 5.2.3 */ - getField: function (formId, fieldId) { + getField(formId, fieldId) { if (!this.hasField(formId, fieldId)) { throw new Error("Unknown field with id '" + formId + "' for form with id '" + fieldId + "'."); } @@ -60,103 +53,87 @@ define([ }, /** * Returns the registered form with given id. - * - * @param {string} formId - * @return {HTMLElement} */ - getForm: function (formId) { + getForm(formId) { if (!this.hasForm(formId)) { throw new Error("Unknown form with id '" + formId + "'."); } return _forms.get(formId); }, /** - * Returns `true` if a field with the given id has been registered for the form with - * the given id and `false` otherwise. - * - * @param {string} formId - * @param {string} fieldId - * @return {boolean} + * Returns `true` if a field with the given id has been registered for the form with the given id + * and `false` otherwise. */ - hasField: function (formId, fieldId) { + hasField(formId, fieldId) { if (!this.hasForm(formId)) { throw new Error("Unknown form with id '" + formId + "'."); } return _fields.get(formId).has(fieldId); }, /** - * Returns `true` if a form with the given id has been registered and `false` - * otherwise. - * - * @param {string} formId - * @return {boolean} + * Returns `true` if a form with the given id has been registered and `false` otherwise. */ - hasForm: function (formId) { + hasForm(formId) { return _forms.has(formId); }, /** * Registers the given field for the form with the given id. - * - * @param {string} formId - * @param {WoltLabSuite/Core/Form/Builder/Field/Field} field */ - registerField: function (formId, field) { + registerField(formId, field) { if (!this.hasForm(formId)) { throw new Error("Unknown form with id '" + formId + "'."); } - if (!(field instanceof FormBuilderField)) { + if (!(field instanceof Field_1.default)) { throw new Error("Add field is no instance of 'WoltLabSuite/Core/Form/Builder/Field/Field'."); } - var fieldId = field.getId(); + const fieldId = field.getId(); if (this.hasField(formId, fieldId)) { throw new Error("Form field with id '" + fieldId + "' has already been registered for form with id '" + formId + "'."); } _fields.get(formId).set(fieldId, field); - EventHandler.fire('WoltLabSuite/Core/Form/Builder/Manager', 'registerField', { + EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerField", { field: field, formId: formId, }); }, /** * Registers the form with the given id. - * - * @param {string} formId */ - registerForm: function (formId) { + registerForm(formId) { if (this.hasForm(formId)) { throw new Error("Form with id '" + formId + "' has already been registered."); } - var form = elById(formId); + const form = document.getElementById(formId); if (form === null) { throw new Error("Unknown form with id '" + formId + "'."); } _forms.set(formId, form); - _fields.set(formId, new Dictionary()); - EventHandler.fire('WoltLabSuite/Core/Form/Builder/Manager', 'registerForm', { - formId: formId + _fields.set(formId, new Map()); + EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerForm", { + formId: formId, }); }, /** * Unregisters the form with the given id. - * - * @param {string} formId */ unregisterForm: function (formId) { if (!this.hasForm(formId)) { throw new Error("Unknown form with id '" + formId + "'."); } - EventHandler.fire('WoltLabSuite/Core/Form/Builder/Manager', 'beforeUnregisterForm', { - formId: formId + EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "beforeUnregisterForm", { + formId: formId, }); _forms.delete(formId); _fields.get(formId).forEach(function (field) { field.destroy(); }); _fields.delete(formId); - FormBuilderFieldDependencyManager.unregister(formId); - EventHandler.fire('WoltLabSuite/Core/Form/Builder/Manager', 'afterUnregisterForm', { - formId: formId + Manager_1.default.unregister(formId); + EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "afterUnregisterForm", { + formId: formId, }); - } + }, }; + Core.enableLegacyInheritance(FormBuilderManager); + return FormBuilderManager; }); diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Data.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Data.ts new file mode 100644 index 0000000000..e2fdaea295 --- /dev/null +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Data.ts @@ -0,0 +1,25 @@ +import { DialogOptions } from "../../Ui/Dialog/Data"; + +interface InternalFormBuilderData { + [key: string]: any; +} + +export type FormBuilderData = InternalFormBuilderData | Promise; + +export interface FormBuilderDialogOptions { + actionParameters: { + [key: string]: any; + }; + closeCallback: () => void; + destroyOnClose: boolean; + dialog: DialogOptions; + onSubmit: (FormBuilderData, HTMLButtonElement) => void; + submitActionName?: string; + successCallback: (AjaxResponseReturnValues) => void; + usesDboAction: boolean; +} + +export interface LabelFormFieldOptions { + forceSelection: boolean; + showWithoutSelection: boolean; +} diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.js deleted file mode 100644 index 4ef7067174..0000000000 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.js +++ /dev/null @@ -1,201 +0,0 @@ -/** - * Manager for registered Ajax forms and its fields that can be used to retrieve the current data - * of the registered forms. - * - * @author Matthias Schmidt - * @copyright 2001-2020 WoltLab GmbH - * @license GNU Lesser General Public License - * @module WoltLabSuite/Core/Form/Builder/Manager - * @since 5.2 - */ -define([ - 'Core', - 'Dictionary', - 'EventHandler', - './Field/Dependency/Manager', - './Field/Field' -], function( - Core, - Dictionary, - EventHandler, - FormBuilderFieldDependencyManager, - FormBuilderField -) { - "use strict"; - - var _fields = new Dictionary(); - var _forms = new Dictionary(); - - return { - /** - * Returns a promise returning the data of the form with the given id. - * - * @param {string} formId - * @return {Promise} - */ - getData: function(formId) { - if (!this.hasForm(formId)) { - throw new Error("Unknown form with id '" + formId + "'."); - } - - var promises = []; - - _fields.get(formId).forEach(function(field) { - var fieldData = field.getData(); - - if (!(fieldData instanceof Promise)) { - throw new TypeError("Data for field with id '" + field.getId() + "' is no promise."); - } - - promises.push(fieldData); - }); - - return Promise.all(promises).then(function(promiseData) { - var data = {}; - - for (var i = 0, length = promiseData.length; i < length; i++) { - data = Core.extend(data, promiseData[i]); - } - - return data; - }); - }, - - /** - * Returns the registered form field with given id. - * - * @param {string} formId - * @return {WoltLabSuite/Core/Form/Builder/Field/Field} - * @since 5.2.3 - */ - getField: function(formId, fieldId) { - if (!this.hasField(formId, fieldId)) { - throw new Error("Unknown field with id '" + formId + "' for form with id '" + fieldId + "'."); - } - - return _fields.get(formId).get(fieldId); - }, - - /** - * Returns the registered form with given id. - * - * @param {string} formId - * @return {HTMLElement} - */ - getForm: function(formId) { - if (!this.hasForm(formId)) { - throw new Error("Unknown form with id '" + formId + "'."); - } - - return _forms.get(formId); - }, - - /** - * Returns `true` if a field with the given id has been registered for the form with - * the given id and `false` otherwise. - * - * @param {string} formId - * @param {string} fieldId - * @return {boolean} - */ - hasField: function(formId, fieldId) { - if (!this.hasForm(formId)) { - throw new Error("Unknown form with id '" + formId + "'."); - } - - return _fields.get(formId).has(fieldId); - }, - - /** - * Returns `true` if a form with the given id has been registered and `false` - * otherwise. - * - * @param {string} formId - * @return {boolean} - */ - hasForm: function(formId) { - return _forms.has(formId); - }, - - /** - * Registers the given field for the form with the given id. - * - * @param {string} formId - * @param {WoltLabSuite/Core/Form/Builder/Field/Field} field - */ - registerField: function(formId, field) { - if (!this.hasForm(formId)) { - throw new Error("Unknown form with id '" + formId + "'."); - } - - if (!(field instanceof FormBuilderField)) { - throw new Error("Add field is no instance of 'WoltLabSuite/Core/Form/Builder/Field/Field'."); - } - - var fieldId = field.getId(); - - if (this.hasField(formId, fieldId)) { - throw new Error("Form field with id '" + fieldId + "' has already been registered for form with id '" + formId + "'."); - } - - _fields.get(formId).set(fieldId, field); - - EventHandler.fire('WoltLabSuite/Core/Form/Builder/Manager', 'registerField', { - field: field, - formId: formId, - }); - }, - - /** - * Registers the form with the given id. - * - * @param {string} formId - */ - registerForm: function(formId) { - if (this.hasForm(formId)) { - throw new Error("Form with id '" + formId + "' has already been registered."); - } - - var form = elById(formId); - if (form === null) { - throw new Error("Unknown form with id '" + formId + "'."); - } - - _forms.set(formId, form); - _fields.set(formId, new Dictionary()); - - EventHandler.fire('WoltLabSuite/Core/Form/Builder/Manager', 'registerForm', { - formId: formId - }); - }, - - /** - * Unregisters the form with the given id. - * - * @param {string} formId - */ - unregisterForm: function(formId) { - if (!this.hasForm(formId)) { - throw new Error("Unknown form with id '" + formId + "'."); - } - - EventHandler.fire('WoltLabSuite/Core/Form/Builder/Manager', 'beforeUnregisterForm', { - formId: formId - }); - - _forms.delete(formId); - - _fields.get(formId).forEach(function(field) { - field.destroy(); - }); - - _fields.delete(formId); - - FormBuilderFieldDependencyManager.unregister(formId); - - EventHandler.fire('WoltLabSuite/Core/Form/Builder/Manager', 'afterUnregisterForm', { - formId: formId - }); - } - }; -}); diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.ts new file mode 100644 index 0000000000..47d20f39fe --- /dev/null +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.ts @@ -0,0 +1,175 @@ +/** + * Manager for registered Ajax forms and its fields that can be used to retrieve the current data + * of the registered forms. + * + * @author Matthias Schmidt + * @copyright 2001-2020 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Form/Builder/Manager + * @since 5.2 + */ + +import * as Core from "../../Core"; +import * as EventHandler from "../../Event/Handler"; +import Field from "./Field/Field"; +import DependencyManager from "./Field/Dependency/Manager"; +import { FormBuilderData } from "./Data"; + +const _fields = new Map>(); +const _forms = new Map(); + +const FormBuilderManager = { + /** + * Returns a promise returning the data of the form with the given id. + */ + getData(formId: string): Promise { + if (!this.hasForm(formId)) { + throw new Error("Unknown form with id '" + formId + "'."); + } + + const promises: Promise[] = []; + + _fields.get(formId)!.forEach(function (field) { + const fieldData = field.getData(); + + if (!(fieldData instanceof Promise)) { + throw new TypeError("Data for field with id '" + field.getId() + "' is no promise."); + } + + promises.push(fieldData); + }); + + return Promise.all(promises).then(function (promiseData: FormBuilderData[]) { + let data = {}; + + promiseData.forEach((_data) => { + data = Core.extend(data, _data); + }); + + return data; + }); + }, + + /** + * Returns the registered form field with given. + * + * @since 5.2.3 + */ + getField(formId: string, fieldId: string): Field { + if (!this.hasField(formId, fieldId)) { + throw new Error("Unknown field with id '" + formId + "' for form with id '" + fieldId + "'."); + } + + return _fields.get(formId)!.get(fieldId)!; + }, + + /** + * Returns the registered form with given id. + */ + getForm(formId: string): HTMLElement { + if (!this.hasForm(formId)) { + throw new Error("Unknown form with id '" + formId + "'."); + } + + return _forms.get(formId)!; + }, + + /** + * Returns `true` if a field with the given id has been registered for the form with the given id + * and `false` otherwise. + */ + hasField(formId: string, fieldId: string): boolean { + if (!this.hasForm(formId)) { + throw new Error("Unknown form with id '" + formId + "'."); + } + + return _fields.get(formId)!.has(fieldId); + }, + + /** + * Returns `true` if a form with the given id has been registered and `false` otherwise. + */ + hasForm(formId): boolean { + return _forms.has(formId); + }, + + /** + * Registers the given field for the form with the given id. + */ + registerField(formId: string, field: Field): void { + if (!this.hasForm(formId)) { + throw new Error("Unknown form with id '" + formId + "'."); + } + + if (!(field instanceof Field)) { + throw new Error("Add field is no instance of 'WoltLabSuite/Core/Form/Builder/Field/Field'."); + } + + const fieldId = field.getId(); + + if (this.hasField(formId, fieldId)) { + throw new Error( + "Form field with id '" + fieldId + "' has already been registered for form with id '" + formId + "'.", + ); + } + + _fields.get(formId)!.set(fieldId, field); + + EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerField", { + field: field, + formId: formId, + }); + }, + + /** + * Registers the form with the given id. + */ + registerForm(formId: string): void { + if (this.hasForm(formId)) { + throw new Error("Form with id '" + formId + "' has already been registered."); + } + + const form = document.getElementById(formId); + if (form === null) { + throw new Error("Unknown form with id '" + formId + "'."); + } + + _forms.set(formId, form); + _fields.set(formId, new Map()); + + EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerForm", { + formId: formId, + }); + }, + + /** + * Unregisters the form with the given id. + */ + unregisterForm: function (formId: string): void { + if (!this.hasForm(formId)) { + throw new Error("Unknown form with id '" + formId + "'."); + } + + EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "beforeUnregisterForm", { + formId: formId, + }); + + _forms.delete(formId); + + _fields.get(formId)!.forEach(function (field) { + field.destroy(); + }); + + _fields.delete(formId); + + DependencyManager.unregister(formId); + + EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "afterUnregisterForm", { + formId: formId, + }); + }, +}; + +Core.enableLegacyInheritance(FormBuilderManager); + +export = FormBuilderManager; -- 2.20.1