diff --git a/README.md b/README.md index 5efa291..9b384fe 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,12 @@ pix_sortable_behavior: AppBundle/Entity/Baz: rang sortable_groups: entities: - AppBundle/Entity/Baz: [ group ] + AppBundle/Entity/Baz: [ group ] #Sortable groups goes here or in your doctrine mapping if you use Gedmo ``` #### Use a draggable list instead of up/down buttons -In order to use a draggable list instead of up/down buttons, change the template in the ```move``` action to ```AppBundle:Admin:_sort_drag_drop.html.twig```. +In order to use a draggable list instead of up/down buttons, change the template in the ```move``` action to ```PixSortableBehaviorBundle:Default:_sort_drag_drop.html.twig```. ```php add('_action', null, array( 'actions' => array( 'move' => array( - 'template' => 'AppBundle:Admin:_sort_drag_drop.html.twig', + 'template' => 'PixSortableBehaviorBundle:Default:_sort_drag_drop.html.twig', 'enable_top_bottom_buttons' => true, //optional + 'groups' => array('group') //only if you've defined a sortable group for your entity in the config ), ), )) @@ -78,8 +79,9 @@ pixSortableBehaviorBundle.error ->add('_action', null, array( 'actions' => array( 'move' => array( - 'template' => 'AppBundle:Admin:_sort.html.twig', - 'enable_top_bottom_buttons' => true, + 'template' => 'PixSortableBehaviorBundle:Default:_sort.html.twig', + 'enable_top_bottom_buttons' => false, //Disabled buttons + 'groups' => array('group') //only if you've defined a sortable group for your entity in the config ), ), )) diff --git a/Resources/public/js/init.js b/Resources/public/js/init.js index f2de988..5eb837e 100644 --- a/Resources/public/js/init.js +++ b/Resources/public/js/init.js @@ -37,6 +37,15 @@ DraggableTable.prototype.init = function (node, settings) { var moved = $(ui.item).find('.js-sortable-move'); var newPosition = ui.item.index(); + groups = moved.data('group'); + if (groups) { + var list = $(ui.item).parent().children() + group = list.filter(function() { + return $(this).find('.js-sortable-move').data("group") == groups; + }); + newPosition = group.index(ui.item); + } + $.ajax({ 'type': 'GET', 'url': moved.data('url').replace('NEW_POSITION', newPosition), @@ -45,10 +54,10 @@ DraggableTable.prototype.init = function (node, settings) { $(document).trigger("pixSortableBehaviorBundle.success", [data]); }, 'error': function(data) { - $(document).trigger("pixSortableBehaviorBundle.error",[data]); + $(document).trigger("pixSortableBehaviorBundle.error", [data]); } }); } }).disableSelection(); -}; \ No newline at end of file +}; diff --git a/Resources/views/Default/_sort_drag_drop.html.twig b/Resources/views/Default/_sort_drag_drop.html.twig index e88baed..018dd50 100644 --- a/Resources/views/Default/_sort_drag_drop.html.twig +++ b/Resources/views/Default/_sort_drag_drop.html.twig @@ -2,9 +2,13 @@ {% set current_position = currentObjectPosition(object) %} {% set last_position = lastPosition(object) %} {% set enable_top_bottom_buttons = field_description.options.actions.move.enable_top_bottom_buttons ?? true %} + {% if field_description.options.actions.move.groups is defined %} + {% set groups = field_description.options.actions.move.groups %} + {% endif %} - - + diff --git a/Services/PositionHandler.php b/Services/PositionHandler.php index 8e966d6..85e4de2 100644 --- a/Services/PositionHandler.php +++ b/Services/PositionHandler.php @@ -114,27 +114,19 @@ public function getPosition($object, $movePosition, $lastPosition) switch ($movePosition) { case 'up' : - if ($currentPosition > 0) { $newPosition = $currentPosition - 1; - } break; case 'down': - if ($currentPosition < $lastPosition) { $newPosition = $currentPosition + 1; - } break; case 'top': - if ($currentPosition > 0) { $newPosition = 0; - } break; case 'bottom': - if ($currentPosition < $lastPosition) { $newPosition = $lastPosition; - } break; default: @@ -143,6 +135,8 @@ public function getPosition($object, $movePosition, $lastPosition) } } + //Asserts the position is in the range + $newPosition = max(0, min($newPosition, $lastPosition)); return $newPosition; }