Skip to content

Commit

Permalink
[Taxon] Drag and drop support
Browse files Browse the repository at this point in the history
  • Loading branch information
Arminek committed Nov 7, 2016
1 parent 9c1052d commit 31b3aa1
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 22 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"dependencies": {
"jquery": "^2.2.0",
"semantic-ui-css": "^2.2.0"
"semantic-ui-css": "^2.2.0",
"sortablejs": "^1.4.2"
},
"devDependencies": {
"gulp": "^3.9.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public function theFirstTaxonOnTheListShouldBe(TaxonInterface $taxon)
$this->createPage->getFirstLeafName(),
$taxon->getName(),
sprintf(
'Expected %s as a first taxon, leaf but got %s.',
'Expected %s as a first taxon, but got %s.',
$taxon->getName(),
$this->createPage->getFirstLeafName()
)
Expand Down
13 changes: 6 additions & 7 deletions src/Sylius/Behat/Page/Admin/Taxon/CreatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ public function moveDown(TaxonInterface $taxon)
*/
public function getFirstLeafName(TaxonInterface $parentTaxon = null)
{
$this->getDocument()->waitFor(5, function () {
return;
});

return $this->getLeafs($parentTaxon)[0]->getText();
}

Expand Down Expand Up @@ -177,10 +173,13 @@ private function moveLeaf(TaxonInterface $taxon, $direction)
$leafs = $this->getLeafs();
foreach ($leafs as $leaf) {
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->waitFor(5, function () use ($moveButton) {
return !$moveButton->hasClass('loading');
$moveButton->waitFor(5, function () use ($taxon) {
return $this->getFirstLeafName() === $taxon->getName();
});

return;
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Bundle/AdminBundle/Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var paths = {
admin: {
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',
'../PromotionBundle/Resources/public/js/sylius-promotion.js',
'../ShippingBundle/Resources/public/js/**',
Expand Down
Original file line number Diff line number Diff line change
@@ -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 );
30 changes: 30 additions & 0 deletions src/Sylius/Bundle/AdminBundle/Resources/private/js/move-taxon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,37 @@
'use strict';

$(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({
method: 'PUT',
on: 'click',
beforeSend: function (settings) {
settings.data = {
position: $(this).data('position') - 1
Expand All @@ -18,6 +47,7 @@

$('.sylius-taxon-move-down').api({
method: 'PUT',
on: 'click',
beforeSend: function (settings) {
settings.data = {
position: $(this).data('position') + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,40 @@
{% import _self as tree %}

{% for taxon in taxons if taxon.id != null %}
<div class="item">
<div class="item" data-id="{{ taxon.id }}">
<i class="folder icon"></i>
<div class="content">
<div class="header">
<a href="{{ path('sylius_admin_product_index_per_taxon', {'taxonId': taxon.id}) }}">
{{ taxon.name }}
</a>
</div>
<div class="ui mini buttons">
{{ 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.delete(path('sylius_admin_taxon_delete', { 'id': taxon.id }), 'sylius.ui.delete'|trans) }}
<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>
</div>
<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>
<div class="ui inline dropdown icon button mini">
<i class="wrench icon"></i>
<div class="menu">
<a class="item" href="{{ path('sylius_admin_taxon_create_for_parent', { 'id': taxon.id }) }}">
<i class="plus icon"></i>
{{ 'sylius.ui.add'|trans }}
</a>
<a class="item" href="{{ path('sylius_admin_taxon_update', { 'id': taxon.id }) }}">
<i class="edit icon"></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 class="list">
<div class="list sylius-sortable-list">
{{ tree.render(taxon.children) }}
</div>
</div>
Expand All @@ -35,7 +49,7 @@

{{ buttons.create(path('sylius_admin_taxon_create')) }}
<div class="ui segment">
<div id="taxons-list" class="ui list">
<div class="ui list sylius-sortable-list">
{{ tree.render(taxons) }}
</div>
</div>

0 comments on commit 31b3aa1

Please sign in to comment.