Skip to content

Commit cf4cf01

Browse files
authored
Update TAjaxSelect.php
1 parent b5587ca commit cf4cf01

File tree

1 file changed

+56
-68
lines changed

1 file changed

+56
-68
lines changed

src/Form/TAjaxSelect.php

Lines changed: 56 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,92 +19,80 @@
1919

2020
trait TAjaxSelect
2121
{
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;
3524

36-
$attrs = [];
37-
$control = parent::getControl();
25+
public function setCallback(callable $callback): self
26+
{
27+
$this->callback = $callback;
28+
return $this;
29+
}
3830

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 . '!'
4140
);
4241

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
7147
{
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);
8349

8450
parent::setValue($value);
8551
}
8652

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+
}
9259

93-
/**
94-
* @param string $signal
95-
*/
96-
public function signalReceived($signal) : void
60+
public function signalReceived(string $signal): void
9761
{
9862
$presenter = $this->lookup(Presenter::class);
9963

10064
if ($signal !== self::SIGNAL_NAME || !$presenter->isAjax() || $this->isDisabled()) {
10165
return;
10266
}
10367

104-
$query = $presenter->getParameter('q');
68+
$presenter->sendJson($this->getData($presenter->getParameter('q')));
69+
}
10570

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+
}
10791

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+
}
10997
}
11098
}

0 commit comments

Comments
 (0)