From eaece2b69d028fba0db5a5bb55b1386acf017cbc Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 28 Jul 2024 14:52:56 +1000 Subject: [PATCH] Move javascript utilities to their own class PageAssets and creatd blade directive @pa to simplify using them --- .../adminlte/views/layouts/app.blade.php | 2 - .../layouts/partials/htmlheader.blade.php | 3 + .../views/layouts/partials/scripts.blade.php | 5 + src/PageAssets.php | 188 ++++++++++++++++++ src/Providers/CustomBladeServiceProvider.php | 96 +-------- src/Providers/LeenooksServiceProvider.php | 17 ++ 6 files changed, 216 insertions(+), 95 deletions(-) create mode 100644 src/PageAssets.php diff --git a/resources/themes/adminlte/views/layouts/app.blade.php b/resources/themes/adminlte/views/layouts/app.blade.php index 6cd611b..e1ee103 100644 --- a/resources/themes/adminlte/views/layouts/app.blade.php +++ b/resources/themes/adminlte/views/layouts/app.blade.php @@ -38,8 +38,6 @@ @section('scripts') @include('adminlte::layouts.partials.scripts') - - @yield('page-scripts') @show \ No newline at end of file diff --git a/resources/themes/adminlte/views/layouts/partials/htmlheader.blade.php b/resources/themes/adminlte/views/layouts/partials/htmlheader.blade.php index 789a4c1..80d2878 100644 --- a/resources/themes/adminlte/views/layouts/partials/htmlheader.blade.php +++ b/resources/themes/adminlte/views/layouts/partials/htmlheader.blade.php @@ -41,6 +41,9 @@ --}} + + {!! PageAssets::css() !!} + @yield('page-styles') @if(file_exists('css/fixes.css')) diff --git a/resources/themes/adminlte/views/layouts/partials/scripts.blade.php b/resources/themes/adminlte/views/layouts/partials/scripts.blade.php index 93ebf08..ab9cf2a 100644 --- a/resources/themes/adminlte/views/layouts/partials/scripts.blade.php +++ b/resources/themes/adminlte/views/layouts/partials/scripts.blade.php @@ -101,6 +101,11 @@ }); + +{!! PageAssets::js() !!} + +@yield('page-scripts') + @if(file_exists('js/custom.js')) diff --git a/src/PageAssets.php b/src/PageAssets.php new file mode 100644 index 0000000..ed0d130 --- /dev/null +++ b/src/PageAssets.php @@ -0,0 +1,188 @@ + [ + 'base' => [ + 'css' => [ + '//cdn.datatables.net/2.1.2/css/dataTables.bootstrap4.css', + //'//cdn.datatables.net/2.1.2/css/dataTables.dataTables.min.css', + ], + 'js' => [ + '//cdn.datatables.net/2.1.2/js/dataTables.min.js', + '//cdn.datatables.net/2.1.2/js/dataTables.bootstrap4.min.js', + ], + ], + 'buttons' => [ + 'css' => [ + '//cdn.datatables.net/buttons/3.1.0/css/buttons.bootstrap4.min.css', + //'//cdn.datatables.net/buttons/3.1.0/css/buttons.dataTables.min.css', + ], + 'js' => [ + '//cdn.datatables.net/buttons/3.1.0/js/dataTables.buttons.min.js', + //'//cdn.datatables.net/buttons/3.1.0/js/buttons.dataTables.min.js', + '//cdn.datatables.net/buttons/3.1.0/js/buttons.bootstrap4.min.js', + '//cdnjs.cloudflare.com/ajax/libs/jszip/3.2.0/jszip.min.js', + ], + ], + 'conditionalpaging' => [ + 'js' => [ + '//cdn.datatables.net/plug-ins/2.0.5/features/conditionalPaging/dataTables.conditionalPaging.min.js', + ], + ], + 'fixedheader' => [ + 'css' => [ + '//cdn.datatables.net/fixedheader/4.0.1/css/fixedHeader.bootstrap4.min.css', + //'//cdn.datatables.net/fixedheader/4.0.1/css/fixedHeader.dataTables.min.css', + ], + 'js' => [ + '//cdn.datatables.net/fixedheader/4.0.1/js/dataTables.fixedHeader.min.js', + //'//cdn.datatables.net/fixedheader/4.0.1/js/fixedHeader.dataTables.min.js', + '//cdn.datatables.net/fixedheader/4.0.1/js/fixedHeader.bootstrap4.min.js', + ] + ], + 'responsive' => [ + 'css' => [ + '//cdn.datatables.net/responsive/3.0.2/css/responsive.bootstrap4.min.css', + //'//cdn.datatables.net/responsive/3.0.2/css/responsive.dataTables.min.css', + ], + 'js' => [ + '//cdn.datatables.net/responsive/3.0.2/js/dataTables.responsive.min.js', + //'//cdn.datatables.net/responsive/3.0.2/js/responsive.bootstrap.min.js', + '//cdn.datatables.net/responsive/3.0.2/js/responsive.bootstrap4.min.js', + ] + ], + 'rowgroup' => [ + 'css' => [ + '//cdn.datatables.net/rowgroup/1.5.0/css/rowGroup.bootstrap4.min.css', + //'//cdn.datatables.net/rowgroup/1.5.0/css/rowGroup.dataTables.min.css', + ], + 'js' => [ + '//cdn.datatables.net/rowgroup/1.5.0/js/dataTables.rowGroup.min.js', + //'//cdn.datatables.net/rowgroup/1.5.0/js/rowGroup.dataTables.min.js', + '//cdn.datatables.net/rowgroup/1.5.0/js/rowGroup.bootstrap4.min.js', + ], + ], + 'searchpanes' => [ + 'css' => [ + '//cdn.datatables.net/searchpanes/2.3.1/css/searchPanes.bootstrap4.min.css', + //'//cdn.datatables.net/searchpanes/2.3.1/css/searchPanes.dataTables.min.css', + ], + 'js' => [ + '//cdn.datatables.net/searchpanes/2.3.1/js/dataTables.searchPanes.min.js', + //'//cdn.datatables.net/searchpanes/2.3.1/js/searchPanes.dataTables.min.js', + '//cdn.datatables.net/searchpanes/2.3.1/js/searchPanes.bootstrap4.min.js', + ], + ], + 'searchpanes-left' => [ + 'css' => [ + '/plugin/dataTables/leftSearchPanes.css', + ], + ], + 'select' => [ + 'css' => [ + '//cdn.datatables.net/select/2.0.3/css/select.bootstrap4.min.css', + //'//cdn.datatables.net/select/2.0.3/css/select.dataTables.min.css', + ], + 'js' => [ + '//cdn.datatables.net/select/2.0.3/js/dataTables.select.min.js', + //'//cdn.datatables.net/select/2.0.3/js/select.dataTables.min.js', + '//cdn.datatables.net/select/2.0.3/js/select.bootstrap4.min.js', + ] + ], + ], + ]; + + // Items to manage + public static Collection $items; + + // Add an item to the list + public static function add(string $type,Collection|array|string $asset): void + { + if (! in_array($type,self::types)) + throw new \Exception('Invalid type: '.$type); + + if (! isset(self::$items)) + self::init(); + + if (is_string($asset)) + self::$items + ->get($type) + ->push($asset) + ->unique(); + else + self::$items->put($type, + self::$items + ->get($type) + ->merge($asset->values()) + ->unique()); + } + + // Add a predefined asset + public static function asset(string $id): void + { + if (! isset(self::$items)) + self::init(); + + if (str_contains($id,',')) { + [$item,$arguments] = explode(',',$id,2); + $arguments = collect(explode('|',$arguments)); + + } else { + $item = $id; + $arguments = collect(); + } + + $arguments = $arguments->prepend('base'); + $asset = collect(Arr::get(self::assets,$item))->only($arguments); + + foreach (self::types as $type) + if ($x=$asset->pluck($type)->filter()->flatten()) + self::add($type,$x); + } + + // Render the CSS items + public static function css(): string + { + return isset(self::$items) + ? self::$items + ->get('css') + ->map(fn($item)=>sprintf('',$item)) + ->join('') + : ''; + } + + public static function init(): void + { + self::$items = collect([ + 'js' => collect(), + 'css' => collect(), + ]); + } + + // Render the JS items + public static function js(): string + { + return isset(self::$items) + ? self::$items + ->get('js') + ->map(fn($item)=>sprintf('',$item)) + ->join('') + : ''; + } +} \ No newline at end of file diff --git a/src/Providers/CustomBladeServiceProvider.php b/src/Providers/CustomBladeServiceProvider.php index e021ebd..1ea1dc6 100644 --- a/src/Providers/CustomBladeServiceProvider.php +++ b/src/Providers/CustomBladeServiceProvider.php @@ -15,11 +15,11 @@ class CustomBladeServiceProvider extends ServiceProvider */ public function boot() { - Blade::directive('css',function(string $expression,array $args=[]) { + Blade::directive('css',function(string $expression) { return $this->resolve('css',$expression); }); - Blade::directive('js',function($expression,string $options=NULL) { + Blade::directive('js',function($expression) { return $this->resolve('js',$expression); }); } @@ -41,97 +41,7 @@ class CustomBladeServiceProvider extends ServiceProvider switch ($type) { case 'datatables': - switch ($content) { - case 'css': - // Base - $css->put($type,'https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css'); - - foreach ($arguments as $option) { - $key = $type.':'.$option; - switch ($option) { - case 'bootstrap4': - $css->put($key,'/plugin/dataTables/dataTables.bootstrap4.css'); - break; - - case 'buttons': - $css->put($key,'https://cdn.datatables.net/buttons/1.6.5/css/buttons.dataTables.min.css'); - break; - - case 'fixedheader': - $css->put($key,'https://cdn.datatables.net/fixedheader/3.1.7/css/fixedHeader.dataTables.min.css'); - break; - - case 'rowgroup': - $css->put($key,'https://cdn.datatables.net/rowgroup/1.1.2/css/rowGroup.dataTables.min.css'); - break; - - case 'responsive': - $css->put($key,'http://cdn.datatables.net/responsive/2.2.6/css/responsive.dataTables.min.css'); - break; - - case 'searchpanes': - $css->put($key,'https://cdn.datatables.net/searchpanes/1.2.1/css/searchPanes.dataTables.min.css'); - break; - - case 'searchpanes-left': - $css->put('searchpanes:searchpanes-left','/plugin/dataTables/leftSearchPanes.css'); - break; - - case 'select': - $css->put($key,'https://cdn.datatables.net/select/1.3.1/css/select.dataTables.min.css'); - break; - - default: - throw new \Exception(sprintf('Unknown [%s] option: [%s:%s]',$type,$content,$option)); - } - } - - break; - - case 'js': - // Base - $js->put($type,'https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js'); - - foreach ($arguments as $option) { - $key = $type.':'.$option; - - switch ($option) { - case 'bootstrap4': - $js->put($key,'/plugin/dataTables/dataTables.bootstrap4.js'); - break; - - case 'buttons': - $js->put($key,'https://cdn.datatables.net/buttons/1.6.5/js/dataTables.buttons.min.js'); - $js->put($key.'html5','https://cdn.datatables.net/buttons/1.6.5/js/buttons.html5.min.js'); - $js->put($key.'jszip','https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.0/jszip.min.js'); - break; - - case 'fixedheader': - $js->put($key,'https://cdn.datatables.net/fixedheader/3.1.7/js/dataTables.fixedHeader.min.js'); - break; - - case 'responsive': - $js->put($key,'https://cdn.datatables.net/responsive/2.2.6/js/dataTables.responsive.min.js'); - break; - - case 'rowgroup': - $js->put($key,'https://cdn.datatables.net/rowgroup/1.1.2/js/dataTables.rowGroup.min.js'); - break; - - case 'searchpanes': - $js->put($key,'https://cdn.datatables.net/searchpanes/1.2.1/js/dataTables.searchPanes.min.js'); - break; - - case 'select': - $js->put($key,'https://cdn.datatables.net/select/1.3.1/js/dataTables.select.min.js'); - break; - - default: - throw new \Exception(sprintf('Unknown [%s] option: [%s:%s]',$type,$content,$option)); - } - } - } - + Log::alert(sprintf('Blade @css/@js for %s deprecicated, use @pa',$type)); break; case 'datepick': diff --git a/src/Providers/LeenooksServiceProvider.php b/src/Providers/LeenooksServiceProvider.php index c1fbff6..6673e06 100644 --- a/src/Providers/LeenooksServiceProvider.php +++ b/src/Providers/LeenooksServiceProvider.php @@ -2,11 +2,15 @@ namespace Leenooks\Providers; +use Illuminate\Foundation\AliasLoader; use Illuminate\Support\Facades\Blade; use Leenooks\Http\Middleware\GuestUser; use Illuminate\Routing\Router; use Illuminate\Support\Collection; use Illuminate\Support\ServiceProvider; +use Leenooks\PageAssets; +use Leenooks\Traits\SingleOrFail; +use Orchestra\Support\Facades\Asset; /** * Class GuestUserServiceProvider. @@ -15,6 +19,8 @@ use Illuminate\Support\ServiceProvider; */ class LeenooksServiceProvider extends ServiceProvider { + use SingleOrFail; + private string $_path = ''; /** @@ -24,6 +30,12 @@ class LeenooksServiceProvider extends ServiceProvider */ public function boot(Router $router) { + self::bootSingleOrfail(); + + // Custom Aliases + $loader = AliasLoader::getInstance(); + $loader->alias('PageAssets',PageAssets::class); + $router->pushMiddlewareToGroup('web',GuestUser::class); $this->loadViewsFrom($this->_path.'/resources/themes/adminlte/views/', 'adminlte'); @@ -36,6 +48,11 @@ class LeenooksServiceProvider extends ServiceProvider //Blade::componentNamespace('Leenooks\\Components','leenooks'); Blade::anonymousComponentPath($this->_path.'/resources/components', 'leenooks'); + // Add our page assets + Blade::directive('pa',function($expression) { + return sprintf('',$expression); + }); + // Enable a Collect::recursive() function Collection::macro('recursive', function () { return $this->map(function ($value) {