debian-mirror-gitlab/app/assets/javascripts/pager.js

79 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import '~/lib/utils/common_utils';
import '~/lib/utils/url_utility';
2017-08-17 22:00:37 +05:30
(() => {
const ENDLESS_SCROLL_BOTTOM_PX = 400;
const ENDLESS_SCROLL_FIRE_DELAY_MS = 1000;
const Pager = {
2017-09-10 17:25:29 +05:30
init(limit = 0, preload = false, disable = false, prepareData = $.noop, callback = $.noop) {
2017-08-17 22:00:37 +05:30
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;
2017-09-10 17:25:29 +05:30
this.prepareData = prepareData;
2017-08-17 22:00:37 +05:30
this.callback = callback;
2016-09-13 17:45:13 +05:30
this.loading = $('.loading').first();
if (preload) {
this.offset = 0;
this.getOld();
}
2017-08-17 22:00:37 +05:30
this.initLoadMore();
2016-09-13 17:45:13 +05:30
},
2017-08-17 22:00:37 +05:30
getOld() {
2016-09-13 17:45:13 +05:30
this.loading.show();
2017-08-17 22:00:37 +05:30
$.ajax({
type: 'GET',
url: this.url,
data: `limit=${this.limit}&offset=${this.offset}`,
dataType: 'json',
error: () => this.loading.hide(),
success: (data) => {
2017-09-10 17:25:29 +05:30
this.append(data.count, this.prepareData(data.html));
2017-08-17 22:00:37 +05:30
this.callback();
// keep loading until we've filled the viewport height
if (!this.disable && !this.isScrollable()) {
this.getOld();
} else {
this.loading.hide();
}
2016-09-13 17:45:13 +05:30
},
});
},
2017-08-17 22:00:37 +05:30
append(count, html) {
$('.content_list').append(html);
2016-09-13 17:45:13 +05:30
if (count > 0) {
2017-08-17 22:00:37 +05:30
this.offset += count;
2016-09-13 17:45:13 +05:30
} else {
2017-08-17 22:00:37 +05:30
this.disable = true;
2016-09-13 17:45:13 +05:30
}
},
2017-08-17 22:00:37 +05:30
isScrollable() {
const $w = $(window);
return $(document).height() > $w.height() + $w.scrollTop() + ENDLESS_SCROLL_BOTTOM_PX;
},
initLoadMore() {
2016-09-13 17:45:13 +05:30
$(document).unbind('scroll');
2017-08-17 22:00:37 +05:30
$(document).endlessScroll({
bottomPixels: ENDLESS_SCROLL_BOTTOM_PX,
fireDelay: ENDLESS_SCROLL_FIRE_DELAY_MS,
2016-09-13 17:45:13 +05:30
fireOnce: true,
2017-08-17 22:00:37 +05:30
ceaseFire: () => this.disable === true,
callback: () => {
if (!this.loading.is(':visible')) {
this.loading.show();
this.getOld();
}
2016-09-13 17:45:13 +05:30
},
});
2017-08-17 22:00:37 +05:30
},
2016-09-13 17:45:13 +05:30
};
2017-08-17 22:00:37 +05:30
window.Pager = Pager;
})();