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

57 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-11-20 06:11:30 +00:00
(() => {
const Pager = {
init(limit, preload, disable, callback) {
2016-07-24 20:45:11 +00:00
this.limit = limit != null ? limit : 0;
this.disable = disable != null ? disable : false;
this.callback = callback != null ? callback : $.noop;
this.loading = $('.loading').first();
if (preload) {
this.offset = 0;
this.getOld();
} else {
this.offset = this.limit;
}
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}`,
complete: () => this.loading.hide(),
success: (data) => {
2016-07-24 20:45:11 +00:00
Pager.append(data.count, data.html);
2016-11-20 06:11:30 +00:00
Pager.callback();
2016-07-24 20:45:11 +00:00
},
2016-11-20 06:11:30 +00:00
dataType: 'json',
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
}
},
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({
2016-07-24 20:45:11 +00:00
bottomPixels: 400,
fireDelay: 1000,
fireOnce: true,
2016-11-20 06:11:30 +00:00
ceaseFire: () => Pager.disable !== true,
callback: () => {
if (!this.loading.is(':visible')) {
this.loading.show();
Pager.getOld();
}
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;
})();