Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve callbacks url generation #1646

Merged
merged 26 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demos/interactive/tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
$modelRegister->addField('name', ['caption' => 'Please enter your name (John)']);

$form = \Atk4\Ui\Form::addTo($tab, ['segment' => true]);
$form->setModel($modelRegister);
$form->setModel($modelRegister->createEntity());
$form->onSubmit(function (\Atk4\Ui\Form $form) {
if ($form->model->get('name') !== 'John') {
return $form->error('name', 'Your name is not John! It is "' . $form->model->get('name') . '". It should be John. Pleeease!');
Expand Down
2 changes: 1 addition & 1 deletion demos/interactive/wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Demonstrates how to use a wizard.
*/
$wizard = Wizard::addTo($app, ['stepCallback' => Callback::addTo($app, ['urlTrigger' => 'demo_wizard'])]);
$wizard = Wizard::addTo($app, ['urlTrigger' => 'demo_wizard']);
// First step will automatcally be active when you open page first. It
// will contain the 'Next' button with a link.
$wizard->addStep('Welcome', function (Wizard $wizard) {
Expand Down
9 changes: 0 additions & 9 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,6 @@ parameters:
-
path: 'src/View.php'
message: '~^If condition is always true\.$~'
-
path: 'src/Wizard.php'
message: '~^Negated boolean expression is always false\.$~'
-
path: 'tests/DemosTest.php'
message: '~^Else branch is unreachable because ternary operator condition is always true\.$~'
Expand Down Expand Up @@ -1201,12 +1198,6 @@ parameters:
-
path: 'src/AbstractView.php'
message: '~^Method Atk4\\Ui\\AbstractView::add\(\) has parameter \$args with no typehint specified\.$~'
-
path: 'src/AbstractView.php'
message: '~^Instanceof between Atk4\\Ui\\View and Atk4\\Ui\\AbstractView will always evaluate to true\.$~'
-
path: 'src/AbstractView.php'
message: '~^Strict comparison using \!\=\= between Atk4\\Ui\\AbstractView and null will always evaluate to true\.$~'
-
path: 'src/Accordion.php'
message: '~^Method Atk4\\Ui\\Accordion::activate\(\) has no return typehint specified\.$~'
Expand Down
87 changes: 0 additions & 87 deletions src/AbstractView.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,91 +119,4 @@ public function add($object, $args = null): self

return $object;
}

// }}}

// {{{ Sticky URLs

/** @var string[] stickyGet arguments */
public $stickyArgs = [];
mvorisek marked this conversation as resolved.
Show resolved Hide resolved

/**
* Build an URL which this view can use for js call-backs. It should
* be guaranteed that requesting returned URL would at some point call
* $this->invokeInit().
*
* @param array $page
*
* @return string
*/
public function jsUrl($page = [])
{
return $this->getApp()->jsUrl($page, false, $this->_getStickyArgs());
}

/**
* Build an URL which this view can use for call-backs. It should
* be guaranteed that requesting returned URL would at some point call
* $this->invokeInit().
*
* @param string|array $page URL as string or array with page name as first element and other GET arguments
*
* @return string
*/
public function url($page = [])
{
return $this->getApp()->url($page, false, $this->_getStickyArgs());
}

/**
* Get sticky arguments defined by the view and parents (including API).
*/
protected function _getStickyArgs(): array
{
if ($this->issetOwner() && $this->getOwner() instanceof self) {
$stickyArgs = array_merge($this->getOwner()->_getStickyArgs(), $this->stickyArgs);
} else {
$stickyArgs = $this->stickyArgs;
}

/** @var self $childView */
$childView = $this->mergeStickyArgsFromChildView();
if ($childView !== null && (!($childView instanceof Callback) || $childView->isTriggered())) {
$alreadyCalled = false;
foreach (debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
if ($childView === ($frame['object'] ?? null) && $frame['function'] === '_getStickyArgs') {
$alreadyCalled = true;
}
}

if (!$alreadyCalled) {
$stickyArgs = array_merge($stickyArgs, $childView->_getStickyArgs());
}
}

return $stickyArgs;
}

protected function mergeStickyArgsFromChildView(): ?self
{
return null;
}

/**
* Mark GET argument as sticky. Calling url() on this view or any
* sub-views will embedd the value of this GET argument.
*
* If GET argument is empty or false, it won't make into URL.
*
* If GET argument is not presently set you can specify a 2nd argument
* to forge-set the GET argument for current view and it's sub-views.
*/
public function stickyGet(string $name, string $newValue = null): ?string
{
$this->stickyArgs[$name] = $newValue ?? $_GET[$name] ?? null;

return $this->stickyArgs[$name];
}

// }}}
}
25 changes: 19 additions & 6 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ class App
'cache-control' => 'no-store', // disable caching by default
];

/** @var Modal[] Modal view that need to be rendered using json output. */
private $modals = [];

/**
* @var bool whether or not semantic-ui vue has been initialised
*/
Expand Down Expand Up @@ -222,6 +225,17 @@ static function (int $severity, string $msg, string $file, int $line): bool {
$this->executorFactory = Factory::factory([ExecutorFactory::class]);
}

/**
* Register a modal view.
* Fomantic-ui Modal are teleported in HTML template
* within specific location. This will keep track
* of modals when terminating app using json.
*/
public function registerModal(Modal $modal): void
{
$this->modals[$modal->short_name] = $modal;
}

public function setExecutorFactory(ExecutorFactory $factory)
{
$this->executorFactory = $factory;
Expand Down Expand Up @@ -554,7 +568,8 @@ public function run()
$this->hook(self::HOOK_BEFORE_OUTPUT);

if (isset($_GET['__atk_callback']) && $this->catch_runaway_callbacks) {
throw new Exception('Callback requested, but never reached. You may be missing some arguments in request URL.');
throw (new Exception('Callback requested, but never reached. You may be missing some arguments in request URL.'))
->addMoreInfo('callback', $_GET['__atk_callback']);
}

$output = $this->html->template->renderToHtml();
Expand Down Expand Up @@ -1132,11 +1147,9 @@ public function getRenderedModals(): array
unset($_GET['__atk_reload']);

$modals = [];
foreach ($this->html !== null ? $this->html->elements : [] as $view) {
if ($view instanceof Modal) {
$modals[$view->name]['html'] = $view->getHtml();
$modals[$view->name]['js'] = $view->getJsRenderActions();
}
foreach ($this->modals as $view) {
$modals[$view->name]['html'] = $view->getHtml();
$modals[$view->name]['js'] = $view->getJsRenderActions();
}

return $modals;
Expand Down
24 changes: 6 additions & 18 deletions src/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ protected function init(): void
public function setUrlTrigger(string $trigger = null)
{
$this->urlTrigger = $trigger ?: $this->name;

$this->getOwner()->stickyGet($this->urlTrigger);
mvorisek marked this conversation as resolved.
Show resolved Hide resolved
}

public function getUrlTrigger(): string
Expand All @@ -61,13 +63,7 @@ public function getUrlTrigger(): string
public function set($fx = null, $args = null)
{
if ($this->isTriggered() && $this->canTrigger()) {
$this->getApp()->catch_runaway_callbacks = false;
$t = $this->getApp()->run_called;
$this->getApp()->run_called = true;
$ret = $fx(...($args ?? []));
$this->getApp()->run_called = $t;

return $ret;
return $fx(...($args ?? []));
}
}

Expand Down Expand Up @@ -115,12 +111,12 @@ public function canTrigger(): bool

/**
* Return URL that will trigger action on this call-back. If you intend to request
* the URL direcly in your browser (as iframe, new tab, or document location), you
* the URL directly in your browser (as iframe, new tab, or document location), you
* should use getUrl instead.
*/
public function getJsUrl(string $value = 'ajax'): string
{
return $this->jsUrl($this->getUrlArguments($value));
return $this->getOwner()->jsUrl($this->getUrlArguments($value));
}

/**
Expand All @@ -129,7 +125,7 @@ public function getJsUrl(string $value = 'ajax'): string
*/
public function getUrl(string $value = 'callback'): string
{
return $this->url($this->getUrlArguments($value));
return $this->getOwner()->url($this->getUrlArguments($value));
}

/**
Expand All @@ -139,12 +135,4 @@ private function getUrlArguments(string $value = null): array
{
return ['__atk_callback' => $this->urlTrigger, $this->urlTrigger => $value ?? $this->getTriggeredValue()];
}

protected function _getStickyArgs(): array
{
// DEV NOTE:
// - getUrlArguments $value used only in https://github.com/atk4/ui/blob/08644a685a9ee07b4e94d1e35e3bd3c95b7a013d/src/VirtualPage.php#L134
// - $_GET['__atk_callback'] from getUrlArguments seems to control terminating behaviour!
return array_merge(parent::_getStickyArgs(), $this->getUrlArguments() /* TODO we do not want/need all Callback args probably*/);
}
}
5 changes: 0 additions & 5 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,4 @@ public function jsLoad($args = [], $apiConfig = [], $storeName = null)
'storeName' => $storeName ? $storeName : null,
]);
}

