Improvement
This commit is contained in:
parent
722f1e8a03
commit
9e264e6f1f
|
@ -6,6 +6,7 @@ import { useRoute } from 'vue-router';
|
||||||
import { useState } from 'src/composables/useState';
|
import { useState } from 'src/composables/useState';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { useArrayData } from 'composables/useArrayData';
|
import { useArrayData } from 'composables/useArrayData';
|
||||||
|
import { tree } from 'quasar/dist/icon-set/material-icons.umd.prod';
|
||||||
|
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
|
@ -24,15 +25,25 @@ const nodes = ref([
|
||||||
{ id: null, name: t('zoneLocations.locations'), sons: true, children: [{}] },
|
{ id: null, name: t('zoneLocations.locations'), sons: true, children: [{}] },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const fetchedChildrensSet = ref(new Set());
|
const previousExpandedNodes = ref(new Set());
|
||||||
|
|
||||||
const onNodeExpanded = (nodeKeysArray) => {
|
const onNodeExpanded = async (nodeKeysArray) => {
|
||||||
if (!fetchedChildrensSet.value.has(nodeKeysArray.at(-1))) {
|
const nodeKeysSet = new Set(nodeKeysArray);
|
||||||
fetchedChildrensSet.value.add(nodeKeysArray.at(-1));
|
const lastNodeKey = nodeKeysArray.at(-1);
|
||||||
fetchNodeLeaves(nodeKeysArray.at(-1));
|
const wasExpanded = !previousExpandedNodes.value.has(lastNodeKey);
|
||||||
|
|
||||||
|
if (wasExpanded) await fetchNodeLeaves(lastNodeKey);
|
||||||
|
else {
|
||||||
|
const difference = new Set(
|
||||||
|
[...previousExpandedNodes.value].filter((x) => !nodeKeysSet.has(x))
|
||||||
|
);
|
||||||
|
const collapsedNode = Array.from(difference).pop();
|
||||||
|
const node = treeRef.value?.getNodeByKey(collapsedNode);
|
||||||
|
const allNodeIds = getNodeIds(node);
|
||||||
|
expanded.value = expanded.value.filter((id) => !allNodeIds.includes(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
state.set('Tree', nodeKeysArray);
|
previousExpandedNodes.value = nodeKeysSet;
|
||||||
};
|
};
|
||||||
|
|
||||||
const formatNodeSelected = (node) => {
|
const formatNodeSelected = (node) => {
|
||||||
|
@ -43,7 +54,6 @@ const formatNodeSelected = (node) => {
|
||||||
const fetchNodeLeaves = async (nodeKey) => {
|
const fetchNodeLeaves = async (nodeKey) => {
|
||||||
try {
|
try {
|
||||||
const node = treeRef.value?.getNodeByKey(nodeKey);
|
const node = treeRef.value?.getNodeByKey(nodeKey);
|
||||||
|
|
||||||
if (!node || node.sons === 0) return;
|
if (!node || node.sons === 0) return;
|
||||||
|
|
||||||
const params = { parentId: node.id };
|
const params = { parentId: node.id };
|
||||||
|
@ -86,8 +96,9 @@ function getNodeIds(node) {
|
||||||
let ids = [];
|
let ids = [];
|
||||||
if (node.id) ids.push(node.id);
|
if (node.id) ids.push(node.id);
|
||||||
|
|
||||||
if (node.childs) {
|
const children = node.child || node.children;
|
||||||
node.childs.forEach((child) => {
|
if (children) {
|
||||||
|
children.forEach((child) => {
|
||||||
ids = ids.concat(getNodeIds(child));
|
ids = ids.concat(getNodeIds(child));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -110,7 +121,8 @@ onMounted(async () => {
|
||||||
await arrayData.fetch({ append: false });
|
await arrayData.fetch({ append: false });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const tree = [...state.get('Tree'), 1];
|
const stateTree = state.get('Tree');
|
||||||
|
const tree = stateTree ? [...state.get('Tree'), 1] : [null, 1];
|
||||||
const lastStateTree = state.get('TreeState');
|
const lastStateTree = state.get('TreeState');
|
||||||
if (tree) {
|
if (tree) {
|
||||||
for (let n of tree) {
|
for (let n of tree) {
|
||||||
|
|
Loading…
Reference in New Issue