Skip to content

Commit

Permalink
Merge pull request #23 from PolymerElements/enable-scroll-listener
Browse files Browse the repository at this point in the history
Add method to install/uninstall the event listener
  • Loading branch information
Emmanuel Garcia authored Sep 17, 2016
2 parents 4c66bc9 + dff0230 commit 37d46ae
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
34 changes: 29 additions & 5 deletions iron-scroll-target-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@
'_scrollTargetChanged(scrollTarget, isAttached)'
],

/**
* True if the event listener should be installed.
*/
_shouldHaveListener: true,

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

if (this._oldScrollTarget) {
eventTarget = this._oldScrollTarget === this._doc ? window : this._oldScrollTarget;
eventTarget.removeEventListener('scroll', this._boundScrollHandler);
this._enableScrollListener(false, this._oldScrollTarget);
this._oldScrollTarget = null;
}

Expand All @@ -97,11 +101,10 @@

} else if (this._isValidScrollTarget()) {

eventTarget = scrollTarget === this._doc ? window : scrollTarget;
this._boundScrollHandler = this._boundScrollHandler || this._scrollHandler.bind(this);
this._oldScrollTarget = scrollTarget;
this._enableScrollListener(hasListener, scrollTarget);

eventTarget.addEventListener('scroll', this._boundScrollHandler);
}
},

Expand Down Expand Up @@ -228,7 +231,28 @@
*/
_isValidScrollTarget: function() {
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) {
if (!this._boundScrollHandler) {
return;
}
var eventTarget = scrollTarget === this._doc ? window : scrollTarget;

if (yes) {
eventTarget.addEventListener('scroll', this._boundScrollHandler);
} else {
eventTarget.removeEventListener('scroll', this._boundScrollHandler);
}
this._shouldHaveListener = yes;
}

};

</script>
19 changes: 19 additions & 0 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@
done();
});
});

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

suite('scrolling region', function() {
Expand Down

0 comments on commit 37d46ae

Please sign in to comment.