| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui\UserAction; |
| 6: | |
| 7: | use Atk4\Ui\Button; |
| 8: | use Atk4\Ui\Message; |
| 9: | use Atk4\Ui\View; |
| 10: | |
| 11: | class PreviewExecutor extends BasicExecutor |
| 12: | { |
| 13: | |
| 14: | public $preview; |
| 15: | |
| 16: | |
| 17: | public $previewType = 'console'; |
| 18: | |
| 19: | #[\Override] |
| 20: | public function initPreview(): void |
| 21: | { |
| 22: | if (!$this->hasAllArguments()) { |
| 23: | Message::addTo($this, ['type' => 'error', $this->missingArgsMsg]); |
| 24: | |
| 25: | return; |
| 26: | } |
| 27: | |
| 28: | $text = $this->executePreview(); |
| 29: | |
| 30: | switch ($this->previewType) { |
| 31: | case 'console': |
| 32: | $this->preview = View::addTo($this, ['ui' => 'inverted black segment', 'element' => 'pre']); |
| 33: | $this->preview->set($text); |
| 34: | |
| 35: | break; |
| 36: | case 'text': |
| 37: | $this->preview = View::addTo($this, ['ui' => 'segment']); |
| 38: | $this->preview->set($text); |
| 39: | |
| 40: | break; |
| 41: | case 'html': |
| 42: | $this->preview = View::addTo($this, ['ui' => 'segment']); |
| 43: | $this->preview->template->dangerouslySetHtml('Content', $text); |
| 44: | |
| 45: | break; |
| 46: | } |
| 47: | |
| 48: | Button::addToWithCl($this, $this->executorButton)->on('click', function () { |
| 49: | return $this->executeModelAction(); |
| 50: | }); |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public function executePreview() |
| 57: | { |
| 58: | $args = []; |
| 59: | foreach ($this->action->args as $key => $val) { |
| 60: | $args[] = $this->arguments[$key]; |
| 61: | } |
| 62: | |
| 63: | return $this->action->preview(...$args); |
| 64: | } |
| 65: | } |
| 66: | |