2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2020-06-23 00:09:42 +05:30
|
|
|
import 'vendor/jquery.endless-scroll';
|
2018-03-17 18:26:18 +05:30
|
|
|
import { getParameterByName } from '~/lib/utils/common_utils';
|
2020-06-23 00:09:42 +05:30
|
|
|
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 {
|
2018-12-13 13:39:08 +05:30
|
|
|
init(
|
|
|
|
limit = 0,
|
|
|
|
preload = false,
|
|
|
|
disable = false,
|
|
|
|
prepareData = $.noop,
|
|
|
|
callback = $.noop,
|
|
|
|
container = '',
|
|
|
|
) {
|
2018-03-17 18:26:18 +05:30
|
|
|
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;
|
2018-12-13 13:39:08 +05:30
|
|
|
this.loading = $(`${container} .loading`).first();
|
2018-03-17 18:26:18 +05:30
|
|
|
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();
|
2018-12-13 13:39:08 +05:30
|
|
|
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-12-13 13:39:08 +05:30
|
|
|
// keep loading until we've filled the viewport height
|
|
|
|
if (!this.disable && !this.isScrollable()) {
|
|
|
|
this.getOld();
|
|
|
|
} else {
|
|
|
|
this.loading.hide();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => this.loading.hide());
|
2018-03-17 18:26:18 +05:30
|
|
|
},
|
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;
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
if (count < this.limit) {
|
|
|
|
this.disable = true;
|
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
} 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() {
|
2021-02-22 17:27:13 +05:30
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|