Skip to content

Commit

Permalink
addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba committed Feb 8, 2017
1 parent f5486a8 commit f9155be
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
44 changes: 44 additions & 0 deletions src/lib/core/style/focus-classes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ describe('FocusOriginMonitor', () => {
expect(changeHandler).toHaveBeenCalledWith('program');
}, 0);
}));

it('should remove focus classes on blur', async(() => {
if (platform.FIREFOX) { return; }

buttonElement.focus();
fixture.detectChanges();

setTimeout(() => {
fixture.detectChanges();

expect(buttonElement.classList.length)
.toBe(2, 'button should have exactly 2 focus classes');
expect(changeHandler).toHaveBeenCalledWith('program');

buttonElement.blur();
fixture.detectChanges();

expect(buttonElement.classList.length)
.toBe(0, 'button should not have any focus classes');
expect(changeHandler).toHaveBeenCalledWith(null);
}, 0);
}));
});


Expand Down Expand Up @@ -275,6 +297,28 @@ describe('cdkFocusClasses', () => {
expect(changeHandler).toHaveBeenCalledWith('program');
}, 0);
}));

it('should remove focus classes on blur', async(() => {
if (platform.FIREFOX) { return; }

buttonElement.focus();
fixture.detectChanges();

setTimeout(() => {
fixture.detectChanges();

expect(buttonElement.classList.length)
.toBe(2, 'button should have exactly 2 focus classes');
expect(changeHandler).toHaveBeenCalledWith('program');

buttonElement.blur();
fixture.detectChanges();

expect(buttonElement.classList.length)
.toBe(0, 'button should not have any focus classes');
expect(changeHandler).toHaveBeenCalledWith(null);
}, 0);
}));
});


Expand Down
10 changes: 5 additions & 5 deletions src/lib/core/style/focus-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export class FocusOriginMonitor {
/** The focus origin that the next focus event is a result of. */
private _origin: FocusOrigin = null;

/** A WeakMap used to track the last element focused via the FocusOriginMonitor. */
private _lastFocused = new WeakMap<Element, FocusOrigin>();
/** The FocusOrigin of the last focus event tracked by the FocusOriginMonitor. */
private _lastFocusOrigin: FocusOrigin;

/** Whether the window has just been focused. */
private _windowFocused = false;
Expand Down Expand Up @@ -63,8 +63,8 @@ export class FocusOriginMonitor {
// 2) The element was programmatically focused, in which case we should mark the origin as
// 'program'.
if (!this._origin) {
if (this._windowFocused && this._lastFocused.has(element)) {
this._origin = this._lastFocused.get(element);
if (this._windowFocused && this._lastFocusOrigin) {
this._origin = this._lastFocusOrigin;
} else {
this._origin = 'program';
}
Expand All @@ -76,7 +76,7 @@ export class FocusOriginMonitor {
renderer.setElementClass(element, 'cdk-program-focused', this._origin == 'program');

subject.next(this._origin);
this._lastFocused = new WeakMap().set(element, this._origin);
this._lastFocusOrigin = this._origin;
this._origin = null;
}

Expand Down

0 comments on commit f9155be

Please sign in to comment.