Skip to content

Commit dff0230

Browse files
author
Emmanuel Garcia
committed
Add method to install/uninstall the event listener
1 parent 4c66bc9 commit dff0230

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

iron-scroll-target-behavior.html

+29-5
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@
7373
'_scrollTargetChanged(scrollTarget, isAttached)'
7474
],
7575

76+
/**
77+
* True if the event listener should be installed.
78+
*/
79+
_shouldHaveListener: true,
80+
7681
_scrollTargetChanged: function(scrollTarget, isAttached) {
77-
var eventTarget;
82+
var eventTarget, hasListener = this._shouldHaveListener;
7883

7984
if (this._oldScrollTarget) {
80-
eventTarget = this._oldScrollTarget === this._doc ? window : this._oldScrollTarget;
81-
eventTarget.removeEventListener('scroll', this._boundScrollHandler);
85+
this._enableScrollListener(false, this._oldScrollTarget);
8286
this._oldScrollTarget = null;
8387
}
8488

@@ -97,11 +101,10 @@
97101

98102
} else if (this._isValidScrollTarget()) {
99103

100-
eventTarget = scrollTarget === this._doc ? window : scrollTarget;
101104
this._boundScrollHandler = this._boundScrollHandler || this._scrollHandler.bind(this);
102105
this._oldScrollTarget = scrollTarget;
106+
this._enableScrollListener(hasListener, scrollTarget);
103107

104-
eventTarget.addEventListener('scroll', this._boundScrollHandler);
105108
}
106109
},
107110

@@ -228,7 +231,28 @@
228231
*/
229232
_isValidScrollTarget: function() {
230233
return this.scrollTarget instanceof HTMLElement;
234+
},
235+
236+
/**
237+
* Enables or disables the scroll event listener
238+
*
239+
* @param {boolean} yes True to add the event, False to remove it
240+
* @param {HTMLElement} scrollTarget The scroll target
241+
*/
242+
_enableScrollListener: function(yes, scrollTarget) {
243+
if (!this._boundScrollHandler) {
244+
return;
245+
}
246+
var eventTarget = scrollTarget === this._doc ? window : scrollTarget;
247+
248+
if (yes) {
249+
eventTarget.addEventListener('scroll', this._boundScrollHandler);
250+
} else {
251+
eventTarget.removeEventListener('scroll', this._boundScrollHandler);
252+
}
253+
this._shouldHaveListener = yes;
231254
}
255+
232256
};
233257

234258
</script>

test/basic.html

+19
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@
9494
done();
9595
});
9696
});
97+
98+
test('_enableScrollListener', function(done) {
99+
flush(function() {
100+
xScroll._scrollHandler = sinon.spy();
101+
xScroll.scrollTarget = 'temporaryScrollingRegion';
102+
xScroll.scroll(0, 100);
103+
xScroll._enableScrollListener(true, xScroll.scrollTarget);
104+
setTimeout(function() {
105+
assert.isTrue(xScroll._scrollHandler.called, '_scrollHandler should be called');
106+
xScroll._scrollHandler.reset();
107+
xScroll._enableScrollListener(false, xScroll.scrollTarget);
108+
xScroll.scroll(0, 200);
109+
setTimeout(function() {
110+
assert.isFalse(xScroll._scrollHandler.called, '_scrollHandler should not be called');
111+
done();
112+
}, 100);
113+
}, 100);
114+
});
115+
});
97116
});
98117

99118
suite('scrolling region', function() {

0 commit comments

Comments
 (0)