Skip to content

Commit

Permalink
Merge pull request #2765 from aRn0D/attribute_fix
Browse files Browse the repository at this point in the history
Attribute choices management
  • Loading branch information
Paweł Jędrzejewski committed May 23, 2015
2 parents 83c7cab + ec6c001 commit a240458
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,24 @@ public function buildChoices(FormEvent $event)
return;
}

$type = $attribute->getType();
$data = null;
$config = $attribute->getConfiguration();

if (null === $type || AttributeTypes::CHOICE === $type) {
$data = null;
$config = $attribute->getConfiguration();

if (!empty($config['choices'])) {
$data = $config['choices'];
}

$event->getForm()->add(
$this->factory->createNamed('choices', 'collection', null, array(
'label' => 'sylius.form.attribute.choices',
'type' => 'text',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'auto_initialize' => false,
'mapped' => false,
'data' => $data,
)
));
if (!empty($config['choices'])) {
$data = $config['choices'];
}

$event->getForm()->add(
$this->factory->createNamed('choices', 'collection', null, array(
'label' => 'sylius.form.attribute.choices',
'type' => 'text',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'auto_initialize' => false,
'mapped' => false,
'data' => $data,
)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,6 @@ function it_does_no_not_build_choices_collection_for_null(
$this->buildChoices($event);
}

function it_builds_choices_collection_for_new_object_without_type(
FormEvent $event,
Form $form,
AttributeInterface $attribute,
Form $collectionField,
$formFactory
) {
$event->getData()->willReturn($attribute);
$event->getForm()->willReturn($form);

$attribute->getType()->willReturn(null);
$attribute->getConfiguration()->willReturn(array());

$formFactory
->createNamed('choices', 'collection', null, array(
'label' => 'sylius.form.attribute.choices',
'type' => 'text',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'auto_initialize' => false,
'mapped' => false,
'data' => null,
))
->willReturn($collectionField)
->shouldBeCalled()
;
$form->add($collectionField)->shouldBeCalled()->willReturn($form);

$this->buildChoices($event);
}

function it_builds_choices_collection_for_choice_attribute(
FormEvent $event,
Form $form,
Expand All @@ -96,7 +64,6 @@ function it_builds_choices_collection_for_choice_attribute(
$event->getData()->willReturn($attribute);
$event->getForm()->willReturn($form);

$attribute->getType()->willReturn(AttributeTypes::CHOICE);
$attribute->getConfiguration()->willReturn(array());

$formFactory
Expand All @@ -118,28 +85,6 @@ function it_builds_choices_collection_for_choice_attribute(
$this->buildChoices($event);
}

function it_does_not_build_choices_collection_for_other_than_choice_attribute_types(
FormEvent $event,
Form $form,
AttributeInterface $attribute,
Form $collectionField,
$formFactory
) {
$attribute->getType()->willReturn(AttributeTypes::TEXT);

$event->getData()->willReturn($attribute);
$event->getForm()->willReturn($form);

$formFactory
->createNamed('choices', 'collection', null, Argument::any())
->willReturn($collectionField)
->shouldNotBeCalled()
;
$form->add(Argument::any())->shouldNotBeCalled();

$this->buildChoices($event);
}

function it_build_configuration_collection_type(
FormEvent $event,
Form $form,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,27 @@
(function ( $ ) {
'use strict';

$(document).ready(function() {
toogleChoices($('#sylius_product_attribute_type').val());
$('#sylius_product_attribute_type').change(function (e) {
toogleChoices($(this).val());
});
$('.delete-link').each(function () {
var removeLink = $(this);
removeLink.on('click', function(e) {
e.preventDefault();
var attributeController = {
choicesContainer: $('#sylius_form_choice_container'),
choicesElement: $('#sylius_product_attribute_type'),

removeLink.parent().parent().remove();
});
});
$('a[data-collection-button="add"]').on('click', function (e) {
e.preventDefault();
listen: function()
{
this.choicesElement.change($.proxy(this.toogleChoices, this));
this.toogleChoices();
},

var collectionContainer = $('#' + $(this).data('collection'));
var item = $('#' + $(this).data('collection') + ' .control-group:last-child');
var removeLink = $('<a class="btn btn-danger sylius_product_attribute_choices_' + (collectionContainer.children().length - 1) + '_delete" href="#"><i class="icon-trash"></i></a>');
removeLink.on('click', function(e) {
e.preventDefault();
toogleChoices: function()
{
if ('choice' === this.choicesElement.val()) {
this.choicesContainer.show();
} else {
this.choicesContainer.hide();
}
}
};

item.remove();
});
item.find('.controls').append(removeLink);
});
$(document).ready(function() {
attributeController.listen();
});

function toogleChoices(value)
{
if (value === 'choice') {
$('.attribute-choices-container').show();
} else {
$('.attribute-choices-container').hide();
}
}
})( jQuery );
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
{{ form_row(form.name, {'attr': {'class': 'input-lg'}}) }}
{{ form_row(form.translations, {'attr': {'class': 'input-lg'}}) }}
{{ form_row(form.type, {'attr': {'class': 'input-lg'}}) }}
{% if form.choices is defined %}
{{ form_row(form.choices) }}
{% endif %}
<div id="sylius_form_choice_container" class="hide">
{% if form.choices is defined %}
{{ form_row(form.choices) }}
{% endif %}
</div>
</fieldset>

{{ form_widget(form._token) }}

0 comments on commit a240458

Please sign in to comment.