Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jquery Slider: Add range option and separate configs #326

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions Form/JQuery/Type/SliderType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
Expand All @@ -28,32 +30,37 @@ class SliderType extends AbstractType
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['configs'] = $options;
$view->vars['configs'] = $options['configs'];
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
$defaults = array(
'min' => 0,
'max' => 100,
'step' => 1,
'orientation' => 'horizontal'
));

$resolver->setAllowedValues(array(
'orientation' => array(
'horizontal',
'vertical'
)
));
'orientation' => 'horizontal',
'range' => false
);

$resolver
->setDefaults(array(
'configs' => $defaults
))
->setNormalizers(array(
'configs' => function (Options $options, $configs) use ($defaults) {
return array_merge($defaults, $configs);
},
))
;
}

/**
* {@inheritdoc}
*/
public function getParent()
{
return 'integer';
return 'text';
}

/**
Expand Down
28 changes: 18 additions & 10 deletions Resources/views/Form/jquery_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,27 @@

{% block genemu_jqueryslider_javascript %}
{% spaceless %}
<script type="text/javascript">
jQuery(document).ready(function($) {
var $field = $('#{{ id }}');
var $configs = $.extend({{ configs|json_encode|raw }}, {
<script type="text/javascript">
jQuery(document).ready(function($) {
var $field = $('#{{ id }}');
var $configs = $.extend({{ configs|json_encode|raw }}, {
{% if configs.range %}
values: {{ value ? value : 0 }},
{% else %}
value: {{ value ? value : 0 }},
slide: function(event, ui) {
{%endif %}
slide: function(event, ui) {
{% if configs.range %}
$field.val(JSON.stringify(ui.values));
{% else %}
$field.val(ui.value);
}
});

$('#{{ id }}_slider').slider($configs);
{%endif %}
}
});
</script>

$('#{{ id }}_slider').slider($configs);
});
</script>
{% endspaceless %}
{% endblock genemu_jqueryslider_javascript %}

Expand Down