Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Html/Enums/LayoutPosition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Yajra\DataTables\Html\Enums;

enum LayoutPosition: string
{
case Start = 'Start';
case End = 'End';
}
67 changes: 51 additions & 16 deletions src/Html/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\Fluent;
use Illuminate\Support\Traits\Macroable;
use Yajra\DataTables\Html\Enums\LayoutPosition;

class Layout extends Fluent
{
Expand All @@ -18,43 +19,77 @@ public static function make(array $options = []): static

public function topStart(string|array|null $options, int $order = 0): static
{
return $this->top($options, $order, 'Start');
return $this->top($options, $order, LayoutPosition::Start);
}

public function top(array|string|null $options, ?int $order = null, ?LayoutPosition $position = null): static
{
if ($order > 0) {
$this->attributes["top{$order}{$position?->value}"] = $options;
} else {
$this->attributes["top{$position?->value}"] = $options;
}

return $this;
}

public function topEnd(string|array|null $options, int $order = 0): static
{
return $this->top($options, $order, 'End');
return $this->top($options, $order, LayoutPosition::End);
}

public function bottomStart(string|array|null $options, int $order = 0): static
public function topEndView(string $selector, int $order = 0): static
{
return $this->bottom($options, $order, 'Start');
return $this->topView($selector, $order, LayoutPosition::End);
}

public function bottomEnd(string|array|null $options, int $order = 0): static
public function topView(string $selector, int $order = 0, ?LayoutPosition $position = null): static
{
return $this->bottom($options, $order, 'End');
$script = "function() { return $('{$selector}').html(); }";

return $this->top($script, $order, $position);
}

public function top(array|string|null $options, ?int $order = null, ?string $position = null): static
public function bottomStartView(string $selector, int $order = 0): static
{
if ($order > 0) {
$this->attributes["top{$order}{$position}"] = $options;
} else {
$this->attributes["top{$position}"] = $options;
}
return $this->bottomView($selector, $order, LayoutPosition::Start);
}

return $this;
public function bottomView(string $selector, int $order = 0, ?LayoutPosition $position = null): static
{
$script = "function() { return $('{$selector}').html(); }";

return $this->bottom($script, $order, $position);
}

public function bottom(array|string|null $options, ?int $order = null, ?string $position = null): static
public function bottom(array|string|null $options, ?int $order = null, ?LayoutPosition $position = null): static
{
if ($order > 0) {
$this->attributes["bottom{$order}{$position}"] = $options;
$this->attributes["bottom{$order}{$position?->value}"] = $options;
} else {
$this->attributes["bottom{$position}"] = $options;
$this->attributes["bottom{$position?->value}"] = $options;
}

return $this;
}

public function bottomEndView(string $selector, int $order = 0): static
{
return $this->bottomView($selector, $order, LayoutPosition::End);
}

public function topStartView(string $selector, int $order = 0): static
{
return $this->topView($selector, $order, LayoutPosition::Start);
}

public function bottomStart(string|array|null $options, int $order = 0): static
{
return $this->bottom($options, $order, LayoutPosition::Start);
}

public function bottomEnd(string|array|null $options, int $order = 0): static
{
return $this->bottom($options, $order, LayoutPosition::End);
}
}
68 changes: 60 additions & 8 deletions tests/LayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\Attributes\Test;
use Yajra\DataTables\Html\Builder;
use Yajra\DataTables\Html\Enums\LayoutPosition;
use Yajra\DataTables\Html\Layout;

class LayoutTest extends TestCase
Expand Down Expand Up @@ -36,16 +37,16 @@ public function it_can_set_positions(): void
$layout->bottom('test', 1);
$this->assertEquals('test', $layout->get('bottom1'));

$layout->top('test', 1, 'Start');
$layout->top('test', 1, LayoutPosition::Start);
$this->assertEquals('test', $layout->get('top1Start'));

$layout->bottom('test', 1, 'Start');
$layout->bottom('test', 1, LayoutPosition::Start);
$this->assertEquals('test', $layout->get('bottom1Start'));

$layout->top('test', 1, 'End');
$layout->top('test', 1, LayoutPosition::End);
$this->assertEquals('test', $layout->get('top1End'));

$layout->bottom('test', 1, 'End');
$layout->bottom('test', 1, LayoutPosition::End);
$this->assertEquals('test', $layout->get('bottom1End'));
}

Expand All @@ -62,10 +63,10 @@ public function it_can_be_used_in_builder(): void
$layout->bottomEnd('test');
$layout->top('test', 1);
$layout->bottom('test', 1);
$layout->top('test', 1, 'Start');
$layout->bottom('test', 1, 'Start');
$layout->top('test', 1, 'End');
$layout->bottom('test', 1, 'End');
$layout->top('test', 1, LayoutPosition::Start);
$layout->bottom('test', 1, LayoutPosition::Start);
$layout->top('test', 1, LayoutPosition::End);
$layout->bottom('test', 1, LayoutPosition::End);
});

$this->assertArrayHasKey('layout', $builder->getAttributes());
Expand Down Expand Up @@ -127,4 +128,55 @@ public function it_can_accept_array_as_parameter_in_builder(): void
$this->assertArrayHasKey('layout', $builder->getAttributes());
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
}

#[Test]
public function it_can_accept_callable_as_parameter_in_builder(): void
{
$builder = resolve(Builder::class);
$builder->layout(fn (Layout $layout) => $layout->top('test'));

$this->assertArrayHasKey('layout', $builder->getAttributes());
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
}

#[Test]
public function it_can_accept_js_selector_for_layout_content(): void
{
$builder = resolve(Builder::class);
$builder->layout(fn (Layout $layout) => $layout->topView('#test'));

$this->assertArrayHasKey('layout', $builder->getAttributes());
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['top']);

$builder->layout(fn (Layout $layout) => $layout->bottomView('#test'));
$this->assertArrayHasKey('layout', $builder->getAttributes());
$this->assertArrayHasKey('bottom', $builder->getAttributes()['layout']);
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['bottom']);

$builder->layout(fn (Layout $layout) => $layout->topStartView('#test'));
$this->assertArrayHasKey('layout', $builder->getAttributes());
$this->assertArrayHasKey('topStart', $builder->getAttributes()['layout']);
$this->assertEquals("function() { return $('#test').html(); }",
$builder->getAttributes()['layout']['topStart']);

$builder->layout(fn (Layout $layout) => $layout->topEndView('#test'));
$this->assertArrayHasKey('layout', $builder->getAttributes());
$this->assertArrayHasKey('topEnd', $builder->getAttributes()['layout']);
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['topEnd']);

$builder->layout(fn (Layout $layout) => $layout->bottomStartView('#test'));
$this->assertArrayHasKey('layout', $builder->getAttributes());
$this->assertArrayHasKey('bottomStart', $builder->getAttributes()['layout']);
$this->assertEquals("function() { return $('#test').html(); }",
$builder->getAttributes()['layout']['bottomStart']);

$builder->layout(fn (Layout $layout) => $layout->bottomEndView('#test'));
$this->assertArrayHasKey('layout', $builder->getAttributes());
$this->assertArrayHasKey('bottomEnd', $builder->getAttributes()['layout']);
$this->assertEquals(
"function() { return $('#test').html(); }",
$builder->getAttributes()['layout']['bottomEnd']
);
}
}