From: Matthias Schmidt Date: Fri, 18 Dec 2020 16:42:29 +0000 (+0100) Subject: Convert `Form/Builder/Field/Dependency/Value` to TypeScript X-Git-Tag: 5.4.0_Alpha_1~526^2~1 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=6b17ae9cb16f3965574d93744cd9541a894324e7;p=GitHub%2FWoltLab%2FWCF.git Convert `Form/Builder/Field/Dependency/Value` to TypeScript --- diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.js index 16f3edf349..5271c56a13 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.js @@ -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 - * @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 + * @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 index 68f49d6d33..0000000000 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.js +++ /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 - * @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 index 0000000000..897603e076 --- /dev/null +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.ts @@ -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 + * @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;