From 085786273ba143aed76e683ddf290686d92e0991 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 30 Jun 2019 14:19:35 +0200 Subject: [PATCH] Add EmptyFormFieldDependency See #2509 --- .../templates/__emptyFormFieldDependency.tpl | 7 +++ syncTemplates.json | 1 + .../templates/__emptyFormFieldDependency.tpl | 7 +++ .../Form/Builder/Field/Dependency/Empty.js | 55 +++++++++++++++++++ .../EmptyFormFieldDependency.class.php | 25 +++++++++ 5 files changed, 95 insertions(+) create mode 100644 com.woltlab.wcf/templates/__emptyFormFieldDependency.tpl create mode 100644 wcfsetup/install/files/acp/templates/__emptyFormFieldDependency.tpl create mode 100644 wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Empty.js create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/dependency/EmptyFormFieldDependency.class.php diff --git a/com.woltlab.wcf/templates/__emptyFormFieldDependency.tpl b/com.woltlab.wcf/templates/__emptyFormFieldDependency.tpl new file mode 100644 index 0000000000..0975ecbe58 --- /dev/null +++ b/com.woltlab.wcf/templates/__emptyFormFieldDependency.tpl @@ -0,0 +1,7 @@ +require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/Empty'], function(EmptyFieldDependency) { + // dependency '{@$dependency->getId()}' + new EmptyFieldDependency( + '{@$dependency->getDependentNode()->getPrefixedId()}Container', + '{@$dependency->getField()->getPrefixedId()}' + ); +}); diff --git a/syncTemplates.json b/syncTemplates.json index 75370394fb..b236044664 100644 --- a/syncTemplates.json +++ b/syncTemplates.json @@ -8,6 +8,7 @@ "__booleanFormField", "__contentLanguageFormField", "__dateFormField", + "__emptyFormFieldDependency", "__form", "__formButton", "__formContainer", diff --git a/wcfsetup/install/files/acp/templates/__emptyFormFieldDependency.tpl b/wcfsetup/install/files/acp/templates/__emptyFormFieldDependency.tpl new file mode 100644 index 0000000000..0975ecbe58 --- /dev/null +++ b/wcfsetup/install/files/acp/templates/__emptyFormFieldDependency.tpl @@ -0,0 +1,7 @@ +require(['WoltLabSuite/Core/Form/Builder/Field/Dependency/Empty'], function(EmptyFieldDependency) { + // dependency '{@$dependency->getId()}' + new EmptyFieldDependency( + '{@$dependency->getDependentNode()->getPrefixedId()}Container', + '{@$dependency->getField()->getPrefixedId()}' + ); +}); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Empty.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Empty.js new file mode 100644 index 0000000000..d14b1ca29b --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Empty.js @@ -0,0 +1,55 @@ +/** + * 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 + * @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() { + switch (this._field.tagName) { + case 'INPUT': + switch (this._field.type) { + case 'checkbox': + // TODO: check if working + 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': + // TODO: check if working for multiselect + return this._field.value.length === 0; + + case 'TEXTAREA': + // TODO: check if working + return this._field.value.trim().length === 0; + } + } + }); + + return Empty; +}); diff --git a/wcfsetup/install/files/lib/system/form/builder/field/dependency/EmptyFormFieldDependency.class.php b/wcfsetup/install/files/lib/system/form/builder/field/dependency/EmptyFormFieldDependency.class.php new file mode 100644 index 0000000000..aa10be2b3e --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/dependency/EmptyFormFieldDependency.class.php @@ -0,0 +1,25 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field\Dependency + * @since 5.2 + */ +class EmptyFormFieldDependency extends AbstractFormFieldDependency { + /** + * @inheritDoc + */ + protected $templateName = '__emptyFormFieldDependency'; + + /** + * @inheritDoc + */ + public function checkDependency() { + return empty($this->getField()->getValue()); + } +} -- 2.20.1