Simplify assets configuration.

This commit is contained in:
David Molineus
2015-01-08 12:44:42 +01:00
parent 977ff01ced
commit 135bd9f7c6
3 changed files with 45 additions and 48 deletions

View File

@@ -160,39 +160,32 @@ require_once TL_ROOT . '/system/modules/leaflet/config/leaflet_providers.php';
* - url: An valid url. * - url: An valid url.
* - file: An file path relative to the Contao Root. * - file: An file path relative to the Contao Root.
* - source: Inline css/javascript. * - source: Inline css/javascript.
*
* You don't have to define it as array if you simply add a file. Do not add |static and or media type flag to it.
* It's getting added by default if not being in debug mode.
*/ */
$GLOBALS['LEAFLET_ASSETS']['leaflet'] = array( $GLOBALS['LEAFLET_ASSETS'] = array
'css' => array( (
array('assets/leaflet/libs/leaflet/leaflet.css', 'file') 'leaflet' => array
(
'css' => 'assets/leaflet/libs/leaflet/leaflet.css',
'javascript' => 'assets/leaflet/libs/leaflet/leaflet.js'
), ),
'javascript' => array( 'leaflet-providers' => array
array('assets/leaflet/libs/leaflet/leaflet.js', 'file') (
) 'javascript' => 'assets/leaflet/libs/leaflet-providers/leaflet-providers.js'
);
$GLOBALS['LEAFLET_ASSETS']['leaflet-providers'] = array(
'javascript' => array(
array('assets/leaflet/libs/leaflet-providers/leaflet-providers.js', 'file')
)
);
$GLOBALS['LEAFLET_ASSETS']['leaflet-ajax'] = array(
'javascript' => array(
array('assets/leaflet/libs/leaflet-ajax/leaflet.ajax.min.js', 'file')
)
);
$GLOBALS['LEAFLET_ASSETS']['leaflet-loading'] = array(
'css' => array(
array('assets/leaflet/libs/leaflet-loading/Control.Loading.css', 'file')
), ),
'javascript' => array( 'leaflet-ajax' => array
array('assets/leaflet/libs/leaflet-loading/Control.Loading.js', 'file') (
) 'javascript' => 'assets/leaflet/libs/leaflet-ajax/leaflet.ajax.min.js'
); ),
'leaflet-loading' => array
$GLOBALS['LEAFLET_ASSETS']['spin.js'] = array( (
'javascript' => array( 'css' => 'assets/leaflet/libs/leaflet-loading/Control.Loading.css',
array('assets/leaflet/libs/spin-js/spin.min.js', 'file') 'javascript' => 'assets/leaflet/libs/leaflet-loading/Control.Loading.js'
),
'spin.js' => array
(
'javascript' => 'assets/leaflet/libs/spin-js/spin.min.js'
) )
); );

View File

@@ -35,7 +35,10 @@ class ContaoAssets implements Assets
break; break;
case static::TYPE_FILE: case static::TYPE_FILE:
$script .= '|static'; if (!\Config::get('debugMode')) {
$script .= '|static';
}
// no break // no break
default: default:
@@ -54,7 +57,9 @@ class ContaoAssets implements Assets
break; break;
case static::TYPE_FILE: case static::TYPE_FILE:
$stylesheet .= '|all|static'; if (!\Config::get('debugMode')) {
$stylesheet .= '|all|static';
}
// no break // no break
default: default:

View File

@@ -18,9 +18,8 @@ use Netzmacht\Contao\Leaflet\Event\InitializeEventDispatcherEvent;
use Netzmacht\Contao\Leaflet\Event\InitializeLeafletBuilderEvent; use Netzmacht\Contao\Leaflet\Event\InitializeLeafletBuilderEvent;
use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper; use Netzmacht\Contao\Leaflet\Mapper\DefinitionMapper;
use Netzmacht\Contao\Leaflet\Model\IconModel; use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\Javascript\Output; use Netzmacht\LeafletPHP\Assets;
use Netzmacht\LeafletPHP\Definition\Type\Icon; use Netzmacht\LeafletPHP\Definition\Type\Icon;
use Netzmacht\LeafletPHP\Leaflet;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/** /**
@@ -97,15 +96,13 @@ class BootSubscriber implements EventSubscriberInterface
foreach ($GLOBALS['LEAFLET_ASSETS'] as $name => $assets) { foreach ($GLOBALS['LEAFLET_ASSETS'] as $name => $assets) {
if (!empty($assets['css'])) { if (!empty($assets['css'])) {
foreach ($assets['css'] as $javascript) { list ($source, $type) = (array) $assets['css'];
$builder->registerStylesheet($name, $javascript[0], $javascript[1]); $builder->registerStylesheet($name, $source, $type ?: Assets::TYPE_FILE);
}
} }
if (!empty($assets['javascript'])) { if (!empty($assets['javascript'])) {
foreach ($assets['javascript'] as $javascript) { list ($source, $type) = (array) $assets['javascript'];
$builder->registerJavascript($name, $javascript[0], $javascript[1]); $builder->registerJavascript($name, $source, $type ?: Assets::TYPE_FILE);
}
} }
} }
} }
@@ -117,7 +114,10 @@ class BootSubscriber implements EventSubscriberInterface
*/ */
public function loadAssets() public function loadAssets()
{ {
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/libs/contao/contao-leaflet.js|static'; $GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/libs/contao/contao-leaflet.js' . (\Config::get('debugMode')
? ''
: '|static'
);
} }
/** /**
@@ -135,10 +135,6 @@ class BootSubscriber implements EventSubscriberInterface
$buffer = ''; $buffer = '';
$icons = array(); $icons = array();
/** @var Leaflet $builder */
$builder = $GLOBALS['container']['leaflet.definition.builder'];
$encoder = $builder->getBuilder()->getEncoder();
foreach ($collection as $model) { foreach ($collection as $model) {
/** @var Icon $icon */ /** @var Icon $icon */
$icon = $mapper->handle($model); $icon = $mapper->handle($model);
@@ -150,7 +146,7 @@ class BootSubscriber implements EventSubscriberInterface
} }
if ($icons) { if ($icons) {
$buffer = sprintf('ContaoLeaflet.loadIcons(%s);', $encoder->encodeValue($icons)); $buffer = sprintf('ContaoLeaflet.loadIcons(%s);', json_encode($icons));
} }
$file = new \File('assets/leaflet/js/icons.js'); $file = new \File('assets/leaflet/js/icons.js');
@@ -158,7 +154,10 @@ class BootSubscriber implements EventSubscriberInterface
$file->close(); $file->close();
// TODO: Cache it. // TODO: Cache it.
$GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/js/icons.js|static'; $GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/js/icons.js' . (\Config::get('debugMode')
? ''
: '|static'
);
} }
} }
} }