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

79 lines
2.1 KiB
JavaScript
Raw Normal View History

import '~/lib/utils/common_utils';
import '~/lib/utils/url_utility';
2016-11-20 01:11:30 -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 = {
init(limit = 0, preload = false, disable = false, prepareData = $.noop, callback = $.noop) {
this.url = $('.content_list').data('href') || gl.utils.removeParams(['limit', 'offset']);
this.limit = limit;
this.offset = parseInt(gl.utils.getParameterByName('offset'), 10) || this.limit;
this.disable = disable;
this.prepareData = prepareData;
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: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',
url: this.url,
2016-11-20 01:11:30 -05:00
data: `limit=${this.limit}&offset=${this.offset}`,
dataType: 'json',
error: () => this.loading.hide(),
2016-11-20 01:11:30 -05:00
success: (data) => {
this.append(data.count, this.prepareData(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 16:45:11 -04: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
}
},
isScrollable() {
const $w = $(window);
return $(document).height() > $w.height() + $w.scrollTop() + ENDLESS_SCROLL_BOTTOM_PX;
},
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({
bottomPixels: ENDLESS_SCROLL_BOTTOM_PX,
fireDelay: ENDLESS_SCROLL_FIRE_DELAY_MS,
2016-07-24 16:45:11 -04:00
fireOnce: true,
ceaseFire: () => this.disable === true,
2016-11-20 01:11:30 -05:00
callback: () => {
if (!this.loading.is(':visible')) {
this.loading.show();
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;
})();