Skip to content

Commit

Permalink
feat(theme:menu): add getDefaultRedirect method (#1850)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Nov 25, 2024
1 parent 8461d9c commit 0323a37
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
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

0 comments on commit 0323a37

Please sign in to comment.