Skip to content

Allow the drop to be cancelled #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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