Skip to content

Commit 1333efe

Browse files
committed
Address comments
1 parent 2241c43 commit 1333efe

File tree

4 files changed

+25
-33
lines changed

4 files changed

+25
-33
lines changed

src/cdk/overlay/keyboard/overlay-keyboard-dispatcher.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ describe('OverlayKeyboardDispatcher', () => {
3434
const overlayTwo = overlay.create();
3535

3636
// Attach overlays
37-
keyboardDispatcher.attach(overlayOne);
38-
keyboardDispatcher.attach(overlayTwo);
37+
keyboardDispatcher.add(overlayOne);
38+
keyboardDispatcher.add(overlayTwo);
3939

4040
expect(keyboardDispatcher._attachedOverlays.length)
4141
.toBe(2, 'Expected both overlays to be tracked.');
4242
expect(keyboardDispatcher._attachedOverlays[0]).toBe(overlayOne, 'Expected one to be first.');
4343
expect(keyboardDispatcher._attachedOverlays[1]).toBe(overlayTwo, 'Expected two to be last.');
4444

4545
// Detach first one and re-attach it
46-
keyboardDispatcher.detach(overlayOne);
47-
keyboardDispatcher.attach(overlayOne);
46+
keyboardDispatcher.remove(overlayOne);
47+
keyboardDispatcher.add(overlayOne);
4848

4949
expect(keyboardDispatcher._attachedOverlays[0])
5050
.toBe(overlayTwo, 'Expected two to now be first.');
@@ -62,8 +62,8 @@ describe('OverlayKeyboardDispatcher', () => {
6262
overlayTwo.keyboardEvents().subscribe(overlayTwoSpy);
6363

6464
// Attach overlays
65-
keyboardDispatcher.attach(overlayOne);
66-
keyboardDispatcher.attach(overlayTwo);
65+
keyboardDispatcher.add(overlayOne);
66+
keyboardDispatcher.add(overlayTwo);
6767

6868
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
6969

@@ -82,8 +82,8 @@ describe('OverlayKeyboardDispatcher', () => {
8282
overlayTwo.keyboardEvents().subscribe(overlayTwoSpy);
8383

8484
// Attach overlays
85-
keyboardDispatcher.attach(overlayOne);
86-
keyboardDispatcher.attach(overlayTwo);
85+
keyboardDispatcher.add(overlayOne);
86+
keyboardDispatcher.add(overlayTwo);
8787

8888
const overlayOnePane = overlayOne.overlayElement;
8989

src/cdk/overlay/keyboard/overlay-keyboard-dispatcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
3838
}
3939

4040
/** Add a new overlay to the list of attached overlay refs. */
41-
attach(overlayRef: OverlayRef): void {
41+
add(overlayRef: OverlayRef): void {
4242
this._attachedOverlays.push(overlayRef);
4343
}
4444

4545
/** Remove an overlay from the list of attached overlay refs. */
46-
detach(overlayRef: OverlayRef): void {
46+
remove(overlayRef: OverlayRef): void {
4747
const index = this._attachedOverlays.indexOf(overlayRef);
4848
if (index > -1) {
4949
this._attachedOverlays.splice(index, 1);

src/cdk/overlay/overlay-ref.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {NgZone} from '@angular/core';
1010
import {PortalHost, Portal} from '@angular/cdk/portal';
1111
import {OverlayState} from './overlay-state';
12+
import {OverlayKeyboardDispatcher} from './keyboard/overlay-keyboard-dispatcher';
1213
import {Observable} from 'rxjs/Observable';
1314
import {Subject} from 'rxjs/Subject';
1415

@@ -30,7 +31,8 @@ export class OverlayRef implements PortalHost {
3031
private _portalHost: PortalHost,
3132
private _pane: HTMLElement,
3233
private _state: OverlayState,
33-
private _ngZone: NgZone) {
34+
private _ngZone: NgZone,
35+
private _keyboardDispatcher: OverlayKeyboardDispatcher) {
3436

3537
_state.scrollStrategy.attach(this);
3638
}
@@ -78,6 +80,9 @@ export class OverlayRef implements PortalHost {
7880
// Only emit the `attachments` event once all other setup is done.
7981
this._attachments.next();
8082

83+
// Track this overlay by the keyboard dispatcher
84+
this._keyboardDispatcher.add(this);
85+
8186
return attachResult;
8287
}
8388

@@ -99,6 +104,9 @@ export class OverlayRef implements PortalHost {
99104
// Only emit after everything is detached.
100105
this._detachments.next();
101106

107+
// Remove this overlay from keyboard dispatcher tracking
108+
this._keyboardDispatcher.remove(this);
109+
102110
return detachmentResult;
103111
}
104112

@@ -129,28 +137,28 @@ export class OverlayRef implements PortalHost {
129137
}
130138

131139
/**
132-
* Returns an observable that emits when the backdrop has been clicked.
140+
* Gets an observable that emits when the backdrop has been clicked.
133141
*/
134142
backdropClick(): Observable<void> {
135143
return this._backdropClick.asObservable();
136144
}
137145

138-
/** Returns an observable that emits when the overlay has been attached. */
146+
/** Gets an observable that emits when the overlay has been attached. */
139147
attachments(): Observable<void> {
140148
return this._attachments.asObservable();
141149
}
142150

143-
/** Returns an observable that emits when the overlay has been detached. */
151+
/** Gets an observable that emits when the overlay has been detached. */
144152
detachments(): Observable<void> {
145153
return this._detachments.asObservable();
146154
}
147155

148-
/** Returns an observable that emits when the overlay has been disposed. */
156+
/** Gets an observable that emits when the overlay has been disposed. */
149157
disposed(): Observable<void> {
150158
return this._disposed.asObservable();
151159
}
152160

153-
/** Returns an observable of keyboard events targeted to this overlay. */
161+
/** Gets an observable of keyboard events targeted to this overlay. */
154162
keyboardEvents(): Observable<KeyboardEvent> {
155163
return this._dispatchedKeyboardEvents.asObservable();
156164
}

src/cdk/overlay/overlay.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
NgZone,
1515
} from '@angular/core';
1616
import {DomPortalHost} from '@angular/cdk/portal';
17-
import {takeUntil} from '@angular/cdk/rxjs';
1817
import {OverlayState} from './overlay-state';
1918
import {OverlayRef} from './overlay-ref';
2019
import {OverlayPositionBuilder} from './position/overlay-position-builder';
@@ -58,9 +57,7 @@ export class Overlay {
5857
create(state: OverlayState = defaultState): OverlayRef {
5958
const pane = this._createPaneElement();
6059
const portalHost = this._createPortalHost(pane);
61-
const overlayRef = new OverlayRef(portalHost, pane, state, this._ngZone);
62-
this._trackOverlayAttachments(overlayRef);
63-
return overlayRef;
60+
return new OverlayRef(portalHost, pane, state, this._ngZone, this._keyboardDispatcher);
6461
}
6562

6663
/**
@@ -93,18 +90,5 @@ export class Overlay {
9390
private _createPortalHost(pane: HTMLElement): DomPortalHost {
9491
return new DomPortalHost(pane, this._componentFactoryResolver, this._appRef, this._injector);
9592
}
96-
/**
97-
* Subscribe to attach and detach events and register them accordingly
98-
* with the keyboard dispatcher service.
99-
*/
100-
private _trackOverlayAttachments(overlayRef: OverlayRef): void {
101-
// Append overlay ref when attached
102-
takeUntil.call(overlayRef.attachments(), overlayRef.disposed())
103-
.subscribe(() => this._keyboardDispatcher.attach(overlayRef));
104-
105-
// Remove overlay ref when detached
106-
takeUntil.call(overlayRef.detachments(), overlayRef.disposed())
107-
.subscribe(() => this._keyboardDispatcher.detach(overlayRef));
108-
}
10993

11094
}

0 commit comments

Comments
 (0)