Skip to content

Commit

Permalink
Revert "fix(menu): detach lazily-rendered content when the menu is cl…
Browse files Browse the repository at this point in the history
…osed (#10005)"

This reverts commit 37b1a09.
  • Loading branch information
tinayuangao committed Mar 6, 2018
1 parent c8ca770 commit 27db2df
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 60 deletions.
14 changes: 2 additions & 12 deletions src/lib/menu/menu-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export class MatMenuContent implements OnDestroy {
attach(context: any = {}) {
if (!this._portal) {
this._portal = new TemplatePortal(this._template, this._viewContainerRef);
} else if (this._portal.isAttached) {
this._portal.detach();
}

this.detach();

if (!this._outlet) {
this._outlet = new DomPortalOutlet(this._document.createElement('div'),
this._componentFactoryResolver, this._appRef, this._injector);
Expand All @@ -62,16 +62,6 @@ export class MatMenuContent implements OnDestroy {
this._portal.attach(this._outlet, context);
}

/**
* Detaches the content.
* @docs-private
*/
detach() {
if (this._portal.isAttached) {
this._portal.detach();
}
}

ngOnDestroy() {
if (this._outlet) {
this._outlet.dispose();
Expand Down
11 changes: 5 additions & 6 deletions src/lib/menu/menu-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
NgZone,
} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import {merge} from 'rxjs/observable/merge';
import {Subscription} from 'rxjs/Subscription';
import {matMenuAnimations} from './menu-animations';
Expand Down Expand Up @@ -97,9 +96,6 @@ export class MatMenu implements AfterContentInit, MatMenuPanel, OnDestroy {
/** Current state of the panel animation. */
_panelAnimationState: 'void' | 'enter-start' | 'enter' = 'void';

/** Emits whenever an animation on the menu completes. */
_animationDone = new Subject<void>();

/** Parent menu of the current menu panel. */
parentMenu: MatMenuPanel | undefined;

Expand Down Expand Up @@ -304,7 +300,10 @@ export class MatMenu implements AfterContentInit, MatMenuPanel, OnDestroy {
}

/** Callback that is invoked when the panel animation completes. */
_onAnimationDone() {
this._animationDone.next();
_onAnimationDone(event: AnimationEvent) {
// After the initial expansion is done, trigger the second phase of the enter animation.
if (event.toState === 'enter-start') {
this._panelAnimationState = 'enter';
}
}
}
26 changes: 6 additions & 20 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
} from '@angular/cdk/overlay';
import {TemplatePortal} from '@angular/cdk/portal';
import {filter} from 'rxjs/operators/filter';
import {take} from 'rxjs/operators/take';
import {
AfterContentInit,
Directive,
Expand Down Expand Up @@ -239,27 +238,14 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {

/** Closes the menu and does the necessary cleanup. */
private _destroyMenu() {
if (!this._overlayRef || !this.menuOpen) {
return;
}

const menu = this.menu;

this._resetMenu();
this._closeSubscription.unsubscribe();
this._overlayRef.detach();

if (menu instanceof MatMenu) {
menu._resetAnimation();
if (this._overlayRef && this.menuOpen) {
this._resetMenu();
this._closeSubscription.unsubscribe();
this._overlayRef.detach();

if (menu.lazyContent) {
// Wait for the exit animation to finish before detaching the content.
menu._animationDone
.pipe(take(1))
.subscribe(() => menu.lazyContent!.detach());
if (this.menu instanceof MatMenu) {
this.menu._resetAnimation();
}
} else if (menu.lazyContent) {
menu.lazyContent.detach();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/menu/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(keydown)="_handleKeydown($event)"
(click)="closed.emit('click')"
[@transformMenu]="_panelAnimationState"
(@transformMenu.done)="_onAnimationDone()"
(@transformMenu.done)="_onAnimationDone($event)"
tabindex="-1"
role="menu">
<div class="mat-menu-content" [@fadeInItems]="'showing'">
Expand Down
23 changes: 2 additions & 21 deletions src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ describe('MatMenu', () => {
describe('lazy rendering', () => {
it('should be able to render the menu content lazily', fakeAsync(() => {
const fixture = TestBed.createComponent(SimpleLazyMenu);

fixture.detectChanges();

fixture.componentInstance.triggerEl.nativeElement.click();
fixture.detectChanges();
tick(500);
Expand All @@ -340,24 +340,6 @@ describe('MatMenu', () => {
expect(fixture.componentInstance.trigger.menuOpen).toBe(true, 'Expected menu to be open');
}));

it('should detach the lazy content when the menu is closed', fakeAsync(() => {
const fixture = TestBed.createComponent(SimpleLazyMenu);

fixture.detectChanges();
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();
tick(500);

expect(fixture.componentInstance.items.length).toBeGreaterThan(0);

fixture.componentInstance.trigger.closeMenu();
fixture.detectChanges();
tick(500);
fixture.detectChanges();

expect(fixture.componentInstance.items.length).toBe(0);
}));

it('should focus the first menu item when opening a lazy menu via keyboard', fakeAsync(() => {
let zone: MockNgZone;

Expand Down Expand Up @@ -390,8 +372,8 @@ describe('MatMenu', () => {

it('should be able to open the same menu with a different context', fakeAsync(() => {
const fixture = TestBed.createComponent(LazyMenuWithContext);

fixture.detectChanges();

fixture.componentInstance.triggerOne.openMenu();
fixture.detectChanges();
tick(500);
Expand Down Expand Up @@ -1575,7 +1557,6 @@ class FakeIcon {}
class SimpleLazyMenu {
@ViewChild(MatMenuTrigger) trigger: MatMenuTrigger;
@ViewChild('triggerEl') triggerEl: ElementRef;
@ViewChildren(MatMenuItem) items: QueryList<MatMenuItem>;
}


Expand Down

0 comments on commit 27db2df

Please sign in to comment.