Create a CollectingAssets hook.

Page module hooking into CollectingAssets to add iCheck
parent 5f9684d4
......@@ -4,6 +4,7 @@ namespace Modules\Core\Composers;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Modules\Core\Events\CollectingAssets;
use Modules\Core\Events\EditorIsRendering;
use Modules\Core\Foundation\Asset\Manager\AssetManager;
use Modules\Core\Foundation\Asset\Pipeline\AssetPipeline;
......@@ -50,6 +51,7 @@ class AssetsViewComposer
$this->assetPipeline->requireJs(config('asgard.core.core.admin-required-assets.js'));
event($editor = new EditorIsRendering($this->assetPipeline));
event(new CollectingAssets($this->assetPipeline));
$view->with('cssFiles', $this->assetPipeline->allCss());
$view->with('jsFiles', $this->assetPipeline->allJs());
......
<?php
namespace Modules\Core\Events;
use Modules\Core\Foundation\Asset\Pipeline\AssetPipeline;
class CollectingAssets
{
/**
* @var AssetPipeline
*/
private $assetPipeline;
public function __construct(AssetPipeline $assetPipeline)
{
$this->assetPipeline = $assetPipeline;
}
/**
* @param string $asset
* @return AssetPipeline
*/
public function requireJs($asset)
{
return $this->assetPipeline->requireJs($asset);
}
/**
* @param string $asset
* @return AssetPipeline
*/
public function requireCss($asset)
{
return $this->assetPipeline->requireCss($asset);
}
/**
* Match a single route
* @param string|array $route
* @return bool
*/
public function onRoute($route)
{
$request = request();
return str_is($route, $request->route()->getName());
}
/**
* Match multiple routes
* @param array $routes
* @return bool
*/
public function onRoutes(array $routes)
{
$request = request();
foreach ($routes as $route) {
if (str_is($route, $request->route()->getName()) === true) {
return true;
}
}
return false;
}
}
......@@ -4,6 +4,7 @@ namespace Modules\Page\Providers;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
use Modules\Core\Events\CollectingAssets;
use Modules\Core\Traits\CanPublishConfiguration;
use Modules\Page\Entities\Page;
use Modules\Page\Repositories\Cache\CachePageDecorator;
......@@ -38,6 +39,8 @@ class PageServiceProvider extends ServiceProvider
$this->app[TagManager::class]->registerNamespace(new Page());
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
$this->handleAssets();
}
/**
......@@ -69,4 +72,17 @@ class PageServiceProvider extends ServiceProvider
}
);
}
/**
* Require iCheck on edit and create pages
*/
private function handleAssets()
{
$this->app['events']->listen(CollectingAssets::class, function (CollectingAssets $event) {
if ($event->onRoutes(['*page*create', '*page*edit'])) {
$event->requireCss('icheck.blue.css');
$event->requireJs('icheck.js');
}
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment