Skip to content

Commit 97dbaf1

Browse files
committed
fix select and getNode
1 parent 8492547 commit 97dbaf1

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ interface ICursorPosition<TDataType> {
127127
| getNode(path: number[]): ISlTreeNode | Find the node by using it's path |
128128
| traverse(cb: (node: ISlTreeNode, nodeModel: ISlTreeNodeModel, siblings: ISlTreeNodeModel[]) => boolean) | Helpful method to traverse all nodes. The traversing will be stopped if callback returns `false`. |
129129
| updateNode(path: number[], patch: Partial<ISlTreeNodeModel>) | Update the node by using it's path |
130-
| select(path: number[]) | Select the node by using it's path |
130+
| select(path: number[], addToSelection = false) | Select the node by using it's path |
131131
| getFirstNode(): ISlTreeNode | Get the first node in the tree |
132132
| getLastNode(): ISlTreeNode | Get the last node in the tree |
133133
| getNodeEl(): HTMLElement | Get the node HTMLElement by using it's path |

dist/sl-vue-tree.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ export default class SlVueTree<TDataType> extends Vue {
4141
getSelected(): ISlTreeNode<TDataType>[];
4242
traverse(cb: (node: ISlTreeNode<TDataType>, nodeModel: ISlTreeNodeModel<TDataType>, siblings: ISlTreeNodeModel<TDataType>[]) => boolean | void, nodeModels?: ISlTreeNodeModel<TDataType>[], parentPath?: number[]): ISlTreeNode<TDataType>[] | boolean;
4343
getNodeEl(path: number[]): HTMLElement;
44-
select(path: number[]): void;
44+
select(path: number[], addToSelection?: boolean): ISlTreeNode<TDataType>;
4545
}

dist/sl-vue-tree.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/sl-vue-tree.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sl-vue-tree.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ export default class SlVueTree<TDataType> extends Vue {
4141
getSelected(): ISlTreeNode<TDataType>[];
4242
traverse(cb: (node: ISlTreeNode<TDataType>, nodeModel: ISlTreeNodeModel<TDataType>, siblings: ISlTreeNodeModel<TDataType>[]) => boolean | void, nodeModels?: ISlTreeNodeModel<TDataType>[], parentPath?: number[]): ISlTreeNode<TDataType>[] | boolean;
4343
getNodeEl(path: number[]): HTMLElement;
44-
select(path: number[]): void;
44+
select(path: number[], addToSelection?: boolean): ISlTreeNode<TDataType>;
4545
}

src/sl-vue-tree.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ export default {
116116
) {
117117
const ind = path.slice(-1)[0];
118118
siblings = siblings || this.getNodeSiblings(this.value, path);
119-
nodeModel = nodeModel || siblings[ind];
119+
nodeModel = nodeModel || (siblings && siblings[ind]) || null;
120+
121+
if (!nodeModel) return null;
120122

121123
const node = {
122124

@@ -167,23 +169,24 @@ export default {
167169
this.getRoot().$emit('nodecontextmenu', node, event);
168170
},
169171

170-
select(path, event = null, addToSelection = false) {
172+
select(path, addToSelection = false, event = null) {
171173
addToSelection = ((event && event.ctrlKey) || addToSelection) && this.allowMultiselect;
172-
const clickedNode = this.getNode(path);
174+
const selectedNode = this.getNode(path);
175+
if (!selectedNode) return null;
173176
const newNodes = this.copy(this.value);
174-
const shiftSelectionMode = this.allowMultiselect && event.shiftKey && this.lastSelectedNode;
177+
const shiftSelectionMode = this.allowMultiselect && event && event.shiftKey && this.lastSelectedNode;
175178
const selectedNodes = [];
176179
let shiftSelectionStarted = false;
177180

178181
this.traverse((node, nodeModel) => {
179182

180183
if (shiftSelectionMode) {
181-
if (node.pathStr === clickedNode.pathStr || node.pathStr === this.lastSelectedNode.pathStr) {
184+
if (node.pathStr === selectedNode.pathStr || node.pathStr === this.lastSelectedNode.pathStr) {
182185
nodeModel.isSelected = true;
183186
shiftSelectionStarted = !shiftSelectionStarted;
184187
}
185188
if (shiftSelectionStarted) nodeModel.isSelected = true;
186-
} else if (node.pathStr === clickedNode.pathStr) {
189+
} else if (node.pathStr === selectedNode.pathStr) {
187190
nodeModel.isSelected = true;
188191
} else if (!addToSelection) {
189192
if (nodeModel.isSelected) nodeModel.isSelected = false;
@@ -194,9 +197,10 @@ export default {
194197
}, newNodes);
195198

196199

197-
this.lastSelectedNode = clickedNode;
200+
this.lastSelectedNode = selectedNode;
198201
this.emitInput(newNodes);
199202
this.emitSelect(selectedNodes, event);
203+
return selectedNode;
200204
},
201205

202206
onNodeMousemoveHandler(event) {
@@ -243,7 +247,7 @@ export default {
243247
const destNode = this.getNode(JSON.parse($nodeItem.getAttribute('path')));
244248

245249
if (isDragStarted && !destNode.isSelected) {
246-
this.select(destNode.path, event);
250+
this.select(destNode.path, false, event);
247251
}
248252

249253

@@ -357,7 +361,7 @@ export default {
357361
this.mouseIsDown = false;
358362

359363
if (!this.isDragging && targetNode) {
360-
this.select(targetNode.path, event);
364+
this.select(targetNode.path, false, event);
361365
return;
362366
}
363367

0 commit comments

Comments
 (0)