erp-platform/ui/src/utils/navigation.ts

60 lines
1.7 KiB
TypeScript
Raw Normal View History

import { NavigationTree } from '@/proxy/menus/navigation'
2025-05-06 06:45:49 +00:00
import {
NAV_ITEM_TYPE_COLLAPSE,
NAV_ITEM_TYPE_ITEM,
NAV_ITEM_TYPE_TITLE,
} from '@/constants/navigation.constant'
2025-08-13 20:29:27 +00:00
import { MenuDto } from '@/proxy/menus/models'
2025-05-06 06:45:49 +00:00
export function navigationTreeToFlat(items: NavigationTree[]): NavigationTree[] {
const result: NavigationTree[] = []
const traverse = (nodes: NavigationTree[]) => {
for (const node of nodes) {
const { subMenu, ...rest } = node
result.push(rest as NavigationTree)
if (subMenu && subMenu.length > 0) {
traverse(subMenu)
}
}
}
traverse(items)
return result
}
2025-05-06 06:45:49 +00:00
export default function getChildren(menu: MenuDto[], parentCode: string | null): NavigationTree[] {
const menus: NavigationTree[] = []
for (const child of menu.filter((a) => a.parentCode === parentCode)) {
const item: NavigationTree = {
key: child.url?.length ? child.url : (child.code ?? ''),
2025-05-06 06:45:49 +00:00
path: child.url ?? '',
title: child.displayName ?? '',
icon: child.icon ?? '',
type: NAV_ITEM_TYPE_TITLE,
translateKey: child.displayName ?? '',
authority: [child.requiredPermissionName ?? ''],
subMenu: [],
}
2025-08-13 20:29:27 +00:00
2025-05-06 06:45:49 +00:00
if (child.code) {
const subMenu = getChildren(menu, child.code)
if (subMenu.length) {
item.subMenu = subMenu
item.type = child.parentCode ? NAV_ITEM_TYPE_COLLAPSE : NAV_ITEM_TYPE_TITLE
} else {
item.type = NAV_ITEM_TYPE_ITEM
}
//Eğer submenu veya path varsa menude gösterilmeli.
//Yani submenüde biri gösterilecekse ana menüsü gösterilmeli
if (item.subMenu.length > 0 || item.path !== '') {
menus.push(item)
}
}
}
return menus
}