2018-03-17 18:26:18 +05:30
|
|
|
import { getParameterByName } from '~/lib/utils/common_utils';
|
|
|
|
import axios from './lib/utils/axios_utils';
|
|
|
|
import { removeParams } from './lib/utils/url_utility';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
const ENDLESS_SCROLL_BOTTOM_PX = 400;
|
|
|
|
const ENDLESS_SCROLL_FIRE_DELAY_MS = 1000;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
export default {
|
|
|
|
init(limit = 0, preload = false, disable = false, prepareData = $.noop, callback = $.noop) {
|
|
|
|
this.url = $('.content_list').data('href') || removeParams(['limit', 'offset']);
|
|
|
|
this.limit = limit;
|
|
|
|
this.offset = parseInt(getParameterByName('offset'), 10) || this.limit;
|
|
|
|
this.disable = disable;
|
|
|
|
this.prepareData = prepareData;
|
|
|
|
this.callback = callback;
|
|
|
|
this.loading = $('.loading').first();
|
|
|
|
if (preload) {
|
|
|
|
this.offset = 0;
|
|
|
|
this.getOld();
|
|
|
|
}
|
|
|
|
this.initLoadMore();
|
|
|
|
},
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
getOld() {
|
|
|
|
this.loading.show();
|
|
|
|
axios.get(this.url, {
|
|
|
|
params: {
|
|
|
|
limit: this.limit,
|
|
|
|
offset: this.offset,
|
|
|
|
},
|
|
|
|
}).then(({ data }) => {
|
|
|
|
this.append(data.count, this.prepareData(data.html));
|
|
|
|
this.callback();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
// keep loading until we've filled the viewport height
|
|
|
|
if (!this.disable && !this.isScrollable()) {
|
|
|
|
this.getOld();
|
2016-09-13 17:45:13 +05:30
|
|
|
} else {
|
2018-03-17 18:26:18 +05:30
|
|
|
this.loading.hide();
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
}).catch(() => this.loading.hide());
|
|
|
|
},
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
append(count, html) {
|
|
|
|
$('.content_list').append(html);
|
|
|
|
if (count > 0) {
|
|
|
|
this.offset += count;
|
|
|
|
} else {
|
|
|
|
this.disable = true;
|
|
|
|
}
|
|
|
|
},
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
isScrollable() {
|
|
|
|
const $w = $(window);
|
|
|
|
return $(document).height() > $w.height() + $w.scrollTop() + ENDLESS_SCROLL_BOTTOM_PX;
|
|
|
|
},
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
initLoadMore() {
|
2018-03-27 19:54:05 +05:30
|
|
|
$(document).off('scroll');
|
2018-03-17 18:26:18 +05:30
|
|
|
$(document).endlessScroll({
|
|
|
|
bottomPixels: ENDLESS_SCROLL_BOTTOM_PX,
|
|
|
|
fireDelay: ENDLESS_SCROLL_FIRE_DELAY_MS,
|
|
|
|
fireOnce: true,
|
|
|
|
ceaseFire: () => this.disable === true,
|
|
|
|
callback: () => {
|
|
|
|
if (!this.loading.is(':visible')) {
|
|
|
|
this.loading.show();
|
|
|
|
this.getOld();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|