Add form field dependency for interval of values (#4328)
authorMatthias Schmidt <gravatronics@live.com>
Mon, 21 Jun 2021 08:20:51 +0000 (10:20 +0200)
committerGitHub <noreply@github.com>
Mon, 21 Jun 2021 08:20:51 +0000 (10:20 +0200)
com.woltlab.wcf/templates/__valueIntervalFormFieldDependency.tpl [new file with mode: 0644]
ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/ValueInterval.ts [new file with mode: 0644]
wcfsetup/install/files/acp/templates/__valueIntervalFormFieldDependency.tpl [new file with mode: 0644]
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/ValueInterval.js [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/dependency/ValueIntervalFormFieldDependency.class.php [new file with mode: 0644]

diff --git a/com.woltlab.wcf/templates/__valueIntervalFormFieldDependency.tpl b/com.woltlab.wcf/templates/__valueIntervalFormFieldDependency.tpl
new file mode 100644 (file)
index 0000000..454204e
--- /dev/null
@@ -0,0 +1,9 @@
+require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/ValueInterval'], ({ ValueInterval }) => {
+    // dependency '{@$dependency->getId()}'
+    new ValueInterval(
+        '{@$dependency->getDependentNode()->getPrefixedId()}Container',
+        '{@$dependency->getField()->getPrefixedId()}'
+    )
+    .minimum({if $dependency->getMinimum() !== null}{@$dependency->getMinimum()}{else}null{/if})
+    .maximum({if $dependency->getMaximum() !== null}{@$dependency->getMaximum()}{else}null{/if});
+});
diff --git a/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/ValueInterval.ts b/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/ValueInterval.ts
new file mode 100644 (file)
index 0000000..f98e781
--- /dev/null
@@ -0,0 +1,62 @@
+/**
+ * Form field dependency implementation that requires the value of a field to be in the interval
+ * [minimum, maximum].
+ *
+ * @author  Matthias Schmidt
+ * @copyright 2001-2021 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Form/Builder/Field/Dependency/ValueInterval
+ * @see module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract
+ * @since 5.5
+ */
+
+import Abstract from "./Abstract";
+import * as DependencyManager from "./Manager";
+
+export class ValueInterval extends Abstract {
+  protected _maximum: number | null = null;
+  protected _minimum: number | null = null;
+
+  checkDependency(): boolean {
+    if (this._field) {
+      if (DependencyManager.isHiddenByDependencies(this._field)) {
+        return false;
+      }
+
+      const value = parseFloat((this._field as HTMLInputElement).value);
+      if (isNaN(value)) {
+        return false;
+      }
+
+      if (this._minimum !== null && this._minimum > value) {
+        return false;
+      } else if (this._maximum !== null && this._maximum < value) {
+        return false;
+      }
+
+      return true;
+    } else {
+      throw new Error("'ValueInterval' is only supported for individual fields.");
+    }
+  }
+
+  /**
+   * Sets the maximum value of the value interval or unsets the maximum value if `null` is given.
+   */
+  maximum(maximum: number | null): ValueInterval {
+    this._maximum = maximum;
+
+    return this;
+  }
+
+  /**
+   * Sets the minimum value of the value interval or unsets the minimum value if `null` is given.
+   */
+  minimum(minimum: number | null): ValueInterval {
+    this._minimum = minimum;
+
+    return this;
+  }
+}
+
+export default ValueInterval;
diff --git a/wcfsetup/install/files/acp/templates/__valueIntervalFormFieldDependency.tpl b/wcfsetup/install/files/acp/templates/__valueIntervalFormFieldDependency.tpl
new file mode 100644 (file)
index 0000000..454204e
--- /dev/null
@@ -0,0 +1,9 @@
+require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/ValueInterval'], ({ ValueInterval }) => {
+    // dependency '{@$dependency->getId()}'
+    new ValueInterval(
+        '{@$dependency->getDependentNode()->getPrefixedId()}Container',
+        '{@$dependency->getField()->getPrefixedId()}'
+    )
+    .minimum({if $dependency->getMinimum() !== null}{@$dependency->getMinimum()}{else}null{/if})
+    .maximum({if $dependency->getMaximum() !== null}{@$dependency->getMaximum()}{else}null{/if});
+});
diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/ValueInterval.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/ValueInterval.js
new file mode 100644 (file)
index 0000000..c4f1cd5
--- /dev/null
@@ -0,0 +1,62 @@
+/**
+ * Form field dependency implementation that requires the value of a field to be in the interval
+ * [minimum, maximum].
+ *
+ * @author  Matthias Schmidt
+ * @copyright 2001-2021 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Form/Builder/Field/Dependency/ValueInterval
+ * @see module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract
+ * @since 5.5
+ */
+define(["require", "exports", "tslib", "./Abstract", "./Manager"], function (require, exports, tslib_1, Abstract_1, DependencyManager) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.ValueInterval = void 0;
+    Abstract_1 = tslib_1.__importDefault(Abstract_1);
+    DependencyManager = tslib_1.__importStar(DependencyManager);
+    class ValueInterval extends Abstract_1.default {
+        constructor() {
+            super(...arguments);
+            this._maximum = null;
+            this._minimum = null;
+        }
+        checkDependency() {
+            if (this._field) {
+                if (DependencyManager.isHiddenByDependencies(this._field)) {
+                    return false;
+                }
+                const value = parseFloat(this._field.value);
+                if (isNaN(value)) {
+                    return false;
+                }
+                if (this._minimum !== null && this._minimum > value) {
+                    return false;
+                }
+                else if (this._maximum !== null && this._maximum < value) {
+                    return false;
+                }
+                return true;
+            }
+            else {
+                throw new Error("'ValueInterval' is only supported for individual fields.");
+            }
+        }
+        /**
+         * Sets the maximum value of the value interval or unsets the maximum value if `null` is given.
+         */
+        maximum(maximum) {
+            this._maximum = maximum;
+            return this;
+        }
+        /**
+         * Sets the minimum value of the value interval or unsets the minimum value if `null` is given.
+         */
+        minimum(minimum) {
+            this._minimum = minimum;
+            return this;
+        }
+    }
+    exports.ValueInterval = ValueInterval;
+    exports.default = ValueInterval;
+});
diff --git a/wcfsetup/install/files/lib/system/form/builder/field/dependency/ValueIntervalFormFieldDependency.class.php b/wcfsetup/install/files/lib/system/form/builder/field/dependency/ValueIntervalFormFieldDependency.class.php
new file mode 100644 (file)
index 0000000..b79e1f4
--- /dev/null
@@ -0,0 +1,100 @@
+<?php
+
+namespace wcf\system\form\builder\field\dependency;
+
+/**
+ * Represents a dependency that requires the value of a field to be in the interval
+ * [minimum, maximum].
+ *
+ * @author  Matthias Schmidt
+ * @copyright   2001-2021 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\Form\Builder\Field\Dependency
+ * @since   5.5
+ */
+final class ValueIntervalFormFieldDependency extends AbstractFormFieldDependency
+{
+    /**
+     * maximum value of the value interval
+     */
+    protected $maximum;
+
+    /**
+     * minimum value of the value interval
+     */
+    protected $minimum;
+
+    /**
+     * @inheritDoc
+     */
+    protected $templateName = '__valueIntervalFormFieldDependency';
+
+    /**
+     * @inheritDoc
+     */
+    public function checkDependency()
+    {
+        $value = $this->getField()->getValue();
+        if (!\is_numeric($value)) {
+            return false;
+        }
+
+        $value = \floatval($value);
+
+        if ($this->minimum !== null && $this->minimum > $value) {
+            return false;
+        } elseif ($this->maximum !== null && $this->maximum < $value) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Returns the maximum value of the value interval or `null` if no maximum has been set.
+     */
+    public function getMaximum(): ?float
+    {
+        return $this->maximum;
+    }
+
+    /**
+     * Returns the minimum value of the value interval or `null` if no minimum has been set.
+     */
+    public function getMinimum(): ?float
+    {
+        return $this->minimum;
+    }
+
+    /**
+     * Sets the maximum value of the value interval or unsets the maximum value if `null` is given.
+     */
+    public function maximum(?float $maximum = null): self
+    {
+        if ($maximum !== null && $this->minimum !== null && $maximum < $this->minimum) {
+            throw new \InvalidArgumentException(
+                "Maximum value '{$maximum}' for dependency '{$this->getId()}' is less than set minimum value '{$this->minimum}'."
+            );
+        }
+
+        $this->maximum = $maximum;
+
+        return $this;
+    }
+
+    /**
+     * Sets the minimum value of the value interval or unsets the minimum value if `null` is given.
+     */
+    public function minimum(?float $minimum = null): self
+    {
+        if ($minimum !== null && $this->maximum !== null && $minimum > $this->maximum) {
+            throw new \InvalidArgumentException(
+                "Minimum value '{$minimum}' for dependency '{$this->getId()}' is greater than set maximum value '{$this->maximum}'."
+            );
+        }
+
+        $this->minimum = $minimum;
+
+        return $this;
+    }
+}