Optimise calling dependant javascript in views

This commit is contained in:
Deon George 2021-11-22 13:52:16 +11:00
parent dd64cba897
commit 0507dc83d8
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
2 changed files with 241 additions and 4 deletions

View File

@ -10,9 +10,8 @@
} }
], ],
"require": { "require": {
"igaster/laravel-theme": "^2.0.17", "creativeorange/gravatar": "^1.0",
"creativeorange/gravatar": "^1.0", "orchestra/asset": "^6.0"
"orchestra/asset": "^6.0"
}, },
"require-dev": { "require-dev": {
}, },
@ -27,7 +26,10 @@
"extra": { "extra": {
"laravel": { "laravel": {
"providers": [ "providers": [
"Leenooks\\Providers\\LeenooksServiceProvider" "Orchestra\\Asset\\AssetServiceProvider",
"Collective\\Html\\HtmlServiceProvider",
"Leenooks\\Providers\\LeenooksServiceProvider",
"Leenooks\\Providers\\CustomBladeServiceProvider"
] ]
} }
}, },

View File

@ -0,0 +1,235 @@
<?php
namespace Leenooks\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Orchestra\Support\Facades\Asset;
class CustomBladeServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
Blade::directive('css',function(string $expression,array $args=[]) {
return $this->resolve('css',$expression);
});
Blade::directive('js',function($expression,string $options=NULL) {
return $this->resolve('js',$expression);
});
}
private function resolve(string $content,string $expression): string
{
if (str_contains($expression,','))
[$type,$arguments] = explode(',',$expression,2);
else {
$type = $expression;
$arguments = '';
}
$arguments = explode('|',$arguments);
$return = collect();
$urls = collect();
switch ($type) {
case 'datatables':
switch ($content) {
case 'css':
// Base
$urls->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':
$urls->put($key,'/plugin/dataTables/dataTables.bootstrap4.css');
break;
case 'buttons':
$urls->put($key,'https://cdn.datatables.net/buttons/1.6.5/css/buttons.dataTables.min.css');
break;
case 'fixedheader':
$urls->put($key,'https://cdn.datatables.net/fixedheader/3.1.7/css/fixedHeader.dataTables.min.css');
break;
case 'rowgroup':
$urls->put($key,'https://cdn.datatables.net/rowgroup/1.1.2/css/rowGroup.dataTables.min.css');
break;
case 'responsive':
$urls->put($key,'http://cdn.datatables.net/responsive/2.2.6/css/responsive.dataTables.min.css');
break;
case 'searchpanes':
$urls->put($key,'https://cdn.datatables.net/searchpanes/1.2.1/css/searchPanes.dataTables.min.css');
break;
case 'searchpanes-left':
$urls->put('searchpanes:searchpanes-left','/plugin/dataTables/leftSearchPanes.css');
break;
case 'select':
$urls->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
$urls->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':
$urls->put($key,'/plugin/dataTables/dataTables.bootstrap4.js');
break;
case 'buttons':
$urls->put($key,'https://cdn.datatables.net/buttons/1.6.5/js/dataTables.buttons.min.js');
$urls->put($key.'html5','https://cdn.datatables.net/buttons/1.6.5/js/buttons.html5.min.js');
$urls->put($key.'jszip','https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.0/jszip.min.js');
break;
case 'fixedheader':
$urls->put($key,'https://cdn.datatables.net/fixedheader/3.1.7/js/dataTables.fixedHeader.min.js');
break;
case 'responsive':
$urls->put($key,'https://cdn.datatables.net/responsive/2.2.6/js/dataTables.responsive.min.js');
break;
case 'rowgroup':
$urls->put($key,'https://cdn.datatables.net/rowgroup/1.1.2/js/dataTables.rowGroup.min.js');
break;
case 'searchpanes':
$urls->put($key,'https://cdn.datatables.net/searchpanes/1.2.1/js/dataTables.searchPanes.min.js');
break;
case 'select':
$urls->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));
}
}
}
break;
case 'datepick':
switch ($content) {
case 'css':
$urls->put($type,'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css');
break;
case 'js':
$urls->put($type,'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js');
}
break;
case 'highcharts':
switch ($content) {
case 'js':
// Base
$urls->put($type,'https://code.highcharts.com/highcharts.js');
foreach ($arguments as $option) {
$key = $type.':'.$option;
switch ($option) {
case '3d':
$urls->put($key,'https://code.highcharts.com/highcharts-3d.js');
$urls->put($key.'mouseover','/plugin/highcharts/3dmouseover.js');
break;
case 'data':
$urls->put($key,'https://code.highcharts.com/modules/data.js');
break;
case 'defaults':
$urls->put($key,'/plugin/highcharts/defaults.js');
break;
case 'drilldown':
$urls->put($key,'https://code.highcharts.com/modules/drilldown.js');
break;
case 'heatmap':
$urls->put($key,'https://code.highcharts.com/modules/heatmap.js');
break;
case 'export':
$urls->put($key,'https://code.highcharts.com/modules/exporting.js');
$urls->put($key.'data','https://code.highcharts.com/modules/export-data.js');
break;
case 'theme-dark':
$urls->put($key,'https://code.highcharts.com/themes/dark-unica.js');
break;
default:
throw new \Exception(sprintf('Unknown [%s] option: [%s:%s]',$type,$content,$option));
}
}
}
break;
case 'highcharts-stock':
switch ($content) {
case 'js':
// Base
$urls->put($type,'https://code.highcharts.com/stock/highstock.js');
foreach ($arguments as $option) {
$key = $type.':'.$option;
switch ($option) {
case 'data':
$urls->put($key,'https://code.highcharts.com/stock/modules/data.js');
break;
case 'export':
$urls->put($key,'https://code.highcharts.com/stock/modules/exporting.js');
break;
default:
throw new \Exception(sprintf('Unknown [%s] option: [%s:%s]',$type,$content,$option));
}
}
}
break;
default:
throw new \Exception(sprintf('Unknown Expression: [%s]',$expression));
}
if ($urls->count())
return $urls->map(function($item,$key) {
$dependancy='';
if (str_contains($key,':'))
[$dependancy,$key] = explode(':',$key);
return "<?php Asset::add('$key','$item','$dependancy'); ?>";
})->join('');
}
}