| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui\Form\Control; |
| 6: | |
| 7: | use Atk4\Ui\Form; |
| 8: | use Atk4\Ui\HtmlTemplate; |
| 9: | use Atk4\Ui\Js\Jquery; |
| 10: | use Atk4\Ui\Js\JsExpressionable; |
| 11: | use Atk4\Ui\JsCallback; |
| 12: | use Atk4\Ui\View; |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | class TreeItemSelector extends Form\Control |
| 27: | { |
| 28: | |
| 29: | public $itemSelectorTemplate; |
| 30: | |
| 31: | |
| 32: | public $itemSelector; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | public $loaderCssName = 'atk-tree-loader'; |
| 41: | |
| 42: | |
| 43: | public $allowMultiple = true; |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | public $treeItems = []; |
| 69: | |
| 70: | |
| 71: | private $cb; |
| 72: | |
| 73: | #[\Override] |
| 74: | protected function init(): void |
| 75: | { |
| 76: | parent::init(); |
| 77: | |
| 78: | $this->addClass(['ui', 'vertical', 'segment', 'basic', $this->loaderCssName])->setStyle(['padding' => '0px!important']); |
| 79: | |
| 80: | if (!$this->itemSelectorTemplate) { |
| 81: | $this->itemSelectorTemplate = new HtmlTemplate('<div class="ui list" style="margin-left: 16px;" {$attributes}><atk-tree-item-selector v-bind="initData"></atk-tree-item-selector><div class="ui hidden divider"></div>{$Input}</div>'); |
| 82: | } |
| 83: | |
| 84: | $this->itemSelector = View::addTo($this, ['template' => $this->itemSelectorTemplate]); |
| 85: | } |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | public function onItem(\Closure $fx): void |
| 95: | { |
| 96: | $this->cb = JsCallback::addTo($this)->set(function (Jquery $j, $data) use ($fx) { |
| 97: | $value = $this->getApp()->decodeJson($data); |
| 98: | if (!$this->allowMultiple) { |
| 99: | $value = $value[0]; |
| 100: | } |
| 101: | |
| 102: | return $fx($value); |
| 103: | }, ['data' => 'value']); |
| 104: | } |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | public function getInput() |
| 112: | { |
| 113: | return $this->getApp()->getTag('input/', [ |
| 114: | 'name' => $this->shortName, |
| 115: | 'type' => 'hidden', |
| 116: | 'value' => $this->getValue(), |
| 117: | ]); |
| 118: | } |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | public function getValue() |
| 124: | { |
| 125: | return $this->getApp()->uiPersistence->typecastSaveField($this->entityField->getField(), $this->entityField->get()); |
| 126: | } |
| 127: | |
| 128: | #[\Override] |
| 129: | protected function renderView(): void |
| 130: | { |
| 131: | parent::renderView(); |
| 132: | |
| 133: | $this->itemSelector->template->tryDangerouslySetHtml('Input', $this->getInput()); |
| 134: | |
| 135: | $this->itemSelector->vue('AtkTreeItemSelector', [ |
| 136: | 'item' => ['id' => 'atk-root', 'nodes' => $this->treeItems], |
| 137: | 'values' => [], |
| 138: | 'field' => $this->shortName, |
| 139: | 'options' => [ |
| 140: | 'mode' => $this->allowMultiple ? 'multiple' : 'single', |
| 141: | 'url' => $this->cb ? $this->cb->getJsUrl() : null, |
| 142: | 'loader' => $this->loaderCssName, |
| 143: | ], |
| 144: | ]); |
| 145: | } |
| 146: | } |
| 147: | |