From ec6c001cfb699a5ddef1601b249f30c24a9f77f6 Mon Sep 17 00:00:00 2001 From: Arnaud Langlade Date: Fri, 22 May 2015 19:14:03 +0200 Subject: [PATCH] Attribute choices management --- .../BuildAttributeFormChoicesListener.php | 38 ++++++------- .../BuildAttributeFormChoicesListenerSpec.php | 55 ------------------- .../public/js/sylius-attribute-choices.js | 51 +++++++---------- .../Backend/ProductAttribute/_form.html.twig | 8 ++- 4 files changed, 41 insertions(+), 111 deletions(-) diff --git a/src/Sylius/Bundle/AttributeBundle/Form/EventListener/BuildAttributeFormChoicesListener.php b/src/Sylius/Bundle/AttributeBundle/Form/EventListener/BuildAttributeFormChoicesListener.php index cb9ebae6ff3..168ece374ee 100644 --- a/src/Sylius/Bundle/AttributeBundle/Form/EventListener/BuildAttributeFormChoicesListener.php +++ b/src/Sylius/Bundle/AttributeBundle/Form/EventListener/BuildAttributeFormChoicesListener.php @@ -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, + ) + )); } } diff --git a/src/Sylius/Bundle/AttributeBundle/spec/Form/EventListener/BuildAttributeFormChoicesListenerSpec.php b/src/Sylius/Bundle/AttributeBundle/spec/Form/EventListener/BuildAttributeFormChoicesListenerSpec.php index 0f7ff9d11e4..0664d1f040e 100644 --- a/src/Sylius/Bundle/AttributeBundle/spec/Form/EventListener/BuildAttributeFormChoicesListenerSpec.php +++ b/src/Sylius/Bundle/AttributeBundle/spec/Form/EventListener/BuildAttributeFormChoicesListenerSpec.php @@ -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, @@ -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 @@ -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, diff --git a/src/Sylius/Bundle/WebBundle/Resources/public/js/sylius-attribute-choices.js b/src/Sylius/Bundle/WebBundle/Resources/public/js/sylius-attribute-choices.js index 313cc9663ce..f4203bcd533 100644 --- a/src/Sylius/Bundle/WebBundle/Resources/public/js/sylius-attribute-choices.js +++ b/src/Sylius/Bundle/WebBundle/Resources/public/js/sylius-attribute-choices.js @@ -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 = $(''); - 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 ); diff --git a/src/Sylius/Bundle/WebBundle/Resources/views/Backend/ProductAttribute/_form.html.twig b/src/Sylius/Bundle/WebBundle/Resources/views/Backend/ProductAttribute/_form.html.twig index 4c5a9075500..7401255af66 100644 --- a/src/Sylius/Bundle/WebBundle/Resources/views/Backend/ProductAttribute/_form.html.twig +++ b/src/Sylius/Bundle/WebBundle/Resources/views/Backend/ProductAttribute/_form.html.twig @@ -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 %} +
+ {% if form.choices is defined %} + {{ form_row(form.choices) }} + {% endif %} +
{{ form_widget(form._token) }}