| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui\Panel; |
| 6: | |
| 7: | use Atk4\Core\Factory; |
| 8: | use Atk4\Ui\Button; |
| 9: | use Atk4\Ui\Js\Jquery; |
| 10: | use Atk4\Ui\Js\JsChain; |
| 11: | use Atk4\Ui\Js\JsExpressionable; |
| 12: | use Atk4\Ui\Modal; |
| 13: | use Atk4\Ui\View; |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | class Right extends View implements Loadable |
| 24: | { |
| 25: | public array $class = ['atk-right-panel']; |
| 26: | public $defaultTemplate = 'panel/right.html'; |
| 27: | |
| 28: | |
| 29: | public $closeModal; |
| 30: | |
| 31: | public $defaultModal = [Modal::class, 'class' => ['mini']]; |
| 32: | |
| 33: | |
| 34: | protected $dynamicContent; |
| 35: | |
| 36: | |
| 37: | protected $hasClickAway = true; |
| 38: | |
| 39: | |
| 40: | protected $hasEscAway = true; |
| 41: | |
| 42: | |
| 43: | public $dynamic = [Content::class]; |
| 44: | |
| 45: | |
| 46: | public $closeSelector = '.atk-panel-close'; |
| 47: | |
| 48: | |
| 49: | public $warningSelector = '.atk-panel-warning'; |
| 50: | |
| 51: | |
| 52: | public $warningTrigger = 'atk-visible'; |
| 53: | |
| 54: | |
| 55: | public $warningIcon = 'exclamation circle'; |
| 56: | |
| 57: | |
| 58: | public $closeIcon = 'times'; |
| 59: | |
| 60: | #[\Override] |
| 61: | protected function init(): void |
| 62: | { |
| 63: | parent::init(); |
| 64: | |
| 65: | if ($this->dynamic) { |
| 66: | $this->addDynamicContent(Factory::factory($this->dynamic)); |
| 67: | } |
| 68: | } |
| 69: | |
| 70: | #[\Override] |
| 71: | public function addDynamicContent(LoadableContent $content): void |
| 72: | { |
| 73: | $this->dynamicContent = Content::addTo($this, [], ['LoadContent']); |
| 74: | } |
| 75: | |
| 76: | #[\Override] |
| 77: | public function getDynamicContent(): LoadableContent |
| 78: | { |
| 79: | return $this->dynamicContent; |
| 80: | } |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | public function service(): JsChain |
| 86: | { |
| 87: | return new JsChain('atk.panelService'); |
| 88: | } |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | public function jsOpen(array $urlArgs = [], array $dataAttribute = [], string $activeCss = null, JsExpressionable $jsTrigger = null): JsExpressionable |
| 99: | { |
| 100: | return $this->service()->openPanel([ |
| 101: | 'triggered' => $jsTrigger ?? new Jquery(), |
| 102: | 'reloadArgs' => $dataAttribute, |
| 103: | 'urlArgs' => $urlArgs, |
| 104: | 'openId' => $this->name, |
| 105: | 'activeCSS' => $activeCss, |
| 106: | ]); |
| 107: | } |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | public function jsPanelReload(array $args = []): JsExpressionable |
| 113: | { |
| 114: | return $this->service()->reloadPanel($this->name, $args); |
| 115: | } |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | public function jsClose(): JsExpressionable |
| 121: | { |
| 122: | return $this->service()->closePanel($this->name); |
| 123: | } |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | public function addConfirmation(string $msg, string $title = 'Closing panel!', string $okButton = null, string $cancelButton = null): void |
| 131: | { |
| 132: | if (!$okButton) { |
| 133: | $okButton = (new Button(['Ok']))->addClass('ok'); |
| 134: | } |
| 135: | |
| 136: | if (!$cancelButton) { |
| 137: | $cancelButton = (new Button(['Cancel']))->addClass('cancel'); |
| 138: | } |
| 139: | $this->closeModal = $this->getApp()->add(array_merge($this->defaultModal, ['title' => $title])); |
| 140: | $this->closeModal->add([View::class, $msg, 'element' => 'p']); |
| 141: | $this->closeModal->addButtonAction(Factory::factory($okButton)); |
| 142: | $this->closeModal->addButtonAction(Factory::factory($cancelButton)); |
| 143: | |
| 144: | $this->closeModal->notClosable(); |
| 145: | } |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | public function onOpen(\Closure $fx): void |
| 154: | { |
| 155: | $this->getDynamicContent()->onLoad($fx); |
| 156: | } |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | public function jsDisplayWarning(bool $state = true): JsExpressionable |
| 164: | { |
| 165: | $chain = new Jquery('#' . $this->name . ' ' . $this->warningSelector); |
| 166: | |
| 167: | return $state ? $chain->addClass($this->warningTrigger) : $chain->removeClass($this->warningTrigger); |
| 168: | } |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | public function jsToggleWarning(): JsExpressionable |
| 176: | { |
| 177: | return (new Jquery('#' . $this->name . ' ' . $this->warningSelector))->toggleClass($this->warningTrigger); |
| 178: | } |
| 179: | |
| 180: | public function getPanelOptions(): array |
| 181: | { |
| 182: | $res = [ |
| 183: | 'id' => $this->name, |
| 184: | 'loader' => ['selector' => '.ui.loader', 'trigger' => 'active'], |
| 185: | 'modal' => $this->closeModal, |
| 186: | 'warning' => ['selector' => $this->warningSelector, 'trigger' => $this->warningTrigger], |
| 187: | 'visible' => 'atk-visible', |
| 188: | 'closeSelector' => $this->closeSelector, |
| 189: | 'hasClickAway' => $this->hasClickAway, |
| 190: | 'hasEscAway' => $this->hasEscAway, |
| 191: | ]; |
| 192: | |
| 193: | if ($this->dynamicContent) { |
| 194: | $res['url'] = $this->getDynamicContent()->getCallbackUrl(); |
| 195: | $res['clearable'] = $this->getDynamicContent()->getClearSelector(); |
| 196: | } |
| 197: | |
| 198: | return $res; |
| 199: | } |
| 200: | |
| 201: | #[\Override] |
| 202: | protected function renderView(): void |
| 203: | { |
| 204: | $this->template->trySet('WarningIcon', $this->warningIcon); |
| 205: | $this->template->trySet('CloseIcon', $this->closeIcon); |
| 206: | |
| 207: | parent::renderView(); |
| 208: | |
| 209: | $this->js(true, $this->service()->addPanel($this->getPanelOptions())); |
| 210: | } |
| 211: | } |
| 212: | |