From 2a072a7e8d7a7327f4a3674736b985340e613b7b Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 12 Dec 2020 16:08:26 +0100 Subject: [PATCH] Convert `Form/Builder/Field/Dependency/NonEmpty` to TypeScript --- .../Form/Builder/Field/Dependency/NonEmpty.js | 70 ++++++++----------- .../Form/Builder/Field/Dependency/NonEmpty.js | 67 ------------------ .../Form/Builder/Field/Dependency/NonEmpty.ts | 59 ++++++++++++++++ 3 files changed, 90 insertions(+), 106 deletions(-) delete mode 100644 wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.js create mode 100644 wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.ts diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.js index 9788af2181..2f4566fe27 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.js @@ -1,59 +1,51 @@ /** * Form field dependency implementation that requires the value of a field not to be empty. * - * @author Matthias Schmidt - * @copyright 2001-2019 WoltLab GmbH - * @license GNU Lesser General Public License - * @module WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty - * @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 + * @module WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty + * @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 NonEmpty(dependentElementId, fieldId) { - this.init(dependentElementId, fieldId); - } - ; - Core.inherit(NonEmpty, 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 NonEmpty 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 false; } - 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 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 true; } } - return false; } + // Check if any of the fields if checked. + return this._fields.some((field) => field.checked); } - }); + } + Core.enableLegacyInheritance(NonEmpty); return NonEmpty; }); diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.js deleted file mode 100644 index 6b23cfb7a5..0000000000 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.js +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Form field dependency implementation that requires the value of a field not to be empty. - * - * @author Matthias Schmidt - * @copyright 2001-2019 WoltLab GmbH - * @license GNU Lesser General Public License - * @module WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty - * @see module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract - * @since 5.2 - */ -define(['./Abstract', 'Core'], function(Abstract, Core) { - "use strict"; - - /** - * @constructor - */ - function NonEmpty(dependentElementId, fieldId) { - this.init(dependentElementId, fieldId); - }; - Core.inherit(NonEmpty, 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 false; - } - - 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 true; - } - } - - return false; - } - } - }); - - return NonEmpty; -}); diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.ts new file mode 100644 index 0000000000..03faca503e --- /dev/null +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty.ts @@ -0,0 +1,59 @@ +/** + * Form field dependency implementation that requires the value of a field not to be empty. + * + * @author Matthias Schmidt + * @copyright 2001-2020 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Form/Builder/Field/Dependency/NonEmpty + * @see module:WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract + * @since 5.2 + */ + +import Abstract from "./Abstract"; +import * as Core from "../../../../Core"; + +class NonEmpty 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 false; + } + + return field.checked; + + default: + return field.value.trim().length !== 0; + } + } + + case "SELECT": { + const field = this._field as HTMLSelectElement; + if (field.multiple) { + return 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 if any of the fields if checked. + return this._fields.some((field: HTMLInputElement) => field.checked); + } +} + +Core.enableLegacyInheritance(NonEmpty); + +export = NonEmpty; -- 2.20.1