From 36f8d45f9bcc83b48f376b6d08c38fd8bb624264 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Mon, 20 Feb 2023 20:32:17 +0800 Subject: [PATCH 1/4] feat: add ability to use vitejs by default --- src/Html/Builder.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 6ab72f6..d95f795 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -2,7 +2,6 @@ namespace Yajra\DataTables\Html; -use Yajra\DataTables\Html\HtmlBuilder; use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\View\Factory; use Illuminate\Support\Collection; @@ -30,26 +29,27 @@ class Builder const SELECT_ITEMS_COLUMN = 'column'; const SELECT_ITEMS_CELL = 'cell'; + /** + * The default type to use for the DataTables javascript. + */ + protected static string $jsType = 'text/javascript'; + /** * @var Collection */ public Collection $collection; - /** * @var array */ protected array $tableAttributes = []; - /** * @var string */ protected string $template = ''; - /** * @var array */ protected array $attributes = []; - /** * @var string|array */ @@ -73,6 +73,14 @@ public function __construct(public Repository $config, public Factory $view, pub ]; } + /** + * Set the default type to module or the DataTables javascript. + */ + public static function useVite(): void + { + static::$jsType = 'module'; + } + /** * Generate DataTable javascript. * @@ -83,7 +91,9 @@ public function __construct(public Repository $config, public Factory $view, pub public function scripts(string $script = null, array $attributes = ['type' => 'text/javascript']): HtmlString { $script = $script ?: $this->generateScripts(); - $attributes = $this->html->attributes($attributes); + $attributes = $this->html->attributes( + array_merge($attributes, ['type' => static::$jsType]) + ); return new HtmlString("$script"); } From daf21cade8ec2e67d7c47d90f1104aa8c25ace96 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Mon, 20 Feb 2023 20:37:37 +0800 Subject: [PATCH 2/4] tests: useVite --- tests/BuilderTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 685a1bc..2c6e559 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -11,6 +11,14 @@ class BuilderTest extends TestCase { + /** @test */ + public function it_can_use_vitejs_module_script() + { + Builder::useVite(); + + $this->assertStringContainsString('type="module"', $this->getHtmlBuilder()->scripts()->toHtml()); + } + /** @test */ public function it_can_resolved_builder_class() { From f84728008b2dcb5a6c16cf251198bc345bd542ff Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Mon, 20 Feb 2023 20:56:19 +0800 Subject: [PATCH 3/4] feat: add useWebpack --- src/Html/Builder.php | 10 +++++++++- tests/BuilderTest.php | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index d95f795..98b5599 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -74,13 +74,21 @@ public function __construct(public Repository $config, public Factory $view, pub } /** - * Set the default type to module or the DataTables javascript. + * Set the default type to module for the DataTables javascript. */ public static function useVite(): void { static::$jsType = 'module'; } + /** + * Set the default type to text/javascript for the DataTables javascript. + */ + public static function useWebpack(): void + { + static::$jsType = 'text/javascript'; + } + /** * Generate DataTable javascript. * diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 2c6e559..c2196b8 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -17,6 +17,8 @@ public function it_can_use_vitejs_module_script() Builder::useVite(); $this->assertStringContainsString('type="module"', $this->getHtmlBuilder()->scripts()->toHtml()); + + Builder::useWebpack(); } /** @test */ From 3db00e154eda1d55ce52ac59352ddce7eab70fe1 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Mon, 20 Feb 2023 21:00:07 +0800 Subject: [PATCH 4/4] fix: new line --- src/Html/Builder.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 98b5599..0eb8e77 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -38,18 +38,22 @@ class Builder * @var Collection */ public Collection $collection; + /** * @var array */ protected array $tableAttributes = []; + /** * @var string */ protected string $template = ''; + /** * @var array */ protected array $attributes = []; + /** * @var string|array */