Skip to content

Commit

Permalink
collect triggers with Callback::set condition
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 27, 2021
1 parent 1cf01e8 commit 428165b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ parameters:
message: '~^Method Atk4\\Ui\\Callback::setUrlTrigger\(\) has no return typehint specified\.$~'
-
path: 'src/Callback.php'
message: '~^Method Atk4\\Ui\\Callback::isTriggered\(\) has no return typehint specified\.$~'
message: '~^Method Atk4\\Ui\\Callback::add\(\) has parameter \$args with no typehint specified\.$~'
-
path: 'src/Card.php'
message: '~^Method Atk4\\Ui\\Card::setDataId\(\) has no return typehint specified\.$~'
Expand Down
7 changes: 6 additions & 1 deletion src/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Callback extends AbstractView
/** @var bool Allow this callback to trigger during a reload. */
public $triggerOnReload = true;

public function add($object, $args = null): AbstractView
{
throw new Exception('Callback can NOT contains children');
}

/**
* Initialization.
*/
Expand Down Expand Up @@ -84,7 +89,7 @@ public function terminateJson(AbstractView $view): void
/**
* Return true if urlTrigger is part of the request.
*/
public function isTriggered()
public function isTriggered(): bool
{
return isset($_GET[self::URL_QUERY_TRIGGER_PREFIX . $this->urlTrigger]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ protected function getRunningCallbackArgs(bool $isTerminated, array $page): arra
$isTerminated = true;
}

if ($isTerminated) {
if ($isTerminated && $v->isTriggered() && $v->canTrigger()) {
$args[Callback::URL_QUERY_TRIGGER_PREFIX . $v->getUrlTrigger()] = $v->getTriggeredValue();
}
}
Expand Down
18 changes: 13 additions & 5 deletions tests/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,22 @@ public function testCallbackTrigger(): void
public function testViewUrlCallback(): void
{
$cbApp = \Atk4\Ui\Callback::addTo($this->app, ['urlTrigger' => 'aa']);
$v1 = \Atk4\Ui\View::addTo($cbApp);
$v1 = \Atk4\Ui\View::addTo($this->app);
$cb = \Atk4\Ui\Callback::addTo($v1, ['urlTrigger' => 'bb']);
$cb->name = 'bbx';

$this->simulateCallbackTriggering($cbApp);
$this->simulateCallbackTriggering($cb);

$this->assertSame('?__atk_cb_aa=callback&__atk_cbtarget=aa', $cbApp->getUrl());
$this->assertSame('?__atk_cb_bb=callback&__atk_cbtarget=bb', $cb->getUrl());
$expectedUrlCbApp = '?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=callback&' . Callback::URL_QUERY_TARGET . '=aa';
$expectedUrlCb = '?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=1&' . Callback::URL_QUERY_TRIGGER_PREFIX . 'bb=callback&' . Callback::URL_QUERY_TARGET . '=bb';
$this->assertSame($expectedUrlCbApp, $cbApp->getUrl());
$this->assertSame($expectedUrlCb, $cb->getUrl());

// URL must remain the same when urlTrigger is set but name is changed
$cbApp->name = 'aax';
$cb->name = 'bbx';
$this->assertSame($expectedUrlCbApp, $cbApp->getUrl());
$this->assertSame($expectedUrlCb, $cb->getUrl());

$var = null;
$cb->set(function ($x) use (&$var, $v1) {
Expand All @@ -109,7 +117,7 @@ public function testCallbackNotFiring(): void
{
$cb = \Atk4\Ui\Callback::addTo($this->app);

// do NOT simulate triggering
// do NOT simulate triggering in this test

$var = null;
$cb->set(function ($x) use (&$var) {
Expand Down

0 comments on commit 428165b

Please sign in to comment.