Skip to content

Commit 24dc1fd

Browse files
authored
Merge pull request #31 from rojtjo/master
Allow the drop to be cancelled
2 parents c415652 + 9dd8b81 commit 24dc1fd

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/sl-vue-tree.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ export default {
203203
this.getRoot().$emit('select', selectedNodes, event);
204204
},
205205

206+
emitBeforeDrop(draggingNodes, position, cancel) {
207+
this.getRoot().$emit('beforedrop', draggingNodes, position, cancel);
208+
},
209+
206210
emitDrop(draggingNodes, position, event) {
207211
this.getRoot().$emit('drop', draggingNodes, position, event);
208212
},
@@ -550,20 +554,35 @@ export default {
550554
if (this.checkNodeIsParent(draggingNode, this.cursorPosition.node)) {
551555
this.stopDrag();
552556
return;
553-
};
557+
}
554558
}
555559

556560
const newNodes = this.copy(this.currentValue);
557-
const nodeModelsToInsert = [];
561+
const nodeModelsSubjectToInsert = [];
558562

559-
// find and mark dragging model to delete
563+
// find dragging model to delete
560564
for (let draggingNode of draggingNodes) {
561565
const sourceSiblings = this.getNodeSiblings(newNodes, draggingNode.path);
562566
const draggingNodeModel = sourceSiblings[draggingNode.ind];
563-
nodeModelsToInsert.push(this.copy(draggingNodeModel));
564-
draggingNodeModel['_markToDelete'] = true;
567+
nodeModelsSubjectToInsert.push(draggingNodeModel);
568+
}
569+
570+
// allow the drop to be cancelled
571+
let cancelled = false;
572+
this.emitBeforeDrop(draggingNodes, this.cursorPosition, () => cancelled = true);
573+
574+
if (cancelled) {
575+
this.stopDrag();
576+
return;
565577
}
566578

579+
const nodeModelsToInsert = [];
580+
581+
// mark dragging model to delete
582+
for (let draggingNodeModel of nodeModelsSubjectToInsert) {
583+
nodeModelsToInsert.push(this.copy(draggingNodeModel));
584+
draggingNodeModel['_markToDelete'] = true;
585+
}
567586

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

0 commit comments

Comments
 (0)