| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui\Form\Control; |
| 6: | |
| 7: | use Atk4\Ui\HtmlTemplate; |
| 8: | use Atk4\Ui\Js\Jquery; |
| 9: | use Atk4\Ui\Js\JsExpression; |
| 10: | use Atk4\Ui\Js\JsExpressionable; |
| 11: | use Atk4\Ui\Js\JsFunction; |
| 12: | |
| 13: | class Dropdown extends Input |
| 14: | { |
| 15: | public $defaultTemplate = 'form/control/dropdown.html'; |
| 16: | |
| 17: | public string $inputType = 'hidden'; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | public array $values; |
| 32: | |
| 33: | |
| 34: | public $empty = "\u{00a0}"; |
| 35: | |
| 36: | |
| 37: | public $dropdownOptions = []; |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | public $multiple = false; |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public $renderRowFunction; |
| 93: | |
| 94: | |
| 95: | protected $_tItem; |
| 96: | |
| 97: | |
| 98: | protected $_tIcon; |
| 99: | |
| 100: | #[\Override] |
| 101: | protected function init(): void |
| 102: | { |
| 103: | parent::init(); |
| 104: | |
| 105: | $this->_tItem = $this->template->cloneRegion('Item'); |
| 106: | $this->template->del('Item'); |
| 107: | $this->_tIcon = $this->_tItem->cloneRegion('Icon'); |
| 108: | $this->_tItem->del('Icon'); |
| 109: | } |
| 110: | |
| 111: | #[\Override] |
| 112: | public function getValue() |
| 113: | { |
| 114: | |
| 115: | return $this->entityField !== null |
| 116: | ? (is_array($this->entityField->get()) ? implode(', ', $this->entityField->get()) : $this->entityField->get()) |
| 117: | : parent::getValue(); |
| 118: | } |
| 119: | |
| 120: | #[\Override] |
| 121: | public function set($value = null) |
| 122: | { |
| 123: | if ($this->entityField) { |
| 124: | if ($this->entityField->getField()->type === 'json' && is_string($value)) { |
| 125: | $value = explode(',', $value); |
| 126: | } |
| 127: | $this->entityField->set($value); |
| 128: | |
| 129: | return $this; |
| 130: | } |
| 131: | |
| 132: | return parent::set($value); |
| 133: | } |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | public function setDropdownOption($option, $value): void |
| 142: | { |
| 143: | $this->dropdownOptions[$option] = $value; |
| 144: | } |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | public function setDropdownOptions($options): void |
| 152: | { |
| 153: | $this->dropdownOptions = array_merge($this->dropdownOptions, $options); |
| 154: | } |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | protected function jsDropdown($when = false, $action = null): JsExpressionable |
| 163: | { |
| 164: | return $this->js($when, $action, 'div.ui.dropdown:has(> #' . $this->name . '_input)'); |
| 165: | } |
| 166: | |
| 167: | protected function jsRenderDropdown(): JsExpressionable |
| 168: | { |
| 169: | return $this->jsDropdown(true)->dropdown($this->dropdownOptions); |
| 170: | } |
| 171: | |
| 172: | protected function htmlRenderValue(): void |
| 173: | { |
| 174: | |
| 175: | if ($this->entityField !== null && !$this->entityField->getField()->required && !$this->multiple) { |
| 176: | $this->_tItem->set('value', ''); |
| 177: | $this->_tItem->set('title', $this->empty); |
| 178: | $this->template->dangerouslyAppendHtml('Item', $this->_tItem->renderToHtml()); |
| 179: | } |
| 180: | |
| 181: | |
| 182: | if ($this->model !== null) { |
| 183: | if ($this->renderRowFunction) { |
| 184: | foreach ($this->model as $row) { |
| 185: | $this->_addCallBackRow($row); |
| 186: | } |
| 187: | } else { |
| 188: | |
| 189: | $this->model->setOnlyFields([$this->model->titleField, $this->model->idField]); |
| 190: | $this->_renderItemsForModel(); |
| 191: | } |
| 192: | } else { |
| 193: | if ($this->renderRowFunction) { |
| 194: | foreach ($this->values as $key => $value) { |
| 195: | $this->_addCallBackRow($value, $key); |
| 196: | } |
| 197: | } else { |
| 198: | $this->_renderItemsForValues(); |
| 199: | } |
| 200: | } |
| 201: | } |
| 202: | |
| 203: | #[\Override] |
| 204: | protected function renderView(): void |
| 205: | { |
| 206: | if ($this->multiple) { |
| 207: | $this->template->dangerouslySetHtml('multipleClass', 'multiple'); |
| 208: | } |
| 209: | |
| 210: | if ($this->readOnly || $this->disabled) { |
| 211: | if ($this->multiple) { |
| 212: | $this->jsDropdown(true)->find('a i.delete.icon')->attr('class', 'disabled'); |
| 213: | } |
| 214: | } |
| 215: | |
| 216: | if ($this->disabled) { |
| 217: | $this->template->set('disabledClass', 'disabled'); |
| 218: | $this->template->dangerouslySetHtml('disabled', 'disabled="disabled"'); |
| 219: | } elseif ($this->readOnly) { |
| 220: | $this->template->set('disabledClass', 'read-only'); |
| 221: | $this->template->dangerouslySetHtml('disabled', 'readonly="readonly"'); |
| 222: | |
| 223: | $this->setDropdownOption('onShow', new JsFunction([], [new JsExpression('return false')])); |
| 224: | } |
| 225: | |
| 226: | $this->template->set('DefaultText', $this->empty); |
| 227: | |
| 228: | $this->htmlRenderValue(); |
| 229: | $this->jsRenderDropdown(); |
| 230: | |
| 231: | parent::renderView(); |
| 232: | } |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | protected function _renderItemsForModel(): void |
| 238: | { |
| 239: | foreach ($this->model as $key => $row) { |
| 240: | $title = $row->getTitle(); |
| 241: | $this->_tItem->set('value', (string) $key); |
| 242: | $this->_tItem->set('title', $title || is_numeric($title) ? (string) $title : ''); |
| 243: | |
| 244: | $this->template->dangerouslyAppendHtml('Item', $this->_tItem->renderToHtml()); |
| 245: | } |
| 246: | } |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: | protected function _renderItemsForValues(): void |
| 252: | { |
| 253: | foreach ($this->values as $key => $val) { |
| 254: | $this->_tItem->set('value', (string) $key); |
| 255: | if (is_array($val)) { |
| 256: | if (array_key_exists('icon', $val)) { |
| 257: | $this->_tIcon->set('iconClass', $val['icon'] . ' icon'); |
| 258: | $this->_tItem->dangerouslySetHtml('Icon', $this->_tIcon->renderToHtml()); |
| 259: | } else { |
| 260: | $this->_tItem->del('Icon'); |
| 261: | } |
| 262: | $this->_tItem->set('title', $val[0] || is_numeric($val[0]) ? (string) $val[0] : ''); |
| 263: | } else { |
| 264: | $this->_tItem->set('title', $val || is_numeric($val) ? (string) $val : ''); |
| 265: | } |
| 266: | |
| 267: | |
| 268: | $this->template->dangerouslyAppendHtml('Item', $this->_tItem->renderToHtml()); |
| 269: | } |
| 270: | } |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | protected function _addCallBackRow($row, $key = null): void |
| 280: | { |
| 281: | $res = ($this->renderRowFunction)($row, $key); |
| 282: | $this->_tItem->set('value', (string) $res['value']); |
| 283: | $this->_tItem->set('title', $res['title']); |
| 284: | |
| 285: | $this->_tItem->del('Icon'); |
| 286: | if (isset($res['icon']) && $res['icon']) { |
| 287: | |
| 288: | |
| 289: | $this->_tIcon->set('iconClass', 'icon ' . $res['icon']); |
| 290: | $this->_tItem->dangerouslyAppendHtml('Icon', $this->_tIcon->renderToHtml()); |
| 291: | } |
| 292: | |
| 293: | |
| 294: | $this->template->dangerouslyAppendHtml('Item', $this->_tItem->renderToHtml()); |
| 295: | } |
| 296: | } |
| 297: | |