From 0323a37813476e439ec8e12873b73e839dc4efcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E8=89=B2?= Date: Mon, 25 Nov 2024 12:31:47 +0800 Subject: [PATCH] feat(theme:menu): add `getDefaultRedirect` method (#1850) --- .../theme/src/services/menu/index.en-US.md | 1 + .../theme/src/services/menu/index.zh-CN.md | 1 + .../src/services/menu/menu.service.spec.ts | 13 +++++++++++++ .../theme/src/services/menu/menu.service.ts | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/packages/theme/src/services/menu/index.en-US.md b/packages/theme/src/services/menu/index.en-US.md index 2f9c86033..8640132d3 100644 --- a/packages/theme/src/services/menu/index.en-US.md +++ b/packages/theme/src/services/menu/index.en-US.md @@ -27,6 +27,7 @@ This is because menus it's essential part of the applications, And it can be use | `open` | Open of the menu | | `toggleOpen` | Toggle menu open or close | | `openAll` | Toggle all menu open or close | +| `getDefaultRedirect` | Returns a default menu link | **recursive** diff --git a/packages/theme/src/services/menu/index.zh-CN.md b/packages/theme/src/services/menu/index.zh-CN.md index 0ecb1810d..33358f459 100644 --- a/packages/theme/src/services/menu/index.zh-CN.md +++ b/packages/theme/src/services/menu/index.zh-CN.md @@ -25,6 +25,7 @@ type: Service | `open` | 展开某菜单 | | `toggleOpen` | 切换菜单的展开或关闭 | | `openAll` | 展开或关闭所有菜单 | +| `getDefaultRedirect` | 返回一个默认跳转的菜单链接 | **recursive** diff --git a/packages/theme/src/services/menu/menu.service.spec.ts b/packages/theme/src/services/menu/menu.service.spec.ts index 087295135..79ff9d653 100644 --- a/packages/theme/src/services/menu/menu.service.spec.ts +++ b/packages/theme/src/services/menu/menu.service.spec.ts @@ -315,6 +315,19 @@ describe('Service: Menu', () => { }); }); + describe('#getDefaultRedirect', () => { + beforeEach(() => srv.add(deepCopy(DATA))); + it('should be get first link', () => { + expect(srv.getDefaultRedirect()).toBe('/dashboard'); + }); + it('should be specifies whether the jump link is valid', () => { + expect(srv.getDefaultRedirect({ redirectUrl: '/dashboard/v1' })).toBe('/dashboard/v1'); + }); + it('should be return first link when redirectUrl is invalid', () => { + expect(srv.getDefaultRedirect({ redirectUrl: '/dashboard/invalid' })).toBe('/dashboard'); + }); + }); + describe('#find', () => { beforeEach(() => srv.add(deepCopy(DATA))); it('via key', () => { diff --git a/packages/theme/src/services/menu/menu.service.ts b/packages/theme/src/services/menu/menu.service.ts index c3ef60013..3f2803cbb 100644 --- a/packages/theme/src/services/menu/menu.service.ts +++ b/packages/theme/src/services/menu/menu.service.ts @@ -34,6 +34,24 @@ export class MenuService implements OnDestroy { return this.data; } + /** + * Returns a default menu link + * + * 返回一个默认跳转的菜单链接 + */ + getDefaultRedirect(opt: { redirectUrl?: string } = {}): string | null | undefined { + let ret: string | null | undefined; + this.visit(this.menus, (item: MenuInner) => { + if (typeof item.link !== 'string' || item.link.length <= 0 || !item._aclResult || item._hidden === true) { + return; + } + if (ret == null || ret.length <= 0 || item.link == opt?.redirectUrl) { + ret = item.link; + } + }); + return ret; + } + visit(data: T[], callback: (item: T, parentMenum: T | null, depth?: number) => void): void; visit(data: Menu[], callback: (item: Menu, parentMenum: Menu | null, depth?: number) => void): void; visit(data: Menu[], callback: (item: Menu, parentMenum: Menu | null, depth?: number) => void): void {