Convert `Form/Builder/Manager` to TypeScript
authorMatthias Schmidt <gravatronics@live.com>
Sun, 6 Dec 2020 13:41:40 +0000 (14:41 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Tue, 15 Dec 2020 17:23:04 +0000 (18:23 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Data.js [new file with mode: 0644]
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Manager.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Data.ts [new file with mode: 0644]
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.ts [new file with mode: 0644]

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 (file)
index 0000000..2ae92b6
--- /dev/null
@@ -0,0 +1,4 @@
+define(["require", "exports"], function (require, exports) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+});
index 8877880bd5cbe5148a84938bc402bb71ccca469f..eef3fa8213bad76fb3e120a683554baf58537622 100644 (file)
@@ -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 <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Form/Builder/Manager
- * @since      5.2
+ * @author  Matthias Schmidt
+ * @copyright 2001-2020 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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 (file)
index 0000000..e2fdaea
--- /dev/null
@@ -0,0 +1,25 @@
+import { DialogOptions } from "../../Ui/Dialog/Data";
+
+interface InternalFormBuilderData {
+  [key: string]: any;
+}
+
+export type FormBuilderData = InternalFormBuilderData | Promise<InternalFormBuilderData>;
+
+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 (file)
index 4ef7067..0000000
+++ /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 <http://opensource.org/licenses/lgpl-license.php>
- * @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 (file)
index 0000000..47d20f3
--- /dev/null
@@ -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 <http://opensource.org/licenses/lgpl-license.php>
+ * @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<string, Map<string, Field>>();
+const _forms = new Map<string, HTMLElement>();
+
+const FormBuilderManager = {
+  /**
+   * Returns a promise returning the data of the form with the given id.
+   */
+  getData(formId: string): Promise<FormBuilderData> {
+    if (!this.hasForm(formId)) {
+      throw new Error("Unknown form with id '" + formId + "'.");
+    }
+
+    const promises: Promise<FormBuilderData>[] = [];
+
+    _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<string, Field>());
+
+    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;