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

chore(api): new output api #6677 #7450

Merged
merged 4 commits into from
Oct 9, 2017
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
33 changes: 30 additions & 3 deletions src/lib/sidenav/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('MatDrawer', () => {
DrawerSetToOpenedTrue,
DrawerDynamicPosition,
DrawerWitFocusableElements,
DrawerOpenBinding,
],
});

Expand Down Expand Up @@ -287,6 +288,20 @@ describe('MatDrawer', () => {

expect(() => fixture.detectChanges()).not.toThrow();
});

it('should bind 2-way bind on opened property', fakeAsync(() => {
const fixture = TestBed.createComponent(DrawerOpenBinding);
fixture.detectChanges();

let drawer: MatDrawer = fixture.debugElement
.query(By.directive(MatDrawer)).componentInstance;

drawer.open();
fixture.detectChanges();
tick();

expect(fixture.componentInstance.isOpen).toBe(true);
}));
});

describe('focus trapping behavior', () => {
Expand Down Expand Up @@ -474,8 +489,8 @@ class DrawerContainerTwoDrawerTestApp {
template: `
<mat-drawer-container (backdropClick)="backdropClicked()">
<mat-drawer #drawer position="start"
(open)="open()"
(close)="close()">
(opened)="open()"
(closed)="close()">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a test for the [(opened)] syntax as well

<button #drawerButton>Content.</button>
</mat-drawer>
<button (click)="drawer.open()" class="open" #openButton></button>
Expand Down Expand Up @@ -517,7 +532,7 @@ class DrawerSetToOpenedFalse { }
@Component({
template: `
<mat-drawer-container>
<mat-drawer #drawer mode="side" opened="true" (open)="openCallback()">
<mat-drawer #drawer mode="side" opened="true" (opened)="openCallback()">
Closed Drawer.
</mat-drawer>
</mat-drawer-container>`,
Expand All @@ -526,6 +541,18 @@ class DrawerSetToOpenedTrue {
openCallback = jasmine.createSpy('open callback');
}

@Component({
template: `
<mat-drawer-container>
<mat-drawer #drawer mode="side" [(opened)]="isOpen">
Closed Drawer.
</mat-drawer>
</mat-drawer-container>`,
})
class DrawerOpenBinding {
isOpen = false;
}

@Component({
template: `
<mat-drawer-container>
Expand Down
62 changes: 46 additions & 16 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
import {DOCUMENT} from '@angular/platform-browser';
import {merge} from 'rxjs/observable/merge';
import {Subject} from 'rxjs/Subject';
import {RxChain, filter, first, startWith, takeUntil} from '@angular/cdk/rxjs';
import {Observable} from 'rxjs/Observable';
import {RxChain, filter, map, first, startWith, takeUntil} from '@angular/cdk/rxjs';


/** Throws an exception when two MatDrawer are matching the same position. */
Expand Down Expand Up @@ -177,11 +178,38 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
/** Current state of the sidenav animation. */
_animationState: 'open-instant' | 'open' | 'void' = 'void';

/** Event emitted when the drawer is fully opened. */
@Output('open') onOpen = new EventEmitter<MatDrawerToggleResult | void>();
/** Event emitted when the drawer open state is changed. */
@Output() openedChange: EventEmitter<boolean> = new EventEmitter<boolean>();

/** Event emitted when the drawer is fully closed. */
@Output('close') onClose = new EventEmitter<MatDrawerToggleResult | void>();
/** Event emitted when the drawer has been opened. */
@Output('opened')
get _openedStream(): Observable<void> {
return RxChain.from(this.openedChange)
.call(filter, o => o)
.call(map, () => {})
.result();
}

/** Event emitted when the drawer has been closed. */
@Output('closed')
get _closedStream(): Observable<void> {
return RxChain.from(this.openedChange)
.call(filter, o => !o)
.call(map, () => {})
.result();
}

/**
* Event emitted when the drawer is fully opened.
* @deprecated Use `openedChange` instead.
*/
@Output('open') onOpen = this._openedStream;

/**
* Event emitted when the drawer is fully closed.
* @deprecated Use `openedChange` instead.
*/
@Output('close') onClose = this._closedStream;

/** Event emitted when the drawer's position changes. */
@Output('positionChanged') onPositionChanged = new EventEmitter<void>();
Expand All @@ -203,17 +231,19 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
constructor(private _elementRef: ElementRef,
private _focusTrapFactory: FocusTrapFactory,
@Optional() @Inject(DOCUMENT) private _doc: any) {
this.onOpen.subscribe(() => {
if (this._doc) {
this._elementFocusedBeforeDrawerWasOpened = this._doc.activeElement as HTMLElement;
}
this.openedChange.subscribe((opened: boolean) => {
if (opened) {
if (this._doc) {
this._elementFocusedBeforeDrawerWasOpened = this._doc.activeElement as HTMLElement;
}

if (this._isFocusTrapEnabled && this._focusTrap) {
this._focusTrap.focusInitialElementWhenReady();
if (this._isFocusTrapEnabled && this._focusTrap) {
this._focusTrap.focusInitialElementWhenReady();
}
} else {
this._restoreFocus();
}
});

this.onClose.subscribe(() => this._restoreFocus());
}

/**
Expand Down Expand Up @@ -309,9 +339,9 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
const {fromState, toState} = event;

if (toState.indexOf('open') === 0 && fromState === 'void') {
this.onOpen.emit(new MatDrawerToggleResult('open', true));
this.openedChange.emit(true);
} else if (toState === 'void' && fromState.indexOf('open') === 0) {
this.onClose.emit(new MatDrawerToggleResult('close', true));
this.openedChange.emit(false);
}
}

Expand Down Expand Up @@ -438,7 +468,7 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
});

if (drawer.mode !== 'side') {
takeUntil.call(merge(drawer.onOpen, drawer.onClose), this._drawers.changes).subscribe(() =>
takeUntil.call(drawer.openedChange, this._drawers.changes).subscribe(() =>
this._setContainerClass(drawer.opened));
}
}
Expand Down