gitlab-org--gitlab-foss/app/assets/javascripts/pager.js.es6

74 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-11-20 06:11:30 +00:00
(() => {
const ENDLESS_SCROLL_BOTTOM_PX = 400;
const ENDLESS_SCROLL_FIRE_DELAY_MS = 1000;
2016-11-20 06:11:30 +00:00
const Pager = {
init(limit = 0, preload = false, disable = false, callback = $.noop) {
this.limit = limit;
this.offset = this.limit;
this.disable = disable;
this.callback = callback;
2016-07-24 20:45:11 +00:00
this.loading = $('.loading').first();
if (preload) {
this.offset = 0;
this.getOld();
}
2016-11-20 06:11:30 +00:00
this.initLoadMore();
2016-07-24 20:45:11 +00:00
},
2016-11-20 06:11:30 +00:00
getOld() {
2016-07-24 20:45:11 +00:00
this.loading.show();
2016-11-20 06:11:30 +00:00
$.ajax({
type: 'GET',
url: $('.content_list').data('href') || window.location.href,
data: `limit=${this.limit}&offset=${this.offset}`,
dataType: 'json',
error: () => this.loading.hide(),
2016-11-20 06:11:30 +00:00
success: (data) => {
this.append(data.count, data.html);
this.callback();
// keep loading until we've filled the viewport height
if (!this.disable && !this.isScrollable()) {
this.getOld();
} else {
this.loading.hide();
}
2016-07-24 20:45:11 +00:00
},
});
},
2016-11-20 06:11:30 +00:00
append(count, html) {
$('.content_list').append(html);
2016-07-24 20:45:11 +00:00
if (count > 0) {
2016-11-20 06:11:30 +00:00
this.offset += count;
2016-07-24 20:45:11 +00:00
} else {
2016-11-20 06:11:30 +00:00
this.disable = true;
2016-07-24 20:45:11 +00:00
}
},
isScrollable() {
const $w = $(window);
return $(document).height() > $w.height() + $w.scrollTop() + ENDLESS_SCROLL_BOTTOM_PX;
},
2016-11-20 06:11:30 +00:00
initLoadMore() {
2016-07-24 20:45:11 +00:00
$(document).unbind('scroll');
2016-11-20 06:11:30 +00:00
$(document).endlessScroll({
bottomPixels: ENDLESS_SCROLL_BOTTOM_PX,
fireDelay: ENDLESS_SCROLL_FIRE_DELAY_MS,
2016-07-24 20:45:11 +00:00
fireOnce: true,
ceaseFire: () => this.disable === true,
2016-11-20 06:11:30 +00:00
callback: () => {
if (!this.loading.is(':visible')) {
this.loading.show();
this.getOld();
2016-11-20 06:11:30 +00:00
}
2016-07-24 20:45:11 +00:00
},
});
2016-11-20 06:11:30 +00:00
},
2016-07-24 20:45:11 +00:00
};
2016-11-20 06:11:30 +00:00
window.Pager = Pager;
})();