| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui; |
| 6: | |
| 7: | use Atk4\Ui\Js\JsChain; |
| 8: | use Atk4\Ui\Js\JsExpressionable; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | class Modal extends View |
| 32: | { |
| 33: | public $ui = 'modal'; |
| 34: | public $defaultTemplate = 'modal.html'; |
| 35: | |
| 36: | |
| 37: | public $title; |
| 38: | |
| 39: | public $loadingLabel = 'Loading...'; |
| 40: | |
| 41: | public $headerCss = 'header'; |
| 42: | |
| 43: | public $fx; |
| 44: | |
| 45: | public $cb; |
| 46: | |
| 47: | public $cbView; |
| 48: | |
| 49: | public $args = []; |
| 50: | |
| 51: | public $options = []; |
| 52: | |
| 53: | |
| 54: | public $type = 'json'; |
| 55: | |
| 56: | |
| 57: | public $contentCss = ['img', 'content', 'atk-dialog-content']; |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | public $showActions = false; |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | #[\Override] |
| 73: | public function set($fx = null) |
| 74: | { |
| 75: | if (!$fx instanceof \Closure) { |
| 76: | throw new \TypeError('$fx must be of type Closure'); |
| 77: | } |
| 78: | |
| 79: | $this->fx = $fx; |
| 80: | $this->enableCallback(); |
| 81: | |
| 82: | return $this; |
| 83: | } |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | public function enableCallback(): void |
| 92: | { |
| 93: | $this->cbView = View::addTo($this); |
| 94: | $this->cbView->stickyGet('__atk_m', $this->name); |
| 95: | if (!$this->cb) { |
| 96: | $this->cb = CallbackLater::addTo($this->cbView); |
| 97: | } |
| 98: | |
| 99: | $this->cb->set(function () { |
| 100: | ($this->fx)($this->cbView); |
| 101: | $this->cb->terminateJson($this->cbView); |
| 102: | }); |
| 103: | } |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | public function addContentCss($class): void |
| 111: | { |
| 112: | $this->contentCss = array_merge($this->contentCss, is_string($class) ? [$class] : $class); |
| 113: | } |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | public function jsShow(array $args = []): JsExpressionable |
| 123: | { |
| 124: | $chain = $this->js(); |
| 125: | if ($args !== []) { |
| 126: | $chain->data(['args' => $args]); |
| 127: | } |
| 128: | |
| 129: | return $chain->modal('show'); |
| 130: | } |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | public function jsHide(): JsExpressionable |
| 138: | { |
| 139: | return $this->js()->modal('hide'); |
| 140: | } |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | public function setOption($option, $value) |
| 151: | { |
| 152: | $this->options[$option] = $value; |
| 153: | |
| 154: | return $this; |
| 155: | } |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | public function addScrolling() |
| 163: | { |
| 164: | $this->addContentCss('scrolling'); |
| 165: | |
| 166: | return $this; |
| 167: | } |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | public function addDenyAction($label, JsExpressionable $jsAction) |
| 178: | { |
| 179: | $button = new Button(); |
| 180: | $button->set($label)->addClass('red cancel'); |
| 181: | $this->addButtonAction($button); |
| 182: | $this->options['onDeny'] = $jsAction; |
| 183: | |
| 184: | return $this; |
| 185: | } |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | public function addApproveAction($label, JsExpressionable $jsAction) |
| 196: | { |
| 197: | $b = new Button(); |
| 198: | $b->set($label)->addClass('green ok'); |
| 199: | $this->addButtonAction($b); |
| 200: | $this->options['onApprove'] = $jsAction; |
| 201: | |
| 202: | return $this; |
| 203: | } |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | public function addButtonAction($button) |
| 213: | { |
| 214: | $this->add($button, 'actions'); |
| 215: | $this->showActions = true; |
| 216: | |
| 217: | return $this; |
| 218: | } |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | public function notClosable() |
| 226: | { |
| 227: | $this->options['closable'] = false; |
| 228: | |
| 229: | return $this; |
| 230: | } |
| 231: | |
| 232: | #[\Override] |
| 233: | protected function renderView(): void |
| 234: | { |
| 235: | $data = []; |
| 236: | $data['type'] = $this->type; |
| 237: | $data['loadingLabel'] = $this->loadingLabel; |
| 238: | |
| 239: | if ($this->title) { |
| 240: | $this->template->trySet('title', $this->title); |
| 241: | $this->template->trySet('headerCss', $this->headerCss); |
| 242: | } else { |
| 243: | |
| 244: | |
| 245: | $this->template->loadFromString(preg_replace('~<div class="\{\$headerCss\}">\{\$title\}</div>\s*~', '', $this->template->toLoadableString(), 1)); |
| 246: | } |
| 247: | |
| 248: | if ($this->contentCss) { |
| 249: | $this->template->trySet('contentCss', implode(' ', $this->contentCss)); |
| 250: | } |
| 251: | |
| 252: | if ($this->fx !== null) { |
| 253: | $data['url'] = $this->cb->getJsUrl(); |
| 254: | } |
| 255: | |
| 256: | if (!$this->showActions) { |
| 257: | $this->template->del('ActionContainer'); |
| 258: | } |
| 259: | |
| 260: | $this->js(true)->modal($this->options); |
| 261: | |
| 262: | if (!isset($this->options['closable']) || $this->options['closable']) { |
| 263: | $this->template->trySet('closeIcon', 'close'); |
| 264: | } else { |
| 265: | |
| 266: | |
| 267: | $this->template->loadFromString(preg_replace('~<i class="\{\$closeIcon\} icon"></i>~', '', $this->template->toLoadableString(), 1)); |
| 268: | } |
| 269: | |
| 270: | if ($this->args) { |
| 271: | $data['args'] = $this->args; |
| 272: | } |
| 273: | $this->js(true)->data($data); |
| 274: | |
| 275: | parent::renderView(); |
| 276: | } |
| 277: | } |
| 278: | |