Skip to content
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

Inserting nodes to dropping position #39

Merged
merged 3 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ interface ICursorPosition<TDataType> {
| select(path: number[], addToSelection = false) | Select the node by using its path | |
| getNodeEl(): HTMLElement | Get the node HTMLElement by using its path |
| getSelected(): ISlTreeNode[] | Get selected nodes |
| insert(position: ICursorPosition, nodeModel: ISlTreeNodeModel) | Insert nodes by the current cursor position. |
| remove(paths: number[][]) | Remove nodes by paths. For example `.remove([[0,1], [0,2]])`
| getFirstNode(): ISlTreeNode | Get the first node in the tree |
| getLastNode(): ISlTreeNode | Get the last node in the tree
Expand Down
1 change: 1 addition & 0 deletions demo/externaldrag.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ <h2> Sl-vue-tree - drag external elements</h2>
methods: {

onExternalDropHandler(cursorPosition, event) {
this.$refs.slVueTree.insert(cursorPosition, {title: "Dragged Item", isLeaf: true});
console.log('external drop', cursorPosition);
}

Expand Down
42 changes: 27 additions & 15 deletions src/sl-vue-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,21 +589,7 @@ export default {
}

// insert dragging nodes to the new place
const destNode = this.cursorPosition.node;
const destSiblings = this.getNodeSiblings(newNodes, destNode.path);
const destNodeModel = destSiblings[destNode.ind];

if (this.cursorPosition.placement === 'inside') {
destNodeModel.children = destNodeModel.children || [];
destNodeModel.children.unshift(...nodeModelsToInsert);
} else {
const insertInd = this.cursorPosition.placement === 'before' ?
destNode.ind :
destNode.ind + 1;

destSiblings.splice(insertInd, 0, ...nodeModelsToInsert);
}

this.insertModels(this.cursorPosition, nodeModelsToInsert, newNodes);


// delete dragging node from the old place
Expand Down Expand Up @@ -740,6 +726,32 @@ export default {
this.emitInput(newNodes);
},

insertModels(cursorPosition, nodeModels, newNodes) {
const destNode = cursorPosition.node;
const destSiblings = this.getNodeSiblings(newNodes, destNode.path);
const destNodeModel = destSiblings[destNode.ind];

if (cursorPosition.placement === 'inside') {
destNodeModel.children = destNodeModel.children || [];
destNodeModel.children.unshift(...nodeModels);
} else {
const insertInd = cursorPosition.placement === 'before' ?
destNode.ind :
destNode.ind + 1;

destSiblings.splice(insertInd, 0, ...nodeModels);
}
},

insert(cursorPosition, nodeModel) {
const nodeModels = Array.isArray(nodeModel) ? nodeModel : [nodeModel];
const newNodes = this.copy(this.currentValue);

this.insertModels(cursorPosition, nodeModels, newNodes);

this.emitInput(newNodes);
},

checkNodeIsParent(sourceNode, destNode) {
const destPath = destNode.path;
return JSON.stringify(destPath.slice(0, sourceNode.path.length)) == sourceNode.pathStr;
Expand Down