Skip to content

Commit 2240b6c

Browse files
devversiontinayuangao
authored andcommitted
refactor(focus-origin-monitor): rename unmonitor to stopMonitoring (#3783)
* Renames the `unmonitor` function to `stopMonitoring`. * Removes unnecessary code that keeps track of the `FocusOriginMonitor` subscription. After calling `stopMonitoring` the observable will complete.
1 parent a71a5af commit 2240b6c

File tree

8 files changed

+21
-50
lines changed

8 files changed

+21
-50
lines changed

src/lib/button/button.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class MdButton implements OnDestroy {
125125
}
126126

127127
ngOnDestroy() {
128-
this._focusOriginMonitor.unmonitor(this._elementRef.nativeElement);
128+
this._focusOriginMonitor.stopMonitoring(this._elementRef.nativeElement);
129129
}
130130

131131
/** The color of the button. Can be `primary`, `accent`, or `warn`. */

src/lib/checkbox/checkbox.spec.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,11 @@ import {MdCheckbox, MdCheckboxChange, MdCheckboxModule} from './index';
1313
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
1414
import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
1515
import {dispatchFakeEvent} from '../core/testing/dispatch-events';
16-
import {FocusOriginMonitor, FocusOrigin} from '../core';
1716
import {RIPPLE_FADE_IN_DURATION, RIPPLE_FADE_OUT_DURATION} from '../core/ripple/ripple-renderer';
18-
import {Subject} from 'rxjs/Subject';
1917

2018

2119
describe('MdCheckbox', () => {
2220
let fixture: ComponentFixture<any>;
23-
let fakeFocusOriginMonitorSubject: Subject<FocusOrigin> = new Subject();
24-
let fakeFocusOriginMonitor = {
25-
monitor: () => fakeFocusOriginMonitorSubject.asObservable(),
26-
unmonitor: () => {},
27-
focusVia: (element: HTMLElement, renderer: any, focusOrigin: FocusOrigin) => {
28-
element.focus();
29-
fakeFocusOriginMonitorSubject.next(focusOrigin);
30-
}
31-
};
3221

3322
beforeEach(async(() => {
3423
TestBed.configureTestingModule({
@@ -45,8 +34,7 @@ describe('MdCheckbox', () => {
4534
CheckboxWithFormControl,
4635
],
4736
providers: [
48-
{provide: ViewportRuler, useClass: FakeViewportRuler},
49-
{provide: FocusOriginMonitor, useValue: fakeFocusOriginMonitor}
37+
{provide: ViewportRuler, useClass: FakeViewportRuler}
5038
]
5139
});
5240

@@ -356,7 +344,9 @@ describe('MdCheckbox', () => {
356344
expect(fixture.nativeElement.querySelectorAll('.mat-ripple-element').length)
357345
.toBe(0, 'Expected no ripples on load.');
358346

359-
fakeFocusOriginMonitorSubject.next('keyboard');
347+
dispatchFakeEvent(inputElement, 'keydown');
348+
dispatchFakeEvent(inputElement, 'focus');
349+
360350
tick(RIPPLE_FADE_IN_DURATION);
361351

362352
expect(fixture.nativeElement.querySelectorAll('.mat-ripple-element').length)

src/lib/checkbox/checkbox.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,7 @@ export class MdCheckbox implements ControlValueAccessor, AfterViewInit, OnDestro
206206
}
207207

208208
ngOnDestroy() {
209-
this._focusOriginMonitor.unmonitor(this._inputElement.nativeElement);
210-
211-
if (this._focusedSubscription) {
212-
this._focusedSubscription.unsubscribe();
213-
this._focusedSubscription = null;
214-
}
209+
this._focusOriginMonitor.stopMonitoring(this._inputElement.nativeElement);
215210
}
216211

217212
/**

src/lib/core/style/focus-origin-monitor.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ describe('FocusOriginMonitor', () => {
218218
}, 0);
219219
}));
220220

221-
it('should remove classes on unmonitor', async(() => {
221+
it('should remove classes on stopMonitoring', async(() => {
222222
buttonElement.focus();
223223
fixture.detectChanges();
224224

@@ -228,7 +228,7 @@ describe('FocusOriginMonitor', () => {
228228
expect(buttonElement.classList.length)
229229
.toBe(2, 'button should have exactly 2 focus classes');
230230

231-
focusOriginMonitor.unmonitor(buttonElement);
231+
focusOriginMonitor.stopMonitoring(buttonElement);
232232
fixture.detectChanges();
233233

234234
expect(buttonElement.classList.length).toBe(0, 'button should not have any focus classes');

src/lib/core/style/focus-origin-monitor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class FocusOriginMonitor {
101101
* Stops monitoring an element and removes all focus classes.
102102
* @param element The element to stop monitoring.
103103
*/
104-
unmonitor(element: Element): void {
104+
stopMonitoring(element: Element): void {
105105
let elementInfo = this._elementInfo.get(element);
106106

107107
if (elementInfo) {
@@ -296,7 +296,7 @@ export class CdkMonitorFocus implements OnDestroy {
296296
}
297297

298298
ngOnDestroy() {
299-
this._focusOriginMonitor.unmonitor(this._elementRef.nativeElement);
299+
this._focusOriginMonitor.stopMonitoring(this._elementRef.nativeElement);
300300
}
301301
}
302302

src/lib/radio/radio.spec.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,10 @@ import {MdRadioGroup, MdRadioButton, MdRadioChange, MdRadioModule} from './index
66
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
77
import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
88
import {dispatchFakeEvent} from '../core/testing/dispatch-events';
9-
import {FocusOriginMonitor, FocusOrigin} from '../core';
109
import {RIPPLE_FADE_IN_DURATION, RIPPLE_FADE_OUT_DURATION} from '../core/ripple/ripple-renderer';
11-
import {Subject} from 'rxjs/Subject';
1210

1311

1412
describe('MdRadio', () => {
15-
let fakeFocusOriginMonitorStream = new Subject<FocusOrigin>();
16-
let fakeFocusOriginMonitor = {
17-
monitor: () => fakeFocusOriginMonitorStream.asObservable(),
18-
unmonitor: () => {},
19-
focusVia: (element: HTMLElement, renderer: any, origin: FocusOrigin) => {
20-
element.focus();
21-
fakeFocusOriginMonitorStream.next(origin);
22-
}
23-
};
2413

2514
beforeEach(async(() => {
2615
TestBed.configureTestingModule({
@@ -32,8 +21,7 @@ describe('MdRadio', () => {
3221
StandaloneRadioButtons,
3322
],
3423
providers: [
35-
{provide: ViewportRuler, useClass: FakeViewportRuler},
36-
{provide: FocusOriginMonitor, useValue: fakeFocusOriginMonitor}
24+
{provide: ViewportRuler, useClass: FakeViewportRuler}
3725
]
3826
});
3927

@@ -47,6 +35,7 @@ describe('MdRadio', () => {
4735
let radioDebugElements: DebugElement[];
4836
let radioNativeElements: HTMLElement[];
4937
let radioLabelElements: HTMLLabelElement[];
38+
let radioInputElements: HTMLInputElement[];
5039
let groupInstance: MdRadioGroup;
5140
let radioInstances: MdRadioButton[];
5241
let testComponent: RadiosInsideRadioGroup;
@@ -67,6 +56,8 @@ describe('MdRadio', () => {
6756

6857
radioLabelElements = radioDebugElements
6958
.map(debugEl => debugEl.query(By.css('label')).nativeElement);
59+
radioInputElements = radioDebugElements
60+
.map(debugEl => debugEl.query(By.css('input')).nativeElement);
7061
}));
7162

7263
it('should set individual radio names based on the group name', () => {
@@ -140,9 +131,7 @@ describe('MdRadio', () => {
140131
});
141132

142133
it('should check a radio upon interaction with the underlying native radio button', () => {
143-
let nativeRadioInput = <HTMLElement> radioNativeElements[0].querySelector('input');
144-
145-
nativeRadioInput.click();
134+
radioInputElements[0].click();
146135
fixture.detectChanges();
147136

148137
expect(radioInstances[0].checked).toBe(true);
@@ -194,13 +183,15 @@ describe('MdRadio', () => {
194183
expect(radioNativeElements[0].querySelectorAll('.mat-ripple-element').length)
195184
.toBe(0, 'Expected no ripples on init.');
196185

197-
fakeFocusOriginMonitorStream.next('keyboard');
186+
dispatchFakeEvent(radioInputElements[0], 'keydown');
187+
dispatchFakeEvent(radioInputElements[0], 'focus');
188+
198189
tick(RIPPLE_FADE_IN_DURATION);
199190

200191
expect(radioNativeElements[0].querySelectorAll('.mat-ripple-element').length)
201192
.toBe(1, 'Expected one ripple after keyboard focus.');
202193

203-
dispatchFakeEvent(radioNativeElements[0].querySelector('input'), 'blur');
194+
dispatchFakeEvent(radioInputElements[0], 'blur');
204195
tick(RIPPLE_FADE_OUT_DURATION);
205196

206197
expect(radioNativeElements[0].querySelectorAll('.mat-ripple-element').length)

src/lib/radio/radio.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,7 @@ export class MdRadioButton implements OnInit, AfterViewInit, OnDestroy {
456456
}
457457

458458
ngOnDestroy() {
459-
this._focusOriginMonitor.unmonitor(this._inputElement.nativeElement);
460-
461-
if (this._focusOriginMonitorSubscription) {
462-
this._focusOriginMonitorSubscription.unsubscribe();
463-
this._focusOriginMonitorSubscription = null;
464-
}
459+
this._focusOriginMonitor.stopMonitoring(this._inputElement.nativeElement);
465460
}
466461

467462
/** Dispatch change event with current value. */

src/lib/slider/slider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ export class MdSlider implements ControlValueAccessor, OnDestroy {
385385
}
386386

387387
ngOnDestroy() {
388-
this._focusOriginMonitor.unmonitor(this._elementRef.nativeElement);
388+
this._focusOriginMonitor.stopMonitoring(this._elementRef.nativeElement);
389389
}
390390

391391
_onMouseenter() {

0 commit comments

Comments
 (0)