Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
WIP: Revert incorrect deletion of initScrollHandler() and `destroyS…
Browse files Browse the repository at this point in the history
…crollHandler()`
  • Loading branch information
acdvorak committed Feb 14, 2019
1 parent d3e87cb commit e3e9023
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
28 changes: 22 additions & 6 deletions packages/mdc-top-app-bar/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,39 @@ class MDCTopAppBarBaseFoundation extends MDCFoundation<MDCTopAppBarAdapter> {
}

init() {
this.initScrollHandler();
this.initResizeHandler_();
this.adapter_.registerNavigationIconInteractionHandler('click', this.navClickHandler_);
}

destroy() {
this.destroyScrollHandler();
this.destroyResizeHandler_();
this.adapter_.deregisterNavigationIconInteractionHandler('click', this.navClickHandler_);
}

initScrollHandler() {
if (this.scrollHandler_) {
this.adapter_.registerScrollHandler(this.scrollHandler_);
}
if (this.resizeHandler_) {
this.adapter_.registerResizeHandler(this.resizeHandler_);
}
this.adapter_.registerNavigationIconInteractionHandler('click', this.navClickHandler_);
}

destroy() {
destroyScrollHandler() {
if (this.scrollHandler_) {
this.adapter_.deregisterScrollHandler(this.scrollHandler_);
}
}

private initResizeHandler_() {
if (this.resizeHandler_) {
this.adapter_.registerResizeHandler(this.resizeHandler_);
}
}

private destroyResizeHandler_() {
if (this.resizeHandler_) {
this.adapter_.deregisterResizeHandler(this.resizeHandler_);
}
this.adapter_.deregisterNavigationIconInteractionHandler('click', this.navClickHandler_);
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/mdc-top-app-bar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ class MDCTopAppBar extends MDCComponent<MDCTopAppBarBaseFoundation> {
}

setScrollTarget(target: EventTarget) {
// Remove scroll handler from the previous scroll target
this.foundation_.destroyScrollHandler();

this.scrollTarget_ = target;

// Initialize scroll handler on the new scroll target
this.foundation_.initScrollHandler();
}

getDefaultFoundation(): MDCTopAppBarBaseFoundation {
Expand Down
4 changes: 0 additions & 4 deletions packages/mdc-top-app-bar/standard/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ class MDCTopAppBarFoundation extends MDCTopAppBarBaseFoundation {
this.resizeHandler_ = () => this.topAppBarResizeHandler_();
}

init() {
super.init();
}

destroy() {
super.destroy();
this.adapter_.setStyle('top', '');
Expand Down
14 changes: 14 additions & 0 deletions test/unit/mdc-top-app-bar/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,17 @@ test('click handler removed from navigation icon during destroy', () => {
foundation.destroy();
td.verify(mockAdapter.deregisterNavigationIconInteractionHandler('click', td.matchers.isA(Function)));
});

test('#initScrollHandler calls registerScrollHandler', () => {
const {foundation, mockAdapter} = setupTest();
foundation.scrollHandler_ = td.func();
foundation.initScrollHandler();
td.verify(mockAdapter.registerScrollHandler(foundation.scrollHandler_), {times: 1});
});

test('#destroyScrollHandler calls deregisterScrollHandler', () => {
const {foundation, mockAdapter} = setupTest();
foundation.scrollHandler_ = td.func();
foundation.destroyScrollHandler();
td.verify(mockAdapter.deregisterScrollHandler(foundation.scrollHandler_), {times: 1});
});
19 changes: 19 additions & 0 deletions test/unit/mdc-top-app-bar/mdc-top-app-bar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ test('destroy destroys icon ripples', () => {
});
});

test('#setScrollTarget deregisters and registers scroll handler on provided target', () => {
const {component} = setupTest();
const fakeTarget1 = document.createElement('div');
const fakeTarget2 = document.createElement('div');

component.setScrollTarget(fakeTarget1);
assert.equal(component.scrollTarget_, fakeTarget1);

component.foundation_.destroyScrollHandler = td.func();
component.foundation_.initScrollHandler = td.func();

component.setScrollTarget(fakeTarget2);

td.verify(component.foundation_.destroyScrollHandler(), {times: 1});
td.verify(component.foundation_.initScrollHandler(), {times: 1});

assert.equal(component.scrollTarget_, fakeTarget2);
});

test('getDefaultFoundation returns the appropriate foundation for default', () => {
const fixture = getFixture();
const root = fixture.querySelector(strings.ROOT_SELECTOR);
Expand Down

0 comments on commit e3e9023

Please sign in to comment.