protected function mergeStickyArgsFromChildView(): AbstractView
{
return $this->cb;
}
}
14 changes: 6 additions & 8 deletions src/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class Modal extends View
*/
public $showActions = false;

protected function init(): void
{
parent::init();
$this->getApp()->registerModal($this);
}

/**
* Set callback function for this modal.
* $fx is set as an array in order to comply with View::set().
Expand Down Expand Up @@ -330,12 +336,4 @@ protected function renderView(): void

parent::renderView();
}

/** @var AbstractView */
public $viewForUrl;

protected function mergeStickyArgsFromChildView(): ?AbstractView
{
return $this->viewForUrl ?? $this->cb;
}
}
5 changes: 0 additions & 5 deletions src/Panel/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,4 @@ public function getClearSelector(): array
{
return ['.atk-panel-content'];
}

protected function mergeStickyArgsFromChildView(): ?\Atk4\Ui\AbstractView
{
return $this->cb;
}
}
5 changes: 0 additions & 5 deletions src/Panel/Right.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,4 @@ protected function renderView(): void

$this->js(true, $this->service()->addPanel($this->getPanelOptions()));
}

protected function mergeStickyArgsFromChildView(): ?\Atk4\Ui\AbstractView
{
return $this->dynamicContent;
}
}
5 changes: 0 additions & 5 deletions src/Popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,4 @@ protected function renderView(): void

parent::renderView();
}

protected function mergeStickyArgsFromChildView(): ?AbstractView
{
return $this->cb;
}
}
15 changes: 1 addition & 14 deletions src/UserAction/ExecutorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Atk4\Ui\Button;
use Atk4\Ui\Exception;
use Atk4\Ui\Item;
use Atk4\Ui\Modal;
use Atk4\Ui\View;

/**
Expand Down Expand Up @@ -172,19 +171,7 @@ protected function createExecutor(UserAction $action, View $owner, string $requi
}
}

$executor = Factory::factory($seed);
if ($executor instanceof Modal) {
// add modal to app->html for proper rendering on callback.
if (!isset($owner->getApp()->html->elements[$executor->short_name])) {
// very dirty hack, @TODO, attach modals in the standard render tree
// but only render the result to a different place/html DOM
$executor->viewForUrl = $owner;
$executor = $owner->getApp()->html->add($executor, 'Modals'); //->setAction($action);
}
} else {
$executor = $owner->add($executor);
}

$executor = $owner->add(Factory::factory($seed));
mvorisek marked this conversation as resolved.
Show resolved Hide resolved
$executor->setAction($action);

return $executor;
Expand Down
Loading