Skip to content

Commit

Permalink
Merge pull request #31 from rojtjo/master
Browse files Browse the repository at this point in the history
Allow the drop to be cancelled
  • Loading branch information
holiber authored Nov 5, 2018
2 parents c415652 + 9dd8b81 commit 24dc1fd
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/sl-vue-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ export default {
this.getRoot().$emit('select', selectedNodes, event);
},

emitBeforeDrop(draggingNodes, position, cancel) {
this.getRoot().$emit('beforedrop', draggingNodes, position, cancel);
},

emitDrop(draggingNodes, position, event) {
this.getRoot().$emit('drop', draggingNodes, position, event);
},
Expand Down Expand Up @@ -550,20 +554,35 @@ export default {
if (this.checkNodeIsParent(draggingNode, this.cursorPosition.node)) {
this.stopDrag();
return;
};
}
}

const newNodes = this.copy(this.currentValue);
const nodeModelsToInsert = [];
const nodeModelsSubjectToInsert = [];

// find and mark dragging model to delete
// find dragging model to delete
for (let draggingNode of draggingNodes) {
const sourceSiblings = this.getNodeSiblings(newNodes, draggingNode.path);
const draggingNodeModel = sourceSiblings[draggingNode.ind];
nodeModelsToInsert.push(this.copy(draggingNodeModel));
draggingNodeModel['_markToDelete'] = true;
nodeModelsSubjectToInsert.push(draggingNodeModel);
}

// allow the drop to be cancelled
let cancelled = false;
this.emitBeforeDrop(draggingNodes, this.cursorPosition, () => cancelled = true);

if (cancelled) {
this.stopDrag();
return;
}

const nodeModelsToInsert = [];

// mark dragging model to delete
for (let draggingNodeModel of nodeModelsSubjectToInsert) {
nodeModelsToInsert.push(this.copy(draggingNodeModel));
draggingNodeModel['_markToDelete'] = true;
}

// insert dragging nodes to the new place
const destNode = this.cursorPosition.node;
Expand Down

0 comments on commit 24dc1fd

Please sign in to comment.