Skip to content

Commit

Permalink
View::js()/on() returns null when JS chain is explicitly given (#1944)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Dec 5, 2022
1 parent b7f97e1 commit 18600ed
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 83 deletions.
2 changes: 1 addition & 1 deletion demos/_includes/DemoLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function initQuickNewRecord(): void

$ret = [
new JsToast('Form submit!. Data are not save in demo mode.'),
(new Jquery('.atk-modal'))->modal('hide'),
(new Jquery())->closest('.atk-modal')->modal('hide'),
];

$row = $this->renderRow($form->model);
Expand Down
2 changes: 1 addition & 1 deletion demos/_unit-test/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
return [
$table->jsReload(),
new JsToast('Save'),
(new Jquery('.ui.modal.visible.active.front'))->modal('hide'),
(new Jquery())->closest('.ui.modal')->modal('hide'),
];
});
12 changes: 6 additions & 6 deletions demos/interactive/modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@
if ($page === 1) {
Message::addTo($p)->set('Thanks for choosing us. We will be asking some questions along the way.');
$session->memorize('success', true);
$p->js(true, $prevAction->js(true)->show());
$p->js(true, $nextAction->js(true)->show());
$p->js(true, $prevAction->js()->show());
$p->js(true, $nextAction->js()->show());
$p->js(true, $prevAction->js()->addClass('disabled'));
$p->js(true, $nextAction->js(true)->removeClass('disabled'));
$p->js(true, $nextAction->js()->removeClass('disabled'));
} elseif ($page === 2) {
$modelRegister = new Model(new Persistence\Array_());
$modelRegister->addField('name', ['caption' => 'Please enter your name (John)']);
Expand All @@ -213,13 +213,13 @@
return $js;
});
$p->js(true, $prevAction->js()->removeClass('disabled'));
$p->js(true, $nextAction->js(true)->addClass('disabled'));
$p->js(true, $nextAction->js()->addClass('disabled'));
} elseif ($page === 3) {
$name = $session->recall('name');
Message::addTo($p)->set("Thank you {$name} for visiting us! We will be in touch");
$session->memorize('success', true);
$p->js(true, $prevAction->js(true)->hide());
$p->js(true, $nextAction->js(true)->hide());
$p->js(true, $prevAction->js()->hide());
$p->js(true, $nextAction->js()->hide());
}
});

Expand Down
10 changes: 8 additions & 2 deletions demos/javascript/js.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@

$b = Button::addTo($app, ['Hide button B']);
$b2 = Button::addTo($app, ['B']);
$b->js('click', $b2->js()->hide())->addClass('disabled')->addClass('disabled');
$b->on('click', [
$b->js()->addClass('disabled')->addClass('disabled'),
$b2->js()->hide(),
]);

Header::addTo($app, ['on() method']);

$b = Button::addTo($app, ['Hide button C and self']);
$b2 = Button::addTo($app, ['C']);
$b->on('click', null, $b2->js()->hide())->hide();
$b->on('click', null, [
$b->js()->hide(),
$b2->js()->hide(),
]);

Header::addTo($app, ['Callbacks']);

Expand Down
2 changes: 1 addition & 1 deletion js/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
'no-restricted-syntax': 'off',
'no-underscore-dangle': 'off',
'max-len': 'off',
'prefer-template': ['off'],
'prefer-template': 'off',
'no-unused-vars': ['error', { vars: 'all', args: 'none' }],
'padding-line-between-statements': ['error', {
blankLine: 'always',
Expand Down
2 changes: 1 addition & 1 deletion public/css/agileui.less
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ header.atk-topMenu.ui.menu .item > .label {
.atk-mainContainer {
flex: 1;
.atk-mainContainerWrapper {
padding: 3em;
padding: 2em;
}
&.atk-admin-layout {
margin-top: @adminMenuHeight;
Expand Down
2 changes: 1 addition & 1 deletion public/css/agileui.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions src/CardDeck.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public function setModel(Model $model, array $fields = null, array $extra = null
$this->search->setModelCondition($this->model);
}

if ($count = $this->initPaginator()) {
$count = $this->initPaginator();
if ($count) {
foreach ($this->model as $m) {
$c = $this->cardHolder->add(Factory::factory([$this->card], ['useLabel' => $this->useLabel, 'useTable' => $this->useTable]))->addClass('segment');
$c->setModel($m, $fields);
Expand Down Expand Up @@ -401,15 +402,10 @@ protected function renderView(): void
*/
private function getModelActions(string $appliesTo): array
{
$actions = [];
if ($appliesTo === Model\UserAction::APPLIES_TO_SINGLE_RECORD && $this->singleScopeActions !== []) {
foreach ($this->singleScopeActions as $action) {
$actions[] = $this->model->getUserAction($action);
}
$actions = array_map(fn ($v) => $this->model->getUserAction($v), $this->singleScopeActions);
} elseif ($appliesTo === Model\UserAction::APPLIES_TO_NO_RECORDS && $this->noRecordScopeActions !== []) {
foreach ($this->noRecordScopeActions as $action) {
$actions[] = $this->model->getUserAction($action);
}
$actions = array_map(fn ($v) => $this->model->getUserAction($v), $this->noRecordScopeActions);
} else {
$actions = $this->model->getUserActions($appliesTo);
}
Expand Down
25 changes: 12 additions & 13 deletions src/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Crud extends Grid
/** @var bool|null should we use table column drop-down menu to display user actions? */
public $useMenuActions;

/** Collection of APPLIES_TO_NO_RECORDS Scope Model action menu item */
/** @var array<string, array{item: MenuItem, executor: object}> Collection of APPLIES_TO_NO_RECORDS Scope Model action menu item */
private array $menuItems = [];

/** Model single scope action to include in table action column. Will include all single scope actions if empty. */
Expand Down Expand Up @@ -63,9 +63,9 @@ public function applySort(): void
{
parent::applySort();

if ($this->getSortBy() && $this->menuItems !== []) {
if ($this->getSortBy()) {
foreach ($this->menuItems as $item) {
// Remove previous click handler and attach new one using sort argument.
// remove previous click handler and attach new one using sort argument
$this->container->js(true, $item['item']->js()->off('click.atk_crud_item'));
$ex = $item['executor'];
if ($ex instanceof UserAction\JsExecutorInterface) {
Expand Down Expand Up @@ -194,8 +194,9 @@ protected function getJsGridAction(Model\UserAction $action): ?JsExpressionable
case Model\UserAction::MODIFIER_DELETE:
// use deleted record id to remove row, fallback to closest tr if id is not available.
$js = $this->deletedId
? (new Jquery('tr[data-id="' . $this->deletedId . '"]'))->transition('fade left')
: (new Jquery())->closest('tr')->transition('fade left');
? $this->js(false, null, 'tr[data-id="' . $this->deletedId . '"]')
: (new Jquery())->closest('tr');
$js = $js->transition('fade left', new JsFunction([], [new JsExpression('this.remove()')]));

break;
default:
Expand Down Expand Up @@ -229,7 +230,10 @@ protected function getNotifier(string $msg = null)
protected function setItemsAction(): void
{
foreach ($this->menuItems as $k => $item) {
$this->container->js(true, $item['item']->on('click.atk_crud_item', $item['executor']));
// hack - render executor action via MenuItem::on() into container
$item['item']->on('click.atk_crud_item', $item['executor']);
$jsAction = array_pop($item['item']->_jsActions['click.atk_crud_item']);
$this->container->js(true, $jsAction);
}
}

Expand Down Expand Up @@ -274,15 +278,10 @@ private function _getReloadArgs()
*/
private function _getModelActions(string $appliesTo): array
{
$actions = [];
if ($appliesTo === Model\UserAction::APPLIES_TO_SINGLE_RECORD && $this->singleScopeActions !== []) {
foreach ($this->singleScopeActions as $action) {
$actions[] = $this->model->getUserAction($action);
}
$actions = array_map(fn ($v) => $this->model->getUserAction($v), $this->singleScopeActions);
} elseif ($appliesTo === Model\UserAction::APPLIES_TO_NO_RECORDS && $this->noRecordScopeActions !== []) {
foreach ($this->noRecordScopeActions as $action) {
$actions[] = $this->model->getUserAction($action);
}
$actions = array_map(fn ($v) => $this->model->getUserAction($v), $this->noRecordScopeActions);
} else {
$actions = $this->model->getUserActions($appliesTo);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ protected function initQuickNewRecord(): void
$form->model->save();

$ret = [
(new Jquery('.atk-modal'))->modal('hide'),
(new Jquery())->closest('.atk-modal')->modal('hide'),
];

$row = $this->renderRow($form->model);
Expand Down
6 changes: 3 additions & 3 deletions src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@
* For example, ypdating Grand Total field of all invoice items.
*
* $ml->onChange(function (array $rows) use ($form) {
* $grand_total = 0;
* $grandTotal = 0;
* foreach ($rows as $row => $cols) {
* foreach ($cols as $col) {
* $fieldName = array_key_first($col);
* if ($fieldName === 'total') {
* $grand_total += $col[$fieldName];
* $grandTotal += $col[$fieldName];
* }
* }
* }
*
* return $form->js(true, null, 'input[name="grand_total"]')->val($app->uiPersistence->typecastSaveField(new Field(['type' => 'atk4_money']), $grand_total));
* return $form->js(false, null, 'input[name="grand_total"]')->val($app->uiPersistence->typecastSaveField(new Field(['type' => 'atk4_money']), $grandTotal));
* }, ['qty', 'price']);
*
* Finally, it's also possible to use Multiline for quickly adding records to a
Expand Down
9 changes: 8 additions & 1 deletion src/JsCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ public function set($fx = null, $args = null)

$response = $fx($chain, ...$values);

if (count($chain->_chain) === 0) {
// TODO should we create/pass $chain to $fx at all?
$chain = null;
} elseif ($response) {
// TODO throw when non-empty chain is to be ignored?
}

$ajaxec = $response ? $this->getAjaxec($response, $chain) : null;

$this->terminateAjax($ajaxec);
Expand Down Expand Up @@ -129,7 +136,7 @@ public function getAjaxec($response, $chain = null): string
{
$actions = [];

if ($chain && $chain->_chain) {
if ($chain !== null) {
$actions[] = $chain;
}

Expand Down
Loading

0 comments on commit 18600ed

Please sign in to comment.