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/Builder.php b/src/Html/Builder.php index 9172874..fa2b434 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; @@ -62,6 +63,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 +161,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 +272,52 @@ public function addScript(string $view): static return $this; } + + public function addScriptIfCan(string $ability, string $view): static + { + if (Gate::allows($ability)) { + $this->addScript($view); + } + + return $this; + } + + public function addScriptIf(bool $condition, string $view): static + { + if ($condition) { + $this->addScript($view); + } + + return $this; + } + + public function addScriptIfCannot(string $ability, string $view): static + { + if (Gate::denies($ability)) { + $this->addScript($view); + } + + return $this; + } + + public function getTemplate(): string + { + return $this->template; + } + + public function getAdditionalScripts(): array + { + return $this->additionalScripts; + } + + public function setTemplateData(array|\Closure $data = []): static + { + if ($data instanceof \Closure) { + $data = $data($this) ?? []; + } + + $this->templateData = $data; + + return $this; + } } 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; + } } 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; + } } 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 = []; 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/Builder/BuilderTest.php b/tests/Html/Builder/BuilderTest.php index 1b61ef2..32071e8 100644 --- a/tests/Html/Builder/BuilderTest.php +++ b/tests/Html/Builder/BuilderTest.php @@ -303,4 +303,31 @@ 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') + ->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/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] 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) });