Convert `Form/Builder/Field/Checkboxes` to TypeScript
authorMatthias Schmidt <gravatronics@live.com>
Wed, 9 Dec 2020 16:33:30 +0000 (17:33 +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/Field/Checkboxes.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Checkboxes.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Checkboxes.ts [new file with mode: 0644]

index 08771b95408ab27dc6b21d7d2216b5406a1ffa32..5e131d2a90cc9c75233303709f9ac5a7504d14cd 100644 (file)
@@ -1,41 +1,34 @@
 /**
  * Data handler for a form builder field in an Ajax form represented by checkboxes.
  *
- * @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/Checkboxes
- * @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/Checkboxes
+ * @since 5.2
  */
-define(['Core', './Field'], function (Core, FormBuilderField) {
+define(["require", "exports", "tslib", "./Field", "../../../Core"], function (require, exports, tslib_1, Field_1, Core) {
     "use strict";
-    /**
-     * @constructor
-     */
-    function FormBuilderFieldCheckboxes(fieldId) {
-        this.init(fieldId);
-    }
-    ;
-    Core.inherit(FormBuilderFieldCheckboxes, FormBuilderField, {
-        /**
-         * @see        WoltLabSuite/Core/Form/Builder/Field/Field#_getData
-         */
-        _getData: function () {
-            var data = {};
-            data[this._fieldId] = [];
-            for (var i = 0, length = this._fields.length; i < length; i++) {
-                if (this._fields[i].checked) {
-                    data[this._fieldId].push(this._fields[i].value);
+    Field_1 = tslib_1.__importDefault(Field_1);
+    Core = tslib_1.__importStar(Core);
+    class Checkboxes extends Field_1.default {
+        _getData() {
+            const values = this._fields
+                .map((input) => {
+                if (input.checked) {
+                    return input.value;
                 }
-            }
-            return data;
-        },
-        /**
-         * @see        WoltLabSuite/Core/Form/Builder/Field/Field#_readField
-         */
-        _readField: function () {
-            this._fields = elBySelAll('input[name="' + this._fieldId + '[]"]');
+                return null;
+            })
+                .filter((v) => v !== null);
+            return {
+                [this._fieldId]: values,
+            };
+        }
+        _readField() {
+            this._fields = Array.from(document.querySelectorAll("input[name=" + this._fieldId + "]"));
         }
-    });
-    return FormBuilderFieldCheckboxes;
+    }
+    Core.enableLegacyInheritance(Checkboxes);
+    return Checkboxes;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Checkboxes.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Checkboxes.js
deleted file mode 100644 (file)
index ecca075..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/**
- * Data handler for a form builder field in an Ajax form represented by checkboxes.
- * 
- * @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/Checkboxes
- * @since      5.2
- */
-define(['Core', './Field'], function(Core, FormBuilderField) {
-       "use strict";
-       
-       /**
-        * @constructor
-        */
-       function FormBuilderFieldCheckboxes(fieldId) {
-               this.init(fieldId);
-       };
-       Core.inherit(FormBuilderFieldCheckboxes, FormBuilderField, {
-               /**
-                * @see WoltLabSuite/Core/Form/Builder/Field/Field#_getData
-                */
-               _getData: function() {
-                       var data = {};
-                       
-                       data[this._fieldId] = [];
-                       
-                       for (var i = 0, length = this._fields.length; i < length; i++) {
-                               if (this._fields[i].checked) {
-                                       data[this._fieldId].push(this._fields[i].value);
-                               }
-                       }
-                       
-                       return data;
-               },
-               
-               /**
-                * @see WoltLabSuite/Core/Form/Builder/Field/Field#_readField
-                */
-               _readField: function() {
-                       this._fields = elBySelAll('input[name="' + this._fieldId + '[]"]');
-               }
-       });
-       
-       return FormBuilderFieldCheckboxes;
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Checkboxes.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Checkboxes.ts
new file mode 100644 (file)
index 0000000..33fdfd0
--- /dev/null
@@ -0,0 +1,41 @@
+/**
+ * Data handler for a form builder field in an Ajax form represented by checkboxes.
+ *
+ * @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/Checkboxes
+ * @since 5.2
+ */
+
+import Field from "./Field";
+import { FormBuilderData } from "../Data";
+import * as Core from "../../../Core";
+
+class Checkboxes extends Field {
+  protected _fields: HTMLInputElement[];
+
+  protected _getData(): FormBuilderData {
+    const values = this._fields
+      .map((input) => {
+        if (input.checked) {
+          return input.value;
+        }
+
+        return null;
+      })
+      .filter((v) => v !== null) as string[];
+
+    return {
+      [this._fieldId]: values,
+    };
+  }
+
+  protected _readField(): void {
+    this._fields = Array.from(document.querySelectorAll("input[name=" + this._fieldId + "]"));
+  }
+}
+
+Core.enableLegacyInheritance(Checkboxes);
+
+export = Checkboxes;