Skip to content

Commit

Permalink
fix(abc:sidebar-nav): fix keeping expand when changed route in enable…
Browse files Browse the repository at this point in the history
…d `openStrictly` (#759)
  • Loading branch information
cipchk authored Dec 30, 2019
1 parent bafb44c commit a7ab84a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
27 changes: 24 additions & 3 deletions packages/abc/sidebar-nav/sidebar-nav.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,34 @@ export class SidebarNavComponent implements OnInit, OnDestroy {
}

_docClick(): void {
this.hideAll();
if (this.collapsed) {
this.hideAll();
}
}

private openedByUrl(url: string | null) {
const { menuSrv, recursivePath, openStrictly } = this;
let findItem = menuSrv.getHit(this.menuSrv.menus, url!, recursivePath, i => {
i._selected = false;
if (!openStrictly) {
i._open = false;
}
});
if (findItem == null) return;

do {
findItem._selected = true;
if (!openStrictly) {
findItem._open = true;
}
findItem = findItem.__parent;
} while (findItem);
}

ngOnInit(): void {
const { doc, router, unsubscribe$, menuSrv, cdr } = this;
this.bodyEl = doc.querySelector('body');
menuSrv.openedByUrl(router.url, this.recursivePath);
this.openedByUrl(router.url);
this.ngZone.runOutsideAngular(() => this.genFloatingContainer());
menuSrv.change.pipe(takeUntil(unsubscribe$)).subscribe(data => {
menuSrv.visit(data, (i: Nav, _p, depth) => {
Expand All @@ -233,7 +254,7 @@ export class SidebarNavComponent implements OnInit, OnDestroy {
});
router.events.pipe(takeUntil(unsubscribe$)).subscribe(e => {
if (e instanceof NavigationEnd) {
this.menuSrv.openedByUrl(e.urlAfterRedirects, this.recursivePath);
this.openedByUrl(e.urlAfterRedirects);
this.underPad();
this.cdr.detectChanges();
}
Expand Down
60 changes: 41 additions & 19 deletions packages/abc/sidebar-nav/sidebar-nav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ const MOCKMENUS = [
},
] as Nav[];

const MOCKOPENSTRICTLY = [
{
text: '',
group: true,
children: [
{
text: '',
link: '/v1',
open: true,
children: [{ text: '' }],
},
{
text: '',
link: '/v1',
open: true,
children: [{ text: '' }],
},
],
},
] as Nav[];

class MockACLService {
can(val: string) {
return val === 'admin';
Expand Down Expand Up @@ -318,6 +339,15 @@ describe('abc: sidebar-nav', () => {
fixture.detectChanges();
expect(router.navigateByUrl).toHaveBeenCalled();
});
it('muse be hide via document click', () => {
createComp();
setSrv.layout.collapsed = true;
fixture.detectChanges();
page.showSubMenu();
document.dispatchEvent(new MouseEvent('click'));
fixture.detectChanges();
expect(router.navigateByUrl).not.toHaveBeenCalled();
});
});
it('#52', () => {
createComp();
Expand Down Expand Up @@ -360,24 +390,7 @@ describe('abc: sidebar-nav', () => {
createComp();
context.openStrictly = true;
fixture.detectChanges();
menuSrv.add([
{
text: '',
group: true,
children: [
{
text: '',
open: true,
children: [{ text: '' }],
},
{
text: '',
open: true,
children: [{ text: '' }],
},
],
},
] as Nav[]);
menuSrv.add(deepCopy(MOCKOPENSTRICTLY));
fixture.detectChanges();
});
it('should working', () => {
Expand Down Expand Up @@ -469,7 +482,7 @@ describe('abc: sidebar-nav', () => {
{
text: '主导航',
group: true,
children: [{ text: 'user', link: '/user' }],
children: [{ text: 'user1', link: '/user' }, { text: 'user2', link: '/user' }],
},
]);
}));
Expand Down Expand Up @@ -497,6 +510,15 @@ describe('abc: sidebar-nav', () => {
fixture.detectChanges();
page.checkCount('.sidebar-nav__selected', 0);
}));
it('should be ingore _open when enabled openStrictly', fakeAsync(() => {
context.openStrictly = true;
fixture.detectChanges();
menuSrv.add(deepCopy(MOCKOPENSTRICTLY));
page.checkCount('.sidebar-nav__open', 2);
router.navigateByUrl('/user2');
fixture.detectChanges();
page.checkCount('.sidebar-nav__open', 2);
}));
});

class PageObject {
Expand Down

0 comments on commit a7ab84a

Please sign in to comment.