Skip to content

Commit 2a26523

Browse files
committed
feat: load layout from view using selector
1 parent 7f17830 commit 2a26523

File tree

2 files changed

+91
-11
lines changed

2 files changed

+91
-11
lines changed

src/Html/Layout.php

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,44 @@ public function topStart(string|array|null $options, int $order = 0): static
2121
return $this->top($options, $order, 'Start');
2222
}
2323

24+
public function top(array|string|null $options, ?int $order = null, ?string $position = null): static
25+
{
26+
if ($order > 0) {
27+
$this->attributes["top{$order}{$position}"] = $options;
28+
} else {
29+
$this->attributes["top{$position}"] = $options;
30+
}
31+
32+
return $this;
33+
}
34+
2435
public function topEnd(string|array|null $options, int $order = 0): static
2536
{
2637
return $this->top($options, $order, 'End');
2738
}
2839

29-
public function bottomStart(string|array|null $options, int $order = 0): static
40+
public function topEndView(string $selector, int $order = 0): static
3041
{
31-
return $this->bottom($options, $order, 'Start');
42+
return $this->topView($selector, $order, 'End');
3243
}
3344

34-
public function bottomEnd(string|array|null $options, int $order = 0): static
45+
public function topView(string $selector, int $order = 0, ?string $position = null): static
3546
{
36-
return $this->bottom($options, $order, 'End');
47+
$script = "function() { return $('{$selector}').html(); }";
48+
49+
return $this->top($script, $order, $position);
3750
}
3851

39-
public function top(array|string|null $options, ?int $order = null, ?string $position = null): static
52+
public function bottomStartView(string $selector, int $order = 0): static
4053
{
41-
if ($order > 0) {
42-
$this->attributes["top{$order}{$position}"] = $options;
43-
} else {
44-
$this->attributes["top{$position}"] = $options;
45-
}
54+
return $this->bottomView($selector, $order, 'Start');
55+
}
4656

47-
return $this;
57+
public function bottomView(string $selector, int $order = 0, ?string $position = null): static
58+
{
59+
$script = "function() { return $('{$selector}').html(); }";
60+
61+
return $this->bottom($script, $order, $position);
4862
}
4963

5064
public function bottom(array|string|null $options, ?int $order = null, ?string $position = null): static
@@ -57,4 +71,24 @@ public function bottom(array|string|null $options, ?int $order = null, ?string $
5771

5872
return $this;
5973
}
74+
75+
public function bottomEndView(string $selector, int $order = 0): static
76+
{
77+
return $this->bottomView($selector, $order, 'End');
78+
}
79+
80+
public function topStartView(string $selector, int $order = 0): static
81+
{
82+
return $this->topView($selector, $order, 'Start');
83+
}
84+
85+
public function bottomStart(string|array|null $options, int $order = 0): static
86+
{
87+
return $this->bottom($options, $order, 'Start');
88+
}
89+
90+
public function bottomEnd(string|array|null $options, int $order = 0): static
91+
{
92+
return $this->bottom($options, $order, 'End');
93+
}
6094
}

tests/LayoutTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,50 @@ public function it_can_accept_array_as_parameter_in_builder(): void
127127
$this->assertArrayHasKey('layout', $builder->getAttributes());
128128
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
129129
}
130+
131+
#[Test]
132+
public function it_can_accept_callable_as_parameter_in_builder(): void
133+
{
134+
$builder = resolve(Builder::class);
135+
$builder->layout(fn (Layout $layout) => $layout->top('test'));
136+
137+
$this->assertArrayHasKey('layout', $builder->getAttributes());
138+
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
139+
}
140+
141+
#[Test]
142+
public function it_can_accept_js_selector_for_layout_content(): void
143+
{
144+
$builder = resolve(Builder::class);
145+
$builder->layout(fn (Layout $layout) => $layout->topView('#test'));
146+
147+
$this->assertArrayHasKey('layout', $builder->getAttributes());
148+
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
149+
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['top']);
150+
151+
$builder->layout(fn (Layout $layout) => $layout->bottomView('#test'));
152+
$this->assertArrayHasKey('layout', $builder->getAttributes());
153+
$this->assertArrayHasKey('bottom', $builder->getAttributes()['layout']);
154+
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['bottom']);
155+
156+
$builder->layout(fn (Layout $layout) => $layout->topStartView('#test'));
157+
$this->assertArrayHasKey('layout', $builder->getAttributes());
158+
$this->assertArrayHasKey('topStart', $builder->getAttributes()['layout']);
159+
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['topStart']);
160+
161+
$builder->layout(fn (Layout $layout) => $layout->topEndView('#test'));
162+
$this->assertArrayHasKey('layout', $builder->getAttributes());
163+
$this->assertArrayHasKey('topEnd', $builder->getAttributes()['layout']);
164+
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['topEnd']);
165+
166+
$builder->layout(fn (Layout $layout) => $layout->bottomStartView('#test'));
167+
$this->assertArrayHasKey('layout', $builder->getAttributes());
168+
$this->assertArrayHasKey('bottomStart', $builder->getAttributes()['layout']);
169+
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['bottomStart']);
170+
171+
$builder->layout(fn (Layout $layout) => $layout->bottomEndView('#test'));
172+
$this->assertArrayHasKey('layout', $builder->getAttributes());
173+
$this->assertArrayHasKey('bottomEnd', $builder->getAttributes()['layout']);
174+
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['bottomEnd']);
175+
}
130176
}

0 commit comments

Comments
 (0)