diff --git a/src/pages/Zone/Card/ZoneLocationsTree.vue b/src/pages/Zone/Card/ZoneLocationsTree.vue index 595abbec0..5cb0a8977 100644 --- a/src/pages/Zone/Card/ZoneLocationsTree.vue +++ b/src/pages/Zone/Card/ZoneLocationsTree.vue @@ -6,6 +6,7 @@ import { useRoute } from 'vue-router'; import { useState } from 'src/composables/useState'; import axios from 'axios'; import { useArrayData } from 'composables/useArrayData'; +import { tree } from 'quasar/dist/icon-set/material-icons.umd.prod'; const { t } = useI18n(); const route = useRoute(); @@ -24,15 +25,25 @@ const nodes = ref([ { id: null, name: t('zoneLocations.locations'), sons: true, children: [{}] }, ]); -const fetchedChildrensSet = ref(new Set()); +const previousExpandedNodes = ref(new Set()); -const onNodeExpanded = (nodeKeysArray) => { - if (!fetchedChildrensSet.value.has(nodeKeysArray.at(-1))) { - fetchedChildrensSet.value.add(nodeKeysArray.at(-1)); - fetchNodeLeaves(nodeKeysArray.at(-1)); +const onNodeExpanded = async (nodeKeysArray) => { + const nodeKeysSet = new Set(nodeKeysArray); + const lastNodeKey = 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) => { @@ -43,7 +54,6 @@ const formatNodeSelected = (node) => { const fetchNodeLeaves = async (nodeKey) => { try { const node = treeRef.value?.getNodeByKey(nodeKey); - if (!node || node.sons === 0) return; const params = { parentId: node.id }; @@ -86,8 +96,9 @@ function getNodeIds(node) { let ids = []; if (node.id) ids.push(node.id); - if (node.childs) { - node.childs.forEach((child) => { + const children = node.child || node.children; + if (children) { + children.forEach((child) => { ids = ids.concat(getNodeIds(child)); }); } @@ -110,7 +121,8 @@ onMounted(async () => { await arrayData.fetch({ append: false }); 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'); if (tree) { for (let n of tree) {