Skip to content

Commit

Permalink
Merge pull request silverstripe#872 from creative-commoners/pulls/4.5…
Browse files Browse the repository at this point in the history
…/step-visibility

FIX Display rules for page breaks/editable form steps now works again
  • Loading branch information
robbieaverill authored Mar 26, 2019
2 parents 53eb9d1 + c2aaec3 commit 4734030
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ dist: precise

matrix:
include:
- php: 5.3
env: DB=MYSQL CORE_RELEASE=3.4
- php: 5.4
env: DB=MYSQL CORE_RELEASE=3.5
- php: 5.6
Expand Down
28 changes: 25 additions & 3 deletions code/model/EditableCustomRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,33 @@ public function buildExpression()
* 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);
}
}
3 changes: 3 additions & 0 deletions code/model/UserDefinedForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ protected function buildWatchJS($watch)
$conjunction = $rule['conjunction'];
$operations = implode(" {$conjunction} ", $rule['operations']);
$target = $rule['targetFieldID'];
$holder = $rule['holder'];

$result .= <<<EOS
\n
Expand All @@ -775,8 +776,10 @@ protected function buildWatchJS($watch)
function (){
if ({$operations}) {
$('{$target}').{$rule['view']};
{$holder}.{$rule['view']}.trigger('{$rule['holder_event']}');
} else {
$('{$target}').{$rule['opposite']};
{$holder}.{$rule['opposite']}.trigger('{$rule['holder_event_opposite']}');
}
});
$("{$target}").find('.hide').removeClass('hide');
Expand Down
6 changes: 4 additions & 2 deletions code/model/editableformfields/EditableFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,11 @@ public function formatDisplayRules()
$result['operations'][] = $expression['operation'];

// View/Show should read
$opposite = ($result['initialState'] === 'hide') ? 'show' : 'hide';
$result['view'] = $rule->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;
Expand Down
13 changes: 13 additions & 0 deletions tests/EditableCustomRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,21 @@ public function testBuildExpression()
*/
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));
}
}

0 comments on commit 4734030

Please sign in to comment.