Implement assets loading.

This commit is contained in:
David Molineus
2017-10-06 15:50:54 +02:00
parent 31cd1c3ae4
commit 524678f56e
3 changed files with 147 additions and 0 deletions

View File

@@ -0,0 +1,135 @@
<?php
/**
* Leaflet maps for Contao CMS.
*
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2016-2017 netzmacht David Molineus. All rights reserved.
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
* @filesource
*/
declare(strict_types=1);
namespace Netzmacht\Contao\Leaflet\Listener;
use Netzmacht\Contao\Leaflet\ContaoAssets;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\LeafletPHP\Assets;
use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Definition\Type\ImageIcon;
/**
* Class LoadAssetsListener.
*
* @package Netzmacht\Contao\Leaflet\Listener
*/
class LoadAssetsListener
{
/**
* Assets.
*
* @var Assets
*/
private $assets;
/**
* Definition mapper.
*
* @var DefinitionMapper
*/
private $definitionMapper;
/**
* Libraries.
*
* @var array
*/
private $libraries;
/**
* LoadAssetsListener constructor.
*
* @param Assets $assets Assets.
* @param DefinitionMapper $definitionMapper Definition mapper.
* @param array $libraries Libraries.
*/
public function __construct(Assets $assets, DefinitionMapper $definitionMapper, array $libraries)
{
$this->assets = $assets;
$this->definitionMapper = $definitionMapper;
}
/**
* Handle the get javascript event.
*
* @return void
*/
public function onGetJavascriptEvent(): void
{
$this->assets->addJavascript('assets/leaflet/maps/contao-leaflet.js', ContaoAssets::TYPE_FILE);
$collection = IconModel::findBy('active', true);
if ($collection) {
$buffer = '';
$icons = [];
foreach ($collection as $model) {
/** @var ImageIcon $icon */
$icon = $this->definitionMapper->handle($model);
$icons[] = [
'id' => $icon->getId(),
'type' => lcfirst($icon->getType()),
'options' => $icon->getOptions(),
];
$this->loadIconsLibraries($icon);
}
if ($icons) {
$buffer = sprintf('L.contao.loadIcons(%s);', json_encode($icons));
}
// @codingStandardsIgnoreStart
// TODO: Cache it.
// codingStandardsIgnoreEnd
$file = new \File('assets/leaflet/js/icons.js');
$file->write($buffer);
$file->close();
$this->assets->addJavascript('assets/leaflet/js/icons.js', ContaoAssets::TYPE_FILE);
}
}
/**
* Load all libraries for an icon.
*
* @param Icon $icon Icon definition.
*
* @return void
*/
protected function loadIconsLibraries($icon): void
{
foreach ($icon::getRequiredLibraries() as $library) {
if (!isset($this->libraries[$library])) {
continue;
}
$assets = $this->libraries[$library];
if (!empty($assets['css'])) {
list ($source, $type) = (array)$assets['css'];
$this->assets->addStylesheet($source, $type ?: Assets::TYPE_FILE);
}
if (!empty($assets['javascript'])) {
list ($source, $type) = (array)$assets['javascript'];
$this->assets->addJavascript($source, $type ?: Assets::TYPE_FILE);
}
}
}
}

View File

@@ -58,3 +58,6 @@ parameters:
- 'id'
- 'title'
- 'alias'
# Leaflet libraries files
netzmacht.contao_leaflet_maps.libraries: []

View File

@@ -54,3 +54,12 @@ services:
- '%netzmacht.contao_leaflet_maps.feature_model_properties%'
tags:
- { name: 'kernel.event_subscriber' }
netzmacht.contao_leaflet_maps.listeners.load_assets:
class: Netzmacht\Contao\Leaflet\Listener\LoadAssetsListener
arguments:
- '@netzmacht.contao_leaflet_maps.map.assets'
- '@netzmacht.contao_leaflet_maps.definition.mapper'
- '%netzmacht.contao_leaflet_maps.libraries%'
tags:
- { name: 'kernel.event_listener', event: 'netzmacht.contao_leaflet.get_javascript' }