Skip to content

Commit

Permalink
rm JsExpression hack
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Feb 13, 2024
1 parent 1dccdc1 commit c8db488
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/Form/Control/DropdownCascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function getNewValues($id): array
$res = ($this->renderRowFunction)($row); // @phpstan-ignore-line
$values[] = ['value' => $res['value'], 'text' => $res['title'], 'name' => $res['title']];
} else {
$values[] = ['value' => $row->getId(), 'text' => $row->get($model->titleField), 'name' => $row->get($model->titleField)];
$values[] = ['value' => $this->getApp()->uiPersistence->typecastAttributeSaveField($model->getField($model->idField), $row->getId()), 'text' => $row->get($model->titleField), 'name' => $row->get($model->titleField)];
}
}

Expand All @@ -93,12 +93,15 @@ public function getNewValues($id): array
* Will mark current value as selected from a list
* of possible values.
*
* @param string|int $value the current field value
* @param mixed $value the current field value
*/
private function getJsValues(array $values, $value): array
{
$model = $this->cascadeFrom->model->ref($this->reference);
$valueStr = $this->getApp()->uiPersistence->typecastAttributeSaveField($model->getField($model->idField), $value);

foreach ($values as $k => $v) {
if ($v['value'] === $value) {
if ($v['value'] === $valueStr) {
$values[$k]['selected'] = true;

break;
Expand Down
8 changes: 0 additions & 8 deletions src/Js/JsExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
namespace Atk4\Ui\Js;

use Atk4\Core\DiContainerTrait;
use Atk4\Data\Field;
use Atk4\Data\Persistence;
use Atk4\Ui\Demos\WrappedId;
use Atk4\Ui\Demos\WrappedIdType;
use Atk4\Ui\Exception;
use Atk4\Ui\Persistence\Ui as UiPersistence;
use Atk4\Ui\View;

class JsExpression implements JsExpressionable
Expand Down Expand Up @@ -111,10 +107,6 @@ protected function _jsEncode($value): string
$res = Persistence\Sql\Expression::castFloatToString($value);
} elseif ($value === null) {
$res = 'null';
} elseif ($value instanceof WrappedId) {
// HACK for debug
$uiPersistence = new UiPersistence();
$res = $this->_jsEncode($uiPersistence->typecastAttributeSaveField(new Field(['type' => WrappedIdType::NAME]), $value));
} else {
throw (new Exception('Argument is not renderable to JS'))
->addMoreInfo('arg', $value);
Expand Down
2 changes: 1 addition & 1 deletion src/UserAction/ConfirmationExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function doConfirmation(View $modal): void
$this->loader->jsLoad(
[
'step' => 'execute',
$this->name => $this->action->getEntity()->getId(),
$this->name => $this->getApp()->uiPersistence->typecastAttributeSaveField($this->action->getModel()->getField($this->action->getModel()->idField), $this->action->getEntity()->getId()),
],
['method' => 'POST']
),
Expand Down
8 changes: 4 additions & 4 deletions src/UserAction/StepExecutorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected function doPreview(View $page): void
$chain = $this->loader->jsLoad(
[
'step' => $this->getPreviousStep($this->step),
$this->name => $this->action->getEntity()->getId(),
$this->name => $this->getApp()->uiPersistence->typecastAttributeSaveField($this->action->getModel()->getField($this->action->getModel()->idField), $this->action->getEntity()->getId()),
],
['method' => 'POST'],
$this->loader->name
Expand All @@ -203,7 +203,7 @@ protected function doPreview(View $page): void
$this->loader->jsLoad(
[
'step' => 'final',
$this->name => $this->action->getEntity()->getId(),
$this->name => $this->getApp()->uiPersistence->typecastAttributeSaveField($this->action->getModel()->getField($this->action->getModel()->idField), $this->action->getEntity()->getId()),
],
['method' => 'POST'],
$this->loader->name
Expand Down Expand Up @@ -363,7 +363,7 @@ protected function jsSetPreviousHandler(View $view, string $step): void
$chain = $this->loader->jsLoad(
[
'step' => $this->getPreviousStep($step),
$this->name => $this->action->getEntity()->getId(),
$this->name => $this->getApp()->uiPersistence->typecastAttributeSaveField($this->action->getModel()->getField($this->action->getModel()->idField), $this->action->getEntity()->getId()),
],
['method' => 'POST'],
$this->loader->name
Expand Down Expand Up @@ -401,7 +401,7 @@ protected function jsStepSubmit(string $step): JsBlock
$this->loader->jsLoad(
[
'step' => $this->isLastStep($step) ? 'final' : $this->getNextStep($step),
$this->name => $this->action->getEntity()->getId(),
$this->name => $this->getApp()->uiPersistence->typecastAttributeSaveField($this->action->getModel()->getField($this->action->getModel()->idField), $this->action->getEntity()->getId()),
],
['method' => 'POST'],
$this->loader->name
Expand Down
7 changes: 6 additions & 1 deletion src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ public function jsRender(): string
} elseif ($action instanceof UserAction\ExecutorInterface || $action instanceof UserAction\SharedExecutor || $action instanceof Model\UserAction) {
$ex = $action instanceof Model\UserAction ? $this->getExecutorFactory()->createExecutor($action, $this) : $action;

$setupNonSharedExecutorFx = static function (UserAction\ExecutorInterface $ex) use (&$defaults, &$arguments): void {
$setupNonSharedExecutorFx = function (UserAction\ExecutorInterface $ex) use (&$defaults, &$arguments): void {
/** @var AbstractView&UserAction\ExecutorInterface $ex https://github.com/phpstan/phpstan/issues/3770 */
$ex = $ex;

Expand All @@ -1020,6 +1020,11 @@ public function jsRender(): string
unset($arguments[0]);
}

if (isset($arguments[$ex->name]) && !$arguments[$ex->name] instanceof JsExpressionable) {
$exModel = $ex->getAction()->getModel();
$arguments[$ex->name] = $this->getApp()->uiPersistence->typecastAttributeSaveField($exModel->getField($exModel->idField), $arguments[$ex->name]);
}

if ($ex instanceof UserAction\JsCallbackExecutor) {
$confirmation = $ex->getAction()->getConfirmation();
if ($confirmation) {
Expand Down

0 comments on commit c8db488

Please sign in to comment.