Convert `Form/Builder/Field/Dependency/Value` to TypeScript
authorMatthias Schmidt <gravatronics@live.com>
Fri, 18 Dec 2020 16:42:29 +0000 (17:42 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Fri, 18 Dec 2020 16:42:29 +0000 (17:42 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.ts [new file with mode: 0644]

index 16f3edf3497acddd0053380f3d9d052c03368438..5271c56a13ed5b4c393f4cae314e15df980b7ad7 100644 (file)
@@ -1,85 +1,77 @@
 /**
  * Form field dependency implementation that requires a field to have a certain value.
  *
- * @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/Value
- * @see        module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract
- * @since      5.2
+ * @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/Value
+ * @see module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract
+ * @since 5.2
  */
-define(['./Abstract', 'Core', './Manager'], function (Abstract, Core, Manager) {
+define(["require", "exports", "tslib", "./Abstract", "./Manager", "../../../../Core"], function (require, exports, tslib_1, Abstract_1, DependencyManager, Core) {
     "use strict";
-    /**
-     * @constructor
-     */
-    function Value(dependentElementId, fieldId, isNegated) {
-        this.init(dependentElementId, fieldId);
-        this._isNegated = false;
-    }
-    ;
-    Core.inherit(Value, Abstract, {
-        /**
-         * @see        WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract#checkDependency
-         */
-        checkDependency: function () {
+    Abstract_1 = tslib_1.__importDefault(Abstract_1);
+    DependencyManager = tslib_1.__importStar(DependencyManager);
+    Core = tslib_1.__importStar(Core);
+    class Value extends Abstract_1.default {
+        constructor() {
+            super(...arguments);
+            this._isNegated = false;
+        }
+        checkDependency() {
             if (!this._values) {
                 throw new Error("Values have not been set.");
             }
-            var values = [];
+            let values = [];
             if (this._field) {
-                if (Manager.isHiddenByDependencies(this._field)) {
+                if (DependencyManager.isHiddenByDependencies(this._field)) {
                     return false;
                 }
                 values.push(this._field.value);
             }
             else {
-                for (var i = 0, length = this._fields.length, field; i < length; i++) {
-                    field = this._fields[i];
+                let hasCheckedField = true;
+                this._fields.forEach((field) => {
                     if (field.checked) {
-                        if (Manager.isHiddenByDependencies(field)) {
+                        if (DependencyManager.isHiddenByDependencies(field)) {
+                            hasCheckedField = false;
                             return false;
                         }
                         values.push(field.value);
                     }
+                });
+                if (!hasCheckedField) {
+                    return false;
                 }
             }
-            // do not use `Array.prototype.indexOf()` as we use a weak comparision
-            for (var i = 0, length = this._values.length; i < length; i++) {
-                for (var j = 0, length2 = values.length; j < length2; j++) {
-                    if (this._values[i] == values[j]) {
-                        if (this._isNegated) {
-                            return false;
-                        }
-                        return true;
+            let foundMatch = false;
+            this._values.forEach((value) => {
+                values.forEach((selectedValue) => {
+                    if (value == selectedValue) {
+                        foundMatch = true;
                     }
-                }
-            }
-            if (this._isNegated) {
-                return true;
+                });
+            });
+            if (foundMatch) {
+                return !this._isNegated;
             }
-            return false;
-        },
+            return this._isNegated;
+        }
         /**
          * Sets if the field value may not have any of the set values.
-         *
-         * @param      {bool}          negate
-         * @return     {WoltLabSuite/Core/Form/Builder/Field/Dependency/Value}
          */
-        negate: function (negate) {
+        negate(negate) {
             this._isNegated = negate;
             return this;
-        },
+        }
         /**
          * Sets the possible values the field may have for the dependency to be met.
-         *
-         * @param      {array}         values
-         * @return     {WoltLabSuite/Core/Form/Builder/Field/Dependency/Value}
          */
-        values: function (values) {
+        values(values) {
             this._values = values;
             return this;
         }
-    });
+    }
+    Core.enableLegacyInheritance(Value);
     return Value;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.js
deleted file mode 100644 (file)
index 68f49d6..0000000
+++ /dev/null
@@ -1,99 +0,0 @@
-/**
- * Form field dependency implementation that requires a field to have a certain value.
- * 
- * @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/Value
- * @see        module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract
- * @since      5.2
- */
-define(['./Abstract', 'Core', './Manager'], function(Abstract, Core, Manager) {
-       "use strict";
-       
-       /**
-        * @constructor
-        */
-       function Value(dependentElementId, fieldId, isNegated) {
-               this.init(dependentElementId, fieldId);
-               
-               this._isNegated = false;
-       };
-       Core.inherit(Value, Abstract, {
-               /**
-                * @see WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract#checkDependency
-                */
-               checkDependency: function() {
-                       if (!this._values) {
-                               throw new Error("Values have not been set.");
-                       }
-                       
-                       var values = [];
-                       if (this._field) {
-                               if (Manager.isHiddenByDependencies(this._field)) {
-                                       return false;
-                               }
-                               
-                               values.push(this._field.value);
-                       }
-                       else {
-                               for (var i = 0, length = this._fields.length, field; i < length; i++) {
-                                       field = this._fields[i];
-                                       
-                                       if (field.checked) {
-                                               if (Manager.isHiddenByDependencies(field)) {
-                                                       return false;
-                                               }
-                                               
-                                               values.push(field.value);
-                                       }
-                               }
-                       }
-                       
-                       // do not use `Array.prototype.indexOf()` as we use a weak comparision
-                       for (var i = 0, length = this._values.length; i < length; i++) {
-                               for (var j = 0, length2 = values.length; j < length2; j++) {
-                                       if (this._values[i] == values[j]) {
-                                               if (this._isNegated) {
-                                                       return false;
-                                               }
-                                               
-                                               return true;
-                                       }
-                               }
-                       }
-                       
-                       if (this._isNegated) {
-                               return true;
-                       }
-                       
-                       return false;
-               },
-               
-               /**
-                * Sets if the field value may not have any of the set values.
-                * 
-                * @param       {bool}          negate
-                * @return      {WoltLabSuite/Core/Form/Builder/Field/Dependency/Value}
-                */
-               negate: function(negate) {
-                       this._isNegated = negate;
-                       
-                       return this;
-               },
-               
-               /**
-                * Sets the possible values the field may have for the dependency to be met.
-                * 
-                * @param       {array}         values
-                * @return      {WoltLabSuite/Core/Form/Builder/Field/Dependency/Value}
-                */
-               values: function(values) {
-                       this._values = values;
-                       
-                       return this;
-               }
-       });
-       
-       return Value;
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.ts
new file mode 100644 (file)
index 0000000..897603e
--- /dev/null
@@ -0,0 +1,87 @@
+/**
+ * Form field dependency implementation that requires a field to have a certain value.
+ *
+ * @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/Value
+ * @see module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract
+ * @since 5.2
+ */
+
+import Abstract from "./Abstract";
+import * as DependencyManager from "./Manager";
+import * as Core from "../../../../Core";
+
+class Value extends Abstract {
+  protected _isNegated = false;
+  protected _values?: string[];
+
+  checkDependency(): boolean {
+    if (!this._values) {
+      throw new Error("Values have not been set.");
+    }
+
+    let values: string[] = [];
+    if (this._field) {
+      if (DependencyManager.isHiddenByDependencies(this._field)) {
+        return false;
+      }
+
+      values.push((this._field as HTMLInputElement).value);
+    } else {
+      let hasCheckedField = true;
+      this._fields.forEach((field: HTMLInputElement) => {
+        if (field.checked) {
+          if (DependencyManager.isHiddenByDependencies(field)) {
+            hasCheckedField = false;
+            return false;
+          }
+
+          values.push(field.value);
+        }
+      });
+
+      if (!hasCheckedField) {
+        return false;
+      }
+    }
+
+    let foundMatch = false;
+    this._values.forEach((value) => {
+      values.forEach((selectedValue) => {
+        if (value == selectedValue) {
+          foundMatch = true;
+        }
+      });
+    });
+
+    if (foundMatch) {
+      return !this._isNegated;
+    }
+
+    return this._isNegated;
+  }
+
+  /**
+   * Sets if the field value may not have any of the set values.
+   */
+  negate(negate: boolean): Value {
+    this._isNegated = negate;
+
+    return this;
+  }
+
+  /**
+   * Sets the possible values the field may have for the dependency to be met.
+   */
+  values(values: string[]): Value {
+    this._values = values;
+
+    return this;
+  }
+}
+
+Core.enableLegacyInheritance(Value);
+
+export = Value;