2024-03-01 17:21:45 +00:00
|
|
|
import type { Ref } from "vue";
|
|
|
|
import type { TreeItem } from "~~/lib/api/types/data-contracts";
|
2023-01-28 20:53:00 +00:00
|
|
|
|
|
|
|
export interface FlatTreeItem {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
2023-02-10 02:47:41 +00:00
|
|
|
treeString: string;
|
2023-01-28 20:53:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
function flatTree(tree: TreeItem[]): FlatTreeItem[] {
|
|
|
|
const v = [] as FlatTreeItem[];
|
2023-01-28 20:53:00 +00:00
|
|
|
|
|
|
|
// turns the nested items into a flat items array where
|
|
|
|
// the display is a string of the tree hierarchy separated by breadcrumbs
|
|
|
|
|
|
|
|
function flatten(items: TreeItem[], display: string) {
|
2023-03-21 05:48:22 +00:00
|
|
|
if (!items) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-28 20:53:00 +00:00
|
|
|
for (const item of items) {
|
2024-02-29 19:45:05 +00:00
|
|
|
v.push({
|
2023-01-28 20:53:00 +00:00
|
|
|
id: item.id,
|
|
|
|
name: item.name,
|
2023-02-10 02:47:41 +00:00
|
|
|
treeString: display + item.name,
|
2023-01-28 20:53:00 +00:00
|
|
|
});
|
|
|
|
if (item.children) {
|
|
|
|
flatten(item.children, display + item.name + " > ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flatten(tree, "");
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
export function useFlatLocations(): Ref<FlatTreeItem[]> {
|
|
|
|
const locations = useLocationStore();
|
2023-01-28 20:53:00 +00:00
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
if (locations.tree === null) {
|
|
|
|
locations.refreshTree();
|
2023-01-28 20:53:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
return computed(() => {
|
|
|
|
if (locations.tree === null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return flatTree(locations.tree);
|
|
|
|
});
|
2023-01-28 20:53:00 +00:00
|
|
|
}
|