Skip to content

Commit

Permalink
Updated to work with bootstrap v3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo101 committed Sep 21, 2015
1 parent 43bb407 commit 8b416e3
Show file tree
Hide file tree
Showing 9 changed files with 626 additions and 176 deletions.
39 changes: 39 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

class Configuration implements ConfigurationInterface
{
const DEFAULT_BSJS_FILE = '%kernel.root_dir%/../src/Lifo/TypeaheadBundle/Resources/public/js/bootstrap-typeahead.js';
const DEFAULT_JS_FILE = '%kernel.root_dir%/../src/Lifo/TypeaheadBundle/Resources/public/js/typeaheadbundle.js';
const DEFAULT_JS_OUTPUT = 'js/lifo_typeahead.js';
const DEFAULT_CSS_FILE = '%kernel.root_dir%/../src/Lifo/TypeaheadBundle/Resources/public/css/typeaheadbundle.css';
const DEFAULT_CSS_OUTPUT = 'css/lifo_typeahead.css';

/**
* {@inheritDoc}
*/
Expand All @@ -15,6 +21,39 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('lifo_typeahead');

$rootNode
->children()
->arrayNode('auto_configure')
->info('Automatically configure subsystems?')
->addDefaultsIfNotSet()
->children()
->booleanNode('assetic')->defaultTrue()->end()
->booleanNode('twig')->defaultTrue()->end()
->end()
->end()
->scalarNode('typeahead_js_file')
->defaultValue(self::DEFAULT_JS_FILE)
->info('(for assetic) Location of the typeaheadbundle.js file (normally the file that comes with lifo/typeahead-bundle')
->end()
->scalarNode('typeahead_bsjs_file')
->defaultValue(self::DEFAULT_BSJS_FILE)
->info('(for assetic) Location of the bootstrap-typeahead.js file (normally the file that comes with lifo/typeahead-bundle')
->end()
->scalarNode('typeahead_js_output')
->defaultValue(self::DEFAULT_JS_OUTPUT)
->info('(for assetic) Output location for typeahead JS code. Should be relative to your web directory.')
->end()
->scalarNode('typeahead_css_file')
->defaultValue(self::DEFAULT_CSS_FILE)
->info('(for assetic) Location of the typeaheadbundle.css file (normally the file that comes with lifo/typeahead-bundle')
->end()
->scalarNode('typeahead_css_output')
->defaultValue(self::DEFAULT_CSS_OUTPUT)
->info('(for assetic) Output location for typeahead CSS styles. Should be relative to your web directory.')
->end()
->end()
;

return $treeBuilder;
}
}
84 changes: 73 additions & 11 deletions DependencyInjection/LifoTypeaheadExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,87 @@
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Kernel;

class LifoTypeaheadExtension extends Extension
//implements PrependExtensionInterface
class LifoTypeaheadExtension extends Extension implements PrependExtensionInterface
{

public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
// $configuration = new Configuration();
// $config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}

//public function prepend(ContainerBuilder $container)
//{
// $configs = $container->getExtensionConfig($this->getAlias());
// $config = $this->processConfiguration(new Configuration(), $configs);
// $container->prependExtensionConfig('twig', $config);
//}
/**
* {@inheritDoc}
*/
public function prepend(ContainerBuilder $container)
{
$bundles = $container->getParameter('kernel.bundles');

$configs = $container->getExtensionConfig($this->getAlias());
$config = $this->processConfiguration(new Configuration(), $configs);

// "lifo_typeahead: auto_configure: assetic: true"
if (isset($bundles['AsseticBundle']) && $config['auto_configure']['assetic']) {
$this->configureAsseticBundle($container, $config);
}

// "lifo_typeahead: auto_configure: twig: true"
if (isset($bundles['TwigBundle']) && $config['auto_configure']['twig']) {
$this->configureTwigBundle($container, $config);
}
}

/**
* @param ContainerBuilder $container The service container
* @param array $config The bundle configuration
*
* @return void
*/
protected function configureAsseticBundle(ContainerBuilder $container, array $config)
{
if ($container->hasExtension('assetic')) {
$container->prependExtensionConfig('assetic', array(
'assets' => array(
// apply in twig template via a javascripts tag using "@lifo_typeahead_js"
'lifo_typeahead_js' => array(
'inputs' => array(
$config['typeahead_bsjs_file'],
$config['typeahead_js_file'],
),
'output' => $config['typeahead_js_output'],
),
// apply in twig template via a stylesheets tag using "@lifo_typeahead_css"
'lifo_typeahead_css' => array(
'inputs' => array(
$config['typeahead_css_file'],
),
'output' => $config['typeahead_css_output'],
)
)
));
}
}

/**
* @param ContainerBuilder $container The service container
* @param array $config The bundle configuration
*
* @return void
*/
protected function configureTwigBundle(ContainerBuilder $container, array $config)
{
if ($container->hasExtension('twig')) {
$resources = array('LifoTypeaheadBundle:Form:typeahead.html.twig');
if (Kernel::VERSION_ID >= '20600') {
$container->prependExtensionConfig('twig', array('form_themes' => $resources));
} else {
$container->prependExtensionConfig('twig', array('form' => array('resources' => $resources)));
}
}
}
}
7 changes: 4 additions & 3 deletions Form/Type/TypeaheadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)

// assign some variables to the view template
$vars = array('render', 'route', 'route_params', 'property',
'minLength', 'items', 'delay', 'loadingIconUrl',
'minLength', 'items', 'delay', 'spinner',
'multiple', 'allow_add', 'allow_remove', 'empty_value',
'resetOnSelect', 'callback');
foreach ($vars as $var) {
Expand All @@ -73,7 +73,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
try {
$params = $options['route_params'] ?: array();
if (!is_array($params) and !($params instanceof \Traversable)) {
throw new UnexpectedTypeException($params, "array or \Traversable");
throw new UnexpectedTypeException($params, "array or \\Traversable");
}
$view->vars['url'] = $this->router->generate($options['route'], $params);
} catch (\InvalidArgumentException $e) {
Expand All @@ -82,6 +82,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
}
}

// public function configureOptions(OptionsResolver $resolver) // sf2.6+
public function setDefaultOptions(OptionsResolverInterface $resolver)
{

Expand All @@ -101,7 +102,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'delay' => 250,
'minLength' => 2,
'items' => 10,
'loadingIconUrl' => null,
'spinner' => 'glyphicon glyphicon-refresh spin',
'resetOnSelect' => function(Options $options) { return $options['multiple']; },
'callback' => null,

Expand Down
Loading

0 comments on commit 8b416e3

Please sign in to comment.