Skip to content

Commit 65e2591

Browse files
committed
GridComponent: refactoring
1 parent a45957f commit 65e2591

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

src/UI/Component/GridComponent.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Utilitte\Nette\UI\Component;
44

5+
use InvalidArgumentException;
56
use Nette\Application\UI\Component;
67
use Nette\Application\UI\Control;
78
use Nette\Application\UI\IRenderable;
9+
use Nette\Application\UI\Multiplier;
810
use Nette\Bridges\ApplicationLatte\Template;
911
use Nette\Utils\Html;
1012
use function assert;
@@ -28,17 +30,30 @@ final class GridComponent extends Control
2830

2931
private int $columnNumber = 1;
3032

33+
/** @var mixed[] */
34+
private array $prepends = [];
35+
36+
/** @var Component[] */
37+
private array $prependsById = [];
38+
3139
public function __construct(Component $component)
3240
{
3341
$this->component = $component;
3442
}
3543

44+
public static function create(Component $component): self
45+
{
46+
return new static($component);
47+
}
48+
3649
/**
3750
* @phpstan-param Html<int, Html|string>|null $row
3851
*/
39-
public function setRow(?Html $row): void
52+
public function setRow(?Html $row): self
4053
{
4154
$this->row = $row;
55+
56+
return $this;
4257
}
4358

4459
/**
@@ -65,6 +80,14 @@ public function setIncrementSequence(string $prefix): self
6580
return $this;
6681
}
6782

83+
public function addComponentAtPosition(string $id, int $position, Component $control): self
84+
{
85+
$this->prependsById[$id] = $control;
86+
$this->prepends[$position][] = $id;
87+
88+
return $this;
89+
}
90+
6891
public function render(): void
6992
{
7093
$component = $this['grid'];
@@ -86,6 +109,7 @@ public function render(): void
86109
}
87110

88111
$col = $this->column;
112+
89113
if ($this->incrementSequencePrefix) {
90114
$col = clone $this->column;
91115
$col->appendAttribute('class', $this->incrementSequencePrefix . $counter);
@@ -117,6 +141,7 @@ public function render(): void
117141
return $this->row->endTag();
118142
});
119143

144+
$controls = $this->mergePrepends($controls);
120145

121146
$template->render(__DIR__ . '/templates/grid.latte', [
122147
'controls' => $controls,
@@ -130,4 +155,44 @@ protected function createComponentGrid(): Component
130155
return $this->component;
131156
}
132157

158+
protected function createComponentPrepend(): Component
159+
{
160+
return new Multiplier(function (string $index) {
161+
if (!isset($this->prependsById[$index])) {
162+
throw new InvalidArgumentException(sprintf('Prepended component %d not exists', $index));
163+
}
164+
165+
return $this->prependsById[$index];
166+
});
167+
}
168+
169+
/**
170+
* @param Component[] $controls
171+
* @return Component[]
172+
*/
173+
private function mergePrepends(array $controls): array
174+
{
175+
if (!$this->prepends) {
176+
return $controls;
177+
}
178+
179+
$return = [];
180+
181+
$position = 1;
182+
183+
foreach ($controls as $control) {
184+
if (isset($this->prepends[$position])) {
185+
foreach ($this->prepends[$position] as $id) {
186+
$return[] = $this->getComponent(sprintf('prepend-%s', $id));
187+
}
188+
}
189+
190+
$return[] = $control;
191+
192+
$position++;
193+
}
194+
195+
return $return;
196+
}
197+
133198
}

src/UI/Component/templates/grid.latte

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
21
{foreach $controls as $ctrl}
2+
{var $counter = $iterator->getCounter()}
3+
4+
{* Row *}
35
{if $calculate && $iterator->isFirst()}
46
{startRow()|noescape}
57
{/if}
68

7-
{startColumn($iterator->getCounter())|noescape}
9+
{* Column *}
10+
{startColumn($counter)|noescape}
811
{control $ctrl}
912
{endColumn()|noescape}
1013

14+
{* Row *}
1115
{if $calculate}
1216
{if $iterator->isLast()}
1317
{endRow()|noescape}
14-
{elseif $iterator->getCounter() % $number === 0}
18+
{elseif $counter % $number === 0}
1519
{endRow()|noescape}
1620
{startRow()|noescape}
1721
{/if}

0 commit comments

Comments
 (0)