2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-09-06 12:14:34 -04:00
|
|
|
import { getParameterByName } from '~/lib/utils/common_utils';
|
2018-02-01 06:12:37 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-12-11 07:28:11 -05:00
|
|
|
import { removeParams } from './lib/utils/url_utility';
|
2017-02-26 21:05:50 -05:00
|
|
|
|
2017-12-15 04:31:58 -05:00
|
|
|
const ENDLESS_SCROLL_BOTTOM_PX = 400;
|
|
|
|
const ENDLESS_SCROLL_FIRE_DELAY_MS = 1000;
|
2016-11-21 11:17:11 -05:00
|
|
|
|
2017-12-15 04:31:58 -05:00
|
|
|
export default {
|
2018-10-24 02:25:46 -04:00
|
|
|
init(
|
|
|
|
limit = 0,
|
|
|
|
preload = false,
|
|
|
|
disable = false,
|
|
|
|
prepareData = $.noop,
|
|
|
|
callback = $.noop,
|
|
|
|
container = '',
|
|
|
|
) {
|
2017-12-15 04:31:58 -05:00
|
|
|
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-10-24 02:25:46 -04:00
|
|
|
this.loading = $(`${container} .loading`).first();
|
2017-12-15 04:31:58 -05:00
|
|
|
if (preload) {
|
|
|
|
this.offset = 0;
|
|
|
|
this.getOld();
|
|
|
|
}
|
|
|
|
this.initLoadMore();
|
|
|
|
},
|
2016-11-20 01:23:12 -05:00
|
|
|
|
2017-12-15 04:31:58 -05:00
|
|
|
getOld() {
|
|
|
|
this.loading.show();
|
2018-10-10 03:13:34 -04:00
|
|
|
axios
|
|
|
|
.get(this.url, {
|
|
|
|
params: {
|
|
|
|
limit: this.limit,
|
|
|
|
offset: this.offset,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
this.append(data.count, this.prepareData(data.html));
|
|
|
|
this.callback();
|
2018-02-01 06:12:37 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
// keep loading until we've filled the viewport height
|
|
|
|
if (!this.disable && !this.isScrollable()) {
|
|
|
|
this.getOld();
|
|
|
|
} else {
|
|
|
|
this.loading.hide();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => this.loading.hide());
|
2017-12-15 04:31:58 -05:00
|
|
|
},
|
2016-11-20 01:23:12 -05:00
|
|
|
|
2017-12-15 04:31:58 -05:00
|
|
|
append(count, html) {
|
|
|
|
$('.content_list').append(html);
|
|
|
|
if (count > 0) {
|
|
|
|
this.offset += count;
|
|
|
|
} else {
|
|
|
|
this.disable = true;
|
|
|
|
}
|
|
|
|
},
|
2016-11-20 01:23:12 -05:00
|
|
|
|
2017-12-15 04:31:58 -05:00
|
|
|
isScrollable() {
|
|
|
|
const $w = $(window);
|
|
|
|
return $(document).height() > $w.height() + $w.scrollTop() + ENDLESS_SCROLL_BOTTOM_PX;
|
|
|
|
},
|
2016-11-20 02:02:33 -05:00
|
|
|
|
2017-12-15 04:31:58 -05:00
|
|
|
initLoadMore() {
|
2018-02-20 17:20:48 -05:00
|
|
|
$(document).off('scroll');
|
2017-12-15 04:31:58 -05:00
|
|
|
$(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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|