Skip to content

Commit 10ca286

Browse files
committed
Support for disabled and title attributes
1 parent 8d37ee6 commit 10ca286

File tree

3 files changed

+144
-16
lines changed

3 files changed

+144
-16
lines changed

src/Form/ResultObject.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Nepttune (https://www.peldax.com)
5+
*
6+
* Copyright (c) 2018 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+
class ResultObject implements \JsonSerializable
18+
{
19+
private int $id;
20+
private string $text;
21+
private ?string $title = null;
22+
private ?bool $disabled = null;
23+
24+
public function __construct(int $id, string $text, ?string $title = null, ?bool $disabled = null)
25+
{
26+
$this->id = $id;
27+
$this->text = $text;
28+
$this->title = $title;
29+
$this->disabled = $disabled;
30+
}
31+
32+
public function getId() : int
33+
{
34+
return $this->id;
35+
}
36+
37+
public function getText() : string
38+
{
39+
return $this->text;
40+
}
41+
42+
public function jsonSerialize()
43+
{
44+
$toReturn = [
45+
'id' => $this->id,
46+
'text' => $this->text,
47+
];
48+
49+
if ($this->title !== null) {
50+
$toReturn['title'] = $this->title;
51+
}
52+
53+
if ($this->disabled !== null) {
54+
$toReturn['disabled'] = $this->disabled;
55+
}
56+
57+
return $toReturn;
58+
}
59+
}

src/Form/ResultObjectSet.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Nepttune (https://www.peldax.com)
5+
*
6+
* Copyright (c) 2018 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+
class ResultObjectSet extends \Infinityloop\Utils\ObjectSet implements \JsonSerializable
18+
{
19+
protected const INNER_CLASS = ResultObject::class;
20+
21+
public static function fromArray(array $data) : \Nepttune\Form\ResultObjectSet
22+
{
23+
$objectSet = [];
24+
25+
foreach ($data as $key => $value) {
26+
if ($value instanceof \Nepttune\Form\ResultObject) {
27+
$objectSet[] = $value;
28+
29+
continue;
30+
}
31+
32+
$objectSet[] = new \Nepttune\Form\ResultObject($key, $value);
33+
}
34+
35+
return new self($objectSet);
36+
}
37+
38+
public function jsonSerialize()
39+
{
40+
$toReturn = [];
41+
42+
foreach ($this as $object) {
43+
$toReturn[] = $object->jsonSerialize();
44+
}
45+
46+
return $toReturn;
47+
}
48+
49+
public function getRawData() : array
50+
{
51+
$toReturn = [];
52+
53+
foreach ($this as $object) {
54+
$toReturn[$object->getId()] = $object->getText();
55+
}
56+
57+
return $toReturn;
58+
}
59+
60+
public function current() : \Nepttune\Form\ResultObject
61+
{
62+
return parent::current();
63+
}
64+
65+
public function offsetGet($offset) : \Nepttune\Form\ResultObject
66+
{
67+
return parent::offsetGet($offset);
68+
}
69+
}

src/Form/TAjaxSelect.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ trait TAjaxSelect
2828
/** @var \Nette\Caching\Cache */
2929
private $storage;
3030

31+
/** @var array */
32+
private $optionsRaw;
33+
3134
public function setCallback(callable $callback): self
3235
{
3336
$this->callback = $callback;
@@ -86,17 +89,7 @@ public function signalReceived(string $signal): void
8689

8790
switch ($signal) {
8891
case self::CALLBACK_SIGNAL_NAME:
89-
$data = $this->getData($presenter->getParameter('q'));
90-
$toJson = [];
91-
92-
foreach ($data as $key => $value) {
93-
$toJson[] = [
94-
'id' => $key,
95-
'text' => $value,
96-
];
97-
}
98-
99-
$presenter->sendJson($toJson);
92+
$presenter->sendJson($this->getData($presenter->getParameter('q')));
10093
break;
10194
case self::ONCHANGE_SIGNAL_NAME:
10295
$this->fireOnchange($presenter->getParameter('s'));
@@ -107,9 +100,9 @@ public function signalReceived(string $signal): void
107100
/**
108101
* @param string $query
109102
* @param array|int|null $default
110-
* @return array
103+
* @return \Nepttune\Form\ResultObjectSet
111104
*/
112-
private function getData(string $query = '', $default = null): array
105+
private function getData(string $query = '', $default = null) : \Nepttune\Form\ResultObjectSet
113106
{
114107
if ($this->callback === null) {
115108
throw new \Nette\InvalidStateException('Callback for "' . $this->getHtmlId() . '" is not set.');
@@ -126,7 +119,11 @@ private function getData(string $query = '', $default = null): array
126119

127120
$data = \call_user_func($this->callback, $query, $default);
128121

129-
if (!\is_array($data)) {
122+
if (\is_array($data)) {
123+
$data = \Nepttune\Form\ResultObjectSet::fromArray($data);
124+
}
125+
126+
if (!$data instanceof \Nepttune\Form\ResultObjectSet) {
130127
throw new \Nette\InvalidStateException('Callback for "' . $this->getHtmlId() . '" must return array.');
131128
}
132129

@@ -140,16 +137,19 @@ private function getData(string $query = '', $default = null): array
140137
private function initiateItems($value = null): void
141138
{
142139
$value = $value ?? $this->value;
140+
$data = [];
143141

144142
if (\in_array($value, [null, '', []], true)) {
145143
if (\count($this->items) > 0) {
146144
return;
147145
}
148146

149-
parent::setItems($this->getData());
147+
$data = $this->getData();
150148
} else {
151-
parent::setItems($this->getData('', $value));
149+
$data = $this->getData('', $value);
152150
}
151+
152+
parent::setItems($data->getRawData());
153153
}
154154

155155
private function fireOnchange($selected = null) : void

0 commit comments

Comments
 (0)