Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(theme:menu): add getDefaultRedirect method #1850

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/theme/src/services/menu/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
1 change: 1 addition & 0 deletions packages/theme/src/services/menu/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type: Service
| `open` | 展开某菜单 |
| `toggleOpen` | 切换菜单的展开或关闭 |
| `openAll` | 展开或关闭所有菜单 |
| `getDefaultRedirect` | 返回一个默认跳转的菜单链接 |

**recursive**

Expand Down
13 changes: 13 additions & 0 deletions packages/theme/src/services/menu/menu.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/theme/src/services/menu/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Menu = Menu>(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 {
Expand Down
Loading