1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui\Js;
6:
7: use Atk4\Core\WarnDynamicPropertyTrait;
8: use Atk4\Ui\View;
9:
10: /**
11: * Generates action, that will be able to loop-back to the callback method.
12: */
13: class JsReload implements JsExpressionable
14: {
15: use WarnDynamicPropertyTrait;
16:
17: /** Specifies which view to reload. Use constructor to set. */
18: public View $view;
19:
20: /** @var JsExpressionable|null A JS function to execute after reload is complete and onSuccess is execute. */
21: public $afterSuccess;
22:
23: /** @var array<string, string|int|JsExpressionable> Added at the end of your URL. */
24: public array $args = [];
25:
26: /**
27: * Fomantic-UI api settings.
28: * ex: ['loadingDuration' => 1000].
29: */
30: public array $apiConfig = [];
31:
32: /** @var bool */
33: public $includeStorage = false;
34:
35: /**
36: * @param array<string, string|int|JsExpressionable> $args
37: */
38: public function __construct(View $view, array $args = [], JsExpressionable $afterSuccess = null, array $apiConfig = [], bool $includeStorage = false)
39: {
40: $this->view = $view;
41: $this->args = $args;
42: $this->afterSuccess = $afterSuccess;
43: $this->apiConfig = $apiConfig;
44: $this->includeStorage = $includeStorage;
45: }
46:
47: #[\Override]
48: public function jsRender(): string
49: {
50: $final = (new Jquery($this->view))
51: ->atkReloadView(
52: [
53: 'url' => $this->view->jsUrl(['__atk_reload' => $this->view->name]),
54: 'urlOptions' => $this->args !== [] ? $this->args : null,
55: 'afterSuccess' => $this->afterSuccess ? $this->afterSuccess->jsRender() : null,
56: 'apiConfig' => $this->apiConfig !== [] ? $this->apiConfig : null,
57: 'storeName' => $this->includeStorage ? $this->view->name : null,
58: ]
59: );
60:
61: return $final->jsRender();
62: }
63: }
64: