|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of Nepttune (https://www.peldax.com) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Václav Pelíšek (info@peldax.com) |
| 7 | + * |
| 8 | + * This software consists of voluntary contributions made by many individuals |
| 9 | + * and is licensed under the MIT license. For more information, see |
| 10 | + * <https://www.peldax.com>. |
| 11 | + */ |
| 12 | + |
| 13 | +declare(strict_types = 1); |
| 14 | + |
| 15 | +namespace Nepttune\Form; |
| 16 | + |
| 17 | +use \Nette\Application\UI\Presenter; |
| 18 | +use \Nette\ComponentModel\IComponent; |
| 19 | + |
| 20 | +trait TAjaxSelect |
| 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 | + } |
| 35 | + |
| 36 | + $attrs = []; |
| 37 | + $control = parent::getControl(); |
| 38 | + |
| 39 | + $attrs['data-ajaxselect'] = $this->getForm()->getPresenter()->link( |
| 40 | + $this->lookupPath(Presenter::class) . IComponent::NAME_SEPARATOR . self::SIGNAL_NAME . '!' |
| 41 | + ); |
| 42 | + |
| 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 |
| 71 | + { |
| 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 | + } |
| 83 | + |
| 84 | + parent::setValue($value); |
| 85 | + } |
| 86 | + |
| 87 | + public function setCallback(callable $callback) : self |
| 88 | + { |
| 89 | + $this->callback = $callback; |
| 90 | + return $this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param string $signal |
| 95 | + */ |
| 96 | + public function signalReceived($signal) : void |
| 97 | + { |
| 98 | + $presenter = $this->lookup(Presenter::class); |
| 99 | + |
| 100 | + if ($signal !== self::SIGNAL_NAME || !$presenter->isAjax() || $this->isDisabled()) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + $query = $presenter->getParameter('q'); |
| 105 | + |
| 106 | + $data = $this->getData($query); |
| 107 | + |
| 108 | + $presenter->sendJson($data); |
| 109 | + } |
| 110 | +} |
0 commit comments