Update state based on selection

pull/1088/head
Cagatay Civici 2021-04-09 17:26:23 +03:00
parent 7996b2208d
commit 3d72768811
1 changed files with 55 additions and 12 deletions

View File

@ -88,22 +88,31 @@ export default {
} }
}, },
watch: { watch: {
modelValue() { modelValue: {
this.touched = false; handler: function() {
if (!this.selfChange) {
this.updateTreeState();
}
this.selfChange = false;
},
immediate: true
},
options() {
this.updateTreeState();
} }
}, },
data() { data() {
return { return {
focused: false, focused: false,
overlayVisible: false, overlayVisible: false,
expandedKeys: {}, expandedKeys: {}
touched: false
}; };
}, },
outsideClickListener: null, outsideClickListener: null,
resizeListener: null, resizeListener: null,
scrollHandler: null, scrollHandler: null,
overlay: null, overlay: null,
selfChange: false,
beforeUnmount() { beforeUnmount() {
this.unbindOutsideClickListener(); this.unbindOutsideClickListener();
this.unbindResizeListener(); this.unbindResizeListener();
@ -114,6 +123,9 @@ export default {
} }
this.overlay = null; this.overlay = null;
}, },
mounted() {
this.updateTreeState();
},
methods: { methods: {
show() { show() {
this.$emit('before-show'); this.$emit('before-show');
@ -140,6 +152,7 @@ export default {
} }
}, },
onSelectionChange(keys) { onSelectionChange(keys) {
this.selfChange = true;
this.$emit('update:modelValue', keys); this.$emit('update:modelValue', keys);
this.$emit('change', keys); this.$emit('change', keys);
}, },
@ -155,7 +168,6 @@ export default {
}, },
onNodeToggle(keys) { onNodeToggle(keys) {
this.expandedKeys = keys; this.expandedKeys = keys;
this.touched = true;
}, },
onKeyDown(event) { onKeyDown(event) {
switch(event.which) { switch(event.which) {
@ -271,30 +283,61 @@ export default {
target: this.$el target: this.$el
}); });
}, },
searchBranch(node, parentNode, keys, selectedNodes) { findSelectedNodes(node, keys, selectedNodes) {
if (node) { if (node) {
if (this.isSelected(node, keys)) { if (this.isSelected(node, keys)) {
selectedNodes.push(node); selectedNodes.push(node);
if (parentNode && !this.touched) {
this.expandedKeys[parentNode.key] = true;
}
delete keys[node.key]; delete keys[node.key];
} }
if (Object.keys(keys).length && node.children) { if (Object.keys(keys).length && node.children) {
for (let childNode of node.children) { for (let childNode of node.children) {
this.searchBranch(childNode, node, keys, selectedNodes); this.findSelectedNodes(childNode, keys, selectedNodes);
} }
} }
} }
else { else {
for (let childNode of this.options) { for (let childNode of this.options) {
this.searchBranch(childNode, null, keys, selectedNodes); this.findSelectedNodes(childNode, keys, selectedNodes);
} }
} }
}, },
isSelected(node, keys) { isSelected(node, keys) {
return this.selectionMode === 'checkbox' ? keys[node.key] && keys[node.key].checked : keys[node.key]; return this.selectionMode === 'checkbox' ? keys[node.key] && keys[node.key].checked : keys[node.key];
},
updateTreeState() {
let keys = {...this.modelValue};
this.expandedKeys = {};
if (keys && this.options) {
this.updateTreeBranchState(null, null, keys);
}
},
updateTreeBranchState(node, path, keys) {
if (node) {
if (this.isSelected(node, keys)) {
this.expandPath(path);
delete keys[node.key];
}
if (Object.keys(keys).length && node.children) {
for (let childNode of node.children) {
path.push(node.key);
this.updateTreeBranchState(childNode, path, keys);
}
}
}
else {
for (let childNode of this.options) {
this.updateTreeBranchState(childNode, [], keys);
}
}
},
expandPath(path) {
if (path.length > 0) {
for (let key of path) {
this.expandedKeys[key] = true;
}
}
} }
}, },
computed: { computed: {
@ -323,7 +366,7 @@ export default {
let selectedNodes = []; let selectedNodes = [];
if (this.modelValue && this.options) { if (this.modelValue && this.options) {
let keys = {...this.modelValue}; let keys = {...this.modelValue};
this.searchBranch(null, null, keys, selectedNodes); this.findSelectedNodes(null, keys, selectedNodes);
} }
return selectedNodes; return selectedNodes;