mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-27 23:46:37 +01:00
Prevent self-referencing locations and items as parents (#773)
* prevent current location and descendants from being selected as parent * prevent an item from showing up in the parent items drop-down for itself * pass location object to filter function to allow for more flexible filtering * align exclude prop and fix type comparison, change item filter to array of ItemsObjects to allow for descendant filtering in future * fix linting prop reference
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Ref } from "vue";
|
||||
import type { TreeItem } from "~~/lib/api/types/data-contracts";
|
||||
import type { TreeItem, LocationSummary } from "~~/lib/api/types/data-contracts";
|
||||
|
||||
export interface FlatTreeItem {
|
||||
id: string;
|
||||
@@ -35,9 +35,29 @@ function flatTree(tree: TreeItem[]): FlatTreeItem[] {
|
||||
return v;
|
||||
}
|
||||
|
||||
export function useFlatLocations(): Ref<FlatTreeItem[]> {
|
||||
const locations = useLocationStore();
|
||||
function filterOutSubtree(tree: TreeItem[], excludeId: string): TreeItem[] {
|
||||
// Recursively filters out a subtree starting from excludeId
|
||||
const result: TreeItem[] = [];
|
||||
|
||||
for (const item of tree) {
|
||||
if (item.id === excludeId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const newItem = { ...item };
|
||||
if (item.children) {
|
||||
newItem.children = filterOutSubtree(item.children, excludeId);
|
||||
}
|
||||
|
||||
result.push(newItem);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function useFlatLocations(excludeSubtreeForLocation?: LocationSummary): Ref<FlatTreeItem[]> {
|
||||
const locations = useLocationStore();
|
||||
|
||||
if (locations.tree === null) {
|
||||
locations.refreshTree();
|
||||
}
|
||||
@@ -47,6 +67,10 @@ export function useFlatLocations(): Ref<FlatTreeItem[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
return flatTree(locations.tree);
|
||||
const filteredTree = excludeSubtreeForLocation
|
||||
? filterOutSubtree(locations.tree, excludeSubtreeForLocation.id)
|
||||
: locations.tree;
|
||||
|
||||
return flatTree(filteredTree);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user