From 0a3ebcb224690847eaef186bfaeea0c6c463a1f9 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 26 Mar 2019 14:11:04 +1300 Subject: [PATCH] FIX Display rules for page breaks/editable form steps now works again This was previously broken in a regression from https://github.com/silverstripe/silverstripe-userforms/pull/575 where the fields themselves could still be toggled, but the jQuery event dispatches for the form steps were removed. This pull request re-introduces those jQuery event triggers when hiding and showing editable form fields via display rules. --- code/model/EditableCustomRule.php | 28 +++++++++++++++++-- code/model/UserDefinedForm.php | 3 ++ .../editableformfields/EditableFormField.php | 6 ++-- tests/EditableCustomRuleTest.php | 13 +++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/code/model/EditableCustomRule.php b/code/model/EditableCustomRule.php index 8b1b744..6c4e213 100644 --- a/code/model/EditableCustomRule.php +++ b/code/model/EditableCustomRule.php @@ -241,11 +241,33 @@ class EditableCustomRule extends DataObject * Returns the opposite visibility function for the value of the initial visibility field, e.g. show/hide. This * will toggle the "hide" class either way, which is handled by CSS. * - * @param string $text + * @param string $initialState + * @param boolean $invert * @return string */ - public function toggleDisplayText($text) + public function toggleDisplayText($initialState, $invert = false) { - return (strtolower($text) === 'hide') ? 'removeClass("hide")' : 'addClass("hide")'; + $action = strtolower($initialState) === 'hide' ? 'removeClass' : 'addClass'; + if ($invert) { + $action = $action === 'removeClass' ? 'addClass' : 'removeClass'; + } + return sprintf('%s("hide")', $action); + } + + /** + * Returns an event name to be dispatched when the field is changed. Matches up with the visibility classes + * added or removed in `toggleDisplayText()`. + * + * @param string $initialState + * @param bool $invert + * @return string + */ + public function toggleDisplayEvent($initialState, $invert = false) + { + $action = strtolower($initialState) === 'hide' ? 'show' : 'hide'; + if ($invert) { + $action = $action === 'hide' ? 'show' : 'hide'; + } + return sprintf('userform.field.%s', $action); } } diff --git a/code/model/UserDefinedForm.php b/code/model/UserDefinedForm.php index 78279f9..f0a78e5 100755 --- a/code/model/UserDefinedForm.php +++ b/code/model/UserDefinedForm.php @@ -767,6 +767,7 @@ JS $conjunction = $rule['conjunction']; $operations = implode(" {$conjunction} ", $rule['operations']); $target = $rule['targetFieldID']; + $holder = $rule['holder']; $result .= <<toggleDisplayText($result['initialState']); - $result['opposite'] = $rule->toggleDisplayText($opposite); + $result['opposite'] = $rule->toggleDisplayText($result['initialState'], true); + $result['holder'] = $this->getSelectorHolder(); + $result['holder_event'] = $rule->toggleDisplayEvent($result['initialState']); + $result['holder_event_opposite'] = $rule->toggleDisplayEvent($result['initialState'], true); } return (count($result['selectors'])) ? $result : null; diff --git a/tests/EditableCustomRuleTest.php b/tests/EditableCustomRuleTest.php index 1bedb8b..010e328 100644 --- a/tests/EditableCustomRuleTest.php +++ b/tests/EditableCustomRuleTest.php @@ -35,8 +35,21 @@ class EditableCustomRuleTest extends SapphireTest */ public function testToggleDisplayText() { + /** @var EditableCustomRule $rule1 */ $rule1 = $this->objFromFixture('EditableCustomRule', 'rule1'); $this->assertSame('addClass("hide")', $rule1->toggleDisplayText('show')); $this->assertSame('removeClass("hide")', $rule1->toggleDisplayText('hide')); + $this->assertSame('removeClass("hide")', $rule1->toggleDisplayText('show', true)); + $this->assertSame('addClass("hide")', $rule1->toggleDisplayText('hide', true)); + } + + public function testToggleDisplayEvent() + { + /** @var EditableCustomRule $rule1 */ + $rule1 = $this->objFromFixture('EditableCustomRule', 'rule1'); + $this->assertSame('userform.field.hide', $rule1->toggleDisplayEvent('show')); + $this->assertSame('userform.field.show', $rule1->toggleDisplayEvent('hide')); + $this->assertSame('userform.field.show', $rule1->toggleDisplayEvent('show', true)); + $this->assertSame('userform.field.hide', $rule1->toggleDisplayEvent('hide', true)); } }