Skip to content

Commit

Permalink
Merge pull request #24 from PolymerElements/enable-scroll-listener
Browse files Browse the repository at this point in the history
Fix _shouldHaveListener flag
  • Loading branch information
Emmanuel Garcia authored Sep 17, 2016
2 parents 590a515 + 38a20d0 commit 877269f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
24 changes: 13 additions & 11 deletions iron-scroll-target-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,12 @@
_shouldHaveListener: true,

_scrollTargetChanged: function(scrollTarget, isAttached) {
var eventTarget, hasListener = this._shouldHaveListener;
var eventTarget;

if (this._oldScrollTarget) {
this._enableScrollListener(false, this._oldScrollTarget);
this._toggleScrollListener(false, this._oldScrollTarget);
this._oldScrollTarget = null;
}

if (!isAttached) {
return;
}
Expand All @@ -103,7 +102,7 @@

this._boundScrollHandler = this._boundScrollHandler || this._scrollHandler.bind(this);
this._oldScrollTarget = scrollTarget;
this._enableScrollListener(hasListener, scrollTarget);
this._toggleScrollListener(this._shouldHaveListener, scrollTarget);

}
},
Expand Down Expand Up @@ -233,13 +232,7 @@
return this.scrollTarget instanceof HTMLElement;
},

/**
* Enables or disables the scroll event listener
*
* @param {boolean} yes True to add the event, False to remove it
* @param {HTMLElement} scrollTarget The scroll target
*/
_enableScrollListener: function(yes, scrollTarget) {
_toggleScrollListener: function(yes, scrollTarget) {
if (!this._boundScrollHandler) {
return;
}
Expand All @@ -250,7 +243,16 @@
} else {
eventTarget.removeEventListener('scroll', this._boundScrollHandler);
}
},

/**
* Enables or disables the scroll event listener.
*
* @param {boolean} yes True to add the event, False to remove it.
*/
toggleScrollListener: function(yes) {
this._shouldHaveListener = yes;
this._toggleScrollListener(yes, this.scrollTarget);
}

};
Expand Down
6 changes: 3 additions & 3 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@
});
});

test('_enableScrollListener', function(done) {
test('toggleScrollListener', function(done) {
flush(function() {
xScroll._scrollHandler = sinon.spy();
xScroll.scrollTarget = 'temporaryScrollingRegion';
xScroll.scroll(0, 100);
xScroll._enableScrollListener(true, xScroll.scrollTarget);
xScroll.toggleScrollListener(true);
setTimeout(function() {
assert.isTrue(xScroll._scrollHandler.called, '_scrollHandler should be called');
xScroll._scrollHandler.reset();
xScroll._enableScrollListener(false, xScroll.scrollTarget);
xScroll.toggleScrollListener(false);
xScroll.scroll(0, 200);
setTimeout(function() {
assert.isFalse(xScroll._scrollHandler.called, '_scrollHandler should not be called');
Expand Down

0 comments on commit 877269f

Please sign in to comment.