From b1f739fe5d4571c3f6a20bbcd060c6c60aa1285f Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Thu, 25 Sep 2025 14:23:26 +0800 Subject: [PATCH 01/18] fix: editors array docs --- src/Html/HasEditor.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Html/HasEditor.php b/src/Html/HasEditor.php index 5726a15..8dca4cb 100644 --- a/src/Html/HasEditor.php +++ b/src/Html/HasEditor.php @@ -8,6 +8,8 @@ trait HasEditor { /** * Collection of Editors. + * + * @var array */ protected array $editors = []; From 338d76a2f3cb5e77da1c27f46311bc2fc0579d9c Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Tue, 30 Sep 2025 14:09:21 +0800 Subject: [PATCH 02/18] fix: types error changed: ordering should be called manually --- phpstan.neon.dist | 1 + src/Html/Options/Plugins/ColumnControl.php | 3 +-- tests/Html/Extensions/ColumnControlTest.php | 4 ---- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 23d24cc..ec46799 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -15,6 +15,7 @@ parameters: - identifier: binaryOp.invalid - identifier: return.type - identifier: argument.type + - identifier: offsetAccess.nonOffsetAccessible excludePaths: - ./src/Html/Fluent.php diff --git a/src/Html/Options/Plugins/ColumnControl.php b/src/Html/Options/Plugins/ColumnControl.php index 1f3030c..f67bb0c 100644 --- a/src/Html/Options/Plugins/ColumnControl.php +++ b/src/Html/Options/Plugins/ColumnControl.php @@ -77,8 +77,7 @@ public function columnControlFooterSearch(array $content = []): static public function columnControlSearchDropdown(int|string $target = 0): static { - $this->addColumnControl($target, ['order', 'searchDropdown']) - ->ordering(['indicators' => false, 'handler' => false]); + $this->addColumnControl($target, ['order', 'searchDropdown']); return $this; } diff --git a/tests/Html/Extensions/ColumnControlTest.php b/tests/Html/Extensions/ColumnControlTest.php index e7e3687..e372e94 100644 --- a/tests/Html/Extensions/ColumnControlTest.php +++ b/tests/Html/Extensions/ColumnControlTest.php @@ -143,10 +143,6 @@ public function it_can_add_column_control_search_dropdown() ['target' => 1, 'content' => ['order', 'searchDropdown']], ]; $this->assertEquals($expected, $builder->getAttributes()['columnControl']); - - // Should also set ordering options - $attributes = $builder->getAttributes(); - $this->assertEquals(['indicators' => false, 'handler' => false], $attributes['ordering']); } #[Test] From 0639087e4cb7172b8efcba41f3ae95b9dbc15034 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Tue, 30 Sep 2025 14:14:58 +0800 Subject: [PATCH 03/18] feat: add refresh api --- src/Html/Editor/FormOptions.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Html/Editor/FormOptions.php b/src/Html/Editor/FormOptions.php index 03c149c..fe4758e 100644 --- a/src/Html/Editor/FormOptions.php +++ b/src/Html/Editor/FormOptions.php @@ -205,4 +205,18 @@ public function title(bool|string $value): static return $this; } + + /** + * Request that the data be refreshed from the server when starting an edit. + * + * @return $this + * + * @see https://editor.datatables.net/reference/type/form-options#refresh + */ + public function refresh(bool $value = true): static + { + $this->attributes['refresh'] = $value; + + return $this; + } } From 6065b1de131453658ca00809cc55e3effb9d4694 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Tue, 30 Sep 2025 14:37:22 +0800 Subject: [PATCH 04/18] feat: button refresh api --- src/Html/Button.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Html/Button.php b/src/Html/Button.php index 0823962..3ff1c43 100755 --- a/src/Html/Button.php +++ b/src/Html/Button.php @@ -459,4 +459,18 @@ public function __call($method, $parameters) return $this; } + + /** + * Request that the data be refreshed from the server when starting an edit. + * + * @return $this + * + * @see https://editor.datatables.net/reference/type/form-options#refresh + */ + public function refresh(bool $value = true): static + { + $this->attributes['refresh'] = $value; + + return $this; + } } From ab330665f392221ca2b0b2352b7a97a3a583c481 Mon Sep 17 00:00:00 2001 From: Harvey Christian Pacleb Date: Wed, 1 Oct 2025 01:23:25 +0800 Subject: [PATCH 05/18] feat: Add additionalscripts get method --- src/Html/Builder.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 9172874..0d2d753 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -263,4 +263,9 @@ public function addScript(string $view): static return $this; } + + public function getAdditionalScripts(): array + { + return $this->additionalScripts; + } } From 86ba666f0bf7003cec971e43c42c1fc753f5fce4 Mon Sep 17 00:00:00 2001 From: Harvey Christian Pacleb Date: Wed, 1 Oct 2025 01:26:01 +0800 Subject: [PATCH 06/18] feat: Add getTemplate method --- src/Html/Builder.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 9172874..1fa3a82 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -263,4 +263,9 @@ public function addScript(string $view): static return $this; } + + public function getTemplate(): string + { + return $this->template; + } } From 99754563724814a169954248dd8e784c1bb071f4 Mon Sep 17 00:00:00 2001 From: Harvey Christian Pacleb Date: Wed, 1 Oct 2025 01:29:07 +0800 Subject: [PATCH 07/18] feat: Add addScriptIf method --- src/Html/Builder.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 9172874..8440f14 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -263,4 +263,13 @@ public function addScript(string $view): static return $this; } + + public function addScriptIf(bool $condition, string $view): static + { + if ($condition) { + $this->addScript($view); + } + + return $this; + } } From ed39d32950419305cbea02e44375f211ede7c8d0 Mon Sep 17 00:00:00 2001 From: Harvey Christian Pacleb Date: Wed, 1 Oct 2025 01:33:36 +0800 Subject: [PATCH 08/18] feat: Add addScriptIfCan method --- src/Html/Builder.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 9172874..f6fff56 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -5,6 +5,7 @@ use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\View\Factory; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\HtmlString; use Illuminate\Support\Traits\Macroable; use Yajra\DataTables\Utilities\Helper; @@ -263,4 +264,13 @@ public function addScript(string $view): static return $this; } + + public function addScriptIfCan(string $permission, string $view): static + { + if (Gate::allows($permission)) { + $this->addScript($view); + } + + return $this; + } } From 9688e01699776dda0d8aa75143d1fc21cab38f76 Mon Sep 17 00:00:00 2001 From: Harvey Christian Pacleb Date: Wed, 1 Oct 2025 01:36:43 +0800 Subject: [PATCH 09/18] feat: Add addScriptIfCannot method --- src/Html/Builder.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 9172874..12310b3 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -5,6 +5,7 @@ use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\View\Factory; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\HtmlString; use Illuminate\Support\Traits\Macroable; use Yajra\DataTables\Utilities\Helper; @@ -263,4 +264,13 @@ public function addScript(string $view): static return $this; } + + public function addScriptIfCannot(string $permission, string $view): static + { + if (! (Gate::allows($permission))) { + $this->addScript($view); + } + + return $this; + } } From f159178dbe66b10b23966cca7794585a943abd02 Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Wed, 1 Oct 2025 09:04:53 +0800 Subject: [PATCH 10/18] feat: allow setting of template data --- src/Html/Builder.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 9172874..7112e50 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -62,6 +62,8 @@ class Builder protected array $additionalScripts = []; + protected array $templateData = []; + public function __construct(public Repository $config, public Factory $view, public HtmlBuilder $html) { /** @var array $defaults */ @@ -158,7 +160,13 @@ protected function template(): string $template = $this->template ?: $configTemplate; - return $this->view->make($template, ['editors' => $this->editors, 'scripts' => $this->additionalScripts])->render(); + return $this->view->make( + $template, + array_merge( + ['editors' => $this->editors, 'scripts' => $this->additionalScripts], + $this->templateData, + ) + )->render(); } /** @@ -263,4 +271,15 @@ public function addScript(string $view): static return $this; } + + public function templateData(array|\Closure $data = []): static + { + if ($data instanceof \Closure) { + $data = $data($this) ?? []; + } + + $this->templateData = $data; + + return $this; + } } From 3b01294d5c8c138f9490b2467fe95e87f0226c24 Mon Sep 17 00:00:00 2001 From: Harvey Christian Pacleb Date: Wed, 1 Oct 2025 09:05:27 +0800 Subject: [PATCH 11/18] fix: Follow laravel convention for variable name Co-authored-by: Arjay Angeles --- src/Html/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 12310b3..4bf7562 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -265,9 +265,9 @@ public function addScript(string $view): static return $this; } - public function addScriptIfCannot(string $permission, string $view): static + public function addScriptIfCannot(string $ability, string $view): static { - if (! (Gate::allows($permission))) { + if (! (Gate::allows($ability))) { $this->addScript($view); } From 1161adc4d9cb8bf62b54e3b73367784248ad2997 Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Wed, 1 Oct 2025 09:09:14 +0800 Subject: [PATCH 12/18] refactor: rename templateData method to setTemplateData --- src/Html/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 7112e50..2686a78 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -272,7 +272,7 @@ public function addScript(string $view): static return $this; } - public function templateData(array|\Closure $data = []): static + public function setTemplateData(array|\Closure $data = []): static { if ($data instanceof \Closure) { $data = $data($this) ?? []; From 19f1828cc9baba491f84a311a8a7b1f4564698d5 Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Wed, 1 Oct 2025 09:26:58 +0800 Subject: [PATCH 13/18] test: add unit test for setting template data --- tests/Html/Builder/BuilderTest.php | 28 +++++++++++++++++++ .../test-builder-script.blade.php | 1 + 2 files changed, 29 insertions(+) create mode 100644 tests/TestComponents/test-builder-script.blade.php diff --git a/tests/Html/Builder/BuilderTest.php b/tests/Html/Builder/BuilderTest.php index 1b61ef2..897efb4 100644 --- a/tests/Html/Builder/BuilderTest.php +++ b/tests/Html/Builder/BuilderTest.php @@ -303,4 +303,32 @@ public function it_ignores_unauthorized_columns(): void $this->assertCount(1, $builder->getColumns()); } + + #[Test] + public function it_can_set_template_data(): void + { + $builder = $this->getHtmlBuilder() + ->addScript('test-builder-script') + ->setTemplateData(['message' => 'Test Message']); + + $this->assertStringContainsString( + "console.log({ tableId: 'noneset', message: 'Test Message' });", + $builder->generateScripts()->toHtml() + ); + + $builder + ->setTableId('my-table') + ->addScript('test-builder-script') + ->setTemplateData(function (Builder $builder): array { + return [ + 'tableId' => $builder->getTableId(), + 'message' => 'Set Template Data Using Callback', + ]; + }); + + $this->assertStringContainsString( + "console.log({ tableId: 'my-table', message: 'Set Template Data Using Callback' });", + $builder->generateScripts()->toHtml() + ); + } } diff --git a/tests/TestComponents/test-builder-script.blade.php b/tests/TestComponents/test-builder-script.blade.php new file mode 100644 index 0000000..a3ef460 --- /dev/null +++ b/tests/TestComponents/test-builder-script.blade.php @@ -0,0 +1 @@ +console.log({ tableId: @js($tableId ?? 'noneset'), message: @js($message) }); From 47f4e660dc329f7b27cfc6cf324dfa1b20a41337 Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Wed, 1 Oct 2025 09:32:48 +0800 Subject: [PATCH 14/18] refactor: remove redundant script addition in BuilderTest --- tests/Html/Builder/BuilderTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Html/Builder/BuilderTest.php b/tests/Html/Builder/BuilderTest.php index 897efb4..32071e8 100644 --- a/tests/Html/Builder/BuilderTest.php +++ b/tests/Html/Builder/BuilderTest.php @@ -318,7 +318,6 @@ public function it_can_set_template_data(): void $builder ->setTableId('my-table') - ->addScript('test-builder-script') ->setTemplateData(function (Builder $builder): array { return [ 'tableId' => $builder->getTableId(), From bbfacfd8e87c0aba164352bbd38d447a17360dcb Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Wed, 1 Oct 2025 10:08:02 +0800 Subject: [PATCH 15/18] chore: pint --- src/Html/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 03e80e0..95e3454 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -276,7 +276,7 @@ public function getTemplate(): string { return $this->template; } - + public function setTemplateData(array|\Closure $data = []): static { if ($data instanceof \Closure) { From 50564524460ce4cca5f6abe4efdb93560b2a678e Mon Sep 17 00:00:00 2001 From: Harvey Christian Pacleb Date: Wed, 1 Oct 2025 11:03:22 +0800 Subject: [PATCH 16/18] fix: Use gate denies instead for readability --- src/Html/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 4953d53..5c9546e 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -267,7 +267,7 @@ public function addScript(string $view): static public function addScriptIfCannot(string $ability, string $view): static { - if (! (Gate::allows($ability))) { + if (Gate::allows($ability)) { $this->addScript($view); } From 44d8e33e0257130d604f222efad812090867842c Mon Sep 17 00:00:00 2001 From: Harvey Christian Pacleb Date: Wed, 1 Oct 2025 11:04:18 +0800 Subject: [PATCH 17/18] fix: Use gate denies instead for readability --- src/Html/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 5c9546e..ed537c9 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -267,7 +267,7 @@ public function addScript(string $view): static public function addScriptIfCannot(string $ability, string $view): static { - if (Gate::allows($ability)) { + if (Gate::denies($ability)) { $this->addScript($view); } From f0e9eb67b3e75fd42ccf21867ce159b1bc967904 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Wed, 1 Oct 2025 14:03:07 +0800 Subject: [PATCH 18/18] chore: use $ability variable --- src/Html/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index e9a2869..3e3af09 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -265,9 +265,9 @@ public function addScript(string $view): static return $this; } - public function addScriptIfCan(string $permission, string $view): static + public function addScriptIfCan(string $ability, string $view): static { - if (Gate::allows($permission)) { + if (Gate::allows($ability)) { $this->addScript($view); }