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

index bc8c77073bd300f02ef6ae7d8ec19b765099e0cb..2736d2e8cd524badee572d2ead9d32f8ee72535a 100644 (file)
@@ -1,59 +1,51 @@
 /**
  * Form field dependency implementation that requires the value of a field to be empty.
  *
- * @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/Dependency/Empty
- * @see        module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract
- * @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/Dependency/Empty
+ * @see module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract
+ * @since 5.2
  */
-define(['./Abstract', 'Core'], function (Abstract, Core) {
+define(["require", "exports", "tslib", "./Abstract", "../../../../Core"], function (require, exports, tslib_1, Abstract_1, Core) {
     "use strict";
-    /**
-     * @constructor
-     */
-    function Empty(dependentElementId, fieldId) {
-        this.init(dependentElementId, fieldId);
-    }
-    ;
-    Core.inherit(Empty, Abstract, {
-        /**
-         * @see        WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract#checkDependency
-         */
-        checkDependency: function () {
+    Abstract_1 = tslib_1.__importDefault(Abstract_1);
+    Core = tslib_1.__importStar(Core);
+    class Empty extends Abstract_1.default {
+        checkDependency() {
             if (this._field !== null) {
                 switch (this._field.tagName) {
-                    case 'INPUT':
-                        switch (this._field.type) {
-                            case 'checkbox':
-                                return !this._field.checked;
-                            case 'radio':
+                    case "INPUT": {
+                        const field = this._field;
+                        switch (field.type) {
+                            case "checkbox":
+                                return !field.checked;
+                            case "radio":
                                 if (this._noField && this._noField.checked) {
                                     return true;
                                 }
-                                return !this._field.checked;
+                                return !field.checked;
                             default:
-                                return this._field.value.trim().length === 0;
+                                return field.value.trim().length === 0;
                         }
-                    case 'SELECT':
-                        if (this._field.multiple) {
-                            return elBySelAll('option:checked', this._field).length === 0;
+                    }
+                    case "SELECT": {
+                        const field = this._field;
+                        if (field.multiple) {
+                            return this._field.querySelectorAll("option:checked").length === 0;
                         }
-                        return this._field.value == 0 || this._field.value.length === 0;
-                    case 'TEXTAREA':
+                        return field.value == "0" || field.value.length === 0;
+                    }
+                    case "TEXTAREA": {
                         return this._field.value.trim().length === 0;
-                }
-            }
-            else {
-                for (var i = 0, length = this._fields.length; i < length; i++) {
-                    if (this._fields[i].checked) {
-                        return false;
                     }
                 }
-                return true;
             }
+            // Check that none of the fields are checked.
+            return this._fields.every((field) => !field.checked);
         }
-    });
+    }
+    Core.enableLegacyInheritance(Empty);
     return Empty;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Empty.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Empty.js
deleted file mode 100644 (file)
index 2be7614..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/**
- * Form field dependency implementation that requires the value of a field to be empty.
- *
- * @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/Dependency/Empty
- * @see        module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract
- * @since      5.2
- */
-define(['./Abstract', 'Core'], function(Abstract, Core) {
-       "use strict";
-       
-       /**
-        * @constructor
-        */
-       function Empty(dependentElementId, fieldId) {
-               this.init(dependentElementId, fieldId);
-       };
-       Core.inherit(Empty, Abstract, {
-               /**
-                * @see WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract#checkDependency
-                */
-               checkDependency: function() {
-                       if (this._field !== null) {
-                               switch (this._field.tagName) {
-                                       case 'INPUT':
-                                               switch (this._field.type) {
-                                                       case 'checkbox':
-                                                               return !this._field.checked;
-                                                       
-                                                       case 'radio':
-                                                               if (this._noField && this._noField.checked) {
-                                                                       return true;
-                                                               }
-                                                               
-                                                               return !this._field.checked;
-                                                       
-                                                       default:
-                                                               return this._field.value.trim().length === 0;
-                                               }
-                                       
-                                       case 'SELECT':
-                                               if (this._field.multiple) {
-                                                       return elBySelAll('option:checked', this._field).length === 0;
-                                               }
-                                               
-                                               return this._field.value == 0 || this._field.value.length === 0;
-                                       
-                                       case 'TEXTAREA':
-                                               return this._field.value.trim().length === 0;
-                               }
-                       }
-                       else {
-                               for (var i = 0, length = this._fields.length; i < length; i++) {
-                                       if (this._fields[i].checked) {
-                                               return false;
-                                       }
-                               }
-                               
-                               return true;
-                       }
-               }
-       });
-       
-       return Empty;
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Empty.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Empty.ts
new file mode 100644 (file)
index 0000000..04c9171
--- /dev/null
@@ -0,0 +1,59 @@
+/**
+ * Form field dependency implementation that requires the value of a field to be empty.
+ *
+ * @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/Dependency/Empty
+ * @see module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract
+ * @since 5.2
+ */
+
+import Abstract from "./Abstract";
+import * as Core from "../../../../Core";
+
+class Empty extends Abstract {
+  public checkDependency(): boolean {
+    if (this._field !== null) {
+      switch (this._field.tagName) {
+        case "INPUT": {
+          const field = this._field as HTMLInputElement;
+          switch (field.type) {
+            case "checkbox":
+              return !field.checked;
+
+            case "radio":
+              if (this._noField && this._noField.checked) {
+                return true;
+              }
+
+              return !field.checked;
+
+            default:
+              return field.value.trim().length === 0;
+          }
+        }
+
+        case "SELECT": {
+          const field = this._field as HTMLSelectElement;
+          if (field.multiple) {
+            return this._field.querySelectorAll("option:checked").length === 0;
+          }
+
+          return field.value == "0" || field.value.length === 0;
+        }
+
+        case "TEXTAREA": {
+          return (this._field as HTMLTextAreaElement).value.trim().length === 0;
+        }
+      }
+    }
+
+    // Check that none of the fields are checked.
+    return this._fields.every((field: HTMLInputElement) => !field.checked);
+  }
+}
+
+Core.enableLegacyInheritance(Empty);
+
+export = Empty;