|
19 | 19 |
|
20 | 20 | trait TAjaxSelect |
21 | 21 | { |
22 | | - /** @var callable */ |
23 | | - private $callback; |
24 | | - |
25 | | - public function getControl() : \Nette\Utils\Html |
26 | | - { |
27 | | - if (\count($this->items) === 0) { |
28 | | - if (!\in_array($this->value, [null, '', []], true)) { |
29 | | - $this->items = $this->getData('', $this->value); |
30 | | - } |
31 | | - else { |
32 | | - $this->items = $this->getData(); |
33 | | - } |
34 | | - } |
| 22 | + /** @var callable */ |
| 23 | + private $callback; |
35 | 24 |
|
36 | | - $attrs = []; |
37 | | - $control = parent::getControl(); |
| 25 | + public function setCallback(callable $callback): self |
| 26 | + { |
| 27 | + $this->callback = $callback; |
| 28 | + return $this; |
| 29 | + } |
38 | 30 |
|
39 | | - $attrs['data-ajaxselect'] = $this->getForm()->getPresenter()->link( |
40 | | - $this->lookupPath(Presenter::class) . IComponent::NAME_SEPARATOR . self::SIGNAL_NAME . '!' |
| 31 | + public function getControl(): \Nette\Utils\Html |
| 32 | + { |
| 33 | + $this->initiateItems(); |
| 34 | + |
| 35 | + $attrs = []; |
| 36 | + $control = parent::getControl(); |
| 37 | + |
| 38 | + $attrs['data-ajaxselect'] = $this->getForm()->getPresenter()->link( |
| 39 | + $this->lookupPath(Presenter::class) . IComponent::NAME_SEPARATOR . self::SIGNAL_NAME . '!' |
41 | 40 | ); |
42 | 41 |
|
43 | | - $control->addAttributes($attrs); |
44 | | - return $control; |
45 | | - } |
46 | | - |
47 | | - /** |
48 | | - * @param string $query |
49 | | - * @param array|int $default |
50 | | - * @return array |
51 | | - */ |
52 | | - private function getData(string $query = '', $default = null) : array |
53 | | - { |
54 | | - if ($this->callback === null) { |
55 | | - throw new \Nette\InvalidStateException('Callback for "' . $this->getHtmlId() . '" is not set.'); |
56 | | - } |
57 | | - |
58 | | - $data = \call_user_func($this->callback, $query, $default); |
59 | | - |
60 | | - if (!\is_array($data)) { |
61 | | - throw new \Nette\InvalidStateException('Callback for "' . $this->getHtmlId() . '" must return array.'); |
62 | | - } |
63 | | - |
64 | | - return $data; |
65 | | - } |
66 | | - |
67 | | - /** |
68 | | - * @param mixed $value |
69 | | - */ |
70 | | - public function setValue($value) : void |
| 42 | + $control->addAttributes($attrs); |
| 43 | + return $control; |
| 44 | + } |
| 45 | + |
| 46 | + public function setValue($value): void |
71 | 47 | { |
72 | | - if (!\array_key_exists($value, $this->items)) { |
73 | | - if (!\in_array($value, [null, '', []], true)) { |
74 | | - $this->items = $this->getData('', $value); |
75 | | - } |
76 | | - else if (!\in_array($this->value, [null, '', []], true)) { |
77 | | - $this->items = $this->getData('', $this->value); |
78 | | - } |
79 | | - else { |
80 | | - $this->items = $this->getData(); |
81 | | - } |
82 | | - } |
| 48 | + $this->initiateItems($value); |
83 | 49 |
|
84 | 50 | parent::setValue($value); |
85 | 51 | } |
86 | 52 |
|
87 | | - public function setCallback(callable $callback) : self |
88 | | - { |
89 | | - $this->callback = $callback; |
90 | | - return $this; |
91 | | - } |
| 53 | + public function getValue() |
| 54 | + { |
| 55 | + $this->initiateItems(); |
| 56 | + |
| 57 | + return \array_key_exists($this->value, $this->items) ? $this->value : null; |
| 58 | + } |
92 | 59 |
|
93 | | - /** |
94 | | - * @param string $signal |
95 | | - */ |
96 | | - public function signalReceived($signal) : void |
| 60 | + public function signalReceived(string $signal): void |
97 | 61 | { |
98 | 62 | $presenter = $this->lookup(Presenter::class); |
99 | 63 |
|
100 | 64 | if ($signal !== self::SIGNAL_NAME || !$presenter->isAjax() || $this->isDisabled()) { |
101 | 65 | return; |
102 | 66 | } |
103 | 67 |
|
104 | | - $query = $presenter->getParameter('q'); |
| 68 | + $presenter->sendJson($this->getData($presenter->getParameter('q'))); |
| 69 | + } |
105 | 70 |
|
106 | | - $data = $this->getData($query); |
| 71 | + private function getData(string $query = '', $default = null): array |
| 72 | + { |
| 73 | + if ($this->callback === null) { |
| 74 | + throw new \Nette\InvalidStateException('Callback for "' . $this->getHtmlId() . '" is not set.'); |
| 75 | + } |
| 76 | + |
| 77 | + $data = \call_user_func($this->callback, $query, $default); |
| 78 | + |
| 79 | + if (!\is_array($data)) { |
| 80 | + throw new \Nette\InvalidStateException('Callback for "' . $this->getHtmlId() . '" must return array.'); |
| 81 | + } |
| 82 | + |
| 83 | + return $data; |
| 84 | + } |
| 85 | + |
| 86 | + private function initiateItems($value = null): void |
| 87 | + { |
| 88 | + if (\count($this->items) > 0) { |
| 89 | + return; |
| 90 | + } |
107 | 91 |
|
108 | | - $presenter->sendJson($data); |
| 92 | + if (!\in_array($value ?? $this->value, [null, '', []], true)) { |
| 93 | + $this->items = $this->getData('', $value ?? $this->value); |
| 94 | + } else { |
| 95 | + $this->items = $this->getData(); |
| 96 | + } |
109 | 97 | } |
110 | 98 | } |
0 commit comments