[Taxon] Make taxon sortable

This commit is contained in:
Arminek 2016-11-03 14:37:36 +01:00
parent 8e4e690335
commit 9c1052d4bc
12 changed files with 108 additions and 28 deletions

View file

@ -9,10 +9,10 @@ Feature: Reordering taxons
And the store classifies its products as "Belts" and "Wallets" And the store classifies its products as "Belts" and "Wallets"
And I am logged in as an administrator And I am logged in as an administrator
@todo @ui @javascript
Scenario: Changing order of the taxon Scenario: Changing order of the taxon
Given I want to see all taxons in store Given I want to see all taxons in store
When I want to move up "Watches" taxon When I want to move up "Watches" taxon
Then I should see 2 taxons on the list Then I should see 4 taxons on the list
And I should see the taxon named "T-Shirts" in the list And I should see the taxon named "T-Shirts" in the list
But the first taxon on the list should be "Watches" But the first taxon on the list should be "Watches"

View file

@ -141,6 +141,10 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface
*/ */
public function getFirstLeafName(TaxonInterface $parentTaxon = null) public function getFirstLeafName(TaxonInterface $parentTaxon = null)
{ {
$this->getDocument()->waitFor(5, function () {
return;
});
return $this->getLeafs($parentTaxon)[0]->getText(); return $this->getLeafs($parentTaxon)[0]->getText();
} }
@ -175,6 +179,9 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface
if ($leaf->getText() === $taxon->getName()) { if ($leaf->getText() === $taxon->getName()) {
$moveButton = $leaf->getParent()->find('css', sprintf('.%s', $direction)); $moveButton = $leaf->getParent()->find('css', sprintf('.%s', $direction));
$moveButton->click(); $moveButton->click();
$moveButton->waitFor(5, function () use ($moveButton) {
return !$moveButton->hasClass('loading');
});
return; return;
} }

View file

@ -1,7 +1,3 @@
sylius_admin_api_taxon:
resource: "@SyliusAdminBundle/Resources/config/routing/api/taxon.yml"
prefix: /taxons
sylius_admin_api_product: sylius_admin_api_product:
resource: "@SyliusAdminBundle/Resources/config/routing/api/product.yml" resource: "@SyliusAdminBundle/Resources/config/routing/api/product.yml"
prefix: /products prefix: /products

View file

@ -1,8 +0,0 @@
sylius_admin_api_taxon_index:
path: /
methods: [GET]
defaults:
_controller: sylius.controller.taxon:indexAction
_format: json
_sylius:
grid: sylius_admin_taxon

View file

@ -20,6 +20,14 @@ sylius_admin_taxon_index:
route: sylius_admin_taxon_create route: sylius_admin_taxon_create
permanent: true permanent: true
sylius_admin_taxon_move:
path: /taxons/{id}/move
methods: [PUT]
defaults:
_controller: sylius.controller.taxon:updateAction
_sylius:
form: sylius_taxon_position
sylius_admin_taxon_create_for_parent: sylius_admin_taxon_create_for_parent:
path: /taxons/new/{id} path: /taxons/new/{id}
defaults: defaults:

View file

@ -0,0 +1,33 @@
(function ($) {
'use strict';
$(document).ready(function() {
$('.sylius-taxon-move-up').api({
method: 'PUT',
beforeSend: function (settings) {
settings.data = {
position: $(this).data('position') - 1
};
return settings;
},
onSuccess: function (response) {
location.reload();
}
});
$('.sylius-taxon-move-down').api({
method: 'PUT',
beforeSend: function (settings) {
settings.data = {
position: $(this).data('position') + 1
};
return settings;
},
onSuccess: function (response) {
location.reload();
}
});
});
})( jQuery );

View file

@ -11,6 +11,7 @@
'use strict'; 'use strict';
$.fn.api.settings.api = { $.fn.api.settings.api = {
'get taxons' : '/admin/api/taxons' 'get taxons': '/admin/taxons?_format=json',
'move taxon': '/admin/taxons/{id}/move?_format=json'
}; };
})(jQuery); })(jQuery);

View file

@ -18,12 +18,12 @@
{{ buttons.create(path('sylius_admin_taxon_create_for_parent', { 'id': taxon.id }), 'sylius.ui.add'|trans) }} {{ buttons.create(path('sylius_admin_taxon_create_for_parent', { 'id': taxon.id }), 'sylius.ui.add'|trans) }}
{{ buttons.edit(path('sylius_admin_taxon_update', { 'id': taxon.id })) }} {{ buttons.edit(path('sylius_admin_taxon_update', { 'id': taxon.id })) }}
{{ buttons.delete(path('sylius_admin_taxon_delete', { 'id': taxon.id }), 'sylius.ui.delete'|trans) }} {{ buttons.delete(path('sylius_admin_taxon_delete', { 'id': taxon.id }), 'sylius.ui.delete'|trans) }}
<a class="ui icon button yellow" href="#"> <div class="ui icon button yellow sylius-taxon-move-up" data-action="move taxon" data-id="{{ taxon.id }}" data-position="{{ taxon.position }}">
<i class="icon arrow up"></i> <i class="icon arrow up"></i>
</a> </div>
<a class="ui icon button yellow" href="#"> <div class="ui icon button yellow sylius-taxon-move-down" data-action="move taxon" data-id="{{ taxon.id }}" data-position="{{ taxon.position }}">
<i class="icon arrow down"></i> <i class="icon arrow down"></i>
</a> </div>
</div> </div>
<div class="list"> <div class="list">
{{ tree.render(taxon.children) }} {{ tree.render(taxon.children) }}
@ -33,7 +33,6 @@
{% endfor %} {% endfor %}
{% endmacro %} {% endmacro %}
{{ buttons.create(path('sylius_admin_taxon_create')) }} {{ buttons.create(path('sylius_admin_taxon_create')) }}
<div class="ui segment"> <div class="ui segment">
<div id="taxons-list" class="ui list"> <div id="taxons-list" class="ui list">

