[Taxon] Drag and drop support

This commit is contained in:
Arminek 2016-11-03 21:17:43 +01:00
parent 9c1052d4bc
commit 31b3aa1640
7 changed files with 99 additions and 22 deletions

View file

@ -1,7 +1,8 @@
{ {
"dependencies": { "dependencies": {
"jquery": "^2.2.0", "jquery": "^2.2.0",
"semantic-ui-css": "^2.2.0" "semantic-ui-css": "^2.2.0",
"sortablejs": "^1.4.2"
}, },
"devDependencies": { "devDependencies": {
"gulp": "^3.9.0", "gulp": "^3.9.0",

View file

@ -496,7 +496,7 @@ final class ManagingTaxonsContext implements Context
$this->createPage->getFirstLeafName(), $this->createPage->getFirstLeafName(),
$taxon->getName(), $taxon->getName(),
sprintf( sprintf(
'Expected %s as a first taxon, leaf but got %s.', 'Expected %s as a first taxon, but got %s.',
$taxon->getName(), $taxon->getName(),
$this->createPage->getFirstLeafName() $this->createPage->getFirstLeafName()
) )

View file

@ -141,10 +141,6 @@ 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();
} }
@ -177,10 +173,13 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface
$leafs = $this->getLeafs(); $leafs = $this->getLeafs();
foreach ($leafs as $leaf) { foreach ($leafs as $leaf) {
if ($leaf->getText() === $taxon->getName()) { if ($leaf->getText() === $taxon->getName()) {
$moveButton = $leaf->getParent()->find('css', sprintf('.%s', $direction)); $leaf = $leaf->getParent();
$menuButton = $leaf->find('css', '.wrench');
$menuButton->click();
$moveButton = $leaf->find('css', sprintf('.%s', $direction));
$moveButton->click(); $moveButton->click();
$moveButton->waitFor(5, function () use ($moveButton) { $moveButton->waitFor(5, function () use ($taxon) {
return !$moveButton->hasClass('loading'); return $this->getFirstLeafName() === $taxon->getName();
}); });
return; return;

View file

@ -17,6 +17,8 @@ var paths = {
admin: { admin: {
js: [ js: [
'../../../../node_modules/jquery/dist/jquery.min.js', '../../../../node_modules/jquery/dist/jquery.min.js',
'../../../../node_modules/sortablejs/Sortable.js',
'../../../../node_modules/sortablejs/jquery.binding.js',
'../../../../node_modules/semantic-ui-css/semantic.min.js', '../../../../node_modules/semantic-ui-css/semantic.min.js',
'../PromotionBundle/Resources/public/js/sylius-promotion.js', '../PromotionBundle/Resources/public/js/sylius-promotion.js',
'../ShippingBundle/Resources/public/js/**', '../ShippingBundle/Resources/public/js/**',

View file

@ -0,0 +1,31 @@
(function ($) {
'use strict';
$(document).ready(function() {
$('.sylius-delete-resource').api({
method: 'delete',
debug: true,
onSuccess: function (response) {
var redirectUrl = $(this).data('success-redirect-url');
if (redirectUrl) {
location.replace(redirectUrl);
return;
}
throw 'You need to define data-success-redirect-url on "' + $(this).attr('class') +'"';
},
onFailure: function (response) {
var message = $(this).parent().parent().popup({
inline: true,
content: response.error.message,
on: 'manual'
});
message.popup('show');
setTimeout(function() { message.popup('hide'); }, 3000);
}
});
});
})( jQuery );

View file

@ -2,8 +2,37 @@
'use strict'; 'use strict';
$(document).ready(function() { $(document).ready(function() {
$('.sylius-sortable-list').sortable({
onEnd: function (event) {
$(this).api({
throttle: 500,
method: 'PUT',
action: 'move taxon',
urlData: {
id: $(event.item).data('id')
},
on: 'now',
beforeSend: function (settings) {
settings.data = {
position: event.newIndex
};
return settings;
},
onSuccess: function (response) {
$(event.item).find('.sylius-taxon-move-up').data('position', event.newIndex);
$(event.item).find('.sylius-taxon-move-down').data('position', event.newIndex);
},
onFailure: function (response) {
throw 'Something went wrong with api call.';
}
});
}
});
$('.sylius-taxon-move-up').api({ $('.sylius-taxon-move-up').api({
method: 'PUT', method: 'PUT',
on: 'click',
beforeSend: function (settings) { beforeSend: function (settings) {
settings.data = { settings.data = {
position: $(this).data('position') - 1 position: $(this).data('position') - 1
@ -18,6 +47,7 @@
$('.sylius-taxon-move-down').api({ $('.sylius-taxon-move-down').api({
method: 'PUT', method: 'PUT',
on: 'click',
beforeSend: function (settings) { beforeSend: function (settings) {
settings.data = { settings.data = {
position: $(this).data('position') + 1 position: $(this).data('position') + 1

View file

@ -6,26 +6,40 @@
{% import _self as tree %} {% import _self as tree %}
{% for taxon in taxons if taxon.id != null %} {% for taxon in taxons if taxon.id != null %}
<div class="item"> <div class="item" data-id="{{ taxon.id }}">
<i class="folder icon"></i> <i class="folder icon"></i>
<div class="content"> <div class="content">
<div class="header"> <div class="header">
<a href="{{ path('sylius_admin_product_index_per_taxon', {'taxonId': taxon.id}) }}"> <a href="{{ path('sylius_admin_product_index_per_taxon', {'taxonId': taxon.id}) }}">
{{ taxon.name }} {{ taxon.name }}
</a> </a>
</div> <div class="ui inline dropdown icon button mini">
<div class="ui mini buttons"> <i class="wrench icon"></i>
{{ buttons.create(path('sylius_admin_taxon_create_for_parent', { 'id': taxon.id }), 'sylius.ui.add'|trans) }} <div class="menu">
{{ buttons.edit(path('sylius_admin_taxon_update', { 'id': taxon.id })) }} <a class="item" href="{{ path('sylius_admin_taxon_create_for_parent', { 'id': taxon.id }) }}">
{{ buttons.delete(path('sylius_admin_taxon_delete', { 'id': taxon.id }), 'sylius.ui.delete'|trans) }} <i class="plus icon"></i>
<div class="ui icon button yellow sylius-taxon-move-up" data-action="move taxon" data-id="{{ taxon.id }}" data-position="{{ taxon.position }}"> {{ 'sylius.ui.add'|trans }}
<i class="icon arrow up"></i> </a>
</div> <a class="item" href="{{ path('sylius_admin_taxon_update', { 'id': taxon.id }) }}">
<div class="ui icon button yellow sylius-taxon-move-down" data-action="move taxon" data-id="{{ taxon.id }}" data-position="{{ taxon.position }}"> <i class="edit icon"></i>
<i class="icon arrow down"></i> {{ 'sylius.ui.edit'|trans }}
</a>
<div class="item sylius-taxon-move-up" data-action="move taxon" data-id="{{ taxon.id }}" data-position="{{ taxon.position }}">
<i class="icon arrow up"></i>
{{ 'sylius.ui.move_up'|trans }}
</div>
<div class="item sylius-taxon-move-down" data-action="move taxon" data-id="{{ taxon.id }}" data-position="{{ taxon.position }}">
<i class="icon arrow down"></i>
{{ 'sylius.ui.move_down'|trans }}
</div>
<div class="item sylius-delete-resource" data-url="{{ path('sylius_admin_taxon_delete', { 'id': taxon.id }) ~ '?_format=json' }}" data-success-redirect-url="{{ path('sylius_admin_taxon_index') }}">
<i class="delete icon"></i>
{{ 'sylius.ui.delete'|trans }}
</div>
</div>
</div> </div>
</div> </div>
<div class="list"> <div class="list sylius-sortable-list">
{{ tree.render(taxon.children) }} {{ tree.render(taxon.children) }}
</div> </div>
</div> </div>
@ -35,7 +49,7 @@
{{ 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 class="ui list sylius-sortable-list">
{{ tree.render(taxons) }} {{ tree.render(taxons) }}
</div> </div>
</div> </div>