2017-05-16 16:52:40 -04:00
|
|
|
import '~/lib/utils/common_utils';
|
|
|
|
import '~/lib/utils/url_utility';
|
2017-02-26 21:05:50 -05:00
|
|
|
|
2016-11-20 01:11:30 -05:00
|
|
|
(() => {
|
2016-11-21 11:17:11 -05:00
|
|
|
const ENDLESS_SCROLL_BOTTOM_PX = 400;
|
|
|
|
const ENDLESS_SCROLL_FIRE_DELAY_MS = 1000;
|
|
|
|
|
2016-11-20 01:11:30 -05:00
|
|
|
const Pager = {
|
2017-06-02 21:40:20 -04:00
|
|
|
init(limit = 0, preload = false, disable = false, prepareData = $.noop, callback = $.noop) {
|
2017-02-26 21:05:50 -05:00
|
|
|
this.url = $('.content_list').data('href') || gl.utils.removeParams(['limit', 'offset']);
|
2016-11-20 01:23:12 -05:00
|
|
|
this.limit = limit;
|
2017-02-26 21:05:50 -05:00
|
|
|
this.offset = parseInt(gl.utils.getParameterByName('offset'), 10) || this.limit;
|
2016-11-20 01:23:12 -05:00
|
|
|
this.disable = disable;
|
2017-06-02 21:40:20 -04:00
|
|
|
this.prepareData = prepareData;
|
2016-11-20 01:23:12 -05:00
|
|
|
this.callback = callback;
|
2016-07-24 16:45:11 -04:00
|
|
|
this.loading = $('.loading').first();
|
|
|
|
if (preload) {
|
|
|
|
this.offset = 0;
|
|
|
|
this.getOld();
|
|
|
|
}
|
2016-11-20 01:11:30 -05:00
|
|
|
this.initLoadMore();
|
2016-07-24 16:45:11 -04:00
|
|
|
},
|
2016-11-20 01:23:12 -05:00
|
|
|
|
2016-11-20 01:11:30 -05:00
|
|
|
getOld() {
|
2016-07-24 16:45:11 -04:00
|
|
|
this.loading.show();
|
2016-11-20 01:11:30 -05:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2017-02-26 21:05:50 -05:00
|
|
|
url: this.url,
|
2016-11-20 01:11:30 -05:00
|
|
|
data: `limit=${this.limit}&offset=${this.offset}`,
|
2016-11-21 11:17:11 -05:00
|
|
|
dataType: 'json',
|
2016-11-20 02:02:33 -05:00
|
|
|
error: () => this.loading.hide(),
|
2016-11-20 01:11:30 -05:00
|
|
|
success: (data) => {
|
2017-06-02 21:40:20 -04:00
|
|
|
this.append(data.count, this.prepareData(data.html));
|
2016-11-20 02:02:33 -05:00
|
|
|
this.callback();
|
|
|
|
|
|
|
|
// keep loading until we've filled the viewport height
|
2016-11-21 11:17:11 -05:00
|
|
|
if (!this.disable && !this.isScrollable()) {
|
2016-11-20 02:02:33 -05:00
|
|
|
this.getOld();
|
|
|
|
} else {
|
|
|
|
this.loading.hide();
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2016-11-20 01:23:12 -05:00
|
|
|
|
2016-11-20 01:11:30 -05:00
|
|
|
append(count, html) {
|
|
|
|
$('.content_list').append(html);
|
2016-07-24 16:45:11 -04:00
|
|
|
if (count > 0) {
|
2016-11-20 01:11:30 -05:00
|
|
|
this.offset += count;
|
2016-07-24 16:45:11 -04:00
|
|
|
} else {
|
2016-11-20 01:11:30 -05:00
|
|
|
this.disable = true;
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
},
|
2016-11-20 01:23:12 -05:00
|
|
|
|
2016-11-20 02:02:33 -05:00
|
|
|
isScrollable() {
|
2016-11-21 11:17:11 -05:00
|
|
|
const $w = $(window);
|
|
|
|
return $(document).height() > $w.height() + $w.scrollTop() + ENDLESS_SCROLL_BOTTOM_PX;
|
2016-11-20 02:02:33 -05:00
|
|
|
},
|
|
|
|
|
2016-11-20 01:11:30 -05:00
|
|
|
initLoadMore() {
|
2016-07-24 16:45:11 -04:00
|
|
|
$(document).unbind('scroll');
|
2016-11-20 01:11:30 -05:00
|
|
|
$(document).endlessScroll({
|
2016-11-21 11:17:11 -05:00
|
|
|
bottomPixels: ENDLESS_SCROLL_BOTTOM_PX,
|
|
|
|
fireDelay: ENDLESS_SCROLL_FIRE_DELAY_MS,
|
2016-07-24 16:45:11 -04:00
|
|
|
fireOnce: true,
|
2016-11-20 02:02:33 -05:00
|
|
|
ceaseFire: () => this.disable === true,
|
2016-11-20 01:11:30 -05:00
|
|
|
callback: () => {
|
|
|
|
if (!this.loading.is(':visible')) {
|
|
|
|
this.loading.show();
|
2016-11-20 02:02:33 -05:00
|
|
|
this.getOld();
|
2016-11-20 01:11:30 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
},
|
|
|
|
});
|
2016-11-20 01:11:30 -05:00
|
|
|
},
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
|
2016-11-20 01:11:30 -05:00
|
|
|
window.Pager = Pager;
|
|
|
|
})();
|