View file

@ -53,7 +53,7 @@ class TaxonRepository extends EntityRepository implements TaxonRepositoryInterfa
->leftJoin('o.children', 'children') ->leftJoin('o.children', 'children')
->andWhere('o.parent = :parent') ->andWhere('o.parent = :parent')
->addOrderBy('o.root') ->addOrderBy('o.root')
->addOrderBy('o.left') ->addOrderBy('o.position')
->setParameter('parent', $taxon) ->setParameter('parent', $taxon)
; ;
@ -100,7 +100,7 @@ class TaxonRepository extends EntityRepository implements TaxonRepositoryInterfa
->leftJoin('o.translations', 'translation') ->leftJoin('o.translations', 'translation')
->where('translation.permalink = :permalink') ->where('translation.permalink = :permalink')
->setParameter('permalink', $permalink) ->setParameter('permalink', $permalink)
->orderBy('o.left') ->orderBy('o.position')
->getQuery() ->getQuery()
->getOneOrNullResult() ->getOneOrNullResult()
; ;
@ -131,6 +131,7 @@ class TaxonRepository extends EntityRepository implements TaxonRepositoryInterfa
$queryBuilder = $this->createQueryBuilder('o'); $queryBuilder = $this->createQueryBuilder('o');
$queryBuilder $queryBuilder
->andWhere($queryBuilder->expr()->isNull($this->getPropertyName('parent'))) ->andWhere($queryBuilder->expr()->isNull($this->getPropertyName('parent')))
->orderBy('o.position')
; ;
return $queryBuilder->getQuery()->getResult(); return $queryBuilder->getQuery()->getResult();
@ -144,7 +145,7 @@ class TaxonRepository extends EntityRepository implements TaxonRepositoryInterfa
$queryBuilder = $this->createQueryBuilder('o'); $queryBuilder = $this->createQueryBuilder('o');
$queryBuilder $queryBuilder
->orderBy('o.root') ->orderBy('o.root')
->addOrderBy('o.left') ->addOrderBy('o.position')
; ;
return $queryBuilder->getQuery()->getResult(); return $queryBuilder->getQuery()->getResult();

View file

@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\TaxonomyBundle\Form\Type;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
/**
* @author Arkadiusz Krakowiak <arkadiusz.krakowiak@lakion.com>
*/
final class TaxonPositionType extends AbstractResourceType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('position', IntegerType::class, [
'label' => 'sylius.form.taxon.position',
])
;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'sylius_taxon_position';
}
}

View file

@ -37,14 +37,10 @@
<cascade-all /> <cascade-all />
</cascade> </cascade>
<order-by> <order-by>
<order-by-field name="left" direction="ASC" /> <order-by-field name="position" direction="ASC" />
</order-by> </order-by>
</one-to-many> </one-to-many>
<order-by>
<order-by-field name="position" direction="ASC" />
</order-by>
<field name="code" column="code" type="string" unique="true" /> <field name="code" column="code" type="string" unique="true" />
<field name="left" column="tree_left" type="integer"> <field name="left" column="tree_left" type="integer">
<gedmo:tree-left /> <gedmo:tree-left />

View file

@ -17,6 +17,11 @@
<argument type="service" id="sylius.repository.taxon" /> <argument type="service" id="sylius.repository.taxon" />
<tag name="form.type" alias="sylius_taxon_choice" /> <tag name="form.type" alias="sylius_taxon_choice" />
</service> </service>
<service id="sylius.form.type.taxon_position" class="Sylius\Bundle\TaxonomyBundle\Form\Type\TaxonPositionType">
<argument>%sylius.model.taxon.class%</argument>
<argument>%sylius.validation_groups.taxon%</argument>
<tag name="form.type" alias="sylius_taxon_position" />
</service>
<service id="sylius.factory.taxon.decorated" class="Sylius\Component\Taxonomy\Factory\TaxonFactory" <service id="sylius.factory.taxon.decorated" class="Sylius\Component\Taxonomy\Factory\TaxonFactory"
decorates="sylius.factory.taxon" public="false"> decorates="sylius.factory.taxon" public="false">