Convert `Form/Builder/Field/Field` to TypeScript
authorMatthias Schmidt <gravatronics@live.com>
Wed, 9 Dec 2020 16:34:00 +0000 (17:34 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Tue, 15 Dec 2020 17:23:05 +0000 (18:23 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Field.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Field.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Field.ts [new file with mode: 0644]

index 9e1e2019c19840607ba693cfc37e7297afc8c521..bc75fb348524773d5f35681ec50063878855589b 100644 (file)
@@ -1,73 +1,65 @@
 /**
  * Data handler for a form builder field in an Ajax form.
  *
- * @author     Matthias Schmidt
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Form/Builder/Field/Field
- * @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/Field/Field
+ * @since 5.2
  */
-define([], function () {
+define(["require", "exports", "tslib", "../../../Core"], function (require, exports, tslib_1, Core) {
     "use strict";
-    /**
-     * @constructor
-     */
-    function FormBuilderField(fieldId) {
-        this.init(fieldId);
-    }
-    ;
-    FormBuilderField.prototype = {
+    Core = tslib_1.__importStar(Core);
+    class Field {
+        constructor(fieldId) {
+            this.init(fieldId);
+        }
         /**
-         * Initializes the form field.
-         *
-         * @param      {string}        fieldId         id of the relevant form builder field
+         * Initializes the field.
          */
-        init: function (fieldId) {
+        init(fieldId) {
             this._fieldId = fieldId;
             this._readField();
-        },
+        }
         /**
          * Returns the current data of the field or a promise returning the current data
          * of the field.
          *
          * @return     {Promise|data}
          */
-        _getData: function () {
+        _getData() {
             throw new Error("Missing implementation of WoltLabSuite/Core/Form/Builder/Field/Field._getData!");
-        },
+        }
         /**
-         * Reads the field HTML element.
+         * Reads the field's HTML element.
          */
-        _readField: function () {
-            this._field = elById(this._fieldId);
+        _readField() {
+            this._field = document.getElementById(this._fieldId);
             if (this._field === null) {
                 throw new Error("Unknown field with id '" + this._fieldId + "'.");
             }
-        },
+        }
         /**
          * Destroys the field.
          *
          * This function is useful for remove registered elements from other APIs like dialogs.
          */
-        destroy: function () {
-            // does nothing
-        },
+        destroy() {
+            // does nothinbg
+        }
         /**
-         * Returns a promise returning the current data of the field.
-         *
-         * @return     {Promise}
+         * Returns a promise providing the current data of the field.
          */
-        getData: function () {
+        getData() {
             return Promise.resolve(this._getData());
-        },
+        }
         /**
          * Returns the id of the field.
-         *
-         * @return     {string}
          */
-        getId: function () {
+        getId() {
             return this._fieldId;
         }
-    };
-    return FormBuilderField;
+    }
+    Core.enableLegacyInheritance(Field);
+    return Field;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Field.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Field.js
deleted file mode 100644 (file)
index d4d6dee..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/**
- * Data handler for a form builder field in an Ajax form.
- *
- * @author     Matthias Schmidt
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Form/Builder/Field/Field
- * @since      5.2
- */
-define([], function() {
-       "use strict";
-       
-       /**
-        * @constructor
-        */
-       function FormBuilderField(fieldId) {
-               this.init(fieldId);
-       };
-       FormBuilderField.prototype = {
-               /**
-                * Initializes the form field.
-                * 
-                * @param       {string}        fieldId         id of the relevant form builder field
-                */
-               init: function(fieldId) {
-                       this._fieldId = fieldId;
-                       
-                       this._readField();
-               },
-               
-               /**
-                * Returns the current data of the field or a promise returning the current data
-                * of the field.
-                * 
-                * @return      {Promise|data}
-                */
-               _getData: function() {
-                       throw new Error("Missing implementation of WoltLabSuite/Core/Form/Builder/Field/Field._getData!");
-               },
-               
-               /**
-                * Reads the field HTML element.
-                */
-               _readField: function() {
-                       this._field = elById(this._fieldId);
-                       
-                       if (this._field === null) {
-                               throw new Error("Unknown field with id '" + this._fieldId + "'.");
-                       }
-               },
-               
-               /**
-                * Destroys the field.
-                * 
-                * This function is useful for remove registered elements from other APIs like dialogs.
-                */
-               destroy: function() {
-                       // does nothing
-               },
-               
-               /**
-                * Returns a promise returning the current data of the field.
-                * 
-                * @return      {Promise}
-                */
-               getData: function() {
-                       return Promise.resolve(this._getData());
-               },
-               
-               /**
-                * Returns the id of the field.
-                * 
-                * @return      {string}
-                */
-               getId: function() {
-                       return this._fieldId;
-               }
-       };
-       
-       return FormBuilderField;
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Field.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Field.ts
new file mode 100644 (file)
index 0000000..5ae819d
--- /dev/null
@@ -0,0 +1,78 @@
+/**
+ * Data handler for a form builder field in an Ajax form.
+ *
+ * @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/Field/Field
+ * @since 5.2
+ */
+
+import * as Core from "../../../Core";
+import { FormBuilderData } from "../Data";
+
+class Field {
+  protected _fieldId: string;
+  protected _field: HTMLElement | null;
+
+  constructor(fieldId: string) {
+    this.init(fieldId);
+  }
+
+  /**
+   * Initializes the field.
+   */
+  protected init(fieldId: string): void {
+    this._fieldId = fieldId;
+
+    this._readField();
+  }
+
+  /**
+   * Returns the current data of the field or a promise returning the current data
+   * of the field.
+   *
+   * @return   {Promise|data}
+   */
+  protected _getData(): FormBuilderData {
+    throw new Error("Missing implementation of WoltLabSuite/Core/Form/Builder/Field/Field._getData!");
+  }
+
+  /**
+   * Reads the field's HTML element.
+   */
+  protected _readField(): void {
+    this._field = document.getElementById(this._fieldId);
+
+    if (this._field === null) {
+      throw new Error("Unknown field with id '" + this._fieldId + "'.");
+    }
+  }
+
+  /**
+   * Destroys the field.
+   *
+   * This function is useful for remove registered elements from other APIs like dialogs.
+   */
+  public destroy(): void {
+    // does nothinbg
+  }
+
+  /**
+   * Returns a promise providing the current data of the field.
+   */
+  public getData(): Promise<FormBuilderData> {
+    return Promise.resolve(this._getData());
+  }
+
+  /**
+   * Returns the id of the field.
+   */
+  public getId(): string {
+    return this._fieldId;
+  }
+}
+
+Core.enableLegacyInheritance(Field);
+
+export = Field;