primefaces: Tree: DnD Using `onDrop` controlled mode results in wrong child order

Describe the bug

The drag & drop feature of the tree component works fine, unless we have the following constellation:

Conditions:

  • using onDrop controlled mode instead of dropRestrict="sibling".
  • ajax event dragdrop with a listener but without update attribute and
  • the dragged item is dropped on the same level (reorder only) and
  • the dragged item is moved downwards (but not below the last item)

Scenario: Let there be three items [A, B, C]. To reproduce the bug you must move A one step down between B and C. When the “dragdrop” listener is called, we get a wrong child order:

  • Actual [B, C, A],
  • Expected [B, A, C]

Reproducer

PrimeFaces-Reproducer-11513.zip

Expected behavior

(see above)

PrimeFaces edition

Community

PrimeFaces version

13

Theme

irrelevant

JSF implementation

Mojarra

JSF version

4.0

Java version

21

Browser(s)

irrelevant

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Excellent! This workaround fixes the symptoms 👍

MonkeyPatch if you want to try it now…

if (PrimeFaces.widget.VerticalTree) {
    PrimeFaces.widget.VerticalTree.prototype.makeDropPoints = function(elements) {
        var $this = this,
            dragdropScope = this.cfg.dragdropScope || this.id;

        elements.droppable({
            hoverClass: 'ui-state-hover',
            accept: '.ui-treenode-content',
            tolerance: 'pointer',
            scope: dragdropScope,
            drop: function(event, ui) {
                var dragSource = PF($(ui.draggable.data('dragsourceid')).data('widget')),
                    dropSource = $this,
                    dropPoint = $(this),
                    dropNode = dropPoint.closest('li.ui-treenode-parent'),
                    dropNodeKey = $this.getRowKey(dropNode),
                    transfer = (dragSource.id !== dropSource.id),
                    draggedSourceKeys = dragSource.draggedSourceKeys,
                    isDroppedNodeCopy = ($this.cfg.dropCopyNode && $this.shiftKey),
                    draggedNodes,
                    dragNodeKey;

                if (draggedSourceKeys) {
                    draggedNodes = dragSource.findNodes(draggedSourceKeys);
                } else {
                    draggedNodes = [ui.draggable];
                }

                if ($this.cfg.controlled) {
                    $this.droppedNodeParams = [];
                }

                $this.invalidSourceKeys = [];

                for (var i = (draggedNodes.length - 1); i >= 0; i--) {
                    var draggedNode = $(draggedNodes[i]),
                        dragMode = ui.draggable.data('dragmode'),
                        dragNode = draggedNode.is('li.ui-treenode') ? draggedNode : draggedNode.closest('li.ui-treenode'),
                        dragNode = (isDroppedNodeCopy) ? dragNode.clone() : dragNode,
                        targetDragNode = $this.findTargetDragNode(dragNode, dragMode);

                    dragNodeKey = $this.getRowKey(targetDragNode);

                    if (!transfer && dropNodeKey && dropNodeKey.indexOf(dragNodeKey + '_') === 0) {
                        return;
                    }

                    if ($this.cfg.controlled) {
                        $this.droppedNodeParams.push({
                            'ui': ui,
                            'dragSource': dragSource,
                            'dragNode': dragNode,
                            'targetDragNode': targetDragNode,
                            'dropPoint': dropPoint,
                            'dropNode': dropNode,
                            'transfer': transfer
                        });
                    } else {
                        $this.onDropPoint(ui, dragSource, dragNode, targetDragNode, dropPoint, dropNode, transfer);
                    }
                }

                if (!draggedSourceKeys) {
                    draggedSourceKeys = [dragNodeKey];
                }
                draggedSourceKeys = draggedSourceKeys.filter(function(key) {
                    return $.inArray(key, $this.invalidSourceKeys) === -1;
                });

                // #11513 dndIndex is based on whether you are dragging up or down
                var dndIndex = dropPoint.prevAll('li.ui-treenode').length;
                if (parseInt(dragNodeKey) < dndIndex) {
                    var nextDndIndex = dropPoint.nextAll('li.ui-treenode').length;
                    if (nextDndIndex > 0) {
                        dndIndex = nextDndIndex;
                    }
                }

                if (draggedSourceKeys && draggedSourceKeys.length) {
                    draggedSourceKeys = draggedSourceKeys.reverse().join(',');
                    $this.fireDragDropEvent({
                        'dragNodeKey': draggedSourceKeys,
                        'dropNodeKey': dropNodeKey,
                        'dragSource': dragSource.id,
                        'dndIndex': dndIndex,
                        'transfer': transfer,
                        'isDroppedNodeCopy': isDroppedNodeCopy
                    });
                }

                dragSource.draggedSourceKeys = null;
                $this.invalidSourceKeys = null;

                if (isDroppedNodeCopy) {
                    $this.initDraggable();
                }
            }
        });
    }
}