2015-06-13 09:34:09 -05:00
|
|
|
import Ember from 'ember';
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
const {Mixin, run} = Ember;
|
|
|
|
|
|
|
|
export default Mixin.create({
|
2015-06-13 09:34:09 -05:00
|
|
|
isLoading: false,
|
|
|
|
triggerPoint: 100,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if we are past a scroll point where we need to fetch the next page
|
|
|
|
* @param {object} event The scroll event
|
|
|
|
*/
|
2015-10-28 11:36:45 +00:00
|
|
|
checkScroll(event) {
|
|
|
|
let element = event.target;
|
|
|
|
let triggerPoint = this.get('triggerPoint');
|
|
|
|
let isLoading = this.get('isLoading');
|
2015-06-13 09:34:09 -05:00
|
|
|
|
|
|
|
// If we haven't passed our threshold or we are already fetching content, exit
|
|
|
|
if (isLoading || (element.scrollTop + element.clientHeight + triggerPoint <= element.scrollHeight)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sendAction('fetch');
|
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
didInsertElement() {
|
|
|
|
let el = this.get('element');
|
2015-06-13 09:34:09 -05:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
el.onscroll = run.bind(this, this.checkScroll);
|
2015-06-13 09:34:09 -05:00
|
|
|
|
|
|
|
if (el.scrollHeight <= el.clientHeight) {
|
|
|
|
this.sendAction('fetch');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
willDestroyElement() {
|
2015-06-13 09:34:09 -05:00
|
|
|
// turn off the scroll handler
|
|
|
|
this.get('element').onscroll = null;
|
|
|
|
}
|
|
|
|
});